-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sanitation fix from Gogs #1461
Sanitation fix from Gogs #1461
Changes from 5 commits
a242510
fa24b11
09e7acd
20a883d
c16b762
15f0a40
d5b711b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Copyright 2017 The Gogs Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package markdown | ||
|
||
import ( | ||
"regexp" | ||
"sync" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
|
||
"github.com/microcosm-cc/bluemonday" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
// Sanitizer is a protection wrapper of *bluemonday.Policy which does not allow | ||
// any modification to the underlying policies once it's been created. | ||
type Sanitizer struct { | ||
policy *bluemonday.Policy | ||
init sync.Once | ||
} | ||
|
||
var sanitizer = &Sanitizer{} | ||
|
||
// NewSanitizer initializes sanitizer with allowed attributes based on settings. | ||
// Multiple calls to this function will only create one instance of Sanitizer during | ||
// entire application lifecycle. | ||
func NewSanitizer() { | ||
log.Trace("Markdown: sanitizer initialization requested") | ||
sanitizer.init.Do(func() { | ||
sanitizer.policy = bluemonday.UGCPolicy() | ||
// We only want to allow HighlightJS specific classes for code blocks | ||
sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^language-\w+$`)).OnElements("code") | ||
|
||
// Checkboxes | ||
sanitizer.policy.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input") | ||
sanitizer.policy.AllowAttrs("checked", "disabled").OnElements("input") | ||
|
||
// Custom URL-Schemes | ||
sanitizer.policy.AllowURLSchemes(setting.Markdown.CustomURLSchemes...) | ||
|
||
log.Trace("Markdown: sanitizer initialized") | ||
}) | ||
} | ||
|
||
// Sanitize takes a string that contains a HTML fragment or document and applies policy whitelist. | ||
func Sanitize(s string) string { | ||
if sanitizer.policy == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about func init() {
NewSanitizer(0
} ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't wanna initialize it unless we need to. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this maybe a race problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
NewSanitizer() | ||
} | ||
return sanitizer.policy.Sanitize(s) | ||
} | ||
|
||
// SanitizeBytes takes a []byte slice that contains a HTML fragment or document and applies policy whitelist. | ||
func SanitizeBytes(b []byte) []byte { | ||
if len(b) == 0 { | ||
// nothing to sanitize | ||
return b | ||
} | ||
if sanitizer.policy == nil { | ||
NewSanitizer() | ||
} | ||
return sanitizer.policy.SanitizeBytes(b) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Copyright 2017 The Gogs Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package markdown | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Sanitizer(t *testing.T) { | ||
NewSanitizer() | ||
testCases := []string{ | ||
// Regular | ||
`<a onblur="alert(secret)" href="http://www.google.com">Google</a>`, `<a href="http://www.google.com" rel="nofollow">Google</a>`, | ||
|
||
// Code highlighting class | ||
`<code class="random string"></code>`, `<code></code>`, | ||
`<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, `<code></code>`, | ||
`<code class="language-go"></code>`, `<code class="language-go"></code>`, | ||
|
||
// Input checkbox | ||
`<input type="hidden">`, ``, | ||
`<input type="checkbox">`, `<input type="checkbox">`, | ||
`<input checked disabled autofocus>`, `<input checked="" disabled="">`, | ||
} | ||
|
||
for i := 0; i < len(testCases); i += 2 { | ||
assert.Equal(t, testCases[i+1], Sanitize(testCases[i])) | ||
assert.Equal(t, testCases[i+1], string(SanitizeBytes([]byte(testCases[i])))) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merge the gitea internal packages.