Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

fix: validate relationships on import

Matthew Kilgored80d33a92029
diff --git a/backend/internal/core/services/service_exports.go b/backend/internal/core/services/service_exports.go
index 986b4dde..887275b1 100644
--- a/backend/internal/core/services/service_exports.go
+++ b/backend/internal/core/services/service_exports.go
@@ -63,6 +63,13 @@ const attachmentsDir = "attachments/"
// import builds INSERT from the JSON keys. Adding a new TABLE still requires
// editing this list and (probably) the dependency graph; same for adding a
// new FK column to an existing table that points at another exported table.
+//
+// Every foreign-key column on an exported table MUST be listed in exactly one
+// of groupCols, userCols, fkCols or deferCols. The import introspects the
+// destination schema's FK columns and refuses any row carrying a FK column
+// that is not covered by the spec, so forgetting to add a new FK here fails
+// the import loudly instead of letting an attacker-controlled zip write a raw
+// UUID into it. See replayImportRows.
type tableSpec struct {
// name is the SQL table name.
name string
@@ -97,7 +104,8 @@ type tableSpec struct {
// server (or a server that already received this export once), reusing PKs
// causes UNIQUE-constraint violations. Remapping always = simpler invariant.
//
-// Self-referential FKs (entities.entity_children, tags.tag_children,
+// Self-referential FKs (entities.entity_children,
+// entities.entity_location_entities, tags.tag_children,
// attachments.attachment_thumbnail) and forward-circular FKs
// (entity_types.entity_type_default_template,
// entity_templates.entity_template_location) live in deferCols so the first
@@ -141,7 +149,10 @@ var exportTables = []tableSpec{
pkCol: "id",
groupCols: []string{"group_entities"},
fkCols: map[string]string{"entity_type_entities": "entity_types"},
- deferCols: map[string]string{"entity_children": entitiesTable},
+ deferCols: map[string]string{
+ "entity_children": entitiesTable,
+ "entity_location_entities": entitiesTable,
+ },
},
{
name: "entity_fields",
@@ -1084,6 +1095,67 @@ func boolColumns(ctx context.Context, db sqlQuerier, dialect, table string) (map
return cols, nil
}
+// fkColumns returns the columns of table that participate in a foreign-key
+// constraint on the destination database. The import uses this to make sure
+// every FK column a row carries is one the tableSpec knows how to remap; see
+// rejectUnmappedFKColumns.
+func fkColumns(ctx context.Context, db sqlQuerier, dialect, table string) (map[string]struct{}, error) {
+ if !isValidSQLIdent(table) {
+ return nil, fmt.Errorf("invalid table identifier %q", table)
+ }
+
+ query := `SELECT "from" FROM pragma_foreign_key_list(?)`
+ if dialect == "postgres" {
+ query = `SELECT kcu.column_name
+ FROM information_schema.table_constraints tc
+ JOIN information_schema.key_column_usage kcu
+ ON kcu.constraint_name = tc.constraint_name
+ AND kcu.constraint_schema = tc.constraint_schema
+ AND kcu.table_name = tc.table_name
+ WHERE tc.constraint_type = 'FOREIGN KEY'
+ AND tc.table_schema = current_schema()
+ AND tc.table_name = $1`
+ }
+
+ rows, err := db.QueryContext(ctx, query, table)
+ if err != nil {
+ return nil, fmt.Errorf("introspect foreign keys on %q: %w", table, err)
+ }
+ defer func() { _ = rows.Close() }()
+
+ cols := make(map[string]struct{})
+ for rows.Next() {
+ var name string
+ if err := rows.Scan(&name); err != nil {
+ return nil, fmt.Errorf("introspect foreign keys on %q: %w", table, err)
+ }
+ cols[name] = struct{}{}
+ }
+ if err := rows.Err(); err != nil {
+ return nil, fmt.Errorf("introspect foreign keys on %q: %w", table, err)
+ }
+ return cols, nil
+}
+
+// rejectUnmappedFKColumns fails if row carries a non-null value in a
+// foreign-key column that the tableSpec does not remap. insertRow turns
+// every JSON key into a column, so without this check a crafted archive
+// could set e.g. entities.entity_location_entities to a UUID owned by
+// another group and the id-only FK constraint would accept it.
+func rejectUnmappedFKColumns(row map[string]any, table string, dbFKCols, knownFKCols map[string]struct{}) error {
+ for col := range dbFKCols {
+ if _, known := knownFKCols[col]; known {
+ continue
+ }
+ v, present := row[col]
+ if !present || v == nil || v == "" {
+ continue
+ }
+ return fmt.Errorf("insert %s: foreign key column %q is not supported by this import and cannot be set", table, col)
+ }
+ return nil
+}
+
// coerceBoolColumns rewrites the boolean-typed entries of row in place so the
// driver is handed a real bool regardless of how the source engine encoded it.
func coerceBoolColumns(row map[string]any, boolCols map[string]struct{}) error {
@@ -1272,23 +1344,48 @@ func (s *ExportService) replayImportRows(ctx context.Context, tx *sql.Tx, zr *zi
idMap[table][oldID] = newID
}
- // remapFK substitutes an old FK value with its remapped new value, or
- // returns the original if unknown (which surfaces as a FK violation on
- // insert — better to fail loud than silently null it out).
- remapFK := func(target string, v any) any {
+ // remapFK substitutes an old FK value with its remapped new value. A
+ // value that is not in the id map is a hard error: every row an export
+ // can legitimately reference lives in the same zip, so an unknown id can
+ // only come from a hand-crafted archive. Passing the original value
+ // through would let it satisfy the DB FK constraint against a row owned
+ // by a different group (the FKs are id-only, with no group component),
+ // which is a cross-tenant write.
+ remapFK := func(target string, v any) (any, error) {
if v == nil {
- return nil
+ return nil, nil
}
s := fmt.Sprint(v)
if s == "" {
- return nil
+ return nil, nil
}
if mapping, ok := idMap[target]; ok {
if newID, found := mapping[s]; found {
- return newID
+ return newID, nil
}
}
- return v
+ return nil, fmt.Errorf("foreign key %q references a %s row that is not part of this export", s, target)
+ }
+
+ // specFKCols is the set of FK columns the spec knows how to remap for
+ // each table. Any other FK column found on the destination schema is
+ // rejected if a row carries it: without a remap entry its value would be
+ // inserted verbatim and could point at another group's row.
+ specFKCols := func(spec tableSpec) map[string]struct{} {
+ out := make(map[string]struct{})
+ for _, c := range spec.groupCols {
+ out[c] = struct{}{}
+ }
+ for _, c := range spec.userCols {
+ out[c] = struct{}{}
+ }
+ for c := range spec.fkCols {
+ out[c] = struct{}{}
+ }
+ for c := range spec.deferCols {
+ out[c] = struct{}{}
+ }
+ return out
}
type deferredUpdate struct {
@@ -1310,13 +1407,21 @@ func (s *ExportService) replayImportRows(ctx context.Context, tx *sql.Tx, zr *zi
if err != nil {
return nil, err
}
+ dbFKCols, err := fkColumns(ctx, tx, s.dialect, spec.name)
+ if err != nil {
+ return nil, err
+ }
+ knownFKCols := specFKCols(spec)
for _, row := range rows {
+ if err := rejectUnmappedFKColumns(row, spec.name, dbFKCols, knownFKCols); err != nil {
+ return nil, err
+ }
if err := coerceBoolColumns(row, boolCols); err != nil {
return nil, fmt.Errorf("insert %s: %w", spec.name, err)
}
newID, err := remapImportRow(row, spec, gid, userID, srcGroupID, remapFK, rememberID)
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("insert %s: %w", spec.name, err)
}
for col, target := range spec.deferCols {
if v, ok := row[col]; ok && v != nil && v != "" {
@@ -1340,7 +1445,10 @@ func (s *ExportService) replayImportRows(ctx context.Context, tx *sql.Tx, zr *zi
// Apply deferred updates (self-referential and forward-circular FKs).
for _, d := range deferred {
- newFK := remapFK(d.targetTable, d.oldFKValue)
+ newFK, err := remapFK(d.targetTable, d.oldFKValue)
+ if err != nil {
+ return nil, fmt.Errorf("deferred update %s.%s: %w", d.table, d.col, err)
+ }
q := fmt.Sprintf("UPDATE %s SET %s = %s WHERE id = %s",
d.table, d.col, placeholder(s.dialect, 1), placeholder(s.dialect, 2))
if _, err := tx.ExecContext(ctx, q, newFK, d.newID); err != nil {
@@ -1360,7 +1468,7 @@ func remapImportRow(
row map[string]any,
spec tableSpec,
gid, userID, srcGroupID uuid.UUID,
- remapFK func(target string, v any) any,
+ remapFK func(target string, v any) (any, error),
rememberID func(table, oldID, newID string),
) (string, error) {
var newID string
@@ -1384,7 +1492,11 @@ func remapImportRow(
}
for col, target := range spec.fkCols {
if v, ok := row[col]; ok {
- row[col] = remapFK(target, v)
+ newV, err := remapFK(target, v)
+ if err != nil {
+ return "", fmt.Errorf("column %q: %w", col, err)
+ }
+ row[col] = newV
}
}
// Attachment paths are "{group_id}/documents/{hash}"; rewrite the source
diff --git a/backend/internal/core/services/service_exports_test.go b/backend/internal/core/services/service_exports_test.go
index 97705bdb..e5a837b5 100644
--- a/backend/internal/core/services/service_exports_test.go
+++ b/backend/internal/core/services/service_exports_test.go
@@ -1,9 +1,11 @@
package services
import (
+ "archive/zip"
"bytes"
"context"
"encoding/csv"
+ "encoding/json"
"io"
"strings"
"testing"
@@ -463,3 +465,170 @@ func copyBlobUnderTest(ctx context.Context, svc *ExportService, srcKey, dstKey s
}
return w.Close()
}
+
+// tamperedImportZip rebuilds the export zip stored at srcKey with the rows of
+// one table rewritten by mutate, stages the result as an import upload for
+// dst, and returns the upload key and size.
+func tamperedImportZip(t *testing.T, ctx context.Context, srcKey string, dst uuid.UUID, table string, mutate func(rows []map[string]any)) (string, int64) {
+ t.Helper()
+ att := tSvc.Exports.repos.Attachments
+ bk, err := blob.OpenBucket(ctx, att.GetConnString())
+ require.NoError(t, err)
+ defer func() { _ = bk.Close() }()
+
+ raw, err := bk.ReadAll(ctx, att.GetFullPath(srcKey))
+ require.NoError(t, err)
+ zr, err := zip.NewReader(bytes.NewReader(raw), int64(len(raw)))
+ require.NoError(t, err)
+
+ var out bytes.Buffer
+ zw := zip.NewWriter(&out)
+ for _, f := range zr.File {
+ rc, err := f.Open()
+ require.NoError(t, err)
+ data, err := io.ReadAll(rc)
+ _ = rc.Close()
+ require.NoError(t, err)
+ if f.Name == table+".json" {
+ var rows []map[string]any
+ require.NoError(t, json.Unmarshal(data, &rows))
+ mutate(rows)
+ data, err = json.Marshal(rows)
+ require.NoError(t, err)
+ }
+ w, err := zw.Create(f.Name)
+ require.NoError(t, err)
+ _, err = w.Write(data)
+ require.NoError(t, err)
+ }
+ require.NoError(t, zw.Close())
+
+ importKey := dst.String() + "/imports/" + uuid.New().String() + ".zip"
+ require.NoError(t, bk.WriteAll(ctx, att.GetFullPath(importKey), out.Bytes(), nil))
+ return importKey, int64(out.Len())
+}
+
+// TestImportRejectsForeignKeyOutsideArchive asserts that an import cannot
+// smuggle a reference to a row owned by another group. Every FK in a
+// legitimate export resolves inside the archive; a UUID that is not in the
+// archive's id map used to be passed through verbatim and accepted by the
+// id-only DB FK constraint, which let an attacker parent their entity under
+// a victim group's location and read its ancestor chain.
+func TestImportRejectsForeignKeyOutsideArchive(t *testing.T) {
+ ctx := context.Background()
+
+ // Victim group with a location whose id the attacker "knows".
+ victim, err := tRepos.Groups.GroupCreate(ctx, "victim-"+fk.Str(4), uuid.Nil)
+ require.NoError(t, err)
+ victimLocET, err := tRepos.EntityTypes.GetDefault(ctx, victim.ID, true)
+ require.NoError(t, err)
+ victimLoc, err := tRepos.Entities.Create(ctx, victim.ID, repo.EntityCreate{
+ Name: "Victim Vault",
+ EntityTypeID: victimLocET.ID,
+ })
+ require.NoError(t, err)
+
+ // Attacker's source group: produce a legitimate export to tamper with.
+ src, err := tRepos.Groups.GroupCreate(ctx, "attacker-src-"+fk.Str(4), uuid.Nil)
+ require.NoError(t, err)
+ srcItemET, err := tRepos.EntityTypes.GetDefault(ctx, src.ID, false)
+ require.NoError(t, err)
+ _, err = tRepos.Entities.Create(ctx, src.ID, repo.EntityCreate{
+ Name: "Probe",
+ EntityTypeID: srcItemET.ID,
+ })
+ require.NoError(t, err)
+ expRow, err := tRepos.Exports.Create(ctx, src.ID)
+ require.NoError(t, err)
+ artifactPath, _, err := tSvc.Exports.buildArtifact(ctx, expRow.ID, src.ID)
+ require.NoError(t, err)
+
+ cases := []struct{ name, col string }{
+ {"parent FK", "entity_children"},
+ {"location override FK", "entity_location_entities"},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ dst, err := tRepos.Groups.GroupCreate(ctx, "attacker-dst-"+fk.Str(4), uuid.Nil)
+ require.NoError(t, err)
+
+ importKey, size := tamperedImportZip(t, ctx, artifactPath, dst.ID, "entities", func(rows []map[string]any) {
+ for _, r := range rows {
+ if r["name"] == "Probe" {
+ r[tc.col] = victimLoc.ID.String()
+ }
+ }
+ })
+ impRow, err := tRepos.Exports.CreateImport(ctx, dst.ID, importKey, size)
+ require.NoError(t, err)
+
+ err = tSvc.Exports.runImport(ctx, dst.ID, tUser.ID, impRow.ID, importKey)
+ require.Error(t, err, "import pointing at another group's row must fail")
+ assert.ErrorContains(t, err, "not part of this export")
+
+ // The tx rolled back: nothing from the archive landed in dst and no
+ // row anywhere references the victim's location.
+ n, err := tClient.Entity.Query().
+ Where(entity.HasGroupWith(group.ID(dst.ID)), entity.Name("Probe")).
+ Count(ctx)
+ require.NoError(t, err)
+ assert.Zero(t, n, "tampered row must not be inserted")
+
+ refs, err := tClient.Entity.Query().
+ Where(entity.Or(
+ entity.HasParentWith(entity.ID(victimLoc.ID)),
+ entity.HasLocationWith(entity.ID(victimLoc.ID)),
+ )).
+ Count(ctx)
+ require.NoError(t, err)
+ assert.Zero(t, refs, "no entity may reference the victim's location")
+ })
+ }
+}
+
+// TestExportTablesCoverAllForeignKeys guards against schema drift: every FK
+// column the destination database has on an exported table must be handled
+// by the tableSpec (group/user/fk/defer). A FK column the spec does not know
+// about would otherwise be inserted verbatim from the archive — which is
+// exactly what rejectUnmappedFKColumns refuses at import time, so this test
+// makes a legitimate export carrying such a column fail here first.
+func TestExportTablesCoverAllForeignKeys(t *testing.T) {
+ ctx := context.Background()
+ db := tSvc.Exports.db.Sql()
+ for _, spec := range exportTables {
+ dbFKs, err := fkColumns(ctx, db, tSvc.Exports.dialect, spec.name)
+ require.NoError(t, err)
+ require.NotEmpty(t, dbFKs, "introspection returned no FK columns for %s", spec.name)
+
+ known := map[string]struct{}{}
+ for _, c := range spec.groupCols {
+ known[c] = struct{}{}
+ }
+ for _, c := range spec.userCols {
+ known[c] = struct{}{}
+ }
+ for c := range spec.fkCols {
+ known[c] = struct{}{}
+ }
+ for c := range spec.deferCols {
+ known[c] = struct{}{}
+ }
+ for col := range dbFKs {
+ _, ok := known[col]
+ assert.True(t, ok, "table %s: FK column %q is not covered by exportTables", spec.name, col)
+ }
+ }
+}
+
+func TestRejectUnmappedFKColumns(t *testing.T) {
+ dbFKs := map[string]struct{}{"a_fk": {}, "b_fk": {}}
+ known := map[string]struct{}{"a_fk": {}}
+
+ require.NoError(t, rejectUnmappedFKColumns(map[string]any{"a_fk": "x", "name": "n"}, "t", dbFKs, known))
+ require.NoError(t, rejectUnmappedFKColumns(map[string]any{"b_fk": nil}, "t", dbFKs, known))
+ require.NoError(t, rejectUnmappedFKColumns(map[string]any{"b_fk": ""}, "t", dbFKs, known))
+
+ err := rejectUnmappedFKColumns(map[string]any{"b_fk": uuid.NewString()}, "t", dbFKs, known)
+ require.Error(t, err)
+ assert.ErrorContains(t, err, `"b_fk"`)
+}
diff --git a/backend/internal/data/repo/repo_entities.go b/backend/internal/data/repo/repo_entities.go
index edfc7785..d91067d7 100644
--- a/backend/internal/data/repo/repo_entities.go
+++ b/backend/internal/data/repo/repo_entities.go
@@ -3004,6 +3004,7 @@ func (r *EntityRepository) PathForEntity(ctx context.Context, gid, entityID uuid
SELECT e.id, e.name, e.entity_children
FROM entities e
JOIN entity_path ep ON e.id = ep.entity_children
+ WHERE e.group_entities = $2
)
SELECT id, name
@@ -3083,6 +3084,7 @@ func (r *EntityRepository) Tree(ctx context.Context, gid uuid.UUID, tq TreeQuery
ON c.entity_children = p.id
WHERE level < 10 -- prevent infinite loop & excessive recursion
AND ct.is_location = true
+ AND c.group_entities = $1
){{ WITH_ITEMS }}
SELECT id,
@@ -3118,6 +3120,7 @@ func (r *EntityRepository) Tree(ctx context.Context, gid uuid.UUID, tq TreeQuery
FROM entities e
JOIN entity_types et ON et.id = e.entity_type_entities
WHERE et.is_location = false
+ AND e.group_entities = $1
AND ` + eff + ` IN (SELECT id FROM entity_tree)
UNION ALL
@@ -3132,6 +3135,7 @@ func (r *EntityRepository) Tree(ctx context.Context, gid uuid.UUID, tq TreeQuery
JOIN item_tree p
ON ` + effChild + ` = p.id
WHERE ct.is_location = false
+ AND c.group_entities = $1
AND level < 10 -- prevent infinite loop & excessive recursion
)`
diff --git a/backend/internal/data/repo/repo_item_attachments.go b/backend/internal/data/repo/repo_item_attachments.go
index d33cfb91..8d0892b0 100644
--- a/backend/internal/data/repo/repo_item_attachments.go
+++ b/backend/internal/data/repo/repo_item_attachments.go
@@ -30,6 +30,8 @@ import (
"github.com/sysadminsmedia/homebox/backend/pkgs/utils"
"github.com/zeebo/blake3"
"go.opentelemetry.io/otel"
+
+ entsql "entgo.io/ent/dialect/sql"
"golang.org/x/image/draw"
"gocloud.dev/blob"
@@ -560,11 +562,36 @@ func (r *AttachmentRepo) Delete(ctx context.Context, gid uuid.UUID, id uuid.UUID
}
// If this is the last attachment for this path, delete the file
if len(all) == 1 {
- thumb, err := doc.QueryThumbnail().First(ctx)
+ // Only follow the thumbnail edge to a row whose blob lives under this
+ // group's prefix. The attachment_thumbnail FK is id-only, so a row
+ // that was linked to another group's thumbnail (e.g. via a crafted
+ // import) must not be able to delete that group's blob or row.
+ thumb, err := doc.QueryThumbnail().
+ Where(attachment.PathHasPrefix(gid.String() + "/")).
+ First(ctx)
if err != nil && !ent.IsNotFound(err) {
log.Err(err).Msg("failed to query thumbnail for attachment")
return err
}
+ if thumb != nil {
+ // If any other attachment row still points at this thumbnail,
+ // just unlink ours and leave the shared row and blob alone.
+ others, err := r.db.Attachment.Query().
+ Where(
+ attachment.IDNEQ(doc.ID),
+ func(s *entsql.Selector) {
+ s.Where(entsql.EQ(s.C(attachment.ThumbnailColumn), thumb.ID))
+ },
+ ).
+ Count(ctx)
+ if err != nil {
+ return err
+ }
+ if others > 0 {
+ _ = doc.Update().SetNillableThumbnailID(nil).SaveX(ctx)
+ thumb = nil
+ }
+ }
if thumb != nil {
thumbBucket, err := blob.OpenBucket(ctx, r.GetConnString())
if err != nil {