-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Merged
Merged
Sanitation fix from Gogs #1461
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a242510
Santiation fix from Gogs
bkcsoft fa24b11
Linting
bkcsoft 09e7acd
Fix build-errors
bkcsoft 20a883d
still not working
bkcsoft c16b762
Fix all the things!
bkcsoft 15f0a40
gofmt
bkcsoft d5b711b
Add code-injection checks
bkcsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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 markup | ||
|
||
import ( | ||
"regexp" | ||
"sync" | ||
|
||
"github.com/microcosm-cc/bluemonday" | ||
log "gopkg.in/clog.v1" | ||
|
||
"github.com/gogits/gogs/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("Markup: 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("Markup: sanitizer initialized") | ||
}) | ||
} | ||
|
||
// Sanitize takes a string that contains a HTML fragment or document and applies policy whitelist. | ||
func Sanitize(s string) string { | ||
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 { | ||
return sanitizer.policy.SanitizeBytes(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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 markup_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
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. goconvey has been removed before. I think change this test file to |
||
|
||
. "github.com/gogits/gogs/modules/markup" | ||
) | ||
|
||
func Test_Sanitizer(t *testing.T) { | ||
BuildSanitizer() | ||
Convey("Sanitize HTML string and bytes", t, func() { | ||
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 { | ||
So(Sanitize(testCases[i]), ShouldEqual, testCases[i+1]) | ||
So(string(SanitizeBytes([]byte(testCases[i]))), ShouldEqual, testCases[i+1]) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
github.com/go-gitea/gitea
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.
actually "code.gitea.io/gitea/modules/setting"
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.
Good catch
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.
^
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.
Noted