Skip to content

Commit a242510

Browse files
committed
Santiation fix from Gogs
1 parent 5acfc7c commit a242510

File tree

5 files changed

+98
-22
lines changed

5 files changed

+98
-22
lines changed

modules/markdown/markdown.go

+1-20
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"strings"
1616

1717
"github.com/Unknwon/com"
18-
"github.com/microcosm-cc/bluemonday"
1918
"github.com/russross/blackfriday"
2019
"golang.org/x/net/html"
2120

@@ -29,24 +28,6 @@ const (
2928
IssueNameStyleAlphanumeric = "alphanumeric"
3029
)
3130

32-
// Sanitizer markdown sanitizer
33-
var Sanitizer = bluemonday.UGCPolicy()
34-
35-
// BuildSanitizer initializes sanitizer with allowed attributes based on settings.
36-
// This function should only be called once during entire application lifecycle.
37-
func BuildSanitizer() {
38-
// Normal markdown-stuff
39-
Sanitizer.AllowAttrs("class").Matching(regexp.MustCompile(`[\p{L}\p{N}\s\-_',:\[\]!\./\\\(\)&]*`)).OnElements("code", "div", "ul", "ol", "dl")
40-
41-
// Checkboxes
42-
Sanitizer.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input")
43-
Sanitizer.AllowAttrs("checked", "disabled").OnElements("input")
44-
Sanitizer.AllowNoAttrs().OnElements("label")
45-
46-
// Custom URL-Schemes
47-
Sanitizer.AllowURLSchemes(setting.Markdown.CustomURLSchemes...)
48-
}
49-
5031
// IsMarkdownFile reports whether name looks like a Markdown file
5132
// based on its extension.
5233
func IsMarkdownFile(name string) bool {
@@ -708,7 +689,7 @@ func render(rawBytes []byte, urlPrefix string, metas map[string]string, isWikiMa
708689
urlPrefix = strings.Replace(urlPrefix, " ", "+", -1)
709690
result := RenderRaw(rawBytes, urlPrefix, isWikiMarkdown)
710691
result = PostProcess(result, urlPrefix, metas, isWikiMarkdown)
711-
result = Sanitizer.SanitizeBytes(result)
692+
result = SanitizeBytes(result)
712693
return result
713694
}
714695

modules/markdown/sanitizer.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Copyright 2017 The Gogs Authors. All rights reserved.
3+
// Use of this source code is governed by a MIT-style
4+
// license that can be found in the LICENSE file.
5+
6+
package markup
7+
8+
import (
9+
"regexp"
10+
"sync"
11+
12+
"github.com/microcosm-cc/bluemonday"
13+
log "gopkg.in/clog.v1"
14+
15+
"github.com/gogits/gogs/modules/setting"
16+
)
17+
18+
// Sanitizer is a protection wrapper of *bluemonday.Policy which does not allow
19+
// any modification to the underlying policies once it's been created.
20+
type Sanitizer struct {
21+
policy *bluemonday.Policy
22+
init sync.Once
23+
}
24+
25+
var sanitizer = &Sanitizer{}
26+
27+
// NewSanitizer initializes sanitizer with allowed attributes based on settings.
28+
// Multiple calls to this function will only create one instance of Sanitizer during
29+
// entire application lifecycle.
30+
func NewSanitizer() {
31+
log.Trace("Markup: sanitizer initialization requested")
32+
sanitizer.init.Do(func() {
33+
sanitizer.policy = bluemonday.UGCPolicy()
34+
// We only want to allow HighlightJS specific classes for code blocks
35+
sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^language-\w+$`)).OnElements("code")
36+
37+
// Checkboxes
38+
sanitizer.policy.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input")
39+
sanitizer.policy.AllowAttrs("checked", "disabled").OnElements("input")
40+
41+
// Custom URL-Schemes
42+
sanitizer.policy.AllowURLSchemes(setting.Markdown.CustomURLSchemes...)
43+
44+
log.Trace("Markup: sanitizer initialized")
45+
})
46+
}
47+
48+
// Sanitize takes a string that contains a HTML fragment or document and applies policy whitelist.
49+
func Sanitize(s string) string {
50+
return sanitizer.policy.Sanitize(s)
51+
}
52+
53+
// SanitizeBytes takes a []byte slice that contains a HTML fragment or document and applies policy whitelist.
54+
func SanitizeBytes(b []byte) []byte {
55+
return sanitizer.policy.SanitizeBytes(b)
56+
}

modules/markdown/sanitizer_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Copyright 2017 The Gogs Authors. All rights reserved.
3+
// Use of this source code is governed by a MIT-style
4+
// license that can be found in the LICENSE file.
5+
6+
package markup_test
7+
8+
import (
9+
"testing"
10+
11+
. "github.com/smartystreets/goconvey/convey"
12+
13+
. "github.com/gogits/gogs/modules/markup"
14+
)
15+
16+
func Test_Sanitizer(t *testing.T) {
17+
BuildSanitizer()
18+
Convey("Sanitize HTML string and bytes", t, func() {
19+
testCases := []string{
20+
// Regular
21+
`<a onblur="alert(secret)" href="http://www.google.com">Google</a>`, `<a href="http://www.google.com" rel="nofollow">Google</a>`,
22+
23+
// Code highlighting class
24+
`<code class="random string"></code>`, `<code></code>`,
25+
`<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, `<code></code>`,
26+
`<code class="language-go"></code>`, `<code class="language-go"></code>`,
27+
28+
// Input checkbox
29+
`<input type="hidden">`, ``,
30+
`<input type="checkbox">`, `<input type="checkbox">`,
31+
`<input checked disabled autofocus>`, `<input checked="" disabled="">`,
32+
}
33+
34+
for i := 0; i < len(testCases); i += 2 {
35+
So(Sanitize(testCases[i]), ShouldEqual, testCases[i+1])
36+
So(string(SanitizeBytes([]byte(testCases[i]))), ShouldEqual, testCases[i+1])
37+
}
38+
})
39+
}

modules/templates/helper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func Safe(raw string) template.HTML {
161161

162162
// Str2html render Markdown text to HTML
163163
func Str2html(raw string) template.HTML {
164-
return template.HTML(markdown.Sanitizer.Sanitize(raw))
164+
return template.HTML(markdown.Sanitize(raw))
165165
}
166166

167167
// List traversings the list

routers/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func GlobalInit() {
4949

5050
if setting.InstallLock {
5151
highlight.NewContext()
52-
markdown.BuildSanitizer()
52+
markdown.NewSanitizer()
5353
if err := models.NewEngine(); err != nil {
5454
log.Fatal(4, "Failed to initialize ORM engine: %v", err)
5555
}

0 commit comments

Comments
 (0)