diff --git a/api/Dockerfile b/api/Dockerfile index 0dd6072..d2d65a9 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -18,7 +18,7 @@ LABEL maintainer="Duarte Santos https://gitlab.com/DuarteSantos8" \ WORKDIR /app COPY package.json package-lock.json* ./ RUN npm install --omit=dev && npm cache clean --force -COPY server.js push-messages.js ./ +COPY server.js push-messages.js verify-error.js ./ ENV NODE_ENV=production ENV PORT=3000 diff --git a/api/server.js b/api/server.js index 67faa2e..cee77c1 100644 --- a/api/server.js +++ b/api/server.js @@ -12,6 +12,7 @@ import { } from '@simplewebauthn/server'; import webpush from 'web-push'; import { dayReminderPush, restTimerPush, testPush } from './push-messages.js'; +import { verifyError } from './verify-error.js'; const PORT = +(process.env.PORT || 3000); const DATA = process.env.DATA_DIR || '/data'; @@ -574,7 +575,7 @@ const routes = { } catch (e) { // e.message can echo attacker-supplied response fields, so only the reason code is kept. audit(req, 'auth.register.fail', { ok: false, name: c.name, msg: 'verify-error' }); - return json(res, 400, { error: 'verification failed: ' + e.message }); + return json(res, 400, { error: verifyError(e, { rpId: RP_ID, origin: ORIGIN }) }); } if (!verification.verified) { audit(req, 'auth.register.fail', { ok: false, name: c.name, msg: 'not-verified' }); @@ -648,7 +649,7 @@ const routes = { }); } catch (e) { audit(req, 'auth.login.fail', { ok: false, user: db.users.find(u => u.id === cred.userId), uid: cred.userId, msg: 'verify-error' }); - return json(res, 400, { error: 'verification failed: ' + e.message }); + return json(res, 400, { error: verifyError(e, { rpId: RP_ID, origin: ORIGIN }) }); } if (!verification.verified) { audit(req, 'auth.login.fail', { ok: false, user: db.users.find(u => u.id === cred.userId), uid: cred.userId, msg: 'not-verified' }); diff --git a/api/verify-error.js b/api/verify-error.js new file mode 100644 index 0000000..9eb5694 --- /dev/null +++ b/api/verify-error.js @@ -0,0 +1,12 @@ +// @simplewebauthn reports an RP_ID or origin mismatch as an opaque "Unexpected ..." string, and +// whoever reads it in the browser has no way to connect that to their .env. A misconfigured +// RP_ID/ORIGIN is the single most common self-hosting failure (see docs/SELF_HOSTING.md), so +// name both values back. Neither is a secret: one is the address bar, the other its hostname. +const MISCONFIGURED = /unexpected (rp id|authentication response origin|registration response origin)/i; + +export function verifyError(e, { rpId, origin } = {}) { + const msg = String((e && e.message) || 'verification failed'); + if (!MISCONFIGURED.test(msg)) return 'verification failed: ' + msg; + return `${msg} — this server is configured with RP_ID=${rpId} and ORIGIN=${origin}, and both ` + + 'must match the address you opened. See docs/SELF_HOSTING.md.'; +} diff --git a/api/verify-error.test.js b/api/verify-error.test.js new file mode 100644 index 0000000..928d17c --- /dev/null +++ b/api/verify-error.test.js @@ -0,0 +1,29 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { verifyError } from './verify-error.js'; + +const cfg = { rpId: 'gym.example.com', origin: 'https://gym.example.com' }; + +test('names the configured values when the RP ID does not match', () => { + const out = verifyError(new Error('Unexpected RP ID hash'), cfg); + assert.match(out, /RP_ID=gym\.example\.com/); + assert.match(out, /ORIGIN=https:\/\/gym\.example\.com/); + assert.match(out, /SELF_HOSTING\.md/); +}); + +test('does the same for an origin mismatch, on login and on registration', () => { + assert.match(verifyError(new Error('Unexpected authentication response origin'), cfg), /must match the address you opened/); + assert.match(verifyError(new Error('Unexpected registration response origin'), cfg), /must match the address you opened/); +}); + +test('leaves an unrelated verification failure alone', () => { + assert.equal( + verifyError(new Error('Signature verification failed'), cfg), + 'verification failed: Signature verification failed' + ); +}); + +test('does not throw on a missing or empty error', () => { + assert.equal(verifyError(undefined, cfg), 'verification failed: verification failed'); + assert.equal(verifyError(new Error(''), cfg), 'verification failed: verification failed'); +}); diff --git a/docs/SELF_HOSTING.md b/docs/SELF_HOSTING.md index 5cd98fd..6fc0abb 100644 --- a/docs/SELF_HOSTING.md +++ b/docs/SELF_HOSTING.md @@ -232,12 +232,59 @@ docker compose up -d --build The app shell is versioned (`?v=N`) so clients pick up changes on next load. Your `./data` and the downloaded media are untouched. +## Passkeys fail even though `RP_ID` looks right + +The most common support question, and the values are usually *nearly* correct. Work through +these in order — the first two account for most of it. + +**1. Ask the server what it actually loaded.** It prints both values on startup: + +``` +docker compose logs api | grep 'gym-api on' +# gym-api on :3000 (rpID=gym.example.com, origin=https://gym.example.com) +``` + +If that disagrees with your `.env`, the container is still running the old environment. +`docker compose restart` does **not** re-read `.env` — use `docker compose up -d`. + +**2. Check the exact shape of each value.** They are not the same kind of string: + +| | Correct | Wrong | +|---|---|---| +| `RP_ID` | `gym.example.com` | `https://gym.example.com`, `gym.example.com:8080`, `gym.example.com/` | +| `ORIGIN` | `https://gym.example.com` | `gym.example.com`, `https://gym.example.com/` | + +`RP_ID` is a bare hostname: no scheme, no port, no trailing slash. `ORIGIN` is the full origin +*with* the scheme and *without* a trailing slash. Both must match your address bar exactly. + +**3. Behind a tunnel or reverse proxy, use the public hostname.** With Cloudflare Tunnel, +Traefik, nginx or Caddy in front, the browser only ever sees the public name — so that is what +both values must be. Not the container name, not the LAN IP, not the internal port: + +```env +RP_ID=gym.example.com +ORIGIN=https://gym.example.com +``` + +The tunnel's own route may point wherever it likes (`http://localhost:8080` is fine). It is the +browser-facing name that has to appear here. + +**4. `www.` is a different host.** A passkey registered on `gym.example.com` will not work on +`www.gym.example.com`. Pick one and redirect the other. + +**5. Changing the hostname invalidates existing passkeys.** They were bound to the old one, so +everybody registers again — which is why it pays to settle the domain before others join. + +> On a LAN without certificates there is nothing to configure that makes passkeys work over +> plain `http://192.168.x.x`: browsers only allow WebAuthn on HTTPS (or `localhost`). Use guest +> mode, the standalone mobile app (`docs/MOBILE.md`), or put a certificate in front of it. + ## Troubleshooting | Symptom | Fix | |---|---| | No passkey prompt on my phone | You're on `http://` or an IP, not HTTPS. Set up a domain (section 3). | -| "verification failed" on login | `RP_ID`/`ORIGIN` don't match the URL in the address bar. Make them exact, restart. | +| "verification failed" on login | `RP_ID`/`ORIGIN` don't match the URL in the address bar. See the section above — start with what the server logged on startup. | | Media didn't download | `docker compose logs media`. Re-run `docker compose up -d`, or run `./scripts/fetch-media.sh`. | | Port 8080 already used | Set `WEB_PORT=9090` in `.env` (and update `ORIGIN` for local testing). | | No "Notifications" option in Settings | Requires a signed-in profile and HTTPS (or `localhost`) — guest mode and plain HTTP over LAN can't subscribe. |