Skip to content

Commit d409d3a

Browse files
bkcsoftlunny
authored andcommitted
Sanitation fix from Gogs (#1461)
* Santiation fix from Gogs * Linting * Fix build-errors * still not working * Fix all the things! * gofmt * Add code-injection checks
1 parent 21290d4 commit d409d3a

File tree

7 files changed

+118
-27
lines changed

7 files changed

+118
-27
lines changed

models/repo.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ func (repo *Repository) DescriptionHTML() template.HTML {
595595
sanitize := func(s string) string {
596596
return fmt.Sprintf(`<a href="%[1]s" target="_blank" rel="noopener">%[1]s</a>`, s)
597597
}
598-
return template.HTML(descPattern.ReplaceAllStringFunc(markdown.Sanitizer.Sanitize(repo.Description), sanitize))
598+
return template.HTML(descPattern.ReplaceAllStringFunc(markdown.Sanitize(repo.Description), sanitize))
599599
}
600600

601601
// LocalCopyPath returns the local repository copy path
@@ -861,8 +861,8 @@ func cleanUpMigrateGitConfig(configPath string) error {
861861
// createDelegateHooks creates all the hooks scripts for the repo
862862
func createDelegateHooks(repoPath string) (err error) {
863863
var (
864-
hookNames = []string{"pre-receive", "update", "post-receive"}
865-
hookTpl = fmt.Sprintf("#!/usr/bin/env %s\ndata=$(cat)\nexitcodes=\"\"\nhookname=$(basename $0)\nGIT_DIR=${GIT_DIR:-$(dirname $0)}\n\nfor hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\ntest -x \"${hook}\" || continue\necho \"${data}\" | \"${hook}\"\nexitcodes=\"${exitcodes} $?\"\ndone\n\nfor i in ${exitcodes}; do\n[ ${i} -eq 0 ] || exit ${i}\ndone\n", setting.ScriptType)
864+
hookNames = []string{"pre-receive", "update", "post-receive"}
865+
hookTpl = fmt.Sprintf("#!/usr/bin/env %s\ndata=$(cat)\nexitcodes=\"\"\nhookname=$(basename $0)\nGIT_DIR=${GIT_DIR:-$(dirname $0)}\n\nfor hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\ntest -x \"${hook}\" || continue\necho \"${data}\" | \"${hook}\"\nexitcodes=\"${exitcodes} $?\"\ndone\n\nfor i in ${exitcodes}; do\n[ ${i} -eq 0 ] || exit ${i}\ndone\n", setting.ScriptType)
866866
giteaHookTpls = []string{
867867
fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' pre-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
868868
fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' update $1 $2 $3\n", setting.ScriptType, setting.AppPath, setting.CustomConf),

models/user.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (u *User) UpdateDiffViewStyle(style string) error {
163163
func (u *User) AfterSet(colName string, _ xorm.Cell) {
164164
switch colName {
165165
case "full_name":
166-
u.FullName = markdown.Sanitizer.Sanitize(u.FullName)
166+
u.FullName = markdown.Sanitize(u.FullName)
167167
case "created_unix":
168168
u.Created = time.Unix(u.CreatedUnix, 0).Local()
169169
case "updated_unix":
@@ -867,7 +867,7 @@ func updateUser(e Engine, u *User) error {
867867
u.Website = base.TruncateString(u.Website, 255)
868868
u.Description = base.TruncateString(u.Description, 255)
869869

870-
u.FullName = markdown.Sanitizer.Sanitize(u.FullName)
870+
u.FullName = markdown.Sanitize(u.FullName)
871871
_, err := e.Id(u.ID).AllCols().Update(u)
872872
return err
873873
}

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

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 markdown
7+
8+
import (
9+
"regexp"
10+
"sync"
11+
12+
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/setting"
14+
15+
"github.com/microcosm-cc/bluemonday"
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("Markdown: 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("Markdown: 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+
if sanitizer.policy == nil {
51+
NewSanitizer()
52+
}
53+
return sanitizer.policy.Sanitize(s)
54+
}
55+
56+
// SanitizeBytes takes a []byte slice that contains a HTML fragment or document and applies policy whitelist.
57+
func SanitizeBytes(b []byte) []byte {
58+
if len(b) == 0 {
59+
// nothing to sanitize
60+
return b
61+
}
62+
if sanitizer.policy == nil {
63+
NewSanitizer()
64+
}
65+
return sanitizer.policy.SanitizeBytes(b)
66+
}

modules/markdown/sanitizer_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 markdown
7+
8+
import (
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func Test_Sanitizer(t *testing.T) {
15+
NewSanitizer()
16+
testCases := []string{
17+
// Regular
18+
`<a onblur="alert(secret)" href="http://www.google.com">Google</a>`, `<a href="http://www.google.com" rel="nofollow">Google</a>`,
19+
20+
// Code highlighting class
21+
`<code class="random string"></code>`, `<code></code>`,
22+
`<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, `<code></code>`,
23+
`<code class="language-go"></code>`, `<code class="language-go"></code>`,
24+
25+
// Input checkbox
26+
`<input type="hidden">`, ``,
27+
`<input type="checkbox">`, `<input type="checkbox">`,
28+
`<input checked disabled autofocus>`, `<input checked="" disabled="">`,
29+
30+
// Code highlight injection
31+
`<code class="language-random&#32;ui&#32;tab&#32;active&#32;menu&#32;attached&#32;animating&#32;sidebar&#32;following&#32;bar&#32;center"></code>`, `<code></code>`,
32+
`<code class="language-lol&#32;ui&#32;tab&#32;active&#32;menu&#32;attached&#32;animating&#32;sidebar&#32;following&#32;bar&#32;center">
33+
<code class="language-lol&#32;ui&#32;container&#32;input&#32;huge&#32;basic&#32;segment&#32;center">&nbsp;</code>
34+
<img src="https://try.gogs.io/img/favicon.png" width="200" height="200">
35+
<code class="language-lol&#32;ui&#32;container&#32;input&#32;massive&#32;basic&#32;segment">Hello there! Something has gone wrong, we are working on it.</code>
36+
<code class="language-lol&#32;ui&#32;container&#32;input&#32;huge&#32;basic&#32;segment">In the meantime, play a game with us at&nbsp;<a href="http://example.com/">example.com</a>.</code>
37+
</code>`, "<code>\n<code>\u00a0</code>\n<img src=\"https://try.gogs.io/img/favicon.png\" width=\"200\" height=\"200\">\n<code>Hello there! Something has gone wrong, we are working on it.</code>\n<code>In the meantime, play a game with us at\u00a0<a href=\"http://example.com/\" rel=\"nofollow\">example.com</a>.</code>\n</code>",
38+
}
39+
40+
for i := 0; i < len(testCases); i += 2 {
41+
assert.Equal(t, testCases[i+1], Sanitize(testCases[i]))
42+
assert.Equal(t, testCases[i+1], string(SanitizeBytes([]byte(testCases[i]))))
43+
}
44+
}

modules/templates/helper.go

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

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

170170
// 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)