LifeMate — Personal, Family & Learning Companion
feat/flutter-mobile-navigation
chore/phase1-repository-cleanup
deploy/stable-web-apk
deployment-static
design/mobile-accessibility-v2
design/mobile-navigation-review
docs/child-education-access-decision
docs/offline-data-rescue-options
docs/owner-guide-and-delivery-plan
docs/phase1-policy-options
docs/public-review-live-2026-10-15
feat/aparat-learning-library
feat/child-device-safety
feat/child-identity-schema
feat/flutter-mobile-navigation
feat/learning-content-operations
feat/local-signing-preflight
feat/online-family-stage0
feat/personal-encrypted-recovery
feat/private-planner-series-api
feat/public-review-page
feat/standalone-weekly-planner
feat/weekly-planner-android
final-feature-1/iran-family-school
fix/auth-abuse-expiry-safety
fix/auth-expiry-after-lock
fix/backend-request-safety
fix/lifeguide-bootstrap-naming-ci
fix/lifeguide-pages-qa-base-preflight
fix/lifeguide-qa-pages-env-deployment
fix/local-task-persistence
fix/managed-child-session-boundary
fix/personal-jalali-dates
fix/personal-notification-controls
fix/personal-upgrade-delivery-guard
fix/sms-approval-gates
fix/study-duration-display
infra/cloudflare-neon-migration
infra/lifeguide-cloudflare-qa
infra/lifeguide-github-pages-qa
infra/lifeguide-supabase-qa
main
phase-1/foundation-completion
phase-2/product-foundation-identity
phase-3/planner-school-family-core
phase-4/ai-learning-wellbeing
phase-5/hardening-observability-release
test/android-reminder-acceptance
test/personal-notification-settings
test/personal-recovery-acceptance
test/role-entry-apk
| 1 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { createEmailProvider, createSmsProvider, safelyDeliver } from '../src/auth_delivery.js'; |
| 4 | |
| 5 | const secret = 'SYNTHETIC_SMS_SECRET_MARKER_20261008'; |
| 6 | const endpoint = 'https://sms-gateway.example.test/otp'; |
| 7 | const env = { SMS_PROVIDER: 'webhook', SMS_LIVE_ENABLED: 'true', SMS_PROVIDER_URL: endpoint, SMS_PROVIDER_TOKEN: secret }; |
| 8 | const message = { phone: '+155500000001', code: '765432', expiresIn: 300, idempotencyKey: '11111111-1111-4111-8111-111111111111' }; |
| 9 | const sensitive = `${secret} ${message.phone} ${message.code}`; |
| 10 | const safeError = (error, code) => { |
| 11 | assert.equal(error.message, code); |
| 12 | assert.equal(String(error.stack).includes(secret), false); |
| 13 | assert.equal(String(error.stack).includes(message.phone), false); |
| 14 | assert.equal(String(error.stack).includes(message.code), false); |
| 15 | assert.equal(error.cause, undefined); |
| 16 | return true; |
| 17 | }; |
| 18 | |
| 19 | test('SMS remains disabled without an explicit webhook selection and makes no outbound request', async () => { |
| 20 | let calls = 0; |
| 21 | for (const selection of [undefined, '', 'none', 'disabled']) { |
| 22 | const provider = createSmsProvider({ env: { ...env, SMS_PROVIDER: selection }, fetchImpl: async () => { calls++; } }); |
| 23 | assert.equal(provider.configured, false); |
| 24 | assert.equal(provider.status, 'UNVERIFIED'); |
| 25 | assert.equal((await provider.sendOtp(message)).delivered, false); |
| 26 | } |
| 27 | assert.equal(calls, 0); |
| 28 | }); |
| 29 | |
| 30 | test('explicit HTTPS webhook uses bearer authentication, minimal OTP data and an opaque idempotency key', async () => { |
| 31 | const calls = []; |
| 32 | const provider = createSmsProvider({ env, fetchImpl: async (url, options) => { calls.push({ url, options }); return new Response('', { status: 202 }); } }); |
| 33 | assert.equal(provider.configured, true); |
| 34 | assert.equal(provider.status, 'CONFIGURED_UNVERIFIED'); |
| 35 | assert.equal(JSON.stringify(provider).includes(secret) || JSON.stringify(provider).includes(endpoint), false); |
| 36 | assert.equal((await provider.sendOtp({ ...message, privateNotes: 'MUST-NOT-SEND' })).delivered, true); |
| 37 | assert.equal(calls.length, 1); |
| 38 | const { url, options } = calls[0]; |
| 39 | assert.equal(url === endpoint, true); |
| 40 | assert.equal(options.method, 'POST'); |
| 41 | assert.equal(options.redirect, 'error'); |
| 42 | assert.equal(options.headers.Authorization === `Bearer ${secret}`, true); |
| 43 | assert.equal(options.headers['Idempotency-Key'] === message.idempotencyKey, true); |
| 44 | assert.equal(options.headers['Content-Type'], 'application/json'); |
| 45 | assert.ok(options.signal instanceof AbortSignal); |
| 46 | const payload = JSON.parse(options.body); |
| 47 | assert.deepEqual(Object.keys(payload).sort(), ['code', 'expiresIn', 'phone']); |
| 48 | assert.equal(payload.phone === message.phone && payload.code === message.code && payload.expiresIn === 300, true); |
| 49 | }); |
| 50 | |
| 51 | test('unsupported or unsafe explicit SMS configuration fails clearly without echoing configuration secrets', () => { |
| 52 | let calls = 0; |
| 53 | const bad = [ |
| 54 | { SMS_PROVIDER: 'unknown' }, { SMS_PROVIDER_URL: '' }, { SMS_PROVIDER_URL: 'http://sms-gateway.example.test/otp' }, |
| 55 | { SMS_PROVIDER_URL: `https://user:${secret}@sms-gateway.example.test/otp` }, |
| 56 | { SMS_PROVIDER_URL: `https://sms-gateway.example.test/otp#${secret}` }, |
| 57 | { SMS_PROVIDER_URL: sensitive }, { SMS_PROVIDER_TOKEN: '' }, { SMS_PROVIDER_TOKEN: 'short' }, |
| 58 | { SMS_PROVIDER_TOKEN: secret + '\n' }, |
| 59 | ]; |
| 60 | for (const config of bad) assert.throws(() => createSmsProvider({ env: { ...env, ...config }, fetchImpl: async () => { calls++; } }), (error) => { |
| 61 | assert.match(error.message, /^sms_provider_(unsupported|url_invalid|token_invalid)$/); |
| 62 | assert.equal(String(error.stack).includes(secret), false); |
| 63 | return true; |
| 64 | }); |
| 65 | assert.equal(calls, 0); |
| 66 | }); |
| 67 | |
| 68 | test('webhook rejection exposes only a sanitized error and retries only transient response statuses', async () => { |
| 69 | for (const status of [302, 400, 401, 429, 503]) { |
| 70 | let bodyRead = false; |
| 71 | const provider = createSmsProvider({ env, fetchImpl: async () => ({ ok: false, status, async text() { bodyRead = true; return sensitive; }, async json() { bodyRead = true; return { error: sensitive }; } }) }); |
| 72 | await assert.rejects(provider.sendOtp(message), (error) => { |
| 73 | safeError(error, 'sms_provider_rejected'); |
| 74 | assert.equal(error.retryable, status === 429 || status >= 500); |
| 75 | return true; |
| 76 | }); |
| 77 | assert.equal(bodyRead, false); |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | test('webhook network exceptions cannot expose provider URL, OTP or authentication material', async () => { |
| 82 | const provider = createSmsProvider({ env, fetchImpl: async () => { throw new Error(sensitive); } }); |
| 83 | await assert.rejects(provider.sendOtp(message), (error) => { |
| 84 | safeError(error, 'sms_provider_unavailable'); |
| 85 | assert.equal(error.retryable, false); |
| 86 | return true; |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | test('webhook timeout actually aborts its transport and returns a sanitized uncertain-delivery error', async () => { |
| 91 | let aborted = false; |
| 92 | const provider = createSmsProvider({ env, timeoutMs: 20, fetchImpl: async (_url, { signal }) => new Promise((_resolve, reject) => { |
| 93 | const keepAlive = setTimeout(() => reject(new Error('Synthetic transport ignored abort')), 1000); |
| 94 | signal.addEventListener('abort', () => { aborted = true; clearTimeout(keepAlive); reject(new Error(sensitive)); }, { once: true }); |
| 95 | }) }); |
| 96 | await assert.rejects(provider.sendOtp(message), (error) => { |
| 97 | safeError(error, 'sms_provider_timeout'); |
| 98 | assert.equal(error.retryable, false); |
| 99 | return true; |
| 100 | }); |
| 101 | assert.equal(aborted, true); |
| 102 | }); |
| 103 | |
| 104 | test('webhook retries preserve one auth-delivery idempotency key and production logs contain metadata only', async () => { |
| 105 | const keys = [], logs = []; |
| 106 | const provider = createSmsProvider({ env, fetchImpl: async (_url, options) => { |
| 107 | keys.push(options.headers['Idempotency-Key']); |
| 108 | return new Response(sensitive, { status: keys.length < 3 ? 503 : 202 }); |
| 109 | } }); |
| 110 | const originalLog = console.log; |
| 111 | let result; |
| 112 | try { |
| 113 | console.log = (line) => { logs.push(String(line)); }; |
| 114 | result = await safelyDeliver({ configured: provider.configured, send: (payload) => provider.sendOtp(payload) }, message, { attemptId: message.idempotencyKey, env: { NODE_ENV: 'production' } }); |
| 115 | } finally { console.log = originalLog; } |
| 116 | assert.equal(result.delivered, true); |
| 117 | assert.equal(keys.length, 3); |
| 118 | assert.equal(keys.every((key) => key === message.idempotencyKey), true); |
| 119 | assert.equal(logs.length, 1); |
| 120 | const event = JSON.parse(logs[0]); |
| 121 | assert.deepEqual(Object.keys(event).sort(), ['attemptId', 'status', 'type']); |
| 122 | assert.equal(event.status, 'accepted'); |
| 123 | assert.equal(event.type, 'auth_delivery'); |
| 124 | assert.equal(logs[0].includes(secret) || logs[0].includes(message.phone) || logs[0].includes(message.code), false); |
| 125 | }); |
| 126 | |
| 127 | test('test runner disables inherited external delivery configuration for default providers', () => { |
| 128 | assert.equal(createSmsProvider().configured, false); |
| 129 | assert.equal(createEmailProvider().configured, false); |
| 130 | }); |