Add masked fallback on decryption error
epic-contact-detail-and-edit-foundation-task-009 — Extend field-encryption-utils with a masked fallback strategy: when decryption fails (wrong key, network error, integrity violation), return a MaskedFieldResult with a redacted placeholder string (e.g. '••••••••') and an error code. Ensure the fallback never exposes partial plaintext or raw cipher bytes to the UI layer.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 5 - 253 tasks
Can start after Tier 4 completes
Implementation Notes
Keep decryptOrMask() a thin adapter — its only responsibility is mapping the DecryptionResult sealed type to FieldDisplayResult. Do not duplicate any crypto logic here. Use a try/catch around the call to the task-008 decryption isolate to catch unexpected errors and degrade to MaskedFieldResult(errorCode: KEY_UNAVAILABLE). Define kMaskedFieldPlaceholder as a const String in a shared constants file so the UI and tests share the same value.
The fixed-width placeholder (not length-matched) is intentional security design — length-matched masks leak metadata about field content. Document this decision in a code comment so future developers do not 'fix' it. The UI layer (not this utility) is responsible for showing tooltips or icons; this layer only provides the errorCode signal.
Testing Requirements
Unit tests with flutter_test: (1) verify decryptOrMask() returns DisplayFieldResult on successful decryption from task-008, (2) verify each DecryptionErrorCode maps to MaskedFieldResult with correct errorCode, (3) assert MaskedFieldResult.maskedValue equals kMaskedFieldPlaceholder for all error paths, (4) assert no substring of the original ciphertext appears in MaskedFieldResult, (5) verify KEY_UNAVAILABLE is returned when key store throws a network exception. Use mock of the decrypt function from task-008. Verify the sealed type pattern-match is exhaustive by compiling switch statements — Dart will error if a case is missing.
Blindeforbundet's encryption key retrieval mechanism may not be finalised at implementation time, or session key availability via Supabase RLS may be inconsistent, causing decryption failures that expose masked placeholders to users and degrade the experience.
Mitigation & Contingency
Mitigation: Agree with Blindeforbundet on key storage and retrieval contract before implementation starts. Prototype key retrieval in a spike against the staging Supabase instance and validate the full decrypt/verify cycle with real test data before committing to the implementation.
Contingency: Implement a fallback that shows a 'field temporarily unavailable' state with a retry affordance. Log decryption failures server-side for audit. Escalate to Blindeforbundet stakeholders to unblock key management before the service tier epic begins.
NHF contacts may belong to up to 5 chapters, each governed by separate RLS policies. A coordinator's chapter scope may not cover all affiliations, causing partial profile reads or silent data omissions that are difficult to detect in tests.
Mitigation & Contingency
Mitigation: Map all RLS policy combinations for multi-chapter contacts early. Write integration tests that create contacts with 5 affiliations and query them from coordinators with varying chapter scopes. Use Supabase's RLS test utilities to verify row visibility per role.
Contingency: Add an explicit 'affiliation partially visible' state in the repository response model so the UI can communicate scope limitations to the coordinator rather than silently showing incomplete data.
Organisation-specific validation rules (e.g., NHF chapter limit, Blindeforbundet encrypted field edit flow) may expand in scope during implementation as edge cases are discovered, causing the validator to grow beyond the planned complexity.
Mitigation & Contingency
Mitigation: Define the complete validation rule set with product and org stakeholders before coding begins. Document each rule with its source organisation and acceptance test. Use a rule registry pattern so new rules can be added without modifying core validator logic.
Contingency: Timebox validator enhancements to 2 hours per additional rule. Defer non-blocking rules to a follow-on maintenance task rather than blocking the epic delivery.