fix(share): stop exposing password hash and bypass token in share API (GHSA-833g-cqhp-h72j)

The share management endpoints serialized the storage struct directly, returning
the bcrypt password_hash (crackable offline) and the bypass token for every
share an authenticated caller could list, with admins seeing them for all users.
Return a response DTO that exposes only whether a share is password-protected
(hasPassword) and drops both secrets. The storage struct keeps its tags so the
secrets stay persisted and the server-side auth/public flows are unchanged.
Updates the frontend to use hasPassword and adds a regression test.
This commit is contained in:
Henrique Dias committed 2026-06-27 09:08:56 +02:00
1 parent 883a36f02f
commit ec13054671
4 files changed
+91 -7

No files matched your search

+54
View File
@@ -6,6 +6,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
@@ -92,6 +93,59 @@ func TestAdminShareGetsHandlerMatchesOwnerScope(t *testing.T) {
}
}
// Regression for the share secret exposure (GHSA-833g-cqhp-h72j): the share API
// must not serialize the bcrypt password hash or the bypass token, while still
// persisting them server-side so password-protected shares keep working.
func TestSharePostHandlerDoesNotLeakSecrets(t *testing.T) {
root := t.TempDir()
userScope := filepath.Join(root, "user")
if err := os.MkdirAll(userScope, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(userScope, "file.txt"), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
key := []byte("test-signing-key")
perm := users.Permissions{Share: true, Download: true}
st := scopedUserStorage(t, userScope, perm, key)
signed := signToken(t, perm, key)
body := `{"password":"ShareSecret123!","expires":"24","unit":"hours"}`
req, _ := http.NewRequest(http.MethodPost, "/file.txt", strings.NewReader(body))
req.Header.Set("X-Auth", signed)
rec := httptest.NewRecorder()
handle(sharePostHandler, "", st, &settings.Server{Root: root}).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d body=%q", rec.Code, rec.Body.String())
}
var resp map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if _, ok := resp["password_hash"]; ok {
t.Errorf("VULNERABLE: response leaks password_hash: %s", rec.Body.String())
}
if _, ok := resp["token"]; ok {
t.Errorf("VULNERABLE: response leaks token: %s", rec.Body.String())
}
if resp["hasPassword"] != true {
t.Errorf("expected hasPassword=true, got %v", resp["hasPassword"])
}
// The secrets must still be persisted server-side (storm uses the JSON codec,
// so the storage struct's tags must keep emitting them).
stored, err := st.Share.GetByHash(resp["hash"].(string))
if err != nil {
t.Fatalf("share not stored: %v", err)
}
if stored.PasswordHash == "" || stored.Token == "" {
t.Fatalf("server-side secrets not persisted: hash=%q token=%q", stored.PasswordHash, stored.Token)
}
}
func signShareTestToken(t *testing.T, id uint, username string, perm users.Permissions, key []byte) string {
t.Helper()