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

+35 -5
View File
@@ -19,6 +19,36 @@ import (
"golang.org/x/crypto/bcrypt"
)
// shareResponse is the client-facing representation of a share. It deliberately
// omits the server-side secrets of share.Link — the bcrypt PasswordHash (which
// would be crackable offline) and the bypass Token — exposing only whether the
// share is password-protected via HasPassword.
type shareResponse struct {
Hash string `json:"hash"`
Path string `json:"path"`
UserID uint `json:"userID"`
Expire int64 `json:"expire"`
HasPassword bool `json:"hasPassword"`
}
func toShareResponse(l *share.Link) *shareResponse {
return &shareResponse{
Hash: l.Hash,
Path: l.Path,
UserID: l.UserID,
Expire: l.Expire,
HasPassword: l.PasswordHash != "",
}
}
func toShareResponses(links []*share.Link) []*shareResponse {
res := make([]*shareResponse, 0, len(links))
for _, l := range links {
res = append(res, toShareResponse(l))
}
return res
}
func withPermShare(fn handleFunc) handleFunc {
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
if !d.user.Perm.Share || !d.user.Perm.Download {
@@ -40,7 +70,7 @@ var shareListHandler = withPermShare(func(w http.ResponseWriter, r *http.Request
s, err = d.store.Share.FindByUserID(d.user.ID)
}
if errors.Is(err, fberrors.ErrNotExist) {
return renderJSON(w, r, []*share.Link{})
return renderJSON(w, r, []*shareResponse{})
}
if err != nil {
@@ -54,7 +84,7 @@ var shareListHandler = withPermShare(func(w http.ResponseWriter, r *http.Request
return s[i].Expire < s[j].Expire
})
return renderJSON(w, r, s)
return renderJSON(w, r, toShareResponses(s))
})
var shareGetsHandler = withPermShare(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
@@ -68,14 +98,14 @@ var shareGetsHandler = withPermShare(func(w http.ResponseWriter, r *http.Request
s, err = d.store.Share.Gets(r.URL.Path, d.user.ID)
}
if errors.Is(err, fberrors.ErrNotExist) {
return renderJSON(w, r, []*share.Link{})
return renderJSON(w, r, []*shareResponse{})
}
if err != nil {
return http.StatusInternalServerError, err
}
return renderJSON(w, r, s)
return renderJSON(w, r, toShareResponses(s))
})
func getSharesForAdminPath(d *data, path string) ([]*share.Link, error) {
@@ -204,7 +234,7 @@ var sharePostHandler = withPermShare(func(w http.ResponseWriter, r *http.Request
return http.StatusInternalServerError, err
}
return renderJSON(w, r, s)
return renderJSON(w, r, toShareResponse(s))
})
func getSharePasswordHash(body share.CreateBody) (data []byte, statuscode int, err error) {