Files
crosscode-platform/apps/accounts/backend/tests/lib.test.ts
T
joshuaandClaude Opus 5.5 fd8f6c28b0 Refuse a TRUST_PROXY the apps can't start with; Accounts reads loopback
A server configuration with TRUST_PROXY=TRUE made an Events install build for
minutes, then fail: Express threw "invalid IP address: TRUE" at startup.

- scripts/lib/envtpl.cjs: VALUE_RULES checked before any .env is written
  (exit 4, naming the value and the fix), for every release.
- Contracts 1.6.0: agent.checkEnvValue / agent.invalidValues, the same rules
  for the Hub.
- Accounts 1.0.1: TRUST_PROXY read as Events reads it; loopback used to
  turn proxy trust off.
- Docs: CHANGELOGs, WHATS-NEW, HANDOVER, AGENT-1.0-PLAN, ROLLOUT-1.0, READMEs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 10:23:49 +02:00

119 lines
5.7 KiB
TypeScript

// The small pieces: passwords (including bcrypt hashes Cross Events 3.0 will bring across), codes,
// recovery codes, secrets at rest, licence states, the church profile rules.
import assert from 'node:assert/strict';
import { describe, test } from 'node:test';
import bcrypt from 'bcryptjs';
import { hashPassword, needsRehash, passwordProblem, verifyPassword } from '../src/lib/passwords.js';
import { codeAt, generateSecret, matchStep, otpauthUrl } from '../src/lib/totp.js';
import { hashRecoveryCode, newRecoveryCodes, normaliseRecoveryCode } from '../src/lib/recoveryCodes.js';
import { Secrets } from '../src/lib/crypto.js';
import { stateFor, type LicenceClaims } from '../src/hub/licence.js';
import { normalisePhone } from '../src/accounts/service.js';
import { waNumber } from '../src/lib/messages.js';
import { loadConfig, trustProxyFrom } from '../src/lib/config.js';
describe('TRUST_PROXY', () => {
test('read as Events reads it: loopback behind frp is on, not off', () => {
assert.equal(trustProxyFrom('loopback', true), 'loopback');
assert.deepEqual(trustProxyFrom('loopback, 203.0.113.10', true), ['loopback', '203.0.113.10']);
assert.equal(trustProxyFrom('2', true), 2);
assert.equal(trustProxyFrom('TRUE', true), 1);
assert.equal(trustProxyFrom('true', false), 1);
assert.equal(trustProxyFrom('false', true), false);
assert.equal(trustProxyFrom('0', true), false);
assert.equal(trustProxyFrom(undefined, true), 1);
assert.equal(trustProxyFrom('', false), false);
});
test('oidc-provider trusts the proxy whenever Express does', () => {
const on = loadConfig({ NODE_ENV: 'test', TRUST_PROXY: 'loopback' });
assert.equal(on.trustProxy, true);
assert.equal(on.trustProxySetting, 'loopback');
const off = loadConfig({ NODE_ENV: 'test', TRUST_PROXY: 'false' });
assert.equal(off.trustProxy, false);
});
});
describe('passwords', () => {
test('Argon2id hashes verify, wrong passwords don\'t', async () => {
const h = await hashPassword('Correct-horse-1');
assert.match(h, /^\$argon2id\$/);
assert.equal(await verifyPassword('Correct-horse-1', h), true);
assert.equal(await verifyPassword('wrong-password', h), false);
assert.equal(needsRehash(h), false);
});
test('bcrypt hashes from Cross Events verify and are marked for re-hashing', async () => {
const h = await bcrypt.hash('Old-events-password', 4);
assert.equal(await verifyPassword('Old-events-password', h), true);
assert.equal(needsRehash(h), true);
assert.equal(await verifyPassword('anything', null), false);
assert.equal(await verifyPassword('anything', 'not-a-hash'), false);
});
test('at least 8 characters', () => {
assert.ok(passwordProblem('short'));
assert.equal(passwordProblem('long-enough'), null);
});
});
describe('authenticator codes', () => {
test('a code works once, with a step either side for clock drift', () => {
const secret = generateSecret();
const now = Date.now();
const step = Math.floor(now / 30000);
assert.equal(matchStep(secret, codeAt(secret, step), { now }), step);
assert.equal(matchStep(secret, codeAt(secret, step - 1), { now }), step - 1);
assert.equal(matchStep(secret, codeAt(secret, step - 3), { now }), null);
assert.equal(matchStep(secret, codeAt(secret, step), { now, afterStep: step }), null);
assert.match(otpauthUrl(secret, 'a@b.co', 'Hope Family'), /^otpauth:\/\/totp\/Hope%20Family%3Aa%40b\.co\?secret=/);
});
test('the same codes as Cross Events 2.3 for the same secret (RFC 6238 test vector)', () => {
// RFC 6238 SHA-1 secret "12345678901234567890" in base32, T = 59 s → 94287082 (last 6 digits).
assert.equal(codeAt('GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', 1), '287082');
});
});
describe('recovery codes', () => {
test('ten, readable, matched whatever the case and dashes', () => {
const { codes, hashes } = newRecoveryCodes();
assert.equal(codes.length, 10);
assert.match(codes[0]!, /^[a-z2-9]{4}-[a-z2-9]{4}-[a-z2-9]{4}$/);
assert.equal(hashRecoveryCode(codes[0]!.toUpperCase().replace(/-/g, ' ')), hashes[0]);
assert.equal(normaliseRecoveryCode('ABCD EFGH JKMN'), 'abcd-efgh-jkmn');
});
});
describe('secrets at rest', () => {
test('round trip; another key can\'t read it; changed data fails', () => {
const a = new Secrets('one-secret-that-is-long-enough-for-tests');
const enc = a.encrypt('totp-secret');
assert.match(enc, /^enc:/);
assert.equal(a.decrypt(enc), 'totp-secret');
assert.throws(() => new Secrets('another-secret-that-is-long-enough').decrypt(enc));
assert.throws(() => a.decrypt(`${enc.slice(0, -2)}00`));
});
});
describe('licence states (Accounts never goes read-only)', () => {
const base: LicenceClaims = { v: 1, installId: 'i', customer: 'Hope', status: 'active', licenceExpiresAt: null, validUntil: '2026-12-01T00:00:00Z', graceDays: 14 };
test('active, grace, lapsed, revoked, unlicensed', () => {
assert.equal(stateFor(base, 'i', new Date('2026-11-20')).state, 'active');
assert.equal(stateFor(base, 'i', new Date('2026-12-05')).state, 'grace');
assert.equal(stateFor(base, 'i', new Date('2027-01-01')).state, 'lapsed');
assert.equal(stateFor({ ...base, status: 'revoked' }, 'i').state, 'revoked');
assert.equal(stateFor(null, null).state, 'unlicensed');
assert.equal(stateFor(base, 'other').state, 'unlicensed');
});
});
describe('phone numbers', () => {
test('South African numbers in every common format', () => {
assert.equal(normalisePhone('082 123 4567'), '+27821234567');
assert.equal(normalisePhone('+27 82 123 4567'), '+27821234567');
assert.equal(normalisePhone('hello'), null);
assert.equal(waNumber('+27821234567'), '27821234567');
});
});