Documentation ¶
Index ¶
- Variables
- func FetchDirectivesIncludes(directive Directive) bool
- func Includes(elements []string, key string) bool
- func IsDirective(directive string) bool
- func IsHash(hash string, strictCheck bool) bool
- func IsKeyword(keyword string) bool
- func IsNonce(nonce string, strictCheck bool) bool
- func IsUrlScheme(urlScheme string) bool
- func LooksLikeIPAddress(maybeIP string) bool
- func NormalizeDirectiveValue(directiveValue string) string
- func Remove(elements []string, key string) []string
- type CSP
- func (c *CSP) EffectiveCSP(version CSPVersion, optFindings ...Finding) CSP
- func (c *CSP) EffectiveDirective(inDirective Directive) Directive
- func (c *CSP) EffectiveDirectives(inDirectives []Directive) []Directive
- func (c *CSP) GetDirective(directive Directive) []string
- func (c *CSP) PolicyHasScriptHashes() bool
- func (c *CSP) PolicyHasScriptNonces() bool
- func (c *CSP) PolicyHasStrictDynamic() bool
- func (c *CSP) String() string
- type CSPEvaluator
- type CSPParser
- type CSPVersion
- type CheckerFn
- type Directive
- type Finding
- func CheckAllowlistFallback(parsedCSP CSP) []Finding
- func CheckDeprecatedDirective(parsedCSP CSP) []Finding
- func CheckFlashObjectAllowlistBypass(parsedCSP CSP) []Finding
- func CheckHasConfiguredReporting(parsedCSP CSP) []Finding
- func CheckIPSource(parsedCSP CSP) []Finding
- func CheckInvalidKeyword(parsedCSP CSP) []Finding
- func CheckMissingBaseUriDirective(parsedCSP CSP) []Finding
- func CheckMissingDirectives(parsedCSP CSP) []Finding
- func CheckMissingObjectSrcDirective(parsedCSP CSP) []Finding
- func CheckMissingScriptSrcDirective(parsedCSP CSP) []Finding
- func CheckMissingSemicolon(parsedCSP CSP) []Finding
- func CheckMultipleMissingBaseUriDirective(parsedCSPs []CSP) []Finding
- func CheckNonceLength(parsedCSP CSP) []Finding
- func CheckPlainUrlSchemes(parsedCSP CSP) []Finding
- func CheckRequiresTrustedTypesForScripts(parsedCSP CSP) []Finding
- func CheckScriptAllowlistBypass(parsedCSP CSP) []Finding
- func CheckScriptUnsafeEval(parsedCSP CSP) []Finding
- func CheckScriptUnsafeInline(effectiveCsp CSP) []Finding
- func CheckSrcHttp(parsedCSP CSP) []Finding
- func CheckStrictDynamic(parsedCSP CSP) []Finding
- func CheckStrictDynamicNotStandalone(parsedCSP CSP) []Finding
- func CheckUnknownDirective(parsedCSP CSP) []Finding
- func CheckUnsafeInlineFallback(parsedCSP CSP) []Finding
- func CheckWildcards(parsedCSP CSP) []Finding
- type FindingType
- type Keyword
- type Severity
- type TrustedTypesSink
Constants ¶
This section is empty.
Variables ¶
var DEFAULT_CHECKS = []CheckerFn{ CheckScriptUnsafeInline, CheckScriptUnsafeEval, CheckPlainUrlSchemes, CheckWildcards, CheckMissingDirectives, CheckScriptAllowlistBypass, CheckFlashObjectAllowlistBypass, CheckIPSource, CheckNonceLength, CheckSrcHttp, CheckDeprecatedDirective, CheckUnknownDirective, CheckMissingSemicolon, CheckInvalidKeyword, }
*
- Set of default checks to run.
var DIRECTIVES_CAUSING_XSS = []Directive{SCRIPT_SRC, OBJECT_SRC, BASE_URI}
var FetchDirectives = []Directive{ CHILD_SRC, CONNECT_SRC, DEFAULT_SRC, FONT_SRC, FRAME_SRC, IMG_SRC, MANIFEST_SRC, MEDIA_SRC, OBJECT_SRC, SCRIPT_SRC, SCRIPT_SRC_ATTR, SCRIPT_SRC_ELEM, STYLE_SRC, STYLE_SRC_ATTR, STYLE_SRC_ELEM, WORKER_SRC, }
var HASH_PATTERN = regexp.MustCompile(`^\'(sha256|sha384|sha512)-(.+)\'$`)
* A regex pattern to check hash prefix.
var IPV6_PATTERN = regexp.MustCompile(`^\[[\d:]+\]`)
var NONCE_PATTERN = regexp.MustCompile(`^\'nonce-(.+)\'$`)
* A regex pattern for checking if nonce prefix.
var REMOVE_PROTO_AGNOSTIC = regexp.MustCompile(`^\/\/`)
var REMOVE_URL_PATTERN = regexp.MustCompile(`^\w[+\w.-]*:\/\/`)
var STRICTCSP_CHECKS = []CheckerFn{ CheckScriptUnsafeInline, CheckScriptUnsafeEval, CheckPlainUrlSchemes, CheckWildcards, CheckMissingDirectives, CheckScriptAllowlistBypass, CheckFlashObjectAllowlistBypass, CheckIPSource, CheckNonceLength, CheckSrcHttp, CheckDeprecatedDirective, CheckUnknownDirective, CheckMissingSemicolon, CheckInvalidKeyword, }
*
- Strict CSP and backward compatibility checks.
var STRICT_HASH_PATTERN = regexp.MustCompile(`^\'(sha256|sha384|sha512)-[a-zA-Z0-9+/]+[=]{0,2}\'$`)
*
- A regex pattern to check hash prefix and Base64 formatting of a hash value.
var STRICT_NONCE_PATTERN = regexp.MustCompile(`^\'nonce-[a-zA-Z0-9+/_-]+[=]{0,2}\'$`)
*
- A regex pattern to check nonce prefix and Base64 formatting of a nonce value.
var URL_SCHEMES_CAUSING_XSS = []string{"data:", "http:", "https:"}
var URL_SCHEME_PATTERN = regexp.MustCompile(`^[a-zA-Z][+a-zA-Z0-9.-]*:$`)
var VALID_SHA_PATTERN = regexp.MustCompile(`^(sha256|sha384|sha512)-`)
var WILDCARD_PLACEHOLDER = regexp.MustCompile("^wildcard_placeholder")
Functions ¶
func FetchDirectivesIncludes ¶
func IsDirective ¶
func IsUrlScheme ¶
*
- Checks if a string is a valid URL scheme.
- Scheme part + ":"
- For scheme part see https://tools.ietf.org/html/rfc3986#section-3.1
- @param urlScheme value to check.
- @return True if urlScheme has a valid scheme.
func LooksLikeIPAddress ¶
*
- Returns whether the given string "looks" like an IP address. This function
- only uses basic heuristics and does not accept all valid IPs nor reject all
- invalid IPs.
func NormalizeDirectiveValue ¶
Types ¶
type CSP ¶
type CSP struct {
// contains filtered or unexported fields
}
func (*CSP) EffectiveCSP ¶
func (c *CSP) EffectiveCSP(version CSPVersion, optFindings ...Finding) CSP
EffectiveCSP returns CSP as it would be seen by a UA supporting a specific CSP version.
func (*CSP) EffectiveDirective ¶
EffectiveDirective returns default-src if directive is a fetch directive and is not present in this CSP. Otherwise the provided directive is returned.
func (*CSP) EffectiveDirectives ¶
EffectiveDirectives returns the passed directives if present in this CSP or default-src otherwise.
func (*CSP) GetDirective ¶
func (*CSP) PolicyHasScriptHashes ¶
func (*CSP) PolicyHasScriptNonces ¶
func (*CSP) PolicyHasStrictDynamic ¶
type CSPEvaluator ¶
type CSPEvaluator struct {
// contains filtered or unexported fields
}
func NewCSPEvaluator ¶
func NewCSPEvaluator(parsedCSP CSP, cspVersion CSPVersion) CSPEvaluator
func (*CSPEvaluator) Evaluate ¶
func (e *CSPEvaluator) Evaluate(parsedCSPChecks, effectiveCSPChecks []CheckerFn) []Finding
type CSPParser ¶
type CSPParser struct {
// contains filtered or unexported fields
}
func NewCSPParser ¶
type Directive ¶
type Directive string
const ( // Fetch directives CHILD_SRC Directive = "child-src" CONNECT_SRC Directive = "connect-src" DEFAULT_SRC Directive = "default-src" FONT_SRC Directive = "font-src" FRAME_SRC Directive = "frame-src" IMG_SRC Directive = "img-src" MEDIA_SRC Directive = "media-src" OBJECT_SRC Directive = "object-src" SCRIPT_SRC Directive = "script-src" SCRIPT_SRC_ATTR Directive = "script-src-attr" SCRIPT_SRC_ELEM Directive = "script-src-elem" STYLE_SRC Directive = "style-src" STYLE_SRC_ATTR Directive = "style-src-attr" STYLE_SRC_ELEM Directive = "style-src-elem" PREFETCH_SRC Directive = "prefetch-src" MANIFEST_SRC Directive = "manifest-src" WORKER_SRC Directive = "worker-src" // Document directives BASE_URI Directive = "base-uri" PLUGIN_TYPES Directive = "plugin-types" SANDBOX Directive = "sandbox" DISOWN_OPENER Directive = "disown-opener" // Navigation directives FORM_ACTION Directive = "form-action" FRAME_ANCESTORS Directive = "frame-ancestors" NAVIGATE_TO Directive = "navigate-to" // Reporting directives REPORT_TO Directive = "report-to" REPORT_URI Directive = "report-uri" // Other directives BLOCK_ALL_MIXED_CONTENT Directive = "block-all-mixed-content" UPGRADE_INSECURE_REQUESTS Directive = "upgrade-insecure-requests" REFLECTED_XSS Directive = "reflected-xss" REFERRER Directive = "referrer" REQUIRE_SRI_FOR Directive = "require-sri-for" TRUSTED_TYPES Directive = "trusted-types" // https://github.com/WICG/trusted-types REQUIRE_TRUSTED_TYPES_FOR Directive = "require-trusted-types-for" WEBRTC Directive = "webrtc" )
type Finding ¶
type Finding struct { FindingType FindingType Directive Directive Description string Severity Severity Value string }
A CSP Finding is returned by a CSP check and can either reference a directive value or a directive. If a directive value is referenced opt_index must be provided.
func CheckAllowlistFallback ¶
*
- Checks if the policy has an allowlist fallback (* or http: and https:) when
- 'strict-dynamic' is present.
- This will ensure backward compatibility to browser that don't support
- 'strict-dynamic'. *
- Example policy where this check would trigger:
- script-src 'nonce-test' 'strict-dynamic' *
- @param parsedCsp A parsed csp.
func CheckDeprecatedDirective ¶
*
- Checks if csp contains directives that are deprecated in CSP3.
- Findings of this check are informal only and are FP free. *
- Example policy where this check would trigger:
- report-uri foo.bar/csp *
- @param parsedCsp Parsed CSP.
func CheckFlashObjectAllowlistBypass ¶
*
- Checks if allowlisted object-src origins are bypassable.
- Findings of this check have a high severity and are FP free. *
- Example policy where this check would trigger:
- default-src 'none'; object-src ajax.googleapis.com *
- @param parsedCsp Parsed CSP.
func CheckHasConfiguredReporting ¶
*
- Checks if the policy has configured reporting in a robust manner.
func CheckIPSource ¶
*
- Checks if csp contains IP addresses.
- Findings of this check are informal only and are FP free. *
- Example policy where this check would trigger:
- script-src 127.0.0.1 *
- @param parsedCsp Parsed CSP.
func CheckInvalidKeyword ¶
*
- Checks if csp contains invalid keywords. *
- Example policy where this check would trigger:
- script-src 'notAkeyword' *
- @param parsedCsp A parsed csp.
func CheckMissingBaseUriDirective ¶
* * Checks if the base-uri needs to be restricted and if so, whether it has been * restricted.
func CheckMissingDirectives ¶
*
- Checks if all necessary directives for preventing XSS are set.
- Findings of this check have a high severity and are FP free. *
- Example policy where this check would trigger:
- script-src 'none' *
- @param parsedCsp Parsed CSP.
func CheckMissingObjectSrcDirective ¶
*
- Checks if object-src is restricted to none either directly or via a
- default-src.
func CheckMissingScriptSrcDirective ¶
*
- Checks if script-src is restricted either directly or via a default-src.
func CheckMissingSemicolon ¶
*
- Checks if semicolons are missing in the csp. *
- Example policy where this check would trigger (missing semicolon before
- start of object-src):
- script-src foo.bar object-src 'none' *
- @param parsedCsp A parsed csp.
func CheckMultipleMissingBaseUriDirective ¶
* * Checks if the base-uri needs to be restricted and if so, whether it has been * restricted.
func CheckNonceLength ¶
*
- Checks if csp nonce is at least 8 characters long.
- Findings of this check are of medium severity and are FP free. *
- Example policy where this check would trigger:
- script-src 'nonce-short' *
- @param parsedCsp Parsed CSP.
func CheckPlainUrlSchemes ¶
*
- Checks if plain URL schemes (e.g. http:) are allowed in sensitive directives.
- Findings of this check have a high severity and are FP free. *
- Example policy where this check would trigger:
- script-src https: http: data: *
- @param parsedCsp Parsed CSP.
func CheckRequiresTrustedTypesForScripts ¶
*
- Checks if the policy requires Trusted Types for scripts. *
- I.e. the policy should have the following dirctive:
- require-trusted-types-for 'script' *
- @param parsedCsp A parsed csp.
func CheckScriptAllowlistBypass ¶
*
- Checks if allowlisted origins are bypassable by JSONP/Angular endpoints.
- High severity findings of this check are FP free. *
- Example policy where this check would trigger:
- default-src 'none'; script-src www.google.com *
- @param parsedCsp Parsed CSP.
func CheckScriptUnsafeEval ¶
*
- Checks if passed csp allows eval in scripts.
- Findings of this check have a medium severity and are FP free. *
- Example policy where this check would trigger:
- script-src 'unsafe-eval' *
- @param parsedCsp Parsed CSP.
func CheckScriptUnsafeInline ¶
*
- Checks if passed csp allows inline scripts.
- Findings of this check are critical and FP free.
- unsafe-inline is ignored in the presence of a nonce or a hash. This check
- does not account for this and therefore the effectiveCsp needs to be passed. *
- Example policy where this check would trigger:
- script-src 'unsafe-inline' *
- @param effectiveCsp A parsed csp that only contains values which
- are active in a certain version of CSP (e.g. no unsafe-inline if a nonce
- is present).
func CheckSrcHttp ¶
*
- Checks if CSP allows sourcing from http://
- Findings of this check are of medium severity and are FP free. *
- Example policy where this check would trigger:
- report-uri http://foo.bar/csp *
- @param parsedCsp Parsed CSP.
func CheckStrictDynamic ¶
func CheckStrictDynamicNotStandalone ¶
*
- Checks if 'strict-dynamic' is only used together with a nonce or a hash. *
- Example policy where this check would trigger:
- script-src 'strict-dynamic' *
- @param parsedCsp A parsed csp.
func CheckUnknownDirective ¶
*
- Checks if the csp contains invalid directives. *
- Example policy where this check would trigger:
- foobar-src foo.bar *
- @param parsedCsp A parsed csp.
func CheckUnsafeInlineFallback ¶
*
- Checks if the policy has 'unsafe-inline' when a nonce or hash are present.
- This will ensure backward compatibility to browser that don't support
- CSP nonces or hasehs. *
- Example policy where this check would trigger:
- script-src 'nonce-test' *
- @param parsedCsp A parsed csp.
func CheckWildcards ¶
*
- Checks if csp contains wildcards in sensitive directives.
- Findings of this check have a high severity and are FP free. *
- Example policy where this check would trigger:
- script-src * *
- @param parsedCsp Parsed CSP.
func (Finding) HighestSeverity ¶
HighestSeverity returns the highest severity of a list of findings
type FindingType ¶
type FindingType int
const ( // Parser checks MISSING_SEMICOLON FindingType = 100 UNKNOWN_DIRECTIVE FindingType = 101 INVALID_KEYWORD FindingType = 102 NONCE_CHARSET FindingType = 106 // Security cheks MISSING_DIRECTIVES FindingType = 300 SCRIPT_UNSAFE_INLINE FindingType = 301 SCRIPT_UNSAFE_EVAL FindingType = 302 PLAIN_URL_SCHEMES FindingType = 303 PLAIN_WILDCARD FindingType = 304 SCRIPT_ALLOWLIST_BYPASS FindingType = 305 OBJECT_ALLOWLIST_BYPASS FindingType = 306 NONCE_LENGTH FindingType = 307 IP_SOURCE FindingType = 308 DEPRECATED_DIRECTIVE FindingType = 309 SRC_HTTP FindingType = 310 // Strict dynamic and backward compatibility checks STRICT_DYNAMIC FindingType = 400 STRICT_DYNAMIC_NOT_STANDALONE FindingType = 401 NONCE_HASH FindingType = 402 UNSAFE_INLINE_FALLBACK FindingType = 403 ALLOWLIST_FALLBACK FindingType = 404 IGNORED FindingType = 405 // Trusted Types checks REQUIRE_TRUSTED_TYPES_FOR_SCRIPTS FindingType = 500 // Lighthouse checks REPORTING_DESTINATION_MISSING FindingType = 600 REPORT_TO_ONLY FindingType = 601 )
type Keyword ¶
type Keyword string
const ( KW_SELF Keyword = "'self'" KW_NONE Keyword = "'none'" KW_UNSAFE_INLINE Keyword = "'unsafe-inline'" KW_UNSAFE_EVAL Keyword = "'unsafe-eval'" KW_WASM_EVAL Keyword = "'wasm-eval'" KW_WASM_UNSAFE_EVAL Keyword = "'wasm-unsafe-eval'" KW_STRICT_DYNAMIC Keyword = "'strict-dynamic'" KW_UNSAFE_HASHED_ATTRIBUTES Keyword = "'unsafe-hashed-attributes'" KW_UNSAFE_HASHES Keyword = "'unsafe-hashes'" KW_REPORT_SAMPLE Keyword = "'report-sample'" KW_BLOCK Keyword = "'block'" KW_ALLOW Keyword = "'allow'" )