77package frontend
88
99import (
10- "crypto/rand"
1110 "errors"
1211 "fmt"
13- "html/template"
1412 "io"
1513 "io/fs"
1614 "net/http"
@@ -19,8 +17,6 @@ import (
1917 "path/filepath"
2018 "strings"
2119 "time"
22-
23- "github.com/jxskiss/base62"
2420)
2521
2622const index = "/index.html"
@@ -111,27 +107,13 @@ func (handler *StaticHandler) serveFile(w http.ResponseWriter, r *http.Request,
111107 if path != index {
112108 w .Header ().Set ("Vary" , "Accept-Encoding, User-Agent" )
113109 w .Header ().Set ("Cache-Control" , fmt .Sprintf ("public, max-age=%d, immutable" , handler .maxAgeSec ))
114- http .ServeContent (w , r , file .Name (), handler .modTime , file )
115110 } else {
116111 w .Header ().Set ("Referrer-Policy" , "strict-origin-when-cross-origin" )
117112
118- b := make ([]byte , 10 )
119- if _ , err := rand .Read (b ); err != nil {
120- writeHTTPError (w , fmt .Errorf ("failed to read random bytes: %w" , err ))
121-
122- return
123- }
124-
125- nonce := base62 .EncodeToString (b )
126-
127- w .Header ().Set ("Content-Security-Policy" ,
128- "upgrade-insecure-requests" +
129- fmt .Sprintf (";default-src 'self' 'nonce-%s'" , nonce )+
130- ";img-src * data:" +
131- ";connect-src 'self' https://*.auth0.com https://*.userpilot.io wss://*.userpilot.io" +
132- ";font-src 'self' data:" +
133- fmt .Sprintf (";style-src 'self' 'nonce-%s' data: https://fonts.googleapis.com https://fonts.gstatic.com" , nonce )+
134- ";frame-src https://*.auth0.com" ,
113+ w .Header ().Set ("Content-Security-Policy" , "default-src 'self' https://*.userpilot.io; img-src * data: ; " +
114+ ";connect-src 'self' https://*.auth0.com https://*.userpilot.io wss://*.userpilot.io ;font-src 'self' data: " +
115+ ";style-src 'self' 'unsafe-inline' https://fonts.googleapis.com data: ;upgrade-insecure-requests;" +
116+ ";frame-src https://*.auth0.com" ,
135117 )
136118
137119 w .Header ().Set ("X-Frame-Options" , "SAMEORIGIN" )
@@ -142,31 +124,10 @@ func (handler *StaticHandler) serveFile(w http.ResponseWriter, r *http.Request,
142124 "magnetometer=(), microphone=(), midi=(), payment=(), picture-in-picture=(), publickey-credentials=(self)," +
143125 "screen-wake-lock=(), sync-xhr=(self), usb=(), web-share=(), xr-spatial-tracking=()" ,
144126 )
145-
146- // Read index.html content
147- content , err := io .ReadAll (file )
148- if err != nil {
149- writeHTTPError (w , err )
150-
151- return
152- }
153-
154- tmpl , err := template .New ("index.html" ).Parse (string (content ))
155- if err != nil {
156- writeHTTPError (w , err )
157-
158- return
159- }
160-
161- // Inject nonce into index.html
162- err = tmpl .Execute (w , struct { Nonce string }{Nonce : nonce })
163- if err != nil {
164- writeHTTPError (w , err )
165-
166- return
167- }
168127 }
169128
129+ http .ServeContent (w , r , file .Name (), handler .modTime , file )
130+
170131 return
171132 }
172133
0 commit comments