// Copyright (c) 2014, David Kitchen // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * Neither the name of the organisation (Microcosm) nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // 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 //TODO sgutzwiller create map of styles to default handlers //TODO sgutzwiller create handlers for various attributes import ( "net/url" "regexp" "strings" "github.com/microcosm-cc/bluemonday/css" ) // 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 // unexported fields contain maps that need to be initialized. type Policy struct { // Declares whether the maps have been initialized, used as a cheap check to // ensure that those using Policy{} directly won't cause nil pointer // exceptions initialized bool // If true then we add spaces when stripping tags, specifically the closing // tag is replaced by a space character. addSpaces bool // When true, add rel="nofollow" to HTML a, area, and link tags requireNoFollow bool // When true, add rel="nofollow" to HTML a, area, and link tags // Will add for href="http://foo" // Will skip for href="/foo" or href="foo" requireNoFollowFullyQualifiedLinks bool // When true, add rel="noreferrer" to HTML a, area, and link tags requireNoReferrer bool // When true, add rel="noreferrer" to HTML a, area, and link tags // Will add for href="http://foo" // 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 and filter sandbox attribute on iframe tags requireSandboxOnIFrame map[string]bool // When true add target="_blank" to fully qualified links // Will add for href="http://foo" // Will skip for href="/foo" or href="foo" addTargetBlankToFullyQualifiedLinks bool // When true, URLs must be parseable by "net/url" url.Parse() requireParseableURLs bool // When true, u, _ := url.Parse("url"); !u.IsAbs() is permitted allowRelativeURLs bool // When true, allow data attributes. allowDataAttributes bool // 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 // map[htmlAttributeName][]attrPolicy globalAttrs map[string][]attrPolicy // map[htmlElementName]map[cssPropertyName][]stylePolicy elsAndStyles map[string]map[string][]stylePolicy // map[regex]map[cssPropertyName][]stylePolicy elsMatchingAndStyles map[*regexp.Regexp]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 // These regexps are used to match allowed URL schemes, for example // if one would want to allow all URL schemes, they would add `.+`. // However pay attention as this can lead to XSS being rendered thus // defeating the purpose of using a HTML sanitizer. // The regexps are only considered if a schema was not explicitly // handled by `AllowURLSchemes` or `AllowURLSchemeWithCustomPolicy`. allowURLSchemeRegexps []*regexp.Regexp // If srcRewriter is not nil, it is used to rewrite the src attribute // of tags that download resources, such as and tag. func (p *Policy) addDefaultSkipElementContent() { p.init() p.setOfElementsToSkipContent["frame"] = struct{}{} p.setOfElementsToSkipContent["frameset"] = struct{}{} p.setOfElementsToSkipContent["iframe"] = struct{}{} p.setOfElementsToSkipContent["noembed"] = struct{}{} p.setOfElementsToSkipContent["noframes"] = struct{}{} p.setOfElementsToSkipContent["noscript"] = struct{}{} p.setOfElementsToSkipContent["nostyle"] = struct{}{} p.setOfElementsToSkipContent["object"] = struct{}{} p.setOfElementsToSkipContent["script"] = struct{}{} p.setOfElementsToSkipContent["style"] = struct{}{} p.setOfElementsToSkipContent["title"] = struct{}{} }