Files
filebrowser/http/public_landing.go
T

53 lines
2.0 KiB
Go

package fbhttp
import (
"net/http"
"strings"
)
// URL.Path has already been decoded once by net/http. Refuse alternate
// separators and residual escapes rather than interpreting a second time.
func validSharePath(p string) bool {
if strings.ContainsAny(p, "\\%\x00:") {
return false
}
depth := 0
for i, part := range strings.Split(p, "/") {
if i == 0 {
continue
}
if part == ".." {
depth--
if depth < 0 {
return false
}
} else if part != "" && part != "." {
depth++
}
}
return true
}
// This empty, credential-free shell is the only place user scripts execute.
// A response CSP (not an editable meta tag) survives document.write. The opaque
// sandbox origin cannot read cookies, storage, or the File Browser parent.
func publicLandingShell(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Referrer-Policy", "no-referrer")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Content-Security-Policy", "sandbox allow-scripts; default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src blob: data:; media-src blob: data:; font-src data:; connect-src 'none'; frame-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'self'")
_, _ = w.Write([]byte(`<!doctype html><meta charset="utf-8"><script>
addEventListener('message', function receive(e) {
if(e.source !== parent || !e.data || typeof e.data !== 'object' || e.data.type !== 'landing-content' || typeof e.data.html !== 'string') return;
removeEventListener('message', receive);
document.open(); document.write(e.data.html); document.close();
document.addEventListener('click', function(e) {
var a=e.target && e.target.closest ? e.target.closest('a[data-landing-link]') : null;
if(a){e.preventDefault();parent.postMessage({type:'landing-link',id:a.dataset.landingLink}, '*');}
}, true);
});
parent.postMessage({type:'landing-ready'}, '*');
</script>`))
}