@davoodmehraban89 / LifeMate / commits / 3e12f37
Fork 0

LifeMate — Personal, Family & Learning Companion

fix(auth): require explicit live SMS and template approval gates

Quinn White committed Oct 15, 2026 at 22:18 UTC 3e12f376c96936a77782a96dd59ecfefb3503bb4
6 files changed +44 -3
.env.example
+4
@@ -36,6 +36,10 @@ SMTP_SECURE=false
36 SMTP_USER=
37 SMTP_PASSWORD=
38 SMS_PROVIDER=disabled
39 +# These are manual approval gates, not evidence that approval or delivery happened.
40 +# Keep false until the owner authorizes live SMS and Kavenegar approves the template.
41 +SMS_LIVE_ENABLED=false
42 +KAVENEGAR_TEMPLATE_APPROVED=false
43 # For an approved Kavenegar VerifyLookup template, opt in with SMS_PROVIDER=kavenegar.
44 # Enter the secret API key in server-side environment / secret manager ONLY.
45 # Never put it in the public GitHub repo, Flutter, PWA or logs.
backend/src/auth_delivery.js
+10 -1
@@ -51,8 +51,17 @@ class WebhookSmsProvider extends SmsProvider {
51 export function createSmsProvider({ env = process.env, fetchImpl = fetch, timeoutMs = 10000 } = {}) {
52 const selection = String(env.SMS_PROVIDER ?? '').trim().toLowerCase();
53 if (['', 'none', 'disabled'].includes(selection)) return new SmsProvider();
54 + if (!['kavenegar', 'webhook'].includes(selection)) throw new Error('sms_provider_unsupported');
55 + // Credentials are configuration, not permission to send a paid real message.
56 + // Development tests inject a local capturing adapter; no OTP is logged or
57 + // returned by a production debug hook. These flags are never set by the app.
58 + if (env.SMS_LIVE_ENABLED !== 'true' ||
59 + (selection === 'kavenegar' && env.KAVENEGAR_TEMPLATE_APPROVED !== 'true')) {
60 + const disabled = new SmsProvider();
61 + disabled.status = 'APPROVAL_REQUIRED';
62 + return disabled;
63 + }
64 if (selection === 'kavenegar') return createKavenegarSmsProvider({ env, fetchImpl, timeoutMs });
55 - if (selection !== 'webhook') throw new Error('sms_provider_unsupported');
65 let url;
66 try { url = new URL(env.SMS_PROVIDER_URL); } catch { throw new Error('sms_provider_url_invalid'); }
67 if (url.protocol !== 'https:' || !url.hostname || url.username || url.password || url.hash) throw new Error('sms_provider_url_invalid');
backend/tests/kavenegar_sms.test.js
+2 -1
@@ -5,7 +5,8 @@ import { createSmsProvider } from '../src/auth_delivery.js';
5
6 const apiKey = 'SyntheticKavenegarApiKey123456789';
7 const template = 'lifeguideotp';
8 -const env = { SMS_PROVIDER: 'kavenegar', KAVENEGAR_API_KEY: apiKey, KAVENEGAR_TEMPLATE: template };
8 +// Fixture approval only: fetch is always injected; these are not live settings.
9 +const env = { SMS_PROVIDER: 'kavenegar', SMS_LIVE_ENABLED: 'true', KAVENEGAR_TEMPLATE_APPROVED: 'true', KAVENEGAR_API_KEY: apiKey, KAVENEGAR_TEMPLATE: template };
10 const request = { phone: '+989121234567', code: '082735', expiresIn: 300,
11 idempotencyKey: '11111111-2222-4333-8444-555555555555' };
12 const ok = () => new Response(JSON.stringify({
backend/tests/sms_approval_gate.test.js new
+25
@@ -0,0 +1,25 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { createSmsProvider } from '../src/auth_delivery.js';
4 +
5 +const config = { NODE_ENV: 'production', SMS_PROVIDER: 'kavenegar',
6 + KAVENEGAR_API_KEY: 'SyntheticApprovalFixtureNotALiveKey2026', KAVENEGAR_TEMPLATE: 'lifeguideotp' };
7 +test('configured credentials cannot enable real SMS before both explicit live and template approval flags', () => {
8 + for (const flags of [{}, { SMS_LIVE_ENABLED: 'true' }, { KAVENEGAR_TEMPLATE_APPROVED: 'true' },
9 + { SMS_LIVE_ENABLED: 'true', KAVENEGAR_TEMPLATE_APPROVED: 'TRUE' }]) {
10 + let calls = 0;
11 + const provider = createSmsProvider({ env: { ...config, ...flags }, fetchImpl: async () => { calls++; assert.fail('No network allowed'); } });
12 + assert.equal(provider.configured, false);
13 + assert.equal(provider.status, 'APPROVAL_REQUIRED');
14 + assert.equal(calls, 0);
15 + }
16 +});
17 +test('nonproduction network providers also remain gated; development uses injected test-only adapters', () => {
18 + assert.equal(createSmsProvider({ env: { ...config, NODE_ENV: 'development' } }).configured, false);
19 +});
20 +test('the two explicit flags permit adapter construction but never send a message by themselves', () => {
21 + let calls = 0;
22 + const provider = createSmsProvider({ env: { ...config, SMS_LIVE_ENABLED: 'true', KAVENEGAR_TEMPLATE_APPROVED: 'true' }, fetchImpl: async () => { calls++; } });
23 + assert.equal(provider.configured, true);
24 + assert.equal(calls, 0);
25 +});
backend/tests/sms_provider.test.js
+1 -1
@@ -4,7 +4,7 @@ import { createEmailProvider, createSmsProvider, safelyDeliver } from '../src/au
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_PROVIDER_URL: endpoint, SMS_PROVIDER_TOKEN: secret };
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) => {
docs/operations/KAVENEGAR_SMS_SETUP.md
+2
@@ -39,6 +39,8 @@ Official API documentation:
39 - `backend/src/auth_delivery.js` retains the original disabled/default and HTTPS webhook behavior; it is **not** switched on by default.
40 - Example environment keys:
41 - `SMS_PROVIDER=kavenegar`
42 + - `SMS_LIVE_ENABLED=true` only after explicit owner authorization for live sending.
43 + - `KAVENEGAR_TEMPLATE_APPROVED=true` only after the provider approves this exact template.
44 - `KAVENEGAR_TEMPLATE=lifeguideotp`
45 - `KAVENEGAR_API_KEY=<secret value in server/secret manager ONLY>`
46 - Adapter restricts to Iranian mobile numbers normalized by existing LifeGuide `normalizePhone()` as `+989XXXXXXXXX`; Kavenegar receives `09XXXXXXXXX`.