github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/safehttp/plugins/csp/strict.go (about) 1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package csp 16 17 import ( 18 "strings" 19 20 "github.com/google/go-safeweb/safehttp" 21 "github.com/google/go-safeweb/safehttp/plugins/csp/internalunsafecsp" 22 ) 23 24 // StrictPolicy can be used to build a strict, nonce-based CSP. 25 // 26 // See https://csp.withgoogle.com/docs/strict-csp.html for more info. 27 type StrictPolicy struct { 28 // NoStrictDynamic controls whether script-src should contain the 'strict-dynamic' 29 // value. 30 // 31 // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#strict-dynamic 32 // for more info. 33 NoStrictDynamic bool 34 // UnsafeEval controls whether script-src should contain the 'unsafe-eval' value. 35 // If enabled, the eval() JavaScript function is allowed. 36 UnsafeEval bool 37 // BaseURI controls the base-uri directive. If BaseURI is an empty string the 38 // directive will be set to 'none'. The base-uri directive restricts the URLs 39 // which can be used in a document's <base> element. 40 // 41 // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/base-uri 42 // for more info. 43 BaseURI string 44 // ReportURI controls the report-uri directive. If ReportUri is empty, no report-uri 45 // directive will be set. 46 ReportURI string 47 // Hashes adds a set of hashes to script-src. An example of a hash would be: 48 // sha256-CihokcEcBW4atb/CW/XWsvWwbTjqwQlE9nj9ii5ww5M= 49 // which is the SHA256 hash for the script "console.log(1)". 50 // 51 // For more info, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src 52 Hashes []string 53 } 54 55 // Serialize serializes this policy for use in a Content-Security-Policy header 56 // or in a Content-Security-Policy-Report-Only header. A nonce will be provided 57 // to Serialize which can be used in 'nonce-{random-nonce}' values in directives. 58 func (s StrictPolicy) Serialize(nonce string, _ safehttp.InterceptorConfig) string { 59 var b strings.Builder 60 61 // object-src 'none'; script-src 'unsafe-inline' 'nonce-{random}' 62 b.WriteString("object-src 'none'; script-src 'unsafe-inline' 'nonce-") 63 b.WriteString(nonce) 64 b.WriteByte('\'') 65 66 if !s.NoStrictDynamic { 67 b.WriteString(" 'strict-dynamic' https: http:") 68 } 69 70 if s.UnsafeEval { 71 b.WriteString(" 'unsafe-eval'") 72 } 73 74 for _, h := range s.Hashes { 75 b.WriteString(" '") 76 b.WriteString(h) 77 b.WriteByte('\'') 78 } 79 80 b.WriteString("; base-uri ") 81 if s.BaseURI == "" { 82 b.WriteString("'none'") 83 } else { 84 b.WriteString(s.BaseURI) 85 } 86 87 if s.ReportURI != "" { 88 b.WriteString("; report-uri ") 89 b.WriteString(s.ReportURI) 90 } 91 92 return b.String() 93 } 94 95 // Match matches strict policies overrides. 96 func (StrictPolicy) Match(cfg safehttp.InterceptorConfig) bool { 97 _, ok := cfg.(internalunsafecsp.DisableStrict) 98 return ok 99 } 100 101 // Overridden checks the override level. 102 func (StrictPolicy) Overridden(cfg safehttp.InterceptorConfig) (disabled, reportOnly bool) { 103 disable := cfg.(internalunsafecsp.DisableStrict) 104 return disable.SkipReports, true 105 }