# Security — QS Simpro App

Avi's rule, in his words: **"Fort Knox."**

## Approval rules

- Nothing goes live, is published, deleted, shared or spent without
  Avi's approval.
- **Read-only first.** No edits or deletes in Simpro without Avi saying so,
  in plain words, for that specific change. Work so far: `GET` only.
- No passwords, API keys or secrets in chat, in this repo, or in any
  repository — code, docs, commit messages, screenshots, anywhere.
- Logins go through the secure vault, never pasted in chat.

## Secure logins — office and mobile

Design only in this build. **There is no fake login and no password
stored anywhere in this app or repo.** The "Sign in with Simpro" button
opens an honest sheet that says sign-in is not connected yet. Do not
add a password box, a demo account, or a local login to work around it.

Live login flow (to be built in the Cloudflare Worker, with Avi's approval):

1. One account per person. Ralph, Sama, Sruli, Avi and each engineer
   (Cameron, Daniel, Gabi, Mason, Jason) sign in with their own Simpro
   account. No shared passwords, no shared logins, no generic "office"
   account.
2. Staff sign in with **Simpro OAuth 2.0 Authorisation Code flow**.
   The browser goes to Simpro and the person signs in there, with
   their Simpro account and Google Authenticator 2FA. The app never
   sees or stores their Simpro password.
3. Simpro returns an authorisation code to the Cloudflare Worker
   (the QS Connector). The Worker swaps it for an access token and
   keeps the token server-side. The browser gets only a short-lived,
   HttpOnly, Secure, SameSite session cookie.
4. The Worker checks, on every request, which companies that person
   may see in Simpro — QS Plumbing (ID 2) and/or QS Certs (ID 35) —
   and returns only that company's data. A QS Plumbing engineer can
   never be served QS Certs data, and vice versa.
5. Sessions are short-lived and expire; re-sign-in is required.
   "Sign out" deletes the session in the Worker as well as the cookie.
   There is no "remember me forever" and no token in localStorage.
6. Background work with no person signed in uses OAuth **Client
   Credentials** in the Worker only — never in the browser.
7. The Simpro API key is a Worker secret (`wrangler secret put
   SIMPRO_API_KEY`), never in code, never in the browser, never in
   a screenshot or chat.

## Hardening in this build

- **No secrets in repo or client code.** Checked by
  `npm run check:secrets` and by a manual scan on every change.
  The API key lives only in the secure vault / Worker secrets.
- **Cloudflare security headers** in `_headers`: `nosniff`,
  `SAMEORIGIN` framing, strict referrer policy, camera / microphone /
  geolocation denied. The service worker is served `no-cache` and
  the manifest as `application/manifest+json`.
- **XSS:** all data rendered into the page goes through `esc()` in
  `assets/js/app.js` (customer names, job titles, notes, contractor
  names). User-typed notes and the new-customer name are escaped
  before they are shown, and never inserted as raw HTML. The only
  `innerHTML` writes use fixed strings from this repo or escaped data.
- **No sensitive data in the client:** this build shows example data
  only. Real customer names, addresses, phone numbers and job details
  are never in the repo, never cached by the service worker, and
  never sent to the browser until sign-in exists. The service worker
  caches the app shell only and never caches `/api/` calls.
- **Input validation:** notes are plain text (textarea), photo uploads
  accept images only (`accept="image/*"`), the new-customer form runs
  a duplicate check and rejects empty names, and search / filter
  inputs are treated as text, never as code.
- **CSRF:** this build makes no writes to any API at all — every
  button that looks like a write (Complete Job, Add Customer) only
  changes local demo state on screen. When live writes are built,
  they will go through the Worker with a same-site session cookie
  and an origin check, not straight from the browser to Simpro.
- **Least privilege:** Simpro API tokens are not scoped to specific
  calls, so the Worker is the only caller and must enforce read-only
  mode until writes are approved, and return only the fields each
  screen needs. Staff see only their assigned company.
- **Rate limiting:** the Worker must queue / batch requests under
  Simpro's 10 requests per second per build limit (HTTP 429 above it).
- **Audit:** this is hardening, not a guarantee. Nothing here claims
  the app is unhackable. Before go-live, re-audit: re-run the secret
  scan, check headers on the live domain, test sign-in / sign-out /
  expiry, and test that a QS Plumbing user cannot fetch QS Certs data.

## Secrets

| Secret | Where it lives | Where it never lives |
| --- | --- | --- |
| Simpro live API key (qsplumbing build) | Secure vault (`custom.simpro` connector); later, Cloudflare Worker secrets | This repo, chat, email, screenshots |
| Simpro test API key (simpro-uk-avia build, when issued) | Secure vault, separate entry | This repo, chat |
| Simpro staff logins | Simpro, with Google Authenticator 2FA | This repo, chat |

Simpro API keys never expire, so treat one like a password. If a key is
ever pasted, photographed or committed by mistake, tell Avi at once,
revoke it in Simpro (System > Setup > API > Applications) and issue a
new one.

## Existing API apps — check before adding a third

The live build already has two Enabled API applications, build-wide
(verified 26 November 2026, look-only, nothing registered or changed):

- **Line** — company "Simpro", grant type API Key, email blank.
  Purpose unknown; not identified in public search.
- **viaSocket** — company "viaSocket", grant type Authorisation Code,
  support@viasocket.com. An automation platform that can read and write
  Simpro data, depending on its workflows — which workflows QS runs is
  internal to QS / viaSocket and was not verified.

Before adding a third API app, identify what those two do. Do not disable
anything on the basis of this note alone.

## Data handling

- Example data in this repo is made up. It is labelled as example in the
  UI and in the code. Never present it as real customers, jobs or money.
- Real customer names, addresses, phone numbers and job details must not
  be published on a public demo link. The live app at
  app.qsplumbing.co.uk will need staff sign-in before it shows real data.
- The app keeps no copy of Simpro data. Simpro is the system of record.
- API access is full access in Simpro — tokens are not scoped to specific
  calls — so the Worker (see `ARCHITECTURE.md`) must be the only caller,
  and must enforce read-only mode until writes are approved.

## If something goes wrong

- A bad deploy: roll back in Cloudflare Pages (see `DEPLOYMENT.md`).
- A leaked key: revoke it in Simpro, issue a new one, update the vault
  and the Worker secret, tell Avi.
- Simpro itself is untouched by this app until a write is approved.
  If in doubt, stop, read-only, and ask Avi.
