feat(assessment): watermark the task text of real attempts

This commit is contained in:
shira committed 2026-10-11 10:44:30 +03:00
1 parent 24b64d698a
commit c975a0d506
57 files changed
+2429 -92

No files matched your search

@@ -33,6 +33,7 @@ const (
labelSignalSession = "lincodex/assessment/signal-session/v1"
labelSignalNetwork = "lincodex/assessment/signal-network/v1"
labelResultCursor = "lincodex/assessment/result-history-cursor/v1"
labelWatermark = "lincodex/assessment/watermark/v1"
)
// Keyring computes the keyed digests of the anonymous identity operations:
@@ -53,6 +54,7 @@ type Keyring struct {
signalSessionKey []byte
signalNetworkKey []byte
resultCursorKey []byte
watermarkKey []byte
}
// NewKeyring derives the purpose keys from secret.
@@ -73,6 +75,7 @@ func NewKeyring(secret []byte) (*Keyring, error) {
signalSessionKey: derive(secret, labelSignalSession),
signalNetworkKey: derive(secret, labelSignalNetwork),
resultCursorKey: derive(secret, labelResultCursor),
watermarkKey: derive(secret, labelWatermark),
}, nil
}
@@ -229,3 +232,13 @@ func (keyring *Keyring) AssessmentNetworkDigest(attemptID uuid.UUID, clientKey s
mac.Write([]byte(clientKey))
return domain.AssessmentDigest(mac.Sum(nil))
}
// AssessmentWatermarkSeed is the secret stream that marks the task text of one
// attempt. It is derived under a key of its own, so it cannot be computed from
// an idempotency scope or a digest, and it is never sent to a client: the
// visible code and the invisible mark are read from it on the server.
func (keyring *Keyring) AssessmentWatermarkSeed(attemptID uuid.UUID) domain.AssessmentWatermarkSeed {
mac := hmac.New(sha256.New, keyring.watermarkKey)
mac.Write(attemptID[:])
return domain.AssessmentWatermarkSeed(mac.Sum(nil))
}
@@ -125,6 +125,33 @@ func TestAssessmentSessionAndNetworkDigestsAreBoundToTheAttempt(t *testing.T) {
}
}
func TestAssessmentWatermarkSeedIsKeyedBoundToTheAttemptAndSeparateFromTheDigests(t *testing.T) {
keyring, err := NewKeyring(bytes.Repeat([]byte{1}, MinKeySecretBytes))
if err != nil {
t.Fatalf("NewKeyring() error = %v", err)
}
other, err := NewKeyring(bytes.Repeat([]byte{2}, MinKeySecretBytes))
if err != nil {
t.Fatalf("NewKeyring(other secret) error = %v", err)
}
attempt, otherAttempt := uuid.New(), uuid.New()
seed := keyring.AssessmentWatermarkSeed(attempt)
if seed.IsZero() || seed != keyring.AssessmentWatermarkSeed(attempt) {
t.Fatal("the watermark seed is empty or not deterministic")
}
if seed == keyring.AssessmentWatermarkSeed(otherAttempt) {
t.Fatal("two attempts have the same watermark seed")
}
if seed == other.AssessmentWatermarkSeed(attempt) {
t.Fatal("the watermark seed does not depend on the server secret, so it could be computed by anyone")
}
// The purpose key is its own: no other digest of the attempt equals the seed.
if seed == domain.AssessmentWatermarkSeed(keyring.AssessmentSessionDigest(attempt, attempt)) ||
seed == domain.AssessmentWatermarkSeed(keyring.AssessmentNetworkDigest(attempt, string(attempt[:]))) {
t.Fatal("the watermark seed shares a key with another assessment digest")
}
}
func TestResultHistoryCursorKeyIsSeparateFromIdempotencyAndBoundToTheCandidate(t *testing.T) {
keyring, err := NewKeyring([]byte(strings.Repeat("k", MinKeySecretBytes)))
if err != nil {