feat(assessment): add the review band of attempt signals and the disclosed collector
This commit is contained in:
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))
|
||||
}
|
||||
@@ -2,9 +2,12 @@ package identity
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
)
|
||||
|
||||
func TestCandidateViewPseudonymKeyIsPurposeDerived(t *testing.T) {
|
||||
@@ -54,3 +57,69 @@ func TestCandidateViewPseudonymKeyRequiresValidKeyring(t *testing.T) {
|
||||
t.Fatal("CandidateViewPseudonymKey() accepted a nil keyring")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssessmentAnswerFingerprintIsKeyedDeterministicAndScoped(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) error = %v", err)
|
||||
}
|
||||
family, otherFamily := uuid.New(), uuid.New()
|
||||
answer := []byte(`{"format":"number","number":42}`)
|
||||
first := keyring.AssessmentAnswerFingerprint(family, 1, answer)
|
||||
if first.IsZero() || first != keyring.AssessmentAnswerFingerprint(family, 1, answer) {
|
||||
t.Fatal("the same family version and answer must give the same non-zero fingerprint")
|
||||
}
|
||||
for name, different := range map[string]domain.AssessmentDigest{
|
||||
"another answer": keyring.AssessmentAnswerFingerprint(family, 1, []byte(`{"format":"number","number":43}`)),
|
||||
"another family": keyring.AssessmentAnswerFingerprint(otherFamily, 1, answer),
|
||||
"another family version": keyring.AssessmentAnswerFingerprint(family, 2, answer),
|
||||
"another secret": other.AssessmentAnswerFingerprint(family, 1, answer),
|
||||
} {
|
||||
if different == first {
|
||||
t.Errorf("%s gave the same fingerprint", name)
|
||||
}
|
||||
}
|
||||
plain := sha256.Sum256(append(append(family[:], 0, 0, 0, 1), answer...))
|
||||
if first == domain.AssessmentDigest(plain) {
|
||||
t.Fatal("the fingerprint is a plain hash and can be checked without the server secret")
|
||||
}
|
||||
if keyring.AssessmentDigestKeyVersion() < 1 {
|
||||
t.Fatal("the digest key version must be positive")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssessmentSessionAndNetworkDigestsAreBoundToTheAttempt(t *testing.T) {
|
||||
keyring, err := NewKeyring(bytes.Repeat([]byte{1}, MinKeySecretBytes))
|
||||
if err != nil {
|
||||
t.Fatalf("NewKeyring() error = %v", err)
|
||||
}
|
||||
attempt, otherAttempt, session, otherSession := uuid.New(), uuid.New(), uuid.New(), uuid.New()
|
||||
sessionDigest := keyring.AssessmentSessionDigest(attempt, session)
|
||||
if sessionDigest != keyring.AssessmentSessionDigest(attempt, session) {
|
||||
t.Fatal("the session digest is not deterministic")
|
||||
}
|
||||
if sessionDigest == keyring.AssessmentSessionDigest(otherAttempt, session) {
|
||||
t.Fatal("one session has the same digest in two attempts, so attempts could be linked")
|
||||
}
|
||||
if sessionDigest == keyring.AssessmentSessionDigest(attempt, otherSession) {
|
||||
t.Fatal("two sessions of one attempt have the same digest")
|
||||
}
|
||||
networkDigest := keyring.AssessmentNetworkDigest(attempt, "198.51.100.0")
|
||||
if networkDigest != keyring.AssessmentNetworkDigest(attempt, "198.51.100.0") {
|
||||
t.Fatal("the network digest is not deterministic")
|
||||
}
|
||||
if networkDigest == keyring.AssessmentNetworkDigest(otherAttempt, "198.51.100.0") {
|
||||
t.Fatal("one network has the same digest in two attempts, so attempts could be linked")
|
||||
}
|
||||
if networkDigest == keyring.AssessmentNetworkDigest(attempt, "198.51.100.1") {
|
||||
t.Fatal("two client networks have the same digest")
|
||||
}
|
||||
// Each purpose has its own key: the same bytes under another purpose differ.
|
||||
if sessionDigest == keyring.AssessmentNetworkDigest(attempt, string(session[:])) {
|
||||
t.Fatal("session and network digests share a key")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user