Update Dependencies (#390)
Co-authored-by: Norwin Roosen <git@nroo.de> Co-authored-by: Norwin <git@nroo.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/390 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Norwin <noerw@noreply.gitea.io> Co-committed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
4
vendor/github.com/microcosm-cc/bluemonday/.travis.yml
generated
vendored
4
vendor/github.com/microcosm-cc/bluemonday/.travis.yml
generated
vendored
@@ -11,6 +11,10 @@ go:
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
- 1.12.x
|
||||
- 1.13.x
|
||||
- 1.14.x
|
||||
- 1.15.x
|
||||
- 1.16.x
|
||||
- tip
|
||||
matrix:
|
||||
allow_failures:
|
||||
|
||||
1
vendor/github.com/microcosm-cc/bluemonday/CONTRIBUTING.md
generated
vendored
1
vendor/github.com/microcosm-cc/bluemonday/CONTRIBUTING.md
generated
vendored
@@ -9,6 +9,7 @@ Third-party patches are essential for keeping bluemonday secure and offering the
|
||||
## Guidelines
|
||||
|
||||
1. Do not vendor dependencies. As a security package, were we to vendor dependencies the projects that then vendor bluemonday may not receive the latest security updates to the dependencies. By not vendoring dependencies the project that implements bluemonday will vendor the latest version of any dependent packages. Vendoring is a project problem, not a package problem. bluemonday will be tested against the latest version of dependencies periodically and during any PR/merge.
|
||||
2. I do not care about spelling mistakes or whitespace and I do not believe that you should either. PRs therefore must be functional in their nature or be substantial and impactful if documentation or examples.
|
||||
|
||||
## Submitting an Issue
|
||||
|
||||
|
||||
2
vendor/github.com/microcosm-cc/bluemonday/Makefile
generated
vendored
2
vendor/github.com/microcosm-cc/bluemonday/Makefile
generated
vendored
@@ -25,7 +25,7 @@ build:
|
||||
@go build
|
||||
|
||||
vet:
|
||||
@go vet *.go
|
||||
@go vet
|
||||
|
||||
lint:
|
||||
@golint *.go
|
||||
|
||||
29
vendor/github.com/microcosm-cc/bluemonday/README.md
generated
vendored
29
vendor/github.com/microcosm-cc/bluemonday/README.md
generated
vendored
@@ -2,7 +2,7 @@
|
||||
|
||||
bluemonday is a HTML sanitizer implemented in Go. It is fast and highly configurable.
|
||||
|
||||
bluemonday takes untrusted user generated content as an input, and will return HTML that has been sanitised against a whitelist of approved HTML elements and attributes so that you can safely include the content in your web page.
|
||||
bluemonday takes untrusted user generated content as an input, and will return HTML that has been sanitised against an allowlist of approved HTML elements and attributes so that you can safely include the content in your web page.
|
||||
|
||||
If you accept user generated content, and your server uses Go, you **need** bluemonday.
|
||||
|
||||
@@ -50,15 +50,15 @@ bluemonday is heavily inspired by both the [OWASP Java HTML Sanitizer](https://c
|
||||
|
||||
## Technical Summary
|
||||
|
||||
Whitelist based, you need to either build a policy describing the HTML elements and attributes to permit (and the `regexp` patterns of attributes), or use one of the supplied policies representing good defaults.
|
||||
Allowlist based, you need to either build a policy describing the HTML elements and attributes to permit (and the `regexp` patterns of attributes), or use one of the supplied policies representing good defaults.
|
||||
|
||||
The policy containing the whitelist is applied using a fast non-validating, forward only, token-based parser implemented in the [Go net/html library](https://godoc.org/golang.org/x/net/html) by the core Go team.
|
||||
The policy containing the allowlist is applied using a fast non-validating, forward only, token-based parser implemented in the [Go net/html library](https://godoc.org/golang.org/x/net/html) by the core Go team.
|
||||
|
||||
We expect to be supplied with well-formatted HTML (closing elements for every applicable open element, nested correctly) and so we do not focus on repairing badly nested or incomplete HTML. We focus on simply ensuring that whatever elements do exist are described in the policy whitelist and that attributes and links are safe for use on your web page. [GIGO](http://en.wikipedia.org/wiki/Garbage_in,_garbage_out) does apply and if you feed it bad HTML bluemonday is not tasked with figuring out how to make it good again.
|
||||
We expect to be supplied with well-formatted HTML (closing elements for every applicable open element, nested correctly) and so we do not focus on repairing badly nested or incomplete HTML. We focus on simply ensuring that whatever elements do exist are described in the policy allowlist and that attributes and links are safe for use on your web page. [GIGO](http://en.wikipedia.org/wiki/Garbage_in,_garbage_out) does apply and if you feed it bad HTML bluemonday is not tasked with figuring out how to make it good again.
|
||||
|
||||
### Supported Go Versions
|
||||
|
||||
bluemonday is tested against Go 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10, 1.11, 1.12, and tip.
|
||||
bluemonday is tested on all versions since Go 1.2 including tip.
|
||||
|
||||
We do not support Go 1.0 as we depend on `golang.org/x/net/html` which includes a reference to `io.ErrNoProgress` which did not exist in Go 1.0.
|
||||
|
||||
@@ -146,8 +146,8 @@ func main() {
|
||||
|
||||
We ship two default policies:
|
||||
|
||||
1. `bluemonday.StrictPolicy()` which can be thought of as equivalent to stripping all HTML elements and their attributes as it has nothing on its whitelist. An example usage scenario would be blog post titles where HTML tags are not expected at all and if they are then the elements *and* the content of the elements should be stripped. This is a *very* strict policy.
|
||||
2. `bluemonday.UGCPolicy()` which allows a broad selection of HTML elements and attributes that are safe for user generated content. Note that this policy does *not* whitelist iframes, object, embed, styles, script, etc. An example usage scenario would be blog post bodies where a variety of formatting is expected along with the potential for TABLEs and IMGs.
|
||||
1. `bluemonday.StrictPolicy()` which can be thought of as equivalent to stripping all HTML elements and their attributes as it has nothing on its allowlist. An example usage scenario would be blog post titles where HTML tags are not expected at all and if they are then the elements *and* the content of the elements should be stripped. This is a *very* strict policy.
|
||||
2. `bluemonday.UGCPolicy()` which allows a broad selection of HTML elements and attributes that are safe for user generated content. Note that this policy does *not* allow iframes, object, embed, styles, script, etc. An example usage scenario would be blog post bodies where a variety of formatting is expected along with the potential for TABLEs and IMGs.
|
||||
|
||||
## Policy Building
|
||||
|
||||
@@ -220,7 +220,7 @@ p.AllowElements("fieldset", "select", "option")
|
||||
|
||||
### Inline CSS
|
||||
|
||||
Although it's possible to handle inline CSS using `AllowAttrs` with a `Matching` rule, writing a single monolithic regular expression to safely process all inline CSS which you wish to allow is not a trivial task. Instead of attempting to do so, you can whitelist the `style` attribute on whichever element(s) you desire and use style policies to control and sanitize inline styles.
|
||||
Although it's possible to handle inline CSS using `AllowAttrs` with a `Matching` rule, writing a single monolithic regular expression to safely process all inline CSS which you wish to allow is not a trivial task. Instead of attempting to do so, you can allow the `style` attribute on whichever element(s) you desire and use style policies to control and sanitize inline styles.
|
||||
|
||||
It is suggested that you use `Matching` (with a suitable regular expression)
|
||||
`MatchingEnum`, or `MatchingHandler` to ensure each style matches your needs,
|
||||
@@ -241,7 +241,7 @@ p.AllowAttrs("style").OnElements("span", "p")
|
||||
p.AllowStyles("text-decoration").MatchingEnum("underline", "line-through", "none").OnElements("span")
|
||||
```
|
||||
|
||||
Or you can specify elements based on a regex patterm match:
|
||||
Or you can specify elements based on a regex pattern match:
|
||||
```go
|
||||
p.AllowAttrs("style").OnElementsMatching(regex.MustCompile(`^my-element-`))
|
||||
// Allow the 'text-decoration' property to be set to 'underline', 'line-through' or 'none'
|
||||
@@ -254,6 +254,7 @@ validate the values for a given property. The string parameter has been
|
||||
converted to lowercase and unicode code points have been converted.
|
||||
```go
|
||||
myHandler := func(value string) bool{
|
||||
// Validate your input here
|
||||
return true
|
||||
}
|
||||
p.AllowAttrs("style").OnElements("span", "p")
|
||||
@@ -279,12 +280,12 @@ We provide some additional global options for safely working with links.
|
||||
p.RequireParseableURLs(true)
|
||||
```
|
||||
|
||||
If you have enabled parseable URLs then the following option will `AllowRelativeURLs`. By default this is disabled (bluemonday is a whitelist tool... you need to explicitly tell us to permit things) and when disabled it will prevent all local and scheme relative URLs (i.e. `href="localpage.html"`, `href="../home.html"` and even `href="//www.google.com"` are relative):
|
||||
If you have enabled parseable URLs then the following option will `AllowRelativeURLs`. By default this is disabled (bluemonday is an allowlist tool... you need to explicitly tell us to permit things) and when disabled it will prevent all local and scheme relative URLs (i.e. `href="localpage.html"`, `href="../home.html"` and even `href="//www.google.com"` are relative):
|
||||
```go
|
||||
p.AllowRelativeURLs(true)
|
||||
```
|
||||
|
||||
If you have enabled parseable URLs then you can whitelist the schemes (commonly called protocol when thinking of `http` and `https`) that are permitted. Bear in mind that allowing relative URLs in the above option will allow for a blank scheme:
|
||||
If you have enabled parseable URLs then you can allow the schemes (commonly called protocol when thinking of `http` and `https`) that are permitted. Bear in mind that allowing relative URLs in the above option will allow for a blank scheme:
|
||||
```go
|
||||
p.AllowURLSchemes("mailto", "http", "https")
|
||||
```
|
||||
@@ -302,7 +303,7 @@ p.RequireNoReferrerOnLinks(true)
|
||||
```
|
||||
|
||||
|
||||
We provide a convenience method that applies all of the above, but you will still need to whitelist the linkable elements for the URL rules to be applied to:
|
||||
We provide a convenience method that applies all of the above, but you will still need to allow the linkable elements for the URL rules to be applied to:
|
||||
```go
|
||||
p.AllowStandardURLs()
|
||||
p.AllowAttrs("cite").OnElements("blockquote", "q")
|
||||
@@ -372,11 +373,11 @@ p.AllowAttrs(
|
||||
)
|
||||
```
|
||||
|
||||
Both examples exhibit the same issue, they declare attributes but do not then specify whether they are whitelisted globally or only on specific elements (and which elements). Attributes belong to one or more elements, and the policy needs to declare this.
|
||||
Both examples exhibit the same issue, they declare attributes but do not then specify whether they are allowed globally or only on specific elements (and which elements). Attributes belong to one or more elements, and the policy needs to declare this.
|
||||
|
||||
## Limitations
|
||||
|
||||
We are not yet including any tools to help whitelist and sanitize CSS. Which means that unless you wish to do the heavy lifting in a single regular expression (inadvisable), **you should not allow the "style" attribute anywhere**.
|
||||
We are not yet including any tools to help allow and sanitize CSS. Which means that unless you wish to do the heavy lifting in a single regular expression (inadvisable), **you should not allow the "style" attribute anywhere**.
|
||||
|
||||
It is not the job of bluemonday to fix your bad HTML, it is merely the job of bluemonday to prevent malicious HTML getting through. If you have mismatched HTML elements, or non-conforming nesting of elements, those will remain. But if you have well-structured HTML bluemonday will not break it.
|
||||
|
||||
|
||||
15
vendor/github.com/microcosm-cc/bluemonday/SECURITY.md
generated
vendored
Normal file
15
vendor/github.com/microcosm-cc/bluemonday/SECURITY.md
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported Versions
|
||||
|
||||
Latest tag and tip are supported.
|
||||
|
||||
Older tags remain present but changes result in new tags and are not back ported... please verify any issue against the latest tag and tip.
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
Email: <bluemonday@buro9.com>
|
||||
|
||||
Bluemonday is pure OSS and not maintained by a company. As such there is no bug bounty program but security issues will be taken seriously and resolved as soon as possible.
|
||||
|
||||
The maintainer lives in the United Kingdom and whilst the email is monitored expect a reply or ACK when the maintainer is awake.
|
||||
@@ -26,7 +26,8 @@
|
||||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
package bluemonday
|
||||
|
||||
package css
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
@@ -328,7 +329,7 @@ func splitValues(value string) []string {
|
||||
return values
|
||||
}
|
||||
|
||||
func getDefaultHandler(attr string) func(string) bool {
|
||||
func GetDefaultHandler(attr string) func(string) bool {
|
||||
|
||||
if defaultStyleHandlers[attr] != nil {
|
||||
return defaultStyleHandlers[attr]
|
||||
14
vendor/github.com/microcosm-cc/bluemonday/doc.go
generated
vendored
14
vendor/github.com/microcosm-cc/bluemonday/doc.go
generated
vendored
@@ -28,10 +28,10 @@
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
/*
|
||||
Package bluemonday provides a way of describing a whitelist of HTML elements
|
||||
Package bluemonday provides a way of describing an allowlist of HTML elements
|
||||
and attributes as a policy, and for that policy to be applied to untrusted
|
||||
strings from users that may contain markup. All elements and attributes not on
|
||||
the whitelist will be stripped.
|
||||
the allowlist will be stripped.
|
||||
|
||||
The default bluemonday.UGCPolicy().Sanitize() turns this:
|
||||
|
||||
@@ -84,21 +84,21 @@ bluemonday is heavily inspired by both the OWASP Java HTML Sanitizer
|
||||
|
||||
We ship two default policies, one is bluemonday.StrictPolicy() and can be
|
||||
thought of as equivalent to stripping all HTML elements and their attributes as
|
||||
it has nothing on its whitelist.
|
||||
it has nothing on its allowlist.
|
||||
|
||||
The other is bluemonday.UGCPolicy() and allows a broad selection of HTML
|
||||
elements and attributes that are safe for user generated content. Note that
|
||||
this policy does not whitelist iframes, object, embed, styles, script, etc.
|
||||
this policy does not allow iframes, object, embed, styles, script, etc.
|
||||
|
||||
The essence of building a policy is to determine which HTML elements and
|
||||
attributes are considered safe for your scenario. OWASP provide an XSS
|
||||
prevention cheat sheet ( https://www.google.com/search?q=xss+prevention+cheat+sheet )
|
||||
to help explain the risks, but essentially:
|
||||
|
||||
1. Avoid whitelisting anything other than plain HTML elements
|
||||
2. Avoid whitelisting `script`, `style`, `iframe`, `object`, `embed`, `base`
|
||||
1. Avoid allowing anything other than plain HTML elements
|
||||
2. Avoid allowing `script`, `style`, `iframe`, `object`, `embed`, `base`
|
||||
elements
|
||||
3. Avoid whitelisting anything other than plain HTML elements with simple
|
||||
3. Avoid allowing anything other than plain HTML elements with simple
|
||||
values that you can match to a regexp
|
||||
*/
|
||||
package bluemonday
|
||||
|
||||
8
vendor/github.com/microcosm-cc/bluemonday/go.mod
generated
vendored
8
vendor/github.com/microcosm-cc/bluemonday/go.mod
generated
vendored
@@ -1,10 +1,10 @@
|
||||
module github.com/microcosm-cc/bluemonday
|
||||
|
||||
go 1.9
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/aymerick/douceur v0.2.0 // indirect
|
||||
github.com/chris-ramon/douceur v0.2.0
|
||||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
|
||||
github.com/aymerick/douceur v0.2.0
|
||||
github.com/gorilla/css v1.0.0 // indirect
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3
|
||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
|
||||
)
|
||||
|
||||
18
vendor/github.com/microcosm-cc/bluemonday/go.sum
generated
vendored
18
vendor/github.com/microcosm-cc/bluemonday/go.sum
generated
vendored
@@ -1,8 +1,18 @@
|
||||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
|
||||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
|
||||
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
|
||||
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
|
||||
github.com/chris-ramon/douceur v0.2.0 h1:IDMEdxlEUUBYBKE4z/mJnFyVXox+MjuEVDJNN27glkU=
|
||||
github.com/chris-ramon/douceur v0.2.0/go.mod h1:wDW5xjJdeoMm1mRt4sD4c/LbF/mWdEpRXQKjTR8nIBE=
|
||||
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
|
||||
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20210421230115-4e50805a0758 h1:aEpZnXcAmXkd6AvLb2OPt+EN1Zu/8Ne3pCqPjja5PXY=
|
||||
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
|
||||
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b h1:k+E048sYJHyVnsr1GDrRZWQ32D2C7lWs9JRc0bel53A=
|
||||
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
|
||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
|
||||
2
vendor/github.com/microcosm-cc/bluemonday/helpers.go
generated
vendored
2
vendor/github.com/microcosm-cc/bluemonday/helpers.go
generated
vendored
@@ -141,7 +141,7 @@ func (p *Policy) AllowStandardURLs() {
|
||||
}
|
||||
|
||||
// AllowStandardAttributes will enable "id", "title" and the language specific
|
||||
// attributes "dir" and "lang" on all elements that are whitelisted
|
||||
// attributes "dir" and "lang" on all elements that are allowed
|
||||
func (p *Policy) AllowStandardAttributes() {
|
||||
// "dir" "lang" are permitted as both language attributes affect charsets
|
||||
// and direction of text.
|
||||
|
||||
172
vendor/github.com/microcosm-cc/bluemonday/policy.go
generated
vendored
172
vendor/github.com/microcosm-cc/bluemonday/policy.go
generated
vendored
@@ -35,9 +35,11 @@ import (
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/microcosm-cc/bluemonday/css"
|
||||
)
|
||||
|
||||
// Policy encapsulates the whitelist of HTML elements and attributes that will
|
||||
// Policy encapsulates the allowlist of HTML elements and attributes that will
|
||||
// be applied to the sanitised HTML.
|
||||
//
|
||||
// You should use bluemonday.NewPolicy() to create a blank policy as the
|
||||
@@ -69,6 +71,9 @@ type Policy struct {
|
||||
// Will skip for href="/foo" or href="foo"
|
||||
requireNoReferrerFullyQualifiedLinks bool
|
||||
|
||||
// When true, add crossorigin="anonymous" to HTML audio, img, link, script, and video tags
|
||||
requireCrossOriginAnonymous bool
|
||||
|
||||
// When true add target="_blank" to fully qualified links
|
||||
// Will add for href="http://foo"
|
||||
// Will skip for href="/foo" or href="foo"
|
||||
@@ -83,28 +88,31 @@ type Policy struct {
|
||||
// When true, allow data attributes.
|
||||
allowDataAttributes bool
|
||||
|
||||
// map[htmlElementName]map[htmlAttributeName]attrPolicy
|
||||
elsAndAttrs map[string]map[string]attrPolicy
|
||||
// When true, allow comments.
|
||||
allowComments bool
|
||||
|
||||
// map[htmlElementName]map[htmlAttributeName][]attrPolicy
|
||||
elsAndAttrs map[string]map[string][]attrPolicy
|
||||
|
||||
// elsMatchingAndAttrs stores regex based element matches along with attributes
|
||||
elsMatchingAndAttrs map[*regexp.Regexp]map[string]attrPolicy
|
||||
elsMatchingAndAttrs map[*regexp.Regexp]map[string][]attrPolicy
|
||||
|
||||
// map[htmlAttributeName]attrPolicy
|
||||
globalAttrs map[string]attrPolicy
|
||||
// map[htmlAttributeName][]attrPolicy
|
||||
globalAttrs map[string][]attrPolicy
|
||||
|
||||
// map[htmlElementName]map[cssPropertyName]stylePolicy
|
||||
elsAndStyles map[string]map[string]stylePolicy
|
||||
// map[htmlElementName]map[cssPropertyName][]stylePolicy
|
||||
elsAndStyles map[string]map[string][]stylePolicy
|
||||
|
||||
// map[regex]map[cssPropertyName]stylePolicy
|
||||
elsMatchingAndStyles map[*regexp.Regexp]map[string]stylePolicy
|
||||
// map[regex]map[cssPropertyName][]stylePolicy
|
||||
elsMatchingAndStyles map[*regexp.Regexp]map[string][]stylePolicy
|
||||
|
||||
// map[cssPropertyName]stylePolicy
|
||||
globalStyles map[string]stylePolicy
|
||||
// map[cssPropertyName][]stylePolicy
|
||||
globalStyles map[string][]stylePolicy
|
||||
|
||||
// If urlPolicy is nil, all URLs with matching schema are allowed.
|
||||
// Otherwise, only the URLs with matching schema and urlPolicy(url)
|
||||
// returning true are allowed.
|
||||
allowURLSchemes map[string]urlPolicy
|
||||
allowURLSchemes map[string][]urlPolicy
|
||||
|
||||
// If an element has had all attributes removed as a result of a policy
|
||||
// being applied, then the element would be removed from the output.
|
||||
@@ -171,22 +179,22 @@ type urlPolicy func(url *url.URL) (allowUrl bool)
|
||||
// init initializes the maps if this has not been done already
|
||||
func (p *Policy) init() {
|
||||
if !p.initialized {
|
||||
p.elsAndAttrs = make(map[string]map[string]attrPolicy)
|
||||
p.elsMatchingAndAttrs = make(map[*regexp.Regexp]map[string]attrPolicy)
|
||||
p.globalAttrs = make(map[string]attrPolicy)
|
||||
p.elsAndStyles = make(map[string]map[string]stylePolicy)
|
||||
p.elsMatchingAndStyles = make(map[*regexp.Regexp]map[string]stylePolicy)
|
||||
p.globalStyles = make(map[string]stylePolicy)
|
||||
p.allowURLSchemes = make(map[string]urlPolicy)
|
||||
p.elsAndAttrs = make(map[string]map[string][]attrPolicy)
|
||||
p.elsMatchingAndAttrs = make(map[*regexp.Regexp]map[string][]attrPolicy)
|
||||
p.globalAttrs = make(map[string][]attrPolicy)
|
||||
p.elsAndStyles = make(map[string]map[string][]stylePolicy)
|
||||
p.elsMatchingAndStyles = make(map[*regexp.Regexp]map[string][]stylePolicy)
|
||||
p.globalStyles = make(map[string][]stylePolicy)
|
||||
p.allowURLSchemes = make(map[string][]urlPolicy)
|
||||
p.setOfElementsAllowedWithoutAttrs = make(map[string]struct{})
|
||||
p.setOfElementsToSkipContent = make(map[string]struct{})
|
||||
p.initialized = true
|
||||
}
|
||||
}
|
||||
|
||||
// NewPolicy returns a blank policy with nothing whitelisted or permitted. This
|
||||
// NewPolicy returns a blank policy with nothing allowed or permitted. This
|
||||
// is the recommended way to start building a policy and you should now use
|
||||
// AllowAttrs() and/or AllowElements() to construct the whitelist of HTML
|
||||
// AllowAttrs() and/or AllowElements() to construct the allowlist of HTML
|
||||
// elements and attributes.
|
||||
func NewPolicy() *Policy {
|
||||
|
||||
@@ -200,7 +208,7 @@ func NewPolicy() *Policy {
|
||||
|
||||
// AllowAttrs takes a range of HTML attribute names and returns an
|
||||
// attribute policy builder that allows you to specify the pattern and scope of
|
||||
// the whitelisted attribute.
|
||||
// the allowed attribute.
|
||||
//
|
||||
// The attribute policy is only added to the core policy when either Globally()
|
||||
// or OnElements(...) are called.
|
||||
@@ -220,7 +228,7 @@ func (p *Policy) AllowAttrs(attrNames ...string) *attrPolicyBuilder {
|
||||
return &abp
|
||||
}
|
||||
|
||||
// AllowDataAttributes whitelists all data attributes. We can't specify the name
|
||||
// AllowDataAttributes permits all data attributes. We can't specify the name
|
||||
// of each attribute exactly as they are customized.
|
||||
//
|
||||
// NOTE: These values are not sanitized and applications that evaluate or process
|
||||
@@ -235,6 +243,22 @@ func (p *Policy) AllowDataAttributes() {
|
||||
p.allowDataAttributes = true
|
||||
}
|
||||
|
||||
// AllowComments allows comments.
|
||||
//
|
||||
// Please note that only one type of comment will be allowed by this, this is the
|
||||
// the standard HTML comment <!-- --> which includes the use of that to permit
|
||||
// conditionals as per https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537512(v=vs.85)?redirectedfrom=MSDN
|
||||
//
|
||||
// What is not permitted are CDATA XML comments, as the x/net/html package we depend
|
||||
// on does not handle this fully and we are not choosing to take on that work:
|
||||
// https://pkg.go.dev/golang.org/x/net/html#Tokenizer.AllowCDATA . If the x/net/html
|
||||
// package changes this then these will be considered, otherwise if you AllowComments
|
||||
// but provide a CDATA comment, then as per the documentation in x/net/html this will
|
||||
// be treated as a plain HTML comment.
|
||||
func (p *Policy) AllowComments() {
|
||||
p.allowComments = true
|
||||
}
|
||||
|
||||
// AllowNoAttrs says that attributes on element are optional.
|
||||
//
|
||||
// The attribute policy is only added to the core policy when OnElements(...)
|
||||
@@ -262,8 +286,7 @@ func (abp *attrPolicyBuilder) AllowNoAttrs() *attrPolicyBuilder {
|
||||
}
|
||||
|
||||
// Matching allows a regular expression to be applied to a nascent attribute
|
||||
// policy, and returns the attribute policy. Calling this more than once will
|
||||
// replace the existing regexp.
|
||||
// policy, and returns the attribute policy.
|
||||
func (abp *attrPolicyBuilder) Matching(regex *regexp.Regexp) *attrPolicyBuilder {
|
||||
|
||||
abp.regexp = regex
|
||||
@@ -281,7 +304,7 @@ func (abp *attrPolicyBuilder) OnElements(elements ...string) *Policy {
|
||||
for _, attr := range abp.attrNames {
|
||||
|
||||
if _, ok := abp.p.elsAndAttrs[element]; !ok {
|
||||
abp.p.elsAndAttrs[element] = make(map[string]attrPolicy)
|
||||
abp.p.elsAndAttrs[element] = make(map[string][]attrPolicy)
|
||||
}
|
||||
|
||||
ap := attrPolicy{}
|
||||
@@ -289,14 +312,14 @@ func (abp *attrPolicyBuilder) OnElements(elements ...string) *Policy {
|
||||
ap.regexp = abp.regexp
|
||||
}
|
||||
|
||||
abp.p.elsAndAttrs[element][attr] = ap
|
||||
abp.p.elsAndAttrs[element][attr] = append(abp.p.elsAndAttrs[element][attr], ap)
|
||||
}
|
||||
|
||||
if abp.allowEmpty {
|
||||
abp.p.setOfElementsAllowedWithoutAttrs[element] = struct{}{}
|
||||
|
||||
if _, ok := abp.p.elsAndAttrs[element]; !ok {
|
||||
abp.p.elsAndAttrs[element] = make(map[string]attrPolicy)
|
||||
abp.p.elsAndAttrs[element] = make(map[string][]attrPolicy)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -309,19 +332,19 @@ func (abp *attrPolicyBuilder) OnElements(elements ...string) *Policy {
|
||||
func (abp *attrPolicyBuilder) OnElementsMatching(regex *regexp.Regexp) *Policy {
|
||||
for _, attr := range abp.attrNames {
|
||||
if _, ok := abp.p.elsMatchingAndAttrs[regex]; !ok {
|
||||
abp.p.elsMatchingAndAttrs[regex] = make(map[string]attrPolicy)
|
||||
abp.p.elsMatchingAndAttrs[regex] = make(map[string][]attrPolicy)
|
||||
}
|
||||
ap := attrPolicy{}
|
||||
if abp.regexp != nil {
|
||||
ap.regexp = abp.regexp
|
||||
}
|
||||
abp.p.elsMatchingAndAttrs[regex][attr] = ap
|
||||
abp.p.elsMatchingAndAttrs[regex][attr] = append(abp.p.elsMatchingAndAttrs[regex][attr], ap)
|
||||
}
|
||||
|
||||
if abp.allowEmpty {
|
||||
abp.p.setOfElementsMatchingAllowedWithoutAttrs = append(abp.p.setOfElementsMatchingAllowedWithoutAttrs, regex)
|
||||
if _, ok := abp.p.elsMatchingAndAttrs[regex]; !ok {
|
||||
abp.p.elsMatchingAndAttrs[regex] = make(map[string]attrPolicy)
|
||||
abp.p.elsMatchingAndAttrs[regex] = make(map[string][]attrPolicy)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,7 +357,7 @@ func (abp *attrPolicyBuilder) Globally() *Policy {
|
||||
|
||||
for _, attr := range abp.attrNames {
|
||||
if _, ok := abp.p.globalAttrs[attr]; !ok {
|
||||
abp.p.globalAttrs[attr] = attrPolicy{}
|
||||
abp.p.globalAttrs[attr] = []attrPolicy{}
|
||||
}
|
||||
|
||||
ap := attrPolicy{}
|
||||
@@ -342,7 +365,7 @@ func (abp *attrPolicyBuilder) Globally() *Policy {
|
||||
ap.regexp = abp.regexp
|
||||
}
|
||||
|
||||
abp.p.globalAttrs[attr] = ap
|
||||
abp.p.globalAttrs[attr] = append(abp.p.globalAttrs[attr], ap)
|
||||
}
|
||||
|
||||
return abp.p
|
||||
@@ -350,7 +373,7 @@ func (abp *attrPolicyBuilder) Globally() *Policy {
|
||||
|
||||
// AllowStyles takes a range of CSS property names and returns a
|
||||
// style policy builder that allows you to specify the pattern and scope of
|
||||
// the whitelisted property.
|
||||
// the allowed property.
|
||||
//
|
||||
// The style policy is only added to the core policy when either Globally()
|
||||
// or OnElements(...) are called.
|
||||
@@ -370,8 +393,7 @@ func (p *Policy) AllowStyles(propertyNames ...string) *stylePolicyBuilder {
|
||||
}
|
||||
|
||||
// Matching allows a regular expression to be applied to a nascent style
|
||||
// policy, and returns the style policy. Calling this more than once will
|
||||
// replace the existing regexp.
|
||||
// policy, and returns the style policy.
|
||||
func (spb *stylePolicyBuilder) Matching(regex *regexp.Regexp) *stylePolicyBuilder {
|
||||
|
||||
spb.regexp = regex
|
||||
@@ -380,8 +402,7 @@ func (spb *stylePolicyBuilder) Matching(regex *regexp.Regexp) *stylePolicyBuilde
|
||||
}
|
||||
|
||||
// MatchingEnum allows a list of allowed values to be applied to a nascent style
|
||||
// policy, and returns the style policy. Calling this more than once will
|
||||
// replace the existing list of allowed values.
|
||||
// policy, and returns the style policy.
|
||||
func (spb *stylePolicyBuilder) MatchingEnum(enum ...string) *stylePolicyBuilder {
|
||||
|
||||
spb.enum = enum
|
||||
@@ -390,8 +411,7 @@ func (spb *stylePolicyBuilder) MatchingEnum(enum ...string) *stylePolicyBuilder
|
||||
}
|
||||
|
||||
// MatchingHandler allows a handler to be applied to a nascent style
|
||||
// policy, and returns the style policy. Calling this more than once will
|
||||
// replace the existing handler.
|
||||
// policy, and returns the style policy.
|
||||
func (spb *stylePolicyBuilder) MatchingHandler(handler func(string) bool) *stylePolicyBuilder {
|
||||
|
||||
spb.handler = handler
|
||||
@@ -409,7 +429,7 @@ func (spb *stylePolicyBuilder) OnElements(elements ...string) *Policy {
|
||||
for _, attr := range spb.propertyNames {
|
||||
|
||||
if _, ok := spb.p.elsAndStyles[element]; !ok {
|
||||
spb.p.elsAndStyles[element] = make(map[string]stylePolicy)
|
||||
spb.p.elsAndStyles[element] = make(map[string][]stylePolicy)
|
||||
}
|
||||
|
||||
sp := stylePolicy{}
|
||||
@@ -420,9 +440,9 @@ func (spb *stylePolicyBuilder) OnElements(elements ...string) *Policy {
|
||||
} else if spb.regexp != nil {
|
||||
sp.regexp = spb.regexp
|
||||
} else {
|
||||
sp.handler = getDefaultHandler(attr)
|
||||
sp.handler = css.GetDefaultHandler(attr)
|
||||
}
|
||||
spb.p.elsAndStyles[element][attr] = sp
|
||||
spb.p.elsAndStyles[element][attr] = append(spb.p.elsAndStyles[element][attr], sp)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -433,25 +453,25 @@ func (spb *stylePolicyBuilder) OnElements(elements ...string) *Policy {
|
||||
// and return the updated policy
|
||||
func (spb *stylePolicyBuilder) OnElementsMatching(regex *regexp.Regexp) *Policy {
|
||||
|
||||
for _, attr := range spb.propertyNames {
|
||||
for _, attr := range spb.propertyNames {
|
||||
|
||||
if _, ok := spb.p.elsMatchingAndStyles[regex]; !ok {
|
||||
spb.p.elsMatchingAndStyles[regex] = make(map[string]stylePolicy)
|
||||
}
|
||||
|
||||
sp := stylePolicy{}
|
||||
if spb.handler != nil {
|
||||
sp.handler = spb.handler
|
||||
} else if len(spb.enum) > 0 {
|
||||
sp.enum = spb.enum
|
||||
} else if spb.regexp != nil {
|
||||
sp.regexp = spb.regexp
|
||||
} else {
|
||||
sp.handler = getDefaultHandler(attr)
|
||||
}
|
||||
spb.p.elsMatchingAndStyles[regex][attr] = sp
|
||||
if _, ok := spb.p.elsMatchingAndStyles[regex]; !ok {
|
||||
spb.p.elsMatchingAndStyles[regex] = make(map[string][]stylePolicy)
|
||||
}
|
||||
|
||||
sp := stylePolicy{}
|
||||
if spb.handler != nil {
|
||||
sp.handler = spb.handler
|
||||
} else if len(spb.enum) > 0 {
|
||||
sp.enum = spb.enum
|
||||
} else if spb.regexp != nil {
|
||||
sp.regexp = spb.regexp
|
||||
} else {
|
||||
sp.handler = css.GetDefaultHandler(attr)
|
||||
}
|
||||
spb.p.elsMatchingAndStyles[regex][attr] = append(spb.p.elsMatchingAndStyles[regex][attr], sp)
|
||||
}
|
||||
|
||||
return spb.p
|
||||
}
|
||||
|
||||
@@ -461,7 +481,7 @@ func (spb *stylePolicyBuilder) Globally() *Policy {
|
||||
|
||||
for _, attr := range spb.propertyNames {
|
||||
if _, ok := spb.p.globalStyles[attr]; !ok {
|
||||
spb.p.globalStyles[attr] = stylePolicy{}
|
||||
spb.p.globalStyles[attr] = []stylePolicy{}
|
||||
}
|
||||
|
||||
// Use only one strategy for validating styles, fallback to default
|
||||
@@ -473,15 +493,15 @@ func (spb *stylePolicyBuilder) Globally() *Policy {
|
||||
} else if spb.regexp != nil {
|
||||
sp.regexp = spb.regexp
|
||||
} else {
|
||||
sp.handler = getDefaultHandler(attr)
|
||||
sp.handler = css.GetDefaultHandler(attr)
|
||||
}
|
||||
spb.p.globalStyles[attr] = sp
|
||||
spb.p.globalStyles[attr] = append(spb.p.globalStyles[attr], sp)
|
||||
}
|
||||
|
||||
return spb.p
|
||||
}
|
||||
|
||||
// AllowElements will append HTML elements to the whitelist without applying an
|
||||
// AllowElements will append HTML elements to the allowlist without applying an
|
||||
// attribute policy to those elements (the elements are permitted
|
||||
// sans-attributes)
|
||||
func (p *Policy) AllowElements(names ...string) *Policy {
|
||||
@@ -491,17 +511,19 @@ func (p *Policy) AllowElements(names ...string) *Policy {
|
||||
element = strings.ToLower(element)
|
||||
|
||||
if _, ok := p.elsAndAttrs[element]; !ok {
|
||||
p.elsAndAttrs[element] = make(map[string]attrPolicy)
|
||||
p.elsAndAttrs[element] = make(map[string][]attrPolicy)
|
||||
}
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// AllowElementsMatching will append HTML elements to the allowlist if they
|
||||
// match a regexp.
|
||||
func (p *Policy) AllowElementsMatching(regex *regexp.Regexp) *Policy {
|
||||
p.init()
|
||||
if _, ok := p.elsMatchingAndAttrs[regex]; !ok {
|
||||
p.elsMatchingAndAttrs[regex] = make(map[string]attrPolicy)
|
||||
p.elsMatchingAndAttrs[regex] = make(map[string][]attrPolicy)
|
||||
}
|
||||
return p
|
||||
}
|
||||
@@ -558,6 +580,16 @@ func (p *Policy) RequireNoReferrerOnFullyQualifiedLinks(require bool) *Policy {
|
||||
return p
|
||||
}
|
||||
|
||||
// RequireCrossOriginAnonymous will result in all audio, img, link, script, and
|
||||
// video tags having a crossorigin="anonymous" added to them if one does not
|
||||
// already exist
|
||||
func (p *Policy) RequireCrossOriginAnonymous(require bool) *Policy {
|
||||
|
||||
p.requireCrossOriginAnonymous = require
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// AddTargetBlankToFullyQualifiedLinks will result in all a, area and link tags
|
||||
// that point to a non-local destination (i.e. starts with a protocol and has a
|
||||
// host) having a target="_blank" added to them if one does not already exist
|
||||
@@ -598,7 +630,7 @@ func (p *Policy) AllowRelativeURLs(require bool) *Policy {
|
||||
return p
|
||||
}
|
||||
|
||||
// AllowURLSchemes will append URL schemes to the whitelist
|
||||
// AllowURLSchemes will append URL schemes to the allowlist
|
||||
// Example: p.AllowURLSchemes("mailto", "http", "https")
|
||||
func (p *Policy) AllowURLSchemes(schemes ...string) *Policy {
|
||||
p.init()
|
||||
@@ -616,7 +648,7 @@ func (p *Policy) AllowURLSchemes(schemes ...string) *Policy {
|
||||
}
|
||||
|
||||
// AllowURLSchemeWithCustomPolicy will append URL schemes with
|
||||
// a custom URL policy to the whitelist.
|
||||
// a custom URL policy to the allowlist.
|
||||
// Only the URLs with matching schema and urlPolicy(url)
|
||||
// returning true will be allowed.
|
||||
func (p *Policy) AllowURLSchemeWithCustomPolicy(
|
||||
@@ -630,13 +662,13 @@ func (p *Policy) AllowURLSchemeWithCustomPolicy(
|
||||
|
||||
scheme = strings.ToLower(scheme)
|
||||
|
||||
p.allowURLSchemes[scheme] = urlPolicy
|
||||
p.allowURLSchemes[scheme] = append(p.allowURLSchemes[scheme], urlPolicy)
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// AddSpaceWhenStrippingTag states whether to add a single space " " when
|
||||
// removing tags that are not whitelisted by the policy.
|
||||
// removing tags that are not allowed by the policy.
|
||||
//
|
||||
// This is useful if you expect to strip tags in dense markup and may lose the
|
||||
// value of whitespace.
|
||||
|
||||
474
vendor/github.com/microcosm-cc/bluemonday/sanitize.go
generated
vendored
474
vendor/github.com/microcosm-cc/bluemonday/sanitize.go
generated
vendored
@@ -31,6 +31,7 @@ package bluemonday
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"regexp"
|
||||
@@ -39,7 +40,7 @@ import (
|
||||
|
||||
"golang.org/x/net/html"
|
||||
|
||||
cssparser "github.com/chris-ramon/douceur/parser"
|
||||
"github.com/aymerick/douceur/parser"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -47,10 +48,11 @@ var (
|
||||
dataAttributeXMLPrefix = regexp.MustCompile("^xml.+")
|
||||
dataAttributeInvalidChars = regexp.MustCompile("[A-Z;]+")
|
||||
cssUnicodeChar = regexp.MustCompile(`\\[0-9a-f]{1,6} ?`)
|
||||
dataURIbase64Prefix = regexp.MustCompile(`^data:[^,]*;base64,`)
|
||||
)
|
||||
|
||||
// Sanitize takes a string that contains a HTML fragment or document and applies
|
||||
// the given policy whitelist.
|
||||
// the given policy allowlist.
|
||||
//
|
||||
// It returns a HTML string that has been sanitized by the policy or an empty
|
||||
// string if an error has occurred (most likely as a consequence of extremely
|
||||
@@ -60,11 +62,11 @@ func (p *Policy) Sanitize(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
return p.sanitize(strings.NewReader(s)).String()
|
||||
return p.sanitizeWithBuff(strings.NewReader(s)).String()
|
||||
}
|
||||
|
||||
// SanitizeBytes takes a []byte that contains a HTML fragment or document and applies
|
||||
// the given policy whitelist.
|
||||
// the given policy allowlist.
|
||||
//
|
||||
// It returns a []byte containing the HTML that has been sanitized by the policy
|
||||
// or an empty []byte if an error has occurred (most likely as a consequence of
|
||||
@@ -74,26 +76,32 @@ func (p *Policy) SanitizeBytes(b []byte) []byte {
|
||||
return b
|
||||
}
|
||||
|
||||
return p.sanitize(bytes.NewReader(b)).Bytes()
|
||||
return p.sanitizeWithBuff(bytes.NewReader(b)).Bytes()
|
||||
}
|
||||
|
||||
// SanitizeReader takes an io.Reader that contains a HTML fragment or document
|
||||
// and applies the given policy whitelist.
|
||||
// and applies the given policy allowlist.
|
||||
//
|
||||
// It returns a bytes.Buffer containing the HTML that has been sanitized by the
|
||||
// policy. Errors during sanitization will merely return an empty result.
|
||||
func (p *Policy) SanitizeReader(r io.Reader) *bytes.Buffer {
|
||||
return p.sanitize(r)
|
||||
return p.sanitizeWithBuff(r)
|
||||
}
|
||||
|
||||
// SanitizeReaderToWriter takes an io.Reader that contains a HTML fragment or document
|
||||
// and applies the given policy allowlist and writes to the provided writer returning
|
||||
// an error if there is one.
|
||||
func (p *Policy) SanitizeReaderToWriter(r io.Reader, w io.Writer) error {
|
||||
return p.sanitize(r, w)
|
||||
}
|
||||
|
||||
const escapedURLChars = "'<>\"\r"
|
||||
|
||||
func escapeUrlComponent(val string) string {
|
||||
w := bytes.NewBufferString("")
|
||||
func escapeUrlComponent(w stringWriterWriter, val string) error {
|
||||
i := strings.IndexAny(val, escapedURLChars)
|
||||
for i != -1 {
|
||||
if _, err := w.WriteString(val[:i]); err != nil {
|
||||
return w.String()
|
||||
return err
|
||||
}
|
||||
var esc string
|
||||
switch val[i] {
|
||||
@@ -114,73 +122,119 @@ func escapeUrlComponent(val string) string {
|
||||
}
|
||||
val = val[i+1:]
|
||||
if _, err := w.WriteString(esc); err != nil {
|
||||
return w.String()
|
||||
return err
|
||||
}
|
||||
i = strings.IndexAny(val, escapedURLChars)
|
||||
}
|
||||
w.WriteString(val)
|
||||
return w.String()
|
||||
_, err := w.WriteString(val)
|
||||
return err
|
||||
}
|
||||
|
||||
func sanitizedUrl(val string) (string, error) {
|
||||
// Query represents a single part of the query string, a query param
|
||||
type Query struct {
|
||||
Key string
|
||||
Value string
|
||||
HasValue bool
|
||||
}
|
||||
|
||||
func parseQuery(query string) (values []Query, err error) {
|
||||
// This is essentially a copy of parseQuery from
|
||||
// https://golang.org/src/net/url/url.go but adjusted to build our values
|
||||
// based on our type, which we need to preserve the ordering of the query
|
||||
// string
|
||||
for query != "" {
|
||||
key := query
|
||||
if i := strings.IndexAny(key, "&;"); i >= 0 {
|
||||
key, query = key[:i], key[i+1:]
|
||||
} else {
|
||||
query = ""
|
||||
}
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
value := ""
|
||||
hasValue := false
|
||||
if i := strings.Index(key, "="); i >= 0 {
|
||||
key, value = key[:i], key[i+1:]
|
||||
hasValue = true
|
||||
}
|
||||
key, err1 := url.QueryUnescape(key)
|
||||
if err1 != nil {
|
||||
if err == nil {
|
||||
err = err1
|
||||
}
|
||||
continue
|
||||
}
|
||||
value, err1 = url.QueryUnescape(value)
|
||||
if err1 != nil {
|
||||
if err == nil {
|
||||
err = err1
|
||||
}
|
||||
continue
|
||||
}
|
||||
values = append(values, Query{
|
||||
Key: key,
|
||||
Value: value,
|
||||
HasValue: hasValue,
|
||||
})
|
||||
}
|
||||
return values, err
|
||||
}
|
||||
|
||||
func encodeQueries(queries []Query) string {
|
||||
var buff bytes.Buffer
|
||||
for i, query := range queries {
|
||||
buff.WriteString(url.QueryEscape(query.Key))
|
||||
if query.HasValue {
|
||||
buff.WriteString("=")
|
||||
buff.WriteString(url.QueryEscape(query.Value))
|
||||
}
|
||||
if i < len(queries)-1 {
|
||||
buff.WriteString("&")
|
||||
}
|
||||
}
|
||||
return buff.String()
|
||||
}
|
||||
|
||||
func sanitizedURL(val string) (string, error) {
|
||||
u, err := url.Parse(val)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// sanitize the url query params
|
||||
sanitizedQueryValues := make(url.Values, 0)
|
||||
queryValues := u.Query()
|
||||
for k, vals := range queryValues {
|
||||
sk := html.EscapeString(k)
|
||||
for _, v := range vals {
|
||||
sv := v
|
||||
sanitizedQueryValues.Add(sk, sv)
|
||||
}
|
||||
|
||||
// we use parseQuery but not u.Query to keep the order not change because
|
||||
// url.Values is a map which has a random order.
|
||||
queryValues, err := parseQuery(u.RawQuery)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
u.RawQuery = sanitizedQueryValues.Encode()
|
||||
// sanitize the url query params
|
||||
for i, query := range queryValues {
|
||||
queryValues[i].Key = html.EscapeString(query.Key)
|
||||
}
|
||||
u.RawQuery = encodeQueries(queryValues)
|
||||
// u.String() will also sanitize host/scheme/user/pass
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func (p *Policy) writeLinkableBuf(buff *bytes.Buffer, token *html.Token) {
|
||||
// do not escape multiple query parameters
|
||||
tokenBuff := bytes.NewBufferString("")
|
||||
tokenBuff.WriteString("<")
|
||||
tokenBuff.WriteString(token.Data)
|
||||
for _, attr := range token.Attr {
|
||||
tokenBuff.WriteByte(' ')
|
||||
tokenBuff.WriteString(attr.Key)
|
||||
tokenBuff.WriteString(`="`)
|
||||
switch attr.Key {
|
||||
case "href", "src":
|
||||
u, ok := p.validURL(attr.Val)
|
||||
if !ok {
|
||||
tokenBuff.WriteString(html.EscapeString(attr.Val))
|
||||
continue
|
||||
}
|
||||
u, err := sanitizedUrl(u)
|
||||
if err == nil {
|
||||
tokenBuff.WriteString(u)
|
||||
} else {
|
||||
// fallthrough
|
||||
tokenBuff.WriteString(html.EscapeString(attr.Val))
|
||||
}
|
||||
default:
|
||||
// re-apply
|
||||
tokenBuff.WriteString(html.EscapeString(attr.Val))
|
||||
}
|
||||
tokenBuff.WriteByte('"')
|
||||
// Performs the actual sanitization process.
|
||||
func (p *Policy) sanitizeWithBuff(r io.Reader) *bytes.Buffer {
|
||||
var buff bytes.Buffer
|
||||
if err := p.sanitize(r, &buff); err != nil {
|
||||
return &bytes.Buffer{}
|
||||
}
|
||||
if token.Type == html.SelfClosingTagToken {
|
||||
tokenBuff.WriteString("/")
|
||||
}
|
||||
tokenBuff.WriteString(">")
|
||||
buff.WriteString(tokenBuff.String())
|
||||
return &buff
|
||||
}
|
||||
|
||||
// Performs the actual sanitization process.
|
||||
func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
type asStringWriter struct {
|
||||
io.Writer
|
||||
}
|
||||
|
||||
func (a *asStringWriter) WriteString(s string) (int, error) {
|
||||
return a.Write([]byte(s))
|
||||
}
|
||||
|
||||
func (p *Policy) sanitize(r io.Reader, w io.Writer) error {
|
||||
// It is possible that the developer has created the policy via:
|
||||
// p := bluemonday.Policy{}
|
||||
// rather than:
|
||||
@@ -189,8 +243,12 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
// would initiliaze the maps, then we need to do that.
|
||||
p.init()
|
||||
|
||||
buff, ok := w.(stringWriterWriter)
|
||||
if !ok {
|
||||
buff = &asStringWriter{w}
|
||||
}
|
||||
|
||||
var (
|
||||
buff bytes.Buffer
|
||||
skipElementContent bool
|
||||
skippingElementsCount int64
|
||||
skipClosingTag bool
|
||||
@@ -204,11 +262,11 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
err := tokenizer.Err()
|
||||
if err == io.EOF {
|
||||
// End of input means end of processing
|
||||
return &buff
|
||||
return nil
|
||||
}
|
||||
|
||||
// Raw tokenizer error
|
||||
return &bytes.Buffer{}
|
||||
return err
|
||||
}
|
||||
|
||||
token := tokenizer.Token()
|
||||
@@ -226,10 +284,14 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
case html.CommentToken:
|
||||
|
||||
// Comments are ignored by default
|
||||
if p.allowComments {
|
||||
// But if allowed then write the comment out as-is
|
||||
buff.WriteString(token.String())
|
||||
}
|
||||
|
||||
case html.StartTagToken:
|
||||
|
||||
mostRecentlyStartedToken = strings.ToLower(token.Data)
|
||||
mostRecentlyStartedToken = normaliseElementName(token.Data)
|
||||
|
||||
aps, ok := p.elsAndAttrs[token.Data]
|
||||
if !ok {
|
||||
@@ -240,14 +302,18 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
skippingElementsCount++
|
||||
}
|
||||
if p.addSpaces {
|
||||
buff.WriteString(" ")
|
||||
if _, err := buff.WriteString(" "); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
aps = aa
|
||||
}
|
||||
if len(token.Attr) != 0 {
|
||||
token.Attr = p.sanitizeAttrs(token.Data, token.Attr, aps)
|
||||
token.Attr = escapeAttributes(
|
||||
p.sanitizeAttrs(token.Data, token.Attr, aps),
|
||||
)
|
||||
}
|
||||
|
||||
if len(token.Attr) == 0 {
|
||||
@@ -255,24 +321,23 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
skipClosingTag = true
|
||||
closingTagToSkipStack = append(closingTagToSkipStack, token.Data)
|
||||
if p.addSpaces {
|
||||
buff.WriteString(" ")
|
||||
if _, err := buff.WriteString(" "); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !skipElementContent {
|
||||
// do not escape multiple query parameters
|
||||
if linkable(token.Data) {
|
||||
p.writeLinkableBuf(&buff, &token)
|
||||
} else {
|
||||
buff.WriteString(token.String())
|
||||
if _, err := buff.WriteString(token.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
case html.EndTagToken:
|
||||
|
||||
if mostRecentlyStartedToken == strings.ToLower(token.Data) {
|
||||
if mostRecentlyStartedToken == normaliseElementName(token.Data) {
|
||||
mostRecentlyStartedToken = ""
|
||||
}
|
||||
|
||||
@@ -282,7 +347,9 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
skipClosingTag = false
|
||||
}
|
||||
if p.addSpaces {
|
||||
buff.WriteString(" ")
|
||||
if _, err := buff.WriteString(" "); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -303,14 +370,18 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
}
|
||||
if !match {
|
||||
if p.addSpaces {
|
||||
buff.WriteString(" ")
|
||||
if _, err := buff.WriteString(" "); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !skipElementContent {
|
||||
buff.WriteString(token.String())
|
||||
if _, err := buff.WriteString(token.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
case html.SelfClosingTagToken:
|
||||
@@ -320,7 +391,9 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
aa, matched := p.matchRegex(token.Data)
|
||||
if !matched {
|
||||
if p.addSpaces && !matched {
|
||||
buff.WriteString(" ")
|
||||
if _, err := buff.WriteString(" "); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -328,21 +401,20 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
}
|
||||
|
||||
if len(token.Attr) != 0 {
|
||||
token.Attr = p.sanitizeAttrs(token.Data, token.Attr, aps)
|
||||
token.Attr = escapeAttributes(p.sanitizeAttrs(token.Data, token.Attr, aps))
|
||||
}
|
||||
|
||||
if len(token.Attr) == 0 && !p.allowNoAttrs(token.Data) {
|
||||
if p.addSpaces {
|
||||
buff.WriteString(" ")
|
||||
if _, err := buff.WriteString(" "); err != nil {
|
||||
return err
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if !skipElementContent {
|
||||
// do not escape multiple query parameters
|
||||
if linkable(token.Data) {
|
||||
p.writeLinkableBuf(&buff, &token)
|
||||
} else {
|
||||
buff.WriteString(token.String())
|
||||
if _, err := buff.WriteString(token.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,23 +422,29 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
|
||||
if !skipElementContent {
|
||||
switch mostRecentlyStartedToken {
|
||||
case "script":
|
||||
case `script`:
|
||||
// not encouraged, but if a policy allows JavaScript we
|
||||
// should not HTML escape it as that would break the output
|
||||
buff.WriteString(token.Data)
|
||||
if _, err := buff.WriteString(token.Data); err != nil {
|
||||
return err
|
||||
}
|
||||
case "style":
|
||||
// not encouraged, but if a policy allows CSS styles we
|
||||
// should not HTML escape it as that would break the output
|
||||
buff.WriteString(token.Data)
|
||||
if _, err := buff.WriteString(token.Data); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
// HTML escape the text
|
||||
buff.WriteString(token.String())
|
||||
if _, err := buff.WriteString(token.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
// A token that didn't exist in the html package when we wrote this
|
||||
return &bytes.Buffer{}
|
||||
return fmt.Errorf("unknown token: %v", token)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -377,7 +455,7 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
|
||||
func (p *Policy) sanitizeAttrs(
|
||||
elementName string,
|
||||
attrs []html.Attribute,
|
||||
aps map[string]attrPolicy,
|
||||
aps map[string][]attrPolicy,
|
||||
) []html.Attribute {
|
||||
|
||||
if len(attrs) == 0 {
|
||||
@@ -402,8 +480,9 @@ func (p *Policy) sanitizeAttrs(
|
||||
}
|
||||
|
||||
// Builds a new attribute slice based on the whether the attribute has been
|
||||
// whitelisted explicitly or globally.
|
||||
// allowed explicitly or globally.
|
||||
cleanAttrs := []html.Attribute{}
|
||||
attrsLoop:
|
||||
for _, htmlAttr := range attrs {
|
||||
if p.allowDataAttributes {
|
||||
// If we see a data attribute, let it through.
|
||||
@@ -426,27 +505,32 @@ func (p *Policy) sanitizeAttrs(
|
||||
}
|
||||
|
||||
// Is there an element specific attribute policy that applies?
|
||||
if ap, ok := aps[htmlAttr.Key]; ok {
|
||||
if ap.regexp != nil {
|
||||
if ap.regexp.MatchString(htmlAttr.Val) {
|
||||
if apl, ok := aps[htmlAttr.Key]; ok {
|
||||
for _, ap := range apl {
|
||||
if ap.regexp != nil {
|
||||
if ap.regexp.MatchString(htmlAttr.Val) {
|
||||
cleanAttrs = append(cleanAttrs, htmlAttr)
|
||||
continue attrsLoop
|
||||
}
|
||||
} else {
|
||||
cleanAttrs = append(cleanAttrs, htmlAttr)
|
||||
continue
|
||||
continue attrsLoop
|
||||
}
|
||||
} else {
|
||||
cleanAttrs = append(cleanAttrs, htmlAttr)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Is there a global attribute policy that applies?
|
||||
if ap, ok := p.globalAttrs[htmlAttr.Key]; ok {
|
||||
|
||||
if ap.regexp != nil {
|
||||
if ap.regexp.MatchString(htmlAttr.Val) {
|
||||
if apl, ok := p.globalAttrs[htmlAttr.Key]; ok {
|
||||
for _, ap := range apl {
|
||||
if ap.regexp != nil {
|
||||
if ap.regexp.MatchString(htmlAttr.Val) {
|
||||
htmlAttr.Val = escapeAttribute(htmlAttr.Val)
|
||||
cleanAttrs = append(cleanAttrs, htmlAttr)
|
||||
}
|
||||
} else {
|
||||
htmlAttr.Val = escapeAttribute(htmlAttr.Val)
|
||||
cleanAttrs = append(cleanAttrs, htmlAttr)
|
||||
}
|
||||
} else {
|
||||
cleanAttrs = append(cleanAttrs, htmlAttr)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -470,7 +554,7 @@ func (p *Policy) sanitizeAttrs(
|
||||
tmpAttrs := []html.Attribute{}
|
||||
for _, htmlAttr := range cleanAttrs {
|
||||
switch elementName {
|
||||
case "a", "area", "link":
|
||||
case "a", "area", "base", "link":
|
||||
if htmlAttr.Key == "href" {
|
||||
if u, ok := p.validURL(htmlAttr.Val); ok {
|
||||
htmlAttr.Val = u
|
||||
@@ -479,7 +563,7 @@ func (p *Policy) sanitizeAttrs(
|
||||
break
|
||||
}
|
||||
tmpAttrs = append(tmpAttrs, htmlAttr)
|
||||
case "blockquote", "q":
|
||||
case "blockquote", "del", "ins", "q":
|
||||
if htmlAttr.Key == "cite" {
|
||||
if u, ok := p.validURL(htmlAttr.Val); ok {
|
||||
htmlAttr.Val = u
|
||||
@@ -488,7 +572,7 @@ func (p *Policy) sanitizeAttrs(
|
||||
break
|
||||
}
|
||||
tmpAttrs = append(tmpAttrs, htmlAttr)
|
||||
case "img", "script":
|
||||
case "audio", "embed", "iframe", "img", "script", "source", "track", "video":
|
||||
if htmlAttr.Key == "src" {
|
||||
if u, ok := p.validURL(htmlAttr.Val); ok {
|
||||
htmlAttr.Val = u
|
||||
@@ -513,7 +597,7 @@ func (p *Policy) sanitizeAttrs(
|
||||
|
||||
// Add rel="nofollow" if a "href" exists
|
||||
switch elementName {
|
||||
case "a", "area", "link":
|
||||
case "a", "area", "base", "link":
|
||||
var hrefFound bool
|
||||
var externalLink bool
|
||||
for _, htmlAttr := range cleanAttrs {
|
||||
@@ -664,20 +748,40 @@ func (p *Policy) sanitizeAttrs(
|
||||
}
|
||||
}
|
||||
|
||||
if p.requireCrossOriginAnonymous && len(cleanAttrs) > 0 {
|
||||
switch elementName {
|
||||
case "audio", "img", "link", "script", "video":
|
||||
var crossOriginFound bool
|
||||
for _, htmlAttr := range cleanAttrs {
|
||||
if htmlAttr.Key == "crossorigin" {
|
||||
crossOriginFound = true
|
||||
htmlAttr.Val = "anonymous"
|
||||
}
|
||||
}
|
||||
|
||||
if !crossOriginFound {
|
||||
crossOrigin := html.Attribute{}
|
||||
crossOrigin.Key = "crossorigin"
|
||||
crossOrigin.Val = "anonymous"
|
||||
cleanAttrs = append(cleanAttrs, crossOrigin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cleanAttrs
|
||||
}
|
||||
|
||||
func (p *Policy) sanitizeStyles(attr html.Attribute, elementName string) html.Attribute {
|
||||
sps := p.elsAndStyles[elementName]
|
||||
if len(sps) == 0 {
|
||||
sps = map[string]stylePolicy{}
|
||||
sps = map[string][]stylePolicy{}
|
||||
// check for any matching elements, if we don't already have a policy found
|
||||
// if multiple matches are found they will be overwritten, it's best
|
||||
// to not have overlapping matchers
|
||||
for regex, policies := range p.elsMatchingAndStyles {
|
||||
if regex.MatchString(elementName) {
|
||||
for k, v := range policies {
|
||||
sps[k] = v
|
||||
sps[k] = append(sps[k], v...)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -687,7 +791,7 @@ func (p *Policy) sanitizeStyles(attr html.Attribute, elementName string) html.At
|
||||
if len(attr.Val) > 0 && attr.Val[len(attr.Val)-1] != ';' {
|
||||
attr.Val = attr.Val + ";"
|
||||
}
|
||||
decs, err := cssparser.ParseDeclarations(attr.Val)
|
||||
decs, err := parser.ParseDeclarations(attr.Val)
|
||||
if err != nil {
|
||||
attr.Val = ""
|
||||
return attr
|
||||
@@ -695,46 +799,51 @@ func (p *Policy) sanitizeStyles(attr html.Attribute, elementName string) html.At
|
||||
clean := []string{}
|
||||
prefixes := []string{"-webkit-", "-moz-", "-ms-", "-o-", "mso-", "-xv-", "-atsc-", "-wap-", "-khtml-", "prince-", "-ah-", "-hp-", "-ro-", "-rim-", "-tc-"}
|
||||
|
||||
decLoop:
|
||||
for _, dec := range decs {
|
||||
addedProperty := false
|
||||
tempProperty := strings.ToLower(dec.Property)
|
||||
tempValue := removeUnicode(strings.ToLower(dec.Value))
|
||||
for _, i := range prefixes {
|
||||
tempProperty = strings.TrimPrefix(tempProperty, i)
|
||||
}
|
||||
if sp, ok := sps[tempProperty]; ok {
|
||||
if sp.handler != nil {
|
||||
if sp.handler(tempValue) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
addedProperty = true
|
||||
if spl, ok := sps[tempProperty]; ok {
|
||||
for _, sp := range spl {
|
||||
if sp.handler != nil {
|
||||
if sp.handler(tempValue) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
continue decLoop
|
||||
}
|
||||
} else if len(sp.enum) > 0 {
|
||||
if stringInSlice(tempValue, sp.enum) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
continue decLoop
|
||||
}
|
||||
} else if sp.regexp != nil {
|
||||
if sp.regexp.MatchString(tempValue) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
continue decLoop
|
||||
}
|
||||
}
|
||||
} else if len(sp.enum) > 0 {
|
||||
if stringInSlice(tempValue, sp.enum) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
addedProperty = true
|
||||
}
|
||||
} else if sp.regexp != nil {
|
||||
if sp.regexp.MatchString(tempValue) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
addedProperty = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
if sp, ok := p.globalStyles[tempProperty]; ok && !addedProperty {
|
||||
if sp.handler != nil {
|
||||
if sp.handler(tempValue) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
if spl, ok := p.globalStyles[tempProperty]; ok {
|
||||
for _, sp := range spl {
|
||||
if sp.handler != nil {
|
||||
if sp.handler(tempValue) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
continue decLoop
|
||||
}
|
||||
} else if len(sp.enum) > 0 {
|
||||
if stringInSlice(tempValue, sp.enum) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
continue decLoop
|
||||
}
|
||||
} else if sp.regexp != nil {
|
||||
if sp.regexp.MatchString(tempValue) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
continue decLoop
|
||||
}
|
||||
}
|
||||
} else if len(sp.enum) > 0 {
|
||||
if stringInSlice(tempValue, sp.enum) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
}
|
||||
} else if sp.regexp != nil {
|
||||
if sp.regexp.MatchString(tempValue) {
|
||||
clean = append(clean, dec.Property+": "+dec.Value)
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -765,11 +874,28 @@ func (p *Policy) validURL(rawurl string) (string, bool) {
|
||||
rawurl = strings.TrimSpace(rawurl)
|
||||
|
||||
// URLs cannot contain whitespace, unless it is a data-uri
|
||||
if (strings.Contains(rawurl, " ") ||
|
||||
if strings.Contains(rawurl, " ") ||
|
||||
strings.Contains(rawurl, "\t") ||
|
||||
strings.Contains(rawurl, "\n")) &&
|
||||
!strings.HasPrefix(rawurl, `data:`) {
|
||||
return "", false
|
||||
strings.Contains(rawurl, "\n") {
|
||||
if !strings.HasPrefix(rawurl, `data:`) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Remove \r and \n from base64 encoded data to pass url.Parse.
|
||||
matched := dataURIbase64Prefix.FindString(rawurl)
|
||||
if matched != "" {
|
||||
rawurl = matched + strings.Replace(
|
||||
strings.Replace(
|
||||
rawurl[len(matched):],
|
||||
"\r",
|
||||
"",
|
||||
-1,
|
||||
),
|
||||
"\n",
|
||||
"",
|
||||
-1,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// URLs are valid if they parse
|
||||
@@ -780,16 +906,21 @@ func (p *Policy) validURL(rawurl string) (string, bool) {
|
||||
|
||||
if u.Scheme != "" {
|
||||
|
||||
urlPolicy, ok := p.allowURLSchemes[u.Scheme]
|
||||
urlPolicies, ok := p.allowURLSchemes[u.Scheme]
|
||||
if !ok {
|
||||
return "", false
|
||||
|
||||
}
|
||||
|
||||
if urlPolicy == nil || urlPolicy(u) == true {
|
||||
if len(urlPolicies) == 0 {
|
||||
return u.String(), true
|
||||
}
|
||||
|
||||
for _, urlPolicy := range urlPolicies {
|
||||
if urlPolicy(u) == true {
|
||||
return u.String(), true
|
||||
}
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
@@ -807,7 +938,14 @@ func (p *Policy) validURL(rawurl string) (string, bool) {
|
||||
|
||||
func linkable(elementName string) bool {
|
||||
switch elementName {
|
||||
case "a", "area", "blockquote", "img", "link", "script":
|
||||
case "a", "area", "base", "link":
|
||||
// elements that allow .href
|
||||
return true
|
||||
case "blockquote", "del", "ins", "q":
|
||||
// elements that allow .cite
|
||||
return true
|
||||
case "audio", "embed", "iframe", "img", "input", "script", "track", "video":
|
||||
// elements that allow .src
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
@@ -874,16 +1012,50 @@ func removeUnicode(value string) string {
|
||||
return substitutedValue
|
||||
}
|
||||
|
||||
func (p *Policy) matchRegex(elementName string) (map[string]attrPolicy, bool) {
|
||||
aps := make(map[string]attrPolicy, 0)
|
||||
func (p *Policy) matchRegex(elementName string) (map[string][]attrPolicy, bool) {
|
||||
aps := make(map[string][]attrPolicy, 0)
|
||||
matched := false
|
||||
for regex, attrs := range p.elsMatchingAndAttrs {
|
||||
if regex.MatchString(elementName) {
|
||||
matched = true
|
||||
for k, v := range attrs {
|
||||
aps[k] = v
|
||||
aps[k] = append(aps[k], v...)
|
||||
}
|
||||
}
|
||||
}
|
||||
return aps, matched
|
||||
}
|
||||
|
||||
// normaliseElementName takes a HTML element like <script> which is user input
|
||||
// and returns a lower case version of it that is immune to UTF-8 to ASCII
|
||||
// conversion tricks (like the use of upper case cyrillic i scrİpt which a
|
||||
// strings.ToLower would convert to script). Instead this func will preserve
|
||||
// all non-ASCII as their escaped equivalent, i.e. \u0130 which reveals the
|
||||
// characters when lower cased
|
||||
func normaliseElementName(str string) string {
|
||||
// that useful QuoteToASCII put quote marks at the start and end
|
||||
// so those are trimmed off
|
||||
return strings.TrimSuffix(
|
||||
strings.TrimPrefix(
|
||||
strings.ToLower(
|
||||
strconv.QuoteToASCII(str),
|
||||
),
|
||||
`"`),
|
||||
`"`,
|
||||
)
|
||||
}
|
||||
|
||||
func escapeAttributes(attrs []html.Attribute) []html.Attribute {
|
||||
escapedAttrs := []html.Attribute{}
|
||||
for _, attr := range attrs {
|
||||
attr.Val = escapeAttribute(attr.Val)
|
||||
escapedAttrs = append(escapedAttrs, attr)
|
||||
}
|
||||
return escapedAttrs
|
||||
}
|
||||
|
||||
func escapeAttribute(val string) string {
|
||||
val = strings.Replace(val, string([]rune{'\u00A0'}), ` `, -1)
|
||||
val = strings.Replace(val, `"`, `"`, -1)
|
||||
return val
|
||||
}
|
||||
10
vendor/github.com/microcosm-cc/bluemonday/stringwriterwriter_go1.12.go
generated
vendored
Normal file
10
vendor/github.com/microcosm-cc/bluemonday/stringwriterwriter_go1.12.go
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
// +build go1.12
|
||||
|
||||
package bluemonday
|
||||
|
||||
import "io"
|
||||
|
||||
type stringWriterWriter interface {
|
||||
io.Writer
|
||||
io.StringWriter
|
||||
}
|
||||
14
vendor/github.com/microcosm-cc/bluemonday/stringwriterwriter_ltgo1.12.go
generated
vendored
Normal file
14
vendor/github.com/microcosm-cc/bluemonday/stringwriterwriter_ltgo1.12.go
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
// +build go1.1,!go1.12
|
||||
|
||||
package bluemonday
|
||||
|
||||
import "io"
|
||||
|
||||
type stringWriterWriter interface {
|
||||
io.Writer
|
||||
StringWriter
|
||||
}
|
||||
|
||||
type StringWriter interface {
|
||||
WriteString(s string) (n int, err error)
|
||||
}
|
||||
Reference in New Issue
Block a user