88760ae6f6
* fix(whatsapp-inbox): erase the WhatsApp channel on account deletion whatsapp_phone_links relied on the auth.users ON DELETE CASCADE, but Accounted never deletes auth.users: account deletion is anonymize_user_account plus a ~100-year ban that keeps the auth row as a tombstone, so the cascade never fires and nothing revokes the link. After erasure the link stayed active with a decryptable phone_enc, lookupActiveLink kept resolving the number, and every further inbound message was persisted with body_text and the verbatim raw_payload while the bot kept replying: GDPR Art 17 plus continued collection with no lawful basis. The RPC is re-created verbatim from 20260724150000 with one added block that revokes and crypto-shreds the link, resets its conversation, nulls body_text/raw_payload on that link's messages and deletes outstanding link codes, plus a guarded repair pass for tombstones anonymized before this migration. Covered by a pg-real test that fails against the previous definition. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(whatsapp-inbox): pepper the link-code hash and bound code minting hashLinkCode stored a bare sha256 over CODE_ALPHABET^6 = 30^6 values behind a fixed 'AC-' prefix. The module cited the invite-token pattern, but invite tokens are 256-bit random; this space enumerates offline in about a second, so hashing at rest protected nothing. The sibling phone-crypto.ts already states the team's own threat model for a LARGER space ("a plain sha256 would be brute-forceable ... hence the pepper"), so link codes now hash through the same env-mandated pepper. /link/start was also an authenticated unbounded INSERT that left every earlier code valid. Minting now burns the caller's unused codes (the code the panel shows is the only one that works) and is capped per TTL window, with the route answering 429 instead of throwing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(whatsapp-inbox): harden the conversation layer against the review findings Pre-merge hardening of the unshipped chat layer. Every change below has a test that fails without it. Lifecycle and races: - conversation writes go through updateConversation(), an optimistic compare-and-set on updated_at (the trigger makes it a revision counter). The ack winner, the answer worker, the pin refresh and the sweep hold different claims, so blind whole-jsonb writes resurrected answered questions, wiped pending_question and dropped queue entries. - terminal markStatus writes are guarded on processing_status='processing' so a losing worker cannot overwrite the winner's 'done' and null its inbox_item_id. - the message -> inbox item path is idempotent: a pre-check plus a 23505 fallback adopt the item a concurrent worker created, instead of throwing after the WORM document is already committed. - PROCESSING_STUCK_MS 90s -> 5 min. The enforced step budget of one media row already exceeds 90s, so the sweep was re-claiming live workers. - sweep 2b re-arms only when the conversation itself has been quiet, not just the rows: pending_ack=false plus unacked rows is also the state of a live finalize, which produced a duplicate combined ack. - pin expiry re-checks against fresh state instead of writing back a stale whole context, which reverted company choices applied mid-pass. - askNextQueuedQuestion claims the pop before sending, so two answer workers cannot ask the same question twice. Company question: - the state is rolled back when the M6 send fails, so the next receipt re-asks instead of parking receipts behind a question nobody received. - applyCompanyChoice claims the open question (company_options) rather than the state: a double tap confirms once, a transient membership-query error is no longer read as "not a member", and a LATE answer still lands. - at the 48h TTL the parked receipts are kept, not discarded: options and staged rows survive so a late digit or tap still files them, and only rows past Meta's ~30-day media window get the terminal marker. - an out-of-range digit or a typed company name now gets the options repeated instead of silence or the "I cannot answer questions" reply. Inline dispositions: - stop/start/byt/company answers run their side effect BEFORE the terminal wamid row, with a SELECT pre-check for dedupe. Writing the row 'done' first made them at-most-once: a crash in between lost the action forever. Copy and answers: - acks state the extracted currency instead of labelling every total 'kr'. - M17 stops promising "about 10 minutes" when the daily quota tripped. - M18 is sent once per message tracked by the outbound row, so a file whose first attempt died still reaches the sender, including from the max-attempts path. - M11 no longer claims the number is disconnected: 'stopp' pauses, and muted senders now persist no chat content at all. - 'byt' is recognized in every state but awaiting_company (m6-confirm teaches the word, and it was being stored as answer data instead). - text sent while a re-send question is open is kept as a note on THAT receipt with the question left open, instead of binding to another receipt's question. - a quoted reply wins over the pending question and is appended when the quoted question is already answered, so corrections stop landing on the wrong receipt. - context answers keep raw_answer + answered_at like representation does. - finalizeBurst checks the send result: on failure it rolls the question back and leaves the rows unacked for the sweep. PII: - the sender's plaintext number is stripped from raw_payload before it is persisted; replies decrypt the link's phone_enc instead. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(whatsapp-inbox): record the erasure path and the hardening decisions RoPA gains the account-deletion row (immediate, not via the cron: the auth.users cascade never fires because the row is tombstoned) plus the two new security measures, and its "never in the clear" phone claim is now true of the stored payload. DECISIONS.md records the non-obvious calls: revoke-not-delete on erasure, commit-then-roll-back for the company question, keeping expired company choices answerable, the compare-and-set conversation write, effect-before-terminal-row for inline dispositions, honest M11 copy, and the raw_payload redaction. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(whatsapp-inbox): stop the answer re-claim from following a confirm with M16 A worker that died after applying an answer and sending its confirmation leaves the row 'processing'. The sweep re-runs it, resolveAnswerTarget finds the question already answered, and the user got "I did not understand" immediately after the confirmation they had just received. The fallback is now first-attempt only. The catch comment claiming the sweep retries these rows is corrected too: 'error' is terminal for the sweep, and nothing on the answer path throws anyway (interpretChatAnswer degrades, sends never throw, supabase-js returns errors), so the catch is a programming-error net. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(whatsapp-inbox): drop the amount floor on the representation question The Swedish compliance review on #1340 caught a real error in the trigger rules: the representation question only fired above 150 kr, but the duty to document deltagare and syfte is what makes the expense deductible at all (BFL 5 kap 6-7 §) and it is not conditioned on any amount. The 300 kr per person figure I had in mind is the VAT-deduction base cap, a different rule. A 120 kr business lunch would have been booked with no participant trail, which is exactly the deduction Skatteverket denies later. Noise stays bounded by the triggers that were already there: the question fires only for receipt-shaped documents from restaurant, cafe or hotel merchants, at most once per receipt, twice per burst and six times per sender per day, and a single "nej" dismisses it. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Jakob Wennberg <jakob.wennberg@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
184 lines
6.8 KiB
TypeScript
184 lines
6.8 KiB
TypeScript
import { randomUUID, createHash } from 'node:crypto'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { getPool, withUserContext } from './setup'
|
|
import { insertAuthUser } from './fixtures'
|
|
|
|
/**
|
|
* Migration 20260803090000: anonymize_user_account must erase the WhatsApp
|
|
* channel too.
|
|
*
|
|
* whatsapp_phone_links.user_id declares ON DELETE CASCADE, but Accounted
|
|
* never deletes auth.users (the row is tombstoned for ~100 years), so the
|
|
* cascade never fires. Before the migration an erased data subject kept an
|
|
* ACTIVE phone link with a decryptable phone_enc, and every further inbound
|
|
* message was persisted with its content: GDPR Art 17 plus continued
|
|
* collection with no lawful basis. These tests fail against the pre-migration
|
|
* definition of the RPC.
|
|
*/
|
|
|
|
function hash(value: string): string {
|
|
return createHash('sha256').update(value).digest('hex')
|
|
}
|
|
|
|
interface SeededChannel {
|
|
userId: string
|
|
linkId: string
|
|
conversationId: string
|
|
messageId: string
|
|
codeId: string
|
|
}
|
|
|
|
async function seedLinkedUser(): Promise<SeededChannel> {
|
|
const userId = await insertAuthUser()
|
|
await getPool().query(
|
|
`INSERT INTO public.profiles (id, email, full_name)
|
|
VALUES ($1, $2, 'PG Real')
|
|
ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email, full_name = EXCLUDED.full_name`,
|
|
[userId, `pg-real-${userId}@test.invalid`],
|
|
)
|
|
|
|
const linkId = randomUUID()
|
|
await getPool().query(
|
|
`INSERT INTO public.whatsapp_phone_links
|
|
(id, user_id, phone_hash, phone_enc, phone_masked, wa_profile_name)
|
|
VALUES ($1, $2, $3, 'deadbeefcafe', '+46 70 *** ** 67', 'Erased Person')`,
|
|
[linkId, userId, hash(randomUUID())],
|
|
)
|
|
|
|
const conversationId = randomUUID()
|
|
await getPool().query(
|
|
`INSERT INTO public.whatsapp_conversations (id, phone_link_id, state, context)
|
|
VALUES ($1, $2, 'awaiting_company', '{"company_options": [{"id": "x", "name": "Bolag AB"}]}'::jsonb)`,
|
|
[conversationId, linkId],
|
|
)
|
|
|
|
const messageId = randomUUID()
|
|
await getPool().query(
|
|
`INSERT INTO public.whatsapp_messages
|
|
(id, direction, wamid, sender_phone_hash, phone_link_id, conversation_id,
|
|
message_type, body_text, raw_payload, processing_status)
|
|
VALUES ($1, 'inbound', $2, $3, $4, $5, 'text', 'lunch med Anna',
|
|
'{"from": "46701234567", "type": "text"}'::jsonb, 'done')`,
|
|
[messageId, `wamid.${randomUUID()}`, hash('sender'), linkId, conversationId],
|
|
)
|
|
|
|
const codeId = randomUUID()
|
|
await getPool().query(
|
|
`INSERT INTO public.whatsapp_link_codes (id, user_id, code_hash, expires_at)
|
|
VALUES ($1, $2, $3, now() + interval '10 minutes')`,
|
|
[codeId, userId, hash(randomUUID())],
|
|
)
|
|
|
|
return { userId, linkId, conversationId, messageId, codeId }
|
|
}
|
|
|
|
describe('anonymize_user_account: WhatsApp channel erasure (pg)', () => {
|
|
it('revokes and shreds the phone link so the number stops resolving', async () => {
|
|
const seeded = await seedLinkedUser()
|
|
|
|
await withUserContext(seeded.userId, async (client) => {
|
|
await client.query('SELECT public.anonymize_user_account($1)', [seeded.userId])
|
|
await client.query('RESET ROLE')
|
|
|
|
const { rows } = await client.query<{
|
|
revoked_at: string | null
|
|
phone_enc: string
|
|
phone_masked: string
|
|
wa_profile_name: string | null
|
|
default_company_id: string | null
|
|
last_company_id: string | null
|
|
}>(
|
|
`SELECT revoked_at, phone_enc, phone_masked, wa_profile_name,
|
|
default_company_id, last_company_id
|
|
FROM public.whatsapp_phone_links WHERE id = $1`,
|
|
[seeded.linkId],
|
|
)
|
|
expect(rows).toHaveLength(1)
|
|
// revoked_at is what lookupActiveLink filters on: a revoked link makes
|
|
// every further inbound message take the unknown-sender path, which
|
|
// persists no content at all.
|
|
expect(rows[0]!.revoked_at).not.toBeNull()
|
|
expect(rows[0]!.phone_enc).toBe('')
|
|
expect(rows[0]!.phone_masked).toBe('+** *** ** **')
|
|
expect(rows[0]!.wa_profile_name).toBeNull()
|
|
expect(rows[0]!.default_company_id).toBeNull()
|
|
expect(rows[0]!.last_company_id).toBeNull()
|
|
})
|
|
})
|
|
|
|
it('nulls body_text and raw_payload on every message of that link', async () => {
|
|
const seeded = await seedLinkedUser()
|
|
|
|
await withUserContext(seeded.userId, async (client) => {
|
|
await client.query('SELECT public.anonymize_user_account($1)', [seeded.userId])
|
|
await client.query('RESET ROLE')
|
|
|
|
const { rows } = await client.query<{
|
|
body_text: string | null
|
|
raw_payload: unknown
|
|
wamid: string | null
|
|
}>(
|
|
`SELECT body_text, raw_payload, wamid
|
|
FROM public.whatsapp_messages WHERE id = $1`,
|
|
[seeded.messageId],
|
|
)
|
|
expect(rows).toHaveLength(1)
|
|
expect(rows[0]!.body_text).toBeNull()
|
|
expect(rows[0]!.raw_payload).toBeNull()
|
|
// The skeleton survives: "a message existed" stays auditable.
|
|
expect(rows[0]!.wamid).not.toBeNull()
|
|
})
|
|
})
|
|
|
|
it('resets the conversation and deletes outstanding link codes', async () => {
|
|
const seeded = await seedLinkedUser()
|
|
|
|
await withUserContext(seeded.userId, async (client) => {
|
|
await client.query('SELECT public.anonymize_user_account($1)', [seeded.userId])
|
|
await client.query('RESET ROLE')
|
|
|
|
const conversation = await client.query<{
|
|
state: string
|
|
context: Record<string, unknown>
|
|
company_id: string | null
|
|
}>(
|
|
`SELECT state, context, company_id
|
|
FROM public.whatsapp_conversations WHERE id = $1`,
|
|
[seeded.conversationId],
|
|
)
|
|
expect(conversation.rows[0]!.state).toBe('idle')
|
|
expect(conversation.rows[0]!.context).toEqual({})
|
|
expect(conversation.rows[0]!.company_id).toBeNull()
|
|
|
|
const codes = await client.query<{ n: number }>(
|
|
`SELECT count(*)::int AS n FROM public.whatsapp_link_codes WHERE user_id = $1`,
|
|
[seeded.userId],
|
|
)
|
|
expect(codes.rows[0]!.n).toBe(0)
|
|
})
|
|
})
|
|
|
|
it('leaves another user WhatsApp data untouched', async () => {
|
|
const erased = await seedLinkedUser()
|
|
const bystander = await seedLinkedUser()
|
|
|
|
await withUserContext(erased.userId, async (client) => {
|
|
await client.query('SELECT public.anonymize_user_account($1)', [erased.userId])
|
|
await client.query('RESET ROLE')
|
|
|
|
const { rows } = await client.query<{ revoked_at: string | null; phone_enc: string }>(
|
|
`SELECT revoked_at, phone_enc FROM public.whatsapp_phone_links WHERE id = $1`,
|
|
[bystander.linkId],
|
|
)
|
|
expect(rows[0]!.revoked_at).toBeNull()
|
|
expect(rows[0]!.phone_enc).toBe('deadbeefcafe')
|
|
|
|
const message = await client.query<{ body_text: string | null }>(
|
|
`SELECT body_text FROM public.whatsapp_messages WHERE id = $1`,
|
|
[bystander.messageId],
|
|
)
|
|
expect(message.rows[0]!.body_text).toBe('lunch med Anna')
|
|
})
|
|
})
|
|
})
|