diff --git a/.env.example b/.env.example index df582f29..8667a3eb 100644 --- a/.env.example +++ b/.env.example @@ -121,6 +121,19 @@ WEB_PORT=3000 # For the daily downloads digest: Sales and Trends -> Reports -> your vendor number. # APPLE_VENDOR_NUMBER= +# ---- Updating Sylo ---- +# Operators see "Update available" in the dashboard when a newer release is out: Sylo asks GitHub for +# its latest release (anonymously, at most every 6 hours). Set it to off to never ask. +# UPDATE_CHECK=off +# Optional: let Wise Wren (https://github.com/Ferdinand99/Wise-Wren) update Sylo from the dashboard +# ("Update now"). All three are needed. The token (a Wise Wren target token) is a secret; it only stays on +# this server. +# WISE_WREN_URL=http://wise-wren:3003 +# WISE_WREN_TOKEN= +# WISE_WREN_TARGET_ID= +# Without a dashboard login (open mode) "Update now" is refused unless you set this to true. +# WISE_WREN_ALLOW_OPEN_MODE=false + # ---- Outbound URLs ---- # RSS feeds and welcome card backgrounds are fetched by the server, so addresses on your own # network (127.0.0.1, 10.x, 192.168.x, 169.254.x ...) are refused. Allow them on a trusted LAN: diff --git a/README.md b/README.md index b85a430f..9612694d 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,16 @@ off the ones you don't need with `INTENT_GUILD_MEMBERS=false` / logging, push) is optional and listed in [`.env.example`](.env.example) and the [environment variable table](docs/self-hosting.md#environment-variables). +### Updating Sylo + +Update with `docker compose pull && docker compose up -d`. Operators (the accounts in `OWNER_IDS`) +see an **Update available** note in the dashboard when a newer release is out, with its release +notes and this command; set `UPDATE_CHECK=off` to turn the check off. With +[Wise Wren](https://github.com/Ferdinand99/Wise-Wren) set up (`WISE_WREN_URL`, `WISE_WREN_TOKEN`, +`WISE_WREN_TARGET_ID`) the same page has an **Update now** button, and Sylo never needs access to +the Docker socket. Details in +[docs/self-hosting.md](docs/self-hosting.md#update-notice-and-update-now). + ### Backups and monitoring Sylo snapshots its database to `/backups` before every schema migration diff --git a/docs/mobile-app.md b/docs/mobile-app.md index d03cd0c3..1f25586b 100644 --- a/docs/mobile-app.md +++ b/docs/mobile-app.md @@ -20,6 +20,7 @@ reviewing it. - [Permissions](#permissions) - [Notifications](#notifications) - [Health and the dev log (operators)](#health-and-the-dev-log-operators) +- [Update notice (operators)](#update-notice-operators) - [Security notes](#security-notes) --- @@ -317,7 +318,7 @@ in there: | `login` | Whether sign-in is available (false in open mode) | | `loginProviders` | The configured logins, for example `["discord", "fluxer"]`; empty when `login` is false | | `platforms` | The platforms the bot runs on, for example `["discord"]` | -| `features` | What this version's app API supports, so the app can hide what an older Sylo lacks. Today: `app-login`, `live-access`, `changelog`, `collectibles`, `welcome-trigger`, `member-search`, `access-levels`, `health`, `app-connect`, `setup-wizard`, `config-history`, `inbox-search`, `config-import`, `duplicate`, `message-templates` and `message-move`, plus `push` and `push-pigeon` when this instance can send notifications. See the list below | +| `features` | What this version's app API supports, so the app can hide what an older Sylo lacks. Today: `app-login`, `live-access`, `changelog`, `collectibles`, `welcome-trigger`, `member-search`, `access-levels`, `health`, `app-connect`, `setup-wizard`, `config-history`, `inbox-search`, `config-import`, `duplicate`, `message-templates`, `message-move` and `update-notice`, plus `push` and `push-pigeon` when this instance can send notifications. See the list below | The feature strings: @@ -338,6 +339,7 @@ The feature strings: - `duplicate`: the `duplicate` routes for reminders, embed messages, reaction-role sets, custom commands and shop items. - `message-templates`: the built-in message templates and the raw message JSON import and export. - `message-move`: when the channel of a posted **embed message**, the **Welcome channel** message or a **role panel** changes and is published, the server posts a fresh message in the new channel, stores it and then deletes the old one, so the app must not unpublish first. Other bot posts (verification, honeypot traps, applications and so on) are not covered. A publish that loses a race with another publish of the same embed message answers `409` with the current record. +- `update-notice`: the operators' [update notice](#update-notice-operators): `GET /api/v2/update`, and with Wise Wren set up `POST /api/v2/update/wren/approve` and `GET /api/v2/update/wren/runs/:id`. Operators only. - `app-connect`: [adding the other login](#adding-the-other-login). Only where sign-in is on and both Discord and Fluxer logins are set up. - `push`, `push-pigeon`: this instance can send notifications to signed-in apps. @@ -646,6 +648,77 @@ its account is an operator. | `GET /api/v2/roadmap/admin` and `POST /api/v2/roadmap/admin[/:id/approve, reject, status, edit, delete]` | Operators only | The roadmap review board | | `GET /api/v2/account` | Bearer token or session | Includes `operator: true` for an operator | +## Update notice (operators) + +Operators also get the update notice (feature `update-notice`): whether a newer Sylo release is out, what +is in it, and, when the instance runs [Wise Wren](https://github.com/Ferdinand99/Wise-Wren), an **Update +now** button. It is the same data as the dashboard's Health page. Everything is operator-only: a server +admin who is not an operator gets `403`, a request without a sign-in `401`. The routes are listed in the +table below. Writes are rate limited (5 approvals a minute, 6 re-checks a minute) and answer +`429 { error }` above that. + +`GET /api/v2/update` answers (add `?refresh=1` to check GitHub again now): + +```json +{ + "enabled": true, + "current": "5.2.0", + "latest": "5.3.0", + "available": true, + "checkedAt": "2026-11-20T10:00:00.000Z", + "checkFailed": false, + "releaseUrl": "https://github.com/Ferdinand99/Sylo/releases/tag/v5.3.0", + "publishedAt": "2026-11-20T09:00:00Z", + "notes": { "version": "5.3.0", "date": "2026-11-20", "url": null, "sections": [ + { "title": "New", "kind": "features", "items": [{ "scope": "dashboard", "text": "Inline markdown, https links only", "refs": [{ "label": "#370", "url": "https://github.com/..." }] }] } + ] }, + "notesText": null, + "command": "docker compose pull && docker compose up -d", + "wren": null +} +``` + +- `enabled` is false when the instance runs with `UPDATE_CHECK=off`: there is nothing else to show then. + `available` is true when `latest` is newer than `current`. `checkFailed` is true when the last look at + GitHub failed (`latest` then still holds an earlier answer, if there was one). +- `notes` and `notesText` are only set while `available` is true. `notes` has the same sections as + `GET /api/v2/changelog` (`items[].text` is inline markdown with `https` links only, to render as text, + never as HTML). Release notes that are not in that format come as `notesText`, plain text. +- `wren` is `null` when Wise Wren is not set up. Otherwise it has `configured`, `error` (`null`, or + `{ code, message }` when Wise Wren could not be asked or does not accept Sylo's token), `openMode` (the + dashboard has no login, so Update now is off unless the operator allowed it), `target` + (`{ name, server, status, installedVersion, latestVersion, mode, lastCheckedAt }` or `null`), + `updateSeen` (Wise Wren sees an update), `canApprove`, `whyNot` (the reason when it cannot, in words), and + `latestRun` (the run Sylo last approved, as below, or `null`; Sylo remembers it across its own restart). + While `latestRun` is not `finished`, `canApprove` is false and the page or app should follow that run. + It never has Wise Wren's address, the token or the version key. + +`POST /api/v2/update/wren/approve` (no body) asks Wise Wren to update Sylo now and answers +`{ runId, status, from }`, where `from` is the version it started from. It approves exactly the version Wise +Wren shows. Errors are `{ error, code }`: `404` (`wren_off`) when Wise Wren is not set up; `409` when there is +nothing to approve (`not_approvable`), when Wise Wren refuses (`wren_refused`, with Wise Wren's own message: +the version changed, a run is already going, the agent would refuse it), or in open mode (`open_mode`); +`429`; and `502` (`wren_unreachable`, `wren_unauthorized`, `wren_forbidden`, `wren_server`, `wren_invalid`) +when Wise Wren cannot be reached or does not accept Sylo's token. These are never `401` or `403`, which stay +for the operator's own sign-in. + +`GET /api/v2/update/wren/runs/:id` answers `{ run }`, where `run` is +`{ id, status, finished, ok, detail, version, createdAt, startedAt, finishedAt, steps: [{ step, status, durationMs }] }` +(`status`: `queued`, `running`, `succeeded`, `rolled_back`, `failed`, `rollback_failed`, `refused`, +`cancelled`, `lapsed`; `finished` is true for all but the first two). A run that is not the Sylo target's +is refused (`404`, or `502` `wren_forbidden` from a token limited to one target). Poll it every few seconds, up to +60 requests a minute. + +**Sylo restarts during an update**, so these requests fail for a while (a dropped connection, or `502`/`503` +from a reverse proxy, without a `code`). The app keeps polling; it is done when the run is `finished`, or +when `GET /api/v2/update` (or `GET /api/mobile/info`) reports a `current` / `version` other than `from`. + +| Method and path | Auth | Answer | +| --- | --- | --- | +| `GET /api/v2/update[?refresh=1]` | Operators only | The update notice above | +| `POST /api/v2/update/wren/approve` | Operators only | `{ runId, status, from }` | +| `GET /api/v2/update/wren/runs/:id` | Operators only | `{ run }` | + --- ## Security notes diff --git a/docs/self-hosting.md b/docs/self-hosting.md index 8e70e1b2..2524204e 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -104,6 +104,11 @@ At least one platform must be configured. | `BACKUP_INTERVAL_HOURS` | `24` | Scheduled DB snapshot interval; `0` disables it (pre-migration + manual still run) | | `BACKUP_RETENTION` | `14` | How many DB snapshots to keep in `/backups` | | `BACKUP_DIR` | `/backups` | Where DB snapshots are written | +| `UPDATE_CHECK` | `on` | The [update notice](#update-notice-and-update-now) for operators: Sylo looks up its newest GitHub release (at most every 6 hours). `off` turns it off and nothing is requested | +| `WISE_WREN_URL` | — | Optional: address of your [Wise Wren](https://github.com/Ferdinand99/Wise-Wren), to update Sylo from the dashboard. Needs the next two as well. `https`, or `http` on your own network | +| `WISE_WREN_TOKEN` | — | A Wise Wren **target token** for Sylo's target (Wise Wren: Settings → Target tokens): it can read and approve only that target. A secret: never shown in the dashboard, never logged | +| `WISE_WREN_TARGET_ID` | — | The id of Sylo's target in Wise Wren (the number in the target page's address) | +| `WISE_WREN_ALLOW_OPEN_MODE` | `false` | Only matters when the dashboard has no login (open mode): then **Update now** is refused, because anyone who can reach the dashboard could use it. `true` allows it anyway | | `LOG_LEVEL` | `info` | `debug` / `info` / `warn` / `error` | | `LOG_FORMAT` | `text` | `text` or `json` (`LOG_JSON=1` = json) | | `NODE_ENV` | `development` | Set to `production` in deployment | @@ -425,6 +430,45 @@ dated) snapshot from `data/backups/` over `data/sylo.db`. A newer database can't be opened by an older build — the schema check refuses it — so always roll the database back together with the code. +### Update notice and "Update now" + +The people who run the instance (the operators, `OWNER_IDS` / `FLUXER_OWNER_IDS`) see a small +**Update available** note in the dashboard's top bar when a newer Sylo release is out. It opens the +**Updates** section of the Health page: the new version, its release notes, and the command to copy, +`docker compose pull && docker compose up -d`. Server admins who are not operators see none of this, +and the Sylo app shows the same to operators (see [mobile-app.md](mobile-app.md#update-notice-operators)). + +- **How it checks.** Sylo asks `api.github.com` for its latest release, anonymously, when an operator + opens the dashboard, and keeps the answer for 6 hours. Nothing about your instance is sent (GitHub sees your server's address, as for any request). A failed + check is quiet (one log line) and never delays startup. To turn it off completely, set + `UPDATE_CHECK=off`; Sylo then makes no request, and the Health page says the check is off. +- **Updating without the command line.** Sylo itself never gets access to the Docker socket (that would + be root on the host). If you run [Wise Wren](https://github.com/Ferdinand99/Wise-Wren), it can do the + update, and the dashboard gets an **Update now** button: + + 1. In Wise Wren, make sure Sylo shows up as a target and that the agent's `[updates]` allow list + includes it. + 2. In Wise Wren, make a **target token** (Settings → Target tokens) for that target. It can read and + approve that one target and nothing else, so a leaked token cannot update anything else. Use the + token's `ww_…` value below. + 3. Set `WISE_WREN_URL`, `WISE_WREN_TOKEN` and `WISE_WREN_TARGET_ID` (all three) and restart Sylo. + + Sylo then shows what Wise Wren sees for the target. **Update now** (operators only, after a + confirmation) approves that exact version in Wise Wren, which stops Sylo, takes a snapshot, starts the + new version and rolls back if it does not come up healthy. The Health page follows the run and + reconnects by itself when Sylo restarts. Sylo remembers the run it approved (in its database), so + **Update now** stays off while that run is going, also after the restart. If Wise Wren refuses (the + version changed, a run is already going, the target only notifies), you see Wise Wren's own words. + The token stays on the server: it is never sent to the browser or the app, and never logged. Sylo only + calls three Wise Wren routes, all for the one target: `GET /api/v1/targets/`, + `POST /api/v1/targets//approve` and `GET /api/v1/runs/`. Without these settings only the notice + and the command are shown. +- **Open mode.** When the dashboard has no login (no `DISCORD_CLIENT_SECRET` or `FLUXER_CLIENT_SECRET`), + everyone who can reach it counts as an operator. **Update now** is refused there (the notice, the + command and what Wise Wren sees are still shown, and Sylo logs a warning at start), because it spends + Sylo's Wise Wren token on behalf of whoever sent the request. Set up a login, or set + `WISE_WREN_ALLOW_OPEN_MODE=true` if you accept that on a network you trust. + ### Upgrading to 5.0 Sylo 5.0 removes the classic dashboard. There are no database steps: just upgrade as diff --git a/src/config.js b/src/config.js index da48d712..4d524eac 100644 --- a/src/config.js +++ b/src/config.js @@ -5,6 +5,7 @@ import { dirname, resolve } from 'node:path'; import { resolveSessionSecret } from './lib/sessionSecret.js'; import { normalisePrivateKey } from './lib/appStoreConnect.js'; import { checkPigeonUrl } from './lib/pigeonUrl.js'; +import { readWrenSettings } from './lib/wiseWren.js'; import { commandPrefixError } from './lib/commandPrefix.js'; /** @@ -242,6 +243,16 @@ if (optionalOrNull('PIGEON_URL')) { console.error(`[config] PIGEON_URL is not usable (${err.message}); push through Pigeon is off.`); } } +// The operators' update notice: Sylo asks GitHub for its newest release (cached, no account, no +// data about this instance is sent). UPDATE_CHECK=off turns it off; nothing is requested then. +const updateCheck = !/^(0|false|no|off)$/i.test(process.env.UPDATE_CHECK?.trim() ?? ''); +// Optional: Wise Wren (https://github.com/Ferdinand99/Wise-Wren) updates this instance from the +// dashboard's "Update now". All three settings are needed; the token is a secret. +const { settings: wiseWren, problem: wiseWrenProblem } = readWrenSettings(process.env); +if (wiseWrenProblem) console.error(`[config] ${wiseWrenProblem}`); +// Without a dashboard login nobody is signed in, so "Update now" would be open to everyone who can reach +// the dashboard. It is refused there unless this says it is wanted anyway. +const wiseWrenAllowOpenMode = optionalBool('WISE_WREN_ALLOW_OPEN_MODE', false); const kickClientSecret = optionalOrNull('KICK_CLIENT_SECRET'); // Signs session cookies and short-lived tokens (e.g. verification links), so it // must always exist. SESSION_SECRET wins; otherwise a generated secret is kept in @@ -370,6 +381,10 @@ export const config = Object.freeze({ pushRelayUrl, // Postal Pigeon (see above); null = off. pigeonUrl, + // The update notice (see above), and Wise Wren's address, token and target id (null = off). + updateCheck, + wiseWren, + wiseWrenAllowOpenMode, // URLs that server admins type in (RSS feeds, the welcome card background) are fetched by // the server, so private, loopback and link-local addresses are refused (src/lib/safeFetch.js). // A self-hosted instance that wants feeds from its own network can allow them. @@ -408,3 +423,10 @@ export const config = Object.freeze({ // Misc nodeEnv: optional('NODE_ENV', 'development'), }); + +if (config.wiseWren && !config.authEnabled && !config.wiseWrenAllowOpenMode) { + console.error( + '[config] Wise Wren is set up, but the dashboard has no login (open mode), so "Update now" is off: ' + + 'anyone who can reach the dashboard could use it. Set up a login, or set WISE_WREN_ALLOW_OPEN_MODE=true to allow it.' + ); +} diff --git a/src/lib/updateCheck.js b/src/lib/updateCheck.js new file mode 100644 index 00000000..95075a6c --- /dev/null +++ b/src/lib/updateCheck.js @@ -0,0 +1,250 @@ +// "Is there a newer Sylo?" for the operators' update notice: the latest GitHub release, compared with +// the version that runs. One anonymous request to a fixed address, kept in memory for six hours, with a +// timeout; a failure is quiet (one log line, then a shorter retry delay) and never reaches startup. +// UPDATE_CHECK=off turns it off completely: nothing is requested. +import { changelogFeed } from './changelog.js'; + +export const RELEASE_API = 'https://api.github.com/repos/Ferdinand99/Sylo/releases/latest'; +export const UPDATE_COMMAND = 'docker compose pull && docker compose up -d'; + +export const CHECK_TTL_MS = 6 * 60 * 60 * 1000; +// After a failed check the next one waits this long, so a dead network is not asked every few seconds. +export const RETRY_AFTER_FAILURE_MS = 30 * 60 * 1000; +// "Check again" can bring the next request forward, but not more often than this for the whole instance +// (GitHub allows an anonymous address 60 requests an hour), and never while a failure is being waited out. +export const FORCE_MIN_INTERVAL_MS = 60 * 1000; +export const REQUEST_TIMEOUT_MS = 5000; +const MAX_BODY_CHARS = 1_000_000; +const MAX_NOTES_CHARS = 20_000; +const MAX_NOTES_TEXT = 4000; + +/** + * "5.3.0" or "v5.3.0-rc.1" as numbers and a prerelease tag, or null when it is not a version. + * @param {unknown} v + */ +export function parseVersion(v) { + const m = /^v?(\d{1,6})\.(\d{1,6})\.(\d{1,6})(?:-([0-9A-Za-z.-]{1,40}))?(?:\+[0-9A-Za-z.-]{1,40})?$/.exec( + String(v ?? '').trim() + ); + if (!m) return null; + return { nums: [Number(m[1]), Number(m[2]), Number(m[3])], pre: m[4] ? m[4].split('.') : null }; +} + +function comparePre(a, b) { + // A release outranks its prereleases; identifiers compare as numbers when both are numbers. + if (!a && !b) return 0; + if (!a) return 1; + if (!b) return -1; + for (let i = 0; i < Math.max(a.length, b.length); i++) { + if (a[i] === undefined) return -1; + if (b[i] === undefined) return 1; + const an = /^\d+$/.test(a[i]); + const bn = /^\d+$/.test(b[i]); + if (an && bn) { + const d = Number(a[i]) - Number(b[i]); + if (d) return Math.sign(d); + } else if (an !== bn) { + return an ? -1 : 1; + } else if (a[i] !== b[i]) { + return a[i] < b[i] ? -1 : 1; + } + } + return 0; +} + +/** + * Compare two versions: 1 when `a` is newer, -1 when older, 0 when equal, null when either is not a version. + * @returns {1 | 0 | -1 | null} + */ +export function compareVersions(a, b) { + const x = parseVersion(a); + const y = parseVersion(b); + if (!x || !y) return null; + for (let i = 0; i < 3; i++) { + if (x.nums[i] !== y.nums[i]) return x.nums[i] > y.nums[i] ? 1 : -1; + } + return comparePre(x.pre, y.pre); +} + +/** The body as text, read as it streams and cut off with an error once it passes `max` characters. */ +export async function readCapped(res, max) { + if (!res.body?.getReader) { + const text = await res.text(); + if (text.length > max) throw new Error('the answer is too large'); + return text; + } + const reader = res.body.getReader(); + const decoder = new TextDecoder(); + let text = ''; + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + text += decoder.decode(value, { stream: true }); + if (text.length > max) { + await reader.cancel().catch(() => {}); + throw new Error('the answer is too large'); + } + } + return text + decoder.decode(); +} + +/** `[label](url)` links in a changelog line keep only https targets: anything else shows as its label. */ +function httpsOnly(text) { + return String(text).replace(/\[([^\]]+)\]\(([^()\s]+(?:\([^()\s]*\))?)\)/g, (whole, label, url) => + /^https:\/\//i.test(url) ? whole : label + ); +} + +/** + * The release notes as data the dashboard and the app render themselves (never as HTML): the sections + * and items of a release-please body, with only https links. Notes that are not in that format come + * back as a short plain text instead. + * @returns {{ notes: object | null, notesText: string | null }} + */ +export function shapeNotes(body, version) { + const text = String(body ?? '').slice(0, MAX_NOTES_CHARS); + const [release] = changelogFeed(text, { limit: 1 }); + if (release && release.sections.length) { + return { + notes: { + version: release.version || version, + date: release.date, + url: release.url && /^https:\/\//i.test(release.url) ? release.url : null, + sections: release.sections.map((s) => ({ + title: s.title, + kind: s.kind, + items: s.items.map((i) => ({ + scope: i.scope, + text: httpsOnly(i.text), + refs: i.refs.filter((r) => /^https:\/\//i.test(r.url)), + })), + })), + }, + notesText: null, + }; + } + const plain = httpsOnly(text).trim(); + return { notes: null, notesText: plain ? plain.slice(0, MAX_NOTES_TEXT) : null }; +} + +/** Only a GitHub page of the Sylo repository is offered as "the release". */ +const RELEASE_PAGE = /^https:\/\/github\.com\/Ferdinand99\/Sylo\/releases\/[A-Za-z0-9._/-]{1,100}$/; + +/** + * @param {object} release GitHub's `releases/latest` answer + */ +export function shapeRelease(release) { + if (!release || typeof release !== 'object') return null; + if (release.draft || release.prerelease) return null; + const tag = typeof release.tag_name === 'string' ? release.tag_name : ''; + const parsed = parseVersion(tag); + if (!parsed) return null; + const version = tag.replace(/^v/, ''); + return { + version, + url: RELEASE_PAGE.test(release.html_url ?? '') ? release.html_url : null, + publishedAt: Number.isNaN(Date.parse(release.published_at)) ? null : release.published_at, + ...shapeNotes(release.body, version), + }; +} + +/** + * A checker with its own cache. The routes use one instance (below); tests make their own. + * @param {{ + * enabled?: boolean, version: string, url?: string, ttlMs?: number, retryMs?: number, + * timeoutMs?: number, now?: () => number, fetchImpl?: typeof fetch, + * log?: { debug?: Function, warn?: Function }, + * }} opts + */ +export function createUpdateChecker({ + enabled = true, + version, + url = RELEASE_API, + ttlMs = CHECK_TTL_MS, + retryMs = RETRY_AFTER_FAILURE_MS, + timeoutMs = REQUEST_TIMEOUT_MS, + now = Date.now, + fetchImpl, + log, +}) { + /** @type {{ at: number, until: number, release: ReturnType, failed: boolean } | null} */ + let cached = null; + /** @type {Promise | null} */ + let inflight = null; + let warned = false; + let lastAttempt = 0; + + async function fetchLatest() { + lastAttempt = now(); + try { + const res = await (fetchImpl ?? globalThis.fetch)(url, { + headers: { + accept: 'application/vnd.github+json', + 'user-agent': `Sylo/${version} (update check)`, + 'x-github-api-version': '2022-11-28', + }, + redirect: 'error', + signal: AbortSignal.timeout(timeoutMs), + }); + if (!res.ok) throw new Error(`GitHub answered ${res.status}`); + const declared = Number(res.headers.get('content-length')); + if (Number.isFinite(declared) && declared > MAX_BODY_CHARS) throw new Error('the answer is too large'); + const text = await readCapped(res, MAX_BODY_CHARS); + const release = shapeRelease(JSON.parse(text)); + if (!release) throw new Error('no usable release in the answer'); + const at = now(); + cached = { at, until: at + ttlMs, release, failed: false }; + } catch (err) { + const at = now(); + // The last good answer stays visible; only the retry is pushed out. + cached = { at: cached?.at ?? 0, until: at + retryMs, release: cached?.release ?? null, failed: true }; + const message = err instanceof Error ? err.message : String(err); + // One warning per process; later failures are only debug lines. + if (!warned) log?.warn?.('update-check', 'could not check for a new Sylo release:', message); + else log?.debug?.('update-check', 'check failed again:', message); + warned = true; + } + } + + /** + * The state of the check. Never throws, never waits for the network when the cache is fresh. + * `force` re-checks now (the Check again button, rate limited by the route). + */ + async function get({ force = false } = {}) { + if (!enabled) return view(null, false, 0); + // A forced check is a request to look sooner; it is served from the cache when the last look was a + // moment ago or a failure is being waited out. + const forced = + force && + cached && + !(cached.failed && now() < cached.until) && + now() - lastAttempt >= FORCE_MIN_INTERVAL_MS; + if (!cached || forced || now() >= cached.until) { + inflight ??= fetchLatest().finally(() => { + inflight = null; + }); + await inflight; + } + return view(cached.release, cached.failed, cached.at); + } + + function view(release, failed, at) { + const newer = release ? compareVersions(release.version, version) === 1 : false; + return { + enabled, + current: version, + latest: release?.version ?? null, + available: newer, + checkedAt: at ? new Date(at).toISOString() : null, + checkFailed: failed, + releaseUrl: release?.url ?? null, + publishedAt: release?.publishedAt ?? null, + // Notes are only worth sending when they are for a version the operator does not run yet. + notes: newer ? release.notes : null, + notesText: newer ? release.notesText : null, + command: UPDATE_COMMAND, + }; + } + + return { get }; +} diff --git a/src/lib/wiseWren.js b/src/lib/wiseWren.js new file mode 100644 index 00000000..3218a8b6 --- /dev/null +++ b/src/lib/wiseWren.js @@ -0,0 +1,327 @@ +// Wise Wren (https://github.com/Ferdinand99/Wise-Wren) as Sylo's optional updater. Sylo never gets +// access to the Docker socket: an operator who runs Wise Wren can let the dashboard ask it to update +// Sylo's own target. This file is the whole client: what Wise Wren knows about the target, "approve +// the update" and the progress of the run. It only ever calls three Wise Wren routes, all on the one +// target id that WISE_WREN_TARGET_ID names (the routes a Wise Wren target token is allowed): +// +// GET /api/v1/targets/{id} what Wren sees (an update, the version key, why it would refuse) +// POST /api/v1/targets/{id}/approve approve exactly that version +// GET /api/v1/runs/{id} a run's steps (the id comes from the approval) +// +// The token (WISE_WREN_TOKEN) is a secret: it is only ever put in the Authorization header of these +// requests. It is not in any error, log line or answer this file produces, and nothing here follows +// a redirect (a redirect to another host must not receive it). +import { BlockList, isIP } from 'node:net'; +import { readCapped } from './updateCheck.js'; + +// Addresses an `http://` Wise Wren may live on: this machine and the local network (as the Wren agent's +// own rule for its server address). +const lan = new BlockList(); +for (const [net, prefix] of [ + ['127.0.0.0', 8], + ['10.0.0.0', 8], + ['100.64.0.0', 10], + ['172.16.0.0', 12], + ['192.168.0.0', 16], + ['169.254.0.0', 16], +]) { + lan.addSubnet(net, prefix, 'ipv4'); +} +lan.addAddress('::1', 'ipv6'); +lan.addSubnet('fc00::', 7, 'ipv6'); +lan.addSubnet('fe80::', 10, 'ipv6'); + +const LOCAL_NAME = /\.(local|lan|home|home\.arpa|internal|ts\.net)$/i; + +function isLocalHost(hostname) { + const host = hostname.replace(/^\[|\]$/g, '').toLowerCase(); + const family = isIP(host); + if (family === 4) return lan.check(host, 'ipv4'); + if (family === 6) return lan.check(host, 'ipv6'); + // A single-label name (a Compose service such as "wise-wren") never leaves the host's network. + return host === 'localhost' || !host.includes('.') || LOCAL_NAME.test(host); +} + +/** + * Wise Wren's address in its canonical form, without a trailing slash: `https`, or `http` for this + * machine and the local network. No credentials, query or fragment. + * @param {unknown} raw + * @returns {string} + * @throws {Error} with a reason when it is not usable + */ +export function checkWrenUrl(raw) { + let url; + try { + url = new URL(String(raw ?? '').trim()); + } catch { + throw new Error('not a URL'); + } + if (url.username || url.password || url.search || url.hash) { + throw new Error('it must not contain credentials, a query or a fragment'); + } + if (!url.hostname) throw new Error('it has no host'); + if (url.protocol !== 'http:' && url.protocol !== 'https:') { + throw new Error('it must be https (http is only for the local network)'); + } + if (url.protocol === 'http:' && !isLocalHost(url.hostname)) { + throw new Error('it must be https (http is only for the local network)'); + } + return `${url.origin}${url.pathname.replace(/\/+$/, '')}`; +} + +/** + * The Wise Wren settings from the environment, or null when the integration is off. A partial or + * unusable setting turns it off too, with a reason for the log (never the token). + * @param {Record} env + * @returns {{ settings: { url: string, token: string, targetId: number } | null, problem: string | null }} + */ +export function readWrenSettings(env) { + const get = (name) => (env[name] ?? '').trim(); + const url = get('WISE_WREN_URL'); + const token = get('WISE_WREN_TOKEN'); + const id = get('WISE_WREN_TARGET_ID'); + if (!url && !token && !id) return { settings: null, problem: null }; + if (!url || !token || !id) { + return { + settings: null, + problem: 'WISE_WREN_URL, WISE_WREN_TOKEN and WISE_WREN_TARGET_ID must all be set; Wise Wren is off.', + }; + } + const targetId = Number(id); + if (!/^\d{1,9}$/.test(id) || !Number.isSafeInteger(targetId) || targetId < 1) { + return { settings: null, problem: 'WISE_WREN_TARGET_ID must be a whole number; Wise Wren is off.' }; + } + if (/[\s\r\n]/.test(token) || token.length > 512) { + return { settings: null, problem: 'WISE_WREN_TOKEN is not usable; Wise Wren is off.' }; + } + try { + return { settings: { url: checkWrenUrl(url), token, targetId }, problem: null }; + } catch (err) { + return { settings: null, problem: `WISE_WREN_URL is not usable (${err.message}); Wise Wren is off.` }; + } +} + +/** + * What went wrong, in a form the routes map to an answer. `message` is safe to show an operator: it + * is written here or is Wren's own short error text, and holds no token. + */ +export class WrenError extends Error { + /** @param {'unreachable'|'unauthorized'|'forbidden'|'not_found'|'conflict'|'refused'|'rate_limited'|'bad_request'|'server'|'invalid'} kind */ + constructor(kind, message, status = null) { + super(message); + this.name = 'WrenError'; + this.kind = kind; + this.status = status; + } +} + +const KIND_BY_STATUS = new Map([ + [400, 'bad_request'], + [401, 'unauthorized'], + [403, 'forbidden'], + [404, 'not_found'], + [409, 'conflict'], + [422, 'bad_request'], + [429, 'rate_limited'], +]); + +const FALLBACK_TEXT = { + unauthorized: "Wise Wren did not accept Sylo's token.", + forbidden: 'Wise Wren does not allow this token to do that.', + not_found: 'Wise Wren does not know this target.', + conflict: 'Wise Wren refused this update.', + refused: 'Wise Wren refused this update.', + rate_limited: 'Wise Wren says to slow down; try again in a moment.', + bad_request: 'Wise Wren did not accept the request.', + server: 'Wise Wren had an error.', +}; + +/** + * Wren's own error text when it sent a short one, never anything that could be markup or a secret. + * Every /api/v1 error is `{"error": {"code", "message"}}`; a plain string error or a top-level + * `message` are read too. + */ +function errorText(body) { + const e = body?.error; + const raw = + typeof e?.message === 'string' + ? e.message + : typeof e === 'string' + ? e + : typeof body?.message === 'string' + ? body.message + : ''; + const text = raw.replace(/\s+/g, ' ').trim(); + if (!text || text.length > 300) return ''; + return text; +} + +// Run states in which Wren is done with the run. +export const TERMINAL_RUN = new Set([ + 'succeeded', + 'rolled_back', + 'failed', + 'rollback_failed', + 'refused', + 'cancelled', + 'lapsed', +]); + +const str = (v, max = 200) => (typeof v === 'string' ? v.slice(0, max) : null); +const num = (v) => (Number.isFinite(v) ? v : null); + +/** A Wise Wren run as the dashboard needs it: no step logs (they can hold paths and image names). */ +export function shapeRun(run) { + if (!run || typeof run !== 'object' || !Number.isSafeInteger(run.id)) return null; + const status = str(run.status, 40) ?? 'unknown'; + return { + id: run.id, + targetId: Number.isSafeInteger(run.target?.id) ? run.target.id : null, + status, + finished: TERMINAL_RUN.has(status), + ok: status === 'succeeded', + detail: str(run.detail, 300), + version: str(run.version, 80), + createdAt: str(run.created_at, 40), + startedAt: str(run.started_at, 40), + finishedAt: str(run.finished_at, 40), + steps: Array.isArray(run.steps) + ? run.steps.slice(0, 20).map((s) => ({ + step: str(s?.step, 40), + status: str(s?.status, 40), + durationMs: num(s?.duration_ms), + })) + : [], + }; +} + +/** The target as the dashboard needs it. */ +export function shapeTarget(t) { + if (!t || typeof t !== 'object') return null; + const blocked = + typeof t.blocked_reason === 'string' && t.blocked_reason ? t.blocked_reason.slice(0, 300) : null; + return { + name: str(t.name, 120), + server: str(t.server?.name, 120), + status: ['update', 'current', 'unknown'].includes(t.status) ? t.status : 'unknown', + versionKey: typeof t.version_key === 'string' && t.version_key.length <= 2000 ? t.version_key : null, + installedVersion: str(t.installed_version, 80) || null, + latestVersion: str(t.latest_version, 80) || null, + mode: str(t.effective_mode, 20), + blockedReason: blocked, + skipped: Boolean(t.skipped), + snoozed: Boolean(t.snoozed_until), + lastCheckedAt: str(t.last_checked_at, 40), + }; +} + +// A read is quick or it is broken; an approval may take Wren a moment longer. +export const READ_TIMEOUT_MS = 4000; +// The most of an answer Sylo reads from Wise Wren (characters). +const MAX_BODY_CHARS = 1_000_000; +export const APPROVE_TIMEOUT_MS = 10_000; + +/** + * @param {{ url: string, token: string, targetId: number }} settings + * @param {{ fetchImpl?: typeof fetch, readTimeoutMs?: number, approveTimeoutMs?: number }} [opts] + */ +export function createWrenClient( + settings, + { fetchImpl, readTimeoutMs = READ_TIMEOUT_MS, approveTimeoutMs = APPROVE_TIMEOUT_MS } = {} +) { + const { url, token, targetId } = settings; + + async function call(method, path, body, timeoutMs = readTimeoutMs) { + let res; + try { + res = await (fetchImpl ?? globalThis.fetch)(`${url}${path}`, { + method, + headers: { + authorization: `Bearer ${token}`, + accept: 'application/json', + 'user-agent': 'Sylo (update via Wise Wren)', + ...(body === undefined ? {} : { 'content-type': 'application/json' }), + }, + body: body === undefined ? undefined : JSON.stringify(body), + // A redirect would carry the request (and its token) to wherever it points. + redirect: 'manual', + signal: AbortSignal.timeout(timeoutMs), + }); + } catch (err) { + // The error of a failed request can hold the address; the reason alone is enough. + const timedOut = err?.name === 'TimeoutError' || err?.name === 'AbortError'; + throw new WrenError( + 'unreachable', + timedOut ? 'Wise Wren did not answer in time.' : 'Wise Wren is not reachable.' + ); + } + if (res.status >= 300 && res.status < 400) { + throw new WrenError('server', 'Wise Wren answered with a redirect; use its final address.', res.status); + } + let data; + try { + // Read as it streams and cut off at the cap: a hostile or spoofed answer must not be buffered whole. + const declared = Number(res.headers.get('content-length')); + if (Number.isFinite(declared) && declared > MAX_BODY_CHARS) { + await res.body?.cancel?.().catch(() => {}); + throw new Error('the answer is too large'); + } + const text = await readCapped(res, MAX_BODY_CHARS); + data = text ? JSON.parse(text) : null; + } catch { + data = null; + } + if (!res.ok) { + const kind = KIND_BY_STATUS.get(res.status) ?? 'server'; + throw new WrenError(kind, errorText(data) || FALLBACK_TEXT[kind], res.status); + } + if (data === null || typeof data !== 'object') { + throw new WrenError('invalid', 'Wise Wren sent an answer Sylo does not understand.', res.status); + } + return data; + } + + return { + targetId, + async target() { + return shapeTarget(await call('GET', `/api/v1/targets/${targetId}`)); + }, + /** + * Approve exactly the update with this version key (Wren refuses it when a newer one has appeared, + * when a run is already queued or running, when the target only notifies, or when there is no + * update). Those refusals are `refused`, with Wren's own words. + * @returns {Promise<{ runId: number, status: string }>} + */ + async approve(versionKey) { + if (typeof versionKey !== 'string' || !versionKey) { + throw new WrenError('refused', 'Wise Wren shows no version to approve yet.'); + } + let data; + try { + data = await call( + 'POST', + `/api/v1/targets/${targetId}/approve`, + { version_key: versionKey }, + approveTimeoutMs + ); + } catch (err) { + // 400 and 409 on an approval are Wren saying no to the action, not Sylo sending junk. + if (err instanceof WrenError && (err.kind === 'bad_request' || err.kind === 'conflict')) { + throw new WrenError('refused', err.message, err.status); + } + throw err; + } + if (!Number.isSafeInteger(data.run_id)) { + throw new WrenError('invalid', 'Wise Wren did not say which run it started.'); + } + return { runId: data.run_id, status: str(data.status, 40) ?? 'queued' }; + }, + /** One run with its steps, or a WrenError (also `not_found` for a run of another target). */ + async run(id) { + if (!Number.isSafeInteger(id) || id < 1) throw new WrenError('not_found', 'No such run.'); + const run = shapeRun(await call('GET', `/api/v1/runs/${id}`)); + if (!run || run.targetId !== targetId) throw new WrenError('not_found', 'No such run.'); + return run; + }, + }; +} diff --git a/src/web/lib/routeAccess.js b/src/web/lib/routeAccess.js index fb09e393..3f9bddc1 100644 --- a/src/web/lib/routeAccess.js +++ b/src/web/lib/routeAccess.js @@ -134,6 +134,9 @@ export const ROUTE_ACCESS = Object.freeze({ 'POST /health/backups/:name/delete': 'operator', 'POST /health/backups/import': 'operator', 'POST /health/backups/:name/restore': 'operator', + 'GET /update': 'operator', + 'POST /update/wren/approve': 'operator', + 'GET /update/wren/runs/:id': 'operator', 'GET /roadmap': 'self', 'POST /roadmap/:id/vote': 'self', 'POST /roadmap/suggest': 'self', diff --git a/src/web/lib/updateService.js b/src/web/lib/updateService.js new file mode 100644 index 00000000..9c70ff91 --- /dev/null +++ b/src/web/lib/updateService.js @@ -0,0 +1,208 @@ +// What the operators' update notice shows and does, built from the release check (lib/updateCheck.js) and +// the optional Wise Wren client (lib/wiseWren.js). The routes (routes/v2Update.js) only call this. +// +// The Wise Wren side is read live but kept for a few seconds, so several operators (or one dashboard +// polling) do not turn into a request to Wise Wren each. Nothing here ever includes the Wise Wren address +// or token in an answer. +import { WrenError } from '../../lib/wiseWren.js'; + +export const WREN_CACHE_MS = 10_000; + +/** A refusal the route answers with `status` and `{ error, code }`. */ +export class UpdateError extends Error { + constructor(status, code, message) { + super(message); + this.name = 'UpdateError'; + this.status = status; + this.code = code; + } +} + +/** + * Wise Wren's errors as the HTTP answer to the dashboard. Never 401 or 403: those tell the dashboard that + * the operator's own session is gone or not allowed, and here it is Wise Wren that said no. + * @param {unknown} err + */ +export function toUpdateError(err) { + if (err instanceof UpdateError) return err; + if (err instanceof WrenError) { + const status = + err.kind === 'conflict' || err.kind === 'refused' + ? 409 + : err.kind === 'not_found' + ? 404 + : err.kind === 'rate_limited' + ? 429 + : 502; + return new UpdateError(status, `wren_${err.kind}`, err.message); + } + return null; +} + +/** What the dashboard tells an operator in open mode, where nobody is signed in. */ +export const OPEN_MODE_TEXT = + 'Update now needs a dashboard login: without one, anyone who can reach this dashboard could use it. Set up a login, or set WISE_WREN_ALLOW_OPEN_MODE=true to allow it anyway.'; + +/** + * @param {{ + * checker: { get(opts?: { force?: boolean }): Promise }, + * wren: ReturnType | null, + * store?: { get(): Promise<{ id: number } | null>, set(run: { id: number }): Promise }, + * openModeBlocked?: () => boolean, + * now?: () => number, + * }} deps + * + * `store` keeps the run Sylo last approved, so "Update now" stays off while it runs, also across the + * restart the update causes. It is a run id only: the Wise Wren target token may read a run by id but + * not list runs. `openModeBlocked` says Update now is refused because nobody is signed in. + */ +export function createUpdateService({ + checker, + wren, + store = null, + openModeBlocked = () => false, + now = Date.now, +}) { + let seen = null; // { at, value } of the last Wise Wren read + let inflight = null; // the read in progress, shared by everyone who asks meanwhile + let lastRunId = null; // the run approved by this process + + const drop = () => { + seen = null; + inflight = null; + }; + + async function knownRunId() { + if (lastRunId) return lastRunId; + try { + return (await store?.get())?.id ?? null; + } catch { + return null; + } + } + + function readWren() { + if (seen && now() - seen.at < WREN_CACHE_MS) return Promise.resolve(seen.value); + if (!inflight) { + const mine = readWrenUncached() + .then((value) => { + if (inflight === mine) seen = { at: now(), value }; + return value; + }) + .finally(() => { + if (inflight === mine) inflight = null; + }); + inflight = mine; + } + return inflight; + } + + // The target and the run Sylo approved, asked at the same time (a read is short or it is broken). + async function readWrenUncached() { + const out = { target: null, error: null, latestRun: null }; + const runId = await knownRunId(); + const [target, run] = await Promise.allSettled([wren.target(), runId ? wren.run(runId) : null]); + if (target.status === 'rejected') { + if (!(target.reason instanceof WrenError)) throw target.reason; + // A 403 here means the token is for another target (or may not read this one): an error to fix. + out.error = { code: `wren_${target.reason.kind}`, message: target.reason.message }; + return out; + } + out.target = target.value; + if (run.status === 'rejected') { + if (!(run.reason instanceof WrenError)) throw run.reason; + // Wren hiccuped: the page just has no run to show now. + } else out.latestRun = run.value; + return out; + } + + /** Why "Update now" is not offered, or null when it is. */ + function whyNot(w) { + if (w.error) return null; + if (openModeBlocked()) return OPEN_MODE_TEXT; + if (w.latestRun && !w.latestRun.finished) return 'An update of Sylo is already running in Wise Wren.'; + const t = w.target; + if (!t) return null; + if (t.blockedReason) return `Wise Wren would refuse it: ${t.blockedReason}`; + if (t.mode === 'notify') { + return 'Wise Wren only notifies for this target. Change its mode in Wise Wren to allow updates.'; + } + if (t.status === 'update') { + return t.versionKey ? null : 'Wise Wren shows no version to approve yet. Try again in a moment.'; + } + if (t.status === 'current') + return 'Wise Wren does not see an update yet. It checks every few hours; use Check now there.'; + return 'Wise Wren cannot tell yet whether Sylo has an update.'; + } + + /** + * The state for GET /api/v2/update. + * @param {{ force?: boolean }} [opts] + */ + async function status({ force = false } = {}) { + if (force) drop(); + // GitHub and Wise Wren are asked at the same time. + const [check, w] = await Promise.all([checker.get({ force }), wren ? readWren() : null]); + if (!wren) return { ...check, wren: null }; + const blocked = whyNot(w); + return { + ...check, + wren: { + configured: true, + error: w.error, + openMode: openModeBlocked(), + target: w.target + ? { + name: w.target.name, + server: w.target.server, + status: w.target.status, + installedVersion: w.target.installedVersion, + latestVersion: w.target.latestVersion, + mode: w.target.mode, + lastCheckedAt: w.target.lastCheckedAt, + } + : null, + // Wren sees an update, whatever the GitHub check says (they can differ for a while). + updateSeen: w.target?.status === 'update', + canApprove: !w.error && blocked === null, + whyNot: blocked, + // The run Sylo last approved (the page follows it while it is not finished). + latestRun: w.latestRun, + }, + }; + } + + /** + * Ask Wise Wren to update Sylo now: exactly the version Wren shows. + * @returns {Promise<{ runId: number, status: string, from: string }>} + */ + async function approve() { + if (!wren) throw new UpdateError(404, 'wren_off', 'Wise Wren is not set up for this Sylo.'); + // Before anything is asked of Wise Wren: nobody is signed in. + if (openModeBlocked()) throw new UpdateError(409, 'open_mode', OPEN_MODE_TEXT); + // Always a fresh read: the approval must be for the version Wren shows now. + drop(); + const w = await readWren(); + if (w.error) throw new UpdateError(502, w.error.code, w.error.message); + const why = whyNot(w); + if (why) throw new UpdateError(409, 'not_approvable', why); + const check = await checker.get(); + const { runId, status } = await wren.approve(w.target.versionKey); + lastRunId = runId; + try { + await store?.set({ id: runId }); + } catch { + // kept in memory for this process; only a restart would forget it + } + drop(); + return { runId, status, from: check.current }; + } + + /** One run for GET /api/v2/update/wren/runs/:id (a run of another target is a not_found). */ + async function run(id) { + if (!wren) throw new UpdateError(404, 'wren_off', 'Wise Wren is not set up for this Sylo.'); + return { run: await wren.run(id) }; + } + + return { status, approve, run, drop }; +} diff --git a/src/web/routes/mobile.js b/src/web/routes/mobile.js index 1d5575c4..68cce09d 100644 --- a/src/web/routes/mobile.js +++ b/src/web/routes/mobile.js @@ -312,6 +312,8 @@ export const mobileApiRouter = Router(); // sign-in is on and both Discord and Fluxer logins are set up. `access-levels`: View / Inbox / Edit // dashboard access, with `access` on each GET /api/v2/guilds entry and GET/POST // /api/v2/guilds/:id/access. `message-move`: a posted message moves to its new channel on publish. +// `update-notice`: operators only, GET /api/v2/update (a newer Sylo release, optionally what Wise Wren sees) +// and, with Wise Wren set up, POST /api/v2/update/wren/approve and GET /api/v2/update/wren/runs/:id. const FEATURES = Object.freeze([ 'app-login', 'live-access', @@ -328,6 +330,7 @@ const FEATURES = Object.freeze([ 'duplicate', 'message-templates', 'message-move', + 'update-notice', ]); // Public and ahead of the open-mode guard: the app calls it before anyone signs in, and an open-mode diff --git a/src/web/routes/v2Api.js b/src/web/routes/v2Api.js index 1db30f44..0130810a 100644 --- a/src/web/routes/v2Api.js +++ b/src/web/routes/v2Api.js @@ -57,6 +57,7 @@ import guildDataRouter from './v2GuildData.js'; import configImportRouter from './v2ConfigImport.js'; import duplicateRouter from './v2Duplicate.js'; import healthRouter, { dropHealthOnWrite } from './v2Health.js'; +import updateRouter from './v2Update.js'; import { moduleIcon } from '../lib/moduleIcons.js'; import eventsRouter from './v2Events.js'; import suggestionsRouter from './v2Suggestions.js'; @@ -507,6 +508,8 @@ function loadGuildJson(req, res, next) { // sits before the access guard below and does its own access check (v2ShopMember.js). // Every /guilds/:guildId route after the guard needs the level src/web/lib/routeAccess.js gives it // (view, inbox or edit; edit when it gives none), checked by the access router (access.js). +// The operators' update notice and Wise Wren's "Update now" (v2Update.js): operator routes, not per server. +router.use(updateRouter); router.use('/shop/:guildId', shopMemberRouter); // The web form of an application (/apply//
): the same kind of page, the same access rule. router.use('/apply/:guildId', applyMemberRouter); diff --git a/src/web/routes/v2Update.js b/src/web/routes/v2Update.js new file mode 100644 index 00000000..7a8283c4 --- /dev/null +++ b/src/web/routes/v2Update.js @@ -0,0 +1,157 @@ +// The operators' update notice (GET /api/v2/update and, with Wise Wren set up, "Update now"). Every route +// here is operator-only (OWNER_IDS / FLUXER_OWNER_IDS, requireOwner): a new release and Sylo's own updater +// are about the whole instance, not one server, so a server admin gets nothing, not even the version of +// the newest release. Writes carry the usual CSRF check (app-wide) and the approve route its own small +// rate limit. Used by the dashboard's Health page and top bar and by the Sylo app's operator screens +// (feature `update-notice`, docs/mobile-app.md). +import { createRequire } from 'node:module'; +import { accessRouter } from '../middleware/access.js'; +import { currentUser, requireOwner } from '../middleware/auth.js'; +import { clientKey, rateLimit } from '../middleware/rateLimit.js'; +import { asyncHandler } from '../lib/asyncHandler.js'; +import { createUpdateService, toUpdateError } from '../lib/updateService.js'; +import { createUpdateChecker } from '../../lib/updateCheck.js'; +import { createWrenClient } from '../../lib/wiseWren.js'; +import { recordBotAudit } from '../../db/botAudit.js'; +import { getAppSetting, setAppSetting } from '../../db/appSettings.js'; +import { config } from '../../config.js'; +import { log } from '../../lib/log.js'; + +const { version } = createRequire(import.meta.url)('../../../package.json'); + +const router = accessRouter(''); + +// The run Sylo last approved, kept in the database so it is still known after the restart the update +// causes (and "Update now" stays off while it runs). Only the run id, and only for the same target. +const RUN_KEY = 'update.wren.lastRun'; +const runStore = { + async get() { + try { + const run = JSON.parse((await getAppSetting(RUN_KEY)) ?? 'null'); + return Number.isSafeInteger(run?.id) && run.targetId === config.wiseWren?.targetId + ? { id: run.id } + : null; + } catch { + return null; + } + }, + async set(run) { + await setAppSetting(RUN_KEY, JSON.stringify({ id: run.id, targetId: config.wiseWren?.targetId })); + }, +}; + +/** + * Open mode (no dashboard login): nobody is signed in, so "Update now" is refused unless + * WISE_WREN_ALLOW_OPEN_MODE=true. + */ +const openModeBlocked = () => !config.authEnabled && !config.wiseWrenAllowOpenMode; + +/** The one service the routes use; tests that need another call `setUpdateService`. */ +let service = createUpdateService({ + checker: createUpdateChecker({ enabled: config.updateCheck, version, log }), + wren: config.wiseWren ? createWrenClient(config.wiseWren) : null, + store: runStore, + openModeBlocked, +}); + +/** Replace the service (tests only). */ +export function setUpdateService(next) { + service = next; +} + +// Per operator (or per client in open mode), so one person's polling cannot use another's budget. +const who = (req) => req.account?.id ?? clientKey(req); +const readLimit = rateLimit({ + windowMs: 60_000, + max: 30, + message: 'Too many update checks — wait a moment.', + keyFn: (req) => `update-read|${who(req)}`, +}); +// "Check again" asks GitHub: a few a minute is plenty. +const refreshLimit = rateLimit({ + windowMs: 60_000, + max: 6, + message: 'Too many re-checks — wait a minute.', + keyFn: (req) => `update-refresh|${who(req)}`, +}); +// An update restarts Sylo; nobody needs more than a handful of attempts a minute. +const approveLimit = rateLimit({ + windowMs: 60_000, + max: 5, + message: 'Too many update requests — wait a minute.', + keyFn: (req) => `update-approve|${who(req)}`, +}); +// The page polls a run every few seconds while Sylo updates. +const runLimit = rateLimit({ + windowMs: 60_000, + max: 60, + message: 'Too many progress checks — wait a moment.', + keyFn: (req) => `update-run|${who(req)}`, +}); + +/** Wise Wren's refusals as JSON; anything else is a real error for the error handler. */ +function fail(err, res, next) { + const mapped = toUpdateError(err); + if (!mapped) return next(err); + if (mapped.status >= 500) log.debug('update', `Wise Wren: ${mapped.code}`); + return res.status(mapped.status).json({ error: mapped.message, code: mapped.code }); +} + +const wantsFresh = (req) => ['1', 'true'].includes(String(req.query.refresh ?? '')); + +router.get( + '/update', + requireOwner, + (req, res, next) => (wantsFresh(req) ? refreshLimit(req, res, next) : next()), + readLimit, + asyncHandler(async (req, res, next) => { + try { + res.set('Cache-Control', 'no-store'); + res.json(await service.status({ force: wantsFresh(req) })); + } catch (err) { + fail(err, res, next); + } + }) +); + +router.post( + '/update/wren/approve', + requireOwner, + approveLimit, + asyncHandler(async (req, res, next) => { + try { + const started = await service.approve(); + // Who asked, for the Health page's bot-wide audit log. Never the run's text or any token. + try { + await recordBotAudit({ + actor: currentUser(req)?.open ? 'Dashboard' : `${currentUser(req)?.name ?? 'operator'} (dashboard)`, + action: 'update:approve', + detail: `asked Wise Wren to update Sylo from ${started.from} (run ${started.runId})`, + }); + } catch (err) { + log.debug('update', 'audit entry failed:', err); + } + log.info('update', `an operator approved the Sylo update in Wise Wren (run ${started.runId})`); + res.json(started); + } catch (err) { + fail(err, res, next); + } + }) +); + +router.get( + '/update/wren/runs/:id', + requireOwner, + runLimit, + asyncHandler(async (req, res, next) => { + const id = /^\d{1,9}$/.test(req.params.id) ? Number(req.params.id) : 0; + try { + res.set('Cache-Control', 'no-store'); + res.json(await service.run(id)); + } catch (err) { + fail(err, res, next); + } + }) +); + +export default router; diff --git a/test/helpers/fakeWren.js b/test/helpers/fakeWren.js new file mode 100644 index 00000000..e3013f36 --- /dev/null +++ b/test/helpers/fakeWren.js @@ -0,0 +1,168 @@ +// A fake Wise Wren (its REST API v1, only the routes Sylo uses) on a free local port, for the update +// notice's tests. It answers like the real one: +// - every error is vole-kit's `{"error": {"code", "message"}}`; +// - an approval Wren refuses is a 400 (the version key changed, a run is already queued or running, the +// target only notifies, there is no update), and a 409 only when the agent would refuse the target +// (`blocked_reason`); +// - a second approval while a run is queued or running is refused; +// - with `targetToken: true` it behaves for a single-target token: only GET /targets/{id}, POST +// /targets/{id}/approve and GET /runs/{id} of its own target, and everything else (the run list, other +// targets and runs) is a 403. +// It checks the bearer token and records every request, so a test can assert what Sylo sent (and that the +// token went nowhere else). +import { createServer } from 'node:http'; + +export const FAKE_TOKEN = 'ww_test_secret_token_0123456789'; + +const ACTIVE = new Set(['queued', 'running']); + +/** + * @param {{ targetId?: number, token?: string, targetToken?: boolean }} [opts] + */ +export async function startFakeWren({ targetId = 7, token = FAKE_TOKEN, targetToken = false } = {}) { + /** What the next answers are; a test changes these. */ + const state = { + target: { + id: targetId, + server: { id: 1, name: 'LANBox' }, + kind: 'compose', + name: 'sylo', + status: 'update', + version_key: 'sylo=sha256:4f90abcd', + installed_version: '5.2.0', + latest_version: '5.3.0', + effective_mode: 'approve', + blocked_reason: null, + skipped: false, + snoozed_until: null, + last_checked_at: '2026-11-16 10:00:00', + }, + /** Run id → run (with steps) */ + runs: new Map(), + nextRunId: 12, + /** Force an answer for a route key ("GET target", "POST approve", "GET run", "GET runs"): [status, body, headers]. */ + force: {}, + /** Delay every answer by this long (ms). */ + delayMs: 0, + /** Mode for a token limited to one target (a test may flip it). */ + targetToken, + }; + /** @type {{ method: string, url: string, headers: Record, body: string }[]} */ + const requests = []; + + const json = (res, status, body, headers = {}) => { + res.writeHead(status, { 'content-type': 'application/json', ...headers }); + res.end(JSON.stringify(body)); + }; + const fail = (res, status, code, message) => json(res, status, { error: { code, message } }); + + const server = createServer((req, res) => { + let body = ''; + req.on('data', (c) => (body += c)); + req.on('end', async () => { + requests.push({ method: req.method, url: req.url, headers: { ...req.headers }, body }); + if (state.delayMs) await new Promise((r) => setTimeout(r, state.delayMs)); + if (req.headers.authorization !== `Bearer ${token}`) { + return fail(res, 401, 'unauthorized', 'Not signed in.'); + } + const url = new URL(req.url, 'http://x'); + const target = /^\/api\/v1\/targets\/(\d+)(\/approve)?$/.exec(url.pathname); + const runPath = /^\/api\/v1\/runs\/(\d+)$/.exec(url.pathname); + const key = + req.method === 'GET' && target && !target[2] + ? 'GET target' + : req.method === 'POST' && target && target[2] + ? 'POST approve' + : req.method === 'GET' && runPath + ? 'GET run' + : req.method === 'GET' && url.pathname === '/api/v1/runs' + ? 'GET runs' + : null; + if (!key) return fail(res, 404, 'not_found', 'Not found.'); + const forced = state.force[key]; + if (forced) return json(res, forced[0], forced[1], forced[2]); + + // A token limited to one target: the list and every other target are refused. + if (state.targetToken) { + if (key === 'GET runs') return fail(res, 403, 'forbidden', 'This token is not allowed here.'); + if (target && Number(target[1]) !== targetId) { + return fail(res, 403, 'forbidden', 'This token is not allowed for that target.'); + } + } else if (target && Number(target[1]) !== targetId) { + return fail(res, 404, 'not_found', 'Not found.'); + } + + if (key === 'GET target') return json(res, 200, state.target); + if (key === 'POST approve') { + const sent = body ? JSON.parse(body) : {}; + const t = state.target; + if (t.blocked_reason) { + return fail(res, 409, 'conflict', `The agent would refuse this update: ${t.blocked_reason}.`); + } + if (t.effective_mode === 'notify') { + return fail( + res, + 400, + 'bad_request', + 'This target is notify-only: Wren only tells you about new versions.' + ); + } + if (t.status !== 'update') return fail(res, 400, 'bad_request', 'There is no update to approve.'); + if ([...state.runs.values()].some((r) => r.target.id === targetId && ACTIVE.has(r.status))) { + return fail(res, 400, 'bad_request', 'An update of this target is already queued or running.'); + } + if (sent.version_key && sent.version_key !== t.version_key) { + return fail( + res, + 400, + 'bad_request', + 'The version changed since you looked; reload and approve the new one.' + ); + } + const id = state.nextRunId++; + state.runs.set(id, { + id, + server: { id: 1, name: 'LANBox' }, + target: { id: targetId, kind: 'compose', name: 'sylo' }, + kind: 'update', + status: 'queued', + detail: '', + version: '5.3.0', + created_at: '2026-11-16 10:01:00', + started_at: null, + finished_at: null, + steps: [], + }); + return json(res, 200, { run_id: id, status: 'queued' }); + } + if (key === 'GET run') { + const run = state.runs.get(Number(runPath[1])); + if (!run) return fail(res, 404, 'not_found', 'Not found.'); + if (state.targetToken && run.target.id !== targetId) { + return fail(res, 403, 'forbidden', 'This token is not allowed for that run.'); + } + return json(res, 200, run); + } + // GET runs (a plain write token only) + const list = [...state.runs.values()] + .filter((r) => String(r.target.id) === url.searchParams.get('target_id')) + .sort((a, b) => b.id - a.id) + .slice(0, Number(url.searchParams.get('limit') ?? 50)) + .map(({ steps: _steps, ...r }) => r); + return json(res, 200, { runs: list }); + }); + }); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + + return { + url: `http://127.0.0.1:${server.address().port}`, + token, + targetId, + state, + requests, + close: () => { + server.closeAllConnections?.(); + server.close(); + }, + }; +} diff --git a/test/mobileInfo.test.js b/test/mobileInfo.test.js index efba5685..c29a4a24 100644 --- a/test/mobileInfo.test.js +++ b/test/mobileInfo.test.js @@ -51,6 +51,7 @@ test('GET /api/mobile/info describes a server with login on, without a cookie or assert.ok(info.features.includes(f), f); } assert.ok(info.features.includes('message-move'), 'a posted message moves when its channel changes'); + assert.ok(info.features.includes('update-notice'), 'GET /api/v2/update for operators'); assert.ok(info.features.every((f) => typeof f === 'string')); }); diff --git a/test/updateCheck.test.js b/test/updateCheck.test.js new file mode 100644 index 00000000..516120e1 --- /dev/null +++ b/test/updateCheck.test.js @@ -0,0 +1,319 @@ +// The update check (src/lib/updateCheck.js): comparing versions, the cache, the switch that turns it off, +// quiet failures, and what the release notes become (data with https links only, never markup). +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { + CHECK_TTL_MS, + FORCE_MIN_INTERVAL_MS, + RETRY_AFTER_FAILURE_MS, + UPDATE_COMMAND, + compareVersions, + createUpdateChecker, + parseVersion, + shapeNotes, + shapeRelease, +} from '../src/lib/updateCheck.js'; + +const BODY = `## [5.3.0](https://github.com/Ferdinand99/Sylo/compare/v5.2.0...v5.3.0) (2026-11-20) + + +### Features + +* **dashboard:** update notice ([#370](https://github.com/Ferdinand99/Sylo/issues/370)) ([abc1234](https://github.com/Ferdinand99/Sylo/commit/abc1234def)) +* a [bad link](javascript:alert(1)) and an [http link](http://example.com/x) and [good](https://example.com/ok) + +### Bug Fixes + +* **security:** something ([#371](http://github.com/Ferdinand99/Sylo/issues/371)) +`; + +const release = (over = {}) => ({ + tag_name: 'v5.3.0', + html_url: 'https://github.com/Ferdinand99/Sylo/releases/tag/v5.3.0', + published_at: '2026-11-20T10:00:00Z', + draft: false, + prerelease: false, + body: BODY, + ...over, +}); + +const okFetch = (payload, calls = []) => + async function fakeFetch(url, init) { + calls.push({ url: String(url), init }); + return new Response(JSON.stringify(payload), { + status: 200, + headers: { 'content-type': 'application/json' }, + }); + }; + +test('versions compare as numbers, a release outranks its prereleases, junk is null', () => { + assert.equal(compareVersions('5.3.0', '5.2.0'), 1); + assert.equal(compareVersions('5.2.0', '5.3.0'), -1); + assert.equal(compareVersions('v5.2.0', '5.2.0'), 0); + assert.equal(compareVersions('5.10.0', '5.9.9'), 1, 'numbers, not text'); + assert.equal(compareVersions('6.0.0', '5.99.99'), 1); + assert.equal(compareVersions('5.3.0', '5.3.0-rc.1'), 1); + assert.equal(compareVersions('5.3.0-rc.2', '5.3.0-rc.10'), -1); + assert.equal(compareVersions('5.3.0-alpha', '5.3.0-beta'), -1); + assert.equal(compareVersions('5.3.0-rc.1', '5.3.0-rc'), 1); + assert.equal(compareVersions('latest', '5.3.0'), null); + assert.equal(compareVersions('5.3', '5.3.0'), null); + assert.equal(compareVersions(undefined, '5.3.0'), null); + assert.equal(parseVersion('5.3.0+build.5').nums.join('.'), '5.3.0'); +}); + +test('a release becomes the version, a GitHub page and notes as data', () => { + const r = shapeRelease(release()); + assert.equal(r.version, '5.3.0'); + assert.equal(r.url, 'https://github.com/Ferdinand99/Sylo/releases/tag/v5.3.0'); + assert.equal(r.publishedAt, '2026-11-20T10:00:00Z'); + assert.equal(r.notesText, null); + assert.deepEqual( + r.notes.sections.map((s) => [s.title, s.kind, s.items.length]), + [ + ['New', 'features', 2], + ['Fixes', 'fixes', 1], + ] + ); + assert.equal(r.notes.items, undefined); +}); + +test('the notes keep https links only, as links or as plain labels', () => { + const { notes } = shapeRelease(release()); + const items = notes.sections[0].items; + assert.equal(items[0].scope, 'dashboard'); + // The text carries no HTML and the bad and http links are only their labels. + const text = items[1].text; + assert.doesNotMatch(text, /javascript:/); + assert.doesNotMatch(text, /http:\/\/example\.com/); + assert.match(text, /\[good\]\(https:\/\/example\.com\/ok\)/); + assert.match(text, /bad link/); + assert.match(text, /http link/); + // The PR reference on the other link: an http one is dropped, an https one kept. + assert.deepEqual(items[0].refs, [{ label: '#370', url: 'https://github.com/Ferdinand99/Sylo/issues/370' }]); + assert.deepEqual(notes.sections[1].items[0].refs, []); +}); + +test('notes that are not in the changelog format come back as short plain text', () => { + const { notes, notesText } = shapeNotes( + 'Plain **text** with a [link](http://x.test/a).\n\nAnd more.', + '5.3.0' + ); + assert.equal(notes, null); + assert.match(notesText, /^Plain \*\*text\*\* with a link\./); + assert.equal(shapeNotes('', '5.3.0').notesText, null); + assert.equal(shapeNotes('x'.repeat(10000), '5.3.0').notesText.length, 4000); +}); + +test('drafts, prereleases and non-versions are not offered', () => { + assert.equal(shapeRelease(release({ draft: true })), null); + assert.equal(shapeRelease(release({ prerelease: true })), null); + assert.equal(shapeRelease(release({ tag_name: 'nightly' })), null); + assert.equal(shapeRelease(null), null); + // A release page outside the Sylo repository is not passed on. + assert.equal(shapeRelease(release({ html_url: 'https://evil.test/x' })).url, null); +}); + +test('a newer release is available, with notes and the command; the same version is not', async () => { + const calls = []; + const newer = createUpdateChecker({ version: '5.2.0', fetchImpl: okFetch(release(), calls) }); + const s = await newer.get(); + assert.equal(s.enabled, true); + assert.equal(s.available, true); + assert.equal(s.current, '5.2.0'); + assert.equal(s.latest, '5.3.0'); + assert.equal(s.checkFailed, false); + assert.equal(s.command, UPDATE_COMMAND); + assert.equal(s.command, 'docker compose pull && docker compose up -d'); + assert.ok(s.notes.sections.length); + assert.ok(Date.parse(s.checkedAt)); + + const same = await createUpdateChecker({ version: '5.3.0', fetchImpl: okFetch(release()) }).get(); + assert.equal(same.available, false); + assert.equal(same.latest, '5.3.0'); + assert.equal(same.notes, null, 'no notes for a version the operator already runs'); + const ahead = await createUpdateChecker({ version: '5.4.0', fetchImpl: okFetch(release()) }).get(); + assert.equal(ahead.available, false); +}); + +test('the request is anonymous, to the fixed GitHub address, with a timeout', async () => { + const calls = []; + await createUpdateChecker({ version: '5.2.0', fetchImpl: okFetch(release(), calls) }).get(); + assert.equal(calls.length, 1); + assert.equal(calls[0].url, 'https://api.github.com/repos/Ferdinand99/Sylo/releases/latest'); + const headers = calls[0].init.headers; + assert.equal(headers.authorization, undefined); + assert.equal(headers.cookie, undefined); + assert.match(headers['user-agent'], /^Sylo\/5\.2\.0/); + assert.ok(calls[0].init.signal instanceof AbortSignal); + assert.equal(calls[0].init.redirect, 'error'); +}); + +test('the answer is cached for six hours, then asked again; many callers share one request', async () => { + let clock = 1_000_000; + const calls = []; + const checker = createUpdateChecker({ + version: '5.2.0', + now: () => clock, + fetchImpl: okFetch(release(), calls), + }); + await Promise.all([checker.get(), checker.get(), checker.get()]); + assert.equal(calls.length, 1, 'one request for three callers at once'); + clock += CHECK_TTL_MS - 1000; + await checker.get(); + assert.equal(calls.length, 1, 'still cached'); + clock += 2000; + await checker.get(); + assert.equal(calls.length, 2, 'asked again after six hours'); + await checker.get({ force: true }); + assert.equal(calls.length, 2, 'a forced check right after a look is served from the cache'); + clock += FORCE_MIN_INTERVAL_MS; + await checker.get({ force: true }); + assert.equal(calls.length, 3, 'a forced check asks again after the minimum interval'); +}); + +test('UPDATE_CHECK=off: nothing is requested', async () => { + const calls = []; + const checker = createUpdateChecker({ + enabled: false, + version: '5.2.0', + fetchImpl: okFetch(release(), calls), + }); + const s = await checker.get(); + await checker.get({ force: true }); + assert.equal(calls.length, 0); + assert.equal(s.enabled, false); + assert.equal(s.available, false); + assert.equal(s.latest, null); + assert.equal(s.current, '5.2.0'); +}); + +test('a failing check never throws, warns once, and waits before the next try', async () => { + let clock = 5_000_000; + let calls = 0; + const logged = { warn: [], debug: [] }; + const checker = createUpdateChecker({ + version: '5.2.0', + now: () => clock, + fetchImpl: async () => { + calls++; + throw new Error('getaddrinfo ENOTFOUND api.github.com'); + }, + log: { warn: (...a) => logged.warn.push(a.join(' ')), debug: (...a) => logged.debug.push(a.join(' ')) }, + }); + const s = await checker.get(); + assert.equal(s.checkFailed, true); + assert.equal(s.available, false); + assert.equal(s.latest, null); + await checker.get(); + assert.equal(calls, 1, 'not asked again straight away'); + clock += RETRY_AFTER_FAILURE_MS + 1; + await checker.get(); + assert.equal(calls, 2); + assert.equal(logged.warn.length, 1, 'one warning'); + assert.equal(logged.debug.length, 1, 'later failures are debug lines'); +}); + +test('a failed re-check keeps the last good answer', async () => { + let clock = 9_000_000; + let fail = false; + const checker = createUpdateChecker({ + version: '5.2.0', + now: () => clock, + fetchImpl: async () => + fail ? new Response('nope', { status: 503 }) : new Response(JSON.stringify(release()), { status: 200 }), + }); + assert.equal((await checker.get()).available, true); + fail = true; + clock += CHECK_TTL_MS + 1; + const s = await checker.get(); + assert.equal(s.checkFailed, true); + assert.equal(s.available, true, 'the earlier answer still shows'); + assert.equal(s.latest, '5.3.0'); +}); + +test('bad answers count as a failed check: HTTP errors, not JSON, no usable release, too large', async () => { + const answers = [ + () => new Response('rate limited', { status: 403 }), + () => new Response('', { status: 200 }), + () => new Response(JSON.stringify({ tag_name: 'nightly' }), { status: 200 }), + () => new Response('x', { status: 200, headers: { 'content-length': '5000000' } }), + () => new Response(JSON.stringify(release({ body: 'x'.repeat(1_100_000) })), { status: 200 }), + ]; + for (const answer of answers) { + const s = await createUpdateChecker({ version: '5.2.0', fetchImpl: async () => answer() }).get(); + assert.equal(s.checkFailed, true); + assert.equal(s.available, false); + } +}); + +test('a request that never answers is cut off by the timeout', async () => { + const checker = createUpdateChecker({ + version: '5.2.0', + timeoutMs: 50, + fetchImpl: (_url, { signal }) => + new Promise((_resolve, reject) => { + signal.addEventListener('abort', () => reject(signal.reason)); + }), + }); + // AbortSignal.timeout does not keep the process alive by itself; in the app the server does. + const keepAlive = setTimeout(() => {}, 5000); + const started = Date.now(); + const s = await checker.get(); + clearTimeout(keepAlive); + assert.ok(Date.now() - started < 2000); + assert.equal(s.checkFailed, true); +}); + +test('"Check again" cannot be used to hammer GitHub: a minimum interval for the whole instance, and no force during a failure backoff', async () => { + let clock = 20_000_000; + let calls = 0; + let fail = false; + const checker = createUpdateChecker({ + version: '5.2.0', + now: () => clock, + fetchImpl: async () => { + calls++; + return fail + ? new Response('nope', { status: 503 }) + : new Response(JSON.stringify(release()), { status: 200 }); + }, + }); + await checker.get(); + for (let i = 0; i < 10; i++) await checker.get({ force: true }); + assert.equal(calls, 1, 'ten forced checks in the same moment are one request'); + clock += FORCE_MIN_INTERVAL_MS - 1; + await checker.get({ force: true }); + assert.equal(calls, 1); + clock += 1; + fail = true; + await checker.get({ force: true }); + assert.equal(calls, 2, 'allowed after the minimum interval'); + // It failed: the failure backoff is respected, force or not. + clock += FORCE_MIN_INTERVAL_MS * 2; + for (let i = 0; i < 5; i++) assert.equal((await checker.get({ force: true })).checkFailed, true); + assert.equal(calls, 2, 'no forced request while a failure is waited out'); + clock += RETRY_AFTER_FAILURE_MS; + fail = false; + assert.equal((await checker.get({ force: true })).checkFailed, false); + assert.equal(calls, 3); +}); + +test('the body is capped while it streams: a huge answer is cut off, not read to the end', async () => { + let pulled = 0; + const chunk = new TextEncoder().encode('x'.repeat(300_000)); + // eslint-disable-next-line n/no-unsupported-features/node-builtins + const body = new ReadableStream({ + pull(controller) { + pulled++; + if (pulled > 50) return controller.close(); + controller.enqueue(chunk); + }, + }); + const s = await createUpdateChecker({ + version: '5.2.0', + fetchImpl: async () => new Response(body, { status: 200 }), + }).get(); + assert.equal(s.checkFailed, true); + assert.ok(pulled < 10, `read ${pulled} chunks of 50`); +}); diff --git a/test/updateConfig.test.js b/test/updateConfig.test.js new file mode 100644 index 00000000..0ef89092 --- /dev/null +++ b/test/updateConfig.test.js @@ -0,0 +1,61 @@ +// The environment switches of the update notice (src/config.js): UPDATE_CHECK=off turns the check off, and +// the Wise Wren settings are all-or-nothing. Config is read once per process, so each case runs in a child. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { execFile } from 'node:child_process'; +import { promisify } from 'node:util'; + +const READ = ` + const { config } = await import(${JSON.stringify(new URL('../src/config.js', import.meta.url).href)}); + console.log(JSON.stringify({ updateCheck: config.updateCheck, wiseWren: config.wiseWren })); +`; + +async function configWith(extra) { + const env = { + PATH: process.env.PATH, + DISCORD_TOKEN: 'ci-placeholder', + DISCORD_CLIENT_ID: '000000000000000000', + SESSION_SECRET: 'x', + ...extra, + }; + const { stdout, stderr } = await promisify(execFile)( + process.execPath, + ['--input-type=module', '--eval', READ], + { env, timeout: 30_000 } + ); + return { ...JSON.parse(stdout.trim().split('\n').pop()), stderr }; +} + +test('the update check is on unless UPDATE_CHECK says off', async () => { + assert.equal((await configWith({})).updateCheck, true); + assert.equal((await configWith({ UPDATE_CHECK: 'on' })).updateCheck, true); + assert.equal((await configWith({ UPDATE_CHECK: '' })).updateCheck, true); + for (const off of ['off', 'OFF', 'false', '0', 'no']) { + assert.equal((await configWith({ UPDATE_CHECK: off })).updateCheck, false, off); + } +}); + +test('Wise Wren is off without all three settings, and a bad one says which but never the token', async () => { + assert.equal((await configWith({})).wiseWren, null); + const on = await configWith({ + WISE_WREN_URL: 'http://wise-wren:3003/', + WISE_WREN_TOKEN: 'ww_secret_env_token', + WISE_WREN_TARGET_ID: '4', + }); + assert.deepEqual(on.wiseWren, { url: 'http://wise-wren:3003', token: 'ww_secret_env_token', targetId: 4 }); + const partial = await configWith({ + WISE_WREN_URL: 'https://wren.example.com', + WISE_WREN_TOKEN: 'ww_secret_env_token', + }); + assert.equal(partial.wiseWren, null); + assert.match(partial.stderr, /WISE_WREN_TARGET_ID/); + assert.doesNotMatch(partial.stderr, /ww_secret_env_token/); + const bad = await configWith({ + WISE_WREN_URL: 'http://wren.example.com', + WISE_WREN_TOKEN: 'ww_secret_env_token', + WISE_WREN_TARGET_ID: '4', + }); + assert.equal(bad.wiseWren, null); + assert.match(bad.stderr, /WISE_WREN_URL is not usable/); + assert.doesNotMatch(bad.stderr, /ww_secret_env_token/); +}); diff --git a/test/updateService.test.js b/test/updateService.test.js new file mode 100644 index 00000000..77b3430a --- /dev/null +++ b/test/updateService.test.js @@ -0,0 +1,351 @@ +// What the update notice offers (src/web/lib/updateService.js): when "Update now" is possible and why not, +// approvals for exactly the version Wren shows, Wren's refusals in its own words, the run Sylo approved being +// remembered across a restart (the single-target token cannot list runs), open mode, and the Wren read being +// shared and cached. Runs against a fake Wise Wren in single-target-token mode. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { FAKE_TOKEN, startFakeWren } from './helpers/fakeWren.js'; +import { createUpdateChecker } from '../src/lib/updateCheck.js'; +import { WrenError, createWrenClient } from '../src/lib/wiseWren.js'; +import { + OPEN_MODE_TEXT, + UpdateError, + WREN_CACHE_MS, + createUpdateService, + toUpdateError, +} from '../src/web/lib/updateService.js'; + +const wren = await startFakeWren({ targetToken: true }); +test.after(() => wren.close()); + +const RELEASE = { + tag_name: 'v5.3.0', + html_url: 'https://github.com/Ferdinand99/Sylo/releases/tag/v5.3.0', + published_at: '2026-11-20T10:00:00Z', + body: '', +}; + +/** A store like the database one: it outlives a service (a restart makes a new service). */ +const memoryStore = () => { + let value = null; + return { get: async () => value, set: async (run) => void (value = run) }; +}; + +function make({ version = '5.2.0', withWren = true, now, store = memoryStore(), openModeBlocked } = {}) { + const checker = createUpdateChecker({ + version, + fetchImpl: async () => new Response(JSON.stringify(RELEASE), { status: 200 }), + }); + const client = withWren + ? createWrenClient({ url: wren.url, token: FAKE_TOKEN, targetId: wren.targetId }) + : null; + return { service: createUpdateService({ checker, wren: client, now, store, openModeBlocked }), store }; +} + +const posts = () => wren.requests.filter((r) => r.method === 'POST'); +const targetReads = () => + wren.requests.filter((r) => r.url.startsWith('/api/v1/targets/') && r.method === 'GET'); + +const reset = () => { + wren.requests.length = 0; + wren.state.force = {}; + wren.state.delayMs = 0; + wren.state.runs.clear(); + wren.state.targetToken = true; + Object.assign(wren.state.target, { + status: 'update', + effective_mode: 'approve', + blocked_reason: null, + version_key: 'sylo=sha256:4f90abcd', + }); +}; + +test('without Wise Wren: the check only, wren is null, approving is refused', async () => { + reset(); + const { service } = make({ withWren: false }); + const s = await service.status(); + assert.equal(s.available, true); + assert.equal(s.wren, null); + await assert.rejects( + service.approve(), + (e) => e instanceof UpdateError && e.status === 404 && e.code === 'wren_off' + ); + await assert.rejects(service.run(1), (e) => e instanceof UpdateError && e.status === 404); + assert.equal(wren.requests.length, 0); +}); + +test('with Wise Wren: what it sees, that Update now is possible, and no run list is asked for', async () => { + reset(); + const { service } = make(); + const s = await service.status(); + assert.equal(s.wren.configured, true); + assert.equal(s.wren.updateSeen, true); + assert.equal(s.wren.canApprove, true); + assert.equal(s.wren.whyNot, null); + assert.equal(s.wren.openMode, false); + assert.equal(s.wren.target.name, 'sylo'); + assert.equal(s.wren.latestRun, null); + assert.equal(s.wren.error, null); + assert.equal('readable' in s.wren, false); + // The answer never carries what is needed to reach Wren, nor its address, nor the version key. + const text = JSON.stringify(s); + assert.doesNotMatch(text, new RegExp(FAKE_TOKEN)); + assert.doesNotMatch(text, /127\.0\.0\.1/); + assert.doesNotMatch(text, /sha256/); + // Only the target is read: the run list is refused to a single-target token. + assert.ok(wren.requests.every((r) => !r.url.startsWith('/api/v1/runs?'))); +}); + +test('Update now is not offered when Wren sees none, only notifies, or would refuse', async () => { + reset(); + wren.state.target.status = 'current'; + let s = await make().service.status(); + assert.equal(s.wren.canApprove, false); + assert.match(s.wren.whyNot, /does not see an update/); + + reset(); + wren.state.target.effective_mode = 'notify'; + s = await make().service.status(); + assert.equal(s.wren.canApprove, false); + assert.match(s.wren.whyNot, /only notifies/); + + reset(); + wren.state.target.blocked_reason = "not allowed by the agent's [updates] list"; + s = await make().service.status(); + assert.equal(s.wren.canApprove, false); + assert.match(s.wren.whyNot, /would refuse it: not allowed/); +}); + +test('approve sends exactly the version Wren shows and answers with the run and the version it started from', async () => { + reset(); + const { service } = make(); + const out = await service.approve(); + assert.equal(out.from, '5.2.0'); + assert.equal(out.status, 'queued'); + assert.ok(Number.isSafeInteger(out.runId)); + assert.deepEqual(JSON.parse(posts()[0].body), { version_key: 'sylo=sha256:4f90abcd' }); +}); + +test('approve is refused when Wren shows nothing to approve: no approval is sent', async () => { + for (const change of [{ status: 'current' }, { effective_mode: 'notify' }, { blocked_reason: 'nope' }]) { + reset(); + Object.assign(wren.state.target, change); + await assert.rejects( + make().service.approve(), + (e) => e instanceof UpdateError && e.status === 409 && e.code === 'not_approvable' + ); + assert.equal(posts().length, 0); + } +}); + +test('an update that has a status but no version key is not approved blindly', async () => { + reset(); + wren.state.target.version_key = ''; + const { service } = make(); + assert.equal((await service.status()).wren.canApprove, false); + await assert.rejects(service.approve(), (e) => e.status === 409); + assert.equal(posts().length, 0); +}); + +test("Wren's refusals at the moment of approving come back as a 409 with Wren's words", async () => { + // The version changed between Sylo's read and the approval: Wren answers 400. + reset(); + const { service } = make(); + wren.state.force['POST approve'] = [ + 400, + { error: { code: 'bad_request', message: 'The version changed since you looked; approve the new one.' } }, + ]; + await assert.rejects(service.approve(), (e) => { + const mapped = toUpdateError(e); + assert.equal(mapped.status, 409); + assert.equal(mapped.code, 'wren_refused'); + assert.match(mapped.message, /version changed/); + return true; + }); + + // Another person's approval is already running (Sylo does not know that run). + reset(); + const first = make(); + await first.service.approve(); + const second = make(); // a service that has not heard of the run + await assert.rejects(second.service.approve(), (e) => { + const mapped = toUpdateError(e); + assert.equal(mapped.status, 409); + assert.match(mapped.message, /already queued or running/); + return true; + }); + + // The agent would refuse the target: 409 too. + reset(); + wren.state.force['POST approve'] = [ + 409, + { error: { code: 'conflict', message: 'The agent would refuse this update: x.' } }, + ]; + await assert.rejects(make().service.approve(), (e) => toUpdateError(e).status === 409); +}); + +test('the approved run is remembered, also across a restart: Update now stays off while it runs', async () => { + reset(); + const store = memoryStore(); + const before = make({ store }); + const { runId } = await before.service.approve(); + assert.deepEqual(await store.get(), { id: runId }); + + // "Restart": a new service with the same store (the database), and a fresh cache. + const after = make({ store }); + let s = await after.service.status(); + assert.equal(s.wren.latestRun.id, runId); + assert.equal(s.wren.latestRun.finished, false); + assert.equal(s.wren.canApprove, false); + assert.match(s.wren.whyNot, /already running/); + assert.ok( + wren.requests.every((r) => !r.url.startsWith('/api/v1/runs?')), + 'read by id only' + ); + await assert.rejects(after.service.approve(), (e) => e.status === 409); + assert.equal(posts().length, 1, 'no second approval'); + + // Wren finishes it: Update now is possible again. + wren.state.runs.get(runId).status = 'succeeded'; + after.service.drop(); + s = await after.service.status(); + assert.equal(s.wren.latestRun.finished, true); + assert.equal(s.wren.canApprove, true); +}); + +test('a store that fails does not break approving; the run is kept in memory', async () => { + reset(); + const broken = { + get: async () => { + throw new Error('db down'); + }, + set: async () => { + throw new Error('db down'); + }, + }; + const { service } = make({ store: broken }); + const { runId } = await service.approve(); + service.drop(); + assert.equal((await service.status()).wren.latestRun.id, runId); +}); + +test('a 403 on the target (a token for another target) is an error, never an approval without a version', async () => { + reset(); + wren.state.force['GET target'] = [ + 403, + { error: { code: 'forbidden', message: 'This token is not allowed for that target.' } }, + ]; + const { service } = make(); + const s = await service.status(); + assert.equal(s.wren.error.code, 'wren_forbidden'); + assert.match(s.wren.error.message, /not allowed for that target/); + assert.equal(s.wren.canApprove, false); + assert.equal(s.wren.target, null); + await assert.rejects( + service.approve(), + (e) => e instanceof UpdateError && e.status === 502 && e.code === 'wren_forbidden' + ); + assert.equal(posts().length, 0); +}); + +test('Wren being down, or refusing the token, is an error in the answer, not a thrown one', async () => { + reset(); + wren.state.force['GET target'] = [401, { error: { code: 'unauthorized', message: 'Bad token.' } }]; + const { service } = make(); + const s = await service.status(); + assert.equal(s.wren.error.code, 'wren_unauthorized'); + assert.equal(s.wren.canApprove, false); + assert.equal(s.available, true, 'the release check still works'); + await assert.rejects( + service.approve(), + (e) => e instanceof UpdateError && e.status === 502 && e.code === 'wren_unauthorized' + ); + reset(); +}); + +test('open mode: Update now is off before Wren is asked, with a reason; the notice still works', async () => { + reset(); + const { service } = make({ openModeBlocked: () => true }); + const s = await service.status(); + assert.equal(s.available, true); + assert.equal(s.wren.updateSeen, true, 'what Wren sees can still be shown'); + assert.equal(s.wren.openMode, true); + assert.equal(s.wren.canApprove, false); + assert.equal(s.wren.whyNot, OPEN_MODE_TEXT); + assert.match(OPEN_MODE_TEXT, /WISE_WREN_ALLOW_OPEN_MODE=true/); + wren.requests.length = 0; + await assert.rejects( + service.approve(), + (e) => e instanceof UpdateError && e.status === 409 && e.code === 'open_mode' + ); + assert.equal(wren.requests.length, 0, 'Wren is not asked at all'); + + // Allowed (the opt-in) or not open mode: it works. + reset(); + assert.ok((await make({ openModeBlocked: () => false }).service.approve()).runId); +}); + +test('Wise Wren errors map to answers that are never 401 or 403, which would look like a lost session', () => { + const expected = { + unreachable: 502, + unauthorized: 502, + forbidden: 502, + not_found: 404, + conflict: 409, + refused: 409, + rate_limited: 429, + server: 502, + invalid: 502, + bad_request: 502, + }; + for (const [kind, status] of Object.entries(expected)) { + const mapped = toUpdateError(new WrenError(kind, 'm')); + assert.equal(mapped.status, status, kind); + assert.equal(mapped.code, `wren_${kind}`); + assert.notEqual(mapped.status, 401); + assert.notEqual(mapped.status, 403); + } + assert.equal(toUpdateError(new Error('boom')), null); +}); + +test('the Wren read is kept for a few seconds and dropped on a re-check or an approval', async () => { + reset(); + let clock = 1_000; + const { service } = make({ now: () => clock }); + await service.status(); + await service.status(); + assert.equal(targetReads().length, 1); + clock += WREN_CACHE_MS + 1; + await service.status(); + assert.equal(targetReads().length, 2); + await service.status({ force: true }); + assert.equal(targetReads().length, 3); +}); + +test('concurrent reads share one request to Wren, and GitHub and Wren are asked at the same time', async () => { + reset(); + wren.state.delayMs = 150; + const { service } = make(); + const started = Date.now(); + const all = await Promise.all([service.status(), service.status(), service.status(), service.status()]); + assert.equal(targetReads().length, 1, 'one read of the target for four callers'); + assert.ok(all.every((s) => s.wren.canApprove)); + assert.ok(Date.now() - started < 1500); +}); + +test('a run of another target is not readable through Sylo', async () => { + reset(); + wren.state.runs.set(77, { + id: 77, + target: { id: 99, kind: 'container', name: 'other' }, + status: 'running', + steps: [], + }); + await assert.rejects( + make().service.run(77), + (e) => toUpdateError(e)?.status === 502 && toUpdateError(e).code === 'wren_forbidden' + ); + wren.state.targetToken = false; + await assert.rejects(make().service.run(77), (e) => toUpdateError(e)?.status === 404); + wren.state.targetToken = true; +}); diff --git a/test/v2.update.access.test.js b/test/v2.update.access.test.js new file mode 100644 index 00000000..b072c5c3 --- /dev/null +++ b/test/v2.update.access.test.js @@ -0,0 +1,243 @@ +// Who may reach the update notice (src/web/routes/v2Update.js), by kind of caller: an operator's app token +// (no CSRF token needed, by design), a non-operator's app token, people who only have Inbox or View access +// to a server, a Fluxer operator, and open mode (no dashboard login), where "Update now" is refused unless +// WISE_WREN_ALLOW_OPEN_MODE=true. Its own file: the approve route's budget is per account and per process. +import './helpers/trustLoopbackProxy.js'; +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { execFile } from 'node:child_process'; +import { promisify } from 'node:util'; +import { + D_GUILD, + OTHER_D_GUILD, + browser, + profiles, + setProfiles, + signIn, + startOAuthApp, +} from './helpers/oauthApp.js'; +import { OPERATOR_ID } from './helpers/v2Guarded.js'; +import { FAKE_TOKEN, startFakeWren } from './helpers/fakeWren.js'; + +await startOAuthApp(); + +const { runtime } = await import('../src/runtime.js'); +const { findIdentity } = await import('../src/db/accounts.js'); +const { createApiToken } = await import('../src/db/apiTokens.js'); +const { setDashboardAccessRoles } = await import('../src/db/guildSettings.js'); +const { setUpdateService } = await import('../src/web/routes/v2Update.js'); +const { createUpdateService } = await import('../src/web/lib/updateService.js'); +const { createUpdateChecker } = await import('../src/lib/updateCheck.js'); +const { createWrenClient } = await import('../src/lib/wiseWren.js'); + +const wren = await startFakeWren({ targetToken: true }); +test.after(() => wren.close()); + +const RELEASE = { + tag_name: 'v99.0.0', + html_url: 'https://github.com/Ferdinand99/Sylo/releases/tag/v99.0.0', + published_at: '2026-11-20T10:00:00Z', + body: '', +}; + +function useService() { + wren.state.runs.clear(); + wren.requests.length = 0; + let remembered = null; + setUpdateService( + createUpdateService({ + checker: createUpdateChecker({ + version: '5.2.0', + fetchImpl: async () => new Response(JSON.stringify(RELEASE), { status: 200 }), + }), + wren: createWrenClient({ url: wren.url, token: FAKE_TOKEN, targetId: wren.targetId }), + store: { get: async () => remembered, set: async (run) => void (remembered = run) }, + }) + ); +} + +const VIEW = '700000000000000511'; +const INBOX = '700000000000000512'; +const dGuild = runtime.clients.discord.guilds.cache.get(D_GUILD); + +function fakeMember(id, roles) { + const cache = new Map(roles.map((r) => [r, { id: r }])); + cache.hasAny = (...ids) => ids.some((r) => cache.has(r)); + cache.some = (fn) => [...cache.values()].some(fn); + return { id, permissions: { has: () => false }, roles: { highest: { position: 5 }, cache } }; +} + +let clients = 0; +const json = { accept: 'application/json', 'content-type': 'application/json' }; + +/** + * Signs in on `platform` as the account with this id and gives the JSON helpers for the web session + * (with its CSRF token) and for an app token of the same account (no CSRF token). Each person + * sends requests from an address of their own. + */ +async function person({ platform = 'discord', id, roles = [], guilds } = {}) { + setProfiles(platform === 'discord' ? { discordId: id } : { fluxerId: id }); + if (guilds) profiles[platform].guilds = guilds; + const b = browser(); + const ip = `10.8.${Math.floor(++clients / 250)}.${clients % 250}`; + const send = (path, init = {}) => + b.get(path, { ...init, headers: { ...init.headers, 'x-forwarded-for': ip } }); + await signIn(b, platform); + const userId = profiles[platform].id; + if (platform === 'discord') dGuild.members.cache.set(userId, fakeMember(userId, roles)); + const csrf = (await (await send('/api/v2/csrf', { headers: { accept: 'application/json' } })).json()).token; + const { accountId } = await findIdentity(platform, userId); + const { token } = await createApiToken(accountId, { deviceName: 'iPhone', ttlMs: 60_000 }); + const auth = { authorization: `Bearer ${token}` }; + return { + web: { + get: (p) => send(`/api/v2${p}`, { headers: { accept: 'application/json' } }), + post: (p, body = {}) => + send(`/api/v2${p}`, { + method: 'POST', + headers: { ...json, 'x-csrf-token': csrf }, + body: JSON.stringify(body), + }), + }, + app: { + get: (p) => send(`/api/v2${p}`, { headers: { ...auth, accept: 'application/json' } }), + post: (p, body = {}) => + send(`/api/v2${p}`, { method: 'POST', headers: { ...json, ...auth }, body: JSON.stringify(body) }), + }, + }; +} + +const ROUTES = [ + ['get', '/update'], + ['get', '/update/wren/runs/12'], + ['post', '/update/wren/approve'], +]; + +async function assertRefused(caller, label) { + for (const [method, path] of ROUTES) { + for (const [how, client] of [ + ['web', caller.web], + ['app', caller.app], + ]) { + const res = await client[method](path); + assert.equal(res.status, 403, `${label} ${how} ${method} ${path}`); + assert.match((await res.json()).error, /operators/); + } + } +} + +test("an operator's app token reads the notice and updates; a POST needs no CSRF token with a bearer token", async () => { + useService(); + const op = await person({ id: OPERATOR_ID }); + const read = await op.app.get('/update'); + assert.equal(read.status, 200); + const status = await read.json(); + assert.equal(status.available, true); + assert.equal(status.wren.canApprove, true); + + const approve = await op.app.post('/update/wren/approve'); + assert.equal(approve.status, 200); + const { runId } = await approve.json(); + assert.ok(Number.isSafeInteger(runId)); + assert.equal((await op.app.get(`/update/wren/runs/${runId}`)).status, 200); +}); + +test('an app token of an account that is not an operator is refused every route, also a POST', async () => { + useService(); + const admin = await person({ + id: '333333333333330101', + guilds: [{ id: D_GUILD, owner: true }], + }); + await assertRefused(admin, 'server admin'); + const nobody = await person({ id: '333333333333330102', guilds: [] }); + await assertRefused(nobody, 'nobody'); + assert.equal(wren.requests.length, 0, 'Wren was not asked for any of them'); +}); + +test('people with only Inbox or View access to a server are not operators', async () => { + useService(); + await setDashboardAccessRoles(D_GUILD, { inbox: [INBOX], view: [VIEW] }); + const guilds = [ + { id: D_GUILD, owner: false, permissions: '0' }, + { id: OTHER_D_GUILD, owner: false, permissions: '0' }, + ]; + const inbox = await person({ id: '333333333333330103', roles: [INBOX], guilds }); + const view = await person({ id: '333333333333330104', roles: [VIEW], guilds }); + // They do have dashboard access (a sanity check that the roles work), but not to this. + assert.equal((await inbox.web.get(`/guilds/${D_GUILD}/tickets`)).status, 200); + assert.equal((await view.web.get(`/guilds/${D_GUILD}/overview`)).status, 200); + await assertRefused(inbox, 'Inbox'); + await assertRefused(view, 'View'); + assert.equal(wren.requests.length, 0); + dGuild.members.cache.clear(); +}); + +test('a Fluxer operator (FLUXER_OWNER_IDS) is an operator too', async () => { + useService(); + const op = await person({ platform: 'fluxer', id: '555555555555555555' }); + assert.equal((await op.web.get('/update')).status, 200); + assert.equal((await op.app.get('/update')).status, 200); +}); + +// Config is read once per process, so open mode (no OAuth configured) runs in a child process. +const OPEN_MODE = ` + await import(${JSON.stringify(new URL('./helpers/tmpDb.js', import.meta.url).href)}); + const { createApp } = await import(${JSON.stringify(new URL('../src/web/server.js', import.meta.url).href)}); + const server = await new Promise((resolve) => { const s = createApp().listen(0, () => resolve(s)); }); + const base = 'http://127.0.0.1:' + server.address().port; + const get = await fetch(base + '/api/v2/update', { headers: { accept: 'application/json' } }); + const status = await get.json(); + const post = await fetch(base + '/api/v2/update/wren/approve', { method: 'POST', headers: { accept: 'application/json', 'content-type': 'application/json' }, body: '{}' }); + console.log(JSON.stringify({ getStatus: get.status, status, postStatus: post.status, post: await post.json() })); + process.exit(0); +`; + +async function openMode(extra = {}) { + wren.state.runs.clear(); + wren.requests.length = 0; + const env = { + ...process.env, + DISCORD_CLIENT_SECRET: '', + FLUXER_CLIENT_SECRET: '', + FLUXER_TOKEN: '', + FLUXER_CLIENT_ID: '', + DASHBOARD_URL: '', + UPDATE_CHECK: 'off', + WISE_WREN_URL: wren.url, + WISE_WREN_TOKEN: FAKE_TOKEN, + WISE_WREN_TARGET_ID: String(wren.targetId), + ...extra, + }; + const { stdout, stderr } = await promisify(execFile)( + process.execPath, + ['--input-type=module', '--eval', OPEN_MODE], + { env, timeout: 30_000 } + ); + return { ...JSON.parse(stdout.trim().split('\n').pop()), stderr }; +} + +test('open mode: Update now is refused with a 409 (and is not offered) unless WISE_WREN_ALLOW_OPEN_MODE=true', async () => { + const refused = await openMode(); + assert.equal(refused.getStatus, 200, 'the notice and what Wren sees are still shown'); + assert.equal(refused.status.wren.updateSeen, true); + assert.equal(refused.status.wren.openMode, true); + assert.equal(refused.status.wren.canApprove, false); + assert.match(refused.status.wren.whyNot, /WISE_WREN_ALLOW_OPEN_MODE=true/); + assert.equal(refused.postStatus, 409); + assert.equal(refused.post.code, 'open_mode'); + assert.equal( + wren.requests.filter((r) => r.method === 'POST').length, + 0, + 'Wren was not asked to approve anything' + ); + assert.match(refused.stderr, /Wise Wren is set up, but the dashboard has no login/); + assert.doesNotMatch(refused.stderr, new RegExp(FAKE_TOKEN)); + + const allowed = await openMode({ WISE_WREN_ALLOW_OPEN_MODE: 'true' }); + assert.equal(allowed.status.wren.openMode, false); + assert.equal(allowed.status.wren.canApprove, true); + assert.equal(allowed.postStatus, 200); + assert.ok(Number.isSafeInteger(allowed.post.runId)); + assert.equal(wren.requests.filter((r) => r.method === 'POST').length, 1); + assert.doesNotMatch(allowed.stderr, /has no login/, 'no warning once it is allowed on purpose'); +}); diff --git a/test/v2.update.test.js b/test/v2.update.test.js new file mode 100644 index 00000000..1e3488b1 --- /dev/null +++ b/test/v2.update.test.js @@ -0,0 +1,268 @@ +// The operators' update notice through the real app (src/web/routes/v2Update.js): only Sylo's operators may +// read it or use "Update now" (a server admin and a signed-out visitor are refused), writes need the CSRF +// token, the approve route has its own small budget, and the Wise Wren token is in no answer and no log line. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { D_GUILD, browser, startOAuthApp } from './helpers/oauthApp.js'; +import { anonymous, session, OPERATOR_ID } from './helpers/v2Guarded.js'; +import { FAKE_TOKEN, startFakeWren } from './helpers/fakeWren.js'; + +await startOAuthApp(); + +const { setUpdateService } = await import('../src/web/routes/v2Update.js'); +const { createUpdateService } = await import('../src/web/lib/updateService.js'); +const { createUpdateChecker } = await import('../src/lib/updateCheck.js'); +const { createWrenClient } = await import('../src/lib/wiseWren.js'); +const { listBotAudit } = await import('../src/db/botAudit.js'); + +// The recommended token: limited to the one target. +const wren = await startFakeWren({ targetToken: true }); +test.after(() => wren.close()); + +const RELEASE = { + tag_name: 'v99.0.0', + html_url: 'https://github.com/Ferdinand99/Sylo/releases/tag/v99.0.0', + published_at: '2026-11-20T10:00:00Z', + body: '## [99.0.0](https://github.com/Ferdinand99/Sylo/compare/v5.2.0...v99.0.0) (2026-11-20)\n\n### Features\n\n* a thing ([#1](https://github.com/Ferdinand99/Sylo/issues/1))\n', +}; + +let githubCalls = 0; +function useService({ withWren = true, openModeBlocked = () => false } = {}) { + githubCalls = 0; + wren.state.runs.clear(); + wren.state.force = {}; + Object.assign(wren.state.target, { + status: 'update', + effective_mode: 'approve', + blocked_reason: null, + version_key: 'sylo=sha256:4f90abcd', + }); + let remembered = null; + const checker = createUpdateChecker({ + version: '5.2.0', + fetchImpl: async () => { + githubCalls++; + return new Response(JSON.stringify(RELEASE), { status: 200 }); + }, + }); + setUpdateService( + createUpdateService({ + checker, + wren: withWren ? createWrenClient({ url: wren.url, token: FAKE_TOKEN, targetId: wren.targetId }) : null, + store: { get: async () => remembered, set: async (run) => void (remembered = run) }, + openModeBlocked, + }) + ); +} + +// Everything the app logs or prints while the tests run, to prove the token stays out of it. +const output = []; +for (const name of ['log', 'error', 'warn', 'info']) { + const original = console[name]; + console[name] = (...args) => { + output.push(args.map(String).join(' ')); + original.apply(console, args); + }; +} + +const operator = await session({ discordId: OPERATOR_ID }); +const admin = await session({ discordId: '333333333333330077', guilds: [{ id: D_GUILD, owner: true }] }); +const signedOut = anonymous(); + +const ROUTES = [ + ['get', '/update'], + ['get', '/update/wren/runs/12'], + ['post', '/update/wren/approve'], +]; + +test('a server admin and a signed-out visitor are refused every update route', async () => { + useService(); + for (const [method, path] of ROUTES) { + const res = await admin[method](path); + assert.equal(res.status, 403, `admin ${method} ${path}`); + assert.match((await res.json()).error, /operators/); + const out = await signedOut[method](path); + // A POST without a session fails the CSRF check before anything else; both are a refusal. + assert.equal(out.status, method === 'post' ? 403 : 401, `anonymous ${method} ${path}`); + } + assert.equal(githubCalls, 0, 'nothing was asked on their behalf'); + assert.equal(wren.requests.length, 0); +}); + +test('an operator reads the notice: a newer release, its notes, the command, and what Wise Wren sees', async () => { + useService(); + wren.requests.length = 0; + const res = await operator.get('/update'); + assert.equal(res.status, 200); + assert.equal(res.headers.get('cache-control'), 'no-store'); + const body = await res.json(); + assert.equal(body.enabled, true); + assert.equal(body.available, true); + assert.equal(body.latest, '99.0.0'); + assert.equal(body.command, 'docker compose pull && docker compose up -d'); + assert.equal(body.notes.sections[0].items[0].text, 'A thing'); + assert.equal(body.wren.updateSeen, true); + assert.equal(body.wren.canApprove, true); + const text = JSON.stringify(body); + assert.doesNotMatch(text, new RegExp(FAKE_TOKEN)); + assert.doesNotMatch(text, /127\.0\.0\.1/); +}); + +test('without Wise Wren the notice still works and approving is a 404', async () => { + useService({ withWren: false }); + const body = await (await operator.get('/update')).json(); + assert.equal(body.wren, null); + assert.equal(body.available, true); + const res = await operator.post('/update/wren/approve'); + assert.equal(res.status, 404); + assert.equal((await res.json()).code, 'wren_off'); +}); + +test('?refresh=1 is a request to look again, limited per operator and by a minimum interval for the instance', async () => { + useService({ withWren: false }); + await operator.get('/update'); + await operator.get('/update'); + assert.equal(githubCalls, 1, 'the cache answers the second read'); + assert.equal((await operator.get('/update?refresh=1')).status, 200); + assert.equal(githubCalls, 1, 'GitHub was asked a moment ago: the minimum interval serves the cache'); + let limited = 0; + for (let i = 0; i < 8; i++) if ((await operator.get('/update?refresh=1')).status === 429) limited++; + assert.ok(limited >= 1, 'too many re-checks are refused'); +}); + +test('a write needs the CSRF token, like every other operator write', async () => { + useService(); + const b = browser(); + const { signIn, setProfiles } = await import('./helpers/oauthApp.js'); + setProfiles({ discordId: OPERATOR_ID }); + await signIn(b, 'discord'); + const res = await b.get('/api/v2/update/wren/approve', { + method: 'POST', + headers: { accept: 'application/json', 'content-type': 'application/json' }, + body: '{}', + }); + assert.equal(res.status, 403); + assert.match((await res.json()).error, /CSRF/i); + assert.equal(wren.requests.filter((r) => r.method === 'POST').length, 0); +}); + +test('Update now: approves the version Wren shows, records who asked, and the run can be followed', async () => { + useService(); + wren.requests.length = 0; + const res = await operator.post('/update/wren/approve'); + assert.equal(res.status, 200); + const started = await res.json(); + assert.deepEqual(Object.keys(started).sort(), ['from', 'runId', 'status']); + assert.equal(started.from, '5.2.0'); + assert.equal(started.status, 'queued'); + assert.deepEqual(JSON.parse(wren.requests.find((r) => r.method === 'POST').body), { + version_key: 'sylo=sha256:4f90abcd', + }); + + const audit = (await listBotAudit(5)).find((a) => a.action === 'update:approve'); + assert.ok(audit, 'an audit entry'); + assert.match(audit.detail, new RegExp(`run ${started.runId}`)); + assert.doesNotMatch(JSON.stringify(audit), new RegExp(FAKE_TOKEN)); + + const run = wren.state.runs.get(started.runId); + run.status = 'running'; + run.steps = [{ step: 'stop', status: 'running', duration_ms: null, log: 'secret path /mnt/user/appdata' }]; + const progress = await (await operator.get(`/update/wren/runs/${started.runId}`)).json(); + assert.deepEqual(Object.keys(progress), ['run']); + assert.equal(progress.run.status, 'running'); + assert.equal(progress.run.finished, false); + assert.deepEqual(progress.run.steps, [{ step: 'stop', status: 'running', durationMs: null }]); + assert.doesNotMatch(JSON.stringify(progress), /secret path/); + + // The page can still follow it when Sylo is back: the status names the active run too. + const status = await (await operator.get('/update')).json(); + assert.equal(status.wren.latestRun.id, started.runId); + assert.equal(status.wren.canApprove, false); +}); + +test("a run that is not this target's is refused by Wren (a 502 here), an unknown run and a junk id are 404s", async () => { + useService(); + wren.state.runs.set(500, { + id: 500, + target: { id: 99, kind: 'container', name: 'other' }, + status: 'running', + steps: [], + }); + // A single-target token is refused another target's run (403 in Wren, never a 403 for the operator). + const other = await operator.get('/update/wren/runs/500'); + assert.equal(other.status, 502); + assert.equal((await other.json()).code, 'wren_forbidden'); + assert.equal((await operator.get('/update/wren/runs/424242')).status, 404); + assert.equal((await operator.get('/update/wren/runs/abc')).status, 404); + assert.equal((await operator.get('/update/wren/runs/1;rm')).status, 404); + assert.equal((await operator.get('/update/wren/runs/0')).status, 404); + // Only numeric ids ever reach Wren. + assert.ok( + wren.requests + .filter((r) => r.url.startsWith('/api/v1/runs/')) + .every((r) => /^\/api\/v1\/runs\/\d+$/.test(r.url)) + ); +}); + +test('Wise Wren refusing is shown as its words with a 409; a wrong token is a 502, never a 401', async () => { + // Sylo already knows it would be refused (the target only notifies): a 409 before Wren is asked. + useService(); + wren.state.target.effective_mode = 'notify'; + let res = await operator.post('/update/wren/approve'); + assert.equal(res.status, 409); + assert.match((await res.json()).error, /only notifies/); + + // Wren refuses at the moment of approving (the version changed): a 400 in Wren, a 409 with its words here. + useService(); + wren.state.force['POST approve'] = [ + 400, + { error: { code: 'bad_request', message: 'The version changed since you looked; approve the new one.' } }, + ]; + res = await operator.post('/update/wren/approve'); + assert.equal(res.status, 409); + const refused = await res.json(); + assert.equal(refused.code, 'wren_refused'); + assert.match(refused.error, /version changed/); + wren.state.force = {}; + + setUpdateService( + createUpdateService({ + checker: createUpdateChecker({ + version: '5.2.0', + fetchImpl: async () => new Response(JSON.stringify(RELEASE)), + }), + wren: createWrenClient({ url: wren.url, token: 'ww_not_the_right_token', targetId: wren.targetId }), + }) + ); + res = await operator.post('/update/wren/approve'); + assert.equal(res.status, 502); + const body = await res.json(); + assert.equal(body.code, 'wren_unauthorized'); + assert.doesNotMatch(JSON.stringify(body), /ww_not_the_right_token/); + useService(); +}); + +test('the approve route has a small budget: the sixth request in a minute is refused', async () => { + useService(); + wren.state.runs.clear(); + // Earlier tests used some of the budget; keep going until it is used up, which must happen within five. + let first429 = -1; + for (let i = 0; i < 6; i++) { + const res = await operator.post('/update/wren/approve'); + if (res.status === 429) { + first429 = i; + break; + } + // A run is waiting after the first approval, so the next ones are refused as conflicts (409): they + // still count against the budget. + assert.ok([200, 409].includes(res.status), `status ${res.status}`); + } + assert.ok(first429 >= 0 && first429 <= 5, 'rate limited'); +}); + +test('the Wise Wren token is in no log line the app wrote', () => { + for (const line of output) { + assert.ok(!line.includes(FAKE_TOKEN), 'token found in a log line'); + assert.ok(!line.includes('ww_not_the_right_token'), 'wrong token found in a log line'); + } +}); diff --git a/test/wiseWren.test.js b/test/wiseWren.test.js new file mode 100644 index 00000000..9941be86 --- /dev/null +++ b/test/wiseWren.test.js @@ -0,0 +1,404 @@ +// The Wise Wren client (src/lib/wiseWren.js) against a fake Wise Wren: reading the target, approving, a run's +// progress, every kind of error, and that the token only ever goes in the Authorization header of a request +// to the configured address (never in an error, a redirect or an answer). +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { createServer } from 'node:http'; +import { FAKE_TOKEN, startFakeWren } from './helpers/fakeWren.js'; +import { + WrenError, + checkWrenUrl, + createWrenClient, + readWrenSettings, + shapeRun, +} from '../src/lib/wiseWren.js'; + +// The recommended token: limited to this one target (the run list and other targets answer 403). +const wren = await startFakeWren({ targetToken: true }); +test.after(() => wren.close()); + +const settings = { url: wren.url, token: FAKE_TOKEN, targetId: wren.targetId }; +const client = () => createWrenClient(settings); +const reset = () => { + wren.requests.length = 0; + wren.state.force = {}; + wren.state.delayMs = 0; + wren.state.runs.clear(); + Object.assign(wren.state.target, { + status: 'update', + effective_mode: 'approve', + blocked_reason: null, + version_key: 'sylo=sha256:4f90abcd', + }); +}; +const KEY = 'sylo=sha256:4f90abcd'; + +async function failure(promise) { + try { + await promise; + } catch (err) { + assert.ok(err instanceof WrenError, `expected a WrenError, got ${err}`); + return err; + } + assert.fail('expected a failure'); +} + +test('settings: all three or none; the address must be https or a local http', () => { + assert.deepEqual(readWrenSettings({}), { settings: null, problem: null }); + assert.deepEqual(readWrenSettings({ WISE_WREN_URL: ' ', WISE_WREN_TOKEN: '' }), { + settings: null, + problem: null, + }); + const ok = readWrenSettings({ + WISE_WREN_URL: 'https://wren.example.com/', + WISE_WREN_TOKEN: ' ww_abc ', + WISE_WREN_TARGET_ID: '12', + }); + assert.deepEqual(ok.settings, { url: 'https://wren.example.com', token: 'ww_abc', targetId: 12 }); + // Some missing: off, with a reason that names the variables, never a value. + const partial = readWrenSettings({ WISE_WREN_URL: 'https://wren.example.com', WISE_WREN_TOKEN: 'ww_abc' }); + assert.equal(partial.settings, null); + assert.match(partial.problem, /WISE_WREN_TARGET_ID/); + assert.doesNotMatch(partial.problem, /ww_abc/); + for (const id of ['0', '-1', '1.5', 'abc', '1e3', '99999999999']) { + const r = readWrenSettings({ + WISE_WREN_URL: 'https://w.example.com', + WISE_WREN_TOKEN: 't', + WISE_WREN_TARGET_ID: id, + }); + assert.equal(r.settings, null, id); + assert.match(r.problem, /TARGET_ID/); + } + const bad = readWrenSettings({ + WISE_WREN_URL: 'http://wren.example.com', + WISE_WREN_TOKEN: 'ww_secret_value', + WISE_WREN_TARGET_ID: '1', + }); + assert.equal(bad.settings, null); + assert.doesNotMatch(bad.problem, /ww_secret_value/); +}); + +test('the address: https anywhere, http only on this machine or the local network, no credentials', () => { + for (const u of [ + 'https://wren.example.com', + 'http://localhost:3003', + 'http://127.0.0.1:3003/', + 'http://10.0.3.152:3003', + 'http://192.168.1.5', + 'http://wise-wren:3003', + 'http://wren.lan', + 'http://tower.ts.net', + 'http://[::1]:3003', + ]) { + assert.doesNotThrow(() => checkWrenUrl(u), u); + } + assert.equal(checkWrenUrl('https://wren.example.com/base/'), 'https://wren.example.com/base'); + for (const u of [ + 'http://wren.example.com', + 'http://8.8.8.8', + 'ftp://wren.local', + 'https://user:pass@wren.example.com', + 'https://wren.example.com/?x=1', + 'https://wren.example.com/#x', + 'not a url', + '', + ]) { + assert.throws(() => checkWrenUrl(u), Error, u); + } +}); + +test('the target: what Wren sees, shaped, with the bearer token and no body', async () => { + reset(); + const t = await client().target(); + assert.equal(t.status, 'update'); + assert.equal(t.name, 'sylo'); + assert.equal(t.server, 'LANBox'); + assert.equal(t.versionKey, 'sylo=sha256:4f90abcd'); + assert.equal(t.mode, 'approve'); + assert.equal(t.blockedReason, null); + assert.equal(wren.requests.length, 1); + const [req] = wren.requests; + assert.equal(req.method, 'GET'); + assert.equal(req.url, `/api/v1/targets/${wren.targetId}`); + assert.equal(req.headers.authorization, `Bearer ${FAKE_TOKEN}`); + assert.equal(req.body, ''); +}); + +test('approve: the exact version key, then the run id; an approval without a key is never sent', async () => { + reset(); + const started = await client().approve(KEY); + assert.deepEqual(started, { runId: started.runId, status: 'queued' }); + assert.ok(Number.isSafeInteger(started.runId)); + const [req] = wren.requests; + assert.equal(req.method, 'POST'); + assert.equal(req.url, `/api/v1/targets/${wren.targetId}/approve`); + assert.equal(req.headers['content-type'], 'application/json'); + assert.deepEqual(JSON.parse(req.body), { version_key: KEY }); + + reset(); + for (const missing of [null, undefined, '']) { + const err = await failure(client().approve(missing)); + assert.equal(err.kind, 'refused'); + } + assert.equal(wren.requests.length, 0, 'nothing asked of Wren without a version key'); +}); + +test("approve: Wren's refusals (400 and 409) are `refused`, with Wren's own words", async () => { + // The version changed since Sylo looked: a 400 in the real Wren. + reset(); + let err = await failure(client().approve('sylo=sha256:old')); + assert.equal(err.kind, 'refused'); + assert.equal(err.status, 400); + assert.match(err.message, /version changed/); + + // A second approval while a run is queued or running. + reset(); + await client().approve(KEY); + err = await failure(client().approve(KEY)); + assert.equal(err.kind, 'refused'); + assert.match(err.message, /already queued or running/); + + // Notify-only and no update are 400s too; an agent that would refuse the target is a 409. + reset(); + wren.state.target.effective_mode = 'notify'; + assert.match((await failure(client().approve(KEY))).message, /notify-only/); + reset(); + wren.state.target.status = 'current'; + assert.match((await failure(client().approve(KEY))).message, /no update/); + reset(); + wren.state.target.blocked_reason = 'not allowed by its [updates] list'; + err = await failure(client().approve(KEY)); + assert.equal(err.kind, 'refused'); + assert.equal(err.status, 409); + assert.match(err.message, /agent would refuse this update: not allowed/); +}); + +test('a run: steps without logs, only a run of this target', async () => { + reset(); + const { runId } = await client().approve(KEY); + const run = wren.state.runs.get(runId); + run.status = 'running'; + run.steps = [ + { step: 'preflight', status: 'ok', duration_ms: 812, log: 'pulled ghcr.io/x/y@sha256:secret-ish' }, + { step: 'stop', status: 'running', duration_ms: null, log: '/mnt/user/appdata/sylo' }, + ]; + const r = await client().run(runId); + assert.equal(r.status, 'running'); + assert.equal(r.finished, false); + assert.equal(r.ok, false); + assert.deepEqual(r.steps, [ + { step: 'preflight', status: 'ok', durationMs: 812 }, + { step: 'stop', status: 'running', durationMs: null }, + ]); + assert.doesNotMatch(JSON.stringify(r), /sha256|appdata/, 'step logs stay in Wise Wren'); + + run.status = 'succeeded'; + run.finished_at = '2026-11-16 10:05:00'; + const done = await client().run(runId); + assert.equal(done.finished, true); + assert.equal(done.ok, true); + + // A run of another target is "no such run", even though Wren has it. + wren.state.runs.set(99, { + id: 99, + target: { id: 8, kind: 'container', name: 'other' }, + status: 'succeeded', + steps: [], + }); + // (A plain write token can read it; a target token is refused it by Wren, see below.) + wren.state.targetToken = false; + assert.equal((await failure(client().run(99))).kind, 'not_found'); + wren.state.targetToken = true; + assert.equal((await failure(client().run(0))).kind, 'not_found'); + assert.equal((await failure(client().run('7; drop'))).kind, 'not_found'); +}); + +test('a single-target token: the run list and other targets are refused, and Sylo never asks for them', async () => { + reset(); + // What Wren does for such a token (so a client that asked would fail): + const direct = (path) => + fetch(`${wren.url}${path}`, { headers: { authorization: `Bearer ${FAKE_TOKEN}` } }); + assert.equal((await direct(`/api/v1/runs?target_id=${wren.targetId}&limit=1`)).status, 403); + assert.equal((await direct('/api/v1/targets/99')).status, 403); + wren.requests.length = 0; + const c = client(); + await c.target(); + const { runId } = await c.approve(KEY); + await c.run(runId); + assert.ok( + wren.requests.every((r) => !r.url.startsWith('/api/v1/runs?')), + 'no run list' + ); + // A run of another target: the token is refused it (403), and Sylo would not show it anyway. + wren.state.runs.set(99, { + id: 99, + target: { id: 8, kind: 'container', name: 'other' }, + status: 'running', + steps: [], + }); + assert.equal((await failure(c.run(99))).kind, 'forbidden'); +}); + +test('terminal states are finished; only succeeded is ok', () => { + for (const status of [ + 'succeeded', + 'rolled_back', + 'failed', + 'rollback_failed', + 'refused', + 'cancelled', + 'lapsed', + ]) { + assert.equal(shapeRun({ id: 1, status }).finished, true, status); + } + for (const status of ['queued', 'running', 'whatever']) + assert.equal(shapeRun({ id: 1, status }).finished, false, status); + assert.equal(shapeRun({ id: 1, status: 'rolled_back' }).ok, false); + assert.equal(shapeRun({ status: 'x' }), null); +}); + +test('errors by status: unauthorized, forbidden, not found, rate limited, server, with safe messages', async () => { + for (const [status, kind] of [ + [401, 'unauthorized'], + [403, 'forbidden'], + [404, 'not_found'], + [400, 'bad_request'], + [409, 'conflict'], + [429, 'rate_limited'], + [500, 'server'], + [502, 'server'], + ]) { + reset(); + wren.state.force['GET target'] = [status, {}]; + const err = await failure(client().target()); + assert.equal(err.kind, kind, String(status)); + assert.equal(err.status, status); + assert.ok(err.message.length > 5); + assert.doesNotMatch(err.message, new RegExp(FAKE_TOKEN)); + assert.doesNotMatch(err.message, /127\.0\.0\.1/); + } + // Wren's own short error text is kept (its envelope is {"error": {"code", "message"}}), a long or missing + // one is replaced, and a plain string error is read too. + reset(); + wren.state.force['GET target'] = [ + 403, + { error: { code: 'forbidden', message: 'This token is for another target.' } }, + ]; + assert.match((await failure(client().target())).message, /for another target/); + wren.state.force['GET target'] = [500, { error: { code: 'x', message: 'x'.repeat(500) } }]; + assert.doesNotMatch((await failure(client().target())).message, /xxxxx/); + wren.state.force['GET target'] = [500, { error: 'Plain text error' }]; + assert.match((await failure(client().target())).message, /Plain text error/); + reset(); +}); + +test('a wrong token is a 401 from Wren: unauthorized, and the message does not echo it', async () => { + reset(); + const err = await failure(createWrenClient({ ...settings, token: 'ww_wrong_token_value' }).target()); + assert.equal(err.kind, 'unauthorized'); + assert.doesNotMatch(err.message, /ww_wrong_token_value/); +}); + +test('an answer that is not JSON, or not an object, is invalid; a missing run id too', async () => { + reset(); + wren.state.force['GET target'] = [200, 'not an object']; + assert.equal((await failure(client().target())).kind, 'invalid'); + wren.state.force['POST approve'] = [200, { status: 'queued' }]; + assert.equal((await failure(client().approve(KEY))).kind, 'invalid'); + reset(); +}); + +test('unreachable and too slow are their own kinds, and the error never carries the address or token', async () => { + const dead = await startFakeWren(); + const url = dead.url; + dead.close(); + const err = await failure(createWrenClient({ ...settings, url }).target()); + assert.equal(err.kind, 'unreachable'); + assert.doesNotMatch(err.message, /127\.0\.0\.1/); + assert.doesNotMatch(err.message, new RegExp(FAKE_TOKEN)); + + reset(); + wren.state.delayMs = 300; + const slow = await failure(createWrenClient(settings, { readTimeoutMs: 50 }).target()); + assert.equal(slow.kind, 'unreachable'); + assert.match(slow.message, /in time/); + reset(); +}); + +test('a redirect is not followed, so the token never reaches another address', async () => { + const seen = []; + const elsewhere = createServer((req, res) => { + seen.push(req.headers.authorization ?? null); + res.end('{}'); + }); + await new Promise((r) => elsewhere.listen(0, '127.0.0.1', r)); + const target = `http://127.0.0.1:${elsewhere.address().port}/stolen`; + const redirecting = createServer((_req, res) => { + res.writeHead(302, { location: target }); + res.end(); + }); + await new Promise((r) => redirecting.listen(0, '127.0.0.1', r)); + try { + const url = `http://127.0.0.1:${redirecting.address().port}`; + const err = await failure(createWrenClient({ ...settings, url }).approve(KEY)); + assert.equal(err.kind, 'server'); + assert.match(err.message, /redirect/); + assert.deepEqual(seen, [], 'nothing was sent to the redirect target'); + } finally { + elsewhere.closeAllConnections?.(); + redirecting.closeAllConnections?.(); + elsewhere.close(); + redirecting.close(); + } +}); + +test('only the three routes on the one target are ever called', async () => { + reset(); + const c = client(); + await c.target(); + const { runId } = await c.approve(KEY); + await c.run(runId); + for (const r of wren.requests) { + assert.match( + `${r.method} ${r.url}`, + new RegExp( + `^(GET /api/v1/targets/${wren.targetId}|POST /api/v1/targets/${wren.targetId}/approve|GET /api/v1/runs/\\d+)$` + ) + ); + assert.equal(r.headers.authorization, `Bearer ${FAKE_TOKEN}`); + } +}); + +test('an oversized answer is cut off while it streams, not buffered whole', async () => { + let pulled = 0; + const chunk = new TextEncoder().encode('x'.repeat(300_000)); + // eslint-disable-next-line n/no-unsupported-features/node-builtins + const body = new ReadableStream({ + pull(controller) { + pulled++; + if (pulled > 50) return controller.close(); + controller.enqueue(chunk); + }, + }); + const c = createWrenClient(settings, { fetchImpl: async () => new Response(body, { status: 200 }) }); + const err = await failure(c.target()); + assert.equal(err.kind, 'invalid'); + assert.ok(pulled < 10, `read ${pulled} chunks of 50`); +}); + +test('an answer that declares more than the cap is refused before it is read', async () => { + let pulled = 0; + // eslint-disable-next-line n/no-unsupported-features/node-builtins + const body = new ReadableStream({ + pull(controller) { + pulled++; + controller.enqueue(new TextEncoder().encode('{}')); + }, + }); + const c = createWrenClient(settings, { + fetchImpl: async () => new Response(body, { status: 200, headers: { 'content-length': '5000000' } }), + }); + const err = await failure(c.target()); + assert.equal(err.kind, 'invalid'); + assert.ok(pulled <= 1, `pulled ${pulled} chunks`); +}); diff --git a/web-v2/src/__tests__/Health.test.jsx b/web-v2/src/__tests__/Health.test.jsx index 96f35198..bf2ec678 100644 --- a/web-v2/src/__tests__/Health.test.jsx +++ b/web-v2/src/__tests__/Health.test.jsx @@ -11,6 +11,9 @@ vi.mock('../api.js', () => ({ restoreBackup: vi.fn(), importBackup: vi.fn(), backupDownloadUrl: vi.fn(), + getUpdate: vi.fn().mockResolvedValue({ enabled: false, current: '5.0.0', available: false }), + approveUpdate: vi.fn(), + getUpdateRun: vi.fn(), ApiError: class ApiError extends Error {}, })); const { getHealth } = await import('../api.js'); diff --git a/web-v2/src/__tests__/updateNotice.test.jsx b/web-v2/src/__tests__/updateNotice.test.jsx new file mode 100644 index 00000000..1b1c98cc --- /dev/null +++ b/web-v2/src/__tests__/updateNotice.test.jsx @@ -0,0 +1,390 @@ +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; +import { MemoryRouter } from 'react-router-dom'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +vi.mock('../api.js', () => { + class ApiError extends Error { + constructor(message, { status, code } = {}) { + super(message); + this.status = status; + this.code = code; + } + } + return { + ApiError, + getUpdate: vi.fn(), + approveUpdate: vi.fn(), + getUpdateRun: vi.fn(), + getAccount: vi.fn(), + }; +}); +vi.mock('../components/ConfirmDialog.jsx', () => ({ confirmDialog: vi.fn().mockResolvedValue(true) })); + +const api = await import('../api.js'); +const { confirmDialog } = await import('../components/ConfirmDialog.jsx'); +const { default: UpdateNotice } = await import('../components/UpdateNotice.jsx'); +const { default: UpdateBadge } = await import('../components/UpdateBadge.jsx'); +const { describeRun, readStoredRun, resetUpdateStatusCache, syloIsDown, writeStoredRun } = + await import('../updateModel.js'); +const { resetOperatorCache } = await import('../useOperator.js'); + +const status = (over = {}) => ({ + enabled: true, + current: '5.2.0', + latest: '5.3.0', + available: true, + checkedAt: '2026-11-16T10:00:00.000Z', + checkFailed: false, + releaseUrl: 'https://github.com/Ferdinand99/Sylo/releases/tag/v5.3.0', + publishedAt: null, + notes: { + version: '5.3.0', + date: '2026-11-20', + url: null, + sections: [ + { + title: 'New', + kind: 'features', + items: [ + { + scope: 'dashboard', + text: 'The update notice, with [a good link](https://example.com/ok) and [a bad one](javascript:alert(1))', + refs: [{ label: '#370', url: 'https://github.com/Ferdinand99/Sylo/issues/370' }], + }, + ], + }, + ], + }, + notesText: null, + command: 'docker compose pull && docker compose up -d', + wren: null, + ...over, +}); + +const wrenState = (over = {}) => ({ + configured: true, + error: null, + target: { name: 'sylo', server: 'docker-1', status: 'update', mode: 'approve', latestVersion: '5.3.0' }, + updateSeen: true, + canApprove: true, + whyNot: null, + latestRun: null, + ...over, +}); + +beforeEach(() => { + vi.clearAllMocks(); + resetUpdateStatusCache(); + resetOperatorCache(); + writeStoredRun(null); + confirmDialog.mockResolvedValue(true); +}); +afterEach(() => vi.useRealTimers()); + +describe('the words for a Wise Wren run', () => { + it('names each state and whether it is going well', () => { + expect(describeRun({ status: 'queued' }).tone).toBe('working'); + expect(describeRun({ status: 'succeeded' }).tone).toBe('success'); + for (const s of ['rolled_back', 'failed', 'rollback_failed', 'refused', 'cancelled', 'lapsed']) { + expect(describeRun({ status: s }).tone, s).toBe('failure'); + } + expect(describeRun({ status: 'rollback_failed' }).text).toMatch(/Wise Wren now/); + const running = describeRun({ + status: 'running', + steps: [ + { step: 'preflight', status: 'ok' }, + { step: 'stop', status: 'running' }, + ], + }); + expect(running.step).toBe('Stopping Sylo'); + expect(describeRun({ status: 'running', steps: [{ step: 'newstep', status: 'running' }] }).step).toBe( + 'newstep' + ); + }); + + it('tells Sylo being down from Wise Wren saying no', () => { + expect(syloIsDown(new TypeError('Failed to fetch'))).toBe(true); + expect(syloIsDown({ status: 502 })).toBe(true); + expect(syloIsDown({ status: 503, code: undefined })).toBe(true); + expect(syloIsDown({ status: 502, code: 'wren_unreachable' })).toBe(false); + expect(syloIsDown({ status: 404, code: 'wren_not_found' })).toBe(false); + }); + + it('keeps the run for this tab', () => { + expect(readStoredRun()).toBeNull(); + writeStoredRun({ id: 12, from: '5.2.0' }); + expect(readStoredRun()).toEqual({ id: 12, from: '5.2.0' }); + sessionStorage.setItem('sylo:v2:updateRun', '{"id":"x"}'); + expect(readStoredRun()).toBeNull(); + }); +}); + +describe('UpdateNotice', () => { + it('says Sylo is up to date, with no command, and no Wise Wren section without it', async () => { + api.getUpdate.mockResolvedValue(status({ available: false, latest: '5.2.0', notes: null })); + render(); + expect(await screen.findByText(/Sylo 5\.2\.0 is up to date/)).toBeTruthy(); + expect(screen.queryByText(/docker compose pull/)).toBeNull(); + expect(screen.queryByText('Wise Wren')).toBeNull(); + }); + + it('shows a new release with its notes, https links only, and the command to copy', async () => { + api.getUpdate.mockResolvedValue(status()); + const { container } = render(); + expect(await screen.findByText(/Sylo 5\.3\.0 is available/)).toBeTruthy(); + expect(screen.getByText('docker compose pull && docker compose up -d')).toBeTruthy(); + expect(screen.getByRole('button', { name: /Copy/ })).toBeTruthy(); + const links = [...container.querySelectorAll('a')].map((a) => a.getAttribute('href')); + expect(links).toContain('https://example.com/ok'); + expect(links).toContain('https://github.com/Ferdinand99/Sylo/issues/370'); + expect(links.every((h) => h.startsWith('https://'))).toBe(true); + expect(container.innerHTML).not.toMatch(/javascript:/); + for (const a of container.querySelectorAll('a')) expect(a.getAttribute('rel')).toMatch(/noopener/); + // Without Wise Wren, the page points at it. + expect(screen.getByRole('link', { name: 'Wise Wren' }).getAttribute('href')).toBe( + 'https://github.com/Ferdinand99/Wise-Wren' + ); + expect(screen.queryByRole('button', { name: 'Update now' })).toBeNull(); + }); + + it('renders notes that are not markup as text, never HTML', async () => { + api.getUpdate.mockResolvedValue( + status({ notes: null, notesText: 'Fixed things' }) + ); + const { container } = render(); + await screen.findByText(/is available/); + expect(container.querySelector('img')).toBeNull(); + expect(container.textContent).toContain(''); + }); + + it('says so when the check is off or could not be made', async () => { + api.getUpdate.mockResolvedValue(status({ enabled: false, available: false, latest: null, notes: null })); + const first = render(); + expect(await screen.findByText(/update check is off \(UPDATE_CHECK=off\)/)).toBeTruthy(); + expect(screen.queryByRole('button', { name: 'Check again' })).toBeNull(); + first.unmount(); + resetUpdateStatusCache(); + api.getUpdate.mockResolvedValue( + status({ available: false, latest: null, checkFailed: true, notes: null }) + ); + render(); + expect(await screen.findByText(/could not be checked/)).toBeTruthy(); + }); + + it('Check again asks the server to look again', async () => { + api.getUpdate.mockResolvedValue(status({ available: false, latest: '5.2.0', notes: null })); + render(); + fireEvent.click(await screen.findByRole('button', { name: 'Check again' })); + await waitFor(() => expect(api.getUpdate).toHaveBeenLastCalledWith({ refresh: true })); + }); + + it('renders nothing for a refused (non-operator) read and a quiet line for a failed one', async () => { + api.getUpdate.mockRejectedValue(Object.assign(new api.ApiError('no', { status: 403 }), {})); + const first = render(); + await waitFor(() => expect(api.getUpdate).toHaveBeenCalled()); + expect(first.container.textContent).toBe(''); + first.unmount(); + resetUpdateStatusCache(); + api.getUpdate.mockRejectedValue(new api.ApiError('down', { status: 500 })); + render(); + expect(await screen.findByText(/Could not read the update status/)).toBeTruthy(); + }); + + it('offers Update now with Wise Wren, asks first, and starts the run', async () => { + api.getUpdate.mockResolvedValue(status({ wren: wrenState() })); + api.approveUpdate.mockResolvedValue({ runId: 12, status: 'queued', from: '5.2.0' }); + api.getUpdateRun.mockResolvedValue({ + run: { id: 12, status: 'queued', finished: false, ok: false, steps: [] }, + }); + render(); + expect(await screen.findByText('Wise Wren sees 5.3.0 for "sylo" on docker-1.')).toBeTruthy(); + fireEvent.click(screen.getByRole('button', { name: 'Update now' })); + await waitFor(() => expect(api.approveUpdate).toHaveBeenCalledTimes(1)); + expect(confirmDialog).toHaveBeenCalledTimes(1); + expect(confirmDialog.mock.calls[0][0].title).toMatch(/Update Sylo to 5\.3\.0/); + expect(await screen.findByText(/Waiting for the Wise Wren agent/)).toBeTruthy(); + expect(readStoredRun()).toEqual({ id: 12, from: '5.2.0' }); + expect(screen.queryByRole('button', { name: 'Update now' })).toBeNull(); + }); + + it('does not start anything when the confirmation is declined', async () => { + confirmDialog.mockResolvedValue(false); + api.getUpdate.mockResolvedValue(status({ wren: wrenState() })); + render(); + fireEvent.click(await screen.findByRole('button', { name: 'Update now' })); + await waitFor(() => expect(confirmDialog).toHaveBeenCalled()); + expect(api.approveUpdate).not.toHaveBeenCalled(); + }); + + it('disables Update now and says why when Wise Wren would refuse; shows a refusal from the server', async () => { + api.getUpdate.mockResolvedValue( + status({ wren: wrenState({ canApprove: false, whyNot: 'Wise Wren only notifies for this target.' }) }) + ); + render(); + const button = await screen.findByRole('button', { name: 'Update now' }); + expect(button.disabled).toBe(true); + expect(screen.getByText(/only notifies/)).toBeTruthy(); + }); + + it('lets the long explanations wrap, so the variable names are readable in full', async () => { + const whyNot = + 'Update now needs a dashboard login. Set up a login, or set WISE_WREN_ALLOW_OPEN_MODE=true if you accept it.'; + api.getUpdate.mockResolvedValue(status({ wren: wrenState({ canApprove: false, whyNot }) })); + const first = render(); + const hint = await screen.findByText(/WISE_WREN_ALLOW_OPEN_MODE=true/); + expect(hint.classList.contains('is-wrap')).toBe(true); + first.unmount(); + resetUpdateStatusCache(); + api.getUpdate.mockResolvedValue(status()); + render(); + const tip = await screen.findByText(/Set WISE_WREN_URL, WISE_WREN_TOKEN/); + expect(tip.closest('p').classList.contains('is-wrap')).toBe(true); + }); + + it('tells the operator what to check when Wise Wren is not reachable, and offers Try again', async () => { + api.getUpdate.mockResolvedValue( + status({ + wren: wrenState({ + target: null, + canApprove: false, + error: { code: 'wren_unreachable', message: 'Wise Wren is not reachable.' }, + }), + }) + ); + render(); + expect(await screen.findByText('Wise Wren is not reachable.')).toBeTruthy(); + expect(screen.getByText(/Check WISE_WREN_URL and that Wise Wren is running/)).toBeTruthy(); + fireEvent.click(screen.getByRole('button', { name: 'Try again' })); + await waitFor(() => expect(api.getUpdate).toHaveBeenLastCalledWith({ refresh: true })); + }); + + it('shows what the server says when the approval fails', async () => { + api.getUpdate.mockResolvedValue(status({ wren: wrenState() })); + api.approveUpdate.mockRejectedValue( + new api.ApiError('Wise Wren did not accept the token.', { status: 502, code: 'wren_unauthorized' }) + ); + render(); + fireEvent.click(await screen.findByRole('button', { name: 'Update now' })); + expect(await screen.findByText(/did not accept the token/)).toBeTruthy(); + expect(readStoredRun()).toBeNull(); + }); + + it('follows the update through the restart and shows the new version', async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + writeStoredRun({ id: 12, from: '5.2.0' }); + api.getUpdate.mockResolvedValueOnce( + status({ wren: wrenState({ latestRun: { id: 12, finished: false } }) }) + ); + api.getUpdateRun + // Wise Wren is stopping Sylo... + .mockResolvedValueOnce({ + run: { + id: 12, + status: 'running', + finished: false, + ok: false, + steps: [{ step: 'stop', status: 'running' }], + }, + }) + // ...Sylo is down: the request never gets an answer, and then a proxy error... + .mockRejectedValueOnce(new TypeError('Failed to fetch')) + .mockRejectedValueOnce(new api.ApiError('Unexpected response (HTTP 502)', { status: 502 })) + // ...and back, with the run finished. + .mockResolvedValue({ + run: { id: 12, status: 'succeeded', finished: true, ok: true, steps: [] }, + }); + api.getUpdate.mockResolvedValue( + status({ current: '5.3.0', available: false, latest: '5.3.0', notes: null, wren: wrenState() }) + ); + + render(); + expect(await screen.findByText(/Now: Stopping Sylo/)).toBeTruthy(); + await act(async () => { + await vi.advanceTimersByTimeAsync(3100); + }); + expect(await screen.findByText(/Sylo is restarting/)).toBeTruthy(); + await act(async () => { + await vi.advanceTimersByTimeAsync(3100); + }); + expect(screen.getByText(/Sylo is restarting/)).toBeTruthy(); + await act(async () => { + await vi.advanceTimersByTimeAsync(3100); + }); + expect(await screen.findByText(/Sylo is now on 5\.3\.0/)).toBeTruthy(); + expect(screen.getByRole('button', { name: 'Reload' })).toBeTruthy(); + expect(readStoredRun()).toBeNull(); + const calls = api.getUpdateRun.mock.calls.length; + await act(async () => { + await vi.advanceTimersByTimeAsync(10_000); + }); + expect(api.getUpdateRun.mock.calls.length).toBe(calls); + }); + + it('shows a failed run and stops asking', async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + writeStoredRun({ id: 12, from: '5.2.0' }); + api.getUpdate.mockResolvedValue(status({ wren: wrenState() })); + api.getUpdateRun.mockResolvedValue({ + run: { id: 12, status: 'rolled_back', finished: true, ok: false, steps: [] }, + }); + render(); + expect(await screen.findByText(/Wise Wren rolled back/)).toBeTruthy(); + expect(screen.getByRole('button', { name: 'Close' })).toBeTruthy(); + const calls = api.getUpdateRun.mock.calls.length; + await act(async () => { + await vi.advanceTimersByTimeAsync(10_000); + }); + expect(api.getUpdateRun.mock.calls.length).toBe(calls); + }); + + it('picks up an update that is already running in Wise Wren', async () => { + api.getUpdate.mockResolvedValue( + status({ wren: wrenState({ latestRun: { id: 31, status: 'running', finished: false } }) }) + ); + api.getUpdateRun.mockResolvedValue({ + run: { id: 31, status: 'running', finished: false, ok: false, steps: [] }, + }); + render(); + expect(await screen.findByText(/Wise Wren is updating Sylo/)).toBeTruthy(); + expect(api.getUpdateRun).toHaveBeenCalledWith(31); + }); +}); + +describe('UpdateBadge', () => { + const inShell = (ui) => render({ui}); + + it('shows a pill linking to the Health page for an operator when a release is out', async () => { + api.getAccount.mockResolvedValue({ operator: true }); + api.getUpdate.mockResolvedValue(status()); + inShell(); + const pill = await screen.findByText('Update available'); + expect(pill.getAttribute('href')).toBe('/health#update'); + expect(pill.getAttribute('title')).toBe('Sylo 5.3.0 is available'); + expect(pill.getAttribute('aria-label')).toBe('Sylo 5.3.0 is available. Open Health'); + }); + + it('asks nothing and shows nothing for someone who is not an operator', async () => { + api.getAccount.mockResolvedValue({ operator: false }); + api.getUpdate.mockResolvedValue(status()); + const { container } = inShell(); + await waitFor(() => expect(api.getAccount).toHaveBeenCalled()); + await act(async () => {}); + expect(container.textContent).toBe(''); + expect(api.getUpdate).not.toHaveBeenCalled(); + }); + + it('shows nothing when Sylo is up to date or the check failed', async () => { + api.getAccount.mockResolvedValue({ operator: true }); + api.getUpdate.mockResolvedValue(status({ available: false })); + const first = inShell(); + await waitFor(() => expect(api.getUpdate).toHaveBeenCalled()); + await act(async () => {}); + expect(first.container.textContent).toBe(''); + first.unmount(); + resetUpdateStatusCache(); + api.getUpdate.mockRejectedValue(new Error('x')); + const second = inShell(); + await act(async () => {}); + expect(second.container.textContent).toBe(''); + }); +}); diff --git a/web-v2/src/api.js b/web-v2/src/api.js index e93b45e1..ab9d4791 100644 --- a/web-v2/src/api.js +++ b/web-v2/src/api.js @@ -295,6 +295,11 @@ export const clearPersonalizerAnnouncement = () => postJson('/api/v2/personalize export const getHealth = () => apiFetch('/api/v2/health'); export const getStats = () => apiFetch('/api/v2/stats'); +// The operators' update notice (GET /api/v2/update): a newer Sylo release, and what Wise Wren sees when it is set up. +export const getUpdate = ({ refresh = false } = {}) => + apiFetch(`/api/v2/update${refresh ? '?refresh=1' : ''}`); +export const approveUpdate = () => postJson('/api/v2/update/wren/approve', {}); +export const getUpdateRun = (id) => apiFetch(`/api/v2/update/wren/runs/${encodeURIComponent(id)}`); // A snapshot download is a plain link (the browser saves the file), not a fetch. export const backupDownloadUrl = (name) => `/api/v2/health/backups/${encodeURIComponent(name)}`; export const sendDevLogTest = () => postJson('/api/v2/health/dev-log-test', {}); diff --git a/web-v2/src/components/Shell.jsx b/web-v2/src/components/Shell.jsx index e856c0f0..4e60b730 100644 --- a/web-v2/src/components/Shell.jsx +++ b/web-v2/src/components/Shell.jsx @@ -10,6 +10,7 @@ import Announcement from './Announcement.jsx'; import ServerSwitcher from './ServerSwitcher.jsx'; import Sidebar from './Sidebar.jsx'; import ThemeToggle from './ThemeToggle.jsx'; +import UpdateBadge from './UpdateBadge.jsx'; import AccountMenu from './AccountMenu.jsx'; import CommandPalette, { isApplePlatform, isPaletteShortcut } from './CommandPalette.jsx'; import { Icon } from './icons.jsx'; @@ -116,6 +117,7 @@ function SignedInShell() { + + ) : null} + {view.version || view.run?.finished || timedOut ? ( + + ) : null} + + + ); +} + +/** + * The "Updates" section of the Health page (operators only): whether a newer Sylo is out, its release notes, + * the command to update by hand and, when Wise Wren is set up, "Update now" with the run's progress. + */ +export default function UpdateNotice() { + const [state, setState] = useState({ loading: true, data: null, failed: false }); + const [busy, setBusy] = useState(false); + const [message, setMessage] = useState(''); + const [run, setRun] = useState(() => readStoredRun()); + + const load = useCallback(async ({ refresh = false } = {}) => { + try { + const data = await loadUpdateStatus({ fresh: true, refresh }); + setState({ loading: false, data, failed: false }); + // An update that is already running (started here earlier, or in Wise Wren) is followed too. + const latest = data?.wren?.latestRun; + if (latest && !latest.finished) { + setRun((current) => current ?? { id: latest.id, from: data.current }); + } + return data; + } catch (err) { + if (err instanceof ApiError && err.status === 403) + setState({ loading: false, data: null, failed: false }); + else setState({ loading: false, data: null, failed: true }); + return null; + } + }, []); + + useEffect(() => { + load(); + }, [load]); + + if (state.loading) return null; + if (state.failed) { + return ( +
+

Updates

+

Could not read the update status. Try again in a moment.

+
+ ); + } + const d = state.data; + if (!d) return null; + const wren = d.wren; + + async function recheck() { + setBusy(true); + setMessage(''); + try { + await load({ refresh: true }); + } finally { + setBusy(false); + } + } + + async function updateNow() { + const ok = await confirmDialog({ + title: `Update Sylo to ${d.latest ?? 'the new version'}?`, + body: ( + <> + Wise Wren stops Sylo for a short while, takes a snapshot, starts the new version and rolls back if + it does not come up healthy. The dashboard and the bot are offline meanwhile. + + ), + confirmLabel: 'Update now', + tone: 'default', + }); + if (!ok) return; + setBusy(true); + setMessage(''); + try { + const started = await approveUpdate(); + const next = { id: started.runId, from: started.from }; + writeStoredRun(next); + setRun(next); + } catch (err) { + setMessage(err.message || 'Could not start the update.'); + } finally { + setBusy(false); + } + } + + function closeRun() { + writeStoredRun(null); + setRun(null); + load(); + } + + let headline; + if (!d.enabled) headline = `Sylo ${d.current}. The update check is off (UPDATE_CHECK=off).`; + else if (d.available) headline = `Sylo ${d.latest} is available. This Sylo runs ${d.current}.`; + else if (d.checkFailed && !d.latest) + headline = `Sylo ${d.current}. The latest release could not be checked.`; + else headline = `Sylo ${d.current} is up to date.`; + + return ( +
+

Updates

+
+
+
+

{headline}

+ {d.available && d.releaseUrl ? ( +

+ + Release on GitHub + +

+ ) : null} + {d.available ? : null} + {d.available ? ( +
+

+ To update by hand, run this where Sylo's compose file is: +

+
+ {d.command} + +
+
+ ) : null} + {d.enabled && d.checkFailed && d.latest ? ( +

The last check failed; this is from an earlier one.

+ ) : null} +
+ {d.enabled ? ( +
+ +
+ ) : null} +
+ + {wren ? ( +
+
+

Wise Wren

+ {run ? ( + + ) : wren.error ? ( + <> +

{wren.error.message}

+

+ Check WISE_WREN_URL and that Wise Wren is running, then try again. +

+
+ +
+ + ) : ( + <> +

{wrenSummary(wren)}

+ {wren.whyNot && (d.available || wren.updateSeen) ? ( +

{wren.whyNot}

+ ) : null} + + )} + {message ?

{message}

: null} +
+ {!run && !wren.error && (d.available || wren.updateSeen) ? ( +
+ +
+ ) : null} +
+ ) : d.available ? ( +
+
+ +

+ + Wise Wren + {' '} + can update Sylo from this page, without a command line. Set WISE_WREN_URL, WISE_WREN_TOKEN + and WISE_WREN_TARGET_ID (see the self-hosting guide, "Updating Sylo"). +

+
+
+
+ ) : null} +
+
+ ); +} diff --git a/web-v2/src/main.jsx b/web-v2/src/main.jsx index e97b7ce7..522aa079 100644 --- a/web-v2/src/main.jsx +++ b/web-v2/src/main.jsx @@ -22,6 +22,7 @@ import './styles/setup-wizard.css'; import './styles/access.css'; import './styles/history.css'; import './styles/health.css'; +import './styles/p2.css'; createRoot(document.getElementById('root')).render( diff --git a/web-v2/src/pages/Health.jsx b/web-v2/src/pages/Health.jsx index fbd7b537..dbb6b5d4 100644 --- a/web-v2/src/pages/Health.jsx +++ b/web-v2/src/pages/Health.jsx @@ -15,6 +15,7 @@ import { confirmDialog } from '../components/ConfirmDialog.jsx'; import { plural } from '../components/Meta.jsx'; import { formatCount } from '../util.js'; import Reveal from '../components/Reveal.jsx'; +import UpdateNotice from '../components/UpdateNotice.jsx'; import { Icon } from '../components/icons.jsx'; // Plain names for the error sources that are not a module (a module's own name comes from the server). @@ -273,6 +274,8 @@ export default function Health() { ) : null} + + {d.sessionSecret === 'generated-ephemeral' ? (

Session secret

diff --git a/web-v2/src/styles/p2.css b/web-v2/src/styles/p2.css new file mode 100644 index 00000000..08c1555c --- /dev/null +++ b/web-v2/src/styles/p2.css @@ -0,0 +1,107 @@ +/* The operators' update notice: the pill in the top bar and the "Updates" section of the Health page + (components/UpdateBadge.jsx, components/UpdateNotice.jsx). The release notes reuse the What's new list + styles (.v2-cl-section). Colours are the status tokens. */ + +/* ---- Top bar --------------------------------------------------------------------------------------- */ +.v2-update-badge { + flex: none; + text-decoration: none; +} +.v2-update-badge:hover { + filter: brightness(1.1); + text-decoration: underline; +} +/* An arrow marks it as a link, unlike the static status pills. */ +.v2-update-badge::after { + content: '\2192'; + font-weight: 600; +} +/* Below the drawer breakpoint the top bar is tight: the pill keeps to a short label. */ +@media (max-width: 600px) { + .v2-update-badge { + padding: 2px 8px; + } +} + +/* ---- Health page ----------------------------------------------------------------------------------- */ +/* Everything in the section is read in full: `.v2-row-main p` otherwise cuts a paragraph to one line with + an ellipsis, which hid the environment variable names in the Wise Wren tip and the open-mode reason. */ +#update .v2-row-main p, +#update .v2-callout p { + white-space: normal; + overflow: visible; + text-overflow: clip; + overflow-wrap: anywhere; +} +/* The main message first: the headline is the section's lead line, not muted row text. */ +#update .v2-upd-headline { + margin: 0; + color: var(--text); + font-size: var(--fs-md); + font-weight: 600; + line-height: 1.4; +} +/* "Check again" stays at the top of the row when the notes are open. */ +#update .v2-row-actions { + align-self: flex-start; +} +.v2-upd-retry { + margin-top: 8px; +} +.v2-upd-notes { + margin-top: 10px; +} +.v2-upd-notes summary { + cursor: pointer; + font-weight: 600; +} +.v2-upd-notes .v2-cl-section { + margin-top: 8px; +} +.v2-upd-notes-text { + margin: 8px 0 0; + white-space: pre-wrap; + overflow-wrap: anywhere; +} + +.v2-upd-command { + margin-top: 12px; +} +.v2-upd-command-row { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px 12px; +} +.v2-upd-command-row code { + padding: 4px 8px; + background: var(--bg-surface-2); + border: 1px solid var(--border-subtle); + border-radius: 4px; + font-size: 0.85em; + overflow-wrap: anywhere; +} + +.v2-upd-progress { + padding: 10px 12px; + border: 1px solid var(--border-subtle); + border-left-width: 3px; + border-radius: var(--radius); +} +.v2-upd-progress.is-working { + border-left-color: var(--warn); +} +.v2-upd-progress.is-success { + border-left-color: var(--success); +} +.v2-upd-progress.is-failure { + border-left-color: var(--danger); +} +.v2-upd-progress p { + margin: 0 0 6px; + white-space: normal; + overflow-wrap: anywhere; +} +.v2-upd-step { + color: var(--text-muted); +} diff --git a/web-v2/src/updateModel.js b/web-v2/src/updateModel.js new file mode 100644 index 00000000..e7dd3a64 --- /dev/null +++ b/web-v2/src/updateModel.js @@ -0,0 +1,125 @@ +// The operators' update notice: what the pages share. Pure helpers (the words for a Wise Wren run, the +// stored run id) and one cached read of GET /api/v2/update, so the top bar's badge and the Health page's +// section do not each ask the server on every render. +import { getUpdate } from './api.js'; + +const RUN_KEY = 'sylo:v2:updateRun'; + +/** The run the operator started, kept for this tab so a reload during the update picks the progress up again. */ +export function readStoredRun() { + try { + const run = JSON.parse(sessionStorage.getItem(RUN_KEY) ?? 'null'); + return Number.isSafeInteger(run?.id) && typeof run.from === 'string' + ? { id: run.id, from: run.from } + : null; + } catch { + return null; + } +} + +export function writeStoredRun(run) { + try { + if (run) sessionStorage.setItem(RUN_KEY, JSON.stringify(run)); + else sessionStorage.removeItem(RUN_KEY); + } catch { + // ignore: the progress then just does not survive a reload + } +} + +// Wise Wren's step names, in words. A step it adds later shows as it is named. +const STEP_LABELS = { + preflight: 'Pulling the new version', + maintenance: 'Opening the maintenance window', + stop: 'Stopping Sylo', + snapshot: 'Taking a snapshot of the data', + update: 'Starting the new version', + health: 'Checking that Sylo is healthy', + rollback: 'Rolling back', + close: 'Cleaning up', +}; + +export const stepLabel = (step) => STEP_LABELS[step] ?? String(step ?? ''); + +/** + * What to tell the operator about a Wise Wren run: `tone` is 'working', 'success' or 'failure', `text` the + * sentence, `step` the step it is on (while it works). + * @param {{ status?: string, steps?: { step: string, status: string }[] } | null} run + */ +export function describeRun(run) { + const status = run?.status ?? 'queued'; + switch (status) { + case 'queued': + return { tone: 'working', text: 'Approved. Waiting for the Wise Wren agent to pick the update up.' }; + case 'running': { + const steps = run?.steps ?? []; + const current = steps.findLast?.((s) => s.status === 'running') ?? steps[steps.length - 1]; + return { + tone: 'working', + text: 'Wise Wren is updating Sylo. Sylo restarts during this and the page reconnects by itself.', + step: current ? stepLabel(current.step) : null, + }; + } + case 'succeeded': + return { tone: 'success', text: 'Wise Wren finished the update.' }; + case 'rolled_back': + return { + tone: 'failure', + text: 'The new version did not come up healthy, so Wise Wren rolled back. Sylo runs the old version again.', + }; + case 'failed': + return { + tone: 'failure', + text: 'The update failed before anything was changed. Sylo still runs the old version.', + }; + case 'rollback_failed': + return { + tone: 'failure', + text: 'The update failed and the rollback failed too. Look at Sylo in Wise Wren now.', + }; + case 'refused': + return { + tone: 'failure', + text: 'The Wise Wren agent refused the update: its own settings do not allow updating Sylo.', + }; + case 'cancelled': + return { tone: 'failure', text: 'The update was cancelled in Wise Wren.' }; + case 'lapsed': + return { + tone: 'failure', + text: 'A newer version appeared before the update started, so it was dropped. Try again.', + }; + default: + return { tone: 'working', text: 'Wise Wren is working on the update.' }; + } +} + +/** + * True when a failed poll means Sylo itself is not answering (restarting, or the proxy has no upstream), + * as opposed to Sylo answering with a Wise Wren error. + */ +export function syloIsDown(err) { + if (typeof err?.status !== 'number') return true; // the request never got an answer + return err.status >= 500 && !String(err.code ?? '').startsWith('wren_'); +} + +const TTL_MS = 5 * 60 * 1000; +let cached = null; // { at, promise } + +/** + * One read of the update status, shared for a few minutes (`fresh` skips the share). A failure is not + * kept: the next call asks again. + */ +export function loadUpdateStatus({ fresh = false, refresh = false } = {}) { + if (!fresh && !refresh && cached && Date.now() - cached.at < TTL_MS) return cached.promise; + const promise = Promise.resolve().then(() => getUpdate({ refresh })); + cached = { at: Date.now(), promise }; + promise.catch(() => { + if (cached?.promise === promise) cached = null; + }); + return promise; +} + +/** For tests. */ +export function resetUpdateStatusCache() { + cached = null; +}