Skip to content

Commit be1d8c0

Browse files
committed
add unique index to module path+version
it would be a security violation if there were duplicates. recognize the error on attempt to insert a duplicate, and increase metric for security errors to get alerting for it.
1 parent b53a218 commit be1d8c0

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

data.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ type Message struct {
233233
// configurable number of most recent module versions around.
234234
type ModuleVersion struct {
235235
ID int64
236-
Module string `bstore:"nonzero"`
236+
Module string `bstore:"nonzero,unique Module+Version"`
237237
Version string `bstore:"nonzero"`
238238
Pseudo bool
239239
Prerelease bool

sums.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"log/slog"
@@ -159,6 +160,10 @@ func initTlog() (TreeState, error) {
159160
ts := TreeState{ID: 1, RecordsInitial: n, RecordsProcessed: n}
160161
err = database.Write(context.Background(), func(tx *bstore.Tx) error {
161162
if resetTree {
163+
if _, err := bstore.QueryTx[ModuleVersion](tx).FilterGreaterEqual("LogRecordID", n).Delete(); err != nil {
164+
return fmt.Errorf("removing previously seen module versions during tree reset: %v", err)
165+
}
166+
162167
// Not checking error. We'll get it on insert.
163168
tx.Delete(&TreeState{ID: 1})
164169
}
@@ -392,6 +397,10 @@ func processModules(ts TreeState, ntree tlog.Tree, modversions []module.Version)
392397
LogRecordID: startID + int64(i),
393398
}
394399
if err := tx.Insert(&modvers); err != nil {
400+
if errors.Is(err, bstore.ErrUnique) {
401+
metricTlogSecurityErrors.Inc()
402+
slog.Error("duplicate module path/version in sumdb, security violation", "err", err, "modvers", modvers)
403+
}
395404
return fmt.Errorf("inserting module version: %v", err)
396405
}
397406
}

0 commit comments

Comments
 (0)