Partage en lecture seule par lien signé
- Lien /s/<jeton> : jeton = id aléatoire 128 bits + HMAC-SHA256 tronqué par la
clé du serveur (SHARE_SECRET, sinon DATA_DIR/.share-secret générée en 0600).
Valide si la signature est correcte, le lien présent dans .shares.json et non
expiré (1, 7, 30 jours ou sans expiration).
- API : GET/POST/DELETE /api/shares, POST /api/shares/revoke-all (renouvelle la
clé), GET /api/public/{jeton} hors authentification (nom, titre, logo, scène).
- Auth Basic : /s/, /api/public/, /healthz et les fichiers statiques du build
sont publics ; le reste de l'API et l'index restent protégés. Le serveur
statique ne liste plus les répertoires. X-Robots-Tag: noindex sur les liens.
- Les liens suivent renommages et déplacements, disparaissent à la suppression.
- Interface : dialogue Partager (barre du haut, menu de l'explorateur), section
Liens de partage dans Paramètres, vue publique Excalidraw en lecture seule
avec téléchargement et bascule clair/sombre.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H6J7vKwXx7uopxKhWfFJpm
This commit is contained in:
1 parent
c1ea1e84a7
commit
50d40cd929
18 files changed
+1076
-12
No files matched your search
+32
-4
@@ -13,6 +13,7 @@ import (
|
||||
"os/signal"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -57,6 +58,15 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Clé de signature des liens de partage : SHARE_SECRET, sinon DATA_DIR/.share-secret.
|
||||
if secret := os.Getenv("SHARE_SECRET"); secret != "" {
|
||||
if len(secret) < 16 {
|
||||
slog.Error("SHARE_SECRET trop court (16 caractères minimum)")
|
||||
os.Exit(1)
|
||||
}
|
||||
store.SetShareSecret(secret)
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
@@ -85,7 +95,7 @@ func main() {
|
||||
|
||||
var h http.Handler = mux
|
||||
if user, pass := os.Getenv("AUTH_USER"), os.Getenv("AUTH_PASSWORD"); user != "" && pass != "" {
|
||||
h = basicAuth(h, user, pass)
|
||||
h = basicAuth(h, user, pass, public)
|
||||
slog.Info("authentification Basic activée", "user", user)
|
||||
}
|
||||
h = logRequests(securityHeaders(h))
|
||||
@@ -155,7 +165,7 @@ func spa() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
p := path.Clean(r.URL.Path)
|
||||
if p != "/" {
|
||||
if _, err := fs.Stat(dist, p[1:]); err == nil {
|
||||
if fi, err := fs.Stat(dist, p[1:]); err == nil && !fi.IsDir() {
|
||||
// Les assets Vite sont nommés par empreinte : cache long.
|
||||
if path.Dir(p) == "/assets" {
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
||||
@@ -175,9 +185,24 @@ func spa() http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func basicAuth(next http.Handler, user, pass string) http.Handler {
|
||||
// public indique les chemins servis sans authentification : sonde, schémas
|
||||
// partagés (/s/<jeton> et son API) et fichiers statiques du build (code de
|
||||
// l'application et polices, sans aucune donnée).
|
||||
func public(p string) bool {
|
||||
switch {
|
||||
case p == "/healthz", strings.HasPrefix(p, "/s/"), strings.HasPrefix(p, "/api/public/"):
|
||||
return true
|
||||
case strings.HasPrefix(p, "/api/"), p == "/", p == "/index.html":
|
||||
return false
|
||||
}
|
||||
dist, _ := fs.Sub(web.Dist, "dist")
|
||||
fi, err := fs.Stat(dist, strings.TrimPrefix(path.Clean(p), "/"))
|
||||
return err == nil && !fi.IsDir()
|
||||
}
|
||||
|
||||
func basicAuth(next http.Handler, user, pass string, public func(string) bool) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/healthz" {
|
||||
if public(r.URL.Path) {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
@@ -198,6 +223,9 @@ func securityHeaders(next http.Handler) http.Handler {
|
||||
h.Set("X-Content-Type-Options", "nosniff")
|
||||
h.Set("Referrer-Policy", "same-origin")
|
||||
h.Set("X-Frame-Options", "SAMEORIGIN")
|
||||
if strings.HasPrefix(r.URL.Path, "/s/") {
|
||||
h.Set("X-Robots-Tag", "noindex, nofollow") // liens de partage
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBasicAuthPublicPaths(t *testing.T) {
|
||||
ok := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {})
|
||||
h := basicAuth(ok, "u", "p", public)
|
||||
cases := map[string]int{
|
||||
"/healthz": 200,
|
||||
"/s/abc.def": 200,
|
||||
"/api/public/abc.def": 200,
|
||||
"/": 401,
|
||||
"/index.html": 401,
|
||||
"/api/list": 401,
|
||||
"/api/file?path=x": 401,
|
||||
"/api/publicx": 401,
|
||||
"/assets/": 401, // dossier : pas un fichier statique
|
||||
"/inconnu": 401,
|
||||
}
|
||||
for p, want := range cases {
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest("GET", p, nil))
|
||||
if rec.Code != want {
|
||||
t.Errorf("%s : %d, attendu %d", p, rec.Code, want)
|
||||
}
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/api/list", nil)
|
||||
req.SetBasicAuth("u", "p")
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Errorf("authentifié : %d", rec.Code)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user