Fix critical and high audit findings
Fixed findings from the audit reports (SECURITY, QUALITY, PERFORMANCE): - P-001 · Dashboard re-downloads the entire dataset every 5 seconds Re-synced the vendored shared/ snapshot from shared commit 199a5ef. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7dd0d34383
commit
a84a36349b
31 files changed
+653
-6055
No files matched your search
@@ -85,16 +85,16 @@ func (c *Core) Mount(r chi.Router) {
|
||||
r.With(c.auth.Require).Post("/security/totp/verify", c.auth.TOTPVerify)
|
||||
r.With(c.auth.Require).Post("/security/totp/disable", c.auth.TOTPDisable)
|
||||
r.With(c.auth.Optional).Get("/sync", c.getSyncState)
|
||||
r.With(c.auth.Optional).Put("/sync", c.putSyncState)
|
||||
r.With(c.auth.Require).Put("/sync", c.putSyncState)
|
||||
r.With(c.auth.Optional).Get("/media/state", c.mediaState)
|
||||
r.With(c.auth.Require).Post("/media/like", c.mediaLike)
|
||||
r.With(c.auth.Require).Post("/media/save", c.mediaSave)
|
||||
r.With(c.auth.Optional).Post("/media/view", c.mediaView)
|
||||
r.With(c.auth.Optional).Post("/media/view-session", c.mediaViewSession)
|
||||
r.With(c.auth.Require).Get("/media/liked", c.mediaLiked)
|
||||
r.Get("/media/stream", c.streamMediaByQuery)
|
||||
r.Head("/media/stream", c.streamMediaByQuery)
|
||||
r.Get("/media/thumb", c.thumbnailMediaByQuery)
|
||||
r.With(c.auth.RequireIfLoginRequired).Get("/media/stream", c.streamMediaByQuery)
|
||||
r.With(c.auth.RequireIfLoginRequired).Head("/media/stream", c.streamMediaByQuery)
|
||||
r.With(c.auth.RequireIfLoginRequired).Get("/media/thumb", c.thumbnailMediaByQuery)
|
||||
|
||||
r.With(c.auth.Optional).Get("/media/{id}/tags", c.mediaTags)
|
||||
r.With(c.auth.Require).Post("/media/{id}/tags", c.addMediaTag)
|
||||
@@ -132,10 +132,15 @@ func (c *Core) putSyncState(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "clientId is required")
|
||||
return
|
||||
}
|
||||
mediaID, ok := syncScalar(input["mediaId"])
|
||||
if !ok {
|
||||
writeError(w, http.StatusBadRequest, "mediaId must be a string, a number or null")
|
||||
return
|
||||
}
|
||||
sessionID := boundedSyncString(firstNonEmpty(syncString(input, "sessionId"), r.URL.Query().Get("sessionId"), "default"))
|
||||
state := map[string]any{
|
||||
"sessionId": sessionID,
|
||||
"mediaId": input["mediaId"],
|
||||
"mediaId": mediaID,
|
||||
"timeMs": syncInt(input, "timeMs", 0),
|
||||
"paused": syncBool(input, "paused"),
|
||||
"fps": syncFPS(input),
|
||||
@@ -144,7 +149,12 @@ func (c *Core) putSyncState(w http.ResponseWriter, r *http.Request) {
|
||||
"updatedAt": time.Now().UTC().Format(time.RFC3339Nano),
|
||||
}
|
||||
if value, ok := input["playAt"]; ok {
|
||||
state["playAt"] = value
|
||||
playAt, valid := syncScalar(value)
|
||||
if !valid {
|
||||
writeError(w, http.StatusBadRequest, "playAt must be a string, a number or null")
|
||||
return
|
||||
}
|
||||
state["playAt"] = playAt
|
||||
}
|
||||
c.sync.SetHTTPState(sessionID, state)
|
||||
c.sync.Broadcast(sessionID)
|
||||
@@ -179,7 +189,7 @@ func (c *Core) streamMediaByQuery(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) streamMediaIdentifier(w http.ResponseWriter, r *http.Request, identifier string) {
|
||||
record, err := c.findMedia(r.Context(), identifier)
|
||||
record, err := c.findMedia(r.Context(), identifier, canSeeHidden(r))
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, "media not found")
|
||||
return
|
||||
@@ -197,6 +207,11 @@ func (c *Core) thumbnailMediaIdentifier(w http.ResponseWriter, r *http.Request,
|
||||
writeError(w, http.StatusBadRequest, "invalid media id")
|
||||
return
|
||||
}
|
||||
includeHidden := canSeeHidden(r)
|
||||
if !includeHidden && c.isHiddenMedia(r.Context(), id) {
|
||||
writeError(w, http.StatusNotFound, "thumbnail not found")
|
||||
return
|
||||
}
|
||||
if !strings.Contains(id, string(filepath.Separator)) && !strings.Contains(id, "/") {
|
||||
if c.carrierNode.ProxyThumbnail(w, r, c.serviceName, id) {
|
||||
return
|
||||
@@ -216,7 +231,7 @@ func (c *Core) thumbnailMediaIdentifier(w http.ResponseWriter, r *http.Request,
|
||||
return
|
||||
}
|
||||
}
|
||||
record, err := c.findMedia(r.Context(), id)
|
||||
record, err := c.findMedia(r.Context(), id, includeHidden)
|
||||
if err != nil || !isImagePath(record.FilePath) {
|
||||
writeError(w, http.StatusNotFound, "thumbnail not found")
|
||||
return
|
||||
@@ -723,7 +738,25 @@ func (c *Core) profile(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"user": user})
|
||||
}
|
||||
|
||||
func (c *Core) findMedia(ctx context.Context, identifier string) (MediaRecord, error) {
|
||||
// canSeeHidden reports whether the caller is a moderator, who may stream and view media that has
|
||||
// been hidden (active = FALSE). Everyone else gets hidden media as not found.
|
||||
func canSeeHidden(r *http.Request) bool {
|
||||
return sharedauth.IsAdmin(sharedauth.UserFromContext(r.Context()))
|
||||
}
|
||||
|
||||
// isHiddenMedia reports whether identifier names a media item that has been hidden. A lookup
|
||||
// error counts as hidden, so a database fault never exposes hidden media.
|
||||
func (c *Core) isHiddenMedia(ctx context.Context, identifier string) bool {
|
||||
var hidden bool
|
||||
err := c.db.QueryRow(ctx, `
|
||||
SELECT EXISTS (SELECT 1 FROM media WHERE NOT active AND (media_key=$1 OR CAST(id AS TEXT)=$1))`,
|
||||
strings.TrimSpace(identifier)).Scan(&hidden)
|
||||
return err != nil || hidden
|
||||
}
|
||||
|
||||
// findMedia looks up one media item by media key or numeric id. Hidden media is only returned
|
||||
// when includeHidden is set.
|
||||
func (c *Core) findMedia(ctx context.Context, identifier string, includeHidden bool) (MediaRecord, error) {
|
||||
identifier = strings.TrimSpace(identifier)
|
||||
var record MediaRecord
|
||||
row := c.db.QueryRow(ctx, `
|
||||
@@ -732,7 +765,7 @@ func (c *Core) findMedia(ctx context.Context, identifier string) (MediaRecord, e
|
||||
COALESCE(width, 0), COALESCE(height, 0), COALESCE(duration_ms, 0), COALESCE(size_bytes, 0),
|
||||
COALESCE(is_vr, FALSE), COALESCE(has_funscript, FALSE), funscript_action_count,
|
||||
funscript_avg_speed, funscript_max_speed, COALESCE(speed, 0), COALESCE(modified_at, created_at)
|
||||
FROM media WHERE media_key=$1 OR CAST(id AS TEXT)=$1 LIMIT 1`, identifier)
|
||||
FROM media WHERE (media_key=$1 OR CAST(id AS TEXT)=$1) AND (active OR $2) LIMIT 1`, identifier, includeHidden)
|
||||
var title, description string
|
||||
var width, height int
|
||||
var duration, size int64
|
||||
@@ -1084,6 +1117,20 @@ func syncBool(message map[string]any, key string) bool {
|
||||
return value
|
||||
}
|
||||
|
||||
// syncScalar keeps a client-supplied sync value to what sync state holds: null, a number or a
|
||||
// bounded string. Objects, arrays and booleans are refused so arbitrary JSON is never stored as
|
||||
// shared session state.
|
||||
func syncScalar(value any) (any, bool) {
|
||||
switch typed := value.(type) {
|
||||
case nil, float64:
|
||||
return typed, true
|
||||
case string:
|
||||
return boundedSyncString(typed), true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
func syncInt(message map[string]any, key string, fallback int64) int64 {
|
||||
value, ok := message[key].(float64)
|
||||
if !ok {
|
||||
|
||||
Reference in new issue
Block a user