github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/safehttp/plugins/reportingapi/reporting.go (about) 1 // Copyright 2020 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 reportingapi is an implementation of the Report-To header described 16 // in https://www.w3.org/TR/reporting/#header. 17 // 18 // It allows for setting reporting groups to use in conjuction with COOP and CSP. 19 package reportingapi 20 21 import ( 22 "encoding/json" 23 "fmt" 24 25 "github.com/google/go-safeweb/safehttp" 26 ) 27 28 var _ safehttp.Interceptor = Interceptor{} 29 30 // DefaultMaxAge is used as default cache duration for report groups and will make them last 7 days. 31 const DefaultMaxAge = 7 * 24 * 60 * 60 32 33 // ReportToHeaderKey is the HTTP header key for the Reporting API. 34 const ReportToHeaderKey = "Report-To" 35 36 // Endpoint is the Go representation of the endpoints values as specified 37 // in https://www.w3.org/TR/reporting/#endpoints-member 38 type Endpoint struct { 39 // URL defines the location of the endpoint. 40 // The URL that the member's value represents MUST be potentially trustworthy. Non-secure endpoints will be ignored. 41 URL string `json:"url"` 42 // Priority defines which failover class the endpoint belongs to. 43 Priority uint `json:"priority,omitempty"` 44 // Weight defines load balancing for the failover class that the endpoint belongs to. 45 Weight uint `json:"weight,omitempty"` 46 } 47 48 // Group is the Go representation of the Report-To header values as specified 49 // in https://www.w3.org/TR/reporting/#header. 50 type Group struct { 51 // Name associates a name with the set of endpoints. 52 // This is serialized as the "group" field in the Report-To header. 53 // If left empty, the endpoint group will be given the name "default". 54 Name string `json:"group,omitempty"` 55 // IncludeSubdomains enables this endpoint group for all subdomains of the current origin’s host. 56 IncludeSubdomains bool `json:"include_subdomains,omitempty"` 57 // MaxAge defines the endpoint group’s lifetime, as a non-negative integer number of seconds. 58 // A value of 0 will cause the endpoint group to be removed from the user agent’s reporting cache. 59 MaxAge uint `json:"max_age"` 60 // Endpoints is the list of endpoints that belong to this endpoint group. 61 Endpoints []Endpoint `json:"endpoints"` 62 } 63 64 // NewGroup creates a new Group with MaxAge set to DefaultMaxage and all 65 // optional values set to their default values. 66 func NewGroup(name string, url string, otherUrls ...string) Group { 67 var es []Endpoint 68 for _, u := range append([]string{url}, otherUrls...) { 69 es = append(es, Endpoint{URL: u}) 70 } 71 return Group{ 72 Name: name, 73 MaxAge: DefaultMaxAge, 74 Endpoints: es, 75 } 76 } 77 78 // Interceptor is the interceptor for the Report-To header. 79 type Interceptor struct { 80 values []string 81 } 82 83 // NewInterceptor instantiates a new Interceptor for the given groups. 84 func NewInterceptor(groups ...Group) Interceptor { 85 var i Interceptor 86 for _, r := range groups { 87 buf, err := json.Marshal(r) 88 if err != nil { 89 // This path should not be possible. 90 panic(fmt.Sprintf("marshalling report: %#v, %v", r, err)) 91 } 92 i.values = append(i.values, string(buf)) 93 } 94 return i 95 } 96 97 // Before adds all the configured Report-To header values as separate headers. 98 func (i Interceptor) Before(w safehttp.ResponseWriter, r *safehttp.IncomingRequest, cfg safehttp.InterceptorConfig) safehttp.Result { 99 for _, v := range i.values { 100 w.Header().Add(ReportToHeaderKey, v) 101 } 102 return safehttp.NotWritten() 103 } 104 105 // Commit is a no-op, required to satisfy the safehttp.Interceptor interface. 106 func (Interceptor) Commit(w safehttp.ResponseHeadersWriter, r *safehttp.IncomingRequest, resp safehttp.Response, _ safehttp.InterceptorConfig) { 107 } 108 109 // Match returns false since there are no supported configurations. 110 func (Interceptor) Match(safehttp.InterceptorConfig) bool { 111 return false 112 }