added CSP

This commit is contained in:
ston1th 2022-03-20 22:50:27 +01:00
commit ce1639f8f8
4 changed files with 28 additions and 37 deletions

View file

@ -109,11 +109,6 @@ func (fs *FS) initMetadata() {
} }
func (fs *FS) Stat(name string) (fi stdfs.FileInfo, err error) { func (fs *FS) Stat(name string) (fi stdfs.FileInfo, err error) {
fi, _, err = fs.StatWithOffline(name)
return
}
func (fs *FS) StatWithOffline(name string) (fi stdfs.FileInfo, offline bool, err error) {
sp, dp := fs.paths(name) sp, dp := fs.paths(name)
fi, err = fs.sc.Get(name) fi, err = fs.sc.Get(name)
if err == nil { if err == nil {
@ -125,7 +120,6 @@ func (fs *FS) StatWithOffline(name string) (fi stdfs.FileInfo, offline bool, err
return return
} }
fi, err = os.Stat(dp) fi, err = os.Stat(dp)
offline = true
return return
} }

View file

@ -7,9 +7,7 @@ import (
"io" "io"
stdfs "io/fs" stdfs "io/fs"
"net/http" "net/http"
"os"
"path" "path"
"runtime"
"strings" "strings"
"cachefs/pkg/fs" "cachefs/pkg/fs"
@ -17,20 +15,11 @@ import (
"github.com/go-logr/logr" "github.com/go-logr/logr"
) )
func PrintStack() { const (
os.Stderr.Write(Stack()) csp = "Content-Security-Policy"
} indexCSP = "default-src 'none';style-src 'unsafe-inline';frame-ancestors 'none'"
videoCSP = indexCSP + ";script-src 'sha256-ZLSc/s5/US9uVMMZOJ/yWiS1tj9nEoi+5Qoohy3QetU=';media-src 'self'"
func Stack() []byte { )
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
return buf[:n]
}
buf = make([]byte, 2*len(buf))
}
}
type FileServer struct { type FileServer struct {
log logr.Logger log logr.Logger
@ -69,17 +58,15 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
p := path.Clean(upath) p := path.Clean(upath)
option := r.FormValue("o") option := r.FormValue("o")
if option == "debug" { d, err := fs.fs.Stat(p)
PrintStack()
}
d, _, err := fs.fs.StatWithOffline(p)
if err != nil { if err != nil {
msg, code := toHTTPError(err) msg, code := toHTTPError(err)
http.Error(w, msg, code) http.Error(w, msg, code)
return return
} }
//option := r.FormValue("o") h := w.Header()
if option == "v" { if option == "v" {
h.Set(csp, videoCSP)
err = video.Execute(w, r.URL.Path) err = video.Execute(w, r.URL.Path)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@ -110,16 +97,12 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
i := &responseInterceptor{w: w} i := &responseInterceptor{w: w}
// TODO implement directory listing cache
offline := true
if offline {
r.Header.Del("If-Modified-Since") r.Header.Del("If-Modified-Since")
r.Header.Del("Cache-Control") r.Header.Del("Cache-Control")
}
fs.h.ServeHTTP(i, r) fs.h.ServeHTTP(i, r)
if offline {
w.Header().Del("Last-Modified") h.Del("Last-Modified")
}
paths, err := i.GetPaths(p, fs.fs) paths, err := i.GetPaths(p, fs.fs)
if err == io.EOF { if err == io.EOF {
w.WriteHeader(i.Status()) w.WriteHeader(i.Status())
@ -129,6 +112,7 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
h.Set(csp, indexCSP)
w.WriteHeader(i.Status()) w.WriteHeader(i.Status())
err = index.Execute(w, paths) err = index.Execute(w, paths)

View file

@ -142,11 +142,19 @@ span {
<html lang="en"> <html lang="en">
<head> <head>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#111111">
<meta name="description" content="cachefs">
<title>cachefs | {{.}}</title> <title>cachefs | {{.}}</title>
<style>
body {
background-color: #111;
margin: 0px;
}
</style>
</head> </head>
<body> <body>
<video id="video" style="width: 100%; height: 100%;" src="{{.}}" controls=""></video> <video id="video" style="width: 100%; height: 100%;" src="{{.}}" controls=""></video>
<script>document.getElementById("video").volume = 0.5;</script> <script>document.getElementById("video").volume=0.5;</script>
</body> </body>
</html>`)) </html>`))
) )

5
script_hash.sh Executable file
View file

@ -0,0 +1,5 @@
#!/bin/bash
echo -n 'sha256-'
echo -n 'document.getElementById("video").volume=0.5;' \
| openssl sha256 -binary \
| openssl base64