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
+62
-12
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -42,6 +42,55 @@ func normalizeEntityTimestamps(createdAt, updatedAt, now int64) (int64, int64) {
|
||||
return created, updated
|
||||
}
|
||||
|
||||
// valueReferences returns the list ID of each field and the existence of each
|
||||
// item that the values refer to, using one statement per table.
|
||||
func valueReferences(ctx context.Context, tx pgx.Tx, values []ItemValue) (map[string]string, map[string]bool, error) {
|
||||
fieldLists := make(map[string]string)
|
||||
existingItems := make(map[string]bool)
|
||||
if len(values) == 0 {
|
||||
return fieldLists, existingItems, nil
|
||||
}
|
||||
fieldIDs := make([]string, 0, len(values))
|
||||
itemIDs := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
fieldIDs = append(fieldIDs, value.FieldID)
|
||||
itemIDs = append(itemIDs, value.ItemID)
|
||||
}
|
||||
|
||||
fieldRows, err := tx.Query(ctx, `SELECT id, listid FROM fields WHERE id = ANY($1)`, fieldIDs)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer fieldRows.Close()
|
||||
for fieldRows.Next() {
|
||||
var fieldID, listID string
|
||||
if err := fieldRows.Scan(&fieldID, &listID); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
fieldLists[fieldID] = listID
|
||||
}
|
||||
if err := fieldRows.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
itemRows, err := tx.Query(ctx, `SELECT id FROM items WHERE id = ANY($1)`, itemIDs)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer itemRows.Close()
|
||||
for itemRows.Next() {
|
||||
var itemID string
|
||||
if err := itemRows.Scan(&itemID); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
existingItems[itemID] = true
|
||||
}
|
||||
if err := itemRows.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return fieldLists, existingItems, nil
|
||||
}
|
||||
|
||||
func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) {
|
||||
var request SyncRequest
|
||||
if err := decodeJSON(r, &request); err != nil {
|
||||
@@ -169,23 +218,23 @@ func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve the fields and items the values refer to once per request, not once
|
||||
// per value. Items are read after the item loop above, so this request's own
|
||||
// item inserts are visible.
|
||||
fieldLists, existingItems, err := valueReferences(r.Context(), tx, request.ItemValues)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, value := range request.ItemValues {
|
||||
updatedAt := normalizeTimestamp(int64(value.UpdatedAt), now, now)
|
||||
var listID string
|
||||
fieldErr := tx.QueryRow(r.Context(), `SELECT listid FROM fields WHERE id = $1`, value.FieldID).Scan(&listID)
|
||||
if errors.Is(fieldErr, pgx.ErrNoRows) {
|
||||
listID, ok := fieldLists[value.FieldID]
|
||||
if !ok {
|
||||
orphans = append(orphans, OrphanValue{ID: value.ID, ItemID: value.ItemID, FieldID: value.FieldID, Reason: "missingField"})
|
||||
continue
|
||||
}
|
||||
if fieldErr != nil {
|
||||
return fieldErr
|
||||
}
|
||||
|
||||
var itemExists bool
|
||||
if err := tx.QueryRow(r.Context(), `SELECT EXISTS(SELECT 1 FROM items WHERE id = $1)`, value.ItemID).Scan(&itemExists); err != nil {
|
||||
return err
|
||||
}
|
||||
if !itemExists {
|
||||
if !existingItems[value.ItemID] {
|
||||
if _, err := tx.Exec(r.Context(), `
|
||||
INSERT INTO items (id, listid, createdat, updatedat, isdeleted)
|
||||
VALUES ($1, $2, $3, $3, 0)
|
||||
@@ -194,6 +243,7 @@ func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) {
|
||||
orphans = append(orphans, OrphanValue{ID: value.ID, ItemID: value.ItemID, FieldID: value.FieldID, Reason: "failedCreateStubItem"})
|
||||
continue
|
||||
}
|
||||
existingItems[value.ItemID] = true
|
||||
}
|
||||
if _, err := tx.Exec(r.Context(), `
|
||||
INSERT INTO item_values (id, itemid, fieldid, value, updatedat)
|
||||
|
||||
Reference in new issue
Block a user