fix(postgres): quote the alert lock key so a NUL cannot reach Postgres text
This commit is contained in:
1 parent
3bf64933b0
commit
150bfd4605
2 files changed
+34
-1
No files matched your search
@@ -50,7 +50,10 @@ func alertParams(a alert.Alert) postgresdb.UpsertAlertParams {
|
||||
}
|
||||
}
|
||||
|
||||
func alertLockKey(k alert.Key) string { return string(k.Org) + "\x00" + k.Kind + "\x00" + k.Subject }
|
||||
// Quoted so a NUL in a field is escaped: Postgres text rejects a raw NUL.
|
||||
func alertLockKey(k alert.Key) string {
|
||||
return fmt.Sprintf("%q|%q|%q", string(k.Org), k.Kind, k.Subject)
|
||||
}
|
||||
|
||||
// lockedAlert takes the key's lock and returns the stored alert, nil when
|
||||
// there is none.
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/alert"
|
||||
)
|
||||
|
||||
// The lock key is sent to Postgres as text, which rejects a NUL byte.
|
||||
func TestAlertLockKeyHasNoNUL(t *testing.T) {
|
||||
keys := []alert.Key{
|
||||
{Org: "org", Kind: "provider.no_credential", Subject: "openai"},
|
||||
{Org: "", Kind: "", Subject: ""},
|
||||
{Org: "a", Kind: "b", Subject: "c\x00d"},
|
||||
}
|
||||
for _, k := range keys {
|
||||
if got := alertLockKey(k); strings.Contains(got, "\x00") {
|
||||
t.Errorf("alertLockKey(%+v) = %q contains a NUL byte", k, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertLockKeyDistinguishesFields(t *testing.T) {
|
||||
a := alertLockKey(alert.Key{Org: "ab", Kind: "c", Subject: "d"})
|
||||
b := alertLockKey(alert.Key{Org: "a", Kind: "bc", Subject: "d"})
|
||||
if a == b {
|
||||
t.Errorf("keys with shifted field boundaries collide: %q", a)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user