Skip to content

Commit

Permalink
hints: prevent malicious timestamp hints to bork calculations.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Nov 29, 2024
1 parent 2519cab commit 9df2fc8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions sdk/hints/memory/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func NewHintDB() *HintDB {
}

func (db *HintDB) Save(pubkey string, relay string, key hints.HintKey, ts nostr.Timestamp) {
if now := nostr.Now(); ts > now {
ts = now
}

relayIndex := slices.Index(db.RelayBySerial, relay)
if relayIndex == -1 {
relayIndex = len(db.RelayBySerial)
Expand Down
10 changes: 7 additions & 3 deletions sdk/hints/sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ func (sh SQLiteHints) TopN(pubkey string, n int) []string {
return res
}

func (sh SQLiteHints) Save(pubkey string, relay string, key hints.HintKey, score nostr.Timestamp) {
_, err := sh.saves[key].Exec(pubkey, relay, score, score)
func (sh SQLiteHints) Save(pubkey string, relay string, key hints.HintKey, ts nostr.Timestamp) {
if now := nostr.Now(); ts > now {
ts = now
}

_, err := sh.saves[key].Exec(pubkey, relay, ts, ts)
if err != nil {
nostr.InfoLogger.Printf("[sdk/hints/sqlite] unexpected error on insert for %s, %s, %d: %s\n",
pubkey, relay, score, err)
pubkey, relay, ts, err)
}
}

Expand Down

0 comments on commit 9df2fc8

Please sign in to comment.