Source File
sanitize.go
Belonging Package
github.com/google/safehtml/template
package template
import (
)
func ( context) ([]string, error) {
switch .state {
case stateTag, stateAttrName, stateAfterName:
return nil, fmt.Errorf("actions must not affect element or attribute names")
case stateHTMLCmt:
return []string{sanitizeHTMLCommentFuncName}, nil
}
return []string{sanitizeHTMLFuncName}, nil
}
return nil, fmt.Errorf("unquoted attribute values disallowed")
}
return sanitizersForAttributeValue()
, := sanitizerForElementContent()
return appendIfNotEmpty([]string{}, ),
}
var , []string
if len(.element.names) == 0 {
= []string{.element.name}
} else {
= .element.names
}
if len(.attr.names) == 0 {
= []string{.attr.name}
} else {
= .attr.names
}
var sanitizationContext
var , string
for , := range {
for , := range {
, := sanitizationContextForAttrVal(, , .linkRel)
if != nil {
if len() == 1 && len() == 1 {
return nil,
}
return nil, fmt.Errorf(`conditional branch with {element=%q, attribute=%q} results in sanitization error: %s`, , , )
}
if == 0 && == 0 {
, , = , ,
continue
}
if != {
return nil, fmt.Errorf(
`conditional branches end in different attribute value sanitization contexts: {element=%q, attribute=%q} has sanitization context %q, {element=%q, attribute=%q} has sanitization context %q`,
, , , , , )
}
}
}
if .isEnum() && .attr.value != "" {
return nil, fmt.Errorf("partial substitutions are disallowed in the %q attribute value context of a %q element", .attr.name, .element.name)
}
if == sanitizationContextStyle && .attr.value != "" {
if := validateDoesNotEndsWithCharRefPrefix(.attr.value); != nil {
return nil, fmt.Errorf("action cannot be interpolated into the %q attribute value of this %q element: %s", .attr.name, .element.name, )
}
= append(, sanitizeHTMLFuncName)
:= .sanitizerName()
if !.isURLorTrustedResourceURL() {
return reverse(appendIfNotEmpty(, )), nil
}
:= .attr.value
return reverse(appendIfNotEmpty(, normalizeURLFuncName, )), nil
if .attr.ambiguousValue {
return nil, fmt.Errorf("actions must not occur after an ambiguous URL prefix in the %q attribute value context of a %q element", .attr.name, .element.name)
}
, := urlPrefixValidators[]
if ! {
return nil, fmt.Errorf("cannot validate attribute value prefix %q in the %q sanitization context", .attr.value, )
}
if := (.attr.value); != nil {
return nil, fmt.Errorf("action cannot be interpolated into the %q URL attribute value of this %q element: %s", .attr.name, .element.name, )
}
switch {
= append(, queryEscapeURLFuncName)
default:
= append(, normalizeURLFuncName)
}
return reverse(), nil
}
func (, , string) (sanitizationContext, error) {
:= strings.Fields()
for , := range {
if urlLinkRelVals[] {
return sanitizationContextTrustedResourceURLOrURL, nil
}
}
}
return sanitizationContextNone, nil
}
if , := elementSpecificAttrValSanitizationContext[][]; {
return , nil
}
, := globalAttrValSanitizationContext[]
, := elementContentSanitizationContext[]
var dataAttributeNamePattern = regexp.MustCompile(`^data-[a-z_][-a-z0-9_]*$`)
var endsWithCharRefPrefixPattern = regexp.MustCompile(
`&(?:[[:alpha:]][[:alnum:]]*|#(?:[xX][[:xdigit:]]*|[[:digit:]]*))?$`)
func ( string) error {
if endsWithCharRefPrefixPattern.MatchString() {
return fmt.Errorf(`prefix %q ends with an incomplete HTML character reference; did you mean "&" instead of "&"?`, )
}
return nil
}
= sanitizationContextHTML
} else {
, = sanitizationContextForElementContent()
}
if != nil {
if len() == 1 {
return "",
}
return "", fmt.Errorf(`conditional branch with element %q results in sanitization error: %s`, , )
}
if == 0 {
, = ,
continue
}
if != {
return "",
fmt.Errorf(`conditional branches end in different element content sanitization contexts: element %q has sanitization context %q, element %q has sanitization context %q`,
, , , )
}
}
return .sanitizerName(), nil
}
func ( string) (sanitizationContext, error) {
, := elementContentSanitizationContext[]
if ! {
return 0, fmt.Errorf("actions must not occur in the element content context of a %q element", )
}
return , nil
}
func ( ...interface{}) string {
return ""
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |