feat(assessment): add the review band of attempt signals and the disclosed collector

This commit is contained in:
shira committed 2026-10-08 23:04:57 +03:00
1 parent 68286af8b7
commit b3c6dc870b
124 files changed
+13522 -633

No files matched your search

@@ -4,9 +4,12 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
@@ -24,6 +27,10 @@ const (
labelTokenBucket = "lincodex/identity/verification-token-bucket/v1"
labelSessionCSRF = "lincodex/identity/session-csrf/v1"
labelCandidateView = "lincodex/identity/candidate-view/v1"
labelAnswerFingerprint = "lincodex/assessment/answer-fingerprint/v1"
labelSignalSession = "lincodex/assessment/signal-session/v1"
labelSignalNetwork = "lincodex/assessment/signal-network/v1"
)
// Keyring computes the keyed digests of the anonymous identity operations:
@@ -39,6 +46,9 @@ type Keyring struct {
tokenKey []byte
sessionCSRFKey []byte
candidateViewKey []byte
answerKey []byte
signalSessionKey []byte
signalNetworkKey []byte
}
// NewKeyring derives the purpose keys from secret.
@@ -54,6 +64,9 @@ func NewKeyring(secret []byte) (*Keyring, error) {
tokenKey: derive(secret, labelTokenBucket),
sessionCSRFKey: derive(secret, labelSessionCSRF),
candidateViewKey: derive(secret, labelCandidateView),
answerKey: derive(secret, labelAnswerFingerprint),
signalSessionKey: derive(secret, labelSignalSession),
signalNetworkKey: derive(secret, labelSignalNetwork),
}, nil
}
@@ -143,3 +156,39 @@ func (keyring *Keyring) VerifySessionCSRF(token domain.SecretToken, supplied str
}
return hmac.Equal([]byte(keyring.SessionCSRF(token)), []byte(supplied))
}
// AssessmentDigestKeyVersion is the key generation of the assessment
// fingerprints and digests. It is stored next to each of them; a rotation adds
// a new generation instead of silently changing the old values.
func (keyring *Keyring) AssessmentDigestKeyVersion() int16 { return 1 }
// AssessmentAnswerFingerprint is the keyed digest of a normalized answer to a
// family version. It is the same for every candidate and every seed, which
// lets stored fingerprints be compared without the answer text.
func (keyring *Keyring) AssessmentAnswerFingerprint(familyID uuid.UUID, familyVersion int32, canonicalAnswer []byte) domain.AssessmentDigest {
mac := hmac.New(sha256.New, keyring.answerKey)
mac.Write(familyID[:])
mac.Write(binary.BigEndian.AppendUint32(nil, uint32(familyVersion))) //nolint:gosec // A family version is a positive int32.
mac.Write(canonicalAnswer)
return domain.AssessmentDigest(mac.Sum(nil))
}
// AssessmentSessionDigest identifies a login session inside one attempt. The
// digest changes with the attempt, so it cannot link attempts or reveal the
// session identifier.
func (keyring *Keyring) AssessmentSessionDigest(attemptID, sessionID uuid.UUID) domain.AssessmentDigest {
mac := hmac.New(sha256.New, keyring.signalSessionKey)
mac.Write(attemptID[:])
mac.Write(sessionID[:])
return domain.AssessmentDigest(mac.Sum(nil))
}
// AssessmentNetworkDigest identifies a client network inside one attempt from
// its limiter key. The address itself is never stored, and the digest cannot
// link attempts.
func (keyring *Keyring) AssessmentNetworkDigest(attemptID uuid.UUID, clientKey string) domain.AssessmentDigest {
mac := hmac.New(sha256.New, keyring.signalNetworkKey)
mac.Write(attemptID[:])
mac.Write([]byte(clientKey))
return domain.AssessmentDigest(mac.Sum(nil))
}