package domain import ( "errors" "fmt" "time" "github.com/google/uuid" ) // ErrInvalidAssessmentLeakTrace reports a malformed leak trace record. var ErrInvalidAssessmentLeakTrace = errors.New("invalid assessment leak trace") // AssessmentLeakTraceStatus is how a search for the source of a leaked text // ended. Not found is a normal outcome, and ambiguous means that the data does // not tell several attempts apart; neither names an attempt. type AssessmentLeakTraceStatus string const ( AssessmentLeakTraceFound AssessmentLeakTraceStatus = "found" AssessmentLeakTraceAmbiguous AssessmentLeakTraceStatus = "ambiguous" AssessmentLeakTraceNotFound AssessmentLeakTraceStatus = "not_found" ) // IsKnown reports whether the status is one of the stored values. func (status AssessmentLeakTraceStatus) IsKnown() bool { switch status { case AssessmentLeakTraceFound, AssessmentLeakTraceAmbiguous, AssessmentLeakTraceNotFound: return true default: return false } } // AssessmentLeakTraceMethod names the evidence that decided a search. type AssessmentLeakTraceMethod string const ( // AssessmentLeakTraceInvisibleMark is the code that the zero-width mark of the // statement carries. It is keyed, so it identifies the attempt on its own. AssessmentLeakTraceInvisibleMark AssessmentLeakTraceMethod = "invisible_mark" // AssessmentLeakTraceVisibleCode is the code of the faint tile, read from the // text of a screenshot. The code is keyed, so it identifies the attempt on // its own; a single wrong character of the recognized text is tolerated. AssessmentLeakTraceVisibleCode AssessmentLeakTraceMethod = "visible_code" // AssessmentLeakTraceWording is the choice of interchangeable phrases among // the attempts that were served the same statement text. AssessmentLeakTraceWording AssessmentLeakTraceMethod = "wording" // AssessmentLeakTraceVariant is a statement text that only one attempt was // ever served. It follows from the seed of the variant, not from a mark. AssessmentLeakTraceVariant AssessmentLeakTraceMethod = "variant" ) // IsKnown reports whether the method is one of the stored values. func (method AssessmentLeakTraceMethod) IsKnown() bool { switch method { case AssessmentLeakTraceInvisibleMark, AssessmentLeakTraceVisibleCode, AssessmentLeakTraceWording, AssessmentLeakTraceVariant: return true default: return false } } // AssessmentLeakTraceRecord is the audit of one search. It holds no leaked // text, only a keyed digest of it and its length. Whatever the outcome, the // record exists before a result is shown, and it names an attempt only when the // search found one. It is not shown to an employer and changes no result, // category, or quota. type AssessmentLeakTraceRecord struct { ID uuid.UUID Actor AssessmentReviewActor SearchedAt time.Time // TextDigest is a keyed digest of the normalized text, so the same text // searched twice is visible in the audit without keeping it. TextDigest AssessmentDigest KeyVersion int16 TextRunes int32 Status AssessmentLeakTraceStatus Method AssessmentLeakTraceMethod AttemptID AssessmentAttemptID // Candidates is the number of attempts that the evidence left in play. Candidates int32 // EvidenceBits is the strength of the deciding evidence, an upper bound on // how rarely another attempt would match it by chance (2^-bits). EvidenceBits int32 } // Validate checks that the record can be stored. func (record AssessmentLeakTraceRecord) Validate() error { const location = "AssessmentLeakTraceRecord.Validate" switch { case record.ID == uuid.Nil, record.Actor == "", record.SearchedAt.IsZero(): return fmt.Errorf("%s - identifier, actor, and time are required: %w", location, ErrInvalidAssessmentLeakTrace) case record.TextDigest.IsZero(), record.KeyVersion < 1, record.TextRunes < 0: return fmt.Errorf("%s - the text digest, its key version, and a length are required: %w", location, ErrInvalidAssessmentLeakTrace) case !record.Status.IsKnown(): return fmt.Errorf("%s - status is unknown: %w", location, ErrInvalidAssessmentLeakTrace) case record.Candidates < 0, record.EvidenceBits < 0: return fmt.Errorf("%s - counts cannot be negative: %w", location, ErrInvalidAssessmentLeakTrace) case record.Status == AssessmentLeakTraceFound && (record.AttemptID.IsZero() || !record.Method.IsKnown()): return fmt.Errorf("%s - a found search names its attempt and its method: %w", location, ErrInvalidAssessmentLeakTrace) case record.Status != AssessmentLeakTraceFound && !record.AttemptID.IsZero(): return fmt.Errorf("%s - only a found search names an attempt: %w", location, ErrInvalidAssessmentLeakTrace) case record.Method != "" && !record.Method.IsKnown(): return fmt.Errorf("%s - method is unknown: %w", location, ErrInvalidAssessmentLeakTrace) } return nil }