Tenant isolation
Queries are scoped to a company id taken from the signed session, never from anything the browser sends. Cache keys carry it too, so one brand’s figures are not served to another’s screen. A static sweep looks for missing company filters, and the API tests run a second tenant beside the first to check nothing crosses.
test/api/tenant-isolation.test.mjs · scripts/tenant-subquery-sweep.mjsSigned sessions (RS256)
Access tokens are signed with an RSA key pair and last 15 minutes. Production refuses to serve if the key pair is missing, so there is no weaker fallback. The algorithm is fixed on our side; the token header cannot choose it. Refresh tokens can be revoked per session.
src/lib/jwt.ts · src/lib/security.tsPassword and second factor
Passwords are hashed with PBKDF2-HMAC-SHA256 and a per-record salt, at 100,000 iterations, the most the hosting platform allows. Sensitive actions (such as paying a supplier bill, approving payroll or closing a period) ask for a fresh authenticator code.
src/lib/crypto.ts · src/lib/totp.tsSigned webhooks
Delivery-app and WhatsApp webhooks are verified by an HMAC over the raw request body before anything is parsed. A bad signature gets a bare refusal with no detail. Delivery-app webhooks that carry a timestamp more than five minutes off are also refused as replays.
src/routes/aggregators.ts · src/routes/whatsapp.tsRate limiting
Sign-in, token refresh and the demo form are counted exactly by a Durable Object, per address, and fail closed: if the counter cannot be reached, the attempt is refused. Till and screen traffic has its own, higher allowance.
src/lib/ratelimit.ts · docs/decisions/0010-rate-limiting.mdCorrections are reversals
One module writes the ledger. It inserts entries, and a mistake is corrected by a reversing entry that sits beside the original, which is marked reversed. The cash ledger is also protected by database triggers that refuse an UPDATE or a DELETE.
src/lib/accounting.ts:250-355 · migrations/0004_service_floor.sqlMoney is calculated in whole fils
Amounts are held in dirhams, but a sale and its journal are worked out in whole fils, not floating point. A sale is split into net, VAT and total so the parts always add up to the whole, and an entry that does not balance is refused.
src/lib/money.ts · src/lib/sale-journal.tsRoles and separation of duties
Access is role-based, with permissions checked per route. Some pairs of actions cannot be done by one person, such as raising and approving the same supplier bill.
src/lib/rbac.ts · migrations/0042_ap_bill_separation_of_duties.sqlAudit trail
Changes to records are written to an audit log with who, what and when. Sensitive fields, such as bank details and secrets, are masked by name before they are stored.
src/lib/audit.ts · src/lib/audit-redact.tsBrowser hardening
Responses carry a content security policy, HSTS in production, no-framing, no-sniffing and no-referrer headers. The access token is held in the page’s memory, not in browser storage.
src/lib/security.ts · src/lib/middleware.tsRecovery
The database supports point-in-time restore through Cloudflare D1 Time Travel, and a restore has a written runbook. The restore window depends on the hosting plan.
docs/runbooks/backup-and-restore.md