import (
"errors"
"fmt"
- "html"
"html/template"
"io/ioutil"
"os"
// DescriptionHtml does special handles to description and return HTML string.
func (repo *Repository) DescriptionHtml() template.HTML {
sanitize := func(s string) string {
- // TODO(nuss-justin): Improve sanitization. Strip all tags?
- ss := html.EscapeString(s)
- return fmt.Sprintf(`<a href="%s" target="_blank">%s</a>`, ss, ss)
+ return fmt.Sprintf(`<a href="%[1]s" target="_blank">%[1]s</a>`, s)
}
- return template.HTML(DescPattern.ReplaceAllStringFunc(base.XSSString(repo.Description), sanitize))
+ return template.HTML(DescPattern.ReplaceAllStringFunc(base.Sanitizer.Sanitize(repo.Description), sanitize))
}
// IsRepositoryExist returns true if the repository with given name under user has already existed.
func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
body := RenderSpecialLink(rawBytes, urlPrefix)
body = RenderRawMarkdown(body, urlPrefix)
- body = XSS(body)
+ body = Sanitizer.SanitizeBytes(body)
return body
}
"strings"
"time"
- "github.com/microcosm-cc/bluemonday"
"golang.org/x/net/html/charset"
"golang.org/x/text/transform"
"github.com/gogits/gogs/modules/setting"
)
-// FIXME: use me to Markdown API renders
-var p = bluemonday.UGCPolicy()
-
func Str2html(raw string) template.HTML {
- return template.HTML(p.Sanitize(raw))
+ return template.HTML(Sanitizer.Sanitize(raw))
}
func Range(l int) []int {
"hash"
"html/template"
"math"
- "regexp"
"strings"
"time"
"github.com/Unknwon/com"
"github.com/Unknwon/i18n"
+ "github.com/microcosm-cc/bluemonday"
"github.com/gogits/gogs/modules/avatar"
"github.com/gogits/gogs/modules/setting"
)
+var Sanitizer = bluemonday.UGCPolicy()
+
// Encode string to md5 hex value.
func EncodeMd5(str string) string {
m := md5.New()
format = replacer.Replace(format)
return t.Format(format)
}
-
-type xssFilter struct {
- reg *regexp.Regexp
- repl []byte
-}
-
-var (
- whiteSpace = []byte(" ")
- xssFilters = []xssFilter{
- {regexp.MustCompile(`\ [ONon]\w*=["]*`), whiteSpace},
- {regexp.MustCompile(`<[SCRIPTscript]{6}`), whiteSpace},
- {regexp.MustCompile(`=[` + "`" + `'"]*[JAVASCRIPTjavascript \t\0
]*:`), whiteSpace},
- }
-)
-
-// XSS goes through all the XSS filters to make user input content as safe as possible.
-func XSS(in []byte) []byte {
- for _, filter := range xssFilters {
- in = filter.reg.ReplaceAll(in, filter.repl)
- }
- return in
-}
-
-func XSSString(in string) string {
- return string(XSS([]byte(in)))
-}