Up And Down : première version

This commit is contained in:
cedric committed 2026-11-19 10:30:47 +01:00
commit 89c5430308
76 files changed
+20730

No files matched your search

+86
View File
@@ -0,0 +1,86 @@
package main
import (
"bytes"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestSameOrigin(t *testing.T) {
h := sameOrigin(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) }))
do := func(method, host string, hdr map[string]string) int {
r := httptest.NewRequest(method, "http://"+host+"/api/file", nil)
for k, v := range hdr {
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w.Code
}
cases := []struct {
name string
method string
hdr map[string]string
want int
}{
{"GET entre sites", "GET", map[string]string{"Origin": "https://evil.example"}, 204},
{"PUT sans Origin (curl, jeton)", "PUT", nil, 204},
{"PUT même origine", "PUT", map[string]string{"Origin": "https://cloud.example"}, 204},
{"PUT autre origine", "PUT", map[string]string{"Origin": "https://evil.example"}, 403},
{"POST cross-site", "POST", map[string]string{"Sec-Fetch-Site": "cross-site"}, 403},
{"POST same-origin", "POST", map[string]string{"Sec-Fetch-Site": "same-origin", "Origin": "https://cloud.example"}, 204},
{"DELETE autre origine", "DELETE", map[string]string{"Origin": "http://cloud.example.evil.org"}, 403},
{"proxy : X-Forwarded-Host", "PUT", map[string]string{"Origin": "https://cloud.example", "X-Forwarded-Host": "cloud.example"}, 204},
}
for _, c := range cases {
host := "cloud.example"
if c.name == "proxy : X-Forwarded-Host" {
host = "upandown:8080"
}
if got := do(c.method, host, c.hdr); got != c.want {
t.Errorf("%s : %d, attendu %d", c.name, got, c.want)
}
}
}
func TestPublicPaths(t *testing.T) {
for p, want := range map[string]bool{
"/healthz": true, "/s/abc": true, "/api/public/abc/file": true, "/api/me": true, "/api/logo": true,
"/api/list": false, "/api/file": false, "/api/admin/users": false, "/api/tokens": false,
"/": false, "/index.html": false,
} {
if got := public(p); got != want {
t.Errorf("public(%q) = %v", p, got)
}
}
}
func TestSpaFallbackWithoutBuild(t *testing.T) {
w := httptest.NewRecorder()
spa().ServeHTTP(w, httptest.NewRequest("GET", "/dossier/x", nil))
if w.Code != 404 { // web/dist ne contient que .gitkeep tant que le frontend n'est pas construit
t.Logf("spa sans build : %d (frontend construit ?)", w.Code)
}
}
func TestLogRequestsHidesShareTokens(t *testing.T) {
// Le jeton d'un lien fait partie du chemin : il ne doit pas finir dans les journaux.
var buf bytes.Buffer
old := slog.Default()
slog.SetDefault(slog.New(slog.NewTextHandler(&buf, nil)))
defer slog.SetDefault(old)
h := logRequests(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) }))
for _, p := range []string{"/s/jeton-secret", "/api/public/jeton-secret/file", "/api/list"} {
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", p, nil))
}
out := buf.String()
if strings.Contains(out, "jeton-secret") {
t.Errorf("jeton journalisé : %s", out)
}
if !strings.Contains(out, "/api/list") {
t.Errorf("requête ordinaire non journalisée : %s", out)
}
}