Skip to content

Commit 9b7ed4a

Browse files
committed
firewalldb: dont allow overwriting priv map keys
1 parent 7f0f385 commit 9b7ed4a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

firewalldb/privacy_mapper.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ func (p *privacyMapTx) NewPair(real, pseudo string) error {
194194
return err
195195
}
196196

197+
if len(realToPseudoBucket.Get([]byte(real))) != 0 {
198+
return fmt.Errorf("an entry already exists for real "+
199+
"value: %x", real)
200+
}
201+
202+
if len(pseudoToRealBucket.Get([]byte(pseudo))) != 0 {
203+
return fmt.Errorf("an entry already exists for pseudo "+
204+
"value: %x", pseudo)
205+
}
206+
197207
err = realToPseudoBucket.Put([]byte(real), []byte(pseudo))
198208
if err != nil {
199209
return err

firewalldb/privacy_mapper_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ func TestPrivacyMapStorage(t *testing.T) {
6161

6262
return nil
6363
})
64+
65+
pdb3 := db.PrivacyDB([4]byte{3, 3, 3, 3})
66+
67+
_ = pdb3.Update(func(tx PrivacyMapTx) error {
68+
// Add a new pair.
69+
err = tx.NewPair("real 1", "pseudo 1")
70+
require.NoError(t, err)
71+
72+
// Try to add a new pair that has the same real value as the
73+
// first pair. This should fail.
74+
err = tx.NewPair("real 1", "pseudo 2")
75+
require.ErrorContains(t, err, "an entry already exists for "+
76+
"real value")
77+
78+
// Try to add a new pair that has the same pseudo value as the
79+
// first pair. This should fail.
80+
err = tx.NewPair("real 2", "pseudo 1")
81+
require.ErrorContains(t, err, "an entry already exists for "+
82+
"pseudo value")
83+
84+
return nil
85+
})
6486
}
6587

6688
// TestPrivacyMapTxs tests that the `Update` and `View` functions correctly

0 commit comments

Comments
 (0)