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

View file

@ -7,9 +7,7 @@ import (
"io"
stdfs "io/fs"
"net/http"
"os"
"path"
"runtime"
"strings"
"cachefs/pkg/fs"
@ -17,20 +15,11 @@ import (
"github.com/go-logr/logr"
)
func PrintStack() {
os.Stderr.Write(Stack())
}
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))
}
}
const (
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'"
)
type FileServer struct {
log logr.Logger
@ -69,17 +58,15 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
p := path.Clean(upath)
option := r.FormValue("o")
if option == "debug" {
PrintStack()
}
d, _, err := fs.fs.StatWithOffline(p)
d, err := fs.fs.Stat(p)
if err != nil {
msg, code := toHTTPError(err)
http.Error(w, msg, code)
return
}
//option := r.FormValue("o")
h := w.Header()
if option == "v" {
h.Set(csp, videoCSP)
err = video.Execute(w, r.URL.Path)
if err != nil {
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}
// TODO implement directory listing cache
offline := true
if offline {
r.Header.Del("If-Modified-Since")
r.Header.Del("Cache-Control")
}
fs.h.ServeHTTP(i, r)
if offline {
w.Header().Del("Last-Modified")
}
h.Del("Last-Modified")
paths, err := i.GetPaths(p, fs.fs)
if err == io.EOF {
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)
return
}
h.Set(csp, indexCSP)
w.WriteHeader(i.Status())
err = index.Execute(w, paths)

View file

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

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