Compare commits

...
Author SHA1 Message Date
Hermes 1328cfda6b [glm-grade=B] fix(notifications): repair UI/API contract drift — SMTP auth, event gate, test button (DC-092)
Three user-facing notification features were silently dead from contract
drift between the settings UI and the backend:

1. SMTP auth never applied: the UI sent email.user/email.pass while the
   manager read username/password. Route now normalizes aliases; legacy
   config files canonicalize at load.
2. Event toggles were cosmetic: UI sent camelCase keys (containerDown),
   the send() gate read kebab-case ('container-down'). EVENT_ALIASES now
   folds every known spelling (manager gate, route store, legacy files);
   UI sends and reads canonical keys.
3. Deploy/auto-restart notifications always dropped: deploy-success/
   deploy-failed/auto-restart were missing from DEFAULT events, and
   send('test') was itself gated -> the Test button was a no-op. Defaults
   added; 'test' bypasses the gate.

Also: strict boolean contract (string 'false' for secure/enabled rejected
— previously coerced truthy, silently forcing TLS), SMTP port bounds,
non-destructive credential merge (blank password no longer clobbers the
stored one), GET /config returns port/secure/to/username + hasPassword
(password never returned), full form prefill + keep-hint placeholder.

Verified live pre-fix: send('test')/'deploymentSuccess'/'auto-restart'
all returned 'not enabled'. Post-fix: +20 tests (route + manager),
full suite 2641/2641 (112 suites).

Judge: GLM-5.3 cold read via delegate_task (deleg_ad765d63), grade B,
ship, zero blockers; judge independently ran the DC-092 suites (38/38).
Polish #1 (canonical event for provider titles) folded in. Remaining
emitters with the same gate-miss class (recipeRemoved, workflow,
ssl-cert-expiry, dns-propagation, drift-detected, dependency-restart-*)
noted for a follow-up tick.
2026-08-22 18:16:47 -07:00
Hermes df55677bd1 Merge DC-091: config-schema KNOWN_KEYS licenseBackup/_version fix [glm-grade=A]
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-22 17:22:20 -07:00
Hermes ddbea0a040 [glm-grade=A] fix(config): teach schema KNOWN_KEYS the licenseBackup/_version writer keys (DC-091)
Every startup logged two false-positive 'Unknown config key — possible typo?'
warns: licenseBackup (written by src/managers/license-manager.js:510 activation
persistence) and _version (stamped by src/config/migrations.js). Both are
first-party writers the validator was never taught about (DC-091).

- config-schema.js: add both keys to KNOWN_KEYS with a source-of-writes comment
- config-schema.test.js (new): 5 regression tests — live production config keyset
  validates with zero unknown-key warnings, writer keys never warn, genuine
  typos still warn (exact string), license/licenseBackup sync guard, _version
  recognized at every migration value

Verified: full jest suite 111 suites / 2621 tests green (baseline 110/2616).
Warns reproduced in live container logs 2026-08-22T23:53:54Z; live config.json
contains both keys (licenseBackup activation, _version 2).

Judge: GLM-5.3 cold read via delegate_task (deleg_30e52384, 36s) — grade A, ship.
Verdict URN: urn:ump:ermkvz6ifbp5svga5cnapv5jhm7c5b7qdbwjjerfpxbwcrolm2za (readback verified)
Codex quota-walled until 2026-08-29; GLM stand-in per Sami 2026-08-17 directive.
2026-08-22 17:22:13 -07:00
Hermes 1d1cd5c95e Merge dc/DC-090-incident-hysteresis-parity: outage incidents follow displayed hysteresis status [glm-grade=A]
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-22 16:53:02 -07:00
Hermes 88f1d4a414 [glm-grade=A] fix(monitoring): DC-090 outage incidents follow displayed hysteresis status
checkForIncidents compared raw probe transitions while the dashboard badge
(DC-086) follows post-hysteresis displayed status. A single raw down blip
between two ups opened AND resolved a critical outage incident; a suppressed
up blip during a real outage resolved it early. Incidents now open/resolve
on displayed-vs-displayed transitions; previousDisplayed=null keeps legacy
raw semantics for direct callers. 6 new parity tests + legacy checkService
test moved to a 4-probe chain. Suite 2616/2616 (110).

Verdict: urn:ump:yc5rdlnmnmhch5audc5fifgbt6d7moi2stqfs5vidsbh6x6zkvgq
2026-08-22 16:52:53 -07:00
Hermes dd1110ef52 Merge dc/DC-089-invite-log-pii-masking: mask invite/user emails in server logs [glm-grade=B]
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-22 16:23:06 -07:00
Hermes 6732a1e1df [glm-grade=B] fix(auth): DC-089 mask invite/user emails in server logs
Two log sites in routes/auth/admin.js wrote raw email PII to the server
log: the SMTP-unconfigured 'auth-invite-send' warn and the 'invite
accepted, user created' info. Both now route through
AuthProvider.maskEmail() with a '[unmaskable-email]' sentinel fallback
(never the raw address). Two regression tests assert the raw address is
absent from log meta and the masked form present. Response contract
unchanged (full email still returned to the authenticated admin).

Judge: GLM-5.3 cold read (deleg_f0896de3), grade B / ship / zero
blockers; polish notes folded in. Verdict URN:
urn:ump:jd2htpwq76ni6bapj3vxjpvypfdnoc4argqbzpcerutmh7u5khea
Full suite: 2610/2610 (109 suites).
2026-08-22 16:22:53 -07:00
Hermes ea96abe95a Merge dc/DC-088-remove-service-tombstones: removeService generation tombstones + incident closure [glm-grade=B]
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-22 15:55:29 -07:00
Hermes 3ccf66754a [glm-grade=B] fix(monitoring): DC-088 removeService generation tombstones + incident closure
- serviceGenerations no longer leaks entries: removeService deletes the live
  entry and records a TTL'd (10min) tombstone swept by cleanupHistory
- monotonic instance-wide generationSeq prevents generation reuse across
  remove->re-add cycles (ABA) and supersedes tombstones on re-configure
- _isStaleCapture(): presence-aware stale check — live entry must match
  exactly; no entry is stale only under a higher-generation tombstone
  (preserves correct behavior for disk-loaded never-configured services)
- catch path increments consecutiveFailures only after the stale check, so
  a late-rejected probe cannot resurrect state for a removed service
- open incidents for a removed service close via the standard resolve path
  (resolvedBy=service-removed, WS/SSE incident-resolved broadcast)
- 6 regression tests; full suite 2608/2608 green

Judge: GLM-5.3 cold read (Codex stand-in), verdict B/ship, zero blockers
2026-08-22 15:55:26 -07:00
Hermes c71b794ccc Merge dc/DC-087-test-mirrors-fetcht: hermetic caddy-admin test mirrors + raw-fetch guard [glm-grade=B]
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-22 15:14:35 -07:00
Hermes f2285a2550 test(api): DC-087 hermetic caddy-admin health mirrors + file-level raw-fetch guard [glm-grade=B]
Two mirrored health-handler test suites (health-endpoints, health-probe-aliases)
probed the Caddy admin API with raw Origin-less native fetch. On the prod host
the adversarial cron runs the full jest suite every 30 min against a live Caddy
admin with enforce_origin: 12 journal 403 lines per run (~700/day of
'client is not allowed to access from origin' spam) while tests stayed green.

- Mirrors now call fetchT (byte-identical to src/app.js:930 probe) with fetchT
  jest.spyOn-mocked at buildApp scope; caddyOk-configurable in both suites
- New guard test in utils-http-caddy-admin-origin.test.js: any __tests__ file
  pairing a raw await-fetch with a Caddy-admin token (:2019|adminUrl|
  CADDY_ADMIN) fails the suite — file-level pairing catches the historical
  cross-line drift shape a call-window regex missed
- DC-087-ALLOW-RAW-FETCH comment escape hatch (raw-text marker, guard file
  never self-exempts, skips logged to jest output)

Judge: GLM-5.3 cold read via delegate_task deleg_4d384dea (round 1 C -> round 2
B, zero blockers, polish folded). Verdict URN: urn:ump:azrv2xp72koiwi5r4yb6ureu4aqqloqq64sgmftsajh6ci2mzj2q
Mutation probes: historical drift reintroduction -> guard red; hatch marker ->
skipped+logged; restore -> 33/33. Full suite 2603/2603.
2026-08-22 15:14:25 -07:00
Hermes 54a1df5ac4 [glm-grade=A] build(status): rebuild dist + sw — DC-085 link-first invite frontend bundle
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-22 14:28:21 -07:00
Hermes eb546bf468 Merge dc/DC-085-link-first-invite-dc-086-flicker-fix: DC-085 link-first invite + DC-086 badge flicker hysteresis + race hardening [glm-grade=A]
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-22 14:27:54 -07:00
Hermes 84e051d975 Merge earlier-tick staging branch (duplicate DC-085/086 cherry-picks + auth-gate/vault tests) — content identical to eab2b00/f8b99f9
# Conflicts:
#	dashcaddy-api/__tests__/health-checker-hysteresis.test.js
#	dashcaddy-api/src/monitoring/health-checker.js
2026-08-22 14:27:40 -07:00
Hermes 628bbe32f6 [glm-grade=A] fix(monitoring): DC-086 round-2 — probe/config race hardening + env parse + incident compare
Round-2 folds the judge-round fixes into DC-086:

- serviceGenerations map: checkService captures the config generation at
  entry and re-validates it before ANY state write (success + error
  paths). In-flight probes that resolve after removeService/updateService
  are discarded — deleted services can no longer resurrect status entries,
  fire incidents, or poke consecutiveFailures from beyond the grave.
- removeService now purges ALL per-service state: displayedStatus,
  consecutiveSinceChange, consecutiveFailures, pending backoff timers,
  and the serviceTimers entry (leaked a live setTimeout before).
- readPositiveIntEnv(): HEALTH_DOWN_THRESHOLD / HEALTH_UP_THRESHOLD
  parsing hardened — empty, non-numeric, fractional, zero, and negative
  values all fall back to defaults instead of Math.max(1, NaN)=NaN.
- previousStatus is captured BEFORE recordStatus() writes the new probe,
  so checkForIncidents() compares against the true prior state instead
  of the just-overwritten one (latent incident-suppression bug).
- Same-status hysteresis path returns the raw consistent snapshot
  (not the stale displayed one) so timestamps stay current without
  mixing contradictory fields.
- Tests: +14 (86 total across the two suites). New coverage: streak
  reset on agreement, malformed env fallbacks (each.of not-a-number/0/
  -2/1.5), in-flight probe after removeService does not resurrect state,
  getCurrentStatus serves internally-consistent displayed snapshot while
  raw currentStatus keeps the suppressed failure. Full suite 2601/2601.

Judge: GLM-5.3 cold-read via delegate_task (deleg_c9fd5900 task-0),
grade A round 1, zero blocking issues. Verdict URN:
urn:ump:quhs33ph2hhmsxjti63eg3ro4aiy34r6nws7z66ofk3bg3rcb3ca
(Codex primary quota-walled until 2026-08-29; GLM-4.6 direct 401;
stand-in chain per codex-as-judge SKILL.md, Sami 2026-08-17.)
2026-08-22 14:23:25 -07:00
Hermes f8b99f9b5a DC-086 service-status flicker fix — asymmetric hysteresis
Dashboard badges perpetually flip green/red for a few seconds at a time,
never stable. Root cause: health-checker emitted 'status-check' on every
probe (every 30s) and dashboard-ws forwarded every one as 'status-change'
to the browser with no diff; live-events.js then unconditionally called
setBadge(). A single transient 5xx (Caddy reload, container restart, TLS
handshake blip) flipped the badge and the next green probe flipped back.

Fix: _computeDisplayedStatus applies asymmetric hysteresis — DOWN_THRESHOLD
(default 2, env-tunable HEALTH_DOWN_THRESHOLD) consecutive probes that
disagree with the displayed 'up' state flip to red; UP_THRESHOLD (default
1, HEALTH_UP_THRESHOLD) flips back to green. History and consecutiveFailures
still record every raw probe so postmortem analysis is unchanged. Only the
SSE broadcast is filtered. getCurrentStatus now returns the displayed
status so a page reload shows the same badge as the live stream.

10 new tests cover first-emit, same-status-dedup, the actual flicker bug
(one-down-then-up keeps green), two-down flips red, one-up recovers fast,
long-steady-green produces exactly one emit, and env-var tuning. All 63
existing health-checker tests still pass. Full suite: 2484/2484.
2026-08-22 06:14:48 -07:00
Hermes eab2b00b13 DC-085 link-first invite — Discord-style share it however you want
Flip POST /api/v1/auth/admin/invites default to no email; always return
the link. Operators copy + share via iMessage/WhatsApp/SMS/Signal/Telegram/
Discord/paste-in-email. Email becomes an opt-in checkbox (was the default).
Add shareText field with pre-formatted message for one-tap paste. Stop
logging raw invite URLs to error.log when SMTP is unconfigured (was just
a dev fallback — link is now in the response). Frontend flips the
checkbox default to unchecked and renders shareText + native share sheet
button (navigator.share) alongside the raw copy-link button. 9 new tests
covering default-no-send, link-always-returned, shareText-shape, opt-in
SMTP send, failed-SMTP-no-leak. Full suite: 2474/2474 + 9 new = 2483.
2026-08-22 06:14:47 -07:00
Hermes 84edb035e3 [grade=B] feat(auth): onboard missing credentials into encrypted vault
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-22 05:41:38 -07:00
Hermes d313b1e872 [grade=B] fix(auth): reuse valid session for cross-host SSO
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-22 04:06:27 -07:00
Hermes be021588c7 DC-086 service-status flicker fix — asymmetric hysteresis
Dashboard badges perpetually flip green/red for a few seconds at a time,
never stable. Root cause: health-checker emitted 'status-check' on every
probe (every 30s) and dashboard-ws forwarded every one as 'status-change'
to the browser with no diff; live-events.js then unconditionally called
setBadge(). A single transient 5xx (Caddy reload, container restart, TLS
handshake blip) flipped the badge and the next green probe flipped back.

Fix: _computeDisplayedStatus applies asymmetric hysteresis — DOWN_THRESHOLD
(default 2, env-tunable HEALTH_DOWN_THRESHOLD) consecutive probes that
disagree with the displayed 'up' state flip to red; UP_THRESHOLD (default
1, HEALTH_UP_THRESHOLD) flips back to green. History and consecutiveFailures
still record every raw probe so postmortem analysis is unchanged. Only the
SSE broadcast is filtered. getCurrentStatus now returns the displayed
status so a page reload shows the same badge as the live stream.

10 new tests cover first-emit, same-status-dedup, the actual flicker bug
(one-down-then-up keeps green), two-down flips red, one-up recovers fast,
long-steady-green produces exactly one emit, and env-var tuning. All 63
existing health-checker tests still pass. Full suite: 2484/2484.
2026-08-20 04:46:17 -07:00
Hermes d8459a4a87 DC-085 link-first invite — Discord-style share it however you want
Flip POST /api/v1/auth/admin/invites default to no email; always return
the link. Operators copy + share via iMessage/WhatsApp/SMS/Signal/Telegram/
Discord/paste-in-email. Email becomes an opt-in checkbox (was the default).
Add shareText field with pre-formatted message for one-tap paste. Stop
logging raw invite URLs to error.log when SMTP is unconfigured (was just
a dev fallback — link is now in the response). Frontend flips the
checkbox default to unchecked and renders shareText + native share sheet
button (navigator.share) alongside the raw copy-link button. 9 new tests
covering default-no-send, link-always-returned, shareText-shape, opt-in
SMTP send, failed-SMTP-no-leak. Full suite: 2474/2474 + 9 new = 2483.
2026-08-20 04:46:12 -07:00
Hermes 499fcc2742 Merge dc/DC-084-arch-sami-caddy-healthcheck-removal: DC-084 remove redundant active Caddy health check from arch.sami [glm-grade=A]
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-19 01:43:30 -07:00
Hermes 93d6c44e45 [glm-grade=A] docs(backlog): document DC-084 — remove redundant active Caddy health check from arch.sami
The /etc/caddy/sites/arch.sami file had an active Caddy health check
(health_uri /api/stats health_interval 10s) probing the permanently
unreachable Arch Linux server (100.120.159.34:5000) every 10 seconds,
generating 6 syslog spam lines per minute with no dashboard value.

src/monitoring/caddy-upstream-watcher.js ALREADY provides equivalent
monitoring at 60s cadence with 5-min dead-confirmation, mute support,
incident creation, and dedup. The source comments explicitly call out
this exact spam as 'the noisy spam the dashboard currently sees for
100.120.159.34:5000'.

Live-verified on DNS2:
- caddy-apply validated + reloaded + committed
- Caddy admin API confirms health_uri/health_interval removed
- journal: 0 health_checker.active lines in last 5min (was ~30)
- container dashcaddy-api healthy (no restart needed)
- live HTTP all 200: status.sami, dashcaddy.net, ca.sami
- watcher correctly tracks 100.120.159.34:5000 as dead (1905+ failures)

Backup .bak-DC-084-pre deleted because Caddy's 'import sites/*' was
picking it up and causing 'ambiguous site definition' validation error.

GLM judge deleg_a87bc740 verdict: A — all live-verification claims
independently confirmed, fix is correct + minimal, no source code
changed, no container restart, no public-facing behavior change.

Co-Authored-By: Hermes <hermes@nousagent.com>
2026-08-19 01:43:10 -07:00
Hermes 4c4ffc35ca Merge dc/DC-082-update-manager-compose-prefix: DC-083 public share endpoint input hardening [glm-grade=A]
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s
2026-08-19 00:11:24 -07:00
Hermes 7e68955e66 [glm-grade=A] fix(share): public-endpoint input hardening + rate limit (DC-083)
Public share endpoints accept untrusted fields. Pre-fix code used bare
type checks (email.includes('@'), typeof deviceId === 'string') so the
two CSRF-exempt public endpoints accepted:
  - bare '@' / 'a@' / '<script>@x.c'
  - 10MB email strings (data/shares.json bloat)
  - CR/LF/NUL in email (corrupts on-disk JSON + log lines)
  - CR/LF/NUL in deviceId (flows into Tailscale auth-key description)

Hardening (5 files, +661 net):

1. routes/share.js + src/security/share-store.js: shared validators
   - validatePublicEmail(raw): charset (a-z0-9._%+-@), 254-char cap,
     reject \x00-\x1f\x7f, block shell-metachars
   - validatePublicDeviceId(raw): charset (a-z0-9._:-), 1-128 length,
     reject \x00-\x1f\x7f
   - Single source of truth: validators live in share-store.js, exported,
     imported by routes/share.js (drift-eliminated)

2. Routes that were 'email.includes(@)' now use validator. Empty/omitted
   email still allowed (backwards-compatible per recordPublicSubscribe
   signature).

3. recordTailscaleUse defaults omitted/null deviceId to 'unknown'
   (backwards-compatible — pre-fix code rejected bare omitted; new code
   matches the store's defensive default).

4. constants.js: RATE_LIMITS.SHARE_PUBLIC = {windowMs: 15min, max: 30}
   Mounted on the 3 CSRF-exempt endpoints (/preview, /subscribe,
   /redeem-tailscale). 30/15min/IP — tighter than the 1000/15min
   general limiter (which is too generous for unauth state-mutating
   endpoints). Falls back to no-op in test envs.

5. recordPublicSubscribe records the (validated, normalized) email in
   subscribers[] capped at last 8 entries (was unbounded → store
   bloat via repeated subscribe).

Test coverage (38 new tests in __tests__/share-dc083.routes.test.js + 3
in __tests__/share-routes.test.js):
- Bare '@', missing TLD, single-char TLD → reject
- CRLF, NUL, oversized >254 → reject
- Non-string type-coerced (number, boolean, object, array) → reject
- XSS-shape payloads → reject
- valid user+tag@sub.domain.io + nodekey:... → accept (pins contract)
- sharePublicLimiter is mounted on /preview (route-stack smoke)
- store-layer defense-in-depth: store rejects what route doesn't catch
- sanitized usedBy flows into shares.json
- rejection does NOT mark share used
- subscriber array bounded at 8 entries

Test results:
- 68/68 share-related tests pass (30 share-routes + 38 share-dc083)
- Full repo: 2427/2427 tests pass
- npx eslint: 0 errors, 22 warnings (baseline HEAD =14; +8 in test mocks)

Judge verdict: GLM-5.3 round-2 grade A. Round 1 was B with 7 polish
suggestions (DRY validators, hoist require, warn-on-missing-dep, new
tests for legit inputs + limiter mount) — all folded into same commit
per multi-round-fix-first protocol. Zero blocking issues.

Threat model: the 2 POST endpoints mutate shares.json + Tailscale auth
descriptions. Pre-fix was effectively 'input trust boundary = NONE'.
Post-fix: every byte that crosses the boundary is charset/length/control-
char-validated at BOTH the route layer (suspenders) and the store layer
(belt).
2026-08-19 00:10:37 -07:00
48 changed files with 3843 additions and 407 deletions
+25
View File
@@ -400,3 +400,28 @@ Tickets DC-046 through DC-049 implement pluggable auth + email magic link. Sami
- **prerequisite:** None.
- **result:** Shipped codex-graded A. All 49 `console.*` sites in `src/managers/update-manager.js` now route through `log.info/log.warn/log.error` from `src/utils/logging` (tag = `'update'`). Mixed-content strings extracted into structured meta payloads (`containerName`, `schedule`, `imageName`, `error.message`, `digestPrefix`, `oldImageIdPrefix`, `httpStatus`, `maxAttempts`, `attempt`, `durationMs`, `scheduledTime`, etc.) so fields are queryable instead of inlined into the message. Errors now go through `log.error(ctx, errObj)` so they land in error.log with full stack trace + context, not just stderr. 1539/1539 Jest tests pass (78/78 update-manager tests still pass). ESLint: 14 pre-existing warnings in this file unchanged, zero new warnings introduced (verified with `git stash` baseline check).
### DC-086: Service-status flicker fix — asymmetric hysteresis on the badge
- **status:** in-progress
- **owner:** hermes
- **details:** Dashboard service badges perpetually flip between green and red for "a few seconds at a time, never stable" (Sami's report, 2026-08-20). Root cause: `src/monitoring/health-checker.js` `recordStatus()` emits `'status-check'` on EVERY probe (every 30s), and `src/websocket/dashboard-ws.js` forwards every probe as `'status-change'` to the browser with no diff. The frontend `live-events.js` then unconditionally calls `setBadge()` — which resets the icon + pill text on every event. A single transient 5xx (Caddy reload, container CPU steal, mid-flight TLS handshake, container restart during probe) flips the badge red and the next green probe flips it back. Fix: add asymmetric hysteresis in `_computeDisplayedStatus(serviceId, rawStatus)` — going DOWN requires 2 consecutive "down" probes (default `HEALTH_DOWN_THRESHOLD=2`), going UP requires only 1 (default `HEALTH_UP_THRESHOLD=1`). History + `consecutiveFailures` still record raw probe results (operators want full fidelity for postmortems); only the dashboard broadcast is filtered. `getCurrentStatus()` now returns the displayed status so a page reload shows the same badge as the live SSE stream. Both thresholds are env-var configurable so operators can tune. New tests in `__tests__/health-checker-hysteresis.test.js` cover: first probe emits; second probe same-status does NOT re-emit; one-down-then-up keeps green; two-down flips to red; one-up after down flips back to green; `getCurrentStatus` returns displayed not raw. Effort: ~30 min. Risk: low — pure behavior filter, no schema breaks, all 63 existing health-checker tests must stay green.
- **impact:** Operators stop seeing perpetual red/green flicker on healthy services. Real outages still get flagged (2 consecutive 30s probes = ~60s before badge flips red, which is still faster than a human notices). Background probe history is unchanged so postmortem analysis still works.
- **prerequisite:** None.
- **result:** _pending — ship + codex round_
### DC-085: Link-first invite — Discord-style "share it however you want"
- **status:** in-progress
- **owner:** hermes
- **details:** Today `POST /api/v1/auth/admin/invites` defaults to sending the invite link via SMTP; if SMTP is not configured it spams the server console with `[DC-048-DEV-INVITE-LINK]` log lines. Sami wants Discord-style: the link is always returned in the response, and email is an opt-in checkbox. Operators should be free to copy the link and share it via iMessage / SMS / WhatsApp / Telegram / Signal / Discord / paste-in-email — whatever fits. (1) Flip default `sendEmail !== false` to `sendEmail === true` in `routes/auth/admin.js` so omitting the field means "no email, just hand me the link." (2) Stop logging the raw invite URL to error.log when SMTP is unconfigured — that path was only useful when there was no UI way to grab the link; now there is. (3) Add a `shareText` field to the response: `"Join my DashCaddy as <role> — <acceptUrl> — expires in Nh."` for one-tap paste into any messenger. (4) Frontend: `status/js/admin.js` `_renderInviteForm` flips the "Send email" checkbox default to **unchecked**, updates `_renderIssuedInviteBanner` to show both the raw link AND the shareText (with its own copy button + `navigator.share()` native share-sheet button where available). (5) New tests in `__tests__/admin-invites.test.js` covering: default sendEmail=false (no SMTP send attempted, no console log); `sendEmail: true` triggers SMTP send; `shareText` is present and well-formed; `acceptUrl` is always returned; expired sendEmail path doesn't leak token to logs. Effort: ~1 hr. Risk: low — pure behavior flip + UI additive change.
- **impact:** Closes the friction between "host wants to add a friend" and "host has to configure SMTP first." Mirrors Discord/Slack/Linear invite flows where the link IS the deliverable. No new tier changes, no schema breaks.
- **prerequisite:** DC-048 (invite store + admin route), DC-052 (Pro gate stays).
- **result:** _pending — ship + codex round_
### DC-084: Remove redundant active Caddy health check from `arch.sami` site — eliminate 6 syslog spam lines/min
- **status:** done
- **owner:** hermes
- **details:** `/etc/caddy/sites/arch.sami` had an active Caddy health check (`health_uri /api/stats health_interval 10s`) probing `100.120.159.34:5000` every 10 seconds. The upstream Arch Linux server `100.120.159.34` has been permanently unreachable (100% packet loss on ping, ports 5000 + 8080 both time out). Result: 6 `level:info HTTP request failed` journal lines per minute, 360/hour, 8640/day — pure noise, no dashboard value, no incident resolution. The `src/monitoring/caddy-upstream-watcher.js` (the same module whose source comments explicitly call out this exact spam as "the noisy spam the dashboard currently sees for `100.120.159.34:5000`") ALREADY provides equivalent monitoring: 60s probe cadence (6x less frequent), 5-minute confirmation window before opening incidents, mute toggle, deduped snapshot, incident integration with the health-checker. The active Caddy check is redundant. Fix: edit `/etc/caddy/sites/arch.sami` to remove the `health_uri / health_interval` block, leaving only `reverse_proxy 100.120.159.34:5000`. Apply via `caddy-apply` (validates+reloads+commits atomically). Backup `.bak-DC-084-pre` created pre-edit; deleted after `caddy-apply` succeeded because the `.bak` file was being picked up by Caddy's `import sites/*` and causing an "ambiguous site definition" validation error.
- **impact:** Eliminates 100% of recurring caddy journal spam from the dead Arch upstream. The dashboard's `caddy-upstream-watcher.js` continues to monitor the dead upstream correctly (now at `consecutiveFailures: 1905+`, `lastSuccessAt: null`, `status: down`, `dead: true`) — operators see the dead upstream in the dashboard, just without the journal noise. Future Caddyfile authors who add an active health check to a `*.sami` site will be unaware that they should not (since the dashboard handles monitoring), so a follow-up could add a CLAUDE.md note or a Caddyfile lint warning. Out of scope for this tick.
- **prerequisite:** None. `caddy-upstream-watcher.js` already provides equivalent monitoring.
- **result:** Shipped GLM-pending (Codex quota dead). Before/after on DNS2 (`journalctl -u caddy --since "5 minutes ago" | grep health_checker.active | wc -l`): **before = ~30 entries / 5min** (active probe every 10s, all failing); **after = 0 entries / 5min**. Live-verified: `caddy validate` succeeded (after removing `.bak` file that caused `ambiguous site definition`), Caddy reloaded via `caddy-apply`, route `arch.sami → 100.120.159.34:5000` still active in admin API (verified via `curl http://localhost:2019/config/apps/http/servers/srv0/routes``health_uri: None, health_interval: None` confirms the block is gone). Container `dashcaddy-api Up About an hour (healthy)` (no restart needed — only Caddyfile changed, not container). Live HTTP smoke all green: `https://status.sami=200`, `https://dashcaddy.net=200`, `https://ca.sami=200`, `https://status.sami/api/health=401` (auth-gated, expected). Watcher state for `100.120.159.34:5000`: `consecutiveFailures: 1905`, `lastError: "probe timeout"`, `status: down`, `dead: true` — correctly tracked in `/opt/dashcaddy/dashcaddy-api/data/caddy-upstreams.json`. Backup deleted (would have caused site-definition ambiguity on next Caddy reload). Git: change lives only in DNS2's `/etc/caddy/sites/arch.sami` (the `/etc/caddy` git repo `.gitignore` excludes `sites/` per design — only the main `Caddyfile` is tracked). The dashcaddy source repo (`/root/dashcaddy`) carries only this BACKLOG.md documentation update on branch `dc/DC-084-arch-sami-caddy-healthcheck-removal`.
- **Tests:** No source code change; existing `__tests__/caddy-upstream-watcher.test.js` 26/26 pass (baseline preserved). 2465/2465 repo tests pass (4 pre-existing billing test suites fail with `Cannot find module pdfkit` — unrelated to this change).
@@ -0,0 +1,285 @@
/**
* Tests for DC-085: link-first invite (Discord-style "share it however you want").
*
* - default sendEmail omission = no email sent, link returned, no token in logs
* - sendEmail:true triggers SMTP send when configured
* - sendEmail:true + SMTP unconfigured = deliveredVia:'failed', no token leaked
* - shareText field present and well-formed in every response
* - acceptUrl always present (regardless of sendEmail)
* - role + ttl validation unchanged from DC-048
*
* Strategy: drive the route handler directly with mock req/res, mount the admin
* router against an isolated userStore + inviteStore + email-sender stub.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const express = require('express');
function _tmpDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'dashcaddy-admin-invites-test-'));
}
function _cleanup(dir) {
try { fs.rmSync(dir, { recursive: true, force: true }); } catch {}
}
// Stub email-sender so we can assert "was it called?" without an SMTP server.
// NOTE: the variable name MUST start with `mock` so Jest's hoisted `jest.mock()`
// call is allowed to reference it (Babel guard against out-of-scope access).
const mockEmailSender = {
isConfigured: jest.fn(() => false),
sendEmail: jest.fn(async () => undefined),
};
jest.mock('../src/auth/providers/email-sender', () => mockEmailSender);
describe('DC-085: link-first admin invites', () => {
let dir, app, request;
let logCalls; // captured { level, msg, meta } from our fake log
beforeEach(async () => {
jest.clearAllMocks();
dir = _tmpDir();
logCalls = [];
// Set up email auth enable flag so userStore mounts.
process.env.NODE_ENV = 'test';
const { createUserStore } = require('../src/security/user-store');
const userStore = createUserStore({ dataDir: dir });
// Bootstrap the admin so we have a session-attributable user.
await userStore.login({ email: 'admin@sami-host.me' });
// Build a tiny Express app with the admin router mounted, but skip the
// global auth gate (we inject req.user directly).
const adminRouter = require('../routes/auth/admin')({
asyncHandler: (fn) => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next),
errorResponse: (_res, code, msg) => ({ status: code, msg }),
log: {
info: (topic, msg, meta) => logCalls.push({ level: 'info', topic, msg, meta }),
warn: (topic, msg, meta) => logCalls.push({ level: 'warn', topic, msg, meta }),
error: (topic, msg, meta) => logCalls.push({ level: 'error', topic, msg, meta }),
},
session: { isSessionValid: () => true, create: () => {}, setCookie: () => {} },
dataDir: dir,
});
app = express();
app.use(express.json());
// Inject req.user = admin so /admin/* passes the role gate.
app.use((req, _res, next) => {
req.user = { id: 'admin-id', email: 'admin@sami-host.me', role: 'admin' };
req.app.locals = req.app.locals || {};
req.app.locals.siteConfig = {}; // no publicBaseUrl — route uses req.headers
req.app.locals.emailConfig = null; // SMTP not configured by default
next();
});
app.use('/api/v1/auth', adminRouter);
// Error handler — last in chain.
app.use((err, _req, res, _next) => {
const code = (err && err.statusCode) || 500;
res.status(code).json({
success: false,
error: err && err.message,
code: err && err.code,
});
});
request = require('supertest');
});
afterEach(() => _cleanup(dir));
test('default sendEmail (omitted) returns link and does NOT send email', async () => {
const res = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ email: 'friend@example.com', role: 'operator' });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(mockEmailSender.sendEmail).not.toHaveBeenCalled();
expect(res.body.acceptUrl).toMatch(/\/api\/v1\/auth\/invites\/[^/]+\/accept$/);
expect(res.body.deliveredVia).toBe('manual');
});
test('default sendEmail does NOT log raw token to server log', async () => {
const res = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ email: 'friend@example.com', role: 'operator' });
const acceptUrl = res.body.acceptUrl;
// Extract the token from the URL and verify it does NOT appear in any log call.
const token = acceptUrl.match(/invites\/([^/]+)\/accept/)[1];
const tokenLeaked = logCalls.some(c =>
typeof c.msg === 'string' && c.msg.includes(token)
);
expect(tokenLeaked).toBe(false);
// Also assert no log entry mentions the URL verbatim (the old
// `[DC-048-DEV-INVITE-LINK] url=...` spam).
const oldSpam = logCalls.find(c =>
typeof c.msg === 'string' && c.msg.includes('[DC-048-DEV-INVITE-LINK]')
);
expect(oldSpam).toBeUndefined();
});
test('shareText is present and well-formed in every response', async () => {
const res = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ email: 'friend@example.com', role: 'operator', ttlHours: 24 });
expect(res.body.shareText).toBeDefined();
expect(res.body.shareText).toContain('Join my DashCaddy');
expect(res.body.shareText).toContain('operator');
expect(res.body.shareText).toContain(res.body.acceptUrl);
expect(res.body.shareText).toContain('expires in 24h');
});
test('acceptUrl is always returned regardless of sendEmail', async () => {
const r1 = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ email: 'a@x.com', sendEmail: false });
const r2 = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ email: 'b@x.com' });
expect(r1.body.acceptUrl).toBeTruthy();
expect(r2.body.acceptUrl).toBeTruthy();
});
test('sendEmail: true triggers SMTP send when configured', async () => {
// Build a SECOND app instance where emailConfig is a real-looking object,
// so isConfigured() returns true. The first app uses emailConfig=null.
mockEmailSender.isConfigured.mockReturnValueOnce(true);
mockEmailSender.sendEmail.mockResolvedValueOnce(undefined);
const app2 = express();
app2.use(express.json());
app2.use((req, _res, next) => {
req.user = { id: 'admin-id', email: 'admin@sami-host.me', role: 'admin' };
req.app.locals = req.app.locals || {};
req.app.locals.siteConfig = {};
req.app.locals.emailConfig = { host: 'smtp.test', from: 'noreply@test' };
next();
});
const { createUserStore } = require('../src/security/user-store');
const userStore2 = createUserStore({ dataDir: dir });
await userStore2.login({ email: 'admin@sami-host.me' });
const router2 = require('../routes/auth/admin')({
asyncHandler: (fn) => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next),
errorResponse: (_res, code, msg) => ({ status: code, msg }),
log: { info() {}, warn: (t, m, meta) => logCalls.push({ level: 'warn', topic: t, msg: m, meta }), error() {} },
session: { isSessionValid: () => true, create: () => {}, setCookie: () => {} },
dataDir: dir,
});
app2.use('/api/v1/auth', router2);
const res = await request(app2)
.post('/api/v1/auth/admin/invites')
.send({ email: 'friend@example.com', role: 'viewer', sendEmail: true });
expect(res.status).toBe(200);
expect(mockEmailSender.sendEmail).toHaveBeenCalledTimes(1);
const [_cfg, to, subject, text, html] = mockEmailSender.sendEmail.mock.calls[0];
expect(to).toBe('friend@example.com');
expect(subject).toMatch(/invited/i);
expect(text).toContain(res.body.acceptUrl);
expect(html).toContain(res.body.acceptUrl);
expect(res.body.deliveredVia).toBe('email');
});
test('sendEmail: true + SMTP unconfigured returns deliveredVia:failed and does NOT leak token', async () => {
mockEmailSender.isConfigured.mockReturnValueOnce(false);
const res = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ email: 'friend@example.com', role: 'operator', sendEmail: true });
expect(res.status).toBe(200);
expect(mockEmailSender.sendEmail).not.toHaveBeenCalled();
expect(res.body.deliveredVia).toBe('failed');
// acceptUrl + shareText still present so the operator can share manually.
expect(res.body.acceptUrl).toBeTruthy();
expect(res.body.shareText).toBeTruthy();
// Token does NOT appear in any log call.
const token = res.body.acceptUrl.match(/invites\/([^/]+)\/accept/)[1];
const tokenLeaked = logCalls.some(c =>
typeof c.msg === 'string' && c.msg.includes(token)
);
expect(tokenLeaked).toBe(false);
});
test('invalid role silently defaults to operator (DC-048 behavior preserved)', async () => {
// DC-048: the route's `(role && VALID_ROLES.has(role)) ? role : 'operator'`
// silently substitutes default rather than throwing. This test pins that
// behavior so a future "strict role validation" change is a deliberate
// decision, not a silent regression.
const res = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ email: 'a@x.com', role: 'superuser' });
expect(res.status).toBe(200);
expect(res.body.role).toBe('operator');
expect(mockEmailSender.sendEmail).not.toHaveBeenCalled();
});
test('email validation: missing email still rejected', async () => {
const res = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ role: 'operator' });
expect(res.status).toBe(400);
expect(mockEmailSender.sendEmail).not.toHaveBeenCalled();
});
test('ttlHours: 1 still produces shareText with correct expiry wording', async () => {
const res = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ email: 'a@x.com', ttlHours: 1 });
expect(res.body.shareText).toContain('expires in 1h');
});
test('DC-089: SMTP-unconfigured warn log masks the invite email (no raw PII)', async () => {
mockEmailSender.isConfigured.mockReturnValueOnce(false);
const res = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ email: 'friend@example.com', role: 'operator', sendEmail: true });
expect(res.status).toBe(200);
expect(res.body.deliveredVia).toBe('failed');
const warn = logCalls.find(c =>
c.level === 'warn' && c.topic === 'auth-invite-send'
);
expect(warn).toBeDefined();
// The raw address must not appear; the masked form must.
expect(JSON.stringify(warn.meta)).not.toContain('friend@example.com');
expect(warn.meta.email).toBe('fr****@example.com');
});
test('DC-089: invite-accepted info log masks the created user email (no raw PII)', async () => {
// Pre-authorize the email (POST /admin/users) so userStore.login doesn't
// reject with not_authorized — bootstrap already happened in beforeEach.
const preauth = await request(app)
.post('/api/v1/auth/admin/users')
.send({ email: 'newfriend@example.com' });
expect(preauth.status).toBe(200);
const issue = await request(app)
.post('/api/v1/auth/admin/invites')
.send({ email: 'newfriend@example.com', role: 'viewer' });
expect(issue.status).toBe(200);
const token = issue.body.acceptUrl.match(/invites\/([^/]+)\/accept/)[1];
const res = await request(app)
.post(`/api/v1/auth/invites/${token}/accept`)
.send({});
expect(res.status).toBe(200);
const info = logCalls.find(c =>
c.level === 'info' && c.msg === 'invite accepted, user created'
);
expect(info).toBeDefined();
expect(JSON.stringify(info.meta)).not.toContain('newfriend@example.com');
expect(info.meta.email).toBe('ne****@example.com');
});
});
@@ -0,0 +1,72 @@
'use strict';
/**
* Regression tests for config-schema.js KNOWN_KEYS — DC-091.
*
* Bug: license-manager.js persists config.licenseBackup (activation
* restore-on-restart) and src/config/migrations.js stamps config._version,
* but neither key was in KNOWN_KEYS — so every startup logged
* `Unknown config key "licenseBackup" / "_version" — possible typo?`
* false positives (verified in live dashcaddy-api container logs,
* 2026-08-22T23:53:54Z restart).
*
* These tests pin: (1) the live production config key set validates with
* zero unknown-key warnings, (2) genuine typos still warn, (3) the schema
* stays in sync with the first-party writer keys.
*/
const { validateConfig } = require('../src/utilities/config-schema');
describe('config-schema KNOWN_KEYS vs first-party writers (DC-091)', () => {
// Exact key set of the live production config.json (DNS2, verified
// 2026-08-23). If a new key appears here, teach KNOWN_KEYS about it —
// or fix the writer if it's a typo.
const LIVE_CONFIG_KEYS = [
'_version', 'configurationType', 'customFavicon', 'customLogo',
'dashboardHost', 'dashboardTitle', 'dns', 'dnsServers', 'language',
'license', 'licenseBackup', 'logoPosition', 'pylon', 'setupComplete',
'timestamp', 'tld', 'updatedAt'
];
test('live production config key set produces zero unknown-key warnings', () => {
const config = {};
for (const key of LIVE_CONFIG_KEYS) {
// Minimal valid-ish values; validateConfig only cares about shape
// for these keys, and unknown-key detection is the target here.
config[key] = key === '_version' ? 2 : (key === 'dnsServers' ? {} : 'x');
}
const result = validateConfig(config);
const unknownWarnings = result.warnings.filter((w) => w.includes('Unknown config key'));
expect(unknownWarnings).toEqual([]);
});
test('licenseBackup and _version (first-party writer keys) do not warn', () => {
const result = validateConfig({ licenseBackup: { code: 'DC-...' }, _version: 2 });
expect(result.warnings).toEqual([]);
});
test('genuine typos still warn (guard against over-allowing)', () => {
const result = validateConfig({ dashboadTitle: 'typo' });
expect(result.warnings).toEqual([
'Unknown config key "dashboadTitle" — possible typo?'
]);
});
test('KNOWN_KEYS stays in sync with license-manager writer keys', () => {
// license-manager writes config.licenseBackup and config.license — both
// must be recognized. We assert via validateConfig (public surface)
// rather than importing the private KNOWN_KEYS array.
const result = validateConfig({ license: { code: 'DC-...' }, licenseBackup: { code: 'DC-...' } });
expect(result.warnings.filter((w) => w.includes('Unknown config key'))).toEqual([]);
});
});
describe('config-schema sync guard: migrations writer', () => {
test('_version is recognized at every migration version value', () => {
// migrations.js bumps _version 0→1→2; the key itself must never warn.
for (const v of [0, 1, 2, 99]) {
const result = validateConfig({ _version: v });
expect(result.warnings).toEqual([]);
}
});
});
@@ -0,0 +1,297 @@
/**
* Tests for DC-086: asymmetric hysteresis on the dashboard service badge.
*
* - First probe always emits (no prior state).
* - Same-status probe does NOT re-emit (dedup against repeated green).
* - One "down" then back to "up" keeps the badge green (no flicker).
* - Two consecutive "down" probes flip the badge to red.
* - One "up" after a down streak flips back to green (fast recovery).
* - History retains every raw probe even when no emit happens.
* - getCurrentStatus returns displayed status, not raw.
*/
'use strict';
const path = require('path');
const fs = require('fs');
const os = require('os');
// Use an isolated data dir so test history doesn't pollute the real one.
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dashcaddy-hyst-'));
process.env.HEALTH_DATA_DIR = tmpDir;
process.env.HEALTH_CONFIG_FILE = path.join(tmpDir, 'health-config.json');
process.env.HEALTH_HISTORY_FILE = path.join(tmpDir, 'health-history.json');
// Module exports a singleton instance, not a class — see module.exports in
// src/monitoring/health-checker.js. The test creates fresh state by replacing
// the relevant maps on the singleton in beforeEach.
const healthCheckerSingleton = require('../src/monitoring/health-checker');
const originalDownThreshold = process.env.HEALTH_DOWN_THRESHOLD;
const originalUpThreshold = process.env.HEALTH_UP_THRESHOLD;
function restoreEnv(name, value) {
if (value === undefined) delete process.env[name];
else process.env[name] = value;
}
function makeUp(serviceId = 'svc1') {
return {
serviceId,
timestamp: new Date().toISOString(),
status: 'up',
responseTime: 50,
statusCode: 200,
message: 'Service is healthy',
details: { headers: {}, bodyLength: 12 }
};
}
function makeDown(serviceId = 'svc1') {
return {
serviceId,
timestamp: new Date().toISOString(),
status: 'down',
responseTime: 50,
statusCode: 500,
message: 'fail',
details: { headers: {}, bodyLength: 0 }
};
}
describe('DC-086: hysteresis on the dashboard badge', () => {
let hc;
let emitSpy;
beforeEach(() => {
// Reset the singleton's per-test state so each case starts clean.
healthCheckerSingleton.displayedStatus = new Map();
healthCheckerSingleton.consecutiveSinceChange = new Map();
healthCheckerSingleton.currentStatus = new Map();
healthCheckerSingleton.history = {};
healthCheckerSingleton.removeAllListeners('status-check');
emitSpy = jest.fn();
healthCheckerSingleton.on('status-check', emitSpy);
hc = healthCheckerSingleton;
});
afterEach(() => {
restoreEnv('HEALTH_DOWN_THRESHOLD', originalDownThreshold);
restoreEnv('HEALTH_UP_THRESHOLD', originalUpThreshold);
});
afterAll(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
test('first probe (no prior state) emits', () => {
hc.recordStatus('svc1', makeUp());
expect(emitSpy).toHaveBeenCalledTimes(1);
expect(emitSpy.mock.calls[0][0].status).toBe('up');
});
test('second probe with same status does NOT re-emit', () => {
hc.recordStatus('svc1', makeUp());
hc.recordStatus('svc1', makeUp());
expect(emitSpy).toHaveBeenCalledTimes(1);
});
test('one "down" then "up" keeps the badge green (the flicker bug)', () => {
hc.recordStatus('svc1', makeUp()); // baseline: green, emit 1
hc.recordStatus('svc1', makeDown()); // one blip — keep green, no emit
hc.recordStatus('svc1', makeUp()); // recovered — still green, no emit
expect(emitSpy).toHaveBeenCalledTimes(1);
expect(hc.displayedStatus.get('svc1').status).toBe('up');
});
test('up, down, up, down, down resets the first streak before flipping', () => {
hc.recordStatus('svc1', makeUp());
hc.recordStatus('svc1', makeDown());
hc.recordStatus('svc1', makeUp());
hc.recordStatus('svc1', makeDown());
expect(hc.displayedStatus.get('svc1').status).toBe('up');
expect(emitSpy).toHaveBeenCalledTimes(1);
hc.recordStatus('svc1', makeDown());
expect(hc.displayedStatus.get('svc1').status).toBe('down');
expect(emitSpy).toHaveBeenCalledTimes(2);
});
test('two consecutive "down" probes flip the badge to red', () => {
hc.recordStatus('svc1', makeUp()); // baseline: green
hc.recordStatus('svc1', makeDown()); // blip #1 — keep green (counter=1)
hc.recordStatus('svc1', makeDown()); // blip #2 — flip red (counter=2 >= DOWN_THRESHOLD)
expect(emitSpy).toHaveBeenCalledTimes(2);
expect(emitSpy.mock.calls[1][0].status).toBe('down');
expect(hc.displayedStatus.get('svc1').status).toBe('down');
});
test('one "up" after a down streak flips back to green (fast recovery)', () => {
hc.recordStatus('svc1', makeUp());
hc.recordStatus('svc1', makeDown());
hc.recordStatus('svc1', makeDown()); // now red
expect(hc.displayedStatus.get('svc1').status).toBe('down');
hc.recordStatus('svc1', makeUp()); // first green — flip back
expect(emitSpy).toHaveBeenCalledTimes(3);
expect(emitSpy.mock.calls[2][0].status).toBe('up');
expect(hc.displayedStatus.get('svc1').status).toBe('up');
});
test('history retains every raw probe even when no emit happens', () => {
hc.recordStatus('svc1', makeUp());
hc.recordStatus('svc1', makeDown()); // blip, no emit
hc.recordStatus('svc1', makeUp()); // recovery, no emit
expect(hc.history['svc1'].length).toBe(3);
expect(hc.history['svc1'][0].status).toBe('up');
expect(hc.history['svc1'][1].status).toBe('down');
expect(hc.history['svc1'][2].status).toBe('up');
});
test('getCurrentStatus returns the displayed status, not the raw probe', () => {
const displayedUp = makeUp();
displayedUp.timestamp = '2026-08-22T09:59:00.000Z';
displayedUp.statusCode = 200;
displayedUp.message = 'healthy';
displayedUp.details = { source: 'accepted-up' };
hc.recordStatus('svc1', displayedUp);
const latestRaw = makeDown();
latestRaw.timestamp = '2026-08-22T10:00:00.000Z';
latestRaw.responseTime = 987;
latestRaw.statusCode = 500;
latestRaw.message = 'failed probe';
latestRaw.error = 'upstream failure';
latestRaw.details = { source: 'suppressed-down' };
hc.recordStatus('svc1', latestRaw); // raw=down, displayed=up
const out = hc.getCurrentStatus();
expect(out['svc1'].status).toBe('up'); // shown to API consumers
expect(out['svc1'].timestamp).toBe(displayedUp.timestamp);
expect(out['svc1'].statusCode).toBe(200);
expect(out['svc1'].message).toBe('healthy');
expect(out['svc1'].error).toBeUndefined();
expect(out['svc1'].details).toEqual({ source: 'accepted-up' });
expect(hc.currentStatus.get('svc1')).toBe(latestRaw);
});
test('a long steady-green run produces exactly ONE emit (no per-probe spam)', () => {
for (let i = 0; i < 50; i++) hc.recordStatus('svc1', makeUp());
expect(emitSpy).toHaveBeenCalledTimes(1);
});
test('a long steady-green-then-steady-red transition: 1 emit (up), 1 emit (red)', () => {
for (let i = 0; i < 10; i++) hc.recordStatus('svc1', makeUp());
expect(emitSpy).toHaveBeenCalledTimes(1);
hc.recordStatus('svc1', makeDown());
hc.recordStatus('svc1', makeDown()); // flips to red
expect(emitSpy).toHaveBeenCalledTimes(2);
for (let i = 0; i < 10; i++) hc.recordStatus('svc1', makeDown());
expect(emitSpy).toHaveBeenCalledTimes(2); // no further broadcasts
});
test('DOWN_THRESHOLD env var is honored', () => {
process.env.HEALTH_DOWN_THRESHOLD = '3';
jest.resetModules();
const HC2Module = require('../src/monitoring/health-checker');
// Module is a singleton with DOWN_THRESHOLD captured at module load —
// resetModules gives us a fresh module-level instance with the new env.
const hc2 = HC2Module;
hc2.displayedStatus = new Map();
hc2.consecutiveSinceChange = new Map();
hc2.currentStatus = new Map();
hc2.history = {};
hc2.removeAllListeners('status-check');
const spy = jest.fn();
hc2.on('status-check', spy);
hc2.recordStatus('svc1', makeUp());
hc2.recordStatus('svc1', makeDown()); // 1
hc2.recordStatus('svc1', makeDown()); // 2 — still green (need 3)
expect(spy).toHaveBeenCalledTimes(1);
expect(hc2.displayedStatus.get('svc1').status).toBe('up');
hc2.recordStatus('svc1', makeDown()); // 3 — flip
expect(spy).toHaveBeenCalledTimes(2);
expect(hc2.displayedStatus.get('svc1').status).toBe('down');
});
test.each(['not-a-number', '0', '-2', '1.5'])('malformed DOWN_THRESHOLD %s falls back to 2', value => {
process.env.HEALTH_DOWN_THRESHOLD = value;
jest.resetModules();
const hc2 = require('../src/monitoring/health-checker');
hc2.displayedStatus = new Map();
hc2.consecutiveSinceChange = new Map();
hc2.currentStatus = new Map();
hc2.history = {};
hc2.removeAllListeners('status-check');
const spy = jest.fn();
hc2.on('status-check', spy);
hc2.recordStatus('svc1', makeUp());
hc2.recordStatus('svc1', makeDown());
expect(spy).toHaveBeenCalledTimes(1);
hc2.recordStatus('svc1', makeDown());
expect(spy).toHaveBeenCalledTimes(2);
});
test('UP_THRESHOLD env var greater than 1 is honored', () => {
process.env.HEALTH_UP_THRESHOLD = '2';
jest.resetModules();
const hc2 = require('../src/monitoring/health-checker');
hc2.displayedStatus = new Map();
hc2.consecutiveSinceChange = new Map();
hc2.currentStatus = new Map();
hc2.history = {};
hc2.removeAllListeners('status-check');
const spy = jest.fn();
hc2.on('status-check', spy);
hc2.recordStatus('svc1', makeDown());
hc2.recordStatus('svc1', makeUp());
expect(spy).toHaveBeenCalledTimes(1);
expect(hc2.displayedStatus.get('svc1').status).toBe('down');
hc2.recordStatus('svc1', makeUp());
expect(spy).toHaveBeenCalledTimes(2);
expect(hc2.displayedStatus.get('svc1').status).toBe('up');
});
test.each(['not-a-number', '0', '-2', '1.5'])('malformed UP_THRESHOLD %s falls back to 1', value => {
process.env.HEALTH_UP_THRESHOLD = value;
jest.resetModules();
const hc2 = require('../src/monitoring/health-checker');
hc2.displayedStatus = new Map();
hc2.consecutiveSinceChange = new Map();
hc2.currentStatus = new Map();
hc2.history = {};
hc2.removeAllListeners('status-check');
const spy = jest.fn();
hc2.on('status-check', spy);
hc2.recordStatus('svc1', makeDown());
hc2.recordStatus('svc1', makeUp());
expect(spy).toHaveBeenCalledTimes(2);
expect(hc2.displayedStatus.get('svc1').status).toBe('up');
});
test('removeService clears hysteresis state before the same ID is re-added', () => {
hc.config.services.svc1 = { name: 'Service 1' };
hc.recordStatus('svc1', makeUp());
hc.recordStatus('svc1', makeDown());
expect(hc.displayedStatus.has('svc1')).toBe(true);
expect(hc.consecutiveSinceChange.get('svc1')).toBe(1);
hc.consecutiveFailures.set('svc1', 3);
const timer = setTimeout(() => {}, 60_000);
hc.serviceTimers.set('svc1', timer);
hc.saveConfig = jest.fn();
hc.removeService('svc1');
expect(hc.displayedStatus.has('svc1')).toBe(false);
expect(hc.consecutiveSinceChange.has('svc1')).toBe(false);
expect(hc.currentStatus.has('svc1')).toBe(false);
expect(hc.consecutiveFailures.has('svc1')).toBe(false);
expect(hc.serviceTimers.has('svc1')).toBe(false);
hc.config.services.svc1 = { name: 'Service 1 re-added' };
const emitSpyAfterReAdd = jest.fn();
hc.on('status-check', emitSpyAfterReAdd);
hc.recordStatus('svc1', makeDown());
expect(emitSpyAfterReAdd).toHaveBeenCalledTimes(1);
expect(hc.displayedStatus.get('svc1').status).toBe('down');
expect(hc.consecutiveSinceChange.has('svc1')).toBe(false);
});
});
@@ -0,0 +1,186 @@
/**
* Tests for DC-090: outage incidents follow the DISPLAYED (post-hysteresis)
* status the same signal that flips the dashboard badge.
*
* - A single raw "down" blip that hysteresis suppresses opens NO outage
* incident (the DC-089-noted raw-transition bug).
* - A suppressed blip does not resolve a real open outage (UP_THRESHOLD=2).
* - DOWN_THRESHOLD consecutive downs open exactly ONE outage incident.
* - The incident payload carries the displayed snapshot, not the raw probe.
* - Direct callers without hysteresis state keep legacy raw semantics.
*
* The probe() helper replicates checkService's exact call order: capture the
* pre-probe raw + displayed state, recordStatus (updates both maps), then
* checkForIncidents with both previous states.
*/
'use strict';
const path = require('path');
const fs = require('fs');
const os = require('os');
// Use an isolated data dir so test history doesn't pollute the real one.
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dashcaddy-incpar-'));
process.env.HEALTH_DATA_DIR = tmpDir;
process.env.HEALTH_CONFIG_FILE = path.join(tmpDir, 'health-config.json');
process.env.HEALTH_HISTORY_FILE = path.join(tmpDir, 'health-history.json');
// Module exports a singleton instance, not a class. Reset per-test state by
// replacing the relevant maps on the singleton in beforeEach.
const healthCheckerSingleton = require('../src/monitoring/health-checker');
const originalDownThreshold = process.env.HEALTH_DOWN_THRESHOLD;
const originalUpThreshold = process.env.HEALTH_UP_THRESHOLD;
function restoreEnv(name, value) {
if (value === undefined) delete process.env[name];
else process.env[name] = value;
}
function makeUp(serviceId = 'svc1') {
return {
serviceId,
timestamp: new Date().toISOString(),
status: 'up',
responseTime: 50,
statusCode: 200,
message: 'Service is healthy',
details: { headers: {}, bodyLength: 12 }
};
}
function makeDown(serviceId = 'svc1') {
return {
serviceId,
timestamp: new Date().toISOString(),
status: 'down',
responseTime: 50,
statusCode: 500,
message: 'fail',
details: { headers: {}, bodyLength: 0 }
};
}
describe('DC-090: outage incidents follow the displayed (hysteresis) status', () => {
let hc;
let incidentCreatedSpy;
let incidentResolvedSpy;
beforeEach(() => {
healthCheckerSingleton.displayedStatus = new Map();
healthCheckerSingleton.consecutiveSinceChange = new Map();
healthCheckerSingleton.currentStatus = new Map();
healthCheckerSingleton.history = {};
healthCheckerSingleton.incidents = [];
healthCheckerSingleton.removeAllListeners('incident-created');
healthCheckerSingleton.removeAllListeners('incident-resolved');
incidentCreatedSpy = jest.fn();
incidentResolvedSpy = jest.fn();
healthCheckerSingleton.on('incident-created', incidentCreatedSpy);
healthCheckerSingleton.on('incident-resolved', incidentResolvedSpy);
hc = healthCheckerSingleton;
});
afterEach(() => {
restoreEnv('HEALTH_DOWN_THRESHOLD', originalDownThreshold);
restoreEnv('HEALTH_UP_THRESHOLD', originalUpThreshold);
});
afterAll(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
// Replicates checkService's record+incident sequence for one raw probe.
function probe(status, config = {}) {
const previousStatus = hc.currentStatus.get(status.serviceId);
const previousDisplayed = hc.displayedStatus.get(status.serviceId) || null;
hc.recordStatus(status.serviceId, status);
hc.checkForIncidents(status.serviceId, status, config, previousStatus, previousDisplayed);
}
test('a single down blip between two ups opens NO outage incident', () => {
probe(makeUp()); // baseline: displayed up
probe(makeDown()); // blip — hysteresis keeps displayed up
probe(makeUp()); // recovered
expect(hc.incidents).toHaveLength(0);
expect(incidentCreatedSpy).not.toHaveBeenCalled();
});
test('DOWN_THRESHOLD consecutive downs open exactly one outage incident (critical)', () => {
probe(makeUp());
probe(makeDown()); // counter=1, displayed still up
probe(makeDown()); // counter=2 → displayed flips down → incident
expect(hc.incidents).toHaveLength(1);
const incident = hc.incidents[0];
expect(incident.type).toBe('outage');
expect(incident.severity).toBe('critical');
expect(incident.status).toBe('open');
expect(incidentCreatedSpy).toHaveBeenCalledTimes(1);
probe(makeDown()); // still down — no new transition, no second incident
expect(hc.incidents).toHaveLength(1);
expect(incident.occurrences).toBe(1); // occurrences count displayed flips, not raw probes
expect(incidentCreatedSpy).toHaveBeenCalledTimes(1);
});
test('the outage incident payload carries the displayed snapshot, not the raw blip', () => {
probe(makeUp());
const blip = makeDown();
blip.statusCode = 599;
probe(blip); // suppressed blip — must not appear in any incident
probe(makeDown()); // flip
expect(hc.incidents).toHaveLength(1);
// The incident's details snapshot is the probe that FLIPPED the displayed
// state (the second down), not the earlier suppressed blip.
expect(hc.incidents[0].details.statusCode).not.toBe(599);
});
test('a suppressed up blip does not resolve a real open outage (UP_THRESHOLD=2)', () => {
process.env.HEALTH_UP_THRESHOLD = '2';
jest.resetModules();
const hc2 = require('../src/monitoring/health-checker');
hc2.displayedStatus = new Map();
hc2.consecutiveSinceChange = new Map();
hc2.currentStatus = new Map();
hc2.history = {};
hc2.incidents = [];
hc2.removeAllListeners('incident-created');
hc2.removeAllListeners('incident-resolved');
const p2 = (status) => {
const prevRaw = hc2.currentStatus.get(status.serviceId);
const prevDisp = hc2.displayedStatus.get(status.serviceId) || null;
hc2.recordStatus(status.serviceId, status);
hc2.checkForIncidents(status.serviceId, status, {}, prevRaw, prevDisp);
};
p2(makeUp());
p2(makeDown());
p2(makeDown()); // displayed down → outage opens
expect(hc2.incidents).toHaveLength(1);
expect(hc2.incidents[0].status).toBe('open');
p2(makeUp()); // counter=1 < UP_THRESHOLD=2 → displayed still down
expect(hc2.displayedStatus.get('svc1').status).toBe('down');
expect(hc2.incidents[0].status).toBe('open'); // NOT resolved by the blip
p2(makeUp()); // counter=2 → displayed up → incident resolves
expect(hc2.displayedStatus.get('svc1').status).toBe('up');
expect(hc2.incidents[0].status).toBe('resolved');
});
test('legacy direct callers (no displayed state) keep raw transition semantics', () => {
hc.currentStatus.set('svc1', { status: 'up' });
const status = { status: 'down', timestamp: new Date().toISOString(), responseTime: 100 };
hc.checkForIncidents('svc1', status, {}); // 4-arg call, no previousDisplayed
expect(hc.incidents).toHaveLength(1);
expect(hc.incidents[0].type).toBe('outage');
});
test('slow-response detection still fires per-probe regardless of hysteresis', () => {
const slowUp = makeUp();
slowUp.responseTime = 6000;
probe(slowUp, { slowResponseThreshold: 5000 });
expect(hc.incidents.some(i => i.type === 'slow-response')).toBe(true);
});
});
@@ -203,6 +203,55 @@ describe('HealthChecker', () => {
expect(result.error).toBe('ECONNREFUSED');
});
it('opens and resolves an outage incident across real checkService transitions', async () => {
// DC-090: incidents follow the DISPLAYED (post-hysteresis) status.
// DOWN_THRESHOLD defaults to 2, so it takes two consecutive failed
// probes to flip displayed down and open the outage; one up probe
// (UP_THRESHOLD=1) resolves it.
healthChecker._doRequest = jest.fn()
.mockResolvedValueOnce({ healthy: true, statusCode: 200, message: 'ok', details: {} })
.mockResolvedValueOnce({ healthy: false, statusCode: 500, message: 'down', details: {} })
.mockResolvedValueOnce({ healthy: false, statusCode: 500, message: 'down', details: {} })
.mockResolvedValueOnce({ healthy: true, statusCode: 200, message: 'ok', details: {} });
const config = { url: 'http://test.local' };
await healthChecker.checkService('svc1', config);
await healthChecker.checkService('svc1', config);
expect(healthChecker.incidents).toHaveLength(0); // one down alone: suppressed blip
await healthChecker.checkService('svc1', config); // second down flips displayed → open
expect(healthChecker.incidents).toHaveLength(1);
expect(healthChecker.incidents[0]).toMatchObject({
serviceId: 'svc1',
type: 'outage',
status: 'open'
});
await healthChecker.checkService('svc1', config); // up resolves
expect(healthChecker.incidents[0].status).toBe('resolved');
expect(healthChecker.incidents[0].resolvedAt).toBeDefined();
});
it('does not resurrect state when an in-flight probe resolves after removal', async () => {
let resolveProbe;
healthChecker.config.services.svc1 = { url: 'http://test.local' };
healthChecker._doRequest = jest.fn(() => new Promise(resolve => {
resolveProbe = resolve;
}));
const pending = healthChecker.checkService('svc1', healthChecker.config.services.svc1);
healthChecker.saveConfig = jest.fn();
healthChecker.removeService('svc1');
resolveProbe({ healthy: true, statusCode: 200, message: 'late', details: {} });
await pending;
expect(healthChecker.currentStatus.has('svc1')).toBe(false);
expect(healthChecker.displayedStatus.has('svc1')).toBe(false);
expect(healthChecker.consecutiveFailures.has('svc1')).toBe(false);
expect(healthChecker.history.svc1).toBeUndefined();
expect(healthChecker.incidents).toEqual([]);
});
it('increments consecutive failures on error', async () => {
healthChecker._doRequest = jest.fn().mockRejectedValue(new Error('fail'));
@@ -549,6 +598,113 @@ describe('HealthChecker', () => {
});
});
describe('DC-088: removeService generation tombstones + incident closure', () => {
it('does not leak a serviceGenerations entry and records a tombstone', () => {
healthChecker.configureService('svc1', { url: 'http://test.local' });
expect(healthChecker.serviceGenerations.has('svc1')).toBe(true);
healthChecker.removeService('svc1');
expect(healthChecker.serviceGenerations.has('svc1')).toBe(false);
const tomb = healthChecker.removedGenerations.get('svc1');
expect(tomb).toBeDefined();
expect(tomb.generation).toBeGreaterThan(0);
expect(tomb.removedAt).toBeGreaterThan(0);
});
it('re-added service gets a strictly higher generation (no ABA)', () => {
healthChecker.configureService('svc1', { url: 'http://test.local' });
const gen1 = healthChecker.serviceGenerations.get('svc1');
healthChecker.removeService('svc1');
healthChecker.configureService('svc1', { url: 'http://test.local/v2' });
const gen2 = healthChecker.serviceGenerations.get('svc1');
expect(gen2).toBeGreaterThan(gen1);
expect(healthChecker.removedGenerations.has('svc1')).toBe(false);
});
it('closes open incidents for the removed service as resolved', () => {
healthChecker.saveConfig = jest.fn();
healthChecker.incidents.push({
id: 'incident-test-1',
serviceId: 'svc1',
type: 'outage',
status: 'open',
createdAt: new Date(Date.now() - 60_000).toISOString()
});
healthChecker.incidents.push({
id: 'incident-other',
serviceId: 'svc2',
type: 'outage',
status: 'open',
createdAt: new Date(Date.now() - 60_000).toISOString()
});
const resolvedSpy = jest.fn();
healthChecker.on('incident-resolved', resolvedSpy);
healthChecker.removeService('svc1');
const closed = healthChecker.incidents.find(i => i.id === 'incident-test-1');
expect(closed.status).toBe('resolved');
expect(closed.resolvedBy).toBe('service-removed');
expect(closed.resolvedAt).toBeDefined();
expect(closed.duration).toBeGreaterThan(0);
expect(healthChecker.incidents.find(i => i.id === 'incident-other').status).toBe('open');
expect(resolvedSpy).toHaveBeenCalledTimes(1);
});
it('in-flight probe captured before removal is discarded via tombstone', async () => {
let resolveProbe;
healthChecker.config.services.svc1 = { url: 'http://test.local' };
healthChecker._doRequest = jest.fn(() => new Promise(resolve => {
resolveProbe = resolve;
}));
const pending = healthChecker.checkService('svc1', healthChecker.config.services.svc1);
healthChecker.saveConfig = jest.fn();
healthChecker.removeService('svc1');
resolveProbe({ healthy: true, statusCode: 200, message: 'late', details: {} });
await pending;
expect(healthChecker.currentStatus.has('svc1')).toBe(false);
expect(healthChecker.consecutiveFailures.has('svc1')).toBe(false);
});
it('a rejected in-flight probe after removal does not re-create failure state', async () => {
let rejectProbe;
healthChecker.config.services.svc1 = { url: 'http://test.local' };
healthChecker._doRequest = jest.fn(() => new Promise((resolve, reject) => {
rejectProbe = reject;
}));
const pending = healthChecker.checkService('svc1', healthChecker.config.services.svc1);
healthChecker.saveConfig = jest.fn();
healthChecker.removeService('svc1');
rejectProbe(new Error('late failure'));
await pending;
expect(healthChecker.consecutiveFailures.has('svc1')).toBe(false);
expect(healthChecker.currentStatus.has('svc1')).toBe(false);
});
it('sweeps expired tombstones in cleanupHistory', () => {
healthChecker.removedGenerations.set('svc1', {
generation: 1,
removedAt: Date.now() - 60 * 60 * 1000 // 1h ago, TTL default 10m
});
healthChecker.removedGenerations.set('svc2', {
generation: 2,
removedAt: Date.now() // fresh
});
healthChecker.cleanupHistory();
expect(healthChecker.removedGenerations.has('svc1')).toBe(false);
expect(healthChecker.removedGenerations.has('svc2')).toBe(true);
});
});
describe('cleanupHistory', () => {
it('removes entries older than retention period', () => {
const old = new Date(Date.now() - 35 * 24 * 60 * 60 * 1000).toISOString(); // 35 days ago
@@ -26,6 +26,19 @@ jest.mock('dockerode', () => {
function buildApp({ configOk = true, servicesOk = true, dockerOk = true, caddyOk = true } = {}) {
process.env.MOCK_DOCKER_DOWN = dockerOk ? '0' : '1';
// DC-087 — mirror src/app.js faithfully: the caddy check goes through
// fetchT (which injects the Origin header Caddy's enforce_origin allowlist
// requires), and is MOCKED so the suite is hermetic — no live request to a
// real Caddy admin on :2019. The previous raw-`fetch` mirror sent an
// Origin-less probe to the LIVE admin whenever the full suite ran on the
// prod host (adversarial cron every 30 min): 12 journal 403 lines per run,
// ~700/day of `client is not allowed to access from origin ''` noise,
// plus a false checks.caddy.ok=false in the mirrored readiness payload.
const fetchT = jest.spyOn(require('../src/utils/http'), 'fetchT')
.mockImplementation(async () => (caddyOk
? { ok: true, status: 200 }
: { ok: false, status: 403 }));
const app = express();
const config = {
CONFIG_FILE: '/tmp/dc-test-config.json',
@@ -103,9 +116,13 @@ function buildApp({ configOk = true, servicesOk = true, dockerOk = true, caddyOk
allOk = false;
}
// DC-087 — mirror src/app.js exactly (fetchT, not raw fetch). fetchT is
// mocked at buildApp() scope, so this stays hermetic: no live probe to a
// real Caddy admin (the old raw-fetch mirror 403-spammed the prod journal
// every time the adversarial cron ran the full suite on this host).
try {
const caddyUrl = config.CADDY_ADMIN_URL || 'http://localhost:2019';
const response = await fetch(`${caddyUrl}/config/apps/http/servers/srv0/listen`, { signal: AbortSignal.timeout(10000) });
const response = await fetchT(`${caddyUrl}/config/apps/http/servers/srv0/listen`, {}, 10000);
checks.caddy = { ok: response.ok, status: response.status };
if (!response.ok) allOk = false;
} catch (e) {
@@ -33,9 +33,18 @@ jest.mock('dockerode', () => {
// Mirror the canonical handler block from src/app.js — if this drifts from
// the real handler, these tests will start failing and force a sync.
function buildApp({ configOk = true, servicesOk = true, dockerOk = true } = {}) {
function buildApp({ configOk = true, servicesOk = true, dockerOk = true, caddyOk = true } = {}) {
process.env.MOCK_DOCKER_DOWN = dockerOk ? '0' : '1';
// DC-087 — mirror src/app.js: caddy check via fetchT (Origin-injecting),
// mocked here so the suite is hermetic. The old raw-fetch mirror probed the
// LIVE Caddy admin on :2019 whenever the full suite ran on the prod host
// (adversarial cron): Origin-less → 403 → 12 journal error lines per run.
const fetchT = jest.spyOn(require('../src/utils/http'), 'fetchT')
.mockImplementation(async () => (caddyOk
? { ok: true, status: 200 }
: { ok: false, status: 403 }));
const app = express();
const config = {
CONFIG_FILE: '/tmp/dc-test-config.json',
@@ -108,8 +117,10 @@ function buildApp({ configOk = true, servicesOk = true, dockerOk = true } = {})
allOk = false;
}
try {
// DC-087 — mirror src/app.js exactly: fetchT (mocked above), not raw
// fetch. Hermetic: no live request to a real Caddy admin.
const caddyUrl = config.CADDY_ADMIN_URL || 'http://localhost:2019';
const response = await fetch(`${caddyUrl}/config/apps/http/servers/srv0/listen`, { signal: AbortSignal.timeout(10000) });
const response = await fetchT(`${caddyUrl}/config/apps/http/servers/srv0/listen`, {}, 10000);
checks.caddy = { ok: response.ok, status: response.status };
if (!response.ok) allOk = false;
} catch (e) {
@@ -214,4 +214,99 @@ describe('NotificationManager', () => {
nm.stopHealthDaemon();
expect(nm.healthDaemonInterval).toBeNull();
});
// ── DC-092: event alias folding + legacy config canonicalization ──────────
test('DC-092: send() folds camelCase aliases onto canonical kebab keys', async () => {
// deploymentSuccess (emitted by routes/apps/deploy.js) previously hit a
// gate miss (no such key in events) and the notification was dropped.
const result = await nm.send('deploymentSuccess', { text: 'deployed' });
expect(result.error).toBeUndefined();
expect(nm.getHistory()[0].event).toBe('deploy-success');
});
test('DC-092: send() accepts the canonical kebab spelling too', async () => {
const result = await nm.send('deploy-success', { text: 'deployed' });
expect(result.error).toBeUndefined();
expect(nm.getHistory()[0].event).toBe('deploy-success');
});
test("DC-092: send('test') bypasses the events gate (Test button works)", async () => {
const result = await nm.send('test', { text: 'Test Notification' });
// No providers are enabled in the default config, so results is empty —
// but the gate must NOT return 'Event test not enabled' like it used to.
expect(result.error).toBeUndefined();
expect(nm.getHistory()[0].event).toBe('test');
});
test('DC-092: send() still gates unknown and disabled events', async () => {
const unknown = await nm.send('some-unknown-event', { text: 'x' });
expect(unknown.success).toBe(false);
expect(unknown.error).toMatch(/not enabled/i);
nm.config.events['container-down'] = false;
const disabled = await nm.send('container-down', { text: 'x' });
expect(disabled.success).toBe(false);
expect(disabled.error).toMatch(/not enabled/i);
});
test('DC-092: DEFAULT_CONFIG includes deploy/auto-restart events', () => {
// Regression pin: these were absent entirely, so deploy notifications
// were dropped for every install regardless of UI toggles.
expect(nm.config.events['deploy-success']).toBe(true);
expect(nm.config.events['deploy-failed']).toBe(true);
expect(nm.config.events['auto-restart']).toBe(true);
});
test('DC-092: legacy config with user/pass and camelCase events canonicalizes on load', () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue(JSON.stringify({
enabled: true,
providers: {
email: {
enabled: true,
host: 'smtp.test',
port: 465,
secure: 'false', // legacy string — must normalize to boolean false
to: 'me@test',
from: 'from@test',
user: 'legacy-user',
pass: 'legacy-pass',
}
},
events: {
containerDown: false,
deploymentSuccess: false,
}
}));
const loaded = new NotificationManager({
NOTIFICATIONS_FILE: NOTIF_FILE,
log: { error: jest.fn(), info: jest.fn(), warn: jest.fn() },
});
const email = loaded.getConfig().providers.email;
expect(email.username).toBe('legacy-user');
expect(email.password).toBe('legacy-pass');
expect(email.user).toBeUndefined();
expect(email.pass).toBeUndefined();
expect(email.secure).toBe(false);
const events = loaded.getConfig().events;
expect(events['container-down']).toBe(false);
expect(events['deploy-success']).toBe(false);
expect(events.containerDown).toBeUndefined();
expect(events.deploymentSuccess).toBeUndefined();
});
test('DC-092: canonical keys win when both spellings exist in a legacy file', () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue(JSON.stringify({
providers: { email: { user: 'legacy', username: 'canonical' } },
events: { containerDown: false, 'container-down': true },
}));
const loaded = new NotificationManager({
NOTIFICATIONS_FILE: NOTIF_FILE,
log: { error: jest.fn(), info: jest.fn(), warn: jest.fn() },
});
expect(loaded.getConfig().providers.email.username).toBe('canonical');
expect(loaded.getConfig().events['container-down']).toBe(true);
});
});
@@ -112,6 +112,7 @@ function createApp(depsOverride = {}) {
errorResponse: jest.fn(),
log,
renewCSRFToken,
siteConfig: { tld: '.sami', dashboardHost: 'status.sami' },
...depsOverride,
};
@@ -299,7 +300,7 @@ describe('TOTP Auth Routes — DC-006 Integration Test', () => {
it('returns 200 + creates new session + rotates CSRF on valid code (BACKLOG: "valid TOTP → session token → authenticated request succeeds")', async () => {
const secret = await setupTOTP();
const token = authenticator.generate(secret);
const res = await request(app).post('/api/totp/verify').send({ code: token });
const res = await request(app).post('/api/totp/verify').send({ code: token, serviceId: 'plex' });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.message).toMatch(/Authenticated successfully/);
@@ -308,8 +309,29 @@ describe('TOTP Auth Routes — DC-006 Integration Test', () => {
expect(deps.session.create).toHaveBeenCalled();
expect(deps.session.setCookie).toHaveBeenCalled();
expect(deps.session.createHandoffToken).toHaveBeenCalledTimes(1);
expect(deps.session.createHandoffToken).toHaveBeenCalledWith('plex.sami');
expect(deps.renewCSRFToken).toHaveBeenCalled();
});
it('does not issue an unbound handoff token for a dashboard-only login', async () => {
const secret = await setupTOTP();
const token = authenticator.generate(secret);
const res = await request(app).post('/api/totp/verify').send({ code: token });
expect(res.status).toBe(200);
expect(res.body.ssoToken).toBeNull();
expect(deps.session.createHandoffToken).not.toHaveBeenCalled();
});
it('rejects an invalid handoff service ID before issuing a token', async () => {
const secret = await setupTOTP();
const token = authenticator.generate(secret);
const res = await request(app).post('/api/totp/verify').send({ code: token, serviceId: 'plex.sami' });
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/Invalid service ID/);
expect(deps.session.createHandoffToken).not.toHaveBeenCalled();
});
});
// ────────────────────────────────────────────────────────────────────
@@ -450,7 +472,7 @@ describe('TOTP Auth Routes — DC-006 Integration Test', () => {
// 4. Re-login via /totp/verify (the "login" path)
const loginCode = authenticator.generate(secret);
const loginRes = await request(app).post('/api/totp/verify').send({ code: loginCode });
const loginRes = await request(app).post('/api/totp/verify').send({ code: loginCode, serviceId: 'plex' });
expect(loginRes.status).toBe(200);
expect(loginRes.body.csrfToken).toBeDefined();
expect(loginRes.body.ssoToken).toBe('mock-sso-handoff-token');
@@ -0,0 +1,267 @@
/**
* DC-092: notifications config contract tests (route level).
*
* The settings UI and the backend drifted apart in three ways, all of which
* made user-facing features silently dead:
* 1. UI sent email.user/email.pass; backend read username/password
* SMTP auth never applied for UI-saved configs.
* 2. UI sent camelCase event keys (containerDown); the send() gate read
* kebab-case keys (container-down) event toggles were cosmetic.
* 3. deploy-success/deploy-failed/auto-restart were missing from DEFAULT
* events deploy + auto-restart notifications always dropped, and
* 'test' was gated too the Test button was a no-op.
* 4. Non-boolean enabled/secure values (string "false") persisted as-is and
* coerced truthy (!!secure) silently forcing TLS.
* 5. UI password field roundtrip: GET /config omitted port/secure/to/
* username, and an empty password on save clobbered the stored one.
*
* These tests pin the FIXED contract: alias normalization, strict booleans,
* event-key folding, non-destructive credential merge, redacted GET fields.
*/
'use strict';
const express = require('express');
const request = require('supertest');
// Stub notification manager: in-memory config object, real merge semantics
// are exercised through the route; manager-level canonicalization has its
// own tests in notification-manager.test.js.
function makeStubNotification(initial) {
const nm = {
config: initial,
getConfig() { return this.config; },
async saveConfig() { this.saved = JSON.parse(JSON.stringify(this.config)); return true; },
startHealthDaemon: jest.fn(),
stopHealthDaemon: jest.fn(),
};
return nm;
}
function buildApp(notification) {
const app = express();
app.use(express.json());
const notificationRoutes = require('../../routes/notifications');
app.use('/api/v1/notifications', notificationRoutes({
notification,
asyncHandler: (fn) => (req, res, next) => Promise.resolve(fn(req, res)).catch(next),
ok: (res, data) => res.json({ success: true, ...data }),
}));
// Inline error handler (same pattern as sites-dc074.routes.test.js): maps
// AppError.statusCode to the HTTP status and surfaces err.message.
app.use((err, req, res, next) => {
const status = err.statusCode || 500;
res.status(status).json({
error: err.message || 'Internal Server Error',
code: err.code || null,
});
});
return app;
}
const DEFAULTS = {
enabled: true,
providers: {
discord: { enabled: false, webhookUrl: '' },
telegram: { enabled: false, botToken: '', chatId: '' },
ntfy: { enabled: false, topic: '', serverUrl: 'https://ntfy.sh' },
email: { enabled: false, host: '', port: 587, to: '', from: '', username: '', password: '' },
},
events: {
'container-down': true,
'container-up': false,
'alert': true,
'backup-complete': true,
'backup-failed': true,
'update-available': true,
'deploy-success': true,
'deploy-failed': true,
'auto-restart': true,
},
healthCheck: { enabled: false },
};
function freshConfig() {
return JSON.parse(JSON.stringify(DEFAULTS));
}
describe('DC-092: POST /config field aliases and typing', () => {
test('UI spelling email.user/email.pass normalizes onto username/password', async () => {
const nm = makeStubNotification(freshConfig());
const app = buildApp(nm);
const res = await request(app)
.post('/api/v1/notifications/config')
.send({ providers: { email: { user: 'svc@example.com', pass: 'app-secret' } } });
expect(res.status).toBe(200);
expect(nm.config.providers.email.username).toBe('svc@example.com');
expect(nm.config.providers.email.password).toBe('app-secret');
expect(nm.config.providers.email.user).toBeUndefined();
expect(nm.config.providers.email.pass).toBeUndefined();
});
test('explicit username/password wins over user/pass aliases', async () => {
const nm = makeStubNotification(freshConfig());
const app = buildApp(nm);
await request(app)
.post('/api/v1/notifications/config')
.send({ providers: { email: { user: 'legacy@x.com', pass: 'old', username: 'modern@x.com', password: 'new' } } });
expect(nm.config.providers.email.username).toBe('modern@x.com');
expect(nm.config.providers.email.password).toBe('new');
});
test('string "false" for secure is rejected, not coerced truthy', async () => {
const nm = makeStubNotification(freshConfig());
const app = buildApp(nm);
const res = await request(app)
.post('/api/v1/notifications/config')
.send({ providers: { email: { secure: 'false' } } });
expect(res.status).toBe(400);
expect(nm.config.providers.email.secure).toBeUndefined();
});
test('string enabled for any provider is rejected', async () => {
const nm = makeStubNotification(freshConfig());
const app = buildApp(nm);
for (const prov of ['discord', 'telegram', 'ntfy', 'email']) {
const res = await request(app)
.post('/api/v1/notifications/config')
.send({ providers: { [prov]: { enabled: 'true' } } });
expect(res.status).toBe(400);
}
const top = await request(app)
.post('/api/v1/notifications/config')
.send({ enabled: 'true' });
expect(top.status).toBe(400);
});
test('real booleans pass and persist', async () => {
const nm = makeStubNotification(freshConfig());
const app = buildApp(nm);
const res = await request(app)
.post('/api/v1/notifications/config')
.send({ enabled: false, providers: { email: { secure: true } } });
expect(res.status).toBe(200);
expect(nm.config.enabled).toBe(false);
expect(nm.config.providers.email.secure).toBe(true);
});
test('SMTP port bounds enforced (0, 65536, non-integer rejected)', async () => {
const nm = makeStubNotification(freshConfig());
const app = buildApp(nm);
for (const bad of [0, 65536, 58.5, 'abc']) {
const res = await request(app)
.post('/api/v1/notifications/config')
.send({ providers: { email: { port: bad } } });
expect(res.status).toBe(400);
}
const good = await request(app)
.post('/api/v1/notifications/config')
.send({ providers: { email: { port: 465 } } });
expect(good.status).toBe(200);
expect(nm.config.providers.email.port).toBe(465);
});
});
describe('DC-092: POST /config event-key folding', () => {
test('camelCase event keys fold onto canonical kebab keys', async () => {
const nm = makeStubNotification(freshConfig());
const app = buildApp(nm);
const res = await request(app)
.post('/api/v1/notifications/config')
.send({ events: { containerDown: false, deploymentSuccess: false, resourceAlert: false } });
expect(res.status).toBe(200);
expect(nm.config.events['container-down']).toBe(false);
expect(nm.config.events['deploy-success']).toBe(false);
expect(nm.config.events['alert']).toBe(false);
// legacy camelCase keys must NOT be stored
expect(nm.config.events.containerDown).toBeUndefined();
expect(nm.config.events.deploymentSuccess).toBeUndefined();
expect(nm.config.events.resourceAlert).toBeUndefined();
});
test('canonical kebab keys accepted directly', async () => {
const nm = makeStubNotification(freshConfig());
const app = buildApp(nm);
const res = await request(app)
.post('/api/v1/notifications/config')
.send({ events: { 'container-down': false, 'auto-restart': false } });
expect(res.status).toBe(200);
expect(nm.config.events['container-down']).toBe(false);
expect(nm.config.events['auto-restart']).toBe(false);
});
test('non-boolean event values rejected', async () => {
const nm = makeStubNotification(freshConfig());
const app = buildApp(nm);
const res = await request(app)
.post('/api/v1/notifications/config')
.send({ events: { 'container-down': 'yes' } });
expect(res.status).toBe(400);
});
});
describe('DC-092: POST /config non-destructive credential merge', () => {
test('empty password does not clobber stored password', async () => {
const cfg = freshConfig();
cfg.providers.email.username = 'svc@example.com';
cfg.providers.email.password = 'stored-secret';
const nm = makeStubNotification(cfg);
const app = buildApp(nm);
const res = await request(app)
.post('/api/v1/notifications/config')
.send({ providers: { email: { host: 'smtp.example.com', password: '' } } });
expect(res.status).toBe(200);
expect(nm.config.providers.email.password).toBe('stored-secret');
expect(nm.config.providers.email.host).toBe('smtp.example.com');
});
test('empty username does not clobber stored username', async () => {
const cfg = freshConfig();
cfg.providers.email.username = 'svc@example.com';
const nm = makeStubNotification(cfg);
const app = buildApp(nm);
await request(app)
.post('/api/v1/notifications/config')
.send({ providers: { email: { username: '' } } });
expect(nm.config.providers.email.username).toBe('svc@example.com');
});
test('non-empty password overwrites', async () => {
const cfg = freshConfig();
cfg.providers.email.password = 'old';
const nm = makeStubNotification(cfg);
const app = buildApp(nm);
await request(app)
.post('/api/v1/notifications/config')
.send({ providers: { email: { password: 'rotated' } } });
expect(nm.config.providers.email.password).toBe('rotated');
});
});
describe('DC-092: GET /config redaction and roundtrip fields', () => {
test('returns port/secure/to/username/hasPassword but never the password', async () => {
const cfg = freshConfig();
cfg.providers.email = {
enabled: true,
host: 'smtp.example.com',
port: 465,
secure: true,
to: 'admin@example.com',
from: 'DashCaddy <noreply@example.com>',
username: 'svc@example.com',
password: 'super-secret',
};
const nm = makeStubNotification(cfg);
const app = buildApp(nm);
const res = await request(app).get('/api/v1/notifications/config');
expect(res.status).toBe(200);
const email = res.body.config.providers.email;
expect(email.port).toBe(465);
expect(email.secure).toBe(true);
expect(email.to).toBe('admin@example.com');
expect(email.username).toBe('svc@example.com');
expect(email.hasPassword).toBe(true);
expect(JSON.stringify(res.body)).not.toContain('super-secret');
expect(res.body.config.providers.email.password).toBeUndefined();
});
});
@@ -288,6 +288,21 @@ describe('Services Routes', () => {
expect(res.status).toBe(200);
expect(res.body.hasApiKey).toBe(true);
});
it('requires both username and password before reporting Basic Auth ready', async () => {
const credentialManager = {
store: jest.fn(),
retrieve: jest.fn().mockImplementation((key) => {
if (key === 'service.radarr.username') return Promise.resolve('admin');
return Promise.resolve(null);
}),
delete: jest.fn(),
};
const { app } = createApp({ credentialManager });
const res = await request(app).get('/api/services/radarr/credentials');
expect(res.status).toBe(200);
expect(res.body.hasBasicAuth).toBe(false);
});
});
// ===== SEEDHOST CREDENTIAL ENDPOINTS =====
@@ -50,6 +50,17 @@ describe('TOTP session cookie scope', () => {
expect(cookie).not.toMatch(/(?:^|;)\s*Domain=/i);
});
test('host-bound SSO token can only be redeemed on its intended service host', () => {
const session = buildSession();
const wrongHostToken = session.createHandoffToken('plex.sami');
expect(session.redeemHandoffToken(wrongHostToken, 'chat.sami')).toBe(false);
expect(session.redeemHandoffToken(wrongHostToken, 'plex.sami')).toBe(false);
const correctHostToken = session.createHandoffToken('plex.sami');
expect(session.redeemHandoffToken(correctHostToken, 'plex.sami')).toBe(true);
expect(session.redeemHandoffToken(correctHostToken, 'plex.sami')).toBe(false);
});
test('logout clears the host-only secure cookie', () => {
const session = buildSession();
const headers = {};
@@ -0,0 +1,483 @@
/**
* DC-083 -- Public share endpoint input hardening.
*
* The two CSRF-exempt public endpoints (POST /share/:token/subscribe +
* POST /share/:token/redeem-tailscale) accept untrusted body fields. The
* pre-fix code had three coupled bugs:
*
* 1. `email.includes('@')` accepted `@`, `a@`, `<script>@x.c`, and 10MB
* strings as "valid email" -- and the field was never even used after
* validation (the subscribe endpoint discarded it).
* 2. `typeof deviceId === 'string'` accepted arbitrary strings of any
* length, including CR/LF/NUL -- which fed straight into the Tailscale
* auth-key description string and the on-disk shares.json.
* 3. No rate-limit; the general limiter (1000/15min) was too generous for
* unauthenticated state-mutating endpoints.
*
* Fix: charset/length/control-char-bounded validators at the route layer
* AND at the store layer (defense-in-depth), plus a dedicated
* SHARE_PUBLIC rate-limit (30/15min) on the public endpoints.
*
* Coverage:
* - subscribe email: rejects bare @, missing TLD, oversized, CR/LF, shell
* metachars, control chars; accepts normal addresses; accepts OMITTED
* email (backwards-compatible with the original behavior).
* - subscribe email propagates to share-store subscriberEmails (capped 8).
* - redeem-tailscale deviceId: rejects CR/LF/NUL, oversized, empty,
* spaces, brackets, quotes; accepts Tailscale-style base64url+hphens;
* accepts OMITTED deviceId (treated as 'unknown').
* - Sanitized usedBy is what flows into the on-disk shares.json.
* - Rate-limit fires after the configured budget per IP.
* - Store-level defense: bypassing the route (direct store call) still
* rejects invalid inputs.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const express = require('express');
const request = require('supertest');
const { createShareStore } = require('../src/security/share-store');
function _tmpDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'dashcaddy-share-dc083-'));
}
function _cleanup(dir) {
try { fs.rmSync(dir, { recursive: true, force: true }); } catch {}
}
function _buildApp({ shareStore } = {}) {
const app = express();
app.use(express.json());
// No req.user injection -- the public endpoints must work without auth.
const shareRoutes = require('../routes/share');
app.use(shareRoutes({
shareStore,
licenseManager: { isPro: () => true, allowsLifetimeLicense: () => false },
tailscaleCoord: { createAuthKey: async () => ({ id: 'k', key: 'tskey-x' }) },
notificationManager: { sendEmail: async () => ({ messageId: 'fake' }) },
servicesStateManager: { get: async () => null, read: async () => [] },
servicesFile: null,
asyncHandler: (fn, _label) => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next),
log: { info() {}, warn() {}, error() {} },
}));
app.use((err, _req, res, _next) => {
if (err && err.statusCode) {
return res.status(err.statusCode).json({
success: false,
error: err.message,
code: err.code,
});
}
return res.status(500).json({ success: false, error: err && err.message });
});
return app;
}
// --------- Subscribe endpoint -- email validation ---------------------------------------------------------------------------------------
describe('DC-083: subscribe email validation', () => {
let dir, shareStore;
beforeEach(() => { dir = _tmpDir(); shareStore = createShareStore({ dataDir: dir }); });
afterEach(() => _cleanup(dir));
test('accepts omitted email (backwards-compatible)', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app).post(`/share/${issued.token}/subscribe`).send({});
expect(res.status).toBe(200);
expect(res.body.data.count).toBe(1);
});
test('accepts a well-formed email', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: 'subscriber@example.com' });
expect(res.status).toBe(200);
expect(res.body.data.count).toBe(1);
});
test('lowercases the email on capture', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: 'Subscriber@Example.COM' });
expect(res.status).toBe(200);
const raw = JSON.parse(fs.readFileSync(path.join(dir, 'shares.json'), 'utf8'));
const id = Object.keys(raw.shares)[0];
expect(raw.shares[id].subscriberEmails).toEqual(['subscriber@example.com']);
});
test('rejects bare @', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: '@' });
expect(res.status).toBe(400);
});
test('rejects missing local-part', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: '@example.com' });
expect(res.status).toBe(400);
});
test('rejects missing TLD', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: 'user@localhost' });
expect(res.status).toBe(400);
});
test('rejects single-char TLD', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: 'user@example.c' });
expect(res.status).toBe(400);
});
test('rejects CR/LF in email (CRLF-injection defense)', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: 'a@b.com\r\nX-Injected: yes' });
expect(res.status).toBe(400);
});
test('rejects NUL in email', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: 'a@b.com\x00hack' });
expect(res.status).toBe(400);
});
test('rejects oversized email (>254 chars)', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const longLocal = 'a'.repeat(250) + '@example.com';
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: longLocal });
expect(res.status).toBe(400);
});
test('rejects XSS-shape email', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: '<script>@x.com' });
expect(res.status).toBe(400);
});
test('rejects non-string email', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: 42 });
expect(res.status).toBe(400);
});
test('keeps subscriberEmails capped to 8 entries', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
for (let i = 0; i < 12; i++) {
await request(app)
.post(`/share/${issued.token}/subscribe`)
.send({ email: `user${i}@example.com` });
}
const raw = JSON.parse(fs.readFileSync(path.join(dir, 'shares.json'), 'utf8'));
const id = Object.keys(raw.shares)[0];
expect(raw.shares[id].subscriberEmails).toHaveLength(8);
// FIFO cap -- the first 4 got dropped, latest 8 remain.
expect(raw.shares[id].subscriberEmails[0]).toBe('user4@example.com');
expect(raw.shares[id].subscriberEmails[7]).toBe('user11@example.com');
});
test('omitted email does not write subscriberEmails', async () => {
const issued = await shareStore.issuePublic({ serviceId: 'plex' });
const app = _buildApp({ shareStore });
await request(app).post(`/share/${issued.token}/subscribe`).send({});
const raw = JSON.parse(fs.readFileSync(path.join(dir, 'shares.json'), 'utf8'));
const id = Object.keys(raw.shares)[0];
expect(raw.shares[id].subscriberEmails).toBeUndefined();
});
});
// --------- Redeem-tailscale endpoint -- deviceId validation ---------------------------------------------------------
describe('DC-083: redeem-tailscale deviceId validation', () => {
let dir, shareStore;
beforeEach(() => { dir = _tmpDir(); shareStore = createShareStore({ dataDir: dir }); });
afterEach(() => _cleanup(dir));
test('accepts Tailscale-style base64url ID', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: 'nodekey-abc123-def456' });
expect(res.status).toBe(200);
expect(res.body.data.redeemed).toBe(true);
});
test('accepts OMITTED deviceId (treated as "unknown")', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({});
expect(res.status).toBe(200);
const raw = JSON.parse(fs.readFileSync(path.join(dir, 'shares.json'), 'utf8'));
const id = Object.keys(raw.shares)[0];
expect(raw.shares[id].usedBy).toBe('unknown');
});
test('rejects CR/LF in deviceId (CRLF-injection defense)', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: 'nodekey\r\nX-Injected: yes' });
expect(res.status).toBe(400);
});
test('rejects NUL in deviceId', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: 'nodekey\x00hack' });
expect(res.status).toBe(400);
});
test('rejects oversized deviceId (>128 chars)', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
const long = 'a'.repeat(200);
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: long });
expect(res.status).toBe(400);
});
test('rejects empty string deviceId', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: '' });
expect(res.status).toBe(400);
});
test('rejects whitespace in deviceId', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: 'node key 1' });
expect(res.status).toBe(400);
});
test('rejects shell metachars in deviceId', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: 'nodekey; rm -rf /' });
expect(res.status).toBe(400);
});
test('rejects non-string deviceId', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: { evil: true } });
expect(res.status).toBe(400);
});
test('sanitized usedBy flows into the on-disk shares.json', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: 'node-abc.def-123' });
const raw = JSON.parse(fs.readFileSync(path.join(dir, 'shares.json'), 'utf8'));
const id = Object.keys(raw.shares)[0];
expect(raw.shares[id].usedBy).toBe('node-abc.def-123');
});
test('rejection does NOT mark the share used', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore });
const bad = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: 'node with spaces' });
expect(bad.status).toBe(400);
// A FOLLOW-UP valid redeem should still succeed.
const ok = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: 'node-clean' });
expect(ok.status).toBe(200);
});
});
// --------- Store-layer defense-in-depth (bypass the route, hit the store) ------------
describe('DC-083: store-layer defense-in-depth', () => {
let dir, store;
beforeEach(() => { dir = _tmpDir(); store = createShareStore({ dataDir: dir }); });
afterEach(() => _cleanup(dir));
test('recordPublicSubscribe rejects CRLF in email', async () => {
const issued = await store.issuePublic({ serviceId: 'svc' });
const r = await store.recordPublicSubscribe(issued.token, { email: 'a@b.com\r\nX: 1' });
expect(r.ok).toBe(false);
expect(r.reason).toBe('invalid_email');
});
test('recordPublicSubscribe rejects oversized email', async () => {
const issued = await store.issuePublic({ serviceId: 'svc' });
const r = await store.recordPublicSubscribe(issued.token, { email: 'a'.repeat(300) + '@x.com' });
expect(r.ok).toBe(false);
expect(r.reason).toBe('invalid_email');
});
test('recordTailscaleUse rejects CRLF in deviceId', async () => {
const issued = await store.issueTailscale({ serviceId: 'svc', email: 'a@b.com' });
const r = await store.recordTailscaleUse(issued.token, { deviceId: 'node\r\nhack' });
expect(r.ok).toBe(false);
expect(r.reason).toBe('invalid_device_id');
});
test('recordTailscaleUse rejects oversized deviceId', async () => {
const issued = await store.issueTailscale({ serviceId: 'svc', email: 'a@b.com' });
const r = await store.recordTailscaleUse(issued.token, { deviceId: 'a'.repeat(200) });
expect(r.ok).toBe(false);
expect(r.reason).toBe('invalid_device_id');
});
test('recordTailscaleUse accepts null deviceId (defaults to "unknown")', async () => {
const issued = await store.issueTailscale({ serviceId: 'svc', email: 'a@b.com' });
const r = await store.recordTailscaleUse(issued.token, { deviceId: null });
expect(r.ok).toBe(true);
expect(r.share.usedBy).toBe('unknown');
});
test('recordTailscaleUse accepts omitted deviceId (defaults to "unknown")', async () => {
const issued = await store.issueTailscale({ serviceId: 'svc', email: 'a@b.com' });
const r = await store.recordTailscaleUse(issued.token, {});
expect(r.ok).toBe(true);
expect(r.share.usedBy).toBe('unknown');
});
test('recordPublicSubscribe accepts omitted email (backwards-compatible)', async () => {
const issued = await store.issuePublic({ serviceId: 'svc' });
const r = await store.recordPublicSubscribe(issued.token);
expect(r.ok).toBe(true);
});
test('recordPublicSubscribe accepts null email (backwards-compatible)', async () => {
const issued = await store.issuePublic({ serviceId: 'svc' });
const r = await store.recordPublicSubscribe(issued.token, { email: null });
expect(r.ok).toBe(true);
});
});
// --------- Rate-limit guard ------------------------------------------------------------------------------------------------------------------------------------------------------
describe('DC-083: SHARE_PUBLIC rate-limit', () => {
// We can't easily trigger the rate-limit in a unit test because the
// default 30/15min is high. Instead, verify the constant is wired and
// that the limiter is mounted on the public endpoints (the test env
// skips the limiter, so we just confirm the constants).
test('RATE_LIMITS.SHARE_PUBLIC is bounded tighter than GENERAL', () => {
const { RATE_LIMITS } = require('../src/utilities/constants');
expect(RATE_LIMITS.SHARE_PUBLIC).toBeDefined();
expect(RATE_LIMITS.SHARE_PUBLIC.max).toBeLessThan(RATE_LIMITS.GENERAL.max);
expect(RATE_LIMITS.SHARE_PUBLIC.max).toBeLessThanOrEqual(30);
expect(RATE_LIMITS.SHARE_PUBLIC.windowMs).toBe(15 * 60 * 1000);
});
test('route module loads without throwing when express-rate-limit is wired', () => {
// Smoke test: the route factory must succeed with the limiter attached.
const dir = _tmpDir();
try {
const shareStore = createShareStore({ dataDir: dir });
const app = _buildApp({ shareStore });
// _buildApp would have thrown if the route factory threw.
expect(typeof app).toBe('function');
} finally {
_cleanup(dir);
}
});
test('sharePublicLimiter is mounted on /preview (route stack contains limiter)', () => {
// Verify the limiter middleware is actually wired into /preview's route
// stack. The route uses express.Router().use(path, ...mw, handler) so we
// can inspect the stack via the router's internal `stack` array.
const dir = _tmpDir();
try {
const shareStore = createShareStore({ dataDir: dir });
const router = require('../routes/share')({
shareStore,
licenseManager: { isPro: () => true, allowsLifetimeLicense: () => false },
tailscaleCoord: { createAuthKey: async () => ({ id: 'k', key: 'tskey-x' }) },
notificationManager: { sendEmail: async () => ({ messageId: 'fake' }) },
servicesStateManager: { get: async () => null, read: async () => [] },
servicesFile: null,
asyncHandler: (fn, _label) => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next),
log: { info() {}, warn() {}, error() {} },
});
const previewStack = router.stack.find(
(layer) => layer.route && layer.route.path === '/share/:token/preview'
);
expect(previewStack).toBeDefined();
// The route handler should be preceded by at least one middleware
// layer (the limiter). route.stack contains the per-route middleware.
// In express, .route.stack has the route-local middleware + handler.
// The limiter is mounted at the router level (router.use pattern), so
// it's actually a separate layer in router.stack. Look for any layer
// that has a regex/path matching /share/:token.
const limiterLayer = router.stack.find(
(layer) => layer.regexp && layer.regexp.test && layer.regexp.test('/share/abc/preview')
);
expect(limiterLayer).toBeDefined();
} finally {
_cleanup(dir);
}
});
});
describe('DC-083: positive smoke tests (legitimate inputs)', () => {
test('validates user+tag@sub.domain.io (RFC 5322 plus addressing)', () => {
const { validatePublicEmail } = require('../src/security/share-store');
const v = validatePublicEmail('user+tag@sub.domain.io');
expect(v).toEqual({ ok: true, email: 'user+tag@sub.domain.io' });
});
test('validates a typical Tailscale node ID as deviceId', () => {
const { validatePublicDeviceId } = require('../src/security/share-store');
// Tailscale node IDs look like "nodekey:abcdef0123456789" or just hex
const v = validatePublicDeviceId('nodekey:abcdef0123456789');
expect(v).toEqual({ ok: true, deviceId: 'nodekey:abcdef0123456789' });
});
});
+24 -1
View File
@@ -378,12 +378,35 @@ describe('share routes: POST /share/:token/redeem-tailscale (public)', () => {
expect(r2.body.error).toMatch(/already_used/);
});
test('rejects missing deviceId', async () => {
test('rejects missing deviceId — DC-083 accepts omitted, treats as "unknown"', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore, noAdmin: true });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({});
// DC-083: omitted deviceId is now accepted; the store defaults usedBy
// to 'unknown'. The pre-fix route layer required deviceId be present;
// the new behavior matches the store's defensive default and is
// safer for partially-malformed forward_auth calls from Caddy.
expect(res.status).toBe(200);
expect(res.body.data.redeemed).toBe(true);
});
test('rejects invalid deviceId (control chars / oversized)', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore, noAdmin: true });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: 'node\r\nhack' });
expect(res.status).toBe(400);
});
test('rejects empty deviceId', async () => {
const issued = await shareStore.issueTailscale({ serviceId: 'plex', email: 'a@b.com' });
const app = _buildApp({ shareStore, noAdmin: true });
const res = await request(app)
.post(`/share/${issued.token}/redeem-tailscale`)
.send({ deviceId: '' });
expect(res.status).toBe(400);
});
});
@@ -1,15 +1,29 @@
const express = require('express');
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const request = require('supertest');
const createSsoRouter = require('../routes/auth/sso-gate');
function createApp({ redeem = true } = {}) {
function loadCredentialVaultHandoff() {
const source = fs.readFileSync(
path.join(__dirname, '..', '..', 'status', 'js', 'credential-vault-handoff.js'),
'utf8',
);
const window = { location: { origin: 'https://status.sami' } };
vm.runInNewContext(source, { window, SITE: { tld: '.sami' }, URL });
return window.DCCredentialVault;
}
function createApp({ redeem = true, valid = true, storedCredentials = {}, dashboardHost = 'status.sami' } = {}) {
const app = express();
const session = {
redeemHandoffToken: jest.fn().mockReturnValue(redeem),
redeemHandoffToken: jest.fn((token) => (typeof redeem === 'function' ? redeem(token) : redeem)),
setCookieHostOnly: jest.fn((res) => {
res.setHeader('Set-Cookie', 'dashcaddy_session=test; Path=/; HttpOnly; Secure; SameSite=Lax');
}),
isValid: jest.fn().mockReturnValue(true),
isValid: jest.fn().mockReturnValue(valid),
createHandoffToken: jest.fn().mockReturnValue('fresh-sso-handoff-token'),
};
const asyncHandler = (fn) => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
const errorResponse = (res, status, message, extra = {}) => res.status(status).json({ success: false, error: message, ...extra });
@@ -21,14 +35,15 @@ function createApp({ redeem = true } = {}) {
log: { warn: jest.fn(), info: jest.fn(), debug: jest.fn(), error: jest.fn() },
getAppSession: jest.fn(),
appSessionCache: new Map(),
credentialManager: { retrieve: jest.fn() },
credentialManager: { retrieve: jest.fn((key) => Promise.resolve(storedCredentials[key] || null)) },
fetchT: jest.fn(),
getServiceById: jest.fn(),
getServiceById: jest.fn((id) => Promise.resolve({ id, url: `https://${id}.sami` })),
licenseManager: {
hasFeature: jest.fn().mockReturnValue(true),
requirePremium: jest.fn(() => (_req, _res, next) => next()),
},
servicesStateManager: { read: jest.fn().mockResolvedValue([]) },
siteConfig: { dashboardHost },
});
app.use('/api/v1', router);
return { app, session };
@@ -44,7 +59,7 @@ describe('cross-host SSO exchange redirect', () => {
expect(res.status).toBe(303);
expect(res.headers.location).toBe('/settings?tab=network#dns');
expect(res.headers['set-cookie'][0]).not.toMatch(/Domain=/i);
expect(session.redeemHandoffToken).toHaveBeenCalledWith('one-time');
expect(session.redeemHandoffToken).toHaveBeenCalledWith('one-time', '127.0.0.1');
});
test.each([
@@ -82,3 +97,105 @@ describe('cross-host SSO exchange redirect', () => {
expect(session.setCookieHostOnly).not.toHaveBeenCalled();
});
});
describe('existing-session SSO handoff', () => {
test('mints a handoff token without asking for TOTP again', async () => {
const { app, session } = createApp();
const res = await request(app)
.get('/api/v1/auth/sso-handoff?serviceId=plex')
.set('Cookie', 'dashcaddy_session=valid-session');
expect(res.status).toBe(200);
expect(res.body).toMatchObject({ success: true, ssoToken: 'fresh-sso-handoff-token' });
expect(session.createHandoffToken).toHaveBeenCalledTimes(1);
expect(session.createHandoffToken).toHaveBeenCalledWith('plex.sami');
});
test('refuses to mint a handoff token without a valid session', async () => {
const { app, session } = createApp({ valid: false });
const res = await request(app).get('/api/v1/auth/sso-handoff?serviceId=plex');
expect(res.status).toBe(401);
expect(session.createHandoffToken).not.toHaveBeenCalled();
});
test('completes the full mint, exchange, cookie, redirect lifecycle', async () => {
const issued = new Set(['fresh-sso-handoff-token']);
const redeemOnce = (token) => issued.delete(token);
const { app } = createApp({ redeem: redeemOnce });
const mint = await request(app)
.get('/api/v1/auth/sso-handoff?serviceId=plex')
.set('Cookie', 'dashcaddy_session=valid-session');
const exchange = await request(app)
.get('/api/v1/auth/sso-exchange')
.query({ token: mint.body.ssoToken, return: '/web/' });
expect(exchange.status).toBe(303);
expect(exchange.headers.location).toBe('/web/');
expect(exchange.headers['set-cookie'][0]).toContain('dashcaddy_session=');
expect(exchange.headers['set-cookie'][0]).not.toMatch(/Domain=/i);
const replay = await request(app)
.get('/api/v1/auth/sso-exchange')
.query({ token: mint.body.ssoToken, return: '/web/' });
expect(replay.status).toBe(401);
});
});
describe('encrypted-vault credential onboarding', () => {
test('app-token identifies missing credentials as a form requirement', async () => {
const { app } = createApp();
const res = await request(app)
.get('/api/v1/auth/app-token/plex')
.set('Cookie', 'dashcaddy_session=valid-session');
expect(res.status).toBe(428);
expect(res.body).toMatchObject({
success: false,
credentialsRequired: true,
serviceId: 'plex',
});
});
test('service login page sends missing credentials to the encrypted vault form', async () => {
const { app } = createApp();
const res = await request(app).get('/api/v1/auth/login-page?service=plex');
expect(res.status).toBe(200);
expect(res.text).toContain("if(j.credentialsRequired){vault('plex');return}");
expect(res.text).toContain("dashboardOrigin+'?credentials='");
});
test('service login page derives the vault origin from trusted dashboard config', async () => {
const { app } = createApp({ dashboardHost: 'dashboard.home' });
const res = await request(app).get('/api/v1/auth/login-page?service=plex');
expect(res.status).toBe(200);
expect(res.text).toContain('dashboardOrigin="https://dashboard.home"');
});
test('full vault-save handoff lifecycle reaches exchange, cookie, and final service path', async () => {
const issued = new Set(['fresh-sso-handoff-token']);
const { app } = createApp({ redeem: (token) => issued.delete(token) });
const mint = await request(app)
.get('/api/v1/auth/sso-handoff?serviceId=plex')
.set('Cookie', 'dashcaddy_session=valid-session');
const vault = loadCredentialVaultHandoff();
const target = new URL(vault.buildHandoffTarget(
'https://plex.sami/web/?direct=1#home',
mint.body.ssoToken,
'plex',
));
// The shared Caddy snippet rewrites /dashcaddy-sso to the canonical API
// route while preserving the token and relative return query.
const exchange = await request(app).get('/api/v1/auth/sso-exchange' + target.search);
expect(target.pathname).toBe('/dashcaddy-sso');
expect(exchange.status).toBe(303);
expect(exchange.headers.location).toBe('/web/?direct=1#home');
expect(exchange.headers['set-cookie'][0]).toContain('dashcaddy_session=');
expect(exchange.headers['set-cookie'][0]).not.toMatch(/Domain=/i);
});
});
@@ -118,6 +118,67 @@ describe('Caddyfile + utils/http.js — Origin header construction (DC-051)', ()
expect(offenders).toEqual([]);
});
test('all :2019 call sites in TESTS use fetchT or a mocked fetchT (not raw fetch)', () => {
// DC-087 — the same rule, extended into __tests__. The api-code walk above
// skips __tests__, which let two mirrored health-handler test files keep a
// raw await-fetch caddy probe long after src/app.js moved to fetchT. On a
// host where the suite runs alongside a live Caddy admin (the prod box
// runs the full jest suite every 30 min via a cron adversarial check),
// that Origin-less raw fetch 403-spammed the Caddy journal (~700
// client-not-allowed error lines per day) while the tests still passed —
// checks.caddy.ok=false was silently accepted as sandbox noise. Mirrors
// MUST call fetchT (mocked at buildApp scope for hermeticity). A raw
// await-fetch at a Caddy-admin-URL call site in a test is an offender.
// NOTE: keep this comment free of backticks — stripComments pairs
// backtick spans across lines, and a stray pair shields real code from
// the comment stripper (this test self-flagged its first draft).
//
// Detection is deliberately FILE-LEVEL, not call-window: the historical
// drift kept the fetch call itself token-free (the URL came from a
// caddyUrl variable defined on a PREVIOUS line from CADDY_ADMIN_URL),
// so a call-window regex never fired. Any raw await-fetch in a file
// that also references the Caddy admin anywhere is an offender.
// Escape hatch for future tests that intentionally assert Origin-less
// 403 behavior against their own local listener: put the marker
// DC-087-ALLOW-RAW-FETCH in the file and it is skipped.
const testsRoot = path.join(__dirname);
const offenders = [];
const skipped = [];
function walk(dir) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.name === 'node_modules') continue;
const p = path.join(dir, entry.name);
if (entry.isDirectory()) walk(p);
else if (entry.name.endsWith('.js')) {
const rawText = fs.readFileSync(p, 'utf8');
// Escape hatch (checked on RAW text so a comment marker works —
// comments are stripped below): a file carrying the
// DC-087-ALLOW-RAW-FETCH marker declares it intentionally
// raw-fetches the Caddy admin (e.g. asserting Origin-less 403
// against its own local listener). The guard file itself is
// always scanned (never skipped) so the hatch can't be used to
// blind this very test.
if (p !== __filename && /DC-087-ALLOW-RAW-FETCH/.test(rawText)) {
skipped.push(p);
continue;
}
const text = stripComments(rawText);
const hasAdminToken = /:2019|adminUrl|admin_api_url|CADDY_ADMIN/.test(text);
const hasRawAwaitFetch = /await\s+fetch\(/.test(text);
if (hasAdminToken && hasRawAwaitFetch) {
offenders.push(`${p}: raw await-fetch in a file referencing the Caddy admin (mock fetchT instead; documented escape-hatch marker available for intentional 403 tests)`);
}
}
}
}
walk(testsRoot);
if (skipped.length) {
// Visibility for hatch use — shows up in jest output for reviewers.
console.info('[DC-087 guard] escape-hatch skipped:', skipped.join(', '));
}
expect(offenders).toEqual([]);
});
test('readiness handler in src/app.js probes the exact URL the watcher needs', () => {
const raw = fs.readFileSync(
path.join(__dirname, '../src/app.js'),
@@ -127,9 +188,9 @@ describe('Caddyfile + utils/http.js — Origin header construction (DC-051)', ()
expect(raw).toMatch(/\/config\/apps\/http\/servers\/srv0\/listen/);
// Goes through fetchT, NOT bare fetch — that's how the Origin injection
// takes effect. Look at the 800 chars BEFORE the probe URL on the same
// line / call site — the call must be `fetchT(...)`, not `await fetch(...)`.
// (We look backward because the URL sits inside the call's argument list,
// so the call site comes before the URL token.)
// line / call site — the call must be fetchT(...), never a raw await of
// the global fetch. (We look backward because the URL sits inside the
// call's argument list, so the call site comes before the URL token.)
const idx = raw.indexOf('srv0/listen');
const around = raw.substr(Math.max(0, idx - 400), 800);
expect(around).toMatch(/fetchT\(/);
+36 -38
View File
@@ -29,26 +29,10 @@ const platformPaths = require('../../platform-paths');
const { createUserStore } = require('../../src/security/user-store');
const { createInviteStore } = require('../../src/security/invite-store');
const emailSender = require('../../src/auth/providers/email-sender');
const AuthProvider = require('../../src/auth/providers/base');
const { ValidationError, NotFoundError, ForbiddenError, PaymentRequiredError } = require('../../src/utilities/errors');
const { ok, successMessage } = require('../../src/utils/responses');
/**
* Build the URL an invitee should click. Mirrors EmailMagicLinkProvider's
* _resolvePublicUrl logic kept duplicated (not extracted) because the two
* callers have slightly different link paths and the duplication is smaller
* than the abstraction would be.
*/
function _buildInviteUrl(req, siteConfig, token) {
if (siteConfig && siteConfig.publicBaseUrl) {
return siteConfig.publicBaseUrl.replace(/\/+$/, '') +
'/api/v1/auth/invites/' + encodeURIComponent(token) + '/accept';
}
const proto = (req.headers && req.headers['x-forwarded-proto']) || (req.protocol || 'https');
const host = (req.headers && (req.headers['x-forwarded-host'] || req.headers.host))
|| (siteConfig && siteConfig.dashboardHost) || 'localhost:3001';
return `${proto}://${host}/api/v1/auth/invites/${encodeURIComponent(token)}/accept`;
}
function _requireAdmin(req, _res, next) {
if (!req.user || req.user.role !== 'admin') {
return next(new ForbiddenError('Admin role required'));
@@ -240,11 +224,21 @@ module.exports = function({ asyncHandler, errorResponse, log, session, dataDir }
});
if (!issued.ok) throw new ValidationError(issued.reason, 'email');
let deliveredVia = 'none';
const maskedEmail = email.replace(/(^.).+(@.*$)/, '$1***$2');
if (sendEmail !== false) {
// Best-effort send. If SMTP isn't configured, log to error.log (dev path).
const acceptUrl = _buildInviteUrl(req, /* siteConfig */ req.app.locals && req.app.locals.siteConfig, issued.token);
// Build the accept URL once — used both for the response and for email delivery.
const baseUrl = (req.app.locals && req.app.locals.siteConfig && req.app.locals.siteConfig.publicBaseUrl
? req.app.locals.siteConfig.publicBaseUrl.replace(/\/+$/, '')
: ((req.headers['x-forwarded-proto'] || req.protocol || 'https') + '://' +
(req.headers['x-forwarded-host'] || req.headers.host || 'localhost:3001')));
const acceptUrl = baseUrl + '/api/v1/auth/invites/' + encodeURIComponent(issued.token) + '/accept';
// DC-085: link-first delivery. Default = no email, just hand the link back.
// Operators opt INTO email by sending { sendEmail: true } (or the admin UI
// checks the "Send email" checkbox). When SMTP is unconfigured AND the
// operator did opt in, we surface the failure as `deliveredVia: 'failed'`
// but NEVER leak the raw token into the server log — the link is already
// in the response, so the operator has a UI-side fallback.
let deliveredVia = 'manual';
if (sendEmail === true) {
const ttlHoursOut = Math.round(issued.ttlMs / (60 * 60 * 1000));
const text = _buildEmailText({ acceptUrl, ttlHours: ttlHoursOut, role: issued.role });
const html = _buildEmailHtml({ acceptUrl, ttlHours: ttlHoursOut, role: issued.role });
@@ -254,33 +248,37 @@ module.exports = function({ asyncHandler, errorResponse, log, session, dataDir }
await emailSender.sendEmail(smtpConfig, issued.email, 'You\'re invited to DashCaddy', text, html);
deliveredVia = 'email';
} else {
// Dev fallback — log the raw link so operators can grab it.
log.warn && log.warn('auth-invite-dev',
'[DC-048-DEV-INVITE-LINK] email=' + issued.email +
' role=' + issued.role + ' url=' + acceptUrl);
deliveredVia = 'dev-console';
// Operator asked for email but SMTP isn't configured. Surface the
// failure cleanly; the link is still in the response so the
// operator can share it manually. Do NOT log the raw URL — it
// would duplicate what's already in the response and pollute the
// server log on every unconfigured-install invite.
log.warn && log.warn('auth-invite-send',
'invite send skipped: SMTP not configured (operator opted in)',
{ inviteId: issued.id, email: AuthProvider.maskEmail(issued.email) || '[unmaskable-email]' });
deliveredVia = 'failed';
}
} catch (sendErr) {
log.warn && log.warn('auth-invite-send',
'invite send failed: ' + (sendErr.message || String(sendErr)));
'invite send failed: ' + (sendErr.message || String(sendErr)),
{ inviteId: issued.id });
deliveredVia = 'failed';
}
} else {
deliveredVia = 'manual';
}
const maskedEmail = email.replace(/(^.).+(@.*$)/, '$1***$2');
const ttlHoursOut = Math.round(issued.ttlMs / (60 * 60 * 1000));
const shareText =
'Join my DashCaddy as ' + issued.role + ' — ' + acceptUrl +
' — expires in ' + ttlHoursOut + 'h.';
return ok(res, {
id: issued.id,
email: issued.email,
role: issued.role,
expiresAt: issued.expiresAt,
// The raw token is returned ONCE so the admin UI can show/copy the
// link. It is also embedded in the email when sendEmail !== false.
acceptUrl: (req.app.locals && req.app.locals.siteConfig && req.app.locals.siteConfig.publicBaseUrl
? req.app.locals.siteConfig.publicBaseUrl.replace(/\/+$/, '')
: ((req.headers['x-forwarded-proto'] || req.protocol || 'https') + '://' +
(req.headers['x-forwarded-host'] || req.headers.host || 'localhost:3001'))) +
'/api/v1/auth/invites/' + encodeURIComponent(issued.token) + '/accept',
acceptUrl,
shareText,
deliveredVia,
maskedEmail,
});
@@ -372,7 +370,7 @@ module.exports = function({ asyncHandler, errorResponse, log, session, dataDir }
log.info && log.info('auth', 'invite accepted, user created', {
userId: userResult.user.id,
email: userResult.user.email,
email: AuthProvider.maskEmail(userResult.user.email) || '[unmaskable-email]',
role: userResult.user.role,
inviteId: invite.id,
});
+62 -23
View File
@@ -12,7 +12,7 @@ module.exports = function(deps) {
const router = express.Router();
// Extract dependencies
const { authManager, totpConfig, session, asyncHandler, errorResponse, log, getAppSession, appSessionCache, credentialManager, fetchT, getServiceById, licenseManager, servicesStateManager } = deps;
const { authManager, totpConfig, session, asyncHandler, errorResponse, log, getAppSession, appSessionCache, credentialManager, fetchT, getServiceById, licenseManager, servicesStateManager, siteConfig } = deps;
// Create ctx-like object for compatibility
const ctx = {
@@ -126,7 +126,12 @@ module.exports = function(deps) {
try {
const username = await ctx.credentialManager.retrieve(`service.${serviceId}.username`).catch(() => null);
const password = await ctx.credentialManager.retrieve(`service.${serviceId}.password`).catch(() => null);
if (!username || !password) throw new NotFoundError('[DC-500] No credentials stored');
if (!username || !password) {
return errorResponse(res, 428, '[DC-500] No credentials stored', {
credentialsRequired: true,
serviceId,
});
}
const service = await ctx.getServiceById(serviceId);
const baseUrl = service?.url;
if (!baseUrl) throw new NotFoundError('No service URL');
@@ -181,7 +186,12 @@ module.exports = function(deps) {
password = await ctx.credentialManager.retrieve(`service.${serviceId}.password`).catch(() => null);
}
if (!username || !password) throw new NotFoundError('[DC-500] No credentials stored');
if (!username || !password) {
return errorResponse(res, 428, '[DC-500] No credentials stored', {
credentialsRequired: true,
serviceId,
});
}
const appCookies = await getAppSession(serviceId, baseUrl, username, password);
if (appCookies) {
@@ -203,8 +213,28 @@ module.exports = function(deps) {
}
}, 'auth-app-token'));
// A browser that already has a valid status.sami session must not be asked
// for TOTP again just because it opened another private-TLD service host.
// Mint a fresh one-time token that the target host can exchange for its own
// host-only cookie. This route is intentionally session-protected both by
// the global middleware and here (defence in depth).
router.get('/auth/sso-handoff', (req, res) => {
res.setHeader('Cache-Control', 'no-store');
if (!session.isValid(req)) {
return errorResponse(res, 401, 'Session expired or invalid');
}
const serviceId = String(req.query.serviceId || '');
if (!/^[a-z0-9][a-z0-9-]*$/.test(serviceId)) {
return errorResponse(res, 400, 'Valid serviceId is required');
}
const suffix = String(siteConfig?.tld || '.sami');
const expectedHost = `${serviceId}${suffix.startsWith('.') ? suffix : `.${suffix}`}`;
ok(res, { ssoToken: session.createHandoffToken(expectedHost) });
});
// Cross-subdomain SSO handoff: exchanges a short-lived single-use token
// (minted by /totp/verify) for a HOST-ONLY session cookie on whichever
// (minted by /totp/verify or /auth/sso-handoff) for a HOST-ONLY session
// cookie on whichever
// *.sami origin calls this. Needed because Domain=.sami cookies are
// silently rejected by real browsers (.sami is an unregistered TLD, so
// browsers treat "sami" as the effective public suffix and refuse to set
@@ -215,7 +245,9 @@ module.exports = function(deps) {
router.get('/auth/sso-exchange', (req, res) => {
res.setHeader('Cache-Control', 'no-store');
const token = req.query.token;
if (!session.redeemHandoffToken(token)) {
const forwardedHost = String(req.headers['x-forwarded-host'] || req.headers.host || '')
.split(',')[0].trim().replace(/:\d+$/, '').toLowerCase();
if (!session.redeemHandoffToken(token, forwardedHost)) {
return errorResponse(res, 401, 'Invalid or expired handoff token');
}
session.setCookieHostOnly(res, totpConfig.sessionDuration);
@@ -237,7 +269,12 @@ module.exports = function(deps) {
// Serve service-specific auto-login page (auth enforced by Caddy forward_auth upstream)
router.get('/auth/login-page', (req, res) => {
const service = (req.query.service || '').replace(/[^a-z]/g, '');
const html = buildLoginPage(service);
const configuredHost = siteConfig?.dashboardHost;
const dashboardOrigin = typeof configuredHost === 'string'
&& /^[a-zA-Z0-9][a-zA-Z0-9.-]*$/.test(configuredHost)
? `https://${configuredHost}`
: 'https://status.sami';
const html = buildLoginPage(service, dashboardOrigin);
if (!html) return res.status(404).send('Unknown service');
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('Cache-Control', 'no-store');
@@ -255,7 +292,7 @@ module.exports = function(deps) {
return router;
};
function buildLoginPage(service) {
function buildLoginPage(service, dashboardOrigin = 'https://status.sami') {
// Pre-auth check via <meta http-equiv="refresh"> so it fires even when JS is
// disabled or blocked. The cookie is sent automatically because we hit the
// same origin (plex.sami); if the API returns 200 the user has a valid
@@ -266,7 +303,7 @@ function buildLoginPage(service) {
<style>body{background:__BG__;color:#e0e0e0;font-family:system-ui;display:flex;align-items:center;justify-items:center;height:100vh;margin:0;flex-direction:column;gap:12px}a{color:__ACCENT__}#d{font-size:12px;color:#888;max-width:80vw;overflow:auto;white-wrap:pre-wrap}</style>
</head><body><p id="m">__TITLE__</p><div id="d"></div>
<script>(function(){
var ls=localStorage,d=document.getElementById('d'),m=document.getElementById('m');
var ls=localStorage,d=document.getElementById('d'),m=document.getElementById('m'),dashboardOrigin=__DASHBOARD_ORIGIN__;
// 2026-07-22 hardening: every fetch now has a hard AbortSignal timeout
// (default 8s) so a hung upstream can NEVER leave the page stuck on
// "Signing in to Plex..." indefinitely. Also: if check-session returns
@@ -274,13 +311,16 @@ function buildLoginPage(service) {
// upstream timeout, etc.), we now ALWAYS redirect to /web/?direct=1 if a
// stale token exists in localStorage, instead of failing silently.
function go(u){setTimeout(function(){location.replace(u)},300)}
function authUrl(){return dashboardOrigin+'?auth=required&return='+encodeURIComponent(location.href)}
function authLink(label){return '<a href="'+authUrl()+'">'+label+'</a>'}
function vault(svc){go(dashboardOrigin+'?credentials='+encodeURIComponent(svc)+'&return='+encodeURIComponent(location.href))}
function fail(msg,info){try{m.innerHTML=msg;d.textContent=info||''}catch(_){}}
function withTimeout(ms){var c=new AbortController();setTimeout(function(){c.abort()},ms);return c.signal}
function ft(svc){return fetch('/dashcaddy-api/api/auth/app-token/'+svc,{credentials:'include',signal:withTimeout(8000)})}
function merge(ck,j,name){try{var c=JSON.parse(ls.getItem(ck)||'{}');if(c.Servers&&c.Servers.length){var s=c.Servers[0];s.AccessToken=j.token;s.UserId=j.userId||s.UserId||'';s.DateLastAccessed=Date.now();ls.setItem(ck,JSON.stringify(c));return}}catch(e){}ls.setItem(ck,JSON.stringify({Servers:[{Id:j.serverId||'',Name:j.serverName||name,UserId:j.userId||'',AccessToken:j.token,ManualAddress:location.origin,LastConnectionMode:2,DateLastAccessed:Date.now()}]}))}
// Belt-and-suspenders hard timeout: if nothing in this script succeeds
// within 15s, force-redirect to status.sami so the user can re-auth.
var overallTimer=setTimeout(function(){go('https://status.sami?auth=required&return='+encodeURIComponent(location.href))},15000);
var overallTimer=setTimeout(function(){go(authUrl())},15000);
// Cross-subdomain SSO handoff: status.sami can't share its session cookie
// with this origin (Domain=.sami cookies are silently rejected by real
// browsers - .sami isn't a registered TLD, so browsers treat "sami" as the
@@ -305,18 +345,17 @@ function buildLoginPage(service) {
preExchange.then(function(){
return fetch('/dashcaddy-api/api/auth/totp/check-session',{credentials:'include',cache:'no-store',signal:withTimeout(5000)})
}).then(function(r){return r.json()}).then(function(st){
if(!st||!st.success||!st.authenticated){go('https://status.sami?auth=required&return='+encodeURIComponent(location.href));return}
if(!st||!st.success||!st.authenticated){go(authUrl());return}
${body}
}).catch(function(e){fail('Could not reach DashCaddy. <a href="https://status.sami?auth=required&return='+encodeURIComponent(location.href)+'">Sign in at DashCaddy</a>','Auth check error: '+(e&&e.message||'unknown'))})
}).catch(function(e){fail('Could not reach DashCaddy. '+authLink('Sign in at DashCaddy'),'Auth check error: '+(e&&e.message||'unknown'))})
})()</script></body></html>`;
const pages = {
chat: {
title: 'Signing in...', bg: '#0a0a0a', accent: '#60a5fa',
body: `if(ls.getItem('token')){go('/?direct=1');return}
d.textContent='Fetching token from DashCaddy...';
body: `d.textContent='Fetching token from DashCaddy...';
ft('chat').then(function(r){return r.text()}).then(function(t){
try{var j=JSON.parse(t);if(j.token){ls.setItem('token',j.token);go('/?direct=1');return}
try{var j=JSON.parse(t);if(j.credentialsRequired){vault('chat');return}if(j.token){ls.setItem('token',j.token);go('/?direct=1');return}
// No token but chat is reachable — fall through to manual UI link below
fail('Auto-login unavailable. <a href="/?direct=1">Open Chat manually</a>','No token field: '+t.substring(0,200))}
catch(e){fail('Auto-login parse error. <a href="/?direct=1">Open Chat manually</a>','Error: '+e.message+' / body: '+t.substring(0,200))}
@@ -324,30 +363,29 @@ ft('chat').then(function(r){return r.text()}).then(function(t){
},
plex: {
title: 'Signing in to Plex...', bg: '#1f1f1f', accent: '#e5a00d',
body: `if(ls.getItem('myPlexAccessToken')){go('/web/?direct=1');return}
ft('plex').then(function(r){return r.json()}).then(function(j){
if(j.token){ls.setItem('myPlexAccessToken',j.token);d.textContent='Token stored, redirecting...';go('/web/?direct=1');return}
body: `ft('plex').then(function(r){return r.json()}).then(function(j){
if(j.credentialsRequired){vault('plex');return}if(j.token){ls.setItem('myPlexAccessToken',j.token);d.textContent='Token stored, redirecting...';go('/web/?direct=1');return}
// No token returned. Three fallbacks in priority order:
// 1. Stale token in localStorage — Plex may still accept it.
if(ls.getItem('myPlexAccessToken')){go('/web/?direct=1');return}
// 2. Manual link so the user is never trapped on this page.
fail('Auto-login unavailable. <a href="/web/?direct=1">Open Plex manually</a> or <a href="https://status.sami?auth=required&return='+encodeURIComponent(location.href)+'">re-authenticate at DashCaddy</a>','API: '+JSON.stringify(j))
fail('Auto-login unavailable. <a href="/web/?direct=1">Open Plex manually</a> or '+authLink('re-authenticate at DashCaddy'),'API: '+JSON.stringify(j))
}).catch(function(e){fail('Could not reach DashCaddy. <a href="/web/?direct=1">Open Plex manually</a>','Error: '+(e&&e.message||'unknown'))})`
},
jellyfin: {
title: 'Signing in to Jellyfin...', bg: '#101014', accent: '#00a4dc',
body: `ft('jellyfin').then(function(r){return r.json()}).then(function(j){
if(j.token){merge('jellyfin_credentials',j,'Jellyfin');merge('_jellyfin_credentials',j,'Jellyfin');d.textContent='Token stored, redirecting...';go('/web/');return}
if(j.credentialsRequired){vault('jellyfin');return}if(j.token){merge('jellyfin_credentials',j,'Jellyfin');merge('_jellyfin_credentials',j,'Jellyfin');d.textContent='Token stored, redirecting...';go('/web/');return}
if(ls.getItem('jellyfin_credentials')||ls.getItem('_jellyfin_credentials')){go('/web/');return}
fail('Auto-login unavailable. <a href="/web/">Open Jellyfin manually</a> or <a href="https://status.sami?auth=required&return='+encodeURIComponent(location.href)+'">re-authenticate at DashCaddy</a>','API: '+JSON.stringify(j))
fail('Auto-login unavailable. <a href="/web/">Open Jellyfin manually</a> or '+authLink('re-authenticate at DashCaddy'),'API: '+JSON.stringify(j))
}).catch(function(e){fail('Could not reach DashCaddy. <a href="/web/">Open Jellyfin manually</a>','Error: '+(e&&e.message||'unknown'))})`
},
emby: {
title: 'Signing in to Emby...', bg: '#101014', accent: '#52b54b',
body: `ft('emby').then(function(r){return r.json()}).then(function(j){
if(j.token){merge('emby_credentials',j,'Emby');merge('_emby_credentials',j,'Emby');d.textContent='Token stored, redirecting...';go('/web/');return}
if(j.credentialsRequired){vault('emby');return}if(j.token){merge('emby_credentials',j,'Emby');merge('_emby_credentials',j,'Emby');d.textContent='Token stored, redirecting...';go('/web/');return}
if(ls.getItem('emby_credentials')||ls.getItem('_emby_credentials')){go('/web/');return}
fail('Auto-login unavailable. <a href="/web/">Open Emby manually</a> or <a href="https://status.sami?auth=required&return='+encodeURIComponent(location.href)+'">re-authenticate at DashCaddy</a>','API: '+JSON.stringify(j))
fail('Auto-login unavailable. <a href="/web/">Open Emby manually</a> or '+authLink('re-authenticate at DashCaddy'),'API: '+JSON.stringify(j))
}).catch(function(e){fail('Could not reach DashCaddy. <a href="/web/">Open Emby manually</a>','Error: '+(e&&e.message||'unknown'))})`
},
};
@@ -357,5 +395,6 @@ ft('plex').then(function(r){return r.json()}).then(function(j){
return SHELL(cfg.body)
.replace(/__TITLE__/g, cfg.title)
.replace('__BG__', cfg.bg)
.replace('__ACCENT__', cfg.accent);
.replace('__ACCENT__', cfg.accent)
.replace('__DASHBOARD_ORIGIN__', JSON.stringify(dashboardOrigin));
}
+13 -4
View File
@@ -15,7 +15,7 @@ const { ok, successMessage } = require('../../src/utils/responses');
* @param {Object} deps.log - Logger instance
* @returns {express.Router}
*/
module.exports = function({ authManager, credentialManager, totpConfig, saveTotpConfig, session, asyncHandler, errorResponse, log, renewCSRFToken }) {
module.exports = function({ authManager, credentialManager, totpConfig, saveTotpConfig, session, asyncHandler, errorResponse, log, renewCSRFToken, siteConfig }) {
const router = express.Router();
// Ctx shim for backward compatibility
@@ -23,7 +23,8 @@ module.exports = function({ authManager, credentialManager, totpConfig, saveTotp
credentialManager,
totpConfig,
saveTotpConfig,
session
session,
siteConfig
};
// Get current TOTP config (public route)
@@ -193,11 +194,14 @@ const SETUP_WINDOW_MS = 60 * 60 * 1000;
// Login: verify TOTP code and set session cookie
router.post('/totp/verify', asyncHandler(async (req, res) => {
const { authenticator } = require('otplib');
const { code } = req.body;
const { code, serviceId } = req.body;
if (!code || !/^\d{6}$/.test(code)) {
throw new ValidationError('Invalid code format', 'code');
}
if (serviceId != null && !/^[a-z0-9][a-z0-9-]*$/.test(String(serviceId))) {
throw new ValidationError('Invalid service ID', 'serviceId');
}
if (!ctx.totpConfig.enabled || !ctx.totpConfig.isSetUp) {
throw new ValidationError('TOTP is not enabled');
@@ -227,7 +231,12 @@ const SETUP_WINDOW_MS = 60 * 60 * 1000;
// URL when bouncing the user back to a gated service. That service's
// login page exchanges it via /auth/sso-exchange for its own host-only
// session cookie. Single-use, 60s TTL — see ctx.session.createHandoffToken.
const ssoToken = ctx.session.createHandoffToken();
let ssoToken = null;
if (serviceId) {
const suffix = String(ctx.siteConfig?.tld || '.sami');
const expectedHost = `${serviceId}${suffix.startsWith('.') ? suffix : `.${suffix}`}`;
ssoToken = ctx.session.createHandoffToken(expectedHost);
}
log.debug('auth', 'Session created', { sessions: ctx.session.ipSessions.size });
ok(res, { message: 'Authenticated successfully', sessionDuration: ctx.totpConfig.sessionDuration, csrfToken: newCsrfToken, ssoToken });
+86 -5
View File
@@ -40,7 +40,15 @@ module.exports = function({ notification, asyncHandler, ok }) {
enabled: notificationConfig.providers.email?.enabled || false,
configured: !!(notificationConfig.providers.email?.host && notificationConfig.providers.email?.to),
host: notificationConfig.providers.email?.host || '',
from: notificationConfig.providers.email?.from || ''
from: notificationConfig.providers.email?.from || '',
// DC-092: the settings UI needs these to roundtrip the form.
// Password is NEVER returned; hasPassword lets the UI show a
// "leave blank to keep" hint instead of an empty-looking field.
port: notificationConfig.providers.email?.port || 587,
secure: notificationConfig.providers.email?.secure === true,
to: notificationConfig.providers.email?.to || '',
username: notificationConfig.providers.email?.username || '',
hasPassword: !!notificationConfig.providers.email?.password
}
},
events: notificationConfig.events,
@@ -54,6 +62,39 @@ module.exports = function({ notification, asyncHandler, ok }) {
const { enabled, providers, events, healthCheck } = req.body;
const notificationConfig = notification.getConfig();
// DC-092: clients have historically sent at least three field spellings:
// the settings UI sends email.user/email.pass (its input ids are
// email-user/email-pass) while the manager/route read username/password.
// Normalize aliases onto the canonical keys BEFORE the merge so SMTP auth
// actually applies for UI-saved configs.
if (providers?.email) {
if (providers.email.user !== undefined && providers.email.username === undefined) {
providers.email.username = providers.email.user;
}
if (providers.email.pass !== undefined && providers.email.password === undefined) {
providers.email.password = providers.email.pass;
}
delete providers.email.user;
delete providers.email.pass;
}
// DC-092 strict boolean contract: enabled/secure must be actual
// booleans. `"false"` (string) is truthy — !!"false" === true — and
// previously persisted as-is, silently forcing TLS on the next send.
// Reject instead of coercing.
const boolOrThrow = (val, label) => {
if (val === undefined) return;
if (typeof val !== 'boolean') {
throw new ValidationError(`${label} must be a boolean (got ${typeof val})`);
}
};
boolOrThrow(enabled, 'enabled');
boolOrThrow(providers?.discord?.enabled, 'providers.discord.enabled');
boolOrThrow(providers?.telegram?.enabled, 'providers.telegram.enabled');
boolOrThrow(providers?.ntfy?.enabled, 'providers.ntfy.enabled');
boolOrThrow(providers?.email?.enabled, 'providers.email.enabled');
boolOrThrow(providers?.email?.secure, 'providers.email.secure');
// Validate provider webhook URLs and tokens
if (providers) {
if (providers.discord?.webhookUrl) {
@@ -96,6 +137,12 @@ module.exports = function({ notification, asyncHandler, ok }) {
throw new ValidationError('Invalid SMTP host');
}
}
if (providers.email?.port !== undefined) {
const p = Number(providers.email.port);
if (!Number.isInteger(p) || p < 1 || p > 65535) {
throw new ValidationError('SMTP port must be an integer 1-65535');
}
}
}
// Update enabled state
@@ -124,16 +171,50 @@ module.exports = function({ notification, asyncHandler, ok }) {
};
}
if (providers.email) {
// Non-destructive merge: an empty-string username/password from the
// UI (password field is intentionally left blank to keep stored
// credentials) must NOT clobber the stored credential.
const stored = notificationConfig.providers.email;
const incoming = { ...providers.email };
if (incoming.password === '') delete incoming.password;
if (incoming.username === '') delete incoming.username;
notificationConfig.providers.email = {
...notificationConfig.providers.email,
...providers.email
...stored,
...incoming
};
}
}
// Update events
// Update events. DC-092: the UI sends camelCase keys (containerDown);
// the canonical store/gate keys are kebab-case (container-down). Fold
// before merging so UI toggles actually reach the keys the send() gate
// reads. Values must be booleans; unknown keys pass through unchanged
// (canonicalized if known alias) and merge over defaults.
if (events) {
notificationConfig.events = { ...notificationConfig.events, ...events };
const EVENT_KEY_ALIASES = {
containerDown: 'container-down',
containerUp: 'container-up',
deploymentSuccess: 'deploy-success',
deploymentFailed: 'deploy-failed',
deploySuccess: 'deploy-success',
deployFailed: 'deploy-failed',
resourceAlert: 'alert',
updateAvailable: 'update-available',
backupComplete: 'backup-complete',
backupFailed: 'backup-failed',
autoRestart: 'auto-restart',
};
const folded = {};
for (const [k, v] of Object.entries(events)) {
const canonicalKey = EVENT_KEY_ALIASES[k] || k;
folded[canonicalKey] = v;
}
for (const [k, v] of Object.entries(folded)) {
if (typeof v !== 'boolean') {
throw new ValidationError(`events.${k} must be a boolean (got ${typeof v})`);
}
}
notificationConfig.events = { ...notificationConfig.events, ...folded };
}
// Update health check settings
+2 -1
View File
@@ -263,9 +263,10 @@ module.exports = function({
const arrKey = await credentialManager.retrieve(`arr.${serviceId}.apikey`).catch(() => null);
const svcKey = await credentialManager.retrieve(`service.${serviceId}.apikey`).catch(() => null);
const username = await credentialManager.retrieve(`service.${serviceId}.username`).catch(() => null);
const password = await credentialManager.retrieve(`service.${serviceId}.password`).catch(() => null);
success(res, {
hasApiKey: !!(arrKey || svcKey),
hasBasicAuth: !!username,
hasBasicAuth: !!username && !!password,
username: username || null
});
}, 'service-creds'));
+61 -9
View File
@@ -37,6 +37,10 @@
const { ValidationError, NotFoundError } = require('../src/utilities/errors');
const { PaymentRequiredError } = require('../src/utilities/errors');
const { ok, created, badRequest, notFound } = require('../src/utils/responses');
// DC-083: route-layer validators for the public CSRF-exempt endpoints. These
// are imported from share-store so the route and store stay in lockstep
// (drift risk if one set is updated and the other is forgotten).
const { validatePublicEmail, validatePublicDeviceId } = require('../src/security/share-store');
const PUBLIC_TTL_OPTIONS = new Set([
60 * 60 * 1000,
@@ -293,7 +297,35 @@ module.exports = function shareRoutesFactory({
// ─── Public endpoints (no auth, no Pro gate) ──────────────────────────────
router.get('/share/:token/preview', asyncHandler(async (req, res) => {
// DC-083: rate-limit the two CSRF-exempt public endpoints. The general
// limiter (1000/15min) is mounted globally in app.js and is too generous
// for unauthenticated state-mutating endpoints. 30/15min per IP is
// enough for a legitimate user clicking "subscribe" once or twice; anything
// beyond is abuse. Skipped in test envs via the standard isTest guard.
// Lazy-loaded so test environments without the dep installed don't blow up;
// a missing-dep in production logs a warning and falls back to no-op (still
// safe — the route+store validators are the primary defense).
const { RATE_LIMITS } = require('../src/utilities/constants');
const isTest = process.env.NODE_ENV === 'test';
let _sharePublicLimiter = (req, _res, next) => next(); // no-op default
try {
const rateLimit = require('express-rate-limit'); // eslint-disable-line global-require
_sharePublicLimiter = rateLimit({
...RATE_LIMITS.SHARE_PUBLIC,
standardHeaders: true,
legacyHeaders: false,
skip: () => isTest,
message: { success: false, error: 'Too many share requests, please try again later' },
});
} catch (e) {
// Don't crash on missing dep in a bare-bones env — but log so it's not
// invisible if production misconfigured.
if (log && typeof log.warn === 'function') {
log.warn({ ctx: 'share-routes', err: e.message }, 'express-rate-limit unavailable; share public endpoints have NO rate limit');
}
}
router.get('/share/:token/preview', _sharePublicLimiter, asyncHandler(async (req, res) => {
const meta = await shareStore.peek(req.params.token);
if (!meta) {
return res.status(404).json({ success: false, error: '[DC-553] share not found or expired' });
@@ -310,12 +342,21 @@ module.exports = function shareRoutesFactory({
});
}, 'share-preview'));
router.post('/share/:token/subscribe', asyncHandler(async (req, res) => {
router.post('/share/:token/subscribe', _sharePublicLimiter, asyncHandler(async (req, res) => {
// DC-083: replace the primitive `email.includes('@')` check with a
// charset/length/control-char-bounded validator. The pre-fix code
// accepted `@`, `a@`, `<script>@x.c`, and 10MB strings as "valid email".
// The subscribe body's `email` is now also captured to the share record
// (capped to last 8 entries, see share-store recordPublicSubscribe) so
// the operator can see who subscribed.
const { email } = req.body || {};
if (!email || typeof email !== 'string' || !email.includes('@')) {
throw new ValidationError('valid email required', 'email');
let normalizedEmail = null;
if (email !== undefined && email !== null) {
const v = validatePublicEmail(email);
if (!v.ok) throw new ValidationError(v.reason, 'email');
normalizedEmail = v.email;
}
const result = await shareStore.recordPublicSubscribe(req.params.token);
const result = await shareStore.recordPublicSubscribe(req.params.token, { email: normalizedEmail });
if (!result.ok) {
if (result.reason === 'not_found') throw new NotFoundError('share not found');
throw new ValidationError(result.reason, 'share');
@@ -323,12 +364,23 @@ module.exports = function shareRoutesFactory({
res.json({ success: true, data: { count: result.count, cap: result.cap } });
}, 'share-subscribe'));
router.post('/share/:token/redeem-tailscale', asyncHandler(async (req, res) => {
router.post('/share/:token/redeem-tailscale', _sharePublicLimiter, asyncHandler(async (req, res) => {
// DC-083: replace the bare `typeof deviceId === 'string'` check with a
// charset/length/control-char-bounded validator. The pre-fix code
// accepted arbitrary strings of any length — including CR/LF/NUL,
// which flow into the Tailscale auth-key description string in
// POST /share/tailscale (routes/share.js:213 in the issue path).
// The redeem-tailscale path receives the deviceId from Caddy's
// forward_auth (a Tailscale machine ID), which is base64url +
// hyphens — well within the validator's charset.
const { deviceId } = req.body || {};
if (!deviceId || typeof deviceId !== 'string') {
throw new ValidationError('deviceId required', 'deviceId');
let normalizedDeviceId = null;
if (deviceId !== undefined && deviceId !== null) {
const v = validatePublicDeviceId(deviceId);
if (!v.ok) throw new ValidationError(v.reason, 'deviceId');
normalizedDeviceId = v.deviceId;
}
const result = await shareStore.recordTailscaleUse(req.params.token, { deviceId });
const result = await shareStore.recordTailscaleUse(req.params.token, { deviceId: normalizedDeviceId });
if (!result.ok) {
if (result.reason === 'not_found') throw new NotFoundError('share not found');
throw new ValidationError(result.reason, 'share');
@@ -7,6 +7,28 @@ const fs = require('fs');
const path = require('path');
const nodemailer = require('nodemailer');
// Canonical event names are kebab-case ('container-down'). Emitters and the
// settings UI historically send camelCase ('containerDown', 'deploymentSuccess')
// and the alias map below folds every known spelling onto the canonical key.
// DC-092: before this map, the events gate looked up the RAW event name, so
// 'deploymentSuccess' (routes/apps/deploy.js, routes/recipes/deploy.js) and
// 'auto-restart' (resource-monitor.js) were absent from DEFAULT events and
// silently dropped, and UI camelCase toggles never reached the kebab keys the
// gate reads — the toggles were cosmetic.
const EVENT_ALIASES = {
containerDown: 'container-down',
containerUp: 'container-up',
deploymentSuccess: 'deploy-success',
deploymentFailed: 'deploy-failed',
deploySuccess: 'deploy-success',
deployFailed: 'deploy-failed',
resourceAlert: 'alert',
updateAvailable: 'update-available',
backupComplete: 'backup-complete',
backupFailed: 'backup-failed',
autoRestart: 'auto-restart',
};
const DEFAULT_CONFIG = {
enabled: true,
providers: {
@@ -21,7 +43,13 @@ const DEFAULT_CONFIG = {
'alert': true,
'backup-complete': true,
'backup-failed': true,
'update-available': true
'update-available': true,
// DC-092: emitters (apps/recipes deploy routes) fire these; they were
// missing from defaults entirely, so every deploy notification was
// silently dropped before this fix.
'deploy-success': true,
'deploy-failed': true,
'auto-restart': true
}
};
@@ -48,6 +76,7 @@ class NotificationManager extends EventEmitter {
try {
if (fs.existsSync(this.NOTIFICATIONS_FILE)) {
const data = JSON.parse(fs.readFileSync(this.NOTIFICATIONS_FILE, 'utf8'));
this._canonicalizeLegacyKeys(data);
this.config = this._mergeConfig(DEFAULT_CONFIG, data);
}
} catch (error) {
@@ -55,6 +84,40 @@ class NotificationManager extends EventEmitter {
}
}
/**
* DC-092: configs saved by older clients may contain the legacy spellings
* the old POST /config merged verbatim email.user/email.pass instead of
* username/password, and camelCase event keys instead of kebab-case. Fold
* them onto the canonical keys BEFORE the defaults merge (after the merge
* the canonical keys always exist from defaults, so the alias guards would
* never fire) so a config file written before this fix keeps working: SMTP
* auth applies and event toggles gate correctly.
*/
_canonicalizeLegacyKeys(data) {
// Email credentials: user/pass → username/password (only when the
// canonical key is absent in the raw data; canonical wins on conflict).
const email = data?.providers?.email;
if (email && typeof email === 'object') {
if (email.user !== undefined && email.username === undefined) email.username = email.user;
if (email.pass !== undefined && email.password === undefined) email.password = email.pass;
delete email.user;
delete email.pass;
// secure must be a real boolean: legacy string values (e.g. "false"
// from hand-edited JSON) are truthy under !! and would force TLS.
if (email.secure !== undefined) email.secure = email.secure === true;
}
// Event keys: camelCase → kebab-case canonical.
if (data?.events && typeof data.events === 'object') {
for (const [k, v] of Object.entries(data.events)) {
const canonicalKey = EVENT_ALIASES[k];
if (canonicalKey) {
if (data.events[canonicalKey] === undefined) data.events[canonicalKey] = v;
delete data.events[k];
}
}
}
}
/**
* Merge loaded config with defaults
*/
@@ -130,9 +193,15 @@ class NotificationManager extends EventEmitter {
return { success: false, error: 'Notifications disabled' };
}
// Check if event is enabled
if (event && this.config.events && !this.config.events[event]) {
return { success: false, error: `Event ${event} not enabled` };
// Fold legacy/camelCase spellings onto canonical kebab-case keys (DC-092).
const canonical = EVENT_ALIASES[event] || event;
// Check if event is enabled. 'test' bypasses the gate: it is the settings
// UI "Send Test" flow and is not an operator-togglable event (there is no
// 'test' key in events; gating on it made the Test button a no-op).
const gated = canonical !== 'test';
if (gated && this.config.events && this.config.events[canonical] !== true) {
return { success: false, error: `Event ${canonical} not enabled` };
}
const results = [];
@@ -141,7 +210,7 @@ class NotificationManager extends EventEmitter {
// Discord
if (providers.discord?.enabled && providers.discord?.webhookUrl) {
try {
const result = await this.sendDiscord(this._formatText(data, event), this._formatEmbed(data, event, type));
const result = await this.sendDiscord(this._formatText(data, canonical), this._formatEmbed(data, canonical, type));
results.push({ provider: 'discord', ...result });
} catch (error) {
results.push({ provider: 'discord', success: false, error: error.message });
@@ -151,7 +220,7 @@ class NotificationManager extends EventEmitter {
// Telegram
if (providers.telegram?.enabled && providers.telegram?.botToken && providers.telegram?.chatId) {
try {
const result = await this.sendTelegram(this._formatText(data, event));
const result = await this.sendTelegram(this._formatText(data, canonical));
results.push({ provider: 'telegram', ...result });
} catch (error) {
results.push({ provider: 'telegram', success: false, error: error.message });
@@ -161,7 +230,7 @@ class NotificationManager extends EventEmitter {
// ntfy
if (providers.ntfy?.enabled && providers.ntfy?.topic) {
try {
const result = await this.sendNtfy(this._formatText(data, event), this._formatTitle(event));
const result = await this.sendNtfy(this._formatText(data, canonical), this._formatTitle(canonical));
results.push({ provider: 'ntfy', ...result });
} catch (error) {
results.push({ provider: 'ntfy', success: false, error: error.message });
@@ -172,8 +241,8 @@ class NotificationManager extends EventEmitter {
if (providers.email?.enabled && providers.email?.host && providers.email?.to) {
try {
const result = await this.sendEmail(
this._formatTitle(event),
this._formatText(data, event)
this._formatTitle(canonical),
this._formatText(data, canonical)
);
results.push({ provider: 'email', ...result });
} catch (error) {
@@ -183,9 +252,9 @@ class NotificationManager extends EventEmitter {
const allSucceeded = results.every(r => r.success);
this._addToHistory({
title: this._formatTitle(event),
title: this._formatTitle(canonical),
type,
event,
event: canonical,
results
});
@@ -290,7 +359,7 @@ class NotificationManager extends EventEmitter {
const transporter = nodemailer.createTransport({
host,
port: parseInt(port) || 587,
secure: !!secure,
secure: secure === true,
auth: username ? {
user: username,
pass: password
+246 -20
View File
@@ -32,6 +32,30 @@ const CHECK_INTERVAL = parseInt(process.env.HEALTH_CHECK_INTERVAL || '30000', 10
const MAX_CHECK_INTERVAL = parseInt(process.env.HEALTH_CHECK_MAX_INTERVAL || '300000', 10); // 5 minutes max backoff
const MAX_ENTRIES_PER_SERVICE = parseInt(process.env.HEALTH_MAX_ENTRIES || '500', 10); // Cap to prevent disk explosion
const HISTORY_RETENTION_DAYS = parseInt(process.env.HEALTH_HISTORY_RETENTION || '30', 10);
// DC-088: how long a removal tombstone outlives the removal itself. Only needs
// to cover the max in-flight probe lifetime (timeout + scheduling headroom);
// swept by cleanupHistory so removed services cannot accumulate map entries.
const REMOVED_GENERATION_TTL_MS = parseInt(process.env.HEALTH_REMOVED_GEN_TTL || '600000', 10);
// DC-086: hysteresis thresholds for badge display.
// The raw probe result can flap on a single transient blip (Caddy reload,
// container CPU steal, network hiccup, mid-flight TLS handshake). Showing
// every probe result as-is to the dashboard creates the "perpetual flicker"
// UX. Asymmetric thresholds: going red is slow (don't false-alarm), going
// green is fast (don't keep showing red after recovery).
// - DOWN_THRESHOLD = N consecutive "down" probes before the badge flips to red
// - UP_THRESHOLD = N consecutive "up" probes before the badge flips back to green
// Single probe flips to green on purpose — false-positive-green is much less
// painful than perpetual-red (operators notice red, ignore green).
function readPositiveIntEnv(name, fallback) {
const raw = process.env[name];
if (raw === undefined || raw === '') return fallback;
const value = Number(raw);
return Number.isSafeInteger(value) && value >= 1 ? value : fallback;
}
const DOWN_THRESHOLD = readPositiveIntEnv('HEALTH_DOWN_THRESHOLD', 2);
const UP_THRESHOLD = readPositiveIntEnv('HEALTH_UP_THRESHOLD', 1);
class HealthChecker extends EventEmitter {
constructor() {
@@ -39,11 +63,27 @@ class HealthChecker extends EventEmitter {
this.config = this.loadConfig();
this.history = this.loadHistory();
this.currentStatus = new Map();
// DC-086: the status the dashboard SHOULD display (post-hysteresis).
// Distinct from currentStatus, which is the latest raw probe result.
this.displayedStatus = new Map();
// DC-086: counter of consecutive healthy/unhealthy probes since the
// last displayed-status change. Reset to 0 whenever displayed status flips.
this.consecutiveSinceChange = new Map();
this.incidents = [];
this.checking = false;
this.checkInterval = null;
this.consecutiveFailures = new Map(); // serviceId -> failure count
this.serviceTimers = new Map(); // serviceId -> timer for per-service backoff
// Invalidate probe completions that race with removal/reconfiguration.
this.serviceGenerations = new Map(); // serviceId -> configuration generation
// DC-088: monotonically increasing sequence so generation numbers can never
// repeat across remove -> re-add cycles (prevents ABA on the stale check).
this.generationSeq = 0;
// DC-088: serviceId -> { generation, removedAt } tombstones. A live entry in
// serviceGenerations means the service is (re)configured; a tombstone with a
// HIGHER generation than the captured one marks the capture as stale. Entry
// is deleted when the service is removed, so the live map cannot leak.
this.removedGenerations = new Map();
}
/**
@@ -111,11 +151,27 @@ class HealthChecker extends EventEmitter {
this.cleanupHistory();
}
/**
* DC-088: true when a probe's captured generation no longer matches the
* service's current configuration state. A live serviceGenerations entry
* must match exactly. With no live entry the service was never configured
* in this process (disk-loaded / direct callers) stale only if a removal
* tombstone with a HIGHER generation exists.
*/
_isStaleCapture(serviceId, generation) {
if (this.serviceGenerations.has(serviceId)) {
return this.serviceGenerations.get(serviceId) !== generation;
}
const tomb = this.removedGenerations.get(serviceId);
return Boolean(tomb && tomb.generation > generation);
}
/**
* Check a single service
*/
async checkService(serviceId, config) {
const startTime = Date.now();
const generation = this.serviceGenerations.get(serviceId) || 0;
try {
const result = await this.performHealthCheck(config);
@@ -131,6 +187,10 @@ class HealthChecker extends EventEmitter {
details: result.details
};
if (this._isStaleCapture(serviceId, generation)) {
return status;
}
// Track consecutive failures for exponential backoff
if (result.healthy) {
this.consecutiveFailures.delete(serviceId);
@@ -138,16 +198,15 @@ class HealthChecker extends EventEmitter {
this.consecutiveFailures.set(serviceId, (this.consecutiveFailures.get(serviceId) || 0) + 1);
}
const previousStatus = this.currentStatus.get(serviceId);
const previousDisplayed = this.displayedStatus.get(serviceId);
this.recordStatus(serviceId, status);
this.checkForIncidents(serviceId, status, config);
this.checkForIncidents(serviceId, status, config, previousStatus, previousDisplayed);
return status;
} catch (error) {
const responseTime = Date.now() - startTime;
// Increment failure count for backoff
this.consecutiveFailures.set(serviceId, (this.consecutiveFailures.get(serviceId) || 0) + 1);
const status = {
serviceId,
timestamp: new Date().toISOString(),
@@ -156,8 +215,18 @@ class HealthChecker extends EventEmitter {
error: error.message
};
if (this._isStaleCapture(serviceId, generation)) {
return status;
}
// Increment failure count for backoff — only after the result is known
// to be non-stale, so a removed service cannot re-create map entries.
this.consecutiveFailures.set(serviceId, (this.consecutiveFailures.get(serviceId) || 0) + 1);
const previousStatus = this.currentStatus.get(serviceId);
const previousDisplayed = this.displayedStatus.get(serviceId);
this.recordStatus(serviceId, status);
this.checkForIncidents(serviceId, status, config);
this.checkForIncidents(serviceId, status, config, previousStatus, previousDisplayed);
return status;
}
@@ -273,27 +342,109 @@ class HealthChecker extends EventEmitter {
return true;
}
/**
* Compute the displayed status for a service given the latest raw probe
* result. Applies asymmetric hysteresis:
* - Going DOWN: requires DOWN_THRESHOLD (default 2) consecutive "down"
* probes since the last display-state change. A single blip keeps the
* badge green.
* - Going UP: requires UP_THRESHOLD (default 1) consecutive "up" probes.
* Any single "up" after a down streak flips back to green so the badge
* doesn't linger red after the service has recovered.
*
* Returns the displayed status object (same shape as the raw status) so
* recordStatus can use it both for the displayed map and as the broadcast
* payload when the displayed status actually changes.
*/
_computeDisplayedStatus(serviceId, rawStatus) {
const currentDisplayed = this.displayedStatus.get(serviceId);
const previousStatus = currentDisplayed ? currentDisplayed.status : null;
// If no prior state, accept the raw probe as-is (first-check bootstrap).
if (!previousStatus) {
return rawStatus;
}
// Probe agrees with current displayed → no change, reset the counter so
// a brief blip doesn't accumulate against the displayed state.
if (rawStatus.status === previousStatus) {
this.consecutiveSinceChange.set(serviceId, 0);
return rawStatus;
}
// Probe disagrees with displayed. Bump the streak counter — this counts
// CONSECUTIVE probes that disagree with what's shown, regardless of
// whether the raw value itself changed between probes. That's what
// makes "down, down" flip after threshold but "down, up, down" not flip.
const prev = this.consecutiveSinceChange.get(serviceId) || 0;
const next = prev + 1;
if (rawStatus.status === 'down') {
// Going DOWN: need DOWN_THRESHOLD consecutive probes that disagree
// with the displayed "up" state.
if (previousStatus === 'up' && next < DOWN_THRESHOLD) {
this.consecutiveSinceChange.set(serviceId, next);
// Keep the last internally-consistent displayed snapshot. Mixing the
// raw failure metadata with status="up" would expose contradictory
// API data (for example statusCode=500 on an "up" service).
return currentDisplayed;
}
// Threshold met (or already down) — flip to red.
this.consecutiveSinceChange.set(serviceId, 0);
return rawStatus;
}
// rawStatus.status === 'up' (must be — the equal-to-displayed case above
// already returned). Going UP after a down streak: need UP_THRESHOLD.
if (previousStatus === 'down' && next < UP_THRESHOLD) {
this.consecutiveSinceChange.set(serviceId, next);
return currentDisplayed;
}
this.consecutiveSinceChange.set(serviceId, 0);
return rawStatus;
}
/**
* Record service status
*
* DC-086: history + consecutiveFailures are updated for EVERY probe
* (operators want full probe history for postmortems). The dashboard's
* `status-check` event is only emitted when the DISPLAYED status changes,
* so the badge stops re-rendering on every probe.
*/
recordStatus(serviceId, status) {
// Update current status
// Update current (raw) status — used by checkForIncidents and history.
this.currentStatus.set(serviceId, status);
// Add to history
// Add raw probe to history (full fidelity — operators rely on this).
if (!this.history[serviceId]) {
this.history[serviceId] = [];
}
this.history[serviceId].push(status);
// Cap entries to prevent unbounded growth (disk explosion fix)
if (this.history[serviceId].length > MAX_ENTRIES_PER_SERVICE) {
this.history[serviceId] = this.history[serviceId].slice(-MAX_ENTRIES_PER_SERVICE);
}
// Emit status event
this.emit('status-check', status);
// Compute the post-hysteresis displayed status; only emit when it changes.
// _computeDisplayedStatus compares the raw probe against the DISPLAYED
// status (not the previous raw status), so the "consecutive since
// change" counter doesn't depend on the order of writes here.
const displayed = this._computeDisplayedStatus(serviceId, status);
const previousDisplayed = this.displayedStatus.get(serviceId);
const displayChanged =
!previousDisplayed || previousDisplayed.status !== displayed.status;
this.displayedStatus.set(serviceId, displayed);
if (displayChanged) {
// Emit with the displayed status so the dashboard renders the same
// state the hysteresis just decided. The raw probe result is still
// in `history` and `currentStatus` for anyone who wants it.
this.emit('status-check', displayed);
}
// Save history periodically
if (Math.random() < 0.05) { // 5% chance (every ~20 checks)
@@ -304,11 +455,27 @@ class HealthChecker extends EventEmitter {
/**
* Check for incidents (downtime, slow response, etc.)
*/
checkForIncidents(serviceId, status, config) {
const previous = this.currentStatus.get(serviceId);
checkForIncidents(serviceId, status, config, previous = this.currentStatus.get(serviceId), previousDisplayed = null) {
// Check for status change (up -> down or down -> up)
if (previous && previous.status !== status.status) {
// DC-090: outage incidents follow the DISPLAYED (post-hysteresis) status —
// the same signal that flips the dashboard badge. A single raw "down"
// blip that hysteresis suppresses must not open a critical outage
// incident (and a suppressed blip must not resolve a real one). When the
// caller supplies the pre-probe displayed state (checkService always
// does), transitions are evaluated displayed-vs-displayed using the
// post-recordStatus state in this.displayedStatus. Direct callers with
// no hysteresis state (previousDisplayed === null) keep the legacy
// raw-probe transition semantics.
if (previousDisplayed) {
const displayed = this.displayedStatus.get(serviceId);
if (displayed && displayed.status !== previousDisplayed.status) {
if (displayed.status === 'down') {
this.createIncident(serviceId, 'outage', 'Service is down', displayed);
} else if (displayed.status === 'up') {
this.resolveIncident(serviceId, 'outage', displayed);
}
}
} else if (previous && previous.status !== status.status) {
if (status.status === 'down') {
this.createIncident(serviceId, 'outage', 'Service is down', status);
} else if (status.status === 'up') {
@@ -445,19 +612,29 @@ class HealthChecker extends EventEmitter {
}
/**
* Get current status for all services
* Get current status for all services.
*
* DC-086: returns the DISPLAYED status (post-hysteresis), not the latest
* raw probe. A page reload should show the same badge state the live
* SSE stream is currently showing otherwise an operator who reloads
* the page after a single blip sees red even though the hysteresis kept
* the badge green for them.
*/
getCurrentStatus() {
const result = {};
for (const [serviceId, status] of this.currentStatus.entries()) {
for (const [serviceId, rawStatus] of this.currentStatus.entries()) {
const config = this.config.services[serviceId];
const uptime24h = this.calculateUptime(serviceId, 24);
const uptime7d = this.calculateUptime(serviceId, 168);
const avgResponseTime = this.calculateAverageResponseTime(serviceId, 24);
// Prefer the displayed status if we've already computed one; fall back
// to the raw probe on the very first call (before recordStatus has run).
const displayed = this.displayedStatus.get(serviceId) || rawStatus;
result[serviceId] = {
...status,
...displayed,
name: config?.name || serviceId,
uptime: {
'24h': uptime24h,
@@ -467,7 +644,7 @@ class HealthChecker extends EventEmitter {
sla: config?.sla
};
}
return result;
}
@@ -530,6 +707,13 @@ class HealthChecker extends EventEmitter {
this.config.services = {};
}
// DC-088: monotonic instance-wide sequence — a re-added service can never
// recycle a previous generation number, and any older in-flight capture is
// invalidated by definition.
this.generationSeq += 1;
this.serviceGenerations.set(serviceId, this.generationSeq);
// Re-configuration supersedes any prior removal tombstone.
this.removedGenerations.delete(serviceId);
this.config.services[serviceId] = {
enabled: config.enabled !== false,
name: config.name || serviceId,
@@ -552,12 +736,42 @@ class HealthChecker extends EventEmitter {
* Remove service configuration
*/
removeService(serviceId) {
// DC-088: tombstone the captured generation instead of leaking an entry.
// The live map entry is deleted; an in-flight probe captured BEFORE this
// point sees no live entry but a higher tombstone generation, so it is
// discarded. configureService clears the tombstone on re-add.
this.generationSeq += 1;
this.serviceGenerations.delete(serviceId);
this.removedGenerations.set(serviceId, {
generation: this.generationSeq,
removedAt: Date.now()
});
if (this.config.services) {
delete this.config.services[serviceId];
this.saveConfig();
}
// DC-088: open incidents for a removed service must not linger forever.
// Close them through the same resolve path a recovery would, annotated so
// history shows why (dashboard renders resolved incidents green + duration).
for (const incident of this.incidents) {
if (incident.serviceId === serviceId && incident.status === 'open') {
incident.status = 'resolved';
incident.resolvedAt = new Date().toISOString();
incident.duration = new Date(incident.resolvedAt) - new Date(incident.createdAt);
incident.resolvedBy = 'service-removed';
this.emit('incident-resolved', incident);
this.emit('log', 'info', `Incident closed by service removal: ${incident.id}`);
}
}
this.currentStatus.delete(serviceId);
this.displayedStatus.delete(serviceId);
this.consecutiveSinceChange.delete(serviceId);
this.consecutiveFailures.delete(serviceId);
const timer = this.serviceTimers.get(serviceId);
if (timer) clearTimeout(timer);
this.serviceTimers.delete(serviceId);
delete this.history[serviceId];
}
@@ -576,6 +790,18 @@ class HealthChecker extends EventEmitter {
this.history[serviceId] = this.history[serviceId].slice(-MAX_ENTRIES_PER_SERVICE);
}
}
// DC-088: sweep expired removal tombstones. After the TTL no probe that
// captured a pre-removal generation can still be in flight (timeout is
// bounded by performHealthCheck), so the tombstone has done its job.
if (this.removedGenerations.size > 0) {
const now = Date.now();
for (const [serviceId, tomb] of this.removedGenerations) {
if (now - tomb.removedAt > REMOVED_GENERATION_TTL_MS) {
this.removedGenerations.delete(serviceId);
}
}
}
}
/**
+82 -3
View File
@@ -47,6 +47,53 @@ const ALLOWED_PUBLIC_TTLS = new Set([60 * 60 * 1000, 24 * 60 * 60 * 1000, 7 * 24
const TAILSCALE_MAX_USES = 1;
const PUBLIC_DEFAULT_SUBSCRIBE_CAP = 1000; // bound on subscribe events per link
// DC-083: Public share endpoint input bounds. The two CSRF-exempt public
// endpoints accept untrusted body fields — bound shape, length, charset so
// an attacker can't bloat data/shares.json, inject CRLF into fields that
// flow into Tailscale auth-key descriptions, or smuggle control chars into
// the on-disk store. See routes/share.js for the route-layer validation;
// these helpers are the defense-in-depth belt under the route's suspenders.
const PUBLIC_EMAIL_REGEX = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
const PUBLIC_EMAIL_MAX_LENGTH = 254; // RFC 5321 §4.5.3.1.3
const PUBLIC_DEVICE_ID_REGEX = /^[a-zA-Z0-9._:-]+$/;
const PUBLIC_DEVICE_ID_MIN_LENGTH = 1;
const PUBLIC_DEVICE_ID_MAX_LENGTH = 128;
function validatePublicEmail(raw) {
if (typeof raw !== 'string') return { ok: false, reason: 'invalid_email' };
// Reject control chars / NUL / CR / LF before they can corrupt the on-disk
// JSON or be embedded in subsequent log lines. RFC 5321 forbids these in
// SMTP addresses; we mirror that at the API layer.
if (raw.length === 0 || raw.length > PUBLIC_EMAIL_MAX_LENGTH) {
return { ok: false, reason: 'invalid_email' };
}
// eslint-disable-next-line no-control-regex
if (/[\x00-\x1f\x7f]/.test(raw)) return { ok: false, reason: 'invalid_email' };
// The local-part can technically contain `+`, `.`, `_`, `%`, `-`; the
// domain part must have at least one dot and a 2+ letter TLD. Reject
// quote-bracket forms (RFC 5321 obs-quote-text) — we don't accept them.
if (!PUBLIC_EMAIL_REGEX.test(raw)) return { ok: false, reason: 'invalid_email' };
// Block obvious shell-attachment characters that the regex doesn't catch.
if (/[<>{}|\\^`\s]/.test(raw)) return { ok: false, reason: 'invalid_email' };
return { ok: true, email: raw.toLowerCase() };
}
function validatePublicDeviceId(raw) {
if (typeof raw !== 'string') return { ok: false, reason: 'invalid_device_id' };
if (raw.length < PUBLIC_DEVICE_ID_MIN_LENGTH || raw.length > PUBLIC_DEVICE_ID_MAX_LENGTH) {
return { ok: false, reason: 'invalid_device_id' };
}
// Tailscale machine IDs are base64url-with-hyphens; we accept a slightly
// broader charset (`._:-`) to also accommodate hostname-style IDs and
// Caddy's `forward_auth` device headers. Reject CR/LF/NUL/TAB explicitly
// so a smuggled control char can't break out of the Tailscale auth-key
// description string in routes/share.js:213.
// eslint-disable-next-line no-control-regex
if (/[\x00-\x1f\x7f]/.test(raw)) return { ok: false, reason: 'invalid_device_id' };
if (!PUBLIC_DEVICE_ID_REGEX.test(raw)) return { ok: false, reason: 'invalid_device_id' };
return { ok: true, deviceId: raw };
}
function _nowMs() { return Date.now(); }
function _nowIso() { return new Date().toISOString(); }
@@ -327,8 +374,18 @@ function createShareStore(opts = {}) {
});
}
function recordPublicSubscribe(token) {
function recordPublicSubscribe(token, { email } = {}) {
return _enqueue(() => {
// DC-083: validate the optional subscriber email at the store layer too.
// The route layer validates first; this is the defense-in-depth catch
// for direct callers (cron sweepers, internal jobs, future endpoints).
// `email` is OPT-IN — callers omitting it get the original behavior.
let normalizedEmail = null;
if (email !== undefined && email !== null) {
const v = validatePublicEmail(email);
if (!v.ok) return { ok: false, reason: v.reason };
normalizedEmail = v.email;
}
const data = _load();
const hash = _sha256(token);
const s = _findByHash(data, hash);
@@ -341,6 +398,16 @@ function createShareStore(opts = {}) {
const cap = s.subscribeCap || PUBLIC_DEFAULT_SUBSCRIBE_CAP;
if (s.subscribeCount >= cap) return { ok: false, reason: 'cap_reached' };
s.subscribeCount += 1;
// DC-083: record the last submitting email (capped to 8 entries to
// bound the on-disk size). PII minimization — we keep only the hash
// + last 8 emails; full email log would grow unbounded.
if (normalizedEmail) {
if (!Array.isArray(s.subscriberEmails)) s.subscriberEmails = [];
s.subscriberEmails.push(normalizedEmail);
if (s.subscriberEmails.length > 8) {
s.subscriberEmails.splice(0, s.subscriberEmails.length - 8);
}
}
_save(data);
return { ok: true, count: s.subscribeCount, cap };
});
@@ -348,6 +415,18 @@ function createShareStore(opts = {}) {
function recordTailscaleUse(token, { deviceId } = {}) {
return _enqueue(() => {
// DC-083: validate deviceId at the store layer. The pre-fix code
// accepted ANY string of any length, including control chars and
// CR/LF — which would flow into the Tailscale auth-key description
// (routes/share.js:213) and into the on-disk shares.json. Reject
// early so an attacker can't bloat the store or smuggle characters
// out of the Tailscale description field.
let normalizedDeviceId = 'unknown';
if (deviceId !== undefined && deviceId !== null) {
const v = validatePublicDeviceId(deviceId);
if (!v.ok) return { ok: false, reason: v.reason };
normalizedDeviceId = v.deviceId;
}
const data = _load();
const hash = _sha256(token);
const s = _findByHash(data, hash);
@@ -359,7 +438,7 @@ function createShareStore(opts = {}) {
return { ok: false, reason: 'expired' };
}
s.usedAt = _nowIso();
s.usedBy = typeof deviceId === 'string' ? deviceId : 'unknown';
s.usedBy = normalizedDeviceId;
_save(data);
return { ok: true, share: _publicView(s) };
});
@@ -411,4 +490,4 @@ function createShareStore(opts = {}) {
};
}
module.exports = { createShareStore };
module.exports = { createShareStore, validatePublicEmail, validatePublicDeviceId };
+5 -1
View File
@@ -14,7 +14,11 @@ const KNOWN_KEYS = [
'configurationType', 'defaults', 'customLogo', 'customFavicon',
'dashboardTitle', 'tailscale', 'license', 'skipped',
'routingMode', 'domain', 'email', 'defaultIP', 'pylon',
'customLogoDark', 'customLogoLight', 'language'
'customLogoDark', 'customLogoLight', 'language',
// license-manager.js persists the last activation to config.licenseBackup
// (restore-on-restart path); src/config/migrations.js stamps _version.
// Both are first-party writes — see DC-091.
'licenseBackup', '_version'
];
/**
+11
View File
@@ -79,6 +79,17 @@ const RATE_LIMITS = {
windowMs: 15 * 60 * 1000,
max: 10,
},
// DC-083: Public share endpoint limiter. The two CSRF-exempt public
// endpoints (POST /share/:token/subscribe + POST /share/:token/redeem-tailscale)
// mutate on-disk state (data/shares.json). Bound them tighter than the
// general limiter (1000/15min) so a single attacker can't bloat the
// store or saturate the tmp+rename writer. 30/15min is enough for a
// legitimate user clicking "subscribe" once or twice — anything beyond
// is abuse.
SHARE_PUBLIC: {
windowMs: 15 * 60 * 1000,
max: 30,
},
};
// ── Caddy ─────────────────────────────────────────────────────
+9 -4
View File
@@ -330,17 +330,22 @@ module.exports = function configureMiddleware(app, {
const ssoHandoffTokens = new Map();
const SSO_HANDOFF_TTL_MS = 60 * 1000;
function createHandoffToken() {
function createHandoffToken(expectedHost = null) {
const token = crypto.randomBytes(24).toString('base64url');
ssoHandoffTokens.set(token, { exp: Date.now() + SSO_HANDOFF_TTL_MS });
ssoHandoffTokens.set(token, {
exp: Date.now() + SSO_HANDOFF_TTL_MS,
expectedHost: expectedHost ? String(expectedHost).toLowerCase() : null,
});
return token;
}
function redeemHandoffToken(token) {
function redeemHandoffToken(token, actualHost = null) {
if (!token) return false;
const entry = ssoHandoffTokens.get(token);
ssoHandoffTokens.delete(token); // one-time use regardless of outcome
return !!entry && entry.exp > Date.now();
if (!entry || entry.exp <= Date.now()) return false;
if (!entry.expectedHost) return true;
return !!actualHost && entry.expectedHost === String(actualHost).toLowerCase();
}
function setHostOnlySessionCookie(res, durationKey) {
+6 -1
View File
@@ -630,10 +630,15 @@ generate_caddyfile() {
SNIP
local auth_snippet="(dashcaddy_auth) {
forward_auth localhost:${API_PORT} {
@needsAuth not path /dashcaddy-sso
forward_auth @needsAuth localhost:${API_PORT} {
uri /api/v1/auth/gate/{args[0]}
copy_headers Authorization X-Api-Key X-App-Cookie X-Emby-Token X-Plex-Token
}
handle /dashcaddy-sso {
rewrite * /api/v1/auth/sso-exchange
reverse_proxy localhost:${API_PORT}
}
}"
local site_body=" root * ${DASHBOARD_DIR}
@@ -51,10 +51,15 @@ class CaddyfileGenerator {
_authSnippet(apiPort) {
return `# DashCaddy SSO auth snippet
(dashcaddy_auth) {
forward_auth localhost:${apiPort} {
@needsAuth not path /dashcaddy-sso
forward_auth @needsAuth localhost:${apiPort} {
uri /api/v1/auth/gate/{args[0]}
copy_headers Authorization X-Api-Key X-App-Cookie X-Emby-Token X-Plex-Token
}
handle /dashcaddy-sso {
rewrite * /api/v1/auth/sso-exchange
reverse_proxy localhost:${apiPort}
}
}
`;
}
@@ -0,0 +1,35 @@
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
const CaddyfileGenerator = require('./caddyfile-generator');
describe('cross-host SSO installer contract', () => {
test('generated auth snippet exposes the public one-time exchange landing route', () => {
const snippet = new CaddyfileGenerator()._authSnippet(3001);
expect(snippet).toContain('@needsAuth not path /dashcaddy-sso');
expect(snippet).toContain('handle /dashcaddy-sso');
expect(snippet).toContain('rewrite * /api/v1/auth/sso-exchange');
expect(snippet).toContain('reverse_proxy localhost:3001');
});
test('shell installer emits the same exchange landing contract', () => {
const installer = fs.readFileSync(path.join(__dirname, '..', '..', 'install.sh'), 'utf8');
expect(installer).toContain('@needsAuth not path /dashcaddy-sso');
expect(installer).toContain('handle /dashcaddy-sso');
expect(installer).toContain('rewrite * /api/v1/auth/sso-exchange');
});
test('Caddy parser accepts a complete service config using the generated snippet', () => {
const available = spawnSync('caddy', ['version'], { encoding: 'utf8' });
if (available.status !== 0) return;
const generator = new CaddyfileGenerator();
const config = `${generator._authSnippet(3001)}\nexample.test {\n import dashcaddy_auth plex\n respond "ok" 200\n}\n`;
const result = spawnSync('caddy', ['validate', '--config', '-', '--adapter', 'caddyfile'], {
input: config,
encoding: 'utf8',
});
expect(result.status).toBe(0);
expect(`${result.stdout}\n${result.stderr}`).toContain('Valid configuration');
});
});
+1
View File
@@ -28,6 +28,7 @@ const bundles = {
// totp-recovery.js registers window._refreshRecoveryLink which totp-auth.js
// calls from showTotpOverlay(). Must come after totp-auth.js.
JS('totp-recovery.js'),
JS('credential-vault-handoff.js'),
JS('service-credentials.js'),
JS('totp-settings.js'),
// DC-048 admin panel — modal-overlay UI for user/invite management.
+108 -108
View File
File diff suppressed because one or more lines are too long
+60 -60
View File
File diff suppressed because one or more lines are too long
+7 -7
View File
File diff suppressed because one or more lines are too long
+65 -4
View File
@@ -216,8 +216,8 @@
_el('input', { name: 'ttlHours', type: 'number', min: '1', max: '168', value: '24', style: 'padding:6px;width:80px' }),
));
form.appendChild(_el('label', { style: 'display:flex;gap:4px;align-items:center;font-size:0.85rem' },
_el('input', { name: 'sendEmail', type: 'checkbox', checked: true }),
_el('span', { text: 'Send email' }),
_el('input', { name: 'sendEmail', type: 'checkbox', checked: false }),
_el('span', { text: 'Also send via email (optional)' }),
));
form.appendChild(_el('button', { type: 'submit', class: 'btn-sm', style: 'padding:6px 12px', text: 'Issue invite' }));
container.appendChild(form);
@@ -297,16 +297,77 @@
},
});
banner.appendChild(copyBtn);
if (invite.deliveredVia === 'dev-console') {
// DC-085: pre-formatted message for one-tap paste into iMessage / WhatsApp /
// Telegram / SMS / Signal / Discord / paste-into-email. The operator can
// copy this as a sentence instead of dealing with the raw URL.
if (invite.shareText) {
const shareBlock = _el('div', { style: 'margin-top:12px' });
shareBlock.appendChild(_el('div', {
style: 'font-size:0.8rem;color:#86efac;margin-bottom:4px',
text: 'Share this message:',
}));
shareBlock.appendChild(_el('div', {
style: 'padding:8px;background:#000;border-radius:4px;color:#d1fae5;white-space:pre-wrap',
text: invite.shareText,
}));
const shareActions = _el('div', { style: 'margin-top:6px;display:flex;gap:6px;flex-wrap:wrap' });
const copyTextBtn = _el('button', {
class: 'btn-sm', style: 'padding:4px 10px',
text: 'Copy message',
onclick: async () => {
try {
await navigator.clipboard.writeText(invite.shareText);
copyTextBtn.textContent = 'Copied!';
setTimeout(() => { copyTextBtn.textContent = 'Copy message'; }, 2000);
} catch (e) {
window.errorHandler && window.errorHandler.show('Clipboard blocked: select the text manually.');
}
},
});
shareActions.appendChild(copyTextBtn);
// Native share sheet on mobile / supported browsers. Falls back silently
// (the copy buttons cover the same intent).
if (typeof navigator !== 'undefined' && typeof navigator.share === 'function') {
const nativeShareBtn = _el('button', {
class: 'btn-sm', style: 'padding:4px 10px',
text: 'Share via…',
onclick: async () => {
try {
await navigator.share({
title: 'DashCaddy invite',
text: invite.shareText,
url: invite.acceptUrl,
});
} catch (e) {
// User-cancelled throws AbortError — that's fine, just stay quiet.
if (e && e.name && e.name !== 'AbortError') {
window.errorHandler && window.errorHandler.show('Share failed: ' + e.message);
}
}
},
});
shareActions.appendChild(nativeShareBtn);
}
shareBlock.appendChild(shareActions);
banner.appendChild(shareBlock);
}
if (invite.deliveredVia === 'failed') {
banner.appendChild(_el('p', {
style: 'margin-top:8px;color:#fbbf24;font-size:0.8rem',
text: 'SMTP not configured the invite was logged to the server console (search for [DC-048-DEV-INVITE-LINK]).',
text: 'Email could not be sent (SMTP not configured). Share the link above instead — it works the same way.',
}));
} else if (invite.deliveredVia === 'email') {
banner.appendChild(_el('p', {
style: 'margin-top:8px;color:#86efac;font-size:0.8rem',
text: 'Email sent to ' + invite.email + '.',
}));
} else if (invite.deliveredVia === 'manual') {
banner.appendChild(_el('p', {
style: 'margin-top:8px;color:#86efac;font-size:0.8rem',
text: 'Share the link above via text, chat, or any messenger.',
}));
}
parent.appendChild(banner);
}
+46 -2
View File
@@ -267,6 +267,46 @@
}
}
function buildSsoHandoffTarget(returnUrl, token) {
const parsed = new URL(returnUrl, window.location.origin);
if (parsed.origin === window.location.origin) return parsed.toString();
const suffix = SITE.tld.startsWith('.') ? SITE.tld : `.${SITE.tld}`;
const isPrivateHost = parsed.hostname === suffix.slice(1) || parsed.hostname.endsWith(suffix);
if (parsed.protocol !== 'https:' || !isPrivateHost || !token) return null;
const returnPath = `${parsed.pathname}${parsed.search}${parsed.hash}`;
parsed.pathname = '/dashcaddy-sso';
parsed.search = '';
parsed.hash = '';
parsed.searchParams.set('token', token);
parsed.searchParams.set('return', returnPath);
return parsed.toString();
}
async function resumeExistingSession(returnUrl) {
if (!returnUrl || !isAllowedReturnUrl(returnUrl)) return false;
try {
const parsedReturn = new URL(returnUrl, window.location.origin);
const suffix = SITE.tld.startsWith('.') ? SITE.tld : `.${SITE.tld}`;
const serviceId = parsedReturn.hostname.slice(0, -suffix.length);
if (!/^[a-z0-9][a-z0-9-]*$/.test(serviceId)) return false;
const res = await fetch(`/api/v1/auth/sso-handoff?serviceId=${encodeURIComponent(serviceId)}`, {
credentials: 'include',
cache: 'no-store',
});
if (!res.ok) return false;
const data = await res.json();
const target = data.success && buildSsoHandoffTarget(returnUrl, data.ssoToken);
if (!target) return false;
try { sessionStorage.removeItem('totp_redirect'); } catch (_) {}
window.location.replace(target);
return true;
} catch (_) {
return false;
}
}
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get('auth') === 'required') {
// Preserve the gated service destination so submitTotpCode() can append
@@ -277,8 +317,12 @@
}
// Clean URL — happens after we've captured the redirect
window.history.replaceState({}, '', window.location.pathname);
// Show on next tick so the DOM (the .totp-card) is ready
setTimeout(show, 0);
// Reuse the valid status.sami session first. Only show the TOTP/provider
// challenge when that session is genuinely absent or expired.
setTimeout(async () => {
if (await resumeExistingSession(returnUrl)) return;
await show();
}, 0);
}
// Expose for hot-trigger from other modules (e.g. logout)
+55 -52
View File
@@ -33,6 +33,20 @@
return server?.name || dnsId.toUpperCase();
}
async function requireSuccessfulDnsMutation(response, label) {
if (!response) throw new Error(`${label} failed: no response`);
let data;
try {
data = await response.json();
} catch (_) {
throw new Error(`${label} failed: invalid server response`);
}
if (!response.ok || data?.success !== true) {
throw new Error(data?.error || `${label} failed (${response.status || 'unknown status'})`);
}
return data;
}
/** Build per-server credential form sections from SITE.dnsServers */
function buildCredentialSections() {
const container = document.getElementById('dns-cred-sections');
@@ -258,14 +272,6 @@
document.getElementById('token-save')?.addEventListener('click', async () => {
const dnsIds = getDnsIds();
// Save all to localStorage
dnsIds.forEach(dnsId => {
setUsername(dnsId, 'readonly', document.getElementById(`${dnsId}-readonly-username`).value.trim());
setToken(dnsId, 'readonly', document.getElementById(`${dnsId}-readonly-token`).value.trim());
setUsername(dnsId, 'admin', document.getElementById(`${dnsId}-admin-username`).value.trim());
setToken(dnsId, 'admin', document.getElementById(`${dnsId}-admin-token`).value.trim());
});
// Build per-server credentials payload for backend sync
const servers = {};
let hasAnyCreds = false;
@@ -304,45 +310,36 @@
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ servers })
});
const data = await res.json();
const data = await requireSuccessfulDnsMutation(res, 'DNS credential save');
if (data.results) {
dnsIds.forEach(dnsId => {
const statusEl = document.getElementById(`${dnsId}-token-status`);
if (!servers[dnsId]) { statusEl.textContent = ''; return; }
const result = data.results[dnsId];
if (result?.success) {
statusEl.textContent = '\u2713 Verified & saved';
statusEl.className = 'token-status success';
} else if (result?.partial) {
statusEl.textContent = '\u2713 ' + result.partial;
statusEl.className = 'token-status success';
} else {
statusEl.textContent = '\u2717 ' + (result?.error || 'Login failed');
statusEl.className = 'token-status error';
}
});
} else if (data.success) {
dnsIds.forEach(dnsId => {
if (servers[dnsId]) {
document.getElementById(`${dnsId}-token-status`).textContent = '\u2713 Saved';
document.getElementById(`${dnsId}-token-status`).className = 'token-status success';
}
});
} else {
dnsIds.forEach(dnsId => {
if (servers[dnsId]) {
document.getElementById(`${dnsId}-token-status`).textContent = '\u2717 ' + (data.error || 'Failed');
document.getElementById(`${dnsId}-token-status`).className = 'token-status error';
}
});
const failed = Object.keys(servers).filter(dnsId => data.results[dnsId]?.success !== true);
if (failed.length) {
const details = failed.map(dnsId => data.results[dnsId]?.error || `${dnsId} failed`).join('; ');
throw new Error(details);
}
}
// Cache locally only after the encrypted server vault confirms success.
dnsIds.forEach(dnsId => {
setUsername(dnsId, 'readonly', document.getElementById(`${dnsId}-readonly-username`).value.trim());
setToken(dnsId, 'readonly', document.getElementById(`${dnsId}-readonly-token`).value.trim());
setUsername(dnsId, 'admin', document.getElementById(`${dnsId}-admin-username`).value.trim());
setToken(dnsId, 'admin', document.getElementById(`${dnsId}-admin-token`).value.trim());
});
dnsIds.forEach(dnsId => {
const statusEl = document.getElementById(`${dnsId}-token-status`);
if (!servers[dnsId]) { statusEl.textContent = ''; return; }
const result = data.results?.[dnsId];
statusEl.textContent = result?.partial ? '\u2713 ' + result.partial : '\u2713 Verified & saved';
statusEl.className = 'token-status success';
});
} catch (e) {
console.error('Failed to sync DNS credentials to backend:', e);
dnsIds.forEach(dnsId => {
if (servers[dnsId]) {
document.getElementById(`${dnsId}-token-status`).textContent = '\u2713 Saved locally (sync failed)';
document.getElementById(`${dnsId}-token-status`).className = 'token-status';
document.getElementById(`${dnsId}-token-status`).textContent = '\u2717 ' + (e.message || 'Save failed');
document.getElementById(`${dnsId}-token-status`).className = 'token-status error';
}
});
}
@@ -368,18 +365,24 @@
document.getElementById('token-clear-all')?.addEventListener('click', async () => {
if (confirm('Clear all stored DNS credentials? This cannot be undone.')) {
clearAllCredentials();
getDnsIds().forEach(dnsId => {
document.getElementById(`${dnsId}-readonly-username`).value = '';
document.getElementById(`${dnsId}-readonly-token`).value = '';
document.getElementById(`${dnsId}-admin-username`).value = '';
document.getElementById(`${dnsId}-admin-token`).value = '';
document.getElementById(`${dnsId}-token-status`).textContent = '\u2713 Cleared';
document.getElementById(`${dnsId}-token-status`).className = 'token-status success';
});
try {
await secureFetch('/api/v1/dns/credentials', { method: 'DELETE' });
} catch (_) {}
const response = await secureFetch('/api/v1/dns/credentials', { method: 'DELETE' });
await requireSuccessfulDnsMutation(response, 'DNS credential removal');
clearAllCredentials();
getDnsIds().forEach(dnsId => {
document.getElementById(`${dnsId}-readonly-username`).value = '';
document.getElementById(`${dnsId}-readonly-token`).value = '';
document.getElementById(`${dnsId}-admin-username`).value = '';
document.getElementById(`${dnsId}-admin-token`).value = '';
document.getElementById(`${dnsId}-token-status`).textContent = '\u2713 Cleared';
document.getElementById(`${dnsId}-token-status`).className = 'token-status success';
});
} catch (e) {
getDnsIds().forEach(dnsId => {
document.getElementById(`${dnsId}-token-status`).textContent = '\u2717 ' + (e.message || 'Clear failed');
document.getElementById(`${dnsId}-token-status`).className = 'token-status error';
});
}
}
});
+3
View File
@@ -61,6 +61,9 @@
await window.loadServices();
await loadTemplateCategories();
window.buildGrid();
if (typeof window.openRequestedCredentialForm === 'function') {
window.openRequestedCredentialForm();
}
animateTopCards();
window.refreshAll();
setInterval(() => {
+52
View File
@@ -0,0 +1,52 @@
// ===== ENCRYPTED VAULT -> SERVICE SSO HANDOFF =====
(function() {
function isAllowedReturnUrl(returnUrl, expectedServiceId) {
if (!returnUrl || !expectedServiceId || !/^[a-z0-9][a-z0-9-]*$/.test(expectedServiceId)) return false;
try {
const parsed = new URL(returnUrl, window.location.origin);
const suffix = SITE.tld.startsWith('.') ? SITE.tld : `.${SITE.tld}`;
const expectedHost = `${expectedServiceId}${suffix}`;
return parsed.protocol === 'https:' && parsed.hostname === expectedHost;
} catch (_) {
return false;
}
}
function buildHandoffTarget(returnUrl, token, expectedServiceId) {
if (!isAllowedReturnUrl(returnUrl, expectedServiceId)) return null;
const parsed = new URL(returnUrl, window.location.origin);
if (!token) return null;
const returnPath = `${parsed.pathname}${parsed.search}${parsed.hash}`;
// The shared (dashcaddy_auth) Caddy snippet installs this public landing
// route on every protected host. It rewrites to /api/v1/auth/sso-exchange.
parsed.pathname = '/dashcaddy-sso';
parsed.search = '';
parsed.hash = '';
parsed.searchParams.set('token', token);
parsed.searchParams.set('return', returnPath);
return parsed.toString();
}
async function resume(returnUrl, expectedServiceId, runtime = {}) {
if (!isAllowedReturnUrl(returnUrl, expectedServiceId)) return false;
const fetchFn = runtime.fetch || window.fetch.bind(window);
const locationObj = runtime.location || window.location;
try {
const response = await fetchFn(`/api/v1/auth/sso-handoff?serviceId=${encodeURIComponent(expectedServiceId)}`, {
credentials: 'include',
cache: 'no-store',
});
if (!response.ok) return false;
const data = await response.json();
const target = data.success && buildHandoffTarget(returnUrl, data.ssoToken, expectedServiceId);
if (!target) return false;
locationObj.replace(target);
return true;
} catch (_) {
return false;
}
}
window.DCCredentialVault = { isAllowedReturnUrl, buildHandoffTarget, resume };
})();
+30 -14
View File
@@ -240,9 +240,23 @@
document.getElementById('ntfy-server').value = config.providers.ntfy.serverUrl;
}
// email fields
// email fields — DC-092: prefill the FULL form so a save doesn't
// silently wipe fields the GET response previously omitted. Password
// is never returned; when one is stored the field shows a keep-hint
// and an empty submit preserves the stored credential server-side.
if (config.providers?.email?.host) document.getElementById('email-host').value = config.providers.email.host;
if (config.providers?.email?.from) document.getElementById('email-from').value = config.providers.email.from;
if (config.providers?.email?.to) document.getElementById('email-to').value = config.providers.email.to;
if (config.providers?.email?.port) document.getElementById('email-port').value = config.providers.email.port;
if (config.providers?.email?.secure !== undefined) document.getElementById('email-secure').checked = config.providers.email.secure === true;
if (config.providers?.email?.username) document.getElementById('email-user').value = config.providers.email.username;
const emailPassEl = document.getElementById('email-pass');
if (config.providers?.email?.hasPassword) {
emailPassEl.value = '';
emailPassEl.placeholder = 'saved — leave blank to keep';
} else {
emailPassEl.placeholder = 'app password';
}
// Health check
document.getElementById('health-check-enabled').checked = config.healthCheck?.enabled || false;
@@ -254,12 +268,14 @@
`Last check: ${new Date(config.healthCheck.lastCheck).toLocaleString()}`;
}
// Events
document.getElementById('event-container-down').checked = config.events?.containerDown !== false;
document.getElementById('event-container-up').checked = config.events?.containerUp !== false;
document.getElementById('event-deploy-success').checked = config.events?.deploymentSuccess !== false;
document.getElementById('event-deploy-failed').checked = config.events?.deploymentFailed !== false;
document.getElementById('event-resource-alert').checked = config.events?.resourceAlert !== false;
// Events — canonical kebab-case keys, matching the backend store
// (DC-092: previously read camelCase keys that never existed, so
// every toggle re-rendered as 'checked' regardless of stored state).
document.getElementById('event-container-down').checked = config.events?.['container-down'] !== false;
document.getElementById('event-container-up').checked = config.events?.['container-up'] === true;
document.getElementById('event-deploy-success').checked = config.events?.['deploy-success'] !== false;
document.getElementById('event-deploy-failed').checked = config.events?.['deploy-failed'] !== false;
document.getElementById('event-resource-alert').checked = config.events?.['alert'] !== false;
}
} catch (error) {
errorHandler.logError('[Notifications] Load Config', error, { function: 'loadConfig' });
@@ -325,18 +341,18 @@
host: document.getElementById('email-host').value.trim(),
port: parseInt(document.getElementById('email-port').value) || 587,
secure: document.getElementById('email-secure').checked,
user: document.getElementById('email-user').value.trim(),
pass: document.getElementById('email-pass').value.trim(),
username: document.getElementById('email-user').value.trim(),
password: document.getElementById('email-pass').value.trim(),
from: document.getElementById('email-from').value.trim(),
to: document.getElementById('email-to').value.trim()
}
},
events: {
containerDown: document.getElementById('event-container-down').checked,
containerUp: document.getElementById('event-container-up').checked,
deploymentSuccess: document.getElementById('event-deploy-success').checked,
deploymentFailed: document.getElementById('event-deploy-failed').checked,
resourceAlert: document.getElementById('event-resource-alert').checked
'container-down': document.getElementById('event-container-down').checked,
'container-up': document.getElementById('event-container-up').checked,
'deploy-success': document.getElementById('event-deploy-success').checked,
'deploy-failed': document.getElementById('event-deploy-failed').checked,
'alert': document.getElementById('event-resource-alert').checked
},
healthCheck: {
enabled: document.getElementById('health-check-enabled').checked,
+88 -20
View File
@@ -32,8 +32,8 @@
injectModal('service-creds-modal', `<div id="service-creds-modal">
<div class="service-creds-content">
<h3 id="svc-creds-title" style="margin: 0 0 4px; font-size: 1.05rem;">Service Credentials</h3>
<p id="svc-creds-desc" style="font-size: 0.75rem; color: var(--muted); margin: 0 0 14px;">Credentials are injected automatically when accessing this service.</p>
<h3 id="svc-creds-title" style="margin: 0 0 4px; font-size: 1.05rem;">Encrypted Credential Vault</h3>
<p id="svc-creds-desc" style="font-size: 0.75rem; color: var(--muted); margin: 0 0 14px;">Passwords are encrypted at rest and used automatically when you open this service.</p>
<!-- Status indicator -->
<div style="display: flex; align-items: center; gap: 6px; margin-bottom: 12px;">
@@ -91,7 +91,7 @@
<!-- Buttons -->
<div style="display: flex; gap: 8px; margin-top: 14px;">
<button id="svc-creds-save" class="btn-accent-solid" style="flex: 1; padding: 9px; border: none; border-radius: 6px; cursor: pointer; font-weight: 600; font-size: 0.85rem;">
Save
Save to encrypted vault
</button>
<button id="svc-creds-clear" style="padding: 9px 14px; background: transparent; color: var(--bad-fg, #ff9aa3); border: 1px solid var(--bad-fg, #ff9aa3); border-radius: 6px; cursor: pointer; font-size: 0.85rem; display: none;">
Clear
@@ -105,6 +105,8 @@
const modal = document.getElementById('service-creds-modal');
let currentService = null;
let credentialReturnUrl = null;
let currentServiceHadCreds = false;
const arrServices = ['sonarr', 'radarr', 'prowlarr', 'overseerr'];
const qualityProfileServices = ['sonarr', 'radarr'];
@@ -124,8 +126,28 @@
el.style.display = 'none';
}
window.openServiceCredsModal = async function(service) {
async function requireSuccessfulWrite(response, label) {
if (!response) throw new Error(`${label} failed: no response`);
let data;
try {
data = await response.json();
} catch (_) {
throw new Error(`${label} failed: invalid server response`);
}
if (!response.ok || data?.success !== true) {
throw new Error(data?.error || `${label} failed (${response.status || 'unknown status'})`);
}
return data;
}
function isAllowedCredentialReturnUrl(returnUrl, serviceId) {
return !!window.DCCredentialVault?.isAllowedReturnUrl(returnUrl, serviceId);
}
window.openServiceCredsModal = async function(service, options = {}) {
currentService = service;
credentialReturnUrl = isAllowedCredentialReturnUrl(options.returnUrl, service.id) ? options.returnUrl : null;
currentServiceHadCreds = false;
hideError();
const title = document.getElementById('svc-creds-title');
const desc = document.getElementById('svc-creds-desc');
@@ -134,7 +156,10 @@
const basicSection = document.getElementById('svc-creds-basic');
const qualitySection = document.getElementById('svc-creds-quality');
title.textContent = service.name + ' Credentials';
title.textContent = service.name + ' — Encrypted Vault';
document.getElementById('svc-creds-save').textContent = credentialReturnUrl
? 'Save to vault & open service'
: 'Save to encrypted vault';
// Determine which sections to show
const isExt = !!service.isExternal;
const isArr = arrServices.includes(service.id) || arrServices.includes(service.appTemplate);
@@ -214,6 +239,7 @@
}
if (hasCreds) {
currentServiceHadCreds = true;
dot.style.background = 'var(--ok-fg, #74dfc4)';
status.style.color = 'var(--ok-fg, #74dfc4)';
status.textContent = 'Credentials stored';
@@ -352,16 +378,35 @@
const isArr = arrServices.includes(currentService.id) || arrServices.includes(currentService.appTemplate);
const svcId = currentService.id || currentService.appTemplate;
if (credentialReturnUrl && !currentServiceHadCreds) {
const externalUser = document.getElementById('svc-seedhost-user').value.trim();
const externalPass = document.getElementById('svc-seedhost-pass').value;
const apiKeyInput = document.getElementById('svc-apikey-input');
const requestedApiKey = apiKeyInput?.value.trim();
const basicUser = document.getElementById('svc-basic-user').value.trim();
const basicPass = document.getElementById('svc-basic-pass').value;
const hasExternalLogin = currentService.isExternal && externalUser && externalPass;
const hasApiKey = isArr && requestedApiKey && requestedApiKey !== '••••••••';
const hasBasicLogin = !currentService.isExternal && basicUser && basicPass;
if (!hasExternalLogin && !hasApiKey && !hasBasicLogin) {
showError('Enter the login or API key DashCaddy should store for this service.');
saveBtn.textContent = 'Save to vault & open service';
saveBtn.disabled = false;
return;
}
}
// Save seedhost creds (shared username + per-service password)
if (currentService.isExternal) {
const user = document.getElementById('svc-seedhost-user').value.trim();
const pass = document.getElementById('svc-seedhost-pass').value;
if (user) {
await secureFetch('/api/v1/seedhost-creds', {
const response = await secureFetch('/api/v1/seedhost-creds', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: user, password: pass || undefined, serviceId: currentService.id })
});
await requireSuccessfulWrite(response, 'Seedhost credential save');
}
}
@@ -387,23 +432,18 @@
qualityProfileName: qualityProfileName || undefined
})
});
const data = await res.json();
if (!data.success) {
showError(data.error || 'Failed to save API key');
saveBtn.textContent = 'Save';
saveBtn.disabled = false;
return;
}
const data = await requireSuccessfulWrite(res, 'ARR credential save');
if (data.connectionTest && !data.connectionTest.success) {
showError(`API key saved but connection test failed: ${data.connectionTest.error}`);
}
} else {
// Non-arr services use the generic endpoint
await secureFetch(`/api/v1/services/${currentService.id}/credentials`, {
const response = await secureFetch(`/api/v1/services/${currentService.id}/credentials`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ apiKey })
});
await requireSuccessfulWrite(response, 'API key save');
}
} else if (isArr && qualityProfileServices.includes(svcId)) {
// API key unchanged but user may have changed quality profile — save profile only
@@ -411,11 +451,12 @@
const qualityProfileId = qualSelect?.value ? parseInt(qualSelect.value) : undefined;
const qualityProfileName = qualSelect?.selectedOptions?.[0]?.textContent || undefined;
if (qualityProfileId) {
await secureFetch('/api/v1/arr/quality-profiles', {
const response = await secureFetch('/api/v1/arr/quality-profiles', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ service: svcId, qualityProfileId, qualityProfileName })
});
await requireSuccessfulWrite(response, 'Quality profile save');
}
}
@@ -424,20 +465,28 @@
const user = document.getElementById('svc-basic-user').value.trim();
const pass = document.getElementById('svc-basic-pass').value;
if (user && pass) {
await secureFetch(`/api/v1/services/${currentService.id}/credentials`, {
const response = await secureFetch(`/api/v1/services/${currentService.id}/credentials`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: user, password: pass })
});
await requireSuccessfulWrite(response, 'Service credential save');
}
}
await loadServiceCreds(currentService);
if (credentialReturnUrl) {
const returnUrl = credentialReturnUrl;
const resumed = await window.DCCredentialVault?.resume(returnUrl, currentService.id);
if (!resumed) throw new Error('Credential saved, but the secure service handoff failed. Try opening the service again.');
credentialReturnUrl = null;
return;
}
} catch (e) {
errorHandler.logError('[ServiceCredentials] Save', e, { function: 'saveCredentials' });
showError('Failed to save: ' + (e.message || 'Unknown error'));
}
saveBtn.textContent = 'Save';
saveBtn.textContent = credentialReturnUrl ? 'Save to vault & open service' : 'Save to encrypted vault';
saveBtn.disabled = false;
});
@@ -450,12 +499,15 @@
const svcId = currentService.id || currentService.appTemplate;
const isArr = arrServices.includes(svcId);
if (currentService.isExternal) {
await secureFetch(`/api/v1/seedhost-creds?serviceId=${currentService.id}`, { method: 'DELETE' });
const response = await secureFetch(`/api/v1/seedhost-creds?serviceId=${currentService.id}`, { method: 'DELETE' });
await requireSuccessfulWrite(response, 'Seedhost credential removal');
}
// Delete from both namespaces
await secureFetch(`/api/v1/services/${currentService.id}/credentials`, { method: 'DELETE' });
const response = await secureFetch(`/api/v1/services/${currentService.id}/credentials`, { method: 'DELETE' });
await requireSuccessfulWrite(response, 'Service credential removal');
if (isArr) {
await secureFetch(`/api/v1/arr/credentials/${svcId}`, { method: 'DELETE' });
const arrResponse = await secureFetch(`/api/v1/arr/credentials/${svcId}`, { method: 'DELETE' });
await requireSuccessfulWrite(arrResponse, 'ARR credential removal');
}
const btn = document.getElementById(`creds-btn-${currentService.id}`);
if (btn) btn.classList.remove('has-creds');
@@ -470,11 +522,13 @@
document.getElementById('svc-creds-close')?.addEventListener('click', () => {
modal.classList.remove('show');
currentService = null;
credentialReturnUrl = null;
});
modal?.addEventListener('click', (e) => {
if (e.target === modal) {
modal.classList.remove('show');
currentService = null;
credentialReturnUrl = null;
}
});
@@ -501,4 +555,18 @@
}
} catch (e) { /* ignore */ }
};
// Protected service login pages send missing credentials here. Reuse the
// normal vault form, then resume through the existing one-time SSO handoff.
window.openRequestedCredentialForm = function() {
const params = new URLSearchParams(window.location.search);
const serviceId = params.get('credentials');
if (!serviceId) return false;
const service = (window.APPS || []).find(app => app.id === serviceId || app.appTemplate === serviceId);
if (!service) return false;
const returnUrl = params.get('return');
window.history.replaceState({}, '', window.location.pathname);
window.openServiceCredsModal(service, { returnUrl });
return true;
};
})();
+12 -2
View File
@@ -90,11 +90,22 @@
errorEl.textContent = 'Verifying...';
errorEl.className = 'totp-error verifying';
const redirect = safeSessionGet('totp_redirect');
let serviceId = null;
if (redirect) {
try {
const parsed = new URL(redirect, window.location.origin);
const suffix = SITE.tld.startsWith('.') ? SITE.tld : `.${SITE.tld}`;
const candidate = parsed.hostname.slice(0, -suffix.length);
if (parsed.hostname.endsWith(suffix) && /^[a-z0-9][a-z0-9-]*$/.test(candidate)) serviceId = candidate;
} catch (_) { /* invalid redirect is handled by the normal auth flow */ }
}
try {
const res = await secureFetch('/api/v1/totp/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code })
body: JSON.stringify({ code, serviceId })
});
const data = await res.json();
@@ -106,7 +117,6 @@
}
hideTotpOverlay();
// Check if redirected here from another service
const redirect = safeSessionGet('totp_redirect');
if (redirect) {
try { sessionStorage.removeItem('totp_redirect'); } catch (_) {}
// .sami is an unregistered TLD, so browsers silently drop the
+1 -1
View File
@@ -1,4 +1,4 @@
const CACHE = 'dashcaddy-shell-a24ef15882';
const CACHE = 'dashcaddy-shell-3354f5fd96';
const PRECACHE = [
'/',
'/index.html',
+38
View File
@@ -93,3 +93,41 @@ test('same-origin and tokenless destinations keep their direct URL', () => {
assert.equal(buildHandoffTarget('/settings', 'one-time'), 'https://status.sami/settings');
assert.equal(buildHandoffTarget('https://router.sami/config', ''), 'https://router.sami/config');
});
test('an existing status.sami session returns to a service without another TOTP prompt', async () => {
const query = new URLSearchParams({ auth: 'required', return: 'https://plex.sami/web/' });
let scheduled;
let redirected;
const location = {
origin: 'https://status.sami',
pathname: '/',
search: `?${query.toString()}`,
replace(value) { redirected = value; },
};
const context = {
URL,
URLSearchParams,
SITE: { tld: '.sami' },
sessionStorage: { setItem() {} },
document: { getElementById() { return null; } },
setTimeout(fn) { scheduled = fn; },
console,
fetch: async (url) => {
assert.equal(url, '/api/v1/auth/sso-handoff?serviceId=plex');
return { ok: true, json: async () => ({ success: true, ssoToken: 'existing-session-token' }) };
},
window: {
location,
history: { replaceState() {} },
},
};
context.window.window = context.window;
vm.runInNewContext(source, context, { filename: 'auth-gate.js' });
assert.equal(typeof scheduled, 'function');
await scheduled();
assert.equal(
redirected,
'https://plex.sami/dashcaddy-sso?token=existing-session-token&return=%2Fweb%2F',
);
});
@@ -0,0 +1,311 @@
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const { JSDOM } = require('jsdom');
const test = require('node:test');
const assert = require('node:assert/strict');
const handoffSource = fs.readFileSync(path.join(__dirname, '..', 'js', 'credential-vault-handoff.js'), 'utf8');
const formSource = fs.readFileSync(path.join(__dirname, '..', 'js', 'service-credentials.js'), 'utf8');
const initSource = fs.readFileSync(path.join(__dirname, '..', 'js', 'core', 'init.js'), 'utf8');
function loadVault() {
const window = { location: { origin: 'https://status.sami' } };
const context = vm.createContext({ window, SITE: { tld: '.sami' }, URL });
vm.runInContext(handoffSource, context);
return window.DCCredentialVault;
}
async function exerciseFailedModalWrite({
service,
fetchJson,
setupInputs,
expectedEndpoint,
writeResponse,
expectedError = /vault write rejected/,
}) {
const dom = new JSDOM('<!doctype html><body></body>', {
url: 'https://status.sami/',
runScripts: 'outside-only',
});
const { window } = dom;
const writeUrls = [];
let resumeCalls = 0;
window.ErrorHandler = class { logError() {} };
window.SITE = { tld: '.sami' };
window.injectModal = (_id, html) => window.document.body.insertAdjacentHTML('beforeend', html);
window.fetch = async (url) => ({ ok: true, json: async () => fetchJson(url) });
window.secureFetch = async (url) => {
writeUrls.push(url);
return writeResponse || {
ok: false,
status: 500,
json: async () => ({ success: false, error: 'vault write rejected' }),
};
};
window.DCCredentialVault = {
isAllowedReturnUrl: () => true,
resume: async () => { resumeCalls++; return true; },
};
window.confirm = () => true;
window.eval(formSource);
await window.openServiceCredsModal(service, { returnUrl: `https://${service.id}.sami/` });
setupInputs(window.document);
window.document.getElementById('svc-creds-save').click();
await new Promise(resolve => setTimeout(resolve, 20));
assert.equal(writeUrls[0], expectedEndpoint);
assert.equal(resumeCalls, 0);
assert.match(window.document.getElementById('svc-creds-error').textContent, expectedError);
}
test('existing dashboard session mints a one-time token and resumes on the target host', async () => {
const vault = loadVault();
const calls = [];
const replacements = [];
const resumed = await vault.resume('https://plex.sami/web/?direct=1#home', 'plex', {
fetch: async (url, options) => {
calls.push({ url, options });
return {
ok: true,
json: async () => ({ success: true, ssoToken: 'one-time-token' }),
};
},
location: { replace: (target) => replacements.push(target) },
});
assert.equal(resumed, true);
assert.equal(calls.length, 1);
assert.equal(calls[0].url, '/api/v1/auth/sso-handoff?serviceId=plex');
assert.equal(calls[0].options.credentials, 'include');
assert.equal(calls[0].options.cache, 'no-store');
assert.equal(
replacements[0],
'https://plex.sami/dashcaddy-sso?token=one-time-token&return=%2Fweb%2F%3Fdirect%3D1%23home',
);
});
test('vault handoff rejects an external return URL before minting a token', async () => {
const vault = loadVault();
let fetchCalled = false;
const resumed = await vault.resume('https://plex.sami.evil.example/phish', 'plex', {
fetch: async () => { fetchCalled = true; },
location: { replace: () => assert.fail('must not navigate') },
});
assert.equal(resumed, false);
assert.equal(fetchCalled, false);
});
test('credential request opens the form and save path calls the tested handoff helper', () => {
assert.match(formSource, /params\.get\('credentials'\)/);
assert.match(formSource, /openServiceCredsModal\(service, \{ returnUrl \}\)/);
assert.match(formSource, /DCCredentialVault\?\.resume\(returnUrl, currentService\.id\)/);
assert.match(initSource, /openRequestedCredentialForm\(\)/);
assert.match(formSource, /Save to vault & open service/);
});
test('actual vault modal save handler stores credentials then resumes the handoff', async () => {
const dom = new JSDOM('<!doctype html><body></body>', {
url: 'https://status.sami/?credentials=plex&return=https%3A%2F%2Fplex.sami%2Fweb%2F',
runScripts: 'outside-only',
});
const { window } = dom;
let stored = false;
const writes = [];
const resumed = [];
window.ErrorHandler = class { logError() {} };
window.SITE = { tld: '.sami' };
window.APPS = [{ id: 'plex', name: 'Plex', appTemplate: 'plex', url: 'https://plex.sami' }];
window.injectModal = (_id, html) => window.document.body.insertAdjacentHTML('beforeend', html);
window.fetch = async () => ({
ok: true,
json: async () => ({
success: true,
hasApiKey: false,
hasBasicAuth: stored,
username: stored ? 'vault-user' : null,
}),
});
window.secureFetch = async (url, options) => {
writes.push({ url, body: JSON.parse(options.body) });
stored = true;
return { ok: true, json: async () => ({ success: true }) };
};
window.DCCredentialVault = {
isAllowedReturnUrl: () => true,
resume: async (returnUrl, serviceId) => { resumed.push({ returnUrl, serviceId }); return true; },
};
window.confirm = () => true;
window.eval(formSource);
await window.openServiceCredsModal(window.APPS[0], { returnUrl: 'https://plex.sami/web/' });
window.document.getElementById('svc-basic-user').value = 'vault-user';
window.document.getElementById('svc-basic-pass').value = 'vault-password';
window.document.getElementById('svc-creds-save').click();
await new Promise(resolve => setTimeout(resolve, 20));
assert.deepEqual(writes, [{
url: '/api/v1/services/plex/credentials',
body: { username: 'vault-user', password: 'vault-password' },
}]);
assert.deepEqual(resumed, [{ returnUrl: 'https://plex.sami/web/', serviceId: 'plex' }]);
});
test('failed credential write does not mint a handoff or navigate', async () => {
const dom = new JSDOM('<!doctype html><body></body>', {
url: 'https://status.sami/',
runScripts: 'outside-only',
});
const { window } = dom;
let resumeCalls = 0;
window.ErrorHandler = class { logError() {} };
window.SITE = { tld: '.sami' };
window.injectModal = (_id, html) => window.document.body.insertAdjacentHTML('beforeend', html);
window.fetch = async () => ({
ok: true,
json: async () => ({ success: true, hasApiKey: false, hasBasicAuth: false, username: null }),
});
window.secureFetch = async () => ({
ok: false,
status: 500,
json: async () => ({ success: false, error: 'vault write rejected' }),
});
window.DCCredentialVault = {
isAllowedReturnUrl: () => true,
resume: async () => { resumeCalls++; return true; },
};
window.confirm = () => true;
window.eval(formSource);
const service = { id: 'plex', name: 'Plex', appTemplate: 'plex', url: 'https://plex.sami' };
await window.openServiceCredsModal(service, { returnUrl: 'https://plex.sami/web/' });
window.document.getElementById('svc-basic-user').value = 'vault-user';
window.document.getElementById('svc-basic-pass').value = 'vault-password';
window.document.getElementById('svc-creds-save').click();
await new Promise(resolve => setTimeout(resolve, 20));
assert.equal(resumeCalls, 0);
assert.match(window.document.getElementById('svc-creds-error').textContent, /vault write rejected/);
});
test('failed ARR credential write does not mint a handoff or navigate', async () => {
await exerciseFailedModalWrite({
service: { id: 'radarr', name: 'Radarr', appTemplate: 'radarr', url: 'https://radarr.sami' },
fetchJson: (url) => url.includes('/services/')
? { success: true, hasApiKey: false, hasBasicAuth: false, username: null }
: { success: true, profiles: [] },
setupInputs: (document) => { document.getElementById('svc-apikey-input').value = 'arr-key'; },
expectedEndpoint: '/api/v1/arr/credentials',
});
});
test('failed ARR quality-profile write does not mint a handoff or navigate', async () => {
await exerciseFailedModalWrite({
service: { id: 'radarr', name: 'Radarr', appTemplate: 'radarr', url: 'https://radarr.sami' },
fetchJson: (url) => url.includes('/services/')
? { success: true, hasApiKey: true, hasBasicAuth: false, username: null }
: { success: true, profiles: [{ id: 1, name: 'Default' }], storedProfileId: 1 },
setupInputs: () => {},
expectedEndpoint: '/api/v1/arr/quality-profiles',
});
});
test('failed seedhost write does not mint a handoff or navigate', async () => {
await exerciseFailedModalWrite({
service: { id: 'torrent', name: 'qBittorrent', isExternal: true, externalUrl: 'https://torrent.sami' },
fetchJson: (url) => url.includes('/seedhost-creds')
? { success: true, hasCredentials: false, username: null }
: { success: true, hasApiKey: false, hasBasicAuth: false, username: null },
setupInputs: (document) => {
document.getElementById('svc-seedhost-user').value = 'seed-user';
document.getElementById('svc-seedhost-pass').value = 'seed-password';
},
expectedEndpoint: '/api/v1/seedhost-creds',
});
});
test('failed generic API-key write does not mint a handoff or navigate', async () => {
await exerciseFailedModalWrite({
service: { id: 'custom', name: 'Custom', url: 'https://custom.sami' },
fetchJson: () => ({ success: true, hasApiKey: false, hasBasicAuth: false, username: null }),
setupInputs: (document) => {
document.getElementById('svc-apikey-input').value = 'custom-key';
document.getElementById('svc-basic-user').value = 'user';
document.getElementById('svc-basic-pass').value = 'password';
},
expectedEndpoint: '/api/v1/services/custom/credentials',
});
});
test('HTTP 2xx with malformed JSON does not mint a handoff or navigate', async () => {
await exerciseFailedModalWrite({
service: { id: 'plex', name: 'Plex', appTemplate: 'plex', url: 'https://plex.sami' },
fetchJson: () => ({ success: true, hasApiKey: false, hasBasicAuth: false, username: null }),
setupInputs: (document) => {
document.getElementById('svc-basic-user').value = 'user';
document.getElementById('svc-basic-pass').value = 'password';
},
expectedEndpoint: '/api/v1/services/plex/credentials',
writeResponse: { ok: true, status: 200, json: async () => { throw new Error('bad json'); } },
expectedError: /invalid server response/,
});
});
test('HTTP 2xx without success:true does not mint a handoff or navigate', async () => {
await exerciseFailedModalWrite({
service: { id: 'plex', name: 'Plex', appTemplate: 'plex', url: 'https://plex.sami' },
fetchJson: () => ({ success: true, hasApiKey: false, hasBasicAuth: false, username: null }),
setupInputs: (document) => {
document.getElementById('svc-basic-user').value = 'user';
document.getElementById('svc-basic-pass').value = 'password';
},
expectedEndpoint: '/api/v1/services/plex/credentials',
writeResponse: { ok: true, status: 200, json: async () => ({ message: 'ambiguous' }) },
expectedError: /failed \(200\)/,
});
});
test('failed credential clear remains visibly failed and keeps stored-state UI', async () => {
const dom = new JSDOM('<!doctype html><body><button id="creds-btn-plex" class="has-creds"></button></body>', {
url: 'https://status.sami/',
runScripts: 'outside-only',
});
const { window } = dom;
window.ErrorHandler = class { logError() {} };
window.SITE = { tld: '.sami' };
window.injectModal = (_id, html) => window.document.body.insertAdjacentHTML('beforeend', html);
window.fetch = async () => ({
ok: true,
json: async () => ({ success: true, hasApiKey: false, hasBasicAuth: true, username: 'vault-user' }),
});
window.secureFetch = async () => ({
ok: false,
status: 500,
json: async () => ({ success: false, error: 'clear rejected' }),
});
window.DCCredentialVault = { isAllowedReturnUrl: () => false };
window.confirm = () => true;
window.eval(formSource);
const service = { id: 'plex', name: 'Plex', appTemplate: 'plex', url: 'https://plex.sami' };
await window.openServiceCredsModal(service);
window.document.getElementById('svc-creds-clear').click();
await new Promise(resolve => setTimeout(resolve, 20));
assert.match(window.document.getElementById('svc-creds-error').textContent, /clear rejected/);
assert.equal(window.document.getElementById('creds-btn-plex').classList.contains('has-creds'), true);
});
test('handoff rejects a private-TLD host that is not the requested protected service', async () => {
const vault = loadVault();
let fetchCalled = false;
const resumed = await vault.resume('https://dns1.sami/', 'plex', {
fetch: async () => { fetchCalled = true; },
location: { replace: () => assert.fail('must not navigate') },
});
assert.equal(resumed, false);
assert.equal(fetchCalled, false);
});
+67
View File
@@ -0,0 +1,67 @@
const fs = require('node:fs');
const path = require('node:path');
const { JSDOM } = require('jsdom');
const test = require('node:test');
const assert = require('node:assert/strict');
const source = fs.readFileSync(path.join(__dirname, '..', 'js', 'core', 'credentials.js'), 'utf8');
function buildDnsCredentialUi() {
const dom = new JSDOM('<!doctype html><body><button id="manage-tokens"></button></body>', {
url: 'https://status.sami/',
runScripts: 'outside-only',
});
const { window } = dom;
const local = new Map();
const session = new Map();
window.SITE = { dnsServers: { dns1: { name: 'Primary DNS' } } };
window.injectModal = (_id, html) => window.document.body.insertAdjacentHTML('beforeend', html);
window.safeGet = key => local.get(key) || null;
window.safeSet = (key, value) => local.set(key, value);
window.safeRemove = key => local.delete(key);
window.safeSessionGet = key => session.get(key) || null;
window.safeSessionSet = (key, value) => session.set(key, value);
window.closeModal = () => {};
window.confirm = () => true;
window.TextEncoder = TextEncoder;
window.setTimeout = () => 1;
window.eval(source);
window.document.getElementById('manage-tokens').click();
return { window, local };
}
test('failed DNS credential save never populates browser cache or success UI', async () => {
const { window, local } = buildDnsCredentialUi();
window.secureFetch = async () => ({
ok: false,
status: 500,
json: async () => ({ success: false, error: 'DNS vault rejected' }),
});
window.document.getElementById('dns1-admin-username').value = 'dns-admin';
window.document.getElementById('dns1-admin-token').value = 'dns-password';
window.document.getElementById('token-save').click();
await new Promise(resolve => setTimeout(resolve, 20));
assert.equal(local.has('dns1-admin-username-enc'), false);
assert.equal(local.has('dns1-admin-token-enc'), false);
assert.match(window.document.getElementById('dns1-token-status').textContent, /DNS vault rejected/);
assert.equal(window.document.getElementById('dns1-token-status').classList.contains('success'), false);
});
test('failed DNS credential clear preserves cached state and shows error', async () => {
const { window, local } = buildDnsCredentialUi();
local.set('dns1-admin-username-enc', 'existing-user');
local.set('dns1-admin-token-enc', 'existing-password');
window.secureFetch = async () => ({
ok: true,
status: 200,
json: async () => ({ message: 'ambiguous response' }),
});
window.document.getElementById('token-clear-all').click();
await new Promise(resolve => setTimeout(resolve, 20));
assert.equal(local.has('dns1-admin-username-enc'), true);
assert.equal(local.has('dns1-admin-token-enc'), true);
assert.match(window.document.getElementById('dns1-token-status').textContent, /DNS credential removal failed/);
assert.equal(window.document.getElementById('dns1-token-status').classList.contains('success'), false);
});