From 981cd6660c9968a24b417adf6af89151d2e923fc Mon Sep 17 00:00:00 2001 From: Jarek Krochmalski Date: Tue, 29 Sep 2026 14:58:14 +0200 Subject: [PATCH 01/22] release housekeeping --- VERSION | 2 +- package.json | 1 + src/lib/data/changelog.json | 31 +++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/VERSION b/VERSION index b8a1558ff..aee6aca6d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v1.0.49 +v1.0.50 diff --git a/package.json b/package.json index 5f777e887..123d6b18f 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "drizzle-orm": "0.45.2", "fast-xml-parser": "5.7.3", "isomorphic-dompurify": "3.22.0", + "jose": "6.2.12", "js-yaml": "4.3.2", "ldapts": "8.1.3", "marked": "18.0.9", diff --git a/src/lib/data/changelog.json b/src/lib/data/changelog.json index 5b4e3f35d..33c3070a3 100644 --- a/src/lib/data/changelog.json +++ b/src/lib/data/changelog.json @@ -1,4 +1,35 @@ [ + { + "version": "1.0.50", + "date": "2026-09-29", + "changes": [ + { "type": "feature", "text": "update a container straight from its details view when a newer image is available (#1588)" }, + { "type": "feature", "text": "duplicate a backup schedule to reuse its settings for another destination (#1578)" }, + { "type": "feature", "text": "mark a secret provider as default so new stacks start with it selected (#1609)" }, + { "type": "feature", "text": "optionally skip the login form, and sign out at the identity provider (#563, #562, #1318)" }, + { "type": "feature", "text": "dashboard disk usage segments link straight to images, volumes or containers (#1314)" }, + { "type": "feature", "text": "the container file browser opens at the container's working directory (#1285)" }, + { "type": "feature", "text": "mattermost notifications arrive as colour-coded cards, like the discord ones (#1607)" }, + { "type": "feature", "text": "container icons fall back to the image namespace when the name has none (#1624)" }, + { "type": "feature", "text": "tag groups are ordered alphabetically by tag name (#1625)" }, + { "type": "fix", "text": "oidc sign-in survives a restart and works across multiple replicas (#1601)" }, + { "type": "fix", "text": "cleaner image scan logs, and the real registry error (#1486, #1628, #1629)" }, + { "type": "fix", "text": "compose validate no longer flags variables a secret provider supplies (#1621)" }, + { "type": "fix", "text": "updating a container through the API keeps its secondary networks and their aliases" }, + { "type": "fix", "text": "recreating a container no longer reuses its old auto-assigned MAC address (#1618)" }, + { "type": "fix", "text": "podman's default bridge is no longer shown as an extra network on containers (#1619)" }, + { "type": "fix", "text": "a scheduled container update clears its pending row, so update all no longer fails on it (#1632)" }, + { "type": "fix", "text": "image prune no longer times out after 30s when filters are set (#1630)" }, + { "type": "fix", "text": "exporting an image whose name contains a slash no longer fails (#1635)" }, + { "type": "fix", "text": "percentage disk warnings are marked as unusable on hosts that report no total (#1631)" }, + { "type": "fix", "text": "https now starts with certificates that have no subject, like newer let's encrypt ones (#1622)" }, + { "type": "fix", "text": "dashboard keeps its top containers and recent events after a container event (#1608)" }, + { "type": "fix", "text": "the secret provider selector is no longer lost on save in the env text view (#1620)" }, + { "type": "fix", "text": "a git stack .env value can be quoted and refer to another variable (#1018)" }, + { "type": "fix", "text": "update docker-compose 5.5.1-r1, devalue 5.9.2 and nodemailer 9.1.1 for security fixes" } + ], + "imageTag": "fnsys/dockhand:v1.0.50" + }, { "version": "1.0.49", "date": "2026-09-23", From c15b523554697f6604acf6823a9eec7a273bfcdb Mon Sep 17 00:00:00 2001 From: Jarek Krochmalski Date: Tue, 29 Sep 2026 14:58:14 +0200 Subject: [PATCH 02/22] an expired licence holds non-admins instead of switching the instance to the free edition --- src/lib/server/authorize.ts | 23 +- src/lib/server/license-state-core.ts | 74 ++++++ src/lib/server/license.ts | 53 +++- src/lib/stores/license.ts | 123 +++++++-- src/lib/utils/licence-hold.ts | 49 ++++ src/lib/utils/license-expiry-text.ts | 130 ++++++++++ src/routes/+layout.svelte | 41 ++- src/routes/api/audit/+server.ts | 2 +- src/routes/api/audit/events/+server.ts | 2 +- src/routes/api/audit/export/+server.ts | 2 +- src/routes/api/audit/users/+server.ts | 2 +- src/routes/api/auth/ldap/+server.ts | 4 +- src/routes/api/auth/ldap/[id]/+server.ts | 6 +- src/routes/api/auth/ldap/[id]/test/+server.ts | 2 +- src/routes/api/license/+server.ts | 15 +- src/routes/audit/+server.ts | 2 +- src/routes/audit/users/+server.ts | 2 +- src/routes/settings/license/LicenseTab.svelte | 27 +- tests/licence-hold.test.ts | 66 +++++ tests/license-expiry-text.test.ts | 237 ++++++++++++++++++ tests/license-state-core.test.ts | 171 +++++++++++++ 21 files changed, 969 insertions(+), 64 deletions(-) create mode 100644 src/lib/server/license-state-core.ts create mode 100644 src/lib/utils/licence-hold.ts create mode 100644 src/lib/utils/license-expiry-text.ts create mode 100644 tests/licence-hold.test.ts create mode 100644 tests/license-expiry-text.test.ts create mode 100644 tests/license-state-core.test.ts diff --git a/src/lib/server/authorize.ts b/src/lib/server/authorize.ts index 197c68ebe..c1a91c2da 100644 --- a/src/lib/server/authorize.ts +++ b/src/lib/server/authorize.ts @@ -40,7 +40,7 @@ import { json } from '@sveltejs/kit'; import type { Permissions } from './db'; import { getUserAccessibleEnvironments, userCanAccessEnvironment, userHasAdminRole } from './db'; import { validateSession, isAuthEnabled, checkPermission, type AuthenticatedUser } from './auth'; -import { isEnterprise } from './license'; +import { isEnterprise, hasValidEnterpriseLicense } from './license'; import { getRequestContext } from './request-context'; export interface AuthorizationContext { @@ -56,9 +56,19 @@ export interface AuthorizationContext { /** Whether the user has admin privileges */ isAdmin: boolean; - /** Whether an enterprise license is active */ + /** + * Whether this instance runs the enterprise model: roles exist and are enforced. + * Stays true when the license lapses, so authorization keeps behaving as it did. + */ isEnterprise: boolean; + /** + * Whether the enterprise license currently validates. Gate paid FEATURES on this + * (LDAP, audit log, role editing) rather than on isEnterprise, which an expiry + * deliberately leaves alone. + */ + hasValidLicense: boolean; + /** * Check if the user has a specific permission. * In free edition, all authenticated users have full access. @@ -136,6 +146,7 @@ export interface AuthorizationContext { export async function authorize(cookies: Cookies): Promise { const authEnabled = await isAuthEnabled(); const enterprise = await isEnterprise(); + const validLicense = await hasValidEnterpriseLicense(); // Try request context first (set by hook — handles both cookie and Bearer) const reqCtx = getRequestContext(); @@ -161,6 +172,7 @@ export async function authorize(cookies: Cookies): Promise user, isAdmin, isEnterprise: enterprise, + hasValidLicense: validLicense, async can(resource: keyof Permissions, action: string, environmentId?: number): Promise { // If auth is disabled, allow everything (initial setup) @@ -240,8 +252,11 @@ export async function authorize(cookies: Cookies): Promise }, async canViewAuditLog(): Promise { - // Audit logs are enterprise-only - if (!enterprise) return false; + // Reading the audit log is a paid feature, so it follows the license rather + // than the roles. Writing to it does not: entries keep being recorded during + // a lapse, or the period nobody can see would also be the period nobody + // wrote down. + if (!validLicense) return false; // If auth is disabled, allow access (enterprise-only protection is enough) if (!authEnabled) return true; diff --git a/src/lib/server/license-state-core.ts b/src/lib/server/license-state-core.ts new file mode 100644 index 000000000..92af23702 --- /dev/null +++ b/src/lib/server/license-state-core.ts @@ -0,0 +1,74 @@ +/** + * The three questions a stored license answers, which are not the same question. + * + * "Does this instance enforce roles?" and "is the customer paid up?" used to be one + * flag, so an expiry switched the whole instance to the free edition - where every + * authenticated user has full access - and silently promoted every account to + * administrator. Keeping them apart means an expiry changes who may act without + * changing how authorization works. + * + * enforcesRoles - roles exist and are checked. Survives an expiry. + * licenseValid - the paid features are available. Does not. + * lapsed - installed but no longer valid: hold everyone but admins. + * + * Pure, so the matrix is a unit test rather than something only an expired key shows. + */ + +/** As much of a validated license as these decisions need. */ +export interface LicenseVerdict { + valid: boolean; + active: boolean; + /** Absent when the key was missing, malformed, or failed its signature check. */ + payload?: { type?: string }; +} + +/** A signed enterprise key, whether or not it still validates. */ +function isEnterpriseKey(verdict: LicenseVerdict | null | undefined): boolean { + return verdict?.payload?.type === 'enterprise'; +} + +/** + * Whether roles exist and are enforced. + * + * True for an enterprise key even once it has lapsed: the roles are still assigned, and + * dozens of endpoints gate environment access on this flag. Dropping it on an expiry + * would turn every one of those checks off at once. + */ +export function enforcesRoles(verdict: LicenseVerdict | null | undefined): boolean { + return isEnterpriseKey(verdict); +} + +/** + * Whether the enterprise features are available to sell. + * + * The question to ask when gating LDAP, the audit log, or role editing - things a + * customer stops being entitled to the moment the license stops validating. + */ +export function licenseValid(verdict: LicenseVerdict | null | undefined): boolean { + return !!verdict && verdict.valid && verdict.active && isEnterpriseKey(verdict); +} + +/** + * Whether an enterprise license is installed but no longer validates. + * + * Only a genuine enterprise key reaches this: the signature is checked before the + * payload is trusted, so a forged or corrupt key leaves nothing to claim enterprise + * with. Otherwise anyone able to write the setting could lock the instance out with a + * junk string. An expired SMB key is not a lapse either - SMB never unlocked roles. + */ +export function lapsed(verdict: LicenseVerdict | null | undefined): boolean { + if (!verdict) return false; + if (verdict.valid && verdict.active) return false; + return isEnterpriseKey(verdict); +} + +/** + * Whether a non-administrator may act, given the license state. + * + * Administrators are decided before this is asked, so it answers only for everybody + * else: a lapse holds them until a valid license is installed, and every other state + * leaves them to their roles. + */ +export function nonAdminMayAct(verdict: LicenseVerdict | null | undefined): boolean { + return !lapsed(verdict); +} diff --git a/src/lib/server/license.ts b/src/lib/server/license.ts index 2eef5426a..2a0806fa8 100644 --- a/src/lib/server/license.ts +++ b/src/lib/server/license.ts @@ -1,6 +1,7 @@ import crypto from 'node:crypto'; import os from 'node:os'; import { getSetting, setSetting } from './db'; +import { enforcesRoles, licenseValid, lapsed } from './license-state-core'; import { sendEventNotification } from './notifications'; // RSA Public Key for license verification @@ -154,7 +155,18 @@ export async function deactivateLicense(): Promise { } /** - * Checks if the current installation has an active enterprise license + * Whether this installation runs the enterprise model: roles exist and are enforced. + * + * Stays true when the license stops validating. The roles are still assigned and still + * describe who was trusted with what, so authorization keeps working exactly as it did + * the minute before - including the environment scoping that dozens of endpoints gate + * on. Flipping this to false on an expiry would switch the whole instance to the free + * edition, where every authenticated user has full access, and silently promote every + * account to administrator. + * + * What a lapse DOES change is who may act: see `isLicenseLapsed`, which holds everyone + * but administrators until a valid license is installed. Feature availability - the + * things a customer is paying for - is `hasValidEnterpriseLicense`. */ export async function isEnterprise(): Promise { const stored = await getStoredLicense(); @@ -162,9 +174,42 @@ export async function isEnterprise(): Promise { return false; } - const validation = validateLicense(stored.key, getHostname()); - // Only true for enterprise licenses (SMB does not unlock enterprise features) - return validation.valid && validation.active && validation.payload?.type === 'enterprise'; + return enforcesRoles(validateLicense(stored.key, getHostname())); +} + +/** + * Whether the enterprise license is currently valid. + * + * This is the question to ask when gating a paid FEATURE - LDAP, the audit log, role + * editing. An expired license should stop selling those, while `isEnterprise` keeps + * enforcing the roles the customer already has. + */ +export async function hasValidEnterpriseLicense(): Promise { + const stored = await getStoredLicense(); + if (!stored || !stored.key) { + return false; + } + + return licenseValid(validateLicense(stored.key, getHostname())); +} + +/** + * Whether an enterprise license is installed but no longer validates. + * + * The instance keeps its roles (see `isEnterprise`) but stops taking instructions from + * anyone who is not an administrator, so an expiry or a renamed host cannot quietly + * hand a viewer more than their role allows. An administrator still gets through, which + * is what makes the state recoverable. + * + * Only a genuine enterprise key reaches this: the signature is checked first, so a + * forged or corrupt key leaves no payload and reads as the free edition. Otherwise + * anyone able to write the setting could lock the instance out with a junk string. + */ +export async function isLicenseLapsed(): Promise { + const stored = await getStoredLicense(); + if (!stored || !stored.key) return false; + + return lapsed(validateLicense(stored.key, getHostname())); } /** diff --git a/src/lib/stores/license.ts b/src/lib/stores/license.ts index b80c6b3de..a39a65f0b 100644 --- a/src/lib/stores/license.ts +++ b/src/lib/stores/license.ts @@ -1,9 +1,15 @@ -import { writable, derived } from 'svelte/store'; +import { writable, derived, readable } from 'svelte/store'; +import { browser } from '$app/environment'; +import { daysLeft, deriveLicenceFlags, licenceHeaderText } from '$lib/utils/license-expiry-text'; export type LicenseType = 'enterprise' | 'smb'; export interface LicenseState { isEnterprise: boolean; + /** An enterprise licence is installed, whether or not it is still valid. */ + hasEnterpriseLicense: boolean; + /** Why the licence does not validate, as the server put it. Null when it does. */ + problem: string | null; isLicensed: boolean; licenseType: LicenseType | null; loading: boolean; @@ -14,6 +20,8 @@ export interface LicenseState { function createLicenseStore() { const { subscribe, set, update } = writable({ isEnterprise: false, + hasEnterpriseLicense: false, + problem: null, isLicensed: false, licenseType: null, loading: true, @@ -21,26 +29,48 @@ function createLicenseStore() { expiresAt: null }); + async function check() { + update(state => ({ ...state, loading: true })); + try { + const response = await fetch('/api/license'); + const data = await response.json(); + const flags = deriveLicenceFlags(data); + set({ + ...flags, + licenseType: flags.licenseType as LicenseType | null, + loading: false, + licensedTo: data.stored?.name || null + }); + } catch { + set({ isEnterprise: false, hasEnterpriseLicense: false, problem: null, isLicensed: false, licenseType: null, loading: false, licensedTo: null, expiresAt: null }); + } + } + return { subscribe, - async check() { - update(state => ({ ...state, loading: true })); - try { - const response = await fetch('/api/license'); - const data = await response.json(); - const isValid = data.valid && data.active; - const licenseType = data.payload?.type as LicenseType | undefined; - set({ - isEnterprise: isValid && licenseType === 'enterprise', - isLicensed: isValid, - licenseType: isValid ? (licenseType || null) : null, - loading: false, - licensedTo: data.stored?.name || null, - expiresAt: data.payload?.expires || null - }); - } catch { - set({ isEnterprise: false, isLicensed: false, licenseType: null, loading: false, licensedTo: null, expiresAt: null }); - } + check, + /** + * Re-read the licence when somebody comes back to the page. + * + * Whether a licence EXISTS is the server's answer, unlike how long is left, + * which the browser can work out for itself. A licence activated in another + * tab, or by an administrator elsewhere, is only visible after asking again. + * Returns a function that stops listening. + */ + watchForChanges(): () => void { + if (!browser) return () => {}; + + const recheck = () => { + if (!document.hidden) check(); + }; + + document.addEventListener('visibilitychange', recheck); + window.addEventListener('focus', recheck); + + return () => { + document.removeEventListener('visibilitychange', recheck); + window.removeEventListener('focus', recheck); + }; }, setEnterprise(value: boolean) { update(state => ({ ...state, isEnterprise: value })); @@ -62,14 +92,53 @@ function createLicenseStore() { export const licenseStore = createLicenseStore(); -// Derived store for days until expiration -export const daysUntilExpiry = derived(licenseStore, ($license) => { - if (!$license.isLicensed || !$license.expiresAt) return null; +/** + * The current time, re-read whenever somebody comes back to the page. + * + * "How long is left" is arithmetic on a date the browser already holds, so it needs no + * request - only a fresh reading of the clock. Taking it on focus and on tab + * visibility covers coming back to a window left open, which is when a stale countdown + * would otherwise be believed. There is deliberately no timer: a page nobody is + * looking at has nothing to update, and the listeners only exist while something is + * actually subscribed. + */ +const clock = readable(Date.now(), (set) => { + if (!browser) return; - const now = new Date(); - const expires = new Date($license.expiresAt); - const diffTime = expires.getTime() - now.getTime(); - const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); + const read = () => set(Date.now()); + const onVisible = () => { + if (!document.hidden) read(); + }; + + document.addEventListener('visibilitychange', onVisible); + window.addEventListener('focus', read); - return diffDays; + return () => { + document.removeEventListener('visibilitychange', onVisible); + window.removeEventListener('focus', read); + }; }); + +/** Milliseconds until the licence expires; negative once it has. Null when unlicensed. */ +export const msUntilExpiry = derived([licenseStore, clock], ([$license, $clock]) => { + // Keyed on the date being there rather than on the licence still being valid: the + // countdown has to survive the moment it reaches zero, or the warning vanishes + // exactly when it becomes true. + if (!$license.expiresAt) return null; + return new Date($license.expiresAt).getTime() - $clock; +}); + +/** Whole days left, used to decide whether to warn and how loudly. */ +export const daysUntilExpiry = derived(msUntilExpiry, ($ms) => + $ms === null ? null : daysLeft($ms) +); + +/** What the header says about the time left, or null when there is nothing to say. */ +export const expiryMessage = derived([licenseStore, msUntilExpiry], ([$license, $ms]) => + licenceHeaderText({ + hasEnterpriseLicense: $license.hasEnterpriseLicense, + isEnterprise: $license.isEnterprise, + msRemaining: $ms, + problem: $license.problem + }) +); diff --git a/src/lib/utils/licence-hold.ts b/src/lib/utils/licence-hold.ts new file mode 100644 index 000000000..5625dd00b --- /dev/null +++ b/src/lib/utils/licence-hold.ts @@ -0,0 +1,49 @@ +/** + * Whether a lapsed licence is what is holding this account back. + * + * When an enterprise licence stops validating, the roles it configured keep their + * shape but nobody except an administrator may act on them, so every screen answers + * the same refusal at once. Read one screen at a time that looks like a broken + * application: an empty table, a spinner that never resolves, a failed request. Said + * once, at the top, it is a sentence anybody can act on. + * + * Pure, so the rule is a unit test rather than something only an expiry reveals. + */ + +export interface LicenceHoldInput { + /** An enterprise licence is installed, whether or not it still validates. */ + hasEnterpriseLicense: boolean; + /** Whether that licence currently validates. */ + isEnterprise: boolean; + /** Whether the signed-in account is an administrator. */ + isAdmin: boolean; + /** Whether anybody is signed in at all. */ + isAuthenticated: boolean; + /** Whether both the licence and the account are still being read. */ + loading?: boolean; +} + +/** + * True when this account is refused everywhere because the licence lapsed. + * + * Deliberately narrow. An administrator still works and needs no banner - they are + * the one who fixes it. Nobody signed in has a login page to look at instead, and an + * instance that never had an enterprise licence is not on hold, it is simply free. + */ +export function heldByLapsedLicence(state: LicenceHoldInput): boolean { + // The licence and the account are read in two separate requests. Answering before + // both are back would tell an administrator, for as long as the slower request + // takes, that they cannot make changes - which is both wrong and the opposite of + // what they need to see. + if (state.loading) return false; + if (!state.isAuthenticated) return false; + if (state.isAdmin) return false; + // Installed but no longer validating is exactly the hold; never installed is not. + return state.hasEnterpriseLicense && !state.isEnterprise; +} + +/** What to tell somebody who is held, or null when they are not. */ +export function licenceHoldMessage(state: LicenceHoldInput): string | null { + if (!heldByLapsedLicence(state)) return null; + return 'This instance needs its licence renewed. Until then only administrators can make changes, so pages will look empty.'; +} diff --git a/src/lib/utils/license-expiry-text.ts b/src/lib/utils/license-expiry-text.ts new file mode 100644 index 000000000..3f376baba --- /dev/null +++ b/src/lib/utils/license-expiry-text.ts @@ -0,0 +1,130 @@ +/** + * How long is left on a licence, in the words the header uses. + * + * Counting whole days rounds the last day up, so an hour left and a full day left read + * the same. That is the difference between "I will renew tomorrow" and losing + * enterprise features during the afternoon, so the final day counts down in hours and + * the final hour in minutes. + */ + +const MINUTE = 60 * 1000; +const HOUR = 60 * MINUTE; +const DAY = 24 * HOUR; + +/** Whole days left, for deciding whether to warn at all and how loudly. */ +export function daysLeft(msRemaining: number): number { + return Math.ceil(msRemaining / DAY); +} + +/** + * What the header says. Null when there is nothing to announce. + * + * `msRemaining` is expiry minus now, so a past licence is negative. + */ +export function expiryText(msRemaining: number): string { + if (msRemaining <= 0) return 'License expired'; + + if (msRemaining < HOUR) { + const minutes = Math.max(1, Math.round(msRemaining / MINUTE)); + return `License expires in ${minutes} ${plural(minutes, 'minute')}`; + } + + if (msRemaining < DAY) { + const hours = Math.max(1, Math.floor(msRemaining / HOUR)); + return `License expires in ${hours} ${plural(hours, 'hour')}`; + } + + // Past the first full day, whole days read better than "in 47 hours". + const days = Math.floor(msRemaining / DAY); + if (days === 1) return 'License expires tomorrow'; + return `License expires in ${days} days`; +} + +function plural(n: number, word: string): string { + return n === 1 ? word : `${word}s`; +} + +/** + * Whether the header should count down at all. + * + * A licence can stop validating for reasons that have nothing to do with time - moved + * to a new host, key replaced - and the payload still carries a future date. Counting + * down then promises days that do not exist and names the wrong remedy, so the header + * stays quiet and leaves the explanation to whoever knows the real reason. Once the + * date itself has passed, saying so is right however the licence failed. + */ +export function shouldShowExpiryWarning(state: { + hasEnterpriseLicense: boolean; + isEnterprise: boolean; + msRemaining: number | null; +}): boolean { + if (!state.hasEnterpriseLicense) return false; + // A licence that fails for a reason other than time still needs saying, and the + // reason carries it - there may be no date to count down at all. + if (!state.isEnterprise) return true; + if (state.msRemaining === null) return false; + return daysLeft(state.msRemaining) <= 30; +} + +/** + * What the header says about a licence, or null when there is nothing to say. + * + * A licence can fail for reasons that have nothing to do with time - moved to another + * host, key replaced - and its payload still carries a future date. Counting down + * there would promise days that do not exist and point at the wrong remedy, so the + * server's own reason is shown instead. It is what the person who can fix it needs. + */ +export function licenceHeaderText(state: { + hasEnterpriseLicense: boolean; + isEnterprise: boolean; + msRemaining: number | null; + problem: string | null; +}): string | null { + if (!shouldShowExpiryWarning(state)) return null; + + // Expiry is the one failure the countdown already words well. + if (state.isEnterprise || (state.msRemaining !== null && state.msRemaining <= 0)) { + return state.msRemaining === null ? null : expiryText(state.msRemaining); + } + + return state.problem ? `License problem: ${state.problem}` : 'License is not valid'; +} + +/** What the licence endpoint says, reduced to the flags the interface reasons about. */ +export interface LicenceFlags { + /** The licence validates right now, so the paid features are available. */ + isEnterprise: boolean; + /** An enterprise licence is installed, whether or not it still validates. */ + hasEnterpriseLicense: boolean; + isLicensed: boolean; + licenseType: string | null; + expiresAt: string | null; + /** Why the licence does not validate, as the server put it. Null when it does. */ + problem: string | null; +} + +/** + * Read the licence endpoint's answer. + * + * `isEnterprise` gates the paid features and so must go false the moment a licence + * stops validating. `hasEnterpriseLicense` answers a different question - is one + * installed at all - and has to survive that, or the warnings about it disappear + * exactly when they start being true. + */ +export function deriveLicenceFlags(response: { + valid?: boolean; + active?: boolean; + error?: string | null; + payload?: { type?: string; expires?: string | null } | null; +}): LicenceFlags { + const isValid = !!(response.valid && response.active); + const licenseType = response.payload?.type ?? null; + return { + isEnterprise: isValid && licenseType === 'enterprise', + hasEnterpriseLicense: licenseType === 'enterprise', + isLicensed: isValid, + licenseType: isValid ? licenseType : null, + expiresAt: response.payload?.expires ?? null, + problem: isValid ? null : (response.error ?? null) + }; +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index fb5056fed..b73d13840 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -15,7 +15,8 @@ import { SidebarProvider, SidebarTrigger } from '$lib/components/ui/sidebar'; import { connectSSE, disconnectSSE } from '$lib/stores/events'; import { currentEnvironment, environments } from '$lib/stores/environment'; - import { licenseStore, daysUntilExpiry } from '$lib/stores/license'; + import { licenseStore, daysUntilExpiry, expiryMessage } from '$lib/stores/license'; + import { licenceHoldMessage } from '$lib/utils/licence-hold'; import { get } from 'svelte/store'; import { authStore } from '$lib/stores/auth'; import { extractPath, isSessionExpiryCandidate } from '$lib/utils/session-expiry'; @@ -35,6 +36,19 @@ let { children, data } = $props(); let envId = $state(null); let commandPaletteOpen = $state(false); + // Said once, at the top: a lapsed licence refuses every read, so each page would + // otherwise report it separately as an empty table or a request that failed. + const licenceHold = $derived( + licenceHoldMessage({ + hasEnterpriseLicense: $licenseStore.hasEnterpriseLicense, + isEnterprise: $licenseStore.isEnterprise, + isAdmin: $authStore.user?.isAdmin ?? false, + isAuthenticated: $authStore.authEnabled ? !!$authStore.user : false, + loading: $licenseStore.loading || $authStore.loading + }) + ); + + // What's New modal state let showWhatsNewModal = $state(false); @@ -85,8 +99,10 @@ // Connect to SSE for real-time Docker events (global) connectSSE(envId); - // Check enterprise license status + // Check enterprise license status, and again whenever somebody comes back to + // the page - a licence activated elsewhere is only visible after asking again. licenseStore.check(); + const stopWatchingLicense = licenseStore.watchForChanges(); // Check auth status authStore.check(); @@ -130,6 +146,7 @@ return () => { window.fetch = originalFetch; disconnectSSE(); + stopWatchingLicense(); }; }); @@ -206,28 +223,30 @@ K - {#if $licenseStore.isEnterprise && $daysUntilExpiry !== null && $daysUntilExpiry <= 30} + {#if $expiryMessage} - {#if $daysUntilExpiry <= 0} - License expired - {:else if $daysUntilExpiry === 1} - License expires tomorrow - {:else} - License expires in {$daysUntilExpiry} days - {/if} + {$expiryMessage} {/if}
+ {#if licenceHold} + +
+ +

{licenceHold}

+
+ {/if} {@render children?.()}
diff --git a/src/routes/api/audit/+server.ts b/src/routes/api/audit/+server.ts index 04cc7633d..d6d687e3a 100644 --- a/src/routes/api/audit/+server.ts +++ b/src/routes/api/audit/+server.ts @@ -26,7 +26,7 @@ export const GET: RequestHandler = async ({ url, cookies }) => { const auth = await authorize(cookies); // Audit log is Enterprise-only - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json(enterpriseRequired(), { status: 403 }); } diff --git a/src/routes/api/audit/events/+server.ts b/src/routes/api/audit/events/+server.ts index 9e348f00a..2c1020a2f 100644 --- a/src/routes/api/audit/events/+server.ts +++ b/src/routes/api/audit/events/+server.ts @@ -12,7 +12,7 @@ export const GET: RequestHandler = async ({ cookies }) => { const auth = await authorize(cookies); // Audit log is Enterprise-only - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return new Response(JSON.stringify(enterpriseRequired()), { status: 403, headers: { 'Content-Type': 'application/json' } diff --git a/src/routes/api/audit/export/+server.ts b/src/routes/api/audit/export/+server.ts index dee9849dd..0d3d6552b 100644 --- a/src/routes/api/audit/export/+server.ts +++ b/src/routes/api/audit/export/+server.ts @@ -102,7 +102,7 @@ export const GET: RequestHandler = async ({ url, cookies }) => { const auth = await authorize(cookies); // Audit log is Enterprise-only - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return new Response(JSON.stringify(enterpriseRequired()), { status: 403, headers: { 'Content-Type': 'application/json' } diff --git a/src/routes/api/audit/users/+server.ts b/src/routes/api/audit/users/+server.ts index 9782f4555..ad229a2f9 100644 --- a/src/routes/api/audit/users/+server.ts +++ b/src/routes/api/audit/users/+server.ts @@ -15,7 +15,7 @@ export const GET: RequestHandler = async ({ cookies }) => { const auth = await authorize(cookies); // Audit log is Enterprise-only - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json(enterpriseRequired(), { status: 403 }); } diff --git a/src/routes/api/auth/ldap/+server.ts b/src/routes/api/auth/ldap/+server.ts index 763e5c963..f5a694f61 100644 --- a/src/routes/api/auth/ldap/+server.ts +++ b/src/routes/api/auth/ldap/+server.ts @@ -22,7 +22,7 @@ export const GET: RequestHandler = async ({ cookies }) => { return json({ error: 'Unauthorized' }, { status: 401 }); } - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json({ error: 'Enterprise license required' }, { status: 403 }); } @@ -62,7 +62,7 @@ export const POST: RequestHandler = async (event) => { return json({ error: 'Unauthorized' }, { status: 401 }); } - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json({ error: 'Enterprise license required' }, { status: 403 }); } diff --git a/src/routes/api/auth/ldap/[id]/+server.ts b/src/routes/api/auth/ldap/[id]/+server.ts index 1a19786b7..3c80ff7ce 100644 --- a/src/routes/api/auth/ldap/[id]/+server.ts +++ b/src/routes/api/auth/ldap/[id]/+server.ts @@ -25,7 +25,7 @@ export const GET: RequestHandler = async ({ params, cookies }) => { return json({ error: 'Unauthorized' }, { status: 401 }); } - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json({ error: 'Enterprise license required' }, { status: 403 }); } @@ -73,7 +73,7 @@ export const PUT: RequestHandler = async (event) => { return json({ error: 'Unauthorized' }, { status: 401 }); } - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json({ error: 'Enterprise license required' }, { status: 403 }); } @@ -155,7 +155,7 @@ export const DELETE: RequestHandler = async (event) => { return json({ error: 'Unauthorized' }, { status: 401 }); } - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json({ error: 'Enterprise license required' }, { status: 403 }); } diff --git a/src/routes/api/auth/ldap/[id]/test/+server.ts b/src/routes/api/auth/ldap/[id]/test/+server.ts index 798a8542b..d2a29c7c0 100644 --- a/src/routes/api/auth/ldap/[id]/test/+server.ts +++ b/src/routes/api/auth/ldap/[id]/test/+server.ts @@ -24,7 +24,7 @@ export const POST: RequestHandler = async ({ params, cookies }) => { return json({ error: 'Unauthorized' }, { status: 401 }); } - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json({ error: 'Enterprise license required' }, { status: 403 }); } diff --git a/src/routes/api/license/+server.ts b/src/routes/api/license/+server.ts index aab253ba6..3d4130ed0 100644 --- a/src/routes/api/license/+server.ts +++ b/src/routes/api/license/+server.ts @@ -10,10 +10,10 @@ import { authorize } from '$lib/server/authorize'; import { clearTokenCache } from '$lib/server/api-tokens'; // GET /api/license - Get current license status -// Any authenticated user can view license status (needed to determine if RBAC applies) /** * @openapi * summary: Get the current license status plus the server hostname the license is bound to + * description: Any authenticated caller sees whether a license is active and who it is issued to, because the UI decides what to offer from that. The key itself is included only for a caller holding license:manage. * resp-200: The license status object merged with the current hostname * resp-401: Authentication required (auth is enabled and the caller is not authenticated) * resp-500: Failed to read the license status @@ -25,11 +25,22 @@ export const GET: RequestHandler = async ({ cookies }) => { } try { - const status = await getLicenseStatus(); + const { stored, ...status } = await getLicenseStatus(); const hostname = getHostname(); + // `stored.key` activates this license on any other host, so it travels only to + // whoever may manage licensing. The name stays: the UI shows who it is issued + // to, and that is already in the payload's own validated half. + const maySeeKey = !auth.authEnabled || (await auth.can('license', 'manage')); + const visibleStored = stored + ? maySeeKey + ? stored + : { name: stored.name, activated_at: stored.activated_at } + : undefined; + return json({ ...status, + ...(visibleStored ? { stored: visibleStored } : {}), hostname }); } catch (error) { diff --git a/src/routes/audit/+server.ts b/src/routes/audit/+server.ts index c86182343..134c38660 100644 --- a/src/routes/audit/+server.ts +++ b/src/routes/audit/+server.ts @@ -22,7 +22,7 @@ export const GET: RequestHandler = async ({ url, cookies }) => { const auth = await authorize(cookies); // Audit log is Enterprise-only - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json(enterpriseRequired(), { status: 403 }); } diff --git a/src/routes/audit/users/+server.ts b/src/routes/audit/users/+server.ts index 0a9be5285..f11ae924c 100644 --- a/src/routes/audit/users/+server.ts +++ b/src/routes/audit/users/+server.ts @@ -14,7 +14,7 @@ export const GET: RequestHandler = async ({ cookies }) => { const auth = await authorize(cookies); // Audit log is Enterprise-only - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json(enterpriseRequired(), { status: 403 }); } diff --git a/src/routes/settings/license/LicenseTab.svelte b/src/routes/settings/license/LicenseTab.svelte index c50a0e4b8..73cce0215 100644 --- a/src/routes/settings/license/LicenseTab.svelte +++ b/src/routes/settings/license/LicenseTab.svelte @@ -8,6 +8,7 @@ import { Crown, Building2, Key, RefreshCw, ShieldCheck, XCircle } from 'lucide-svelte'; import { canAccess } from '$lib/stores/auth'; import { licenseStore } from '$lib/stores/license'; + import ConfirmPopover from '$lib/components/ConfirmPopover.svelte'; import { formatDate } from '$lib/stores/settings'; // License state @@ -183,10 +184,28 @@ {#if $canAccess('settings', 'edit')}
- + + {#snippet children({ open })} + + + Deactivate license + + {/snippet} + {#snippet extraContent()} +

+ Roles stop being enforced, so everyone who can sign in gets full access + until a valid license is installed. +

+ {/snippet} +
{/if} diff --git a/tests/licence-hold.test.ts b/tests/licence-hold.test.ts new file mode 100644 index 000000000..aad77572e --- /dev/null +++ b/tests/licence-hold.test.ts @@ -0,0 +1,66 @@ +import { describe, expect, test } from 'bun:test'; +import { heldByLapsedLicence, licenceHoldMessage } from '../src/lib/utils/licence-hold'; + +/** + * Who is told that a lapsed licence, rather than their own permissions, is what is + * stopping them. + * + * A licence that stops validating refuses every read for everybody but an + * administrator, so without one sentence at the top the whole application reads as + * broken: empty tables, spinners that never resolve, requests that failed. + */ + +const viewerOnHold = { + hasEnterpriseLicense: true, + isEnterprise: false, + isAdmin: false, + isAuthenticated: true +}; + +describe('an account that is held', () => { + test('a non-admin on an instance whose licence stopped validating', () => { + expect(heldByLapsedLicence(viewerOnHold)).toBe(true); + }); + + test('and is told which of the two it is', () => { + // "Permission denied" everywhere is true but useless: nothing they can do + // about their own role will change it. + // Asserted exactly: a loose match passes for a message saying the opposite, + // and a banner that reassures somebody who is locked out is worse than none. + expect(licenceHoldMessage(viewerOnHold)).toBe( + 'This instance needs its licence renewed. Until then only administrators can make changes, so pages will look empty.' + ); + }); +}); + +describe('an account that is not', () => { + test('an administrator, who is the one who fixes it', () => { + expect(heldByLapsedLicence({ ...viewerOnHold, isAdmin: true })).toBe(false); + expect(licenceHoldMessage({ ...viewerOnHold, isAdmin: true })).toBeNull(); + }); + + test('anybody on a licence that still validates', () => { + expect(heldByLapsedLicence({ ...viewerOnHold, isEnterprise: true })).toBe(false); + }); + + test('an instance that never had an enterprise licence', () => { + // The free edition is not on hold; it simply never enforced roles. + expect( + heldByLapsedLicence({ ...viewerOnHold, hasEnterpriseLicense: false }) + ).toBe(false); + }); + + test('nobody signed in, who is looking at a login page instead', () => { + expect(heldByLapsedLicence({ ...viewerOnHold, isAuthenticated: false })).toBe(false); + }); + + test('anybody, while the licence and the account are still being read', () => { + // They arrive in two separate requests. An administrator whose licence answers + // first would otherwise be told, until the slower one lands, that they cannot + // make changes - the opposite of what they need to see. + expect(heldByLapsedLicence({ ...viewerOnHold, loading: true })).toBe(false); + expect( + heldByLapsedLicence({ ...viewerOnHold, isAdmin: true, loading: true }) + ).toBe(false); + }); +}); diff --git a/tests/license-expiry-text.test.ts b/tests/license-expiry-text.test.ts new file mode 100644 index 000000000..fb9574082 --- /dev/null +++ b/tests/license-expiry-text.test.ts @@ -0,0 +1,237 @@ +import { describe, expect, test } from 'bun:test'; +import { + expiryText, + daysLeft, + shouldShowExpiryWarning, + deriveLicenceFlags, + licenceHeaderText +} from '../src/lib/utils/license-expiry-text'; + +const MINUTE = 60 * 1000; +const HOUR = 60 * MINUTE; +const DAY = 24 * HOUR; + +/** + * What the header tells somebody about the time they have left. + * + * Counting whole days rounds the last one up, so an hour and a full day read the same. + * An administrator who reads "tomorrow" and plans to renew in the morning loses + * enterprise features that afternoon, so the final day has to count down in real units. + */ + +describe('the last day, where the wording decides whether somebody acts', () => { + test('minutes left says minutes, not tomorrow', () => { + expect(expiryText(10 * MINUTE)).toBe('License expires in 10 minutes'); + expect(expiryText(1 * MINUTE)).toBe('License expires in 1 minute'); + }); + + test('under a minute still reads as a minute rather than zero', () => { + // Rounding to 0 would say "expires in 0 minutes" on a licence that is still live. + expect(expiryText(20 * 1000)).toBe('License expires in 1 minute'); + }); + + test('hours left says hours', () => { + expect(expiryText(3 * HOUR)).toBe('License expires in 3 hours'); + expect(expiryText(1 * HOUR)).toBe('License expires in 1 hour'); + }); + + test('most of a day left is still hours, not tomorrow', () => { + expect(expiryText(23 * HOUR)).toBe('License expires in 23 hours'); + }); + + test('a part hour is not rounded up into time that is not there', () => { + // Reporting more than is left is the failure this wording exists to remove. + expect(expiryText(90 * MINUTE)).toBe('License expires in 1 hour'); + expect(expiryText(23 * HOUR + 59 * MINUTE)).toBe('License expires in 23 hours'); + }); +}); + +describe('beyond the first day, whole days read better', () => { + test('a day and a bit is tomorrow', () => { + expect(expiryText(DAY + HOUR)).toBe('License expires tomorrow'); + expect(expiryText(DAY)).toBe('License expires tomorrow'); + }); + + test('longer is counted in days', () => { + expect(expiryText(2 * DAY)).toBe('License expires in 2 days'); + expect(expiryText(30 * DAY)).toBe('License expires in 30 days'); + }); +}); + +describe('a licence that has run out', () => { + test('says so plainly', () => { + expect(expiryText(0)).toBe('License expired'); + expect(expiryText(-5 * MINUTE)).toBe('License expired'); + expect(expiryText(-10 * DAY)).toBe('License expired'); + }); +}); + +describe('the day count that decides whether to warn at all', () => { + test('the last day counts as one, so the banner still shows', () => { + // The header only renders at 30 days or fewer; rounding the final day down to + // zero would hide the warning exactly when it matters most. + expect(daysLeft(10 * MINUTE)).toBe(1); + expect(daysLeft(23 * HOUR)).toBe(1); + }); + + test('an expired licence is not a positive number of days', () => { + expect(daysLeft(-1 * MINUTE)).toBeLessThanOrEqual(0); + }); + + test('whole days beyond the first', () => { + expect(daysLeft(2 * DAY)).toBe(2); + expect(daysLeft(30 * DAY)).toBe(30); + }); +}); + +describe('whether the header counts down at all', () => { + const ent = { hasEnterpriseLicense: true, isEnterprise: true }; + + test('a valid licence inside the warning window', () => { + expect(shouldShowExpiryWarning({ ...ent, msRemaining: 10 * DAY })).toBe(true); + }); + + test('nothing to say while there is plenty of time left', () => { + expect(shouldShowExpiryWarning({ ...ent, msRemaining: 90 * DAY })).toBe(false); + }); + + test('an expired licence still says so', () => { + expect( + shouldShowExpiryWarning({ hasEnterpriseLicense: true, isEnterprise: false, msRemaining: -2 * DAY }) + ).toBe(true); + }); + + test('a licence that fails for some other reason still warrants a warning', () => { + // Moved to a new host, key replaced: it is not working, and the person who can + // fix it needs to know. What it SAYS is the reason rather than a countdown - + // see licenceHeaderText below. + expect( + shouldShowExpiryWarning({ + hasEnterpriseLicense: true, + isEnterprise: false, + msRemaining: 10 * DAY + }) + ).toBe(true); + }); + + test('an instance with no enterprise licence has no countdown', () => { + expect( + shouldShowExpiryWarning({ hasEnterpriseLicense: false, isEnterprise: false, msRemaining: 5 * DAY }) + ).toBe(false); + }); + + test('no expiry date at all', () => { + expect(shouldShowExpiryWarning({ ...ent, msRemaining: null })).toBe(false); + }); +}); + +describe('reading the licence endpoint', () => { + const future = '2099-01-01T00:00:00Z'; + + test('a licence that validates', () => { + const f = deriveLicenceFlags({ valid: true, active: true, payload: { type: 'enterprise', expires: future } }); + expect(f.isEnterprise).toBe(true); + expect(f.hasEnterpriseLicense).toBe(true); + }); + + test('an expired one is still an enterprise licence that is installed', () => { + // The two answer different questions: one gates the paid features, the other + // decides whether there is anything to warn about at all. + const f = deriveLicenceFlags({ valid: false, active: false, payload: { type: 'enterprise', expires: '2020-01-01T00:00:00Z' } }); + expect(f.isEnterprise).toBe(false); + expect(f.hasEnterpriseLicense).toBe(true); + expect(f.expiresAt).toBe('2020-01-01T00:00:00Z'); + }); + + test('the reason a licence fails is carried through', () => { + // It is the only thing that tells the header which failure this is, and the + // only actionable half of the message. + const f = deriveLicenceFlags({ + valid: false, + active: false, + error: 'License is not valid for this host (new.example.com)', + payload: { type: 'enterprise', expires: future } + }); + expect(f.problem).toBe('License is not valid for this host (new.example.com)'); + }); + + test('a working licence has no problem to report', () => { + const f = deriveLicenceFlags({ valid: true, active: true, error: null, payload: { type: 'enterprise', expires: future } }); + expect(f.problem).toBeNull(); + }); + + test('one refused for its host keeps its future date', () => { + // Moving the instance leaves a licence that fails while its date is still ahead. + const f = deriveLicenceFlags({ valid: false, active: false, payload: { type: 'enterprise', expires: future } }); + expect(f.isEnterprise).toBe(false); + expect(f.hasEnterpriseLicense).toBe(true); + expect(f.expiresAt).toBe(future); + }); + + test('an SMB licence is not an enterprise one', () => { + const f = deriveLicenceFlags({ valid: true, active: true, payload: { type: 'smb', expires: future } }); + expect(f.isEnterprise).toBe(false); + expect(f.hasEnterpriseLicense).toBe(false); + expect(f.isLicensed).toBe(true); + }); + + test('no licence at all', () => { + const f = deriveLicenceFlags({ valid: false, active: false }); + expect(f).toEqual({ + isEnterprise: false, + hasEnterpriseLicense: false, + isLicensed: false, + licenseType: null, + expiresAt: null, + problem: null + }); + }); +}); + +describe('what the header says about a licence', () => { + const ent = { hasEnterpriseLicense: true, isEnterprise: true, problem: null }; + + test('a working licence counts down inside the window', () => { + expect(licenceHeaderText({ ...ent, msRemaining: 10 * DAY })).toBe('License expires in 10 days'); + }); + + test('and says nothing while there is plenty of time', () => { + expect(licenceHeaderText({ ...ent, msRemaining: 90 * DAY })).toBeNull(); + }); + + test('an expired one says so', () => { + expect( + licenceHeaderText({ hasEnterpriseLicense: true, isEnterprise: false, msRemaining: -2 * DAY, problem: 'License has expired' }) + ).toBe('License expired'); + }); + + test('a licence refused for its host names the real reason', () => { + // The date is still ahead, so counting down would promise days that do not + // exist and send somebody to renew a licence that does not need renewing. + expect( + licenceHeaderText({ + hasEnterpriseLicense: true, + isEnterprise: false, + msRemaining: 10 * DAY, + problem: 'License is not valid for this host (new.example.com)' + }) + ).toBe('License problem: License is not valid for this host (new.example.com)'); + }); + + test('a licence that fails with no reason given still says something', () => { + // Silence would leave the one person who can fix it with no signal at all. + expect( + licenceHeaderText({ hasEnterpriseLicense: true, isEnterprise: false, msRemaining: 10 * DAY, problem: null }) + ).toBe('License is not valid'); + }); + + test('a perpetual licence that works has nothing to report', () => { + expect(licenceHeaderText({ ...ent, msRemaining: null })).toBeNull(); + }); + + test('an instance with no enterprise licence is silent', () => { + expect( + licenceHeaderText({ hasEnterpriseLicense: false, isEnterprise: false, msRemaining: null, problem: null }) + ).toBeNull(); + }); +}); diff --git a/tests/license-state-core.test.ts b/tests/license-state-core.test.ts new file mode 100644 index 000000000..0fecb363f --- /dev/null +++ b/tests/license-state-core.test.ts @@ -0,0 +1,171 @@ +import { describe, expect, test } from 'bun:test'; +import { + enforcesRoles, + licenseValid, + lapsed, + nonAdminMayAct, + type LicenseVerdict +} from '../src/lib/server/license-state-core'; + +// The shapes validateLicense really returns. It hands back the decoded payload even +// when it rejects the key, but only once the RSA signature has passed - which is what +// lets a lapse be told apart from a forgery. +const noLicense = null; +const enterpriseValid: LicenseVerdict = { valid: true, active: true, payload: { type: 'enterprise' } }; +const enterpriseExpired: LicenseVerdict = { valid: false, active: false, payload: { type: 'enterprise' } }; +const enterpriseWrongHost: LicenseVerdict = { valid: false, active: false, payload: { type: 'enterprise' } }; +const smbValid: LicenseVerdict = { valid: true, active: true, payload: { type: 'smb' } }; +const smbExpired: LicenseVerdict = { valid: false, active: false, payload: { type: 'smb' } }; +const badSignature: LicenseVerdict = { valid: false, active: false }; + +describe('roles keep being enforced', () => { + // The point of the split: an expiry must not turn authorization off. Dozens of + // endpoints read this flag to decide environment access, so dropping it would + // switch every one of those checks off at the same moment. + test('while the license is valid', () => { + expect(enforcesRoles(enterpriseValid)).toBe(true); + }); + + test('and after it expires', () => { + expect(enforcesRoles(enterpriseExpired)).toBe(true); + }); + + test('and after the host is renamed', () => { + expect(enforcesRoles(enterpriseWrongHost)).toBe(true); + }); + + test('but not on an instance that never had a license', () => { + expect(enforcesRoles(noLicense)).toBe(false); + expect(enforcesRoles(undefined)).toBe(false); + }); + + test('and not on SMB, which never unlocked roles', () => { + expect(enforcesRoles(smbValid)).toBe(false); + expect(enforcesRoles(smbExpired)).toBe(false); + }); + + test('and not on a key that fails its signature', () => { + expect(enforcesRoles(badSignature)).toBe(false); + }); +}); + +describe('paid features follow the license, not the roles', () => { + test('available while it validates', () => { + expect(licenseValid(enterpriseValid)).toBe(true); + }); + + test('withdrawn once it expires', () => { + // LDAP, the audit log and role editing stop; enforcement above does not. + expect(licenseValid(enterpriseExpired)).toBe(false); + }); + + test('withdrawn when the host no longer matches', () => { + expect(licenseValid(enterpriseWrongHost)).toBe(false); + }); + + test('never available without a license', () => { + expect(licenseValid(noLicense)).toBe(false); + }); + + test('and an SMB license does not buy them', () => { + expect(licenseValid(smbValid)).toBe(false); + }); +}); + +describe('who may act once a license has lapsed', () => { + test('a lapse is an enterprise key that stopped validating', () => { + expect(lapsed(enterpriseExpired)).toBe(true); + expect(lapsed(enterpriseWrongHost)).toBe(true); + }); + + test('a working license is not a lapse', () => { + expect(lapsed(enterpriseValid)).toBe(false); + }); + + test('nor is an instance that never had one', () => { + expect(lapsed(noLicense)).toBe(false); + expect(lapsed(undefined)).toBe(false); + }); + + test('nor an expired SMB license', () => { + expect(lapsed(smbExpired)).toBe(false); + }); + + test('nor a key that cannot be verified', () => { + // Locking the instance out on an unverifiable string would let anyone able to + // write the setting deny service to everybody. + expect(lapsed(badSignature)).toBe(false); + }); + + test('a non-admin is held during a lapse', () => { + // The defect this closes: an expiry used to grant everyone full access. + expect(nonAdminMayAct(enterpriseExpired)).toBe(false); + expect(nonAdminMayAct(enterpriseWrongHost)).toBe(false); + }); + + test('and is not held in any other state', () => { + expect(nonAdminMayAct(enterpriseValid)).toBe(true); + expect(nonAdminMayAct(noLicense)).toBe(true); + expect(nonAdminMayAct(smbExpired)).toBe(true); + expect(nonAdminMayAct(badSignature)).toBe(true); + }); +}); + +describe('every paid feature is gated on the license, not on the roles', () => { + // The trade this design makes: isEnterprise stays true through a lapse, so anything + // that used it to decide whether a feature is SOLD has to move to hasValidLicense. + // Missing one leaves a paid feature working on an expired license - which is how the + // second copy of the audit routes under src/routes/audit/ was found. + const { readFileSync, readdirSync, statSync } = require('node:fs') as typeof import('node:fs'); + + function serverFiles(dir: string, acc: string[] = []): string[] { + for (const entry of readdirSync(dir)) { + const full = `${dir}/${entry}`; + if (statSync(full).isDirectory()) serverFiles(full, acc); + else if (entry === '+server.ts') acc.push(full); + } + return acc; + } + + // Reads of isEnterprise that are enforcement, not selling: they scope access to an + // environment, or decide whether roles exist at all, and must survive a lapse. + const ENFORCEMENT = [ + 'src/routes/api/roles/+server.ts', // GET: an admin still sees the roles + 'src/routes/api/roles/[id]/+server.ts', + 'src/routes/api/users/+server.ts' // free edition grants Admin to new users + ]; + + test('no handler sells a feature off auth.isEnterprise', () => { + const offenders: string[] = []; + for (const file of serverFiles(new URL('../src/routes', import.meta.url).pathname)) { + const rel = file.slice(file.indexOf('src/routes')); + if (ENFORCEMENT.includes(rel)) continue; + const src = readFileSync(file, 'utf8'); + // The shape that denies outright is the one that sells. + if (/if \(!auth\.isEnterprise\) \{/.test(src)) offenders.push(rel); + } + expect(offenders).toEqual([]); + }); +}); + +describe('the three answers together', () => { + // Read as a table, because the value of the split is in how the rows differ. + const rows: [string, LicenseVerdict | null, boolean, boolean, boolean][] = [ + // state verdict enforces valid lapsed + ['no license', noLicense, false, false, false], + ['valid enterprise', enterpriseValid, true, true, false], + ['expired enterprise', enterpriseExpired, true, false, true], + ['enterprise, wrong host', enterpriseWrongHost, true, false, true], + ['valid SMB', smbValid, false, false, false], + ['expired SMB', smbExpired, false, false, false], + ['unverifiable key', badSignature, false, false, false] + ]; + + for (const [name, verdict, roles, paid, held] of rows) { + test(name, () => { + expect(enforcesRoles(verdict)).toBe(roles); + expect(licenseValid(verdict)).toBe(paid); + expect(lapsed(verdict)).toBe(held); + }); + } +}); From af0ef52ed6a89f03bbe21fbb16d096bf1bf69db6 Mon Sep 17 00:00:00 2001 From: Jarek Krochmalski Date: Tue, 29 Sep 2026 14:58:14 +0200 Subject: [PATCH 03/22] tighten permission checks and input handling across auth, updates and settings --- src/hooks.server.ts | 27 +- src/lib/server/auth.ts | 336 +++++++++++++----- src/lib/server/general-settings-visibility.ts | 75 ++++ src/lib/server/oidc-callback-core.ts | 129 +++++++ src/lib/server/oidc-claims-core.ts | 168 +++++++++ src/lib/server/oidc-login-state.ts | 206 +++++++++++ src/lib/server/oidc-token-core.ts | 123 +++++++ src/lib/server/own-container-core.ts | 39 ++ src/lib/server/own-container-guard.ts | 36 ++ src/lib/server/public-paths-core.ts | 48 +++ src/lib/server/self-update-network-core.ts | 76 ++++ src/routes/api/auth/oidc/callback/+server.ts | 38 +- src/routes/api/auto-update/+server.ts | 28 +- .../api/environments/detect-socket/+server.ts | 16 +- .../api/networks/[id]/connect/+server.ts | 12 +- .../api/networks/[id]/disconnect/+server.ts | 12 +- src/routes/api/roles/+server.ts | 21 +- src/routes/api/roles/[id]/+server.ts | 22 +- src/routes/api/self-update/+server.ts | 55 +-- src/routes/api/settings/semver/+server.ts | 9 +- src/routes/api/stacks/base-path/+server.ts | 20 +- src/routes/api/stacks/default-path/+server.ts | 13 +- src/routes/api/users/+server.ts | 14 +- src/routes/api/users/[id]/+server.ts | 12 +- src/routes/api/users/[id]/roles/+server.ts | 16 +- src/routes/settings/auth/AuthTab.svelte | 136 ++++--- src/routes/settings/general/GeneralTab.svelte | 26 +- tests/general-settings-visibility.test.ts | 111 ++++++ tests/oidc-callback-core.test.ts | 326 +++++++++++++++++ tests/oidc-callback-wiring.test.ts | 113 ++++++ tests/own-container-core.test.ts | 116 ++++++ tests/public-paths.test.ts | 88 +++++ tests/self-update-network-core.test.ts | 154 ++++++++ tests/settings-visibility-wiring.test.ts | 61 ++++ updater/update.sh | 2 +- 35 files changed, 2423 insertions(+), 261 deletions(-) create mode 100644 src/lib/server/general-settings-visibility.ts create mode 100644 src/lib/server/oidc-callback-core.ts create mode 100644 src/lib/server/oidc-claims-core.ts create mode 100644 src/lib/server/oidc-login-state.ts create mode 100644 src/lib/server/oidc-token-core.ts create mode 100644 src/lib/server/own-container-core.ts create mode 100644 src/lib/server/own-container-guard.ts create mode 100644 src/lib/server/public-paths-core.ts create mode 100644 src/lib/server/self-update-network-core.ts create mode 100644 tests/general-settings-visibility.test.ts create mode 100644 tests/oidc-callback-core.test.ts create mode 100644 tests/oidc-callback-wiring.test.ts create mode 100644 tests/own-container-core.test.ts create mode 100644 tests/public-paths.test.ts create mode 100644 tests/self-update-network-core.test.ts create mode 100644 tests/settings-visibility-wiring.test.ts diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 5317f2160..2a71dcbd2 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -21,6 +21,7 @@ import type { HandleServerError, Handle } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit'; import { startRssTracker, stopRssTracker, rssBeforeOp, rssAfterOp } from '$lib/server/rss-tracker'; import { getClientIp } from '$lib/server/client-ip'; +import { isPublicPath } from '$lib/server/public-paths-core'; import { BACKUPS_ENABLED, API_DOCS_ENABLED } from '$lib/server/features'; // Side-effect import: installs globalThis.__authenticateWsUpgrade and // globalThis.__canAccessEnvForUser used by the raw WS upgrade handlers in @@ -258,32 +259,6 @@ function isBearerRateLimited(ip: string): boolean { return true; } -// Routes that don't require authentication -const PUBLIC_PATHS = [ - '/login', - '/api/auth/login', - '/api/auth/logout', - '/api/auth/session', - '/api/auth/settings', - '/api/auth/providers', - '/api/auth/oidc', - '/api/license', - '/api/changelog', - '/api/dependencies', - '/api/health', - '/api/settings/theme', - '/api/docs' -]; - -// Check if path is public -function isPublicPath(pathname: string): boolean { - // Webhook endpoints have their own auth (signature/secret verification) - if (pathname.match(/^\/api\/git\/stacks\/\d+\/webhook$/)) return true; - if (pathname.match(/^\/api\/git\/webhook\/\d+$/)) return true; - - return PUBLIC_PATHS.some(path => pathname === path || pathname.startsWith(path + '/')); -} - // True only for real static files. API routes are never static, even when a path // ends in an asset-like extension (a route parameter can), so they always go through // the normal request handling below. diff --git a/src/lib/server/auth.ts b/src/lib/server/auth.ts index ee910d94d..a20a4c3cd 100644 --- a/src/lib/server/auth.ts +++ b/src/lib/server/auth.ts @@ -44,10 +44,36 @@ import { import { Client as LdapClient } from 'ldapts'; import { resolveSessionTimeout, cookieMaxAge } from '$lib/utils/session-timeout'; import { escapeLdapFilterValue } from './ldap-filter'; -import { isEnterprise } from './license'; +import { isEnterprise, isLicenseLapsed } from './license'; import { secureRandomBytes } from './crypto-fallback'; import { invalidateTokenCacheForUser } from './token-cache'; +import { encrypt, decrypt, looksLikeCiphertext } from './encryption'; import { oidcDiscoveryUrls, fetchOidcDiscovery } from '$lib/utils/oidc-discovery-url'; +import { createRemoteJWKSet } from 'jose'; +import { verifyIdTokenWithKeys } from './oidc-token-core'; +import { + requireIdToken, + rejectedToken, + applyUserinfo, + describeTokenExchangeFailure, + redactTokenErrorBody +} from './oidc-callback-core'; +import { OIDC_END_SESSION } from './features'; +import { providerKind, oidcProviderName } from './provider-kind-core'; +import { + OIDC_STATE_TTL_SECONDS, + loginStateCookieName, + loginStateCookiesToEvict, + encodeLoginState, + type OidcLoginState +} from './oidc-login-state'; +import { + mergeUserinfo, + claimsGrantAdmin, + identityFromClaims, + rolesFromClaims, + type Claims +} from './oidc-claims-core'; // Session cookie name const SESSION_COOKIE_NAME = 'dockhand_session'; @@ -274,7 +300,7 @@ export async function validateSessionById(sessionId: string): Promise { cookies.delete(SESSION_COOKIE_NAME, { path: '/' }); } +/** + * Where to send somebody so the identity provider ends its session too. + * + * Signing out of Dockhand alone leaves the provider's session open, so the next + * sign-in goes straight back to the same account without asking - which is not what + * anybody means by signing out, and on a shared machine is a real problem (#562, + * #1318). + * + * Split in two on purpose. Reading the provider needs the session cookie, so it has + * to happen first; reaching the provider is network I/O that can hang or throw, so it + * must not stand between the caller and destroying the session. A logout that cannot + * reach the provider still logs the user out. + */ + +/** Which OIDC provider issued this session, read while the session still exists. */ +export async function getSessionOidcProvider(cookies: Cookies): Promise { + if (!OIDC_END_SESSION) return null; + + const sessionId = getSessionIdFromCookies(cookies); + if (!sessionId) return null; + + const session = await dbGetSession(sessionId); + if (!session || providerKind(session.provider) !== 'oidc') return null; + + // A bare `oidc` cannot say which provider, so it gets the local logout alone. + return oidcProviderName(session.provider); +} + +/** + * Where to send the browser to end the session at the provider, or null. + * + * Null when the session named no provider, when that provider is gone or publishes + * no end-session endpoint, or when `OIDC_END_SESSION` is off - one instance-wide env + * flag, not a per-provider switch. Never throws: every failure degrades to the + * local-only logout that has already happened by the time this runs. + */ +export async function getOidcLogoutRedirect( + providerName: string | null, + postLogoutRedirectUri?: string +): Promise { + if (!OIDC_END_SESSION || !providerName) return null; + + try { + const configs = await getOidcConfigs(); + const config = configs.find((c) => c.name === providerName); + if (!config) return null; + + return await buildOidcLogoutUrl(config.id, postLogoutRedirectUri); + } catch (error) { + console.warn('[OIDC] Could not build the provider logout URL:', error); + return null; + } +} + // ============================================ // User Permissions // ============================================ @@ -399,6 +479,13 @@ export async function checkPermission( // Admins (those with Admin role) can do anything if (await userHasAdminRole(user.id)) return true; + // Past this point the caller is not an administrator. A license that stopped + // validating holds them here: the roles still say what they may do, but an expiry + // or a renamed host is an accident, and carrying on as normal would leave an + // instance running on a license nobody is paying for. An administrator got through + // above, so the state can still be fixed. + if (await isLicenseLapsed()) return false; + // If checking within an environment context, get environment-specific permissions if (environmentId !== undefined) { const permissions = await getUserPermissionsForEnvironment(user.id, environmentId); @@ -1092,7 +1179,6 @@ const RATE_LIMIT_LOCKOUT_MS = 15 * 60 * 1000; // 15 minute lockout // Guard against multiple intervals during HMR declare global { var __authRateLimitCleanupInterval: ReturnType | undefined; - var __authOidcStateCleanupInterval: ReturnType | undefined; } // Cleanup expired rate limit entries every 5 minutes (guarded for HMR) @@ -1168,28 +1254,61 @@ export function clearRateLimit(identifier: string): void { // OIDC/SSO Authentication // ============================================ -// In-memory store for OIDC state (nonce, code_verifier) -// In production, consider using Redis or database for multi-instance deployments -const oidcStateStore = new Map(); +// A login in flight is carried by the browser, not held here: see +// oidc-login-state.ts. Anything kept in this process is lost when the callback +// reaches a different one, which is every login on a two-container deployment. -// Clean up expired OIDC states periodically (guarded for HMR) -if (!globalThis.__authOidcStateCleanupInterval) { - globalThis.__authOidcStateCleanupInterval = setInterval(() => { - const now = Date.now(); - for (const [state, data] of oidcStateStore.entries()) { - if (data.expiresAt < now) { - oidcStateStore.delete(state); - } - } - }, 60000); // Every minute +/** When a login-state cookie expires, or null if it cannot be read. */ +function loginStateExpiry(value: string): number | null { + if (!looksLikeCiphertext(value)) return null; + try { + const parsed = JSON.parse(decrypt(value) ?? ''); + return typeof parsed?.expiresAt === 'number' ? parsed.expiresAt : null; + } catch { + return null; + } +} + +/** The cookie name this login's state lives under. */ +export function cookieNameFor(state: string): string { + return loginStateCookieName(state, (v) => createHash('sha256').update(v).digest('hex')); } +/** + * Hand the in-flight login to the browser. + * + * `sameSite: 'lax'` is what lets it survive the redirect back from the provider - a + * stricter cookie is not sent on a cross-site navigation, which is exactly the trip + * this cookie exists to make. Encrypted with the instance key, so the PKCE verifier + * and nonce inside are neither readable nor forgeable by the browser holding them. + */ +export function setLoginStateCookie( + cookies: Cookies, + state: OidcLoginState, + request?: Request +): void { + // A browser may only hold a few unfinished logins. Decided from the cookies THIS + // request carried, so it can never touch another browser's sign-in. + const existing = cookies.getAll().map((c) => ({ + name: c.name, + expiresAt: loginStateExpiry(c.value) + })); + for (const stale of loginStateCookiesToEvict(existing)) { + cookies.delete(stale, { path: '/' }); + } + + cookies.set(cookieNameFor(state.state), encodeLoginState(state, encrypt), { + path: '/', + httpOnly: true, + secure: isSecureContext(request), + sameSite: 'lax', + maxAge: OIDC_STATE_TTL_SECONDS + }); +} + +/** How long to wait on an issuer's discovery document before giving up. */ +const OIDC_DISCOVERY_TIMEOUT_MS = 10_000; + // OIDC Discovery document cache const oidcDiscoveryCache = new Map fetch(url)); + // Bounded: an unreachable issuer that never answers would otherwise hold every + // caller open for as long as the socket lives, and two candidate URLs are tried + // one after the other. + const response = await fetchOidcDiscovery(oidcDiscoveryUrls(issuerUrl), (url) => + fetch(url, { signal: AbortSignal.timeout(OIDC_DISCOVERY_TIMEOUT_MS) }) + ); const document = await response.json() as OidcDiscoveryDocument; @@ -1240,6 +1364,38 @@ async function getOidcDiscovery(issuerUrl: string): Promise>(); + +function getKeyStore(jwksUri: string): ReturnType { + let store = oidcKeyStores.get(jwksUri); + if (!store) { + store = createRemoteJWKSet(new URL(jwksUri)); + oidcKeyStores.set(jwksUri, store); + } + return store; +} + +/** The claims of an id_token, once it has been proven to come from the provider. */ +async function verifyIdToken( + idToken: string, + discovery: OidcDiscoveryDocument, + config: { clientId: string; name: string }, + expectedNonce: string +): Promise { + if (!discovery.jwks_uri) { + throw new Error(`${config.name} publishes no signing keys (jwks_uri), so tokens cannot be verified`); + } + + return verifyIdTokenWithKeys(idToken, getKeyStore(discovery.jwks_uri), { + issuer: discovery.issuer, + clientId: config.clientId, + nonce: expectedNonce + }); +} + /** * Generate PKCE code verifier and challenge */ @@ -1255,7 +1411,7 @@ function generatePkce(): { codeVerifier: string; codeChallenge: string } { export async function buildOidcAuthorizationUrl( configId: number, redirectUrl: string = '/' -): Promise<{ url: string; state: string } | { error: string }> { +): Promise<{ url: string; state: string; loginState: OidcLoginState } | { error: string }> { const config = await getOidcConfig(configId); if (!config || !config.enabled) { return { error: 'OIDC configuration not found or disabled' }; @@ -1269,14 +1425,17 @@ export async function buildOidcAuthorizationUrl( const nonce = secureRandomBytes(16).toString('base64url'); const { codeVerifier, codeChallenge } = generatePkce(); - // Store state for callback verification (expires in 10 minutes) - oidcStateStore.set(state, { + // Handed back for the caller to put in the browser's cookie. Holding it in + // this process instead loses every login that comes back to a different one - + // two containers behind one proxy, or a restart mid-login (#1601). + const loginState: OidcLoginState = { configId, + state, codeVerifier, nonce, redirectUrl, - expiresAt: Date.now() + 600000 - }); + expiresAt: Date.now() + OIDC_STATE_TTL_SECONDS * 1000 + }; // Build authorization URL const params = new URLSearchParams({ @@ -1291,7 +1450,7 @@ export async function buildOidcAuthorizationUrl( }); const authUrl = `${discovery.authorization_endpoint}?${params.toString()}`; - return { url: authUrl, state }; + return { url: authUrl, state, loginState }; } catch (error: any) { const errorMsg = error instanceof Error ? error.message : String(error); console.error('[OIDC] Failed to build authorization URL:', errorMsg); @@ -1304,21 +1463,8 @@ export async function buildOidcAuthorizationUrl( */ export async function handleOidcCallback( code: string, - state: string -): Promise { - // Validate state - const stateData = oidcStateStore.get(state); - if (!stateData) { - return { success: false, error: 'Invalid or expired state' }; - } - - // Remove state immediately to prevent replay - oidcStateStore.delete(state); - - if (stateData.expiresAt < Date.now()) { - return { success: false, error: 'SSO session expired' }; - } - + stateData: OidcLoginState +): Promise { const config = await getOidcConfig(stateData.configId); if (!config || !config.enabled) { return { success: false, error: 'OIDC configuration not found or disabled' }; @@ -1345,11 +1491,11 @@ export async function handleOidcCallback( if (!tokenResponse.ok) { const errorBody = await tokenResponse.text(); - console.error('Token exchange failed:', tokenResponse.status, errorBody); - console.error('Token endpoint:', discovery.token_endpoint); - console.error('Redirect URI:', config.redirectUri); - console.error('Client ID:', config.clientId); - return { success: false, error: `Failed to exchange authorization code: ${errorBody}` }; + // Named and prefixed like every other OIDC line: with more than one provider + // configured, a bare message does not say which one refused. + console.error(`[OIDC] ${config.name} refused the authorization code: HTTP ${tokenResponse.status} ${redactTokenErrorBody(errorBody)}`); + console.error(`[OIDC] ${config.name} token endpoint: ${discovery.token_endpoint}, redirect URI sent: ${config.redirectUri}`); + return { success: false, error: describeTokenExchangeFailure(tokenResponse.status, errorBody) }; } const tokens = await tokenResponse.json() as { @@ -1359,21 +1505,29 @@ export async function handleOidcCallback( expires_in?: number; }; - // Decode and validate ID token (basic validation - in production use a JWT library) - let claims: Record = {}; + // The id_token is the only thing tying this login to the provider. Everything + // downstream - the username, and whether the account becomes an administrator - + // is read out of it, so it is verified before a single claim is trusted: + // signature against the provider's published keys, issuer, audience, expiry, + // and the nonce from the request that started this login. + const present = requireIdToken(tokens); + if (!present.proceed) { + return { success: false, error: present.error }; + } - if (tokens.id_token) { - const idTokenParts = tokens.id_token.split('.'); - if (idTokenParts.length === 3) { - try { - claims = JSON.parse(Buffer.from(idTokenParts[1], 'base64url').toString()); - } catch { - return { success: false, error: 'Invalid ID token' }; - } - } + let claims: Claims; + try { + claims = await verifyIdToken(tokens.id_token!, discovery, config, stateData.nonce); + } catch (error) { + const reason = error instanceof Error ? error.message : String(error); + console.warn(`[OIDC] Rejected an ID token from ${config.name}: ${reason}`); + const rejected = rejectedToken(error); + return { success: false, error: rejected.proceed ? reason : rejected.error }; } - // If no ID token or need more info, fetch from userinfo endpoint + // userinfo may carry claims the token omits, but only about the same person. + // Its values override the token's, and the admin claim is read afterwards, so a + // response about a different subject would be a way in. if (discovery.userinfo_endpoint && tokens.access_token) { try { const userinfoResponse = await fetch(discovery.userinfo_endpoint, { @@ -1383,39 +1537,30 @@ export async function handleOidcCallback( }); if (userinfoResponse.ok) { const userinfo = await userinfoResponse.json(); - claims = { ...claims, ...userinfo }; + const step = applyUserinfo(claims, userinfo, config.name); + if (!step.proceed) { + console.warn( + `[OIDC] ${config.name} returned userinfo for a different subject; rejecting the sign-in` + ); + return { success: false, error: step.error }; + } + if (step.warn) console.warn(step.warn); + claims = step.claims; } } catch (e) { console.warn('Failed to fetch userinfo:', e); } } - // Validate nonce if present in ID token - if (claims.nonce && claims.nonce !== stateData.nonce) { - return { success: false, error: 'Invalid nonce' }; - } - - // Extract user information using configured claims - const username = claims[config.usernameClaim] || claims.preferred_username || claims.sub; - const email = claims[config.emailClaim] || claims.email; - const displayName = claims[config.displayNameClaim] || claims.name; - - if (!username) { + const identity = identityFromClaims(claims, config); + if (!identity.ok) { return { success: false, error: 'Username claim not found in token' }; } + const { username, email, displayName } = identity; - // Determine if user should be admin based on claim - let shouldBeAdmin = false; - if (config.adminClaim && config.adminValue) { - const adminClaimValue = claims[config.adminClaim]; - // Support multiple comma-separated admin values - const adminValues = config.adminValue.split(',').map((v: string) => v.trim()); - if (Array.isArray(adminClaimValue)) { - shouldBeAdmin = adminClaimValue.some((v: string) => adminValues.includes(v)); - } else { - shouldBeAdmin = adminValues.includes(adminClaimValue); - } - } + // Read off claims that are now known to come from the provider, and to be about + // the subject the token names. + const shouldBeAdmin = claimsGrantAdmin(claims, config.adminClaim, config.adminValue); // Build provider string for storage (e.g., "oidc:Keycloak") const authProvider = `oidc:${config.name}`; @@ -1460,21 +1605,15 @@ export async function handleOidcCallback( ? JSON.parse(config.roleMappings) : config.roleMappings; - const roleMappingsClaim = config.roleMappingsClaim || 'groups'; - const claimValue = claims[roleMappingsClaim]; - - if (Array.isArray(roleMappings) && claimValue) { - const claimValues = Array.isArray(claimValue) ? claimValue : [claimValue]; + const mapped = rolesFromClaims(claims, roleMappings, config.roleMappingsClaim); - // Get user's current roles to avoid duplicates + if (mapped.length > 0) { + // Roles the user already holds, so a repeat login is not a repeat write. const userRoles = await getUserRoles(user.id); - for (const mapping of roleMappings) { - if (mapping.claimValue && mapping.roleId && claimValues.includes(mapping.claimValue)) { - const hasRole = userRoles.some(r => r.roleId === mapping.roleId); - if (!hasRole) { - await assignUserRole(user.id, mapping.roleId, null); - } + for (const roleId of mapped) { + if (!userRoles.some((r) => r.roleId === roleId)) { + await assignUserRole(user.id, roleId, null); } } } @@ -1495,7 +1634,8 @@ export async function handleOidcCallback( success: true, user: await buildAuthenticatedUser(user, 'oidc'), redirectUrl: stateData.redirectUrl, - providerName: config.name + providerName: config.name, + providerId: config.id }; } catch (error: any) { const errorMsg = error instanceof Error ? error.message : String(error); diff --git a/src/lib/server/general-settings-visibility.ts b/src/lib/server/general-settings-visibility.ts new file mode 100644 index 000000000..7858e313f --- /dev/null +++ b/src/lib/server/general-settings-visibility.ts @@ -0,0 +1,75 @@ +/** + * Which general settings every signed-in user may read. + * + * The page renders for everybody, so the values that decide how a date or a log line + * is drawn have to reach everybody. The rest of the table does not: scanner images and + * arguments, the network and DNS those scanners run on, stack paths on the host, and + * the collection and cleanup schedules describe how the installation is built and + * operated. Somebody who may not view settings should not learn them by opening a tab. + * + * An allow-list rather than a deny-list: a setting added later is private until + * somebody decides otherwise, which is the safe direction to be wrong in. + */ + +/** Settings that only affect how the interface looks or reads. */ +export const PRESENTATION_SETTINGS = [ + 'actionIconSize', + 'animateIcons', + 'coloredActionButtons', + 'compactPorts', + 'confirmDestructive', + 'darkTheme', + 'dateFormat', + 'downloadFormat', + 'editorFont', + 'editorIndentGuides', + 'editorTheme', + 'font', + 'fontSize', + 'formatLogTimestamps', + 'gridFontSize', + 'highlightUpdates', + 'labelFilterMode', + 'lightTheme', + 'logBufferSizeKb', + 'logMaxLines', + 'showExposedPorts', + 'showGitCommitHash', + 'showImageChangelogLinks', + 'showStoppedContainers', + 'showWhatsNew', + 'stackLogOperations', + 'terminalFont', + 'timeFormat', + 'useSelfhstIcons', + // Not presentation as such, but every screen formats timestamps with it. + 'defaultTimezone', + // The starting point a new stack is seeded from. Written for the people who author + // stacks, who need no settings permission to do so, and it describes nothing about + // how this host is built. + 'defaultComposeTemplate' +] as const; + +const PRESENTATION = new Set(PRESENTATION_SETTINGS); + +/** + * The settings a caller may see. + * + * With permission to view settings, everything. Without it, only the presentation + * half - the operational values are dropped rather than blanked, so a reader cannot + * tell a hidden setting from one that was never configured. + */ +export function visibleGeneralSettings( + all: T, + canViewSettings: boolean +): Partial { + if (canViewSettings) return all; + + const visible: Partial = {}; + for (const key of Object.keys(all) as (keyof T & string)[]) { + if (PRESENTATION.has(key)) { + visible[key] = all[key]; + } + } + return visible; +} diff --git a/src/lib/server/oidc-callback-core.ts b/src/lib/server/oidc-callback-core.ts new file mode 100644 index 000000000..427120b63 --- /dev/null +++ b/src/lib/server/oidc-callback-core.ts @@ -0,0 +1,129 @@ +/** + * The decisions a completed OIDC login makes, in one place. + * + * Verifying a token and reading claims are each covered by their own module, but what + * the callback DOES with those answers is where a sign-in is actually granted or + * refused. Keeping those steps here, away from the fetches and the database, means a + * test can prove the answers are acted on rather than merely computed - a check that + * is called and then ignored would otherwise look exactly like a check that works. + */ + +import { mergeUserinfo, type Claims, type MergeResult } from './oidc-claims-core'; + +/** Either the sign-in continues with these claims, or it stops with a reason. */ +export type CallbackStep = + | { proceed: true; claims: Claims; warn?: string } + | { proceed: false; error: string }; + +/** + * Why a token exchange failed, in words the person fixing it can act on. + * + * A mistyped secret or an unregistered redirect URI is the first thing a new setup + * gets wrong, and the provider names which in its OAuth error - so its words travel + * rather than a bare "the exchange failed". + */ +export function describeTokenExchangeFailure(status: number, body: string): string { + let code: unknown; + let description: unknown; + try { + const parsed = JSON.parse(body) as Record; + code = parsed.error; + description = parsed.error_description; + } catch { + // Not JSON - some providers answer HTML or a bare string. + } + + const oneLine = (v: unknown) => { + if (typeof v !== 'string' || !v.trim()) return null; + const flat = v.trim().replace(/\s+/g, ' '); + // Cut by code POINT, not code unit: half of a surrogate pair is not a character, + // and this string is handed to encodeURIComponent on its way into a redirect, + // which throws URIError on a lone surrogate. + return [...flat].slice(0, 200).join(''); + }; + + // The status decides the verb, not the body: a 5xx is the provider or something in + // front of it failing rather than a decision about this sign-in, and calling that a + // refusal sends whoever reads it to check the client secret when the thing to check + // is whether the provider is up. + const lead = + status >= 500 + ? 'The provider could not be reached to exchange the authorization code' + : 'The provider refused the authorization code'; + + const detail = oneLine(description) ?? oneLine(code); + return detail ? `${lead}: ${detail}` : `${lead} (HTTP ${status})`; +} + +/** + * A provider's error body, made safe to log. + * + * The request is posted with the client secret and the authorization code in it, and a + * provider that echoes what it received puts both in its reply. Bounded too, so one + * failure cannot pour a page of HTML into the log. + */ +export function redactTokenErrorBody(body: string, limit = 500): string { + if (!body) return '(empty)'; + // Bound FIRST. The body is whatever a remote service sent, and running a pattern + // over a megabyte of it is work an outsider gets to ask for. + const bounded = body.length > limit ? `${body.slice(0, limit)}... (truncated)` : body; + // The name is matched with the separator optional and a bounded prefix allowed, so + // a provider that renames the field (oauth_client_secret, clientSecret) does not + // slip past; the quote may be escaped, because some answer with the request + // embedded as a JSON string. + // The value runs to the delimiter of whichever encoding carried it - a quote for + // JSON, an ampersand for a form - so a secret holding a space or a comma is masked + // whole rather than to its first odd character. + return bounded.replace( + /((?:\\?")?[a-z0-9]{0,12}_?(?:client_?secret|refresh_?token|access_?token|id_?token|assertion|\bcode)(?:\\?")?\s*[=:]\s*)(\\?"[^"\\]*\\?"|[^&\s]+)/gi, + '$1' + ); +} + +/** A token response is only usable when it actually carries an id_token. */ +export function requireIdToken(tokens: { id_token?: string } | null | undefined): CallbackStep { + if (!tokens?.id_token) { + return { + proceed: false, + error: 'The provider returned no ID token, so the sign-in could not be verified' + }; + } + return { proceed: true, claims: {} }; +} + +/** How a rejected token is reported: the reason travels, so support can act on it. */ +export function rejectedToken(reason: unknown): CallbackStep { + const message = reason instanceof Error ? reason.message : String(reason); + return { proceed: false, error: `Sign-in rejected: ${message}` }; +} + +/** + * What to do with a userinfo response, given the claims already verified. + * + * A response describing a different person stops the sign-in: its values override the + * token's and the admin claim is read afterwards, so merging one would be a way in. + */ +export function applyUserinfo( + verified: Claims, + userinfo: Claims | null | undefined, + providerName: string +): CallbackStep { + const merged: MergeResult = mergeUserinfo(verified, userinfo); + + if (!merged.ok) { + return { + proceed: false, + error: 'Sign-in rejected: the provider described a different account' + }; + } + + if (merged.ignored === 'no-subject') { + return { + proceed: true, + claims: merged.claims, + warn: `[OIDC] ${providerName} returned userinfo naming no subject; continuing on the token claims` + }; + } + + return { proceed: true, claims: merged.claims }; +} diff --git a/src/lib/server/oidc-claims-core.ts b/src/lib/server/oidc-claims-core.ts new file mode 100644 index 000000000..a8d1cbad9 --- /dev/null +++ b/src/lib/server/oidc-claims-core.ts @@ -0,0 +1,168 @@ +/** + * What a verified id_token means, once the signature has been checked. + * + * Signature, issuer, audience and expiry are the library's job - they need network + * access and a clock, and hand-rolling them is how this went wrong in the first place. + * What is left is the part that decides who somebody is and what they may do, and that + * part is worth being able to test exhaustively without an identity provider. + * + * Pure: no fetch, no database, no clock. + */ + +/** Claims as they arrive, from the token or the userinfo response. */ +export type Claims = Record; + +/** + * Merge userinfo into the token's claims, refusing a response about somebody else. + * + * OIDC Core 5.3.2 requires the userinfo `sub` to match the id_token's. Without that + * check the merge is an open door: userinfo values overwrite the token's, and the + * admin claim is read afterwards, so a provider - or anything able to answer as one - + * could hand out administrator rights through the weaker endpoint. + */ +export function mergeUserinfo( + tokenClaims: Claims, + userinfo: Claims | null | undefined +): MergeResult { + if (!userinfo) return { ok: true, claims: tokenClaims }; + + // Compared as text, because a provider that numbers its subjects may write the + // same one as 12345 in the token and "12345" here. + const tokenSub = subjectText(tokenClaims.sub); + const infoSub = subjectText(userinfo.sub); + + // Two subjects that disagree is the attack: the token says one person, the + // weaker endpoint says another, and the admin claim is read after the merge. + if (infoSub !== undefined && tokenSub !== undefined && infoSub !== tokenSub) { + return { ok: false, reason: 'subject-mismatch' }; + } + + // A response that names nobody cannot be tied to this token, but it is a poor + // provider rather than an attack - and the token is already verified, so the + // login continues on its claims alone. + if (infoSub === undefined) { + return { ok: true, claims: tokenClaims, ignored: 'no-subject' }; + } + + return { ok: true, claims: { ...tokenClaims, ...userinfo } }; +} + +/** A subject as comparable text, or undefined when the claim names nobody. */ +function subjectText(value: unknown): string | undefined { + if (typeof value === 'string' && value.length > 0) return value; + if (typeof value === 'number' && Number.isFinite(value)) return String(value); + return undefined; +} + +export type MergeResult = + | { ok: true; claims: Claims; ignored?: 'no-subject' } + | { ok: false; reason: 'subject-mismatch' }; + +/** + * Whether the claims say this user is an administrator. + * + * Both the claim name and the accepted values come from the provider config; an + * unconfigured pair grants nothing. The claim may be a single value or a list, and + * the configured values are comma-separated, which is how the settings form presents + * them. + */ +export function claimsGrantAdmin( + claims: Claims, + adminClaim: string | null | undefined, + adminValue: string | null | undefined +): boolean { + if (!adminClaim || !adminValue) return false; + + const wanted = adminValue + .split(',') + .map((v) => v.trim()) + .filter(Boolean); + if (wanted.length === 0) return false; + + const actual = claims[adminClaim]; + if (Array.isArray(actual)) { + return actual.some((v) => typeof v === 'string' && wanted.includes(v)); + } + return typeof actual === 'string' && wanted.includes(actual); +} + +/** The identity a login resolves to, or what was missing. */ +export type IdentityResult = + | { ok: true; username: string; email?: string; displayName?: string } + | { ok: false; reason: 'no-username' }; + +/** + * The account details carried by a set of claims. + * + * The username is the join key against local accounts, so a login without one cannot + * proceed. `sub` is the last resort because it is the only claim a provider must + * always send. + */ +export function identityFromClaims( + claims: Claims, + config: { + usernameClaim?: string | null; + emailClaim?: string | null; + displayNameClaim?: string | null; + } +): IdentityResult { + const pick = (name: string | null | undefined, ...fallbacks: string[]): string | undefined => { + const names = [name, ...fallbacks].filter((n): n is string => !!n); + for (const n of names) { + const v = claims[n]; + if (typeof v === 'string' && v.length > 0) return v; + // Some providers number their subjects. The claims are verified by the + // time they get here, so a numeric one is as trustworthy as a written one + // and refusing it would lock those users out for a formatting detail. + if (typeof v === 'number' && Number.isFinite(v)) return String(v); + } + return undefined; + }; + + const username = pick(config.usernameClaim, 'preferred_username', 'sub'); + if (!username) return { ok: false, reason: 'no-username' }; + + return { + ok: true, + username, + email: pick(config.emailClaim, 'email'), + displayName: pick(config.displayNameClaim, 'name') + }; +} + +/** A role the provider's claims ask for. */ +export interface RoleMapping { + claimValue: string; + roleId: number; +} + +/** + * The roles a set of claims asks for, from the configured mappings. + * + * Returns role ids rather than assigning anything, so the caller decides what to do + * with them and the matching stays testable. + */ +export function rolesFromClaims( + claims: Claims, + mappings: RoleMapping[] | null | undefined, + claimName: string | null | undefined +): number[] { + if (!mappings || mappings.length === 0) return []; + + const actual = claims[claimName || 'groups']; + const values = Array.isArray(actual) + ? actual.filter((v): v is string => typeof v === 'string') + : typeof actual === 'string' + ? [actual] + : []; + if (values.length === 0) return []; + + // The mappings arrive from JSON in the provider row, so the shape is a claim, not + // a guarantee. A mapping pointing at no real role is dropped rather than turned + // into a bad write. + const ids = mappings + .filter((m) => m && values.includes(m.claimValue)) + .map((m) => m.roleId) + .filter((id): id is number => Number.isInteger(id) && id > 0); + return [...new Set(ids)]; +} diff --git a/src/lib/server/oidc-login-state.ts b/src/lib/server/oidc-login-state.ts new file mode 100644 index 000000000..2fdd67fb6 --- /dev/null +++ b/src/lib/server/oidc-login-state.ts @@ -0,0 +1,206 @@ +/** + * The short-lived state of a login that is away at the identity provider. + * + * Between the redirect out and the callback back, Dockhand has to remember four + * things: which provider was used, the PKCE verifier, the nonce, and where the user + * was heading. Holding them in process memory loses them whenever the process + * restarts mid-login, and loses them every time when two containers sit behind the + * same proxy - the browser can come back to the one that never issued the request + * (#1601). Carrying them in the browser removes the shared-memory assumption + * altogether. + * + * The cookie is encrypted with the instance key, and a value that is not ciphertext + * from this instance is refused before anything is read out of it. So the PKCE + * verifier and nonce it carries are neither readable nor authorable by the browser + * holding it, which is the whole point of both. + * + * Pure: the caller does the cookie I/O and passes the crypto in, so the shape and the + * expiry rules can be tested without a server or a key. + */ + +/** Every login-state cookie starts with this. */ +export const OIDC_STATE_COOKIE_PREFIX = 'dockhand_oidc_state_'; + +/** + * The cookie name for one login. + * + * Named after the login rather than fixed, because two tabs sign in at once more + * often than it sounds - a second tab opened on a deep link, a bookmark followed + * while the first is still away at the provider. One shared name means the second + * authorize overwrites the first, and then BOTH callbacks fail: the first finds a + * state that does not match, and it has already deleted the cookie the second needed. + * + * The name is derived from `state`, which the provider echoes back, so the callback + * can find its own cookie among several. It is a hash rather than the state itself: + * cookie names are the one part not covered by the encryption, and the state is worth + * no more to an onlooker than it has to be. + * + * An abandoned login's cookie is left to its own maxAge rather than swept here: the + * callback is reachable without a session, so clearing anything but its own cookie + * would let one request end everybody else's sign-in. + */ +export function loginStateCookieName(state: string, hash: (value: string) => string): string { + return OIDC_STATE_COOKIE_PREFIX + hash(state).slice(0, 16); +} + +/** + * How many unfinished logins one browser may hold at once. + * + * Several tabs signing in together is ordinary; dozens is not. Starting a login + * needs no session, so without a ceiling any page could point a browser at the + * initiate URL repeatedly and pile up cookies until the request header grew too + * large for the server to accept - locking the site out of its own login page. + */ +export const MAX_LOGIN_STATE_COOKIES = 5; + +/** + * Which login-state cookies to drop to make room for a new one. + * + * Decided ONLY from the cookies the browser sent with this request, so a request can + * never affect anybody else's sign-in. Oldest first, by the expiry each cookie + * carries; one that cannot be read at all goes first, since it can never be resumed. + */ +export function loginStateCookiesToEvict( + cookies: { name: string; expiresAt: number | null }[], + max: number = MAX_LOGIN_STATE_COOKIES +): string[] { + const live = cookies.filter((c) => c.name.startsWith(OIDC_STATE_COOKIE_PREFIX)); + + // The new cookie takes a slot, so room has to be left for it. + const excess = live.length - (max - 1); + if (excess <= 0) return []; + + const oldestFirst = [...live].sort( + (a, b) => (a.expiresAt ?? -Infinity) - (b.expiresAt ?? -Infinity) + ); + return oldestFirst.slice(0, excess).map((c) => c.name); +} + + +/** How long a login may stay away at the provider. */ +export const OIDC_STATE_TTL_SECONDS = 600; + +export interface OidcLoginState { + /** Which provider row started this login. */ + configId: number; + /** The `state` parameter, echoed back by the provider. */ + state: string; + /** PKCE verifier, proving this callback belongs to this authorize request. */ + codeVerifier: string; + /** Nonce, tying the returned id_token to this request. */ + nonce: string; + /** Where the user was going before being sent to the provider. */ + redirectUrl: string; + /** Epoch ms after which the login is too old to finish. */ + expiresAt: number; +} + +/** True when the value has every field a callback needs, with the right types. */ +function isLoginState(value: unknown): value is OidcLoginState { + if (!value || typeof value !== 'object') return false; + const s = value as Record; + return ( + typeof s.configId === 'number' && + typeof s.state === 'string' && + s.state.length > 0 && + typeof s.codeVerifier === 'string' && + s.codeVerifier.length > 0 && + typeof s.nonce === 'string' && + s.nonce.length > 0 && + typeof s.redirectUrl === 'string' && + typeof s.expiresAt === 'number' + ); +} + +/** The cookie value for a login that is about to leave for the provider. */ +export function encodeLoginState( + state: OidcLoginState, + encrypt: (plaintext: string) => string | null +): string { + const encoded = encrypt(JSON.stringify(state)); + if (!encoded) throw new Error('Could not protect the OIDC login state'); + return encoded; +} + +export type LoginStateFailure = + | 'missing' // no cookie: a bookmarked callback, or a different browser + | 'unreadable' // not our ciphertext, or the instance key changed + | 'malformed' // decrypted to something that is not a login state + | 'expired' // the user took longer than the TTL + | 'state-mismatch'; // the provider echoed a different state than we issued + +export type LoginStateResult = + | { ok: true; state: OidcLoginState } + | { ok: false; reason: LoginStateFailure }; + +/** + * Reading a cookie takes both halves together: whether the value is this instance's + * ciphertext, and how to decrypt it. Passing the pair as one object means a caller + * cannot supply the second without the first. + */ +export interface LoginStateCrypto { + isCiphertext: (value: string) => boolean; + decrypt: (value: string) => string | null; +} + +/** + * The login state a callback may act on, or why it may not. + * + * `returnedState` is what the provider echoed. Comparing it here rather than at the + * call site keeps the check from being forgotten: a callback whose state does not + * match the cookie is a different login, or a forged one. + */ +export function decodeLoginState( + cookieValue: string | undefined | null, + returnedState: string | undefined | null, + crypto: LoginStateCrypto, + now: number +): LoginStateResult { + if (!cookieValue) return { ok: false, reason: 'missing' }; + + // Only a value this instance encrypted is worth reading. `decrypt` returns + // anything without the ciphertext marker unchanged, so without this the cookie + // would be plain JSON that the browser can author - handing an attacker the + // provider, the PKCE verifier and the nonce of the login it completes. + if (!crypto.isCiphertext(cookieValue)) return { ok: false, reason: 'unreadable' }; + + let plaintext: string | null; + try { + plaintext = crypto.decrypt(cookieValue); + } catch { + return { ok: false, reason: 'unreadable' }; + } + if (!plaintext) return { ok: false, reason: 'unreadable' }; + + let parsed: unknown; + try { + parsed = JSON.parse(plaintext); + } catch { + return { ok: false, reason: 'malformed' }; + } + if (!isLoginState(parsed)) return { ok: false, reason: 'malformed' }; + + if (parsed.expiresAt <= now) return { ok: false, reason: 'expired' }; + + // Compared after decoding, so a caller cannot skip it by forgetting to. + if (!returnedState || returnedState !== parsed.state) { + return { ok: false, reason: 'state-mismatch' }; + } + + return { ok: true, state: parsed }; +} + +/** What to tell somebody whose login could not be resumed. */ +export function loginStateMessage(reason: LoginStateFailure): string { + switch (reason) { + case 'missing': + return 'Login session not found. Start the sign-in from the login page, and allow cookies for this site.'; + case 'unreadable': + case 'malformed': + return 'Login session could not be read. Start the sign-in again.'; + case 'expired': + return 'Login took too long and expired. Start the sign-in again.'; + case 'state-mismatch': + return 'Login response did not match the request. Start the sign-in again.'; + } +} diff --git a/src/lib/server/oidc-token-core.ts b/src/lib/server/oidc-token-core.ts new file mode 100644 index 000000000..12004aa4e --- /dev/null +++ b/src/lib/server/oidc-token-core.ts @@ -0,0 +1,123 @@ +/** + * Proving that an id_token really came from the configured provider. + * + * Claims decide who somebody is and whether they administer this instance, so nothing + * is read out of a token until it is proven to come from the configured provider. Kept + * apart from the rest of the OIDC flow so a test can call the real thing: the only I/O + * is fetching the provider's signing keys, and that arrives as an argument. + */ + +import { jwtVerify, type JWTVerifyGetKey, type KeyObject } from 'jose'; +import type { Claims } from './oidc-claims-core'; + +/** + * How far the clocks either side of a login may drift before it fails. + * + * Two hosts that disagree by a few seconds is ordinary; refusing those logins would + * make Dockhand look broken for a reason nobody would guess from the outside. The + * allowance covers both directions - a token that just expired, and one issued a + * moment in the future by a provider running ahead - and is small against the + * several minutes an id_token is valid for, so it does not meaningfully widen the + * window in which a stolen token can be replayed. Matches the openid-client default. + */ +export const ID_TOKEN_CLOCK_TOLERANCE_SECONDS = 30; + +/** + * Signing algorithms an id_token may use. + * + * Every asymmetric algorithm the JOSE registry defines, so no conforming provider is + * turned away - what the list excludes is the symmetric family (HS*) and `none`, + * where the verifying key is either public or absent. + */ +export const DEFAULT_ID_TOKEN_ALGORITHMS = [ + 'RS256', 'RS384', 'RS512', + 'PS256', 'PS384', 'PS512', + 'ES256', 'ES256K', 'ES384', 'ES512', + 'EdDSA', 'Ed25519' +]; + +export interface IdTokenExpectations { + /** The issuer the provider names in its own discovery document. */ + issuer: string; + /** This deployment's client id, which the token must be addressed to. */ + clientId: string; + /** The nonce this login sent, tying the token to this request. */ + nonce: string; +} + +/** + * The claims of an id_token, once it has been proven to come from the provider. + * + * Throws with a reason a person can act on. Each failure is a different + * misconfiguration - a provider whose issuer does not match its own discovery + * document is a different problem from a clock that has drifted - and saying which is + * the difference between a support thread and a five-minute fix. + */ +export async function verifyIdTokenWithKeys( + idToken: string, + keys: JWTVerifyGetKey | KeyObject | CryptoKey | Uint8Array, + expected: IdTokenExpectations +): Promise { + let payload: Claims; + try { + const result = await jwtVerify(idToken, keys as JWTVerifyGetKey, { + issuer: expected.issuer, + audience: expected.clientId, + clockTolerance: ID_TOKEN_CLOCK_TOLERANCE_SECONDS, + // Both are REQUIRED of an id_token by OIDC Core, and jose only checks a + // claim that is present. Without exp a captured token never expires; + // without sub the token names nobody, and the userinfo response - a weaker + // source - is left free to say who this is and what they may do. + requiredClaims: ['exp', 'sub'], + // Asymmetric only. A symmetric algorithm would let a token be signed with + // the provider's PUBLIC key, which anybody can fetch. The key resolver + // happens to refuse those too, but that is its property, not this one's, + // and it would go away with a different key source. + algorithms: DEFAULT_ID_TOKEN_ALGORITHMS + }); + payload = result.payload as Claims; + } catch (error) { + throw new Error(describeVerifyFailure(error)); + } + + // A token may name several applications. Membership alone does not say it was + // meant for this one, so the provider has to name the party it authorized. + const audiences = payload.aud; + if (Array.isArray(audiences) && audiences.length > 1 && payload.azp !== expected.clientId) { + throw new Error('the ID token names several applications and was not authorised for this one'); + } + + // Required, not checked-if-present: a token without a nonce cannot be tied to the + // request that started this login, which is the whole point of sending one. + if (payload.nonce !== expected.nonce) { + throw new Error('the ID token does not belong to this sign-in request'); + } + + return payload; +} + +/** The human half of a verification failure, kept apart so it can be tested alone. */ +export function describeVerifyFailure(error: unknown): string { + const code = (error as { code?: string })?.code; + switch (code) { + case 'ERR_JWT_EXPIRED': + return `the ID token has expired by more than ${ID_TOKEN_CLOCK_TOLERANCE_SECONDS}s (check the clock on this host and on the provider)`; + case 'ERR_JWT_CLAIM_VALIDATION_FAILED': { + const claim = (error as { claim?: string })?.claim; + if (claim === 'iss') return 'the ID token came from a different issuer than configured'; + if (claim === 'aud') return 'the ID token was issued for a different application'; + if (claim === 'exp') return 'the ID token carries no expiry, which OIDC requires'; + if (claim === 'sub') return 'the ID token names no subject, which OIDC requires'; + return `the ID token failed its ${claim ?? 'claim'} check`; + } + case 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED': + return 'the ID token signature does not match the provider signing keys'; + case 'ERR_JWKS_NO_MATCHING_KEY': + return 'the ID token was signed with a key the provider does not publish'; + case 'ERR_JOSE_ALG_NOT_ALLOWED': + case 'ERR_JOSE_NOT_SUPPORTED': + return 'the ID token uses a signing algorithm the provider keys do not allow'; + default: + return `the ID token could not be verified (${error instanceof Error ? error.message : String(error)})`; + } +} diff --git a/src/lib/server/own-container-core.ts b/src/lib/server/own-container-core.ts new file mode 100644 index 000000000..f067d6166 --- /dev/null +++ b/src/lib/server/own-container-core.ts @@ -0,0 +1,39 @@ +/** + * Whether a request is aiming at the container Dockhand itself runs in. + * + * Changing Dockhand's own networking is not the same kind of act as changing an + * application's: the update helper reconnects the replacement container to whatever + * networks it finds, so what is attached here is read later by a privileged step. + * Attaching a network is a permission of its own, so without this an account that + * holds it could arrange that input without holding the administrator role. + * + * Pure, so the matching rules are a unit test rather than something only a + * misidentified container reveals. + */ + +/** A docker id: 64 hex characters, or the 12-character short form. */ +function isDockerId(value: string): boolean { + return /^[a-f0-9]{12}$|^[a-f0-9]{64}$/.test(value); +} + +/** + * True when `resolvedId` is the id of the container Dockhand runs in. + * + * Both sides must be real ids. Docker accepts a NAME wherever it accepts an id, and a + * name can be reassigned between this check and the act it guards, so the caller + * resolves whatever it was given to an id first and passes that here. An unknown own + * identity is false: guessing would refuse legitimate work on some other container. + */ +export function targetsOwnContainer( + resolvedId: string | null | undefined, + own: { id?: string | null } +): boolean { + if (typeof resolvedId !== 'string') return false; + const t = resolvedId.trim().toLowerCase(); + const ownId = own.id?.trim().toLowerCase(); + if (!t || !ownId || !isDockerId(t) || !isDockerId(ownId)) return false; + + // Either may be the short form, so compare over the shorter length. + const n = Math.min(t.length, ownId.length); + return t.slice(0, n) === ownId.slice(0, n); +} diff --git a/src/lib/server/own-container-guard.ts b/src/lib/server/own-container-guard.ts new file mode 100644 index 000000000..472804b56 --- /dev/null +++ b/src/lib/server/own-container-guard.ts @@ -0,0 +1,36 @@ +import { json } from '@sveltejs/kit'; +import { inspectContainer } from './docker'; +import { getOwnContainerId } from './host-path'; +import { targetsOwnContainer } from './own-container-core'; + +/** + * Refuse an act on the container Dockhand itself runs in, or null to allow it. + * + * Docker accepts a container NAME wherever it accepts an id, so the reference is + * resolved to an id before it is compared: a name can also be reassigned between the + * check and the act it guards, which an id cannot. + * + * An inspect that fails means the reference names nothing this daemon knows, so the + * act would fail anyway - it is let through to fail on its own terms rather than + * being reported as a permission problem. + */ +export async function refuseOwnContainer( + containerRef: string, + envId?: number +): Promise { + const ownId = getOwnContainerId(); + if (!ownId) return null; + + let resolvedId: string | null = null; + try { + resolvedId = (await inspectContainer(containerRef, envId))?.Id ?? null; + } catch { + return null; + } + + if (!targetsOwnContainer(resolvedId, { id: ownId })) return null; + return json( + { error: 'Only an administrator can change the networks of the Dockhand container' }, + { status: 403 } + ); +} diff --git a/src/lib/server/public-paths-core.ts b/src/lib/server/public-paths-core.ts new file mode 100644 index 000000000..5695235d5 --- /dev/null +++ b/src/lib/server/public-paths-core.ts @@ -0,0 +1,48 @@ +/** + * Which request paths skip authentication. + * + * Two lists, because they match differently. An exact entry covers only itself, so a + * route added beneath one starts out protected like any other - a new + * /api/license/ cannot be born unauthenticated by inheriting its parent's + * entry. A prefix entry deliberately covers its subtree, for the few features whose + * subtree IS the feature. + * + * Pure, so what the server lets through is a unit test rather than something only a + * running instance can show. + */ + +/** Public, and nothing beneath them. */ +export const PUBLIC_EXACT = [ + '/login', + '/api/auth/login', + '/api/auth/logout', + '/api/auth/session', + '/api/auth/settings', + '/api/auth/providers', + '/api/license', + '/api/changelog', + '/api/settings/theme' +]; + +/** + * Public along with their subtree: the OIDC dance spans several callbacks, the docs + * page loads its own viewer, and the health probes include /api/health/database, + * which redacts its own payload by permission. + */ +export const PUBLIC_PREFIXES = ['/api/auth/oidc', '/api/docs', '/api/health']; + +/** + * Endpoints carrying their own authentication - a signature or a shared secret - so + * they must not be answered from a session either way. + */ +export const PUBLIC_PATH_PATTERNS = [ + /^\/api\/git\/stacks\/\d+\/webhook$/, + /^\/api\/git\/webhook\/\d+$/ +]; + +/** Whether this path is reachable without a session. */ +export function isPublicPath(pathname: string): boolean { + if (PUBLIC_PATH_PATTERNS.some((re) => re.test(pathname))) return true; + if (PUBLIC_EXACT.includes(pathname)) return true; + return PUBLIC_PREFIXES.some((path) => pathname === path || pathname.startsWith(path + '/')); +} diff --git a/src/lib/server/self-update-network-core.ts b/src/lib/server/self-update-network-core.ts new file mode 100644 index 000000000..e8321b259 --- /dev/null +++ b/src/lib/server/self-update-network-core.ts @@ -0,0 +1,76 @@ +/** + * Which network names and options the update sidecar may be told about. + * + * The sidecar reconnects the replacement container to the networks the old one was + * on, and it reads them from its environment into an unquoted shell expansion. + * Naming a network is a permission of its own, so the person who chose a name need + * not be the administrator who later runs the update - and Docker accepts almost + * anything as a name, including `a;id;b`. + */ + +/** + * Docker's own name grammar. A name may open with `_` or `.` as well as an + * alphanumeric - the daemon accepts those - but never with `-`, which the daemon + * refuses and which would reach the CLI as a flag. + */ +const SAFE_NETWORK_NAME = /^[a-zA-Z0-9_.][a-zA-Z0-9_.-]*$/; + +/** + * An address or alias, which must survive an unquoted expansion as one word. A leading + * colon is allowed because an IPv6 address may start with one; a leading dash is not, + * because docker would read it as a flag. + */ +const SAFE_OPTION_VALUE = /^[a-zA-Z0-9:][a-zA-Z0-9_.:-]*$/; + +export function isSafeNetworkName(name: unknown): name is string { + return typeof name === 'string' && name.length <= 256 && SAFE_NETWORK_NAME.test(name); +} + +export interface NetworkEnvPlan { + /** The env var lines for the sidecar. Empty when there is nothing to reconnect. */ + envVars: string[]; + /** Names withheld, so the caller can say so rather than reconnecting silently. */ + skipped: string[]; +} + +/** + * The NETWORKS and NETWORK_OPTS_* lines for a container's networks. + * + * Anything that is not plainly a name or a value is dropped rather than escaped: + * coming back on one fewer network is a smaller failure than a name reaching a shell. + */ +export function planNetworkEnvVars( + networks: Record | null | undefined +): NetworkEnvPlan { + const names: string[] = []; + const optionLines: string[] = []; + const skipped: string[] = []; + + for (const [netName, netConfig] of Object.entries(networks || {})) { + if (!isSafeNetworkName(netName)) { + skipped.push(String(netName)); + continue; + } + names.push(netName); + + const nc = (netConfig || {}) as { + IPAMConfig?: { IPv4Address?: string; IPv6Address?: string }; + Aliases?: unknown[]; + Links?: unknown[]; + }; + const safe = (v: unknown) => typeof v === 'string' && SAFE_OPTION_VALUE.test(v); + const opts: string[] = []; + + if (safe(nc.IPAMConfig?.IPv4Address)) opts.push(`--ip ${nc.IPAMConfig!.IPv4Address}`); + if (safe(nc.IPAMConfig?.IPv6Address)) opts.push(`--ip6 ${nc.IPAMConfig!.IPv6Address}`); + for (const alias of nc.Aliases || []) if (safe(alias)) opts.push(`--alias ${alias}`); + for (const link of nc.Links || []) if (safe(link)) opts.push(`--link ${link}`); + + if (opts.length > 0) { + optionLines.push(`NETWORK_OPTS_${netName.replace(/[.-]/g, '_')}=${opts.join(' ')}`); + } + } + + if (names.length === 0) return { envVars: [], skipped }; + return { envVars: [`NETWORKS=${names.join(' ')}`, ...optionLines], skipped }; +} diff --git a/src/routes/api/auth/oidc/callback/+server.ts b/src/routes/api/auth/oidc/callback/+server.ts index 6b8e865b5..e6b151140 100644 --- a/src/routes/api/auth/oidc/callback/+server.ts +++ b/src/routes/api/auth/oidc/callback/+server.ts @@ -1,9 +1,14 @@ import { json, redirect } from '@sveltejs/kit'; import type { RequestHandler } from '@sveltejs/kit'; -import { handleOidcCallback, createUserSession, isAuthEnabled } from '$lib/server/auth'; +import { handleOidcCallback, createUserSession, isAuthEnabled, cookieNameFor } from '$lib/server/auth'; import { auditAuth } from '$lib/server/audit'; import { getClientIp } from '$lib/server/client-ip'; import { safeRedirectOrRoot } from '$lib/utils/safe-redirect'; +import { decrypt, looksLikeCiphertext } from '$lib/server/encryption'; +import { + decodeLoginState, + loginStateMessage +} from '$lib/server/oidc-login-state'; // GET /api/auth/oidc/callback - Handle OIDC callback from IdP /** @@ -33,8 +38,7 @@ export const GET: RequestHandler = async (event) => { // Handle error from IdP if (error) { - console.error('OIDC error from IdP:', error, errorDescription); - console.warn(`[Auth] OIDC login failed: ip=${clientIp} error=${errorDescription || error}`); + console.warn(`[Auth] OIDC login failed: ip=${clientIp} error=${error}${errorDescription ? ` - ${errorDescription}` : ''}`); const errorMsg = encodeURIComponent(errorDescription || error); throw redirect(302, `/login?error=${errorMsg}`); } @@ -44,8 +48,29 @@ export const GET: RequestHandler = async (event) => { throw redirect(302, '/login?error=invalid_callback'); } + // Read the login this callback belongs to, clearing the cookie first so a later + // callback in this browser cannot resume it. What stops a concurrent replay is + // the authorization code, which the provider honours once. + const cookieName = cookieNameFor(state); + const stateCookie = cookies.get(cookieName); + cookies.delete(cookieName, { path: '/' }); + + const loginState = decodeLoginState( + stateCookie, + state, + { isCiphertext: looksLikeCiphertext, decrypt }, + Date.now() + ); + if (!loginState.ok) { + const message = loginStateMessage(loginState.reason); + // The sentence, not the bare code: "missing" alone leaves whoever reads the log + // to guess between an expired login, a replay, and cookies being blocked. + console.warn(`[Auth] OIDC login failed: ip=${clientIp} reason=${loginState.reason} - ${message}`); + throw redirect(302, `/login?error=${encodeURIComponent(message)}`); + } + try { - const result = await handleOidcCallback(code, state); + const result = await handleOidcCallback(code, loginState.state); if (!result.success || !result.user) { console.warn(`[Auth] OIDC login failed: ip=${clientIp} error=${result.error || 'Authentication failed'}`); @@ -54,7 +79,10 @@ export const GET: RequestHandler = async (event) => { } // Create session - await createUserSession(result.user.id, 'oidc', cookies, event.request); + // Named, not a bare 'oidc': logging out has to know which provider to end the + // session with, and the column already holds this shape for the user row. + const sessionProvider = result.providerName ? `oidc:${result.providerName}` : 'oidc'; + await createUserSession(result.user.id, sessionProvider, cookies, event.request); console.log(`[Auth] OIDC login successful: user=${result.user.username} provider=${result.providerName || 'oidc'} ip=${clientIp}`); // Audit log diff --git a/src/routes/api/auto-update/+server.ts b/src/routes/api/auto-update/+server.ts index 9cc9820a1..729701d2a 100644 --- a/src/routes/api/auto-update/+server.ts +++ b/src/routes/api/auto-update/+server.ts @@ -1,6 +1,7 @@ import { json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { getAutoUpdateSettings } from '$lib/server/db'; +import { authorize } from '$lib/server/authorize'; /** * Batch endpoint to get all auto-update settings for an environment. @@ -11,15 +12,38 @@ import { getAutoUpdateSettings } from '$lib/server/db'; * query: env:integer Environment ID to read settings for (from GET /api/environments) * resp-200: {} * resp-200-example: {"web-1":{"enabled":true,"scheduleType":"daily","cronExpression":"0 3 * * *","vulnerabilityCriteria":"never"}} + * resp-403: Permission denied (needs schedules:view, and access to the environment) * resp-500: Failed to get auto-update settings */ -export const GET: RequestHandler = async ({ url }) => { +export const GET: RequestHandler = async ({ url, cookies }) => { + // The same answer as the per-container route, for every container at once, so it + // carries the same gate: these are the cron schedules on which each container is + // recreated, and whether a vulnerability finding would stop that. + const auth = await authorize(cookies); + + const permDenied = await auth.requirePermission('schedules', 'view'); + if (permDenied) return permDenied; + try { const envIdParam = url.searchParams.get('env'); const envId = envIdParam ? parseInt(envIdParam) : undefined; + const envDenied = await auth.requireEnvAccess(envId); + if (envDenied) return envDenied; + const settings = await getAutoUpdateSettings(envId); + // Without ?env= the query spans every environment, and the check above only + // answers for a named one, so the answer is narrowed to what this caller may + // actually see. Null means no restriction (admin, free edition, auth off). + const accessible = envId === undefined ? await auth.getAccessibleEnvironmentIds() : null; + const visible = + accessible === null + ? settings + : settings.filter( + (s) => s.environmentId === null || accessible.includes(s.environmentId) + ); + // Convert to a map keyed by container name for efficient frontend lookup const settingsMap: Record { vulnerabilityCriteria: string; }> = {}; - for (const setting of settings) { + for (const setting of visible) { if (setting.enabled) { settingsMap[setting.containerName] = { enabled: setting.enabled, diff --git a/src/routes/api/environments/detect-socket/+server.ts b/src/routes/api/environments/detect-socket/+server.ts index 5dcc24ea3..6a04b90af 100644 --- a/src/routes/api/environments/detect-socket/+server.ts +++ b/src/routes/api/environments/detect-socket/+server.ts @@ -2,6 +2,7 @@ import { json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { existsSync } from 'node:fs'; import { homedir } from 'node:os'; +import { authorize } from '$lib/server/authorize'; interface DetectedSocket { path: string; @@ -16,8 +17,21 @@ interface DetectedSocket { * summary: Detect common Docker/Podman socket paths that exist on the Dockhand host * resp-200: {sockets:array<{path:string!, name:string!, exists:boolean!}>!, homedir:string!} * resp-200-example: {"sockets":[{"path":"/var/run/docker.sock","name":"Docker (default)","exists":true}],"homedir":"/home/dockhand"} + * resp-403: Permission denied (needs environments:create or environments:edit) */ -export const GET: RequestHandler = async () => { +export const GET: RequestHandler = async ({ cookies }) => { + // Reports which container runtime this host runs and the account it runs as. The + // environment form is the only caller, and it opens under either permission, so + // requiring just one of them would refuse half the people who can reach it. + const auth = await authorize(cookies); + if ( + auth.authEnabled && + !(await auth.can('environments', 'create')) && + !(await auth.can('environments', 'edit')) + ) { + return json({ error: 'Permission denied' }, { status: 403 }); + } + const home = homedir(); // Common socket paths to check diff --git a/src/routes/api/networks/[id]/connect/+server.ts b/src/routes/api/networks/[id]/connect/+server.ts index 16b02a56e..2c68e3d48 100644 --- a/src/routes/api/networks/[id]/connect/+server.ts +++ b/src/routes/api/networks/[id]/connect/+server.ts @@ -4,6 +4,7 @@ import { connectContainerToNetwork, inspectNetwork } from '$lib/server/docker'; import { authorize } from '$lib/server/authorize'; import { auditNetwork } from '$lib/server/audit'; import { validateDockerIdParam } from '$lib/server/docker-validation'; +import { refuseOwnContainer } from '$lib/server/own-container-guard'; /** * @openapi @@ -16,7 +17,7 @@ import { validateDockerIdParam } from '$lib/server/docker-validation'; * resp-200: {success:boolean!} * resp-200-example: {"success":true} * resp-400: Container ID is required - * resp-403: Permission denied + * resp-403: Permission denied (needs networks:connect; the Dockhand container's own networks are admin-only) * resp-500: Failed to connect container to network */ export const POST: RequestHandler = async (event) => { @@ -46,6 +47,15 @@ export const POST: RequestHandler = async (event) => { const invalidContainer = validateDockerIdParam(containerId, 'container'); if (invalidContainer) return invalidContainer; + // Dockhand's own networking is administrator territory: the update helper + // reconnects the replacement container to whatever it finds attached here, so + // this is read back later by a privileged step. Docker takes a name wherever + // it takes an id, so resolve to the id before comparing. + if (auth.authEnabled && !auth.isAdmin) { + const denied = await refuseOwnContainer(containerId, envIdNum); + if (denied) return denied; + } + // Get network name for audit let networkName = params.id; try { diff --git a/src/routes/api/networks/[id]/disconnect/+server.ts b/src/routes/api/networks/[id]/disconnect/+server.ts index 18c73dcd5..1e9ce6595 100644 --- a/src/routes/api/networks/[id]/disconnect/+server.ts +++ b/src/routes/api/networks/[id]/disconnect/+server.ts @@ -4,6 +4,7 @@ import { disconnectContainerFromNetwork, inspectNetwork } from '$lib/server/dock import { authorize } from '$lib/server/authorize'; import { auditNetwork } from '$lib/server/audit'; import { validateDockerIdParam } from '$lib/server/docker-validation'; +import { refuseOwnContainer } from '$lib/server/own-container-guard'; /** * @openapi @@ -16,7 +17,7 @@ import { validateDockerIdParam } from '$lib/server/docker-validation'; * resp-200: {success:boolean!} * resp-200-example: {"success":true} * resp-400: Container ID is required - * resp-403: Permission denied + * resp-403: Permission denied (needs networks:disconnect; the Dockhand container's own networks are admin-only) * resp-500: Failed to disconnect container from network */ export const POST: RequestHandler = async (event) => { @@ -46,6 +47,15 @@ export const POST: RequestHandler = async (event) => { const invalidContainer = validateDockerIdParam(containerId, 'container'); if (invalidContainer) return invalidContainer; + // Dockhand's own networking is administrator territory: the update helper + // reconnects the replacement container to whatever it finds attached here, so + // this is read back later by a privileged step. Docker takes a name wherever + // it takes an id, so resolve to the id before comparing. + if (auth.authEnabled && !auth.isAdmin) { + const denied = await refuseOwnContainer(containerId, envIdNum); + if (denied) return denied; + } + // Get network name for audit let networkName = params.id; try { diff --git a/src/routes/api/roles/+server.ts b/src/routes/api/roles/+server.ts index 99065fb05..f1cc7c07d 100644 --- a/src/routes/api/roles/+server.ts +++ b/src/routes/api/roles/+server.ts @@ -10,18 +10,25 @@ import { auditRole } from '$lib/server/audit'; // GET /api/roles - List all roles /** * @openapi - * summary: List all roles (built-in and custom); available in setup mode or with an enterprise license + * summary: List all roles (built-in and custom); needs admin or users:view, plus an enterprise license * resp-200: array<{id:integer!, name:string!, description:string, isSystem:boolean!, permissions:{}}> - * resp-403: Enterprise license required + * resp-403: Enterprise license required, or the account may not view users * resp-500: Failed to read the roles */ export const GET: RequestHandler = async ({ cookies }) => { const auth = await authorize(cookies); - // Allow viewing roles when auth is disabled (setup mode) or with enterprise license - // This lets users see built-in roles before activating auth/enterprise - if (auth.authEnabled && !auth.isEnterprise) { - return json({ error: 'Enterprise license required' }, { status: 403 }); + // Before auth is switched on there is nobody to check, and the setup screens show + // the built-in roles. + if (auth.authEnabled) { + if (!auth.isEnterprise) { + return json({ error: 'Enterprise license required' }, { status: 403 }); + } + // A role carries its full permission matrix, which is a map of who may do what + // here. Reading it belongs with managing users, not with holding an account. + if (!auth.isAdmin && !(await auth.can('users', 'view'))) { + return json({ error: 'Permission denied' }, { status: 403 }); + } } try { @@ -51,7 +58,7 @@ export const POST: RequestHandler = async (event) => { const auth = await authorize(cookies); // Check enterprise license - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json({ error: 'Enterprise license required' }, { status: 403 }); } diff --git a/src/routes/api/roles/[id]/+server.ts b/src/routes/api/roles/[id]/+server.ts index 4f6b3f488..9b0a01026 100644 --- a/src/routes/api/roles/[id]/+server.ts +++ b/src/routes/api/roles/[id]/+server.ts @@ -13,20 +13,28 @@ import { clearTokenCache } from '$lib/server/api-tokens'; // GET /api/roles/[id] - Get a specific role /** * @openapi - * summary: Get a single role by id; available in setup mode or with an enterprise license + * summary: Get a single role by id; needs admin or users:view, plus an enterprise license * path: id:integer! Numeric id of the role (from GET /api/roles) * resp-200: {id:integer!, name:string!, description:string, isSystem:boolean!, permissions:{}} * resp-400: Role id is required - * resp-403: Enterprise license required + * resp-403: Enterprise license required, or the account may not view users * resp-404: Role not found * resp-500: Failed to read the role */ export const GET: RequestHandler = async ({ params, cookies }) => { const auth = await authorize(cookies); - // Allow viewing roles when auth is disabled (setup mode) or with enterprise license - if (auth.authEnabled && !auth.isEnterprise) { - return json({ error: 'Enterprise license required' }, { status: 403 }); + // Before auth is switched on there is nobody to check, and the setup screens show + // the built-in roles. + if (auth.authEnabled) { + if (!auth.isEnterprise) { + return json({ error: 'Enterprise license required' }, { status: 403 }); + } + // Same gate as the list route: a role carries its full permission matrix, and + // role ids are sequential, so reading them one at a time rebuilds the list. + if (!auth.isAdmin && !(await auth.can('users', 'view'))) { + return json({ error: 'Permission denied' }, { status: 403 }); + } } if (!params.id) { @@ -68,7 +76,7 @@ export const PUT: RequestHandler = async (event) => { const auth = await authorize(cookies); // Check enterprise license - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json({ error: 'Enterprise license required' }, { status: 403 }); } @@ -135,7 +143,7 @@ export const DELETE: RequestHandler = async (event) => { const auth = await authorize(cookies); // Check enterprise license - if (!auth.isEnterprise) { + if (!auth.hasValidLicense) { return json({ error: 'Enterprise license required' }, { status: 403 }); } diff --git a/src/routes/api/self-update/+server.ts b/src/routes/api/self-update/+server.ts index 652e371bb..69b2a1138 100644 --- a/src/routes/api/self-update/+server.ts +++ b/src/routes/api/self-update/+server.ts @@ -1,5 +1,6 @@ import { json } from '@sveltejs/kit'; import { authorize } from '$lib/server/authorize'; +import { planNetworkEnvVars } from '$lib/server/self-update-network-core'; import { getAdditionalVolumeBinds, dedupeVolumesForRecreate } from '$lib/server/mount-dedupe'; import { getOwnContainerId, @@ -191,52 +192,6 @@ function buildCreateConfig(inspectData: any, newImage: string): any { return createConfig; } -/** - * Build NETWORKS and NETWORK_OPTS_* env vars from inspect data's NetworkSettings. - * The sidecar uses these to reconnect networks via `docker network connect` CLI. - */ -function buildNetworkEnvVars(inspectData: any): string[] { - const networks: Record = inspectData.NetworkSettings?.Networks || {}; - const entries = Object.entries(networks); - if (entries.length === 0) return []; - - const networkNames: string[] = []; - const envVars: string[] = []; - - for (const [netName, netConfig] of entries) { - networkNames.push(netName); - - const nc = netConfig as any; - const opts: string[] = []; - - if (nc.IPAMConfig?.IPv4Address) { - opts.push(`--ip ${nc.IPAMConfig.IPv4Address}`); - } - if (nc.IPAMConfig?.IPv6Address) { - opts.push(`--ip6 ${nc.IPAMConfig.IPv6Address}`); - } - if (nc.Aliases && nc.Aliases.length > 0) { - for (const alias of nc.Aliases) { - opts.push(`--alias ${alias}`); - } - } - if (nc.Links && nc.Links.length > 0) { - for (const link of nc.Links) { - opts.push(`--link ${link}`); - } - } - - if (opts.length > 0) { - // Env var name: dots and dashes become underscores - const safeNetName = netName.replace(/[.-]/g, '_'); - envVars.push(`NETWORK_OPTS_${safeNetName}=${opts.join(' ')}`); - } - } - - envVars.unshift(`NETWORKS=${networkNames.join(' ')}`); - return envVars; -} - /** * SSE stream endpoint for self-update. * Pulls image, creates new container, then launches minimal sidecar. @@ -324,7 +279,13 @@ export const POST: RequestHandler = async ({ request, cookies }) => { } const inspectData = await inspectResponse.json(); const createConfig = buildCreateConfig(inspectData, newImage); - const networkEnvVars = buildNetworkEnvVars(inspectData); + const networkPlan = planNetworkEnvVars(inspectData.NetworkSettings?.Networks); + if (networkPlan.skipped.length > 0) { + send('log', { + message: `Not reconnecting ${networkPlan.skipped.length} network(s) whose names are not plain network names: ${networkPlan.skipped.join(', ')}` + }); + } + const networkEnvVars = networkPlan.envVars; send('log', { message: `Networks: ${networkEnvVars.length > 0 ? networkEnvVars[0] : 'default'}` }); sendStep('building_config', 'completed', 'Config ready'); diff --git a/src/routes/api/settings/semver/+server.ts b/src/routes/api/settings/semver/+server.ts index f30b7f8e7..f45c5782a 100644 --- a/src/routes/api/settings/semver/+server.ts +++ b/src/routes/api/settings/semver/+server.ts @@ -7,9 +7,16 @@ import { authorize } from '$lib/server/authorize'; * summary: Get the global newer-version-tag (semver) detection config * resp-200: {enabled:boolean!, maxBump:string!, matchFlavor:boolean!, includePrerelease:boolean!} * resp-200-example: {"enabled":true,"maxBump":"minor","matchFlavor":true,"includePrerelease":false} + * resp-403: Permission denied (needs settings:view) */ export const GET: RequestHandler = async ({ cookies }) => { - await authorize(cookies); + // Describes how this installation is operated, and the settings screen is its only + // reader - the update check and the scheduler read the config straight from the + // database, so gating here costs nothing elsewhere. + const auth = await authorize(cookies); + if (auth.authEnabled && !await auth.can('settings', 'view')) { + return json({ error: 'Permission denied' }, { status: 403 }); + } return json(await getGlobalSemverConfig()); }; diff --git a/src/routes/api/stacks/base-path/+server.ts b/src/routes/api/stacks/base-path/+server.ts index 2fde23741..69e1ab2e7 100644 --- a/src/routes/api/stacks/base-path/+server.ts +++ b/src/routes/api/stacks/base-path/+server.ts @@ -1,5 +1,6 @@ import { json } from '@sveltejs/kit'; import { getStacksBasePathForEnv } from '$lib/server/stacks'; +import { authorize } from '$lib/server/authorize'; import type { RequestHandler } from './$types'; /** @@ -10,17 +11,28 @@ import type { RequestHandler } from './$types'; * query: env:integer Environment ID used to select the local or environment-scoped stacks root * resp-200: {basePath:string!} * resp-200-example: {"basePath":"/data/stacks"} + * resp-403: Permission denied (needs stacks:view, and access to the environment) * * Returns the Dockhand stacks root for the requested environment context. * Query params: * - env: Environment ID (optional) — when set, returns STACKS_DIR for local envs * with STACKS_DIR configured, otherwise $DATA_DIR/stacks (staging / legacy). */ -export const GET: RequestHandler = async ({ url }) => { +export const GET: RequestHandler = async ({ url, cookies }) => { + // A host filesystem path, held to the same permission as the rest of the stack + // layout. Its only caller is the stack file browser. + const auth = await authorize(cookies); + const permDenied = await auth.requirePermission('stacks', 'view'); + if (permDenied) return permDenied; + const envParam = url.searchParams.get('env'); const envIdNum = envParam ? parseInt(envParam) : undefined; - const basePath = await getStacksBasePathForEnv( - envIdNum !== undefined && !Number.isNaN(envIdNum) ? envIdNum : undefined - ); + const resolvedEnvId = + envIdNum !== undefined && !Number.isNaN(envIdNum) ? envIdNum : undefined; + + const envDenied = await auth.requireEnvAccess(resolvedEnvId); + if (envDenied) return envDenied; + + const basePath = await getStacksBasePathForEnv(resolvedEnvId); return json({ basePath }); }; diff --git a/src/routes/api/stacks/default-path/+server.ts b/src/routes/api/stacks/default-path/+server.ts index 4fd6fac8f..ad8138051 100644 --- a/src/routes/api/stacks/default-path/+server.ts +++ b/src/routes/api/stacks/default-path/+server.ts @@ -2,6 +2,7 @@ import { json } from '@sveltejs/kit'; import { join } from 'path'; import { getStackDir } from '$lib/server/stacks'; import { getEnvironment } from '$lib/server/db'; +import { authorize } from '$lib/server/authorize'; import type { RequestHandler } from './$types'; /** @@ -17,13 +18,23 @@ import type { RequestHandler } from './$types'; * resp-200: {stackDir:string!, composePath:string!, envPath:string!, source:string!} * resp-200-example: {"stackDir":"/data/stacks/prod/web","composePath":"/data/stacks/prod/web/compose.yaml","envPath":"/data/stacks/prod/web/.env","source":"default"} * resp-400: Stack name is required + * resp-403: Permission denied (needs stacks:view, and access to the environment) */ -export const GET: RequestHandler = async ({ url }) => { +export const GET: RequestHandler = async ({ url, cookies }) => { + // The answer embeds the environment's name in a host path, so without the env + // check this walks ?env=1..N and reads back every environment's name. + const auth = await authorize(cookies); + const permDenied = await auth.requirePermission('stacks', 'view'); + if (permDenied) return permDenied; + const stackName = url.searchParams.get('name'); const envId = url.searchParams.get('env'); const location = url.searchParams.get('location'); const envIdNum = envId ? parseInt(envId) : undefined; + const envDenied = await auth.requireEnvAccess(envIdNum); + if (envDenied) return envDenied; + if (!stackName) { return json({ error: 'Stack name is required' }, { status: 400 }); } diff --git a/src/routes/api/users/+server.ts b/src/routes/api/users/+server.ts index 434a703b5..1a732f7e9 100644 --- a/src/routes/api/users/+server.ts +++ b/src/routes/api/users/+server.ts @@ -15,24 +15,28 @@ import { invalidateTokenCacheForUser } from '$lib/server/api-tokens'; import { auditUser } from '$lib/server/audit'; // GET /api/users - List all users -// Free for all - local users are needed for basic auth /** * @openapi - * summary: List all local/SSO users (any authenticated user may view — only mutations are RBAC-gated) + * summary: List all local/SSO users + * description: Requires users:view. The list carries every account's email address and says which ones are administrators. * resp-200: array<{id:integer!, username:string!, email:string, displayName:string, isAdmin:boolean!, isActive:boolean!, isSso:boolean!, authProvider:string!}> * resp-200-example: [{"id":1,"username":"admin","email":"admin@example.com","displayName":"Admin","isAdmin":true,"isActive":true,"isSso":false,"authProvider":"local"}] * resp-401: Not authenticated + * resp-403: Permission denied (missing users:view) * resp-500: Unexpected error while loading users */ export const GET: RequestHandler = async ({ cookies }) => { const auth = await authorize(cookies); - // When auth is enabled, require valid session (no specific permission needed to view users list) if (auth.authEnabled && !auth.isAuthenticated) { return json({ error: 'Authentication required' }, { status: 401 }); } - // Any authenticated user can view the users list - // Admin permissions are only needed for create/edit/delete operations + // Names, email addresses and which accounts hold the Admin role: a ready-made + // target list for phishing, so it is gated like any other personal data. Creating + // the very first account is unaffected - that path is a POST, and the hook lets it + // through while no user exists. + const denied = await auth.requirePermission('users', 'view'); + if (denied) return denied; try { const allUsers = await getUsers(); diff --git a/src/routes/api/users/[id]/+server.ts b/src/routes/api/users/[id]/+server.ts index 746ea9b92..72c16c781 100644 --- a/src/routes/api/users/[id]/+server.ts +++ b/src/routes/api/users/[id]/+server.ts @@ -19,21 +19,21 @@ import { computeAuditDiff } from '$lib/utils/diff'; import { invalidateTokenCacheForUser } from '$lib/server/api-tokens'; // GET /api/users/[id] - Get a specific user -// Free for all - local users are needed for basic auth /** * @openapi * summary: Get a single user by id (password hash is never returned; isAdmin is derived from role assignment) + * description: Requires users:view, except for one's own record. The payload names the account, its email, whether it holds the Admin role and whether MFA is set up. * path: id:integer! Numeric id of the user (from GET /api/users) * resp-200: {id:integer!, username:string!, email:string, displayName:string, mfaEnabled:boolean!, isAdmin:boolean!, isActive:boolean!, lastLogin:string, createdAt:string!, updatedAt:string!} * resp-400: User id is required * resp-401: Authentication required (auth is enabled and the caller is not authenticated) + * resp-403: Permission denied (missing users:view, reading another user's record) * resp-404: User not found * resp-500: Failed to read the user */ export const GET: RequestHandler = async ({ params, cookies }) => { const auth = await authorize(cookies); - // When auth is enabled, require authentication (any authenticated user can view) if (auth.authEnabled && !auth.isAuthenticated) { return json({ error: 'Authentication required' }, { status: 401 }); } @@ -42,6 +42,14 @@ export const GET: RequestHandler = async ({ params, cookies }) => { return json({ error: 'User ID is required' }, { status: 400 }); } + // Ids are sequential, so leaving this open would rebuild the whole directory a + // loop at a time - and this record says more than the list does, down to who has + // no MFA. Reading one's own record needs no permission. + if (Number(params.id) !== auth.user?.id) { + const denied = await auth.requirePermission('users', 'view'); + if (denied) return denied; + } + try { const id = parseInt(params.id); const user = await getUser(id); diff --git a/src/routes/api/users/[id]/roles/+server.ts b/src/routes/api/users/[id]/roles/+server.ts index 1fd71a6a9..43824d27d 100644 --- a/src/routes/api/users/[id]/roles/+server.ts +++ b/src/routes/api/users/[id]/roles/+server.ts @@ -1,6 +1,7 @@ import { json } from '@sveltejs/kit'; import type { RequestHandler } from '@sveltejs/kit'; import { isEnterprise } from '$lib/server/license'; +import { authorize } from '$lib/server/authorize'; import { validateSession } from '$lib/server/auth'; import { getUserRoles, @@ -16,10 +17,12 @@ import { invalidateTokenCacheForUser } from '$lib/server/api-tokens'; /** * @openapi * summary: List the roles assigned to a user (enterprise only) + * description: Requires users:view, except for one's own assignments. Which roles an account holds says what it can reach, so it is read like the account itself. * path: id:integer! Numeric id of the user (from GET /api/users) * resp-200: array<{roleId:integer!, name:string!, environmentId:integer}> * resp-400: User id is required - * resp-403: Enterprise license required + * resp-401: Not authenticated + * resp-403: Enterprise license required, or permission denied (missing users:view) * resp-404: User not found * resp-500: Failed to read the user roles */ @@ -29,6 +32,17 @@ export const GET: RequestHandler = async ({ params, cookies }) => { return json({ error: 'Enterprise license required' }, { status: 403 }); } + const auth = await authorize(cookies); + if (auth.authEnabled && !auth.isAuthenticated) { + return json({ error: 'Authentication required' }, { status: 401 }); + } + // Naming somebody's roles names what they can reach, which is the same map of the + // instance the account listing gives. Own assignments need no permission. + if (Number(params.id) !== auth.user?.id) { + const denied = await auth.requirePermission('users', 'view'); + if (denied) return denied; + } + if (!params.id) { return json({ error: 'User ID is required' }, { status: 400 }); } diff --git a/src/routes/settings/auth/AuthTab.svelte b/src/routes/settings/auth/AuthTab.svelte index 75f0e37bf..9f02ce245 100644 --- a/src/routes/settings/auth/AuthTab.svelte +++ b/src/routes/settings/auth/AuthTab.svelte @@ -17,7 +17,7 @@ Save } from 'lucide-svelte'; import { TogglePill, ToggleGroup } from '$lib/components/ui/toggle-pill'; - import { canAccess, authStore } from '$lib/stores/auth'; + import { canAccess, isAdmin, authStore } from '$lib/stores/auth'; import { licenseStore } from '$lib/stores/license'; // Sub-tab components @@ -46,6 +46,9 @@ // Authentication state let authSubTab = $state<'general' | 'local' | 'ldap' | 'sso' | 'roles'>('general'); let authEnabled = $state(false); + // Set when the server refuses to describe the auth setup, so the page can say that + // rather than render its defaults as though they were the configuration. + let authForbidden = $state(false); let authLoading = $state(true); let sessionTimeout = $state(86400); let neverExpire = $state(false); @@ -59,7 +62,10 @@ authLoading = true; try { const response = await fetch('/api/auth/settings'); - if (response.ok) { + if (response.status === 401 || response.status === 403) { + authForbidden = true; + } else if (response.ok) { + authForbidden = false; const data = await response.json(); authEnabled = data.authEnabled; // 0 is the "never expire" sentinel; keep the last real timeout for the input. @@ -157,6 +163,7 @@
+{#if !authForbidden}
@@ -185,6 +192,7 @@

+{/if}
@@ -198,48 +206,65 @@ General - - - - + + {#if $canAccess('users', 'view')} + + {/if} + + {#if $canAccess('settings', 'view')} + + {/if} + + {#if $isAdmin} + + {/if} + + {#if $canAccess('users', 'view')} + + {/if}
@@ -247,7 +272,20 @@ {#if authSubTab === 'general'}
- {#if authEnabled} + {#if authForbidden} + +
+ +
+

Authentication settings are not visible to you

+

+ Your account does not have permission to see how this instance + authenticates. Ask an administrator if you need it. +

+
+
+ {:else if authEnabled} @@ -314,28 +352,28 @@ {/if} -{#if authSubTab === 'local'} +{#if authSubTab === 'local' && $canAccess('users', 'view')}
{/if} -{#if authSubTab === 'ldap'} +{#if authSubTab === 'ldap' && $isAdmin}
{/if} -{#if authSubTab === 'sso'} +{#if authSubTab === 'sso' && $canAccess('settings', 'view')}
{/if} -{#if authSubTab === 'roles'} +{#if authSubTab === 'roles' && $canAccess('users', 'view')}
diff --git a/src/routes/settings/general/GeneralTab.svelte b/src/routes/settings/general/GeneralTab.svelte index fc92952f1..c1a89cd3c 100644 --- a/src/routes/settings/general/GeneralTab.svelte +++ b/src/routes/settings/general/GeneralTab.svelte @@ -335,8 +335,15 @@ services: semverMatchFlavor = c.matchFlavor ?? true; semverIncludePrerelease = c.includePrerelease ?? false; } - } catch { /* keep defaults */ } - semverLoaded = true; + // A refusal means the values on screen are the built-in defaults, never the + // configuration, so the save effect stays disarmed rather than writing a + // guess over it. Any other failure still arms it: the controls are visible + // and edits have to reach the server rather than vanish. + semverLoaded = res.status !== 401 && res.status !== 403; + } catch { + // The request never landed, so nothing says this account may not save. + semverLoaded = true; + } try { const res = await fetch('/api/settings/general'); @@ -864,6 +871,10 @@ services:
+ + {#if $canAccess('settings', 'view')} @@ -983,7 +994,12 @@ services:
+ {/if} + + {#if $canAccess('settings', 'view')} @@ -1005,7 +1021,12 @@ services: /> + {/if} + + {#if $canAccess('settings', 'view')} @@ -1266,6 +1287,7 @@ services:
+ {/if} diff --git a/tests/general-settings-visibility.test.ts b/tests/general-settings-visibility.test.ts new file mode 100644 index 000000000..f60ef74cc --- /dev/null +++ b/tests/general-settings-visibility.test.ts @@ -0,0 +1,111 @@ +import { describe, expect, test } from 'bun:test'; +import { + visibleGeneralSettings, + PRESENTATION_SETTINGS +} from '../src/lib/server/general-settings-visibility'; + +/** + * Who sees what in the general settings table. + * + * The page renders for everybody, so the values that decide how a date or a log line + * is drawn have to reach everybody. The rest describes how the installation is built + * and operated, and somebody who may not view settings should not learn it by opening + * a tab. + */ + +const ALL = { + // presentation + timeFormat: '24h', + dateFormat: 'DD.MM.YYYY', + fontSize: 'normal', + confirmDestructive: true, + defaultTimezone: 'UTC', + defaultComposeTemplate: 'services:\n app:\n image: nginx:alpine', + // operational + defaultGrypeArgs: '-o json -v {image}', + defaultTrivyArgs: 'image --format json {image}', + defaultGrypeImage: 'anchore/grype:v0.110.0', + defaultTrivyImage: 'aquasec/trivy:0.69.3', + defaultScannerNetworkMode: 'bridge', + defaultScannerDns: ['10.0.0.1'], + externalStackPaths: ['/opt/stacks', '/srv/compose'], + primaryStackLocation: '/opt/stacks', + scheduleCleanupCron: '0 3 * * *', + metricsCollectionInterval: 30000, + honorProxyLabels: true, + protectScannerImages: true +}; + +describe('somebody who may view settings', () => { + test('sees the table as it is', () => { + expect(visibleGeneralSettings(ALL, true)).toEqual(ALL); + }); +}); + +describe('somebody who may not', () => { + const visible = visibleGeneralSettings(ALL, false); + + test('keeps the compose template, which is a feature for people who author stacks', () => { + // Authoring a stack needs no settings permission, so withholding this would + // hand those users the built-in sample instead of their own starting point. + expect(visible.defaultComposeTemplate).toContain('nginx:alpine'); + }); + + test('still gets what the interface needs to draw itself', () => { + expect(visible.timeFormat).toBe('24h'); + expect(visible.dateFormat).toBe('DD.MM.YYYY'); + expect(visible.fontSize).toBe('normal'); + expect(visible.confirmDestructive).toBe(true); + expect(visible.defaultTimezone).toBe('UTC'); + }); + + test('learns nothing about the security scanners', () => { + // Their image versions say which published vulnerabilities this host still has. + for (const key of [ + 'defaultGrypeArgs', + 'defaultTrivyArgs', + 'defaultGrypeImage', + 'defaultTrivyImage' + ]) { + expect(key in visible).toBe(false); + } + }); + + test('learns nothing about the network those scanners run on', () => { + expect('defaultScannerNetworkMode' in visible).toBe(false); + expect('defaultScannerDns' in visible).toBe(false); + expect('honorProxyLabels' in visible).toBe(false); + }); + + test('learns nothing about the filesystem layout of the host', () => { + expect('externalStackPaths' in visible).toBe(false); + expect('primaryStackLocation' in visible).toBe(false); + }); + + test('learns nothing about how the installation is scheduled', () => { + expect('scheduleCleanupCron' in visible).toBe(false); + expect('metricsCollectionInterval' in visible).toBe(false); + }); + + test('hidden settings are absent, not blanked', () => { + // A key present with a null value would still say the setting exists and + // distinguish "hidden" from "never configured". + expect(Object.values(visible)).not.toContain(null); + expect(Object.keys(visible).length).toBeLessThan(Object.keys(ALL).length); + }); +}); + +describe('a setting nobody has classified yet', () => { + test('is private until somebody decides otherwise', () => { + // The allow-list is what makes this the default, and being wrong in this + // direction only costs a setting the UI has to ask for again. + const withNewKey = { ...ALL, someSettingAddedLater: 'secret-ish' }; + expect('someSettingAddedLater' in visibleGeneralSettings(withNewKey, false)).toBe(false); + }); + + test('the allow-list holds no operational key by accident', () => { + const operational = /grype|trivy|scanner|cron|interval|retention|path|proxy|stackLocation/i; + const slipped = PRESENTATION_SETTINGS.filter((k) => operational.test(k)); + expect(slipped).toEqual([]); + }); +}); diff --git a/tests/oidc-callback-core.test.ts b/tests/oidc-callback-core.test.ts new file mode 100644 index 000000000..f6c6c0bfe --- /dev/null +++ b/tests/oidc-callback-core.test.ts @@ -0,0 +1,326 @@ +import { describe, expect, test } from 'bun:test'; +import { + requireIdToken, + rejectedToken, + applyUserinfo, + describeTokenExchangeFailure, + redactTokenErrorBody +} from '../src/lib/server/oidc-callback-core'; + +/** + * What the callback does with the answers it gets. + * + * Verifying a token and merging claims have their own tests, but a check that is + * called and then ignored looks exactly like a check that works. These cover the + * decisions: a sign-in continues, or it stops and says why. + */ + +describe('a token response with nothing to verify', () => { + test('stops the sign-in rather than continuing without claims', () => { + // Carrying on here would mean building an identity from the userinfo endpoint + // alone, which nothing has proven belongs to this login. + for (const tokens of [undefined, null, {}, { id_token: '' }]) { + const step = requireIdToken(tokens as any); + expect(step.proceed).toBe(false); + if (!step.proceed) expect(step.error).toMatch(/no ID token/i); + } + }); + + test('a response carrying one continues', () => { + expect(requireIdToken({ id_token: 'a.b.c' }).proceed).toBe(true); + }); +}); + +describe('a token that failed verification', () => { + test('stops the sign-in and carries the reason', () => { + // The reason is what turns a support thread into a five-minute fix. + const step = rejectedToken(new Error('the ID token came from a different issuer')); + expect(step.proceed).toBe(false); + if (!step.proceed) expect(step.error).toContain('different issuer'); + }); + + test('a non-Error reason still reaches the user', () => { + const step = rejectedToken('something odd'); + expect(step.proceed).toBe(false); + if (!step.proceed) expect(step.error).toContain('something odd'); + }); +}); + +describe('what a userinfo response is allowed to change', () => { + const verified = { sub: 'alice', groups: ['viewers'] }; + + test('a response about the same person is merged', () => { + const step = applyUserinfo(verified, { sub: 'alice', email: 'alice@example.test' }, 'Keycloak'); + expect(step.proceed).toBe(true); + if (step.proceed) expect(step.claims.email).toBe('alice@example.test'); + }); + + test('a response about somebody else STOPS the sign-in', () => { + // The one that matters: userinfo is unsigned, its values override the token's, + // and the admin claim is read after this point. Ignoring the mismatch would let + // the weaker endpoint name the user and hand out administrator rights. + const step = applyUserinfo(verified, { sub: 'mallory', groups: ['admins'] }, 'Keycloak'); + + expect(step.proceed).toBe(false); + if (!step.proceed) expect(step.error).toMatch(/different account/i); + }); + + test('and the foreign claims never reach the caller', () => { + const step = applyUserinfo(verified, { sub: 'mallory', groups: ['admins'] }, 'Keycloak'); + expect(step.proceed).toBe(false); + // A refusal that still handed back merged claims would be no refusal at all. + expect((step as { claims?: unknown }).claims).toBeUndefined(); + }); + + test('a response naming nobody continues on the verified claims, and says so', () => { + const step = applyUserinfo(verified, { email: 'x@example.test' }, 'Keycloak'); + + expect(step.proceed).toBe(true); + if (step.proceed) { + expect(step.claims).toEqual(verified); + expect(step.warn).toMatch(/naming no subject/i); + } + }); + + test('no userinfo at all leaves the verified claims alone', () => { + const step = applyUserinfo(verified, null, 'Keycloak'); + expect(step.proceed).toBe(true); + if (step.proceed) expect(step.claims).toEqual(verified); + }); +}); + +describe('a provider that refuses the authorization code', () => { + // The first thing a new OIDC setup gets wrong is the secret or the redirect URI, + // and the provider says which. Reporting only that the exchange failed turns a + // one-line fix into a support thread. + test('carries the provider description, which names the actual mistake', () => { + const body = JSON.stringify({ + error: 'invalid_client', + error_description: 'Invalid client or Invalid client credentials' + }); + expect(describeTokenExchangeFailure(401, body)).toBe( + 'The provider refused the authorization code: Invalid client or Invalid client credentials' + ); + }); + + test('falls back to the error code when there is no description', () => { + expect(describeTokenExchangeFailure(400, JSON.stringify({ error: 'invalid_grant' }))).toBe( + 'The provider refused the authorization code: invalid_grant' + ); + }); + + test('a redirect-uri mismatch reads as itself', () => { + const body = JSON.stringify({ + error: 'invalid_grant', + error_description: 'Incorrect redirect_uri' + }); + expect(describeTokenExchangeFailure(400, body)).toContain('Incorrect redirect_uri'); + }); + + test('a non-JSON body leaves the status as the only fact available', () => { + // Some providers answer HTML, and a proxy in front may answer anything at all. + expect(describeTokenExchangeFailure(400, 'Bad Request')).toBe( + 'The provider refused the authorization code (HTTP 400)' + ); + }); + + test('a 5xx reads as unreachable, not as a refusal', () => { + // A gateway erroring is not the provider deciding anything about this sign-in, + // and "refused" would send somebody to check the client secret instead of the + // connection. + expect(describeTokenExchangeFailure(502, 'Bad Gateway')).toBe( + 'The provider could not be reached to exchange the authorization code (HTTP 502)' + ); + expect(describeTokenExchangeFailure(500, '')).toBe( + 'The provider could not be reached to exchange the authorization code (HTTP 500)' + ); + // A 4xx still reads as a refusal, because it is one. + expect(describeTokenExchangeFailure(401, '')).toBe( + 'The provider refused the authorization code (HTTP 401)' + ); + }); + + test('a 5xx that carries an OAuth body still reads as unreachable', () => { + // The status decides the verb. A maintenance notice arriving with a 503 is not + // the provider refusing this sign-in, and saying so would send somebody to + // check the client secret. + const body = JSON.stringify({ + error: 'temporarily_unavailable', + error_description: 'Service is down for maintenance' + }); + expect(describeTokenExchangeFailure(503, body)).toBe( + 'The provider could not be reached to exchange the authorization code: Service is down for maintenance' + ); + }); + + test('a non-string description is ignored rather than rendered as an object', () => { + // The body is whatever the provider sends. A number, an object or an array in + // that field must not reach the redirect URL as "[object Object]" - fall through + // to the error code, which is the next most useful thing. + for (const description of [42, null, { msg: 'x' }, ['a', 'b'], true]) { + const body = JSON.stringify({ error: 'invalid_grant', error_description: description }); + expect(describeTokenExchangeFailure(400, body)).toBe( + 'The provider refused the authorization code: invalid_grant' + ); + } + }); + + test('a non-string error code falls through to the status', () => { + expect(describeTokenExchangeFailure(400, JSON.stringify({ error: 99 }))).toBe( + 'The provider refused the authorization code (HTTP 400)' + ); + }); + + test('JSON that is not an object does not throw', () => { + // JSON.parse('null') succeeds and returns null, so reading .error off it would + // be a TypeError inside the handler rather than a message for the user. + for (const body of ['null', 'false', '"a bare string"', '[1,2,3]', '0']) { + expect(() => describeTokenExchangeFailure(400, body)).not.toThrow(); + expect(describeTokenExchangeFailure(400, body)).toBe( + 'The provider refused the authorization code (HTTP 400)' + ); + } + }); + + test('JSON without either field still reports the status', () => { + expect(describeTokenExchangeFailure(403, JSON.stringify({ message: 'nope' }))).toBe( + 'The provider refused the authorization code (HTTP 403)' + ); + }); + + test('a blank description does not produce a dangling colon', () => { + expect(describeTokenExchangeFailure(400, JSON.stringify({ error_description: ' ' }))).toBe( + 'The provider refused the authorization code (HTTP 400)' + ); + }); + + test('a blank description falls through to the code, not to the status', () => { + // A provider that sends the field empty still names the fault in `error`, and + // "invalid_grant" is worth more to whoever is fixing it than "HTTP 400". + expect( + describeTokenExchangeFailure(400, JSON.stringify({ error: 'invalid_grant', error_description: ' ' })) + ).toBe('The provider refused the authorization code: invalid_grant'); + expect( + describeTokenExchangeFailure(400, JSON.stringify({ error: 'invalid_grant', error_description: '' })) + ).toBe('The provider refused the authorization code: invalid_grant'); + }); + + test('truncation never splits a character, so the redirect can encode it', () => { + // The message is handed to encodeURIComponent on its way into /login?error=, + // and a lone surrogate makes that throw URIError - which would replace the + // provider's actual reason with "URI malformed". + const body = JSON.stringify({ error_description: 'x'.repeat(199) + '\u{1F600}' + 'tail' }); + const out = describeTokenExchangeFailure(400, body); + expect(() => encodeURIComponent(out)).not.toThrow(); + + // And when the text is nothing but astral characters. + const emoji = JSON.stringify({ error_description: '\u{1F600}'.repeat(300) }); + expect(() => encodeURIComponent(describeTokenExchangeFailure(400, emoji))).not.toThrow(); + }); + + test('the description is flattened and bounded', () => { + // It is free text from the provider, and it reaches a log line and a URL. + const long = 'x'.repeat(500); + const out = describeTokenExchangeFailure(400, JSON.stringify({ error_description: `a\n b ${long}` })); + expect(out).not.toContain('\n'); + expect(out.length).toBeLessThan(260); + expect(out).toContain('a b'); + }); +}); + +describe('a provider error body on its way into the log', () => { + // The exchange request carries the client secret and the authorization code, so a + // provider that echoes what it received would otherwise put both in our log. + test('a secret echoed back is masked', () => { + const body = 'error=invalid_client&client_secret=hunter2&client_id=dockhand'; + const out = redactTokenErrorBody(body); + expect(out).not.toContain('hunter2'); + expect(out).toContain(''); + // What is safe to see stays visible, or the log stops being useful. + expect(out).toContain('invalid_client'); + expect(out).toContain('dockhand'); + }); + + test('a JSON body is masked the same way', () => { + const body = JSON.stringify({ error: 'invalid_client', client_secret: 'hunter2' }); + const out = redactTokenErrorBody(body); + expect(out).not.toContain('hunter2'); + expect(out).toContain('invalid_client'); + }); + + test('the authorization code is masked too', () => { + expect(redactTokenErrorBody('error=invalid_grant&code=abc123xyz')).not.toContain('abc123xyz'); + }); + + test('a token echoed back is masked', () => { + for (const field of ['access_token', 'refresh_token', 'id_token']) { + expect(redactTokenErrorBody(`${field}=sensitive-value-here`)).not.toContain( + 'sensitive-value-here' + ); + } + }); + + test('an ordinary body is left readable', () => { + const body = JSON.stringify({ error: 'invalid_grant', error_description: 'Incorrect redirect_uri' }); + expect(redactTokenErrorBody(body)).toContain('Incorrect redirect_uri'); + }); + + test('a page of HTML cannot flood the log', () => { + const out = redactTokenErrorBody('' + 'x'.repeat(5000) + ''); + expect(out.length).toBeLessThan(560); + expect(out).toContain('truncated'); + }); + + test('a renamed secret field does not slip past', () => { + // The field name is the provider's to choose in its echo, so the match is on the + // name as a suffix rather than an exact string. + for (const body of [ + 'oauth_client_secret=hunter2', + JSON.stringify({ clientSecret: 'hunter2' }), + JSON.stringify({ app_client_secret: 'hunter2' }) + ]) { + expect(redactTokenErrorBody(body)).not.toContain('hunter2'); + } + }); + + test('a request echoed back as a JSON string is still masked', () => { + // Some providers answer with the whole request embedded as a string, so the + // quotes around the field arrive escaped. + const inner = JSON.stringify({ client_secret: 'hunter2', code: 'abc123' }); + const out = redactTokenErrorBody(JSON.stringify(inner)); + expect(out).not.toContain('hunter2'); + expect(out).not.toContain('abc123'); + }); + + test('a diagnostic field that merely ends in code stays readable', () => { + // Masking error_code or status_code would take away the very thing the log is + // being read for. + const body = JSON.stringify({ error: 'invalid_grant', error_code: 'AADSTS70008', status_code: 400 }); + const out = redactTokenErrorBody(body); + expect(out).toContain('AADSTS70008'); + expect(out).toContain('400'); + }); + + test('a megabyte of hostile text does not hang the process', () => { + // The body comes from a remote service, and the pattern must never be the way + // it gets to spend our CPU. Bounding before matching is what makes this fast. + for (const body of ['_'.repeat(1_000_000), 'client_secre'.repeat(80_000), 'a='.repeat(500_000)]) { + const started = Date.now(); + redactTokenErrorBody(body); + expect(Date.now() - started).toBeLessThan(1000); + } + }); + + test('a secret holding a space or a comma is masked whole', () => { + // The value runs to the delimiter of its own encoding, so a JSON secret with an + // odd character inside does not leave its tail behind in the log. + for (const secret of ['hun ter2secret', 'hun,ter2secret', 'hun}ter2secret']) { + const out = redactTokenErrorBody(JSON.stringify({ client_secret: secret })); + expect(out).not.toContain('ter2secret'); + } + }); + + test('an empty body says so rather than logging nothing', () => { + expect(redactTokenErrorBody('')).toBe('(empty)'); + }); +}); diff --git a/tests/oidc-callback-wiring.test.ts b/tests/oidc-callback-wiring.test.ts new file mode 100644 index 000000000..d43113b65 --- /dev/null +++ b/tests/oidc-callback-wiring.test.ts @@ -0,0 +1,113 @@ +import { describe, expect, test } from 'bun:test'; +import { readFileSync } from 'node:fs'; + +/** + * That the callback ACTS on the answers its checks give it. + * + * `handleOidcCallback` lives in auth.ts, which reaches the database through + * better-sqlite3 - a module bun cannot load - so no unit test can call it. The + * decisions themselves are covered in oidc-callback-core.test.ts; what is left, and + * what this covers, is the wiring: a refusal that is computed and then dropped on the + * floor grants exactly the sign-in it was meant to stop, and reads as working code. + * + * Source inspection is the available instrument, so these assertions are deliberately + * narrow: they name the specific line that must remain, not the shape of the function. + */ + +const AUTH = readFileSync(new URL('../src/lib/server/auth.ts', import.meta.url), 'utf8'); + +/** The body of handleOidcCallback, so a match elsewhere in the file cannot satisfy these. */ +const CALLBACK = (() => { + const start = AUTH.indexOf('export async function handleOidcCallback'); + expect(start).toBeGreaterThan(-1); + const end = AUTH.indexOf('\nexport ', start + 1); + return AUTH.slice(start, end === -1 ? undefined : end); +})(); + +describe('the callback acts on its own checks', () => { + test('a token response with no id_token ends the sign-in', () => { + expect(CALLBACK).toContain('requireIdToken(tokens)'); + // Computed and ignored would mean building an identity from userinfo alone. + expect(CALLBACK).toMatch(/if \(!present\.proceed\)[\s\S]{0,120}return \{\s*success: false/); + }); + + test('a token that fails verification ends the sign-in', () => { + expect(CALLBACK).toContain('verifyIdToken('); + // The throw has to become a refusal, not a warning the flow continues past. + expect(CALLBACK).toMatch(/catch \(error\)[\s\S]{0,400}return \{ success: false/); + }); + + test('a userinfo response about a different subject ends the sign-in', () => { + // The one that matters most: userinfo is unsigned, its values override the + // token's, and the admin claim is read after the merge. + expect(CALLBACK).toContain('applyUserinfo(claims, userinfo, config.name)'); + expect(CALLBACK).toMatch(/if \(!step\.proceed\)[\s\S]{0,200}return \{ success: false, error: step\.error \}/); + }); + + test('the merged claims come from the step, never from the raw response', () => { + // `claims = userinfo` or `{...claims, ...userinfo}` would skip the check + // entirely while still calling it. + expect(CALLBACK).toContain('claims = step.claims'); + expect(CALLBACK).not.toMatch(/claims\s*=\s*\{\s*\.\.\.claims,\s*\.\.\.userinfo/); + expect(CALLBACK).not.toMatch(/claims\s*=\s*userinfo/); + }); + + test('admin rights are decided from the verified claims', () => { + expect(CALLBACK).toContain('claimsGrantAdmin(claims'); + }); +}); + +describe('the reason reaches the login page intact', () => { + // The refusal travels as /login?error=, and a provider is free to put a + // literal '%' in it ("Client secret is 100% wrong"). searchParams.get() already + // decodes, so decoding a second time throws URIError - and that throw lands before + // the page fetches its providers, leaving no way to sign in at all. + const page = readFileSync(new URL('../src/routes/login/+page.svelte', import.meta.url), 'utf8'); + + test('the login page does not decode what searchParams already decoded', () => { + expect(page).not.toMatch(/error\s*=\s*decodeURIComponent\(urlError\)/); + expect(page).toMatch(/const urlError = \$derived\(\$page\.url\.searchParams\.get\('error'\)\)/); + }); + + test('a percent in the reason survives the round trip the callback uses', () => { + // Exactly what the callback does: encodeURIComponent into the query, then the + // page reads it back. + const reason = 'The provider refused the authorization code: Client secret is 100% wrong'; + const url = new URL(`https://example.invalid/login?error=${encodeURIComponent(reason)}`); + expect(url.searchParams.get('error')).toBe(reason); + }); +}); + +describe('a refused token exchange reports what the provider said', () => { + // The reason is computed in oidc-callback-core and tested there. What is left is + // that auth.ts USES it: reverting to a fixed string, or dropping the provider name + // from the log, leaves every test green while undoing the whole point. + const auth = readFileSync(new URL('../src/lib/server/auth.ts', import.meta.url), 'utf8'); + const block = auth.slice( + auth.indexOf('if (!tokenResponse.ok)'), + auth.indexOf('const tokens = await tokenResponse.json()') + ); + + test('the returned error is the described one, not a fixed string', () => { + expect(block).toContain('describeTokenExchangeFailure(tokenResponse.status, errorBody)'); + expect(block).not.toMatch(/error:\s*'Failed to exchange the authorization code'/); + }); + + test('both log lines name the provider and carry the OIDC prefix', () => { + // Without these a second configured provider makes the log unreadable, and + // `grep OIDC` misses the failure entirely. + const logs = block.match(/console\.error\(`[^`]*`\)/g) ?? []; + expect(logs.length).toBeGreaterThanOrEqual(2); + for (const line of logs) { + expect(line).toContain('[OIDC]'); + expect(line).toContain('${config.name}'); + } + }); + + test('the body is redacted before it reaches the log', () => { + // The request carried the client secret; a provider that echoes it back must + // not put it in our log. + expect(block).toContain('redactTokenErrorBody(errorBody)'); + expect(block).not.toMatch(/\$\{errorBody\}/); + }); +}); diff --git a/tests/own-container-core.test.ts b/tests/own-container-core.test.ts new file mode 100644 index 000000000..d52d1329f --- /dev/null +++ b/tests/own-container-core.test.ts @@ -0,0 +1,116 @@ +import { describe, expect, test } from 'bun:test'; +import { targetsOwnContainer } from '../src/lib/server/own-container-core'; + +/** + * Recognising the container Dockhand itself runs in. + * + * Changing its networking is not the same act as changing an application's: the + * update helper reconnects the replacement to whatever it finds attached, so what is + * attached here is read back by a privileged step. Attaching a network is its own + * permission, so without this an account holding it could arrange that input without + * holding the administrator role. + * + * The comparison is id to id. Docker takes a container NAME wherever it takes an id, + * so the caller resolves what it was given before asking. + */ + +const LONG = 'a'.repeat(64); +const SHORT = LONG.slice(0, 12); + +describe('the same container', () => { + test('the identical id', () => { + expect(targetsOwnContainer(LONG, { id: LONG })).toBe(true); + }); + + test('a short id against the long one, and the reverse', () => { + // A caller may hold either form; docker treats them as the same container. + expect(targetsOwnContainer(SHORT, { id: LONG })).toBe(true); + expect(targetsOwnContainer(LONG, { id: SHORT })).toBe(true); + }); + + test('case does not hide it', () => { + expect(targetsOwnContainer(LONG.toUpperCase(), { id: LONG })).toBe(true); + }); +}); + +describe('some other container', () => { + test('a different id', () => { + expect(targetsOwnContainer('b'.repeat(64), { id: LONG })).toBe(false); + }); + + test('an id differing only in the last character of the short form', () => { + // The near miss: the comparison runs over the shorter length, so this says + // whether it compares at all rather than matching any prefix. + expect(targetsOwnContainer(SHORT.slice(0, 11) + 'b', { id: LONG })).toBe(false); + }); +}); + +describe('anything that is not a resolved id', () => { + test('a name never matches, however it is spelled', () => { + // Docker accepts these wherever it accepts an id, so the caller must resolve + // one to an id first; comparing the string it was handed is not the same test. + for (const ref of ['dockhand', '/dockhand', 'DOCKHAND']) { + expect(targetsOwnContainer(ref, { id: LONG })).toBe(false); + } + }); + + test('a short non-hex string is not treated as an id', () => { + expect(targetsOwnContainer('aaaaaaaaaaaz', { id: LONG })).toBe(false); + }); + + test('empty and non-string values', () => { + for (const v of ['', ' ', null, undefined]) { + expect(targetsOwnContainer(v as string, { id: LONG })).toBe(false); + } + }); +}); + +describe('when our own identity is unknown', () => { + test('nothing matches, so unrelated containers stay manageable', () => { + // getOwnContainerId returns null outside Docker or where cgroup cannot be read. + expect(targetsOwnContainer(LONG, { id: null })).toBe(false); + expect(targetsOwnContainer(LONG, {})).toBe(false); + }); +}); + +describe('the guard is wired into both network endpoints', () => { + // Those routes reach the database through better-sqlite3, which bun cannot load, + // so the source is the available instrument. A guard that is written and never + // called reads exactly like one that works. + const read = (p: string) => Bun.file(new URL(p, import.meta.url)).text(); + + for (const route of ['connect', 'disconnect'] as const) { + test(`${route} refuses a non-admin aiming at the Dockhand container`, async () => { + const src = await read(`../src/routes/api/networks/[id]/${route}/+server.ts`); + // The whole condition, not its parts: `false && refuse(...)` would mention + // every piece while refusing nobody. + expect(src).toMatch( + /if \(auth\.authEnabled && !auth\.isAdmin\) \{\s*const denied = await refuseOwnContainer\(containerId, envIdNum\);\s*if \(denied\) return denied;/ + ); + // Fail-closed: the refusal must precede the docker call, not follow it. + const guard = src.indexOf('refuseOwnContainer'); + const action = src.indexOf( + route === 'connect' ? 'connectContainerToNetwork(' : 'disconnectContainerFromNetwork(' + ); + expect(guard).toBeGreaterThan(-1); + expect(action).toBeGreaterThan(-1); + expect(guard).toBeLessThan(action); + }); + } + + test('the guard resolves the reference before comparing it', async () => { + // Comparing the caller's string directly is what lets a name walk past: docker + // resolves it, so the guard has to resolve it too. + const src = await read('../src/lib/server/own-container-guard.ts'); + expect(src).toContain('await inspectContainer(containerRef, envId)'); + // The comparison decides the 403, and nothing returns ahead of it: an early + // `return null` would leave every assertion above true while refusing nobody. + const body = src.slice(src.indexOf('export async function refuseOwnContainer')); + const compare = body.indexOf('targetsOwnContainer(resolvedId'); + const earlyReturns = [...body.slice(0, compare).matchAll(/^\treturn /gm)]; + expect(earlyReturns).toHaveLength(0); + expect(body).toMatch( + /if \(!targetsOwnContainer\(resolvedId, \{ id: ownId \}\)\) return null;\s*return json\(/ + ); + }); +}); diff --git a/tests/public-paths.test.ts b/tests/public-paths.test.ts new file mode 100644 index 000000000..77e907b6b --- /dev/null +++ b/tests/public-paths.test.ts @@ -0,0 +1,88 @@ +import { describe, expect, test } from 'bun:test'; +import { + isPublicPath, + PUBLIC_EXACT, + PUBLIC_PREFIXES +} from '../src/lib/server/public-paths-core'; + +describe('an exact entry covers only itself', () => { + test('the licence route is public', () => { + expect(isPublicPath('/api/license')).toBe(true); + }); + + test('but nothing underneath it is', () => { + // A route added beneath an exact entry starts out protected, so nobody publishes + // one by accident. + expect(isPublicPath('/api/license/export')).toBe(false); + expect(isPublicPath('/api/license/keys')).toBe(false); + }); + + test('the same holds for every other exact entry', () => { + for (const path of PUBLIC_EXACT) { + expect(isPublicPath(path)).toBe(true); + expect(isPublicPath(`${path}/anything`)).toBe(false); + } + }); +}); + +describe('a prefix entry covers its subtree, which is the point of it', () => { + test('the health probes include their database check', () => { + expect(isPublicPath('/api/health')).toBe(true); + expect(isPublicPath('/api/health/database')).toBe(true); + }); + + test('the OIDC dance spans several callbacks', () => { + expect(isPublicPath('/api/auth/oidc')).toBe(true); + expect(isPublicPath('/api/auth/oidc/callback')).toBe(true); + expect(isPublicPath('/api/auth/oidc/1/initiate')).toBe(true); + }); + + test('the docs page loads its own viewer', () => { + expect(isPublicPath('/api/docs')).toBe(true); + expect(isPublicPath('/api/docs/ui')).toBe(true); + }); + + test('the prefix list stays short, because each entry opens a whole subtree', () => { + expect(PUBLIC_PREFIXES.sort()).toEqual(['/api/auth/oidc', '/api/docs', '/api/health']); + }); +}); + +describe('routes that must never be public', () => { + const mustBeProtected = [ + '/api/users', + '/api/users/1', + '/api/containers', + '/api/environments', + '/api/settings/general', + '/api/hawser/connect', + '/api/hawser/tokens', + '/api/notifications/trigger-test', + '/api/roles', + '/api/audit', + // A version-exact inventory says which published advisories apply here, and the + // only screen that shows it is behind a login anyway. + '/api/dependencies' + ]; + + for (const path of mustBeProtected) { + test(path, () => { + expect(isPublicPath(path)).toBe(false); + }); + } +}); + +describe('a near miss does not slip through', () => { + test('a path that merely starts with an entry is not public', () => { + // '/api/licenses' shares a prefix with '/api/license' but is a different route. + expect(isPublicPath('/api/licenses')).toBe(false); + expect(isPublicPath('/api/health-check')).toBe(false); + expect(isPublicPath('/loginx')).toBe(false); + }); + + test('the webhook exceptions still match only their own shape', () => { + expect(isPublicPath('/api/git/webhook/12')).toBe(true); + expect(isPublicPath('/api/git/stacks/3/webhook')).toBe(true); + expect(isPublicPath('/api/git/webhook/abc')).toBe(false); + expect(isPublicPath('/api/git/webhook/12/extra')).toBe(false); + }); +}); diff --git a/tests/self-update-network-core.test.ts b/tests/self-update-network-core.test.ts new file mode 100644 index 000000000..60a387c06 --- /dev/null +++ b/tests/self-update-network-core.test.ts @@ -0,0 +1,154 @@ +import { describe, expect, test } from 'bun:test'; +import { isSafeNetworkName, planNetworkEnvVars } from '../src/lib/server/self-update-network-core'; + +/** + * What the update sidecar is allowed to be told about a container's networks. + * + * The sidecar reads these names from its environment and passes them to the docker + * CLI. Creating a network is a permission of its own, so on an instance with roles + * the person who chose a name need not be the administrator who later runs the + * update - which makes the name untrusted input to a privileged step. Docker stores + * almost anything: `docker network create 'a;id;b'` succeeds. + */ + +describe('a name that is plainly a network name', () => { + test('the ordinary shapes travel', () => { + for (const name of ['bridge', 'my_net', 'my-net', 'my.net', 'proj_default', 'net123']) { + expect(isSafeNetworkName(name)).toBe(true); + } + }); +}); + +describe('a name that is something else', () => { + test('shell metacharacters are refused', () => { + for (const name of ['a;id;b', 'a$(id)b', 'a`id`b', 'a|id', 'a&b', 'a>b', "a'b", 'a"b']) { + expect(isSafeNetworkName(name)).toBe(false); + } + }); + + test('a name that would read as a flag is refused', () => { + // It reaches the CLI as an argument, where a leading dash is an option. + expect(isSafeNetworkName('--rm')).toBe(false); + expect(isSafeNetworkName('-v/:/host')).toBe(false); + }); + + test('whitespace is refused, because the sidecar splits on it', () => { + // NETWORKS is a space-separated list, so a name with a space is two names. + for (const name of ['a b', 'a\tb', 'a\nb']) { + expect(isSafeNetworkName(name)).toBe(false); + } + }); + + test('an absurd length is refused', () => { + expect(isSafeNetworkName('a'.repeat(257))).toBe(false); + }); + + test('nothing that is not a string', () => { + for (const v of [null, undefined, 42, {}, []]) expect(isSafeNetworkName(v)).toBe(false); + }); +}); + +describe('planning what the sidecar reconnects', () => { + test('an ordinary container keeps its networks and options', () => { + const plan = planNetworkEnvVars({ + 'my.net': { IPAMConfig: { IPv4Address: '10.0.0.5' }, Aliases: ['web'] } + }); + expect(plan.envVars).toEqual(['NETWORKS=my.net', 'NETWORK_OPTS_my_net=--ip 10.0.0.5 --alias web']); + expect(plan.skipped).toEqual([]); + }); + + test('a hostile name is dropped and reported, not escaped', () => { + // Coming back on one fewer network is a smaller failure than a name reaching + // a shell, and the caller has to be able to say which was left off. + const plan = planNetworkEnvVars({ bridge: {}, 'a;id;b': {} }); + expect(plan.envVars).toEqual(['NETWORKS=bridge']); + expect(plan.skipped).toEqual(['a;id;b']); + }); + + test('a container on nothing but a hostile network reconnects to nothing', () => { + const plan = planNetworkEnvVars({ 'a;touch /tmp/pwn;b': {} }); + expect(plan.envVars).toEqual([]); + expect(plan.skipped).toHaveLength(1); + }); + + test('an option value that is not plainly an address or alias is left off', () => { + // Each lands in an unquoted expansion in the sidecar, so a space in one would + // become a second argument to docker. + const plan = planNetworkEnvVars({ + netA: { IPAMConfig: { IPv4Address: '10.0.0.5 --privileged' }, Aliases: ['ok', 'not ok'] } + }); + expect(plan.envVars).toEqual(['NETWORKS=netA', 'NETWORK_OPTS_netA=--alias ok']); + }); + + test('no networks at all', () => { + expect(planNetworkEnvVars({})).toEqual({ envVars: [], skipped: [] }); + expect(planNetworkEnvVars(null)).toEqual({ envVars: [], skipped: [] }); + expect(planNetworkEnvVars(undefined)).toEqual({ envVars: [], skipped: [] }); + }); + + test('a network with no options contributes only its name', () => { + expect(planNetworkEnvVars({ bridge: {} }).envVars).toEqual(['NETWORKS=bridge']); + }); +}); + +describe('the env var names the sidecar looks up', () => { + test('dots and dashes become underscores, as the sidecar expects', () => { + // The sidecar builds the variable name the same way; if the two disagree the + // options are silently lost and the container comes back without them. + const plan = planNetworkEnvVars({ 'a-b.c': { Aliases: ['z'] } }); + expect(plan.envVars).toEqual(['NETWORKS=a-b.c', 'NETWORK_OPTS_a_b_c=--alias z']); + }); + + test('several networks share one NETWORKS line', () => { + const plan = planNetworkEnvVars({ bridge: {}, backend: { Aliases: ['api'] } }); + expect(plan.envVars[0]).toBe('NETWORKS=bridge backend'); + expect(plan.envVars).toContain('NETWORK_OPTS_backend=--alias api'); + }); + + test('a name with whitespace never reaches the space-separated list', () => { + // NETWORKS is split on whitespace, so a name containing any would become two. + const plan = planNetworkEnvVars({ 'a b': {}, ok: {} }); + expect(plan.envVars).toEqual(['NETWORKS=ok']); + expect(plan.skipped).toEqual(['a b']); + }); +}); + +describe('names the daemon itself accepts', () => { + test('a leading underscore or dot is kept', () => { + // Verified against a real daemon: `docker network create _leading` succeeds, + // so dropping it would disconnect a network that genuinely exists. + const plan = planNetworkEnvVars({ _leading: {}, '.leading': {} }); + expect(plan.skipped).toEqual([]); + expect(plan.envVars[0]).toBe('NETWORKS=_leading .leading'); + }); + + test('a leading dash is still refused', () => { + // The daemon refuses it too, and it would reach the CLI as a flag. + const plan = planNetworkEnvVars({ '-leading': {}, ok: {} }); + expect(plan.envVars).toEqual(['NETWORKS=ok']); + expect(plan.skipped).toEqual(['-leading']); + }); +}); + +describe('addresses a real deployment uses', () => { + test('an IPv6 address starting with a colon is kept', () => { + // The compressed form is legal and a user may pin one in IPAMConfig; dropping + // it would bring the container back without its address. + const plan = planNetworkEnvVars({ v6: { IPAMConfig: { IPv6Address: '::1' } } }); + expect(plan.envVars).toContain('NETWORK_OPTS_v6=--ip6 ::1'); + }); + + test('a value that would read as a flag is still dropped', () => { + // The expansion is unquoted, so a leading dash reaches docker as an option. + const plan = planNetworkEnvVars({ n: { Aliases: ['--privileged'], IPAMConfig: { IPv4Address: '-rm' } } }); + expect(plan.envVars).toEqual(['NETWORKS=n']); + }); + + test('the compose default and other ordinary names survive', () => { + // Dropping one of these would silently disconnect a real deployment. + const names = ['bridge', 'myproject_default', 'my-app_net', 'web.prod', 'App_Net', '2fa-net']; + const plan = planNetworkEnvVars(Object.fromEntries(names.map((n) => [n, {}]))); + expect(plan.skipped).toEqual([]); + expect(plan.envVars[0]).toBe(`NETWORKS=${names.join(' ')}`); + }); +}); diff --git a/tests/settings-visibility-wiring.test.ts b/tests/settings-visibility-wiring.test.ts new file mode 100644 index 000000000..654b601dd --- /dev/null +++ b/tests/settings-visibility-wiring.test.ts @@ -0,0 +1,61 @@ +import { describe, expect, test } from 'bun:test'; +import { readFileSync } from 'node:fs'; + +/** + * That the two permission gates are actually wired to the handlers. + * + * The rules themselves are covered in general-settings-visibility.test.ts, but a filter + * that is written and never applied reads exactly like one that works: reverting either + * handler to its unfiltered form leaves the whole suite green. These endpoints live in + * SvelteKit route files that reach the database through better-sqlite3, which bun + * cannot load, so the source is the available instrument. + */ + +const read = (path: string) => readFileSync(new URL(path, import.meta.url), 'utf8'); + +describe('general settings are filtered before they are returned', () => { + const handler = read('../src/routes/api/settings/general/+server.ts'); + // GET only: the POST below it saves the table and already requires settings:edit, + // so returning the whole thing there is right. + const GET = handler.slice( + handler.indexOf('export const GET'), + handler.indexOf('export const POST') + ); + + test('the response goes through the filter, not straight out', () => { + expect(GET).toContain('visibleGeneralSettings(settings'); + // `return json(settings)` would hand every operational value to any caller. + expect(GET).not.toMatch(/return json\(settings\)/); + }); + + test('and the filter is told the caller permission, not a constant', () => { + // Passing `true` would keep the call and lose the point of it. + expect(GET).toMatch(/visibleGeneralSettings\(settings,\s*await auth\.can\('settings',\s*'view'\)\)/); + }); +}); + +describe('the role permission matrix needs permission to read', () => { + const list = read('../src/routes/api/roles/+server.ts'); + const byId = read('../src/routes/api/roles/[id]/+server.ts'); + + const gateOf = (source: string) => + source.slice(source.indexOf('export const GET'), source.indexOf('try {')); + + test('the list route checks more than the licence', () => { + expect(gateOf(list)).toMatch(/auth\.isAdmin[\s\S]{0,80}auth\.can\('users',\s*'view'\)/); + }); + + test('so does the by-id route, which role ids walk one at a time', () => { + // Sequential ids mean an ungated by-id route rebuilds the list the other one + // refuses to serve. + expect(gateOf(byId)).toMatch(/auth\.isAdmin[\s\S]{0,80}auth\.can\('users',\s*'view'\)/); + }); + + test('both still answer during setup, before auth is switched on', () => { + // Gating on an account that does not exist yet would lock the first admin out + // of the screens that create one. + for (const gate of [gateOf(list), gateOf(byId)]) { + expect(gate).toContain('if (auth.authEnabled)'); + } + }); +}); diff --git a/updater/update.sh b/updater/update.sh index 62408fed7..1eef24f57 100644 --- a/updater/update.sh +++ b/updater/update.sh @@ -41,7 +41,7 @@ log "Container renamed to $CONTAINER_NAME" if [ -n "$NETWORKS" ]; then for NET in $NETWORKS; do OPTS_VAR="NETWORK_OPTS_$(echo "$NET" | tr '.-' '__')" - OPTS=$(eval echo "\$$OPTS_VAR" 2>/dev/null || true) + OPTS=$(env | grep -F -m1 -- "$OPTS_VAR=" | cut -d= -f2-) log "Connecting to network $NET ${OPTS:+($OPTS)}" # shellcheck disable=SC2086 docker network connect $OPTS "$NET" "$NEW_CONTAINER_ID" || log " Warning: failed to connect to $NET" From 4e27152dd5fcbcc0e4bb2c3d71d75dd771892f8e Mon Sep 17 00:00:00 2001 From: Jarek Krochmalski Date: Tue, 29 Sep 2026 14:58:14 +0200 Subject: [PATCH 04/22] smaller fixes and internal cleanups across scheduling, env vars, hawser and the build --- scripts/generate-openapi.ts | 3 +- scripts/openapi/lib.ts | 40 ++- server.js | 69 ++-- src/lib/components/StackEnvVarsPanel.svelte | 83 ++--- src/lib/server/hawser.ts | 14 +- src/lib/server/podman-network-key.ts | 50 +++ src/lib/server/reader-abort-core.ts | 13 + src/lib/server/subprocess-manager.ts | 29 +- src/lib/utils/backup.ts | 24 ++ src/lib/utils/env-file-values.ts | 205 +++++++++++ src/lib/utils/env-panel-core.ts | 100 ++++++ .../auto-update/[containerName]/+server.ts | 3 + src/routes/api/dependencies/+server.ts | 13 +- .../environments/[id]/disk-warning/+server.ts | 30 +- src/routes/api/events/+server.ts | 1 + src/routes/api/hawser/connect/+server.ts | 42 ++- src/routes/api/images/[id]/export/+server.ts | 34 +- src/routes/api/schedules/+server.ts | 1 + .../api/schedules/[type]/[id]/+server.ts | 1 + .../api/schedules/[type]/[id]/run/+server.ts | 1 + .../schedules/[type]/[id]/toggle/+server.ts | 1 + src/routes/api/settings/general/+server.ts | 11 +- src/routes/containers/+page.svelte | 9 + .../containers/ContainerInspectModal.svelte | 63 +++- .../environments/EnvironmentModal.svelte | 19 + .../environments/tabs/ActivityTab.svelte | 62 +++- tests/compose-validate/config-errors.test.ts | 47 +++ tests/env-file-values.test.ts | 334 ++++++++++++++++++ tests/env-panel-core.test.ts | 179 ++++++++++ tests/image-export-name.test.ts | 72 ++++ tests/notification-basic-auth.test.ts | 139 ++++++++ tests/openapi-body-annotations.test.ts | 27 ++ tests/podman-network-key.test.ts | 111 ++++++ tests/provider-kind-core.test.ts | 56 +++ tests/reader-abort-core.test.ts | 59 ++++ 35 files changed, 1812 insertions(+), 133 deletions(-) create mode 100644 src/lib/server/podman-network-key.ts create mode 100644 src/lib/server/reader-abort-core.ts create mode 100644 src/lib/utils/env-file-values.ts create mode 100644 src/lib/utils/env-panel-core.ts create mode 100644 tests/env-file-values.test.ts create mode 100644 tests/env-panel-core.test.ts create mode 100644 tests/image-export-name.test.ts create mode 100644 tests/notification-basic-auth.test.ts create mode 100644 tests/podman-network-key.test.ts create mode 100644 tests/provider-kind-core.test.ts create mode 100644 tests/reader-abort-core.test.ts diff --git a/scripts/generate-openapi.ts b/scripts/generate-openapi.ts index 96fa06679..f938c2f8c 100644 --- a/scripts/generate-openapi.ts +++ b/scripts/generate-openapi.ts @@ -45,7 +45,8 @@ import { buildSpec } from './openapi/build-spec'; const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url)); const ROOT_DIR = join(SCRIPT_DIR, '..'); const ROUTES_ROOT = join(ROOT_DIR, 'src', 'routes'); -const HOOKS_FILE = join(ROOT_DIR, 'src', 'hooks.server.ts'); +// The public-route lists live in public-paths-core.ts. +const HOOKS_FILE = join(ROOT_DIR, 'src', 'lib', 'server', 'public-paths-core.ts'); const PACKAGE_JSON = join(ROOT_DIR, 'package.json'); // Bundled as an importable module (src/lib) rather than read from disk at // runtime — see src/routes/api/docs/+server.ts. The production Docker image diff --git a/scripts/openapi/lib.ts b/scripts/openapi/lib.ts index 513cebfd6..dcb32871f 100644 --- a/scripts/openapi/lib.ts +++ b/scripts/openapi/lib.ts @@ -547,6 +547,17 @@ export function analyzeHandlerBody(body: string, pathParamNames: string[] = []): const redirectRe = /\bredirect\(\s*(\d{3})/g; let rm; while ((rm = redirectRe.exec(body)) !== null) statusCodes.add(rm[1]); + // The authorize() guards return a ready-made 403 instead of building one inline, so + // a handler that uses them emits a status no literal in its body mentions. Without + // this the gate calls the documented 403 stale and argues against the shared guard. + // Comment lines are skipped: a handler that merely mentions the guard in its JSDoc + // would otherwise be credited with a 403 it never returns, and the drift gate would + // stop noticing when one goes missing. + const callsGuard = body + .split('\n') + .filter((line) => !/^\s*(\*|\/\/)/.test(line)) + .some((line) => /\b(auth\.)?require(Permission|EnvAccess)\s*\(/.test(line)); + if (callsGuard) statusCodes.add('403'); const bodyFields = new Set(); // `const { a, b } = await request.json();` and `const { a, b } = body;` @@ -586,9 +597,8 @@ export function analyzeHandlerBody(body: string, pathParamNames: string[] = []): // PUBLIC_PATHS extraction (from hooks.server.ts) — auth-exemption discovery // --------------------------------------------------------------------------- -export function extractPublicPaths(hooksFile: string): string[] { - const content = readFileSync(hooksFile, 'utf-8'); - const m = content.match(/const PUBLIC_PATHS\s*=\s*\[([\s\S]*?)\];/); +function literalsIn(content: string, listName: string): string[] { + const m = content.match(new RegExp(`const ${listName}\\s*=\\s*\\[([\\s\\S]*?)\\];`)); if (!m) return []; const paths: string[] = []; const strRe = /'([^']+)'/g; @@ -597,14 +607,34 @@ export function extractPublicPaths(hooksFile: string): string[] { return paths; } +/** + * The paths hooks.server.ts lets through unauthenticated. + * + * Kept as two lists because they match differently: an exact entry covers only itself, + * a prefix entry covers its subtree. Flattening them would mark a protected route + * beneath an exact entry as public in the spec. + */ +export function extractPublicPaths(hooksFile: string): { exact: string[]; prefixes: string[] } { + const content = readFileSync(hooksFile, 'utf-8'); + return { + exact: literalsIn(content, 'PUBLIC_EXACT'), + prefixes: literalsIn(content, 'PUBLIC_PREFIXES') + }; +} + // Two exceptions hardcoded directly in isPublicPath() as regexes (webhook // signature/secret auth instead of session/token) — not expressible as a // simple PUBLIC_PATHS prefix string. The one manual special-case this // generator needs; documented in the research doc's coverage-gap section. export const PUBLIC_PATH_REGEXES = [/^\/api\/git\/stacks\/\d+\/webhook$/, /^\/api\/git\/webhook\/\d+$/]; -export function isPublic(openapiPath: string, publicPaths: string[]): boolean { - if (publicPaths.some((p) => openapiPath === p || openapiPath.startsWith(p + '/'))) return true; +export function isPublic( + openapiPath: string, + publicPaths: { exact: string[]; prefixes: string[] } +): boolean { + if (publicPaths.exact.includes(openapiPath)) return true; + if (publicPaths.prefixes.some((p) => openapiPath === p || openapiPath.startsWith(p + '/'))) + return true; const asConcretePath = openapiPath.replace(/\{[^}]+\}/g, '1'); return PUBLIC_PATH_REGEXES.some((re) => re.test(asConcretePath)); } diff --git a/server.js b/server.js index 97678f406..e4ed32390 100644 --- a/server.js +++ b/server.js @@ -66,25 +66,44 @@ if (useHttps) { process.exit(1); } - // Parse cert metadata so operators can confirm they mounted the right file. + // Keep in sync with src/lib/server/x509-display-core.ts (this file runs against + // ./build and cannot import from src). Node leaves subject/issuer undefined when + // the cert has an empty distinguished name, as newer Let's Encrypt profiles do. + const formatCertName = (name) => { + if (typeof name !== 'string') return '(none)'; + const trimmed = name.trim(); + return trimmed === '' ? '(none)' : trimmed.replace(/\n/g, ', '); + }; + const daysUntilExpiry = (validTo, now) => { + if (typeof validTo !== 'string') return null; + const expiresAt = new Date(validTo).getTime(); + if (Number.isNaN(expiresAt)) return null; + return Math.floor((expiresAt - now) / 86400000); + }; + const expiryLine = (daysLeft) => { + if (daysLeft === null) return { text: 'cert expiry: (unknown)', warn: false }; + if (daysLeft < 0) { + return { text: `WARNING: certificate expired ${-daysLeft} day(s) ago`, warn: true }; + } + if (daysLeft < 30) { + return { text: `WARNING: certificate expires in ${daysLeft} day(s)`, warn: true }; + } + return { text: `cert expires in ${daysLeft} day(s)`, warn: false }; + }; + + // Cert metadata is logged so operators can confirm they mounted the right file. + // It is diagnostics only, so a cert TLS itself accepts must still start the + // server - hence the log failure is reported and swallowed, not fatal. try { const x509 = new X509Certificate(certPem); - console.log(`[HTTPS] cert subject: ${x509.subject.replace(/\n/g, ', ')}`); - console.log(`[HTTPS] cert issuer: ${x509.issuer.replace(/\n/g, ', ')}`); + console.log(`[HTTPS] cert subject: ${formatCertName(x509.subject)}`); + console.log(`[HTTPS] cert issuer: ${formatCertName(x509.issuer)}`); console.log(`[HTTPS] cert SAN: ${x509.subjectAltName || '(none)'}`); - console.log(`[HTTPS] cert valid: ${x509.validFrom} → ${x509.validTo}`); - const expiresAt = new Date(x509.validTo).getTime(); - const daysLeft = Math.floor((expiresAt - Date.now()) / 86400000); - if (daysLeft < 0) { - console.warn(`[HTTPS] WARNING: certificate expired ${-daysLeft} day(s) ago`); - } else if (daysLeft < 30) { - console.warn(`[HTTPS] WARNING: certificate expires in ${daysLeft} day(s)`); - } else { - console.log(`[HTTPS] cert expires in ${daysLeft} day(s)`); - } + console.log(`[HTTPS] cert valid: ${x509.validFrom} -> ${x509.validTo}`); + const expiry = expiryLine(daysUntilExpiry(x509.validTo, Date.now())); + (expiry.warn ? console.warn : console.log)(`[HTTPS] ${expiry.text}`); } catch (e) { - console.error(`[HTTPS] Failed to parse certificate: ${e.message}`); - process.exit(1); + console.warn(`[HTTPS] Could not read certificate metadata: ${e.message}`); } const tlsOptions = { cert: certPem, key: keyPem }; @@ -182,6 +201,11 @@ globalThis.__terminalHandleExecMessage = (msg) => { // Handle WebSocket upgrade server.on('upgrade', async (req, socket, head) => { + // The socket is bare until ws adopts it in handleUpgrade below, and authentication + // awaits a database lookup first. A peer that resets during that window makes the + // write of our own 401/500 throw ECONNRESET with no listener, which is fatal. + socket.on('error', () => {}); + const url = new URL(req.url || '/', `http://${req.headers.host}`); // Only handle our specific WebSocket paths @@ -379,6 +403,14 @@ function processDockerStreamChunk(data, state) { } async function handleTerminalConnection(ws, url, connId) { + // Registered before the first await: this runs unawaited, so every await below is a + // window in which a closed tab emits 'error' on a socket with no listener, which is + // fatal rather than a dropped connection. + ws.on('error', (err) => { + console.error('[Terminal WS] Connection error:', err.message); + wsConnections.delete(connId); + }); + const pathParts = url.pathname.split('/'); const containerIdIndex = pathParts.indexOf('containers') + 1; const containerId = pathParts[containerIdIndex]; @@ -597,13 +629,6 @@ async function handleTerminalConnection(ws, url, connId) { ws.on('close', () => { wsConnections.delete(connId); }); - - // Without an 'error' listener, an emitted socket error (abrupt disconnect, - // ECONNRESET) is re-thrown as an uncaught exception and crashes the process. - ws.on('error', (err) => { - console.error('[Terminal WS] Connection error:', err.message); - wsConnections.delete(connId); - }); } /** diff --git a/src/lib/components/StackEnvVarsPanel.svelte b/src/lib/components/StackEnvVarsPanel.svelte index b051e0871..60f1f6c76 100644 --- a/src/lib/components/StackEnvVarsPanel.svelte +++ b/src/lib/components/StackEnvVarsPanel.svelte @@ -9,6 +9,7 @@ import { getProviderIcon } from '$lib/components/provider-icons'; import { providerTypeLabel } from '../../routes/settings/secrets/ProviderModal.svelte'; import { effectiveMissing } from '$lib/utils/invault-markers'; + import { parseRawContent, generateRawContent, keysInRawContent, mergeParsedIntoVariables, textEditorContent as deriveTextEditorContent } from '$lib/utils/env-panel-core'; interface Props { variables: EnvVar[]; // Bindable - ALL variables (secrets + non-secrets) @@ -106,16 +107,9 @@ }) ); - // Generate text representation from variables (non-secrets only) - // This is used for text view display - const generatedRawContent = $derived.by(() => { - const nonSecrets = variables.filter(v => v.key.trim() && !v.isSecret); - if (nonSecrets.length === 0) return ''; - return nonSecrets.map(v => `${v.key.trim()}=${v.value}`).join('\n') + '\n'; - }); - - // Text editor content - either from file (rawContent prop) or generated from variables - const textEditorContent = $derived(rawContent.trim() ? rawContent : generatedRawContent); + // What text view shows: the .env file, or the non-secret rows rendered as text when + // there is no file yet. + const textEditorContent = $derived(deriveTextEditorContent(rawContent, variables)); /** * Sync variables with rawContent after initial load. @@ -124,7 +118,7 @@ */ export function syncAfterLoad(loadedVars: EnvVar[], loadedRaw: string) { if (!loadedRaw.trim()) { - // No raw content from file - just set variables, text view will use generatedRawContent + // No raw content from file - just set variables; text view renders them as text variables = loadedVars; rawContent = ''; return; @@ -144,39 +138,6 @@ rawContent = loadedRaw; } - /** - * Parse raw content to extract non-secret variables. - */ - function parseRawContent(content: string): { vars: EnvVar[], warnings: string[] } { - const result: EnvVar[] = []; - const warnings: string[] = []; - let lineNum = 0; - - for (const line of content.split('\n')) { - lineNum++; - const trimmed = line.trim(); - if (!trimmed || trimmed.startsWith('#')) continue; - - const eqIndex = trimmed.indexOf('='); - if (eqIndex === -1) { - warnings.push(`Line ${lineNum}: "${trimmed.slice(0, 30)}${trimmed.length > 30 ? '...' : ''}" (no = found)`); - continue; - } - - const key = trimmed.slice(0, eqIndex).trim(); - const value = trimmed.slice(eqIndex + 1); - - if (key) { - if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) { - warnings.push(`Line ${lineNum}: "${key}" (invalid variable name)`); - continue; - } - result.push({ key, value, isSecret: false }); - } - } - - return { vars: result, warnings }; - } /** * Sync variables (non-secrets) TO rawContent. @@ -188,7 +149,7 @@ // If no raw content exists, generate fresh if (!rawContent.trim()) { if (nonSecretVars.length > 0) { - rawContent = nonSecretVars.map(v => `${v.key.trim()}=${v.value}`).join('\n') + '\n'; + rawContent = generateRawContent(nonSecretVars); } return; } @@ -242,18 +203,17 @@ } /** - * Sync rawContent TO variables. - * Parses raw content for non-secrets, preserves existing secrets. + * Parse editor text back into variables. + * + * `shownBefore` is what the editor was displaying before this text arrived. Rows + * outside it were never on screen - a selector written straight into variables by + * the provider picker, say - so they are kept rather than wiped (#1620). Rows that + * WERE on screen and are absent from the new text were deleted by the user. */ - function syncRawToVariables(content?: string) { + function syncRawToVariables(content?: string, shownBefore?: Set) { const { vars, warnings } = parseRawContent(content ?? rawContent); parseWarnings = warnings; - - // Preserve existing secrets (they're not in rawContent) - const existingSecrets = variables.filter(v => v.isSecret); - - // Merge: non-secrets from raw + existing secrets - variables = [...vars, ...existingSecrets]; + variables = mergeParsedIntoVariables(vars, variables, shownBefore ?? keysInRawContent(textEditorContent)); } /** @@ -261,9 +221,11 @@ * Always syncs variables→raw to get proper .env content for disk. */ export function prepareForSave(): { rawContent: string; variables: EnvVar[] } { - // If in text view, first sync raw→variables to capture edits + // If in text view, first sync raw->variables to capture edits. Parse what the + // editor SHOWS: with an empty .env that is the rows rendered as text, and reading + // rawContent instead would drop every visible non-secret row (#1620). if (viewMode === 'text') { - syncRawToVariables(); + syncRawToVariables(textEditorContent); } // Then sync variables→raw to ensure rawContent is up to date syncVariablesToRaw(); @@ -275,8 +237,11 @@ } function handleTextChange(value: string) { + // Capture what was on screen BEFORE the edit, so a row the user just deleted is + // not mistaken for one the editor never showed. + const shownBefore = keysInRawContent(textEditorContent); rawContent = value; - syncRawToVariables(); // Sync to variables so parent's envVars updates (for compose decorations) + syncRawToVariables(value, shownBefore); // keeps parent's envVars live for compose decorations onchange?.(); } @@ -285,8 +250,8 @@ // Form → Text: sync variables to raw (preserves comments) syncVariablesToRaw(); } else if (newMode === 'form' && viewMode === 'text') { - // Text → Form: use textEditorContent which falls back to generatedRawContent - // when rawContent is empty (fixes vars lost on view switch for git stacks) + // Text -> Form: parse what the editor shows, which is the rows rendered as text + // when there is no .env file (keeps vars that only exist as rows) syncRawToVariables(textEditorContent); } diff --git a/src/lib/server/hawser.ts b/src/lib/server/hawser.ts index ec1cf4c3c..3b2214d44 100644 --- a/src/lib/server/hawser.ts +++ b/src/lib/server/hawser.ts @@ -128,7 +128,13 @@ export function initializeEdgeManager(): void { } for (const [requestId, pending] of conn.pendingStreamRequests) { console.log(`[Hawser] Ending stream request ${requestId} due to connection timeout`); - pending.onEnd?.('Connection timeout'); + // onEnd is caller-supplied and runs on a bare interval callback, so a + // throw here would reach the event loop instead of ending one stream. + try { + pending.onEnd?.('Connection timeout'); + } catch (err) { + console.error(`[Hawser] Error ending stream request ${requestId}:`, err); + } } conn.pendingRequests.clear(); conn.pendingStreamRequests.clear(); @@ -138,7 +144,11 @@ export function initializeEdgeManager(): void { conn.pingInterval = undefined; } - conn.ws.close(1001, 'Connection timeout'); + try { + conn.ws.close(1001, 'Connection timeout'); + } catch (err) { + console.error(`[Hawser] Error closing timed-out connection for environment ${envId}:`, err); + } edgeConnections.delete(envId); updateEnvironmentStatus(envId, null); } diff --git a/src/lib/server/podman-network-key.ts b/src/lib/server/podman-network-key.ts new file mode 100644 index 000000000..5a1ada3ac --- /dev/null +++ b/src/lib/server/podman-network-key.ts @@ -0,0 +1,50 @@ +/** + * Podman is inconsistent about the default bridge: `/networks` lists it as + * `bridge` and `HostConfig.NetworkMode` accepts that name, but the + * `EndpointsConfig` key does not - a create keyed by `bridge` fails with + * "unable to find network with name or ID bridge: network not found" (#1619). + * Container inspect calls the same network `podman`, so the two halves of the + * API disagree and nothing in the network payload reveals which name to use. + * + * The network id is accepted as a key by both engines, so the create is retried + * with it - but only after the name has been refused, because Docker echoes the + * id back as the inspect key and keying by id up front would leave ids where the + * UI expects network names. + */ + +/** + * Whether a create failure is worth retrying by network id. + * + * This is a cheap PRE-FILTER, not the decision: Podman reports a genuinely + * missing network with the same wording, so a user's typo matches too. What + * actually separates the two is looking the network up - it resolves for the + * aliased bridge and 404s for a name that does not exist. Keep both; dropping + * the lookup would retry typos, and dropping its null-tolerance would replace a + * clear "network not found" with an opaque inspect error. + */ +export function isUnknownNetworkKeyError(message: string | undefined | null): boolean { + if (typeof message !== 'string') return false; + return /unable to find network with name or ID/i.test(message); +} + +/** + * The key to retry the create with, or null to rethrow the original error. + * + * `networkId` is what the daemon returned for the refused key, or null when the + * lookup failed - which is the case that must NOT retry, since it means the + * network genuinely does not exist. + */ +export function retryEndpointKey(args: { + message: string | undefined | null; + endpoints: Record | undefined; + key: string; + networkId: string | null | undefined; +}): string | null { + const { message, endpoints, key, networkId } = args; + if (!key || !endpoints?.[key]) return null; + if (!isUnknownNetworkKeyError(message)) return null; + // No id means the lookup 404'd: the name was refused because it is not a + // network, not because Podman knows it under another name. + if (!networkId || networkId === key) return null; + return networkId; +} diff --git a/src/lib/server/reader-abort-core.ts b/src/lib/server/reader-abort-core.ts new file mode 100644 index 000000000..f6d37e421 --- /dev/null +++ b/src/lib/server/reader-abort-core.ts @@ -0,0 +1,13 @@ +/** + * Cancel a stream reader when the signal aborts. + * + * The listener must not RETURN the cancel() promise. A stream the far end dropped is + * already errored, so cancel() rejects, and Node's EventTarget rethrows a rejected + * listener result as an uncaught exception - which takes the whole process down. + */ +export function cancelReaderOnAbort( + reader: { cancel(): Promise }, + signal: AbortSignal | undefined +): void { + signal?.addEventListener('abort', () => { reader.cancel().catch(() => {}); }, { once: true }); +} diff --git a/src/lib/server/subprocess-manager.ts b/src/lib/server/subprocess-manager.ts index 3ba34a233..70a4cb13a 100644 --- a/src/lib/server/subprocess-manager.ts +++ b/src/lib/server/subprocess-manager.ts @@ -94,6 +94,8 @@ let readyPromise: Promise | null = null; const recentEvents: Map = new Map(); // Disk warning cooldown per env const lastDiskWarning: Map = new Map(); +/** Environments already told they cannot use percentage warnings. */ +const warnedNoPoolSize = new Set(); // Environment name cache (for notifications) const envNames: Map = new Map(); // Track which envIds are currently configured in Go @@ -396,7 +398,22 @@ async function handleDiskUsage(msg: GoMessage): Promise { } } } - if (dataSpaceTotal <= 0) return; + if (dataSpaceTotal <= 0) { + // Say it once per environment: silence here looks exactly like "measured, + // all fine", which is how a full disk goes unnoticed. Only latch it once + // the host has actually answered - a cycle that carried no /info says + // nothing about the host, and latching on it would spend the one warning + // on an outage and stay quiet after the host comes back. + if (msg.info && !warnedNoPoolSize.has(msg.envId)) { + warnedNoPoolSize.add(msg.envId); + console.warn( + `[metrics] env ${msg.envId}: percentage disk warnings cannot fire - this host ` + + `reports no storage pool size (driver ${msg.info?.Driver ?? 'unknown'}). ` + + `Switch the environment to absolute (GB) warnings.` + ); + } + return; + } const diskPercentUsed = (totalUsed / dataSpaceTotal) * 100; const threshold = (await getEnvSetting('disk_warning_threshold', msg.envId)) || 80; @@ -550,7 +567,15 @@ function readStdout(): void { if (lineBuffer[i] === 0x0a) { // newline if (i > start) { const line = lineBuffer.toString('utf8', start, i); - handleLine(line); + // This runs on a bare stdout callback, and the sync branches dispatch + // to every SSE subscriber. A throw anywhere down there would reach the + // event loop and take the process with it, so one worker message can + // never be worth more than itself. + try { + handleLine(line); + } catch (err) { + console.error('[SubprocessManager] Error handling worker message:', err); + } } start = i + 1; } diff --git a/src/lib/utils/backup.ts b/src/lib/utils/backup.ts index 30fafd88c..3a7911f5a 100644 --- a/src/lib/utils/backup.ts +++ b/src/lib/utils/backup.ts @@ -177,6 +177,30 @@ export interface BackupFormState { */ export type BackupAction = 'save' | 'save-run' | 'run-once'; +/** + * Whether a duplicated config should start enabled. + * + * A manual config is stored disabled because it has no schedule; its copy exists to + * be given one, so it starts enabled. A copy of a scheduled config keeps whatever + * the original was, so duplicating a paused schedule does not quietly resume it. + */ +export function duplicateStartsEnabled(source: { schedule?: string | null; enabled?: boolean }): boolean { + if (!source.schedule) return true; + return source.enabled !== false; +} + +/** + * Destination for a duplicated schedule: the first repository this target has no + * schedule for yet, so the copy lands elsewhere by default; else the source's own. + */ +export function pickDuplicateDestinationId( + sourceDestinationId: number, + destinationIds: number[], + usedDestinationIds: number[] +): number { + return destinationIds.find((id) => !usedDestinationIds.includes(id)) ?? sourceDestinationId; +} + export interface BuildBodyOptions { enabled: boolean; /** Set to null to explicitly clear the schedule (run-once needs this). */ diff --git a/src/lib/utils/env-file-values.ts b/src/lib/utils/env-file-values.ts new file mode 100644 index 000000000..220531d91 --- /dev/null +++ b/src/lib/utils/env-file-values.ts @@ -0,0 +1,205 @@ +/** + * Reading a `.env` value the way `docker compose` reads it. + * + * Two things a naive `split('=')` gets wrong, both reported against git stacks where + * Dockhand parses the file itself instead of handing it to compose: + * + * DIRECTORY=/srv/data + * LOCATION="${DIRECTORY}/immich" + * + * The quotes belong to the file format, not to the value, and `${DIRECTORY}` refers to + * an earlier line. Kept verbatim, the label reads `"${DIRECTORY}/immich"` and a bind + * mount using it resolves to nothing - which compose then reports as a volume that does + * not exist, giving no hint that a variable was the problem. + * + * Pure, so the rules can be tested against what compose actually does. + */ + +/** A value's surrounding quotes, which are syntax rather than content. */ +export function unquote(raw: string): string { + const v = raw.trim(); + if (v.length < 2) return v; + const first = v[0]; + if ((first === '"' || first === "'") && v[v.length - 1] === first) { + return v.slice(1, -1); + } + return v; +} + +/** + * `${NAME}` and `$NAME` replaced from what is already known, with `$$` left as one + * literal dollar. + * + * `$$` is compose's escape, and it is what people write for a password, a Traefik + * pattern or a bcrypt hash containing a dollar - so eating it truncates the value + * silently. A name with no value becomes empty, as in a shell, which is what + * `${NAME:?...}` exists to complain about. Single quotes are literal in a `.env` file, + * so a single-quoted value is returned untouched. + * + * One pass, not three: a value that expands to text containing `$NAME` must not have + * that rescanned as another reference. + */ +export function expandValue(raw: string, known: Readonly>): string { + const trimmed = raw.trim(); + + // Single quotes are literal throughout: no escapes, no references. + if (trimmed.startsWith("'")) return unquote(trimmed); + + // Double quotes take shell escape sequences first, then substitution. + if (trimmed.startsWith('"')) return substitute(expandEscapes(unquote(trimmed)), known); + + // Unquoted: a hash PRECEDED BY A SPACE starts a comment, and the trailing space goes + // with it. A hash inside the value - a colour, a URL fragment - is part of it. + const comment = trimmed.indexOf(' #'); + const value = comment === -1 ? trimmed : trimmed.slice(0, comment).trimEnd(); + return substitute(value, known); +} + +/** + * The escape sequences a double-quoted value carries. + * + * `\$` becomes the `$$` form so the substitution pass reads it as one literal dollar, + * `\0NNN` is octal, and the rest are the usual shell escapes. Anything outside the set + * is left as written rather than guessed at. + */ +function expandEscapes(value: string): string { + const SIMPLE: Record = { + a: '\x07', + b: '\b', + c: '', + f: '\f', + n: '\n', + r: '\r', + t: '\t', + v: '\v', + '"': '"', + '\\': '\\' + }; + + return value.replace(/\\(?:[abcfnrtv$"\\]|0\d{0,3})/g, (match) => { + if (match === '\\$') return '$$'; + if (match.startsWith('\\0')) { + const code = parseInt(match.slice(2) || '0', 8); + return Number.isNaN(code) ? match : String.fromCharCode(code); + } + return SIMPLE[match[1]] ?? match; + }); +} + +/** + * One left-to-right pass over a value, replacing every reference it finds. + * + * Written as a scan rather than a regex because a default may itself hold a reference + * (`${A:-${B}/x}`) and brace counting is what tells its end from the value's own text. + * A pass never re-reads what it produced, so a resolved value containing `$NAME` stays + * as it is. + */ +function substitute(value: string, known: Readonly>): string { + let out = ''; + let i = 0; + while (i < value.length) { + const dollar = value.indexOf('$', i); + if (dollar === -1) return out + value.slice(i); + out += value.slice(i, dollar); + + const next = value[dollar + 1]; + if (next === '$') { + // Compose's escape for a literal dollar. + out += '$'; + i = dollar + 2; + } else if (next === '{') { + const end = closingBrace(value, dollar + 1); + if (end === -1) { + // Unbalanced: nothing to resolve, so the text stands. + return out + value.slice(dollar); + } + out += resolveReference(value.slice(dollar + 2, end), known); + i = end + 1; + } else if (next && /[A-Za-z_]/.test(next)) { + let end = dollar + 1; + while (end < value.length && /[A-Za-z0-9_]/.test(value[end])) end++; + out += known[value.slice(dollar + 1, end)] ?? ''; + i = end; + } else { + // A dollar that starts no name - a price, a trailing one - is just text. + out += '$'; + i = dollar + 1; + } + } + return out; +} + +/** The `}` matching the `{` at `open`, or -1 when the braces do not balance. */ +function closingBrace(value: string, open: number): number { + let depth = 0; + for (let i = open; i < value.length; i++) { + if (value[i] === '{') depth++; + else if (value[i] === '}' && --depth === 0) return i; + } + return -1; +} + +/** + * The inside of a `${...}`, including the forms that carry a default or an alternative. + * + * Measured against compose: `:-` and `-` fall back when the name is unset or empty, + * `:+` yields its alternative only when the name HAS a value, and a default may itself + * contain a reference. `:?` explains a missing value to a person; there is nobody to + * tell here, so it resolves like the plain form and lets the deploy report it. + */ +function resolveReference(body: string, known: Readonly>): string { + const op = body.search(/[:\-+?]/); + if (op === -1) return known[body] ?? ''; + + const name = body.slice(0, op); + const rest = body.slice(op); + const value = known[name]; + + // The colon is what makes an EMPTY value count as absent. Without it, only an + // unset name does - so ${EMPTY-d} keeps the empty value while ${EMPTY:-d} takes + // the default. + if (rest.startsWith(':+')) return value ? substitute(rest.slice(2), known) : ''; + if (rest.startsWith('+')) { + return value !== undefined ? substitute(rest.slice(1), known) : ''; + } + if (rest.startsWith(':-')) return value || substitute(rest.slice(2), known); + if (rest.startsWith('-')) return value ?? substitute(rest.slice(1), known); + // `:?` / `?` ask to stop rather than deploy with a hole, which compose does when it + // reads the file itself. Nothing here can refuse a deploy, so the value resolves + // like the plain form and compose raises it on its own pass. + return value ?? ''; +} + +/** + * Resolve a file's values against each other, in the order they were written. + * + * Only earlier lines are visible to a later one, which is how a shell and compose read + * a file; a forward reference is empty rather than an error. + */ +export function resolveEnvValues( + entries: readonly (readonly [string, string])[] +): Record { + const out: Record = {}; + for (const [key, raw] of entries) { + out[key] = expandValue(raw, out); + } + return out; +} + +/** + * A resolved value written back into a `.env` file compose will read again. + * + * Whatever writes that file hands compose a second interpolation pass, and the bare + * `KEY=value` form loses more than dollars to it: a hash after a space becomes a + * comment, surrounding spaces are trimmed, and a value starting with a quote breaks the + * file outright. Quoting the whole value makes it one token, and escaping backslash, + * quote and dollar - in that order - carries it through the pass unchanged. + */ +export function quoteForEnvFile(value: string): string { + const escaped = value + .replace(/\\/g, '\\\\') + .replace(/"/g, '\\"') + .replace(/\$/g, '$$$$'); + return `"${escaped}"`; +} + diff --git a/src/lib/utils/env-panel-core.ts b/src/lib/utils/env-panel-core.ts new file mode 100644 index 000000000..69e5ce4de --- /dev/null +++ b/src/lib/utils/env-panel-core.ts @@ -0,0 +1,100 @@ +/** + * Pure helpers for the stack environment-variables panel. + * + * The panel holds the same data twice: `variables` (rows, including secrets) and + * `rawContent` (the .env text). Either side can be edited, so every crossing between + * them goes through here. + */ + +export interface EnvVarLike { + key: string; + value: string; + isSecret?: boolean; +} + +export interface ParsedRawContent { + vars: T[]; + warnings: string[]; +} + +/** Parse .env text into non-secret rows, collecting a warning per unusable line. */ +export function parseRawContent(content: string): ParsedRawContent { + const vars: (EnvVarLike & { isSecret: false })[] = []; + const warnings: string[] = []; + let lineNum = 0; + + for (const line of content.split('\n')) { + lineNum++; + const trimmed = line.trim(); + if (!trimmed || trimmed.startsWith('#')) continue; + + const eqIndex = trimmed.indexOf('='); + if (eqIndex === -1) { + warnings.push( + `Line ${lineNum}: "${trimmed.slice(0, 30)}${trimmed.length > 30 ? '...' : ''}" (no = found)` + ); + continue; + } + + const key = trimmed.slice(0, eqIndex).trim(); + const value = trimmed.slice(eqIndex + 1); + if (!key) continue; + + if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) { + warnings.push(`Line ${lineNum}: "${key}" (invalid variable name)`); + continue; + } + vars.push({ key, value, isSecret: false }); + } + + return { vars, warnings }; +} + +/** Render non-secret rows as .env text. Empty when there is nothing to write. */ +export function generateRawContent(variables: EnvVarLike[]): string { + const nonSecrets = variables.filter((v) => v.key.trim() && !v.isSecret); + if (nonSecrets.length === 0) return ''; + return nonSecrets.map((v) => `${v.key.trim()}=${v.value}`).join('\n') + '\n'; +} + +/** + * The text the editor shows, and so the text to parse back when saving or leaving text + * view: the file when it has content, otherwise the rows rendered as text. + * + * Reading `rawContent` instead is what loses data - with an empty .env the two differ, + * and parsing the empty one drops every non-secret row the user can see, including a + * bulk selector written into `variables` by the secret provider picker (#1620). + */ +export function textEditorContent(rawContent: string, variables: EnvVarLike[]): string { + return rawContent.trim() ? rawContent : generateRawContent(variables); +} + +/** + * Fold the text view's parsed rows back into the panel's variables. + * + * The editor only ever shows non-secret rows that reached the .env text, so parsing it + * alone would discard two things it never had a chance to display: secrets, and rows + * written straight into `variables` from outside the panel - the secret provider's bulk + * selector being the one users hit (#1620). + * + * `editorKeys` is what the editor was showing before this edit. A row missing from both + * that set and the parsed result was never on screen, so it is carried over; a row that + * WAS on screen and is now gone was deleted by the user, so it stays deleted. + */ +export function mergeParsedIntoVariables( + parsed: T[], + variables: T[], + editorKeys: ReadonlySet +): T[] { + const parsedKeys = new Set(parsed.map((v) => v.key.trim())); + const offScreen = variables.filter( + (v) => !v.isSecret && v.key.trim() && !parsedKeys.has(v.key.trim()) && !editorKeys.has(v.key.trim()) + ); + const secrets = variables.filter((v) => v.isSecret); + return [...parsed, ...offScreen, ...secrets]; +} + +/** The non-secret keys a given editor text is displaying. */ +export function keysInRawContent(content: string): Set { + return new Set(parseRawContent(content).vars.map((v) => v.key)); +} diff --git a/src/routes/api/auto-update/[containerName]/+server.ts b/src/routes/api/auto-update/[containerName]/+server.ts index 634295488..6b54282e2 100644 --- a/src/routes/api/auto-update/[containerName]/+server.ts +++ b/src/routes/api/auto-update/[containerName]/+server.ts @@ -16,6 +16,7 @@ import { authorize } from '$lib/server/authorize'; * query: env:integer Environment the container belongs to (from GET /api/environments) * resp-200: {enabled:boolean!, scheduleType:string!, cronExpression:string, vulnerabilityCriteria:string!} * resp-200-example: {"enabled":false,"scheduleType":"daily","cronExpression":"0 3 * * *","vulnerabilityCriteria":"never"} + * resp-403: Permission denied, or no access to the environment * resp-500: Failed to get auto-update setting */ export const GET: RequestHandler = async ({ params, url, cookies }) => { @@ -65,6 +66,7 @@ export const GET: RequestHandler = async ({ params, url, cookies }) => { * body-example: {"enabled":true,"cronExpression":"0 3 * * *","vulnerabilityCriteria":"never"} * resp-200: {id:integer!, enabled:boolean!, scheduleType:string!, cronExpression:string, vulnerabilityCriteria:string!, deleted:boolean, success:boolean} * resp-200-example: {"success":true,"deleted":true} + * resp-403: Permission denied, or no access to the environment * resp-500: Failed to save auto-update setting */ export const POST: RequestHandler = async ({ params, url, request, cookies }) => { @@ -145,6 +147,7 @@ export const POST: RequestHandler = async ({ params, url, request, cookies }) => * query: env:integer Environment the container belongs to (from GET /api/environments) * resp-200: {success:boolean!} * resp-200-example: {"success":true} + * resp-403: Permission denied, or no access to the environment * resp-500: Failed to delete auto-update setting */ export const DELETE: RequestHandler = async ({ params, url, cookies }) => { diff --git a/src/routes/api/dependencies/+server.ts b/src/routes/api/dependencies/+server.ts index e71e045d9..80f6caabd 100644 --- a/src/routes/api/dependencies/+server.ts +++ b/src/routes/api/dependencies/+server.ts @@ -1,6 +1,7 @@ import { json, type RequestHandler } from '@sveltejs/kit'; import dependencies from '$lib/data/dependencies.json'; import { DEFAULT_GRYPE_IMAGE, DEFAULT_TRIVY_IMAGE } from '$lib/server/scanner'; +import { authorize } from '$lib/server/authorize'; // Extract version tag from image string (e.g., "anchore/grype:v0.110.0" -> "v0.110.0") function imageTag(image: string): string { @@ -26,12 +27,22 @@ const externalTools = [ /** * GET /api/dependencies - Dependency and external-tool inventory * + * Behind authentication: a version-exact inventory tells a reader which published + * advisories apply to this instance, and the About screen that shows it is already + * behind a login. + * * @openapi * summary: Return the combined list of npm dependencies and external tool images (grype, trivy), sorted by name, excluding Dockhand itself * resp-200: array<{name:string!, version:string!, license:string, repository:string}> * resp-200-example: [{"name":"anchore/grype","version":"v0.110.0","license":"Apache-2.0","repository":"https://github.com/anchore/grype"}] + * resp-401: Not authenticated */ -export const GET: RequestHandler = async () => { +export const GET: RequestHandler = async ({ cookies }) => { + const auth = await authorize(cookies); + if (auth.authEnabled && !auth.isAuthenticated) { + return json({ error: 'Authentication required' }, { status: 401 }); + } + // Combine npm dependencies with external tools, exclude dockhand itself const allDependencies = [...dependencies, ...externalTools] .filter((dep) => dep.name !== 'dockhand') diff --git a/src/routes/api/environments/[id]/disk-warning/+server.ts b/src/routes/api/environments/[id]/disk-warning/+server.ts index 952659fb3..46618a57c 100644 --- a/src/routes/api/environments/[id]/disk-warning/+server.ts +++ b/src/routes/api/environments/[id]/disk-warning/+server.ts @@ -2,13 +2,16 @@ import { json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { authorize } from '$lib/server/authorize'; import { getEnvironment, getEnvSetting, setEnvSetting } from '$lib/server/db'; +import { getDockerInfo } from '$lib/server/docker'; +import { supportsPercentageWarnings } from '$lib/utils/disk-percentage-support'; /** * @openapi * summary: Get the disk-space warning thresholds for an environment + * description: percentageSupported and storageDriver are null when the host could not be reached, which means unknown rather than unsupported - only false says the host reports no storage pool size and so percentage warnings would never fire there. * path: id:integer! Environment id (from GET /api/environments) - * resp-200: {enabled:boolean!, mode:string!, threshold:integer!, thresholdGb:integer!} - * resp-200-example: {"enabled":true,"mode":"percentage","threshold":80,"thresholdGb":50} + * resp-200: {enabled:boolean!, mode:string!, threshold:integer!, thresholdGb:integer!, percentageSupported:boolean, storageDriver:string} + * resp-200-example: {"enabled":true,"mode":"percentage","threshold":80,"thresholdGb":50,"percentageSupported":false,"storageDriver":"overlay2"} * resp-403: Permission denied (RBAC 'environments:view' missing) * resp-404: Environment not found * resp-500: Unexpected error while loading the settings @@ -33,7 +36,28 @@ export const GET: RequestHandler = async ({ params, cookies }) => { const threshold = (await getEnvSetting('disk_warning_threshold', id)) ?? 80; const thresholdGb = (await getEnvSetting('disk_warning_threshold_gb', id)) ?? 50; - return json({ enabled, mode, threshold, thresholdGb }); + // Percentage mode divides by a total only some storage drivers report, so the + // form can say up front that the mode would do nothing here. An unreachable + // host answers null - unknown, not unsupported. + let percentageSupported: boolean | null = null; + let storageDriver: string | null = null; + try { + // Bounded: a wedged daemon that accepted the connection answers nothing, and + // reading a settings page must not wait on it. Giving up lands on the same + // "could not ask" answer as any other failure. + const info = await Promise.race([ + getDockerInfo(id), + new Promise((resolve) => setTimeout(() => resolve(null), 5000)) + ]); + if (info) { + percentageSupported = supportsPercentageWarnings(info.DriverStatus); + storageDriver = typeof info.Driver === 'string' ? info.Driver : null; + } + } catch { + // Host down or unreachable: leave it unknown rather than claiming anything. + } + + return json({ enabled, mode, threshold, thresholdGb, percentageSupported, storageDriver }); } catch (error) { console.error('Failed to get disk warning settings:', error); return json({ error: 'Failed to get disk warning settings' }, { status: 500 }); diff --git a/src/routes/api/events/+server.ts b/src/routes/api/events/+server.ts index acab50d7b..71f4c196d 100644 --- a/src/routes/api/events/+server.ts +++ b/src/routes/api/events/+server.ts @@ -8,6 +8,7 @@ import { authorize } from '$lib/server/authorize'; * summary: Stream live Docker events (container/image/volume/network) for an environment via SSE, with periodic heartbeats * query: env:integer Environment id — without it, an "info" SSE message is sent and the stream ends (from GET /api/environments) * resp-200: text/event-stream SSE stream ("connected", "heartbeat" every 5s, "docker" events with {type,action,actor,time,timeNano}, or an "error"/"info" event for edge environments, missing/unknown environment, or a lost Docker connection) + * resp-403: Permission denied, or no access to the environment */ export const GET: RequestHandler = async ({ url, cookies }) => { const auth = await authorize(cookies); diff --git a/src/routes/api/hawser/connect/+server.ts b/src/routes/api/hawser/connect/+server.ts index 5484830a0..7d7f47b2b 100644 --- a/src/routes/api/hawser/connect/+server.ts +++ b/src/routes/api/hawser/connect/+server.ts @@ -11,6 +11,7 @@ import { json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { isEdgeConnected, getAllEdgeConnections } from '$lib/server/hawser'; +import { authorize } from '$lib/server/authorize'; /** * GET /api/hawser/connect @@ -20,22 +21,39 @@ import { isEdgeConnected, getAllEdgeConnections } from '$lib/server/hawser'; /** * @openapi * summary: Report the Hawser Edge WebSocket endpoint status and list currently connected agents + * description: Requires environments:view. Each entry names a remote host and the Docker and agent versions running on it, so the listing is limited to the environments the caller can reach. * resp-200: {status:string!, message:string!, protocol:string!, activeConnections:integer!, connections:array<{environmentId:integer!, agentId:string, agentName:string, agentVersion:string, dockerVersion:string, hostname:string, capabilities:array, connectedAt:string, lastHeartbeat:string}>!} * resp-200-example: {"status":"ready","message":"Hawser Edge WebSocket endpoint. Connect via WebSocket.","protocol":"wss:///api/hawser/connect","activeConnections":0,"connections":[]} + * resp-401: Not authenticated + * resp-403: Permission denied (missing environments:view) */ -export const GET: RequestHandler = async () => { +export const GET: RequestHandler = async ({ cookies }) => { + const auth = await authorize(cookies); + if (auth.authEnabled && !auth.isAuthenticated) { + return json({ error: 'Authentication required' }, { status: 401 }); + } + // An inventory of the remote machines this instance manages, down to the Docker + // build running on each, so it follows the environments it describes. + const denied = await auth.requirePermission('environments', 'view'); + if (denied) return denied; + + // null means every environment is the caller's to see. + const reachable = await auth.getAccessibleEnvironmentIds(); + const connections = getAllEdgeConnections(); - const connectionList = Array.from(connections.entries()).map(([envId, conn]) => ({ - environmentId: envId, - agentId: conn.agentId, - agentName: conn.agentName, - agentVersion: conn.agentVersion, - dockerVersion: conn.dockerVersion, - hostname: conn.hostname, - capabilities: conn.capabilities, - connectedAt: conn.connectedAt.toISOString(), - lastHeartbeat: new Date(conn.lastHeartbeat).toISOString() - })); + const connectionList = Array.from(connections.entries()) + .filter(([envId]) => reachable === null || reachable.includes(envId)) + .map(([envId, conn]) => ({ + environmentId: envId, + agentId: conn.agentId, + agentName: conn.agentName, + agentVersion: conn.agentVersion, + dockerVersion: conn.dockerVersion, + hostname: conn.hostname, + capabilities: conn.capabilities, + connectedAt: conn.connectedAt.toISOString(), + lastHeartbeat: new Date(conn.lastHeartbeat).toISOString() + })); return json({ status: 'ready', diff --git a/src/routes/api/images/[id]/export/+server.ts b/src/routes/api/images/[id]/export/+server.ts index 890a88091..5f0f65af3 100644 --- a/src/routes/api/images/[id]/export/+server.ts +++ b/src/routes/api/images/[id]/export/+server.ts @@ -4,6 +4,7 @@ import { authorize } from '$lib/server/authorize'; import { createGzip } from 'zlib'; import { Readable } from 'stream'; import { validateDockerIdParam } from '$lib/server/docker-validation'; +import { exportFileStem, shortId, exportRefFor } from '$lib/server/image-export-name'; import type { RequestHandler } from './$types'; /** @@ -11,8 +12,9 @@ import type { RequestHandler } from './$types'; * * @openapi * summary: Export a Docker image as a downloadable tar (or tar.gz) stream - * path: id:string! Image ID or name to export (from GET /api/images) + * path: id:string! Image ID to export (from GET /api/images); a name containing a slash cannot be used here, since it would split the route * query: env:integer ID of the environment the image belongs to (from GET /api/environments) + * query: tag:string Which of the image's tags to name the download after (defaults to its first tag) * query: compress:boolean Gzip the tar stream and serve it as .tar.gz (default false) * resp-200: The image tar (application/x-tar) or gzipped tar (application/gzip) as an attachment * resp-403: Permission denied @@ -35,24 +37,25 @@ export const GET: RequestHandler = async ({ params, url, cookies }) => { try { - // Get image info for filename - let imageName = params.id; + // The tag the caller clicked, so an image with several tags is not always + // named after the first one. + const requestedTag = url.searchParams.get('tag'); + let imageName: string; + let exportRef = params.id; try { const imageInfo = await inspectImage(params.id, envIdNum); - if (imageInfo.RepoTags?.[0]) { - // Use first tag, replace : and / with _ for filename safety - imageName = imageInfo.RepoTags[0].replace(/[:/]/g, '_'); - } else { - // Use short ID - imageName = params.id.replace('sha256:', '').slice(0, 12); - } + imageName = exportFileStem(params.id, imageInfo.RepoTags, requestedTag); + + // Exported by tag rather than by id, because a tar saved by id carries + // RepoTags: null and loads back as an unnamed image. The tag has to be one + // the image really has - it is going to the daemon. + exportRef = exportRefFor(params.id, imageInfo.RepoTags, requestedTag); } catch { - // Use ID as fallback - imageName = params.id.replace('sha256:', '').slice(0, 12); + imageName = shortId(params.id); } // Get the tar stream from Docker - const dockerResponse = await exportImage(params.id, envIdNum); + const dockerResponse = await exportImage(exportRef, envIdNum); if (!dockerResponse.body) { return json({ error: 'No response body from Docker' }, { status: 500 }); @@ -66,6 +69,11 @@ export const GET: RequestHandler = async ({ params, url, cookies }) => { // Create a gzip stream and pipe the tar through it const gzip = createGzip(); const nodeStream = Readable.fromWeb(dockerResponse.body as any); + // pipe() does not forward a source error, and the Response has already been + // returned by the time a multi-GB export loses its host, so the failure + // arrives on the event loop with nothing to catch it. Hand it to the + // destination, which surfaces it as a truncated download. + nodeStream.on('error', (err) => gzip.destroy(err)); const compressedStream = nodeStream.pipe(gzip); // Convert back to web stream diff --git a/src/routes/api/schedules/+server.ts b/src/routes/api/schedules/+server.ts index 82ea8e8ea..7475b9f4c 100644 --- a/src/routes/api/schedules/+server.ts +++ b/src/routes/api/schedules/+server.ts @@ -51,6 +51,7 @@ export interface ScheduleInfo { * @openapi * summary: List all schedules (container/env auto-updates, git syncs, image-prune, backups, repo maintenance, system jobs) * resp-200: {schedules:array<{id:integer!, type:string!, name:string!, entityName:string!, environmentId:integer, enabled:boolean!, cronExpression:string, nextRun:string, isSystem:boolean!}>!} + * resp-403: Permission denied (missing schedules:view) * resp-500: Unexpected error while assembling the schedule list */ export const GET: RequestHandler = async ({ cookies }) => { diff --git a/src/routes/api/schedules/[type]/[id]/+server.ts b/src/routes/api/schedules/[type]/[id]/+server.ts index ae1fec5ec..80bb85a4a 100644 --- a/src/routes/api/schedules/[type]/[id]/+server.ts +++ b/src/routes/api/schedules/[type]/[id]/+server.ts @@ -27,6 +27,7 @@ import { authorize } from '$lib/server/authorize'; * path: id:integer! Schedule id (semantics depend on type — container/git-stack/backup-config id, environment id, or a synthetic repo-policy id) (from GET /api/schedules) * resp-200: {success:boolean!} * resp-400: Invalid schedule id, invalid/unsupported type, or system_cleanup (cannot be removed) + * resp-403: Permission denied * resp-404: Schedule (or backup destination, for repo_* types) not found * resp-500: Unexpected error while deleting the schedule */ diff --git a/src/routes/api/schedules/[type]/[id]/run/+server.ts b/src/routes/api/schedules/[type]/[id]/run/+server.ts index 2abe8f246..9409dccf5 100644 --- a/src/routes/api/schedules/[type]/[id]/run/+server.ts +++ b/src/routes/api/schedules/[type]/[id]/run/+server.ts @@ -25,6 +25,7 @@ import { BACKUPS_ENABLED } from '$lib/server/features'; * path: id:integer! Schedule id (semantics depend on type) (from GET /api/schedules) * resp-200: {success:boolean!, message:string!} * resp-400: Invalid schedule id/type, or the triggered task itself reported failure + * resp-403: Permission denied * resp-404: Schedule, backup config, or backup destination not found (or backup feature disabled) * resp-500: Unexpected error while triggering the schedule */ diff --git a/src/routes/api/schedules/[type]/[id]/toggle/+server.ts b/src/routes/api/schedules/[type]/[id]/toggle/+server.ts index 83f773ca2..9d2f8cabc 100644 --- a/src/routes/api/schedules/[type]/[id]/toggle/+server.ts +++ b/src/routes/api/schedules/[type]/[id]/toggle/+server.ts @@ -17,6 +17,7 @@ import { auditBackup, auditBackupDestination } from '$lib/server/audit'; * path: id:integer! Schedule id (semantics depend on type) (from GET /api/schedules) * resp-200: {success:boolean!, enabled:boolean!} * resp-400: Invalid schedule id, unsupported type, or system_cleanup/deploy_log_reconcile (cannot be paused) + * resp-403: Permission denied * resp-404: Schedule, backup config, or backup destination not found * resp-500: Unexpected error while toggling the schedule */ diff --git a/src/routes/api/settings/general/+server.ts b/src/routes/api/settings/general/+server.ts index dae354268..2a2692ce3 100644 --- a/src/routes/api/settings/general/+server.ts +++ b/src/routes/api/settings/general/+server.ts @@ -36,6 +36,7 @@ import { setPrimaryStackLocation } from '$lib/server/db'; import { authorize } from '$lib/server/authorize'; +import { visibleGeneralSettings } from '$lib/server/general-settings-visibility'; import { refreshSystemJobs } from '$lib/server/scheduler'; import { sendToEventSubprocess, sendToMetricsSubprocess } from '$lib/server/subprocess-manager'; import { DEFAULT_GRYPE_IMAGE, DEFAULT_TRIVY_IMAGE } from '$lib/server/scanner'; @@ -234,13 +235,14 @@ function parseScannerDnsStorage(raw: string | null | undefined): string[] { /** * @openapi * summary: Get global (instance-wide) general settings + * description: A caller without settings:view receives only the values the interface needs to render itself. The operational settings are omitted from the body rather than returned empty, so an absent field says nothing about whether it is configured. * resp-401: Not authenticated * resp-500: Failed to load settings */ export const GET: RequestHandler = async ({ cookies }) => { const auth = await authorize(cookies); - // UI preferences (time format, date format) should be available to all authenticated users - // This doesn't expose sensitive data and is needed for proper UI rendering + // Every signed-in user gets the presentation half of this table, which the UI needs + // to render at all; the operational half is filtered out below. if (auth.authEnabled && !auth.isAuthenticated) { return json({ error: 'Authentication required' }, { status: 401 }); } @@ -421,7 +423,10 @@ export const GET: RequestHandler = async ({ cookies }) => { stackLogOperations: parseStackLogOperationsStorage(stackLogOperationsRaw) }; - return json(settings); + // Everybody needs the values that decide how a date or a log line is drawn. + // The rest describes how this installation is built and run, so it is held to + // the same permission as the settings page itself. + return json(visibleGeneralSettings(settings, await auth.can('settings', 'view'))); } catch (error) { console.error('Failed to get general settings:', error); return json({ error: 'Failed to get general settings' }, { status: 500 }); diff --git a/src/routes/containers/+page.svelte b/src/routes/containers/+page.svelte index 13bdfd668..d690db110 100644 --- a/src/routes/containers/+page.svelte +++ b/src/routes/containers/+page.svelte @@ -2222,6 +2222,12 @@ {/if} {/if}
+ {:else if !$canAccess('schedules', 'view')} + + ? {:else} - {/if} @@ -2685,6 +2691,9 @@ onRestart={$canAccess('containers', 'restart') ? (id) => restartContainer(id) : undefined} onRemove={$canAccess('containers', 'remove') ? (id) => removeContainer(id) : undefined} onEdit={$canAccess('containers', 'edit') ? (id) => editContainer(id) : undefined} + onUpdate={$canAccess('containers', 'create') + ? (id, name) => updateSingleContainer(id, name) + : undefined} /> Promise | void; onRemove?: (id: string) => Promise | void; onEdit?: (id: string) => void; + /** Runs the image update. The parent owns the update modal, so this closes ours. */ + onUpdate?: (id: string, name: string) => void; } - let { open = $bindable(), containerId, containerName, onRename, onStart, onStop, onRestart, onRemove, onEdit }: Props = $props(); + let { open = $bindable(), containerId, containerName, onRename, onStart, onStop, onRestart, onRemove, onEdit, onUpdate }: Props = $props(); + + // Whether this container has a newer image waiting, from the same store the list reads. + const hasImageUpdate = $derived($containerStore.pendingUpdateIds.includes(containerId)); + + function doUpdate() { + if (!onUpdate) return; + // The update recreates the container, so this view's data is about to go stale. + open = false; + onUpdate(containerId, displayName || containerId.slice(0, 12)); + } // Confirmation-popover open state for the destructive actions in the header. + let confirmUpdateOpen = $state(false); + let confirmUpdateImageOpen = $state(false); let confirmStopOpen = $state(false); let confirmRestartOpen = $state(false); let confirmRemoveOpen = $state(false); @@ -725,6 +741,21 @@
+ {#if hasImageUpdate && onUpdate} + confirmUpdateOpen = o} + > + {#snippet children({ open })} + + {/snippet} + + {/if} {#if containerData.State?.Running} {#if onStop} Image
{containerData.Config?.Image || 'N/A'} + {#if hasImageUpdate} + {#if onUpdate} + confirmUpdateImageOpen = o} + > + {#snippet children({ open })} + + + Update available + + {/snippet} + + {:else} + + + Update available + + {/if} + {/if}
@@ -1391,6 +1447,7 @@ {:else if containerData.State?.Paused}
diff --git a/src/routes/settings/environments/EnvironmentModal.svelte b/src/routes/settings/environments/EnvironmentModal.svelte index 4286f33b6..6d9d83aef 100644 --- a/src/routes/settings/environments/EnvironmentModal.svelte +++ b/src/routes/settings/environments/EnvironmentModal.svelte @@ -302,6 +302,11 @@ let formHighlightChanges = $state(true); let formDiskWarningEnabled = $state(true); let formDiskWarningMode = $state<'percentage' | 'absolute'>('percentage'); + /** Whether this host reports a total for percentage warnings; null = not asked yet. */ + let formPercentageSupported = $state(null); + let formStorageDriver = $state(null); + /** The mode the server has, kept apart from the form so edits cannot lock it out. */ + let storedDiskWarningMode = $state<'percentage' | 'absolute' | null>(null); let formDiskWarningThreshold = $state(80); let formDiskWarningThresholdGb = $state(50); let formConnectionType = $state('socket'); @@ -608,6 +613,11 @@ formLabels = parseLabels(environment.labels); newLabelInput = ''; formPublicIp = environment.publicIp || ''; + // Unknown until this environment's own host answers: the previous one's + // verdict must not be shown against a different host. + formPercentageSupported = null; + formStorageDriver = null; + storedDiskWarningMode = null; modalTab = 'general'; // Reset icon state pendingIconData = null; @@ -648,6 +658,9 @@ formHighlightChanges = true; formDiskWarningEnabled = true; formDiskWarningMode = 'percentage'; + formPercentageSupported = null; + formStorageDriver = null; + storedDiskWarningMode = null; formDiskWarningThreshold = 80; formDiskWarningThresholdGb = 50; formConnectionType = 'socket'; @@ -1079,8 +1092,11 @@ const data = await response.json(); formDiskWarningEnabled = data.enabled ?? true; formDiskWarningMode = data.mode ?? 'percentage'; + storedDiskWarningMode = formDiskWarningMode; formDiskWarningThreshold = data.threshold ?? 80; formDiskWarningThresholdGb = data.thresholdGb ?? 50; + formPercentageSupported = data.percentageSupported ?? null; + formStorageDriver = data.storageDriver ?? null; } } catch (error) { console.error('Failed to load disk warning settings:', error); @@ -2629,6 +2645,9 @@ bind:diskWarningMode={formDiskWarningMode} bind:diskWarningThreshold={formDiskWarningThreshold} bind:diskWarningThresholdGb={formDiskWarningThresholdGb} + percentageSupported={formPercentageSupported} + storageDriver={formStorageDriver} + {storedDiskWarningMode} /> diff --git a/src/routes/settings/environments/tabs/ActivityTab.svelte b/src/routes/settings/environments/tabs/ActivityTab.svelte index dc9861496..bc96ccabe 100644 --- a/src/routes/settings/environments/tabs/ActivityTab.svelte +++ b/src/routes/settings/environments/tabs/ActivityTab.svelte @@ -3,7 +3,11 @@ import { Label } from '$lib/components/ui/label'; import { TogglePill } from '$lib/components/ui/toggle-pill'; import * as Select from '$lib/components/ui/select'; - import { Percent, HardDrive } from 'lucide-svelte'; + import { Percent, HardDrive, TriangleAlert } from 'lucide-svelte'; + import { + percentageUnsupportedNote, + percentageOptionDisabled + } from '$lib/utils/disk-percentage-support'; interface Props { collectActivity: boolean; @@ -13,6 +17,11 @@ diskWarningMode: 'percentage' | 'absolute'; diskWarningThreshold: number; diskWarningThresholdGb: number; + /** Whether this host reports a total to measure against; null = could not ask. */ + percentageSupported?: boolean | null; + storageDriver?: string | null; + /** The mode as loaded from the server, so switching away cannot lock it out. */ + storedDiskWarningMode?: 'percentage' | 'absolute' | null; } let { @@ -22,8 +31,16 @@ diskWarningEnabled = $bindable(), diskWarningMode = $bindable(), diskWarningThreshold = $bindable(), - diskWarningThresholdGb = $bindable() + diskWarningThresholdGb = $bindable(), + percentageSupported = null, + storageDriver = null, + storedDiskWarningMode = null }: Props = $props(); + + // Only a definite "no" disables the option. An unreachable host stays unknown, so a + // momentary outage never takes the setting away from somebody. + const percentageDead = $derived(percentageSupported === false); + const percentageNote = $derived(percentageUnsupportedNote(storageDriver));
@@ -63,7 +80,11 @@
{#if diskWarningMode === 'percentage'} - + {#if percentageDead} + + {:else} + + {/if} Percentage {:else} @@ -72,10 +93,18 @@
- -
- - Percentage + +
+
+ + Percentage +
+ {#if percentageDead} + does not work on this host + {/if}
@@ -106,5 +135,24 @@ GB {/if}
+ + {#if percentageDead && diskWarningMode === 'percentage'} + +
+ +
+

{percentageNote}

+ +
+
+ {/if} {/if}
diff --git a/tests/compose-validate/config-errors.test.ts b/tests/compose-validate/config-errors.test.ts index 36b7bc11a..b2da74a1c 100644 --- a/tests/compose-validate/config-errors.test.ts +++ b/tests/compose-validate/config-errors.test.ts @@ -35,4 +35,51 @@ describe('parseConfigErrors (docker compose config stderr)', () => { expect(out).toHaveLength(1); expect(out[0].message).toBe('services.db.image is required'); }); + + describe('complaints about the stand-in for a secret', () => { + // Previewing a stack whose values come from a provider substitutes a placeholder, + // and config then objects to the placeholder rather than to the file. Reporting + // that would tell the user their compose is broken when it is not. + // + // The stand-in is spelled out rather than imported: these pin the bytes compose + // echoes back, and comparing the constant against itself would assert nothing. + test('a stderr that is only placeholder complaints reports nothing', () => { + const stderr = + 'validating /tmp/x/docker-compose.yml: services.web.ports.0: invalid hostPort: dockhand-provider-value'; + expect(parseConfigErrors(stderr)).toEqual([]); + }); + + test('a real error alongside a placeholder complaint is still reported', () => { + const stderr = [ + 'services.web.ports.0: invalid hostPort: dockhand-provider-value', + 'services.db.image is required' + ].join('\n'); + const out = parseConfigErrors(stderr); + expect(out).toHaveLength(1); + expect(out[0].message).toBe('services.db.image is required'); + }); + + test('the generic fallback is not raised for a placeholder-only failure', () => { + // Without the placeholder branch the empty finding list would fall through to + // "rejected the file (no detail provided)", which is the same false alarm. + const out = parseConfigErrors('invalid hostPort: dockhand-provider-value'); + expect(out).toEqual([]); + }); + + test('the match is on the name alone, so any message carrying it is dropped', () => { + // isPlaceholderArtefact is a substring test, which means a real complaint that + // merely names the stand-in is suppressed too. Pinned rather than asserted as + // desirable: a preview cannot tell the two apart, and staying quiet about a + // value the user did not write beats crying wolf on every previewed stack. + expect(parseConfigErrors('services.db.image is required: dockhand-provider-value')).toEqual( + [] + ); + }); + + test('an error naming no stand-in is reported as usual', () => { + const out = parseConfigErrors('services.web.environment.API_KEY is required'); + expect(out).toHaveLength(1); + expect(out[0].ruleId).toBe('COMPOSE_SCHEMA_ERROR'); + }); + }); }); diff --git a/tests/env-file-values.test.ts b/tests/env-file-values.test.ts new file mode 100644 index 000000000..9ef002ffa --- /dev/null +++ b/tests/env-file-values.test.ts @@ -0,0 +1,334 @@ +import { describe, test, expect } from 'bun:test'; +import { readFileSync } from 'node:fs'; +import { isGitStackOverride } from '../src/lib/env-merge'; +import { + unquote, + expandValue, + resolveEnvValues, + quoteForEnvFile +} from '../src/lib/utils/env-file-values'; + +// Every expectation below was measured against `docker compose config` with the same +// .env file, so these pin real behaviour rather than an assumption about it. +describe('quotes belong to the file, not the value', () => { + test('double and single quotes are stripped', () => { + expect(unquote('"/srv/data"')).toBe('/srv/data'); + expect(unquote("'/srv/data'")).toBe('/srv/data'); + }); + + test('an unquoted value is untouched, and so is a lone quote', () => { + expect(unquote('/srv/data')).toBe('/srv/data'); + expect(unquote('"')).toBe('"'); + expect(unquote('say "hi"')).toBe('say "hi"'); + }); + + test('mismatched quotes are not a pair', () => { + expect(unquote(`"/srv'`)).toBe(`"/srv'`); + }); +}); + +describe('a value referring to another', () => { + const known = { BASE: '/srv' }; + + // The reported case: the label read "${DIRECTORY}/immich" verbatim, and a bind + // mount using it resolved to nothing. + test('${NAME} is replaced, quotes and all', () => { + expect(expandValue('"${BASE}/immich"', known)).toBe('/srv/immich'); + }); + + test('the brace-less form works too', () => { + expect(expandValue('$BASE/bare', known)).toBe('/srv/bare'); + }); + + test('single quotes are literal, as in a shell', () => { + expect(expandValue("'${BASE}/sq'", known)).toBe('${BASE}/sq'); + }); + + test('an unknown name becomes empty rather than staying as text', () => { + expect(expandValue('${NOPE}/x', known)).toBe('/x'); + }); + + test('several references in one value are all replaced', () => { + expect(expandValue('${BASE}/a/${BASE}/b', known)).toBe('/srv/a//srv/b'); + }); + + test('text with no reference is returned as it is', () => { + expect(expandValue('plain/path', known)).toBe('plain/path'); + expect(expandValue('', known)).toBe(''); + }); +}); + +describe('a whole file', () => { + test('a later line sees an earlier one', () => { + const out = resolveEnvValues([ + ['DIRECTORY', '/path/to/base/directory'], + ['LOCATION', '"${DIRECTORY}/immich"'] + ]); + expect(out).toEqual({ + DIRECTORY: '/path/to/base/directory', + LOCATION: '/path/to/base/directory/immich' + }); + }); + + // Measured: compose resolves a forward reference to nothing, not to the later value. + test('a reference to a line further down is empty', () => { + const out = resolveEnvValues([ + ['FWD', '${LATER}/fwd'], + ['LATER', '/later'] + ]); + expect(out.FWD).toBe('/fwd'); + expect(out.LATER).toBe('/later'); + }); + + test('a chain resolves through every step', () => { + const out = resolveEnvValues([ + ['A', '/a'], + ['B', '${A}/b'], + ['C', '${B}/c'] + ]); + expect(out.C).toBe('/a/b/c'); + }); + + test('a later line wins on a repeated key', () => { + expect(resolveEnvValues([['K', 'one'], ['K', 'two']]).K).toBe('two'); + }); + + test('an empty file gives nothing', () => { + expect(resolveEnvValues([])).toEqual({}); + }); +}); + +describe('a literal dollar', () => { + const known = { BASE: '/srv' }; + + // $$ is compose's escape and what people write for passwords, Traefik patterns and + // bcrypt hashes. Eating it truncates the value with no error - measured against a + // running container: compose gives pa$word. + test('$$ becomes one dollar, and the rest of the word survives', () => { + expect(expandValue('pa$$word', known)).toBe('pa$word'); + }); + + test('escapes and references mix in one value', () => { + expect(expandValue('$$literal-${BASE}-$$end', known)).toBe('$literal-/srv-$end'); + }); + + test('a caddy-style pattern keeps its shape', () => { + expect(expandValue('%{http.request.uri}$$foo', known)).toBe('%{http.request.uri}$foo'); + }); + + // One pass: text that arrives BY EXPANSION must not be scanned again, or a value + // that happens to contain $NAME would be expanded twice. + test('an expanded value is not rescanned for references', () => { + const out = resolveEnvValues([ + ['A', '$$B'], + ['B', 'x'], + ['C', '${A}'] + ]); + expect(out.A).toBe('$B'); + expect(out.C).toBe('$B'); + }); +}); + +describe('where values are resolved and where they are not', () => { + // Expanding is for a deploy. A view must hand back the user's own text: it is the + // diff base the editor decides overrides against, so showing a resolved value there + // would let a save flatten ${DIRECTORY} into a path for good. + const git = readFileSync(new URL('../src/lib/server/git.ts', import.meta.url), 'utf8'); + + test('the editor read and the previews keep the raw text', () => { + const views = git.match(/parseEnvFileContent\([^)]*expand: false[^)]*\)/g) ?? []; + expect(views.length).toBe(3); + }); + + test('the deploy paths resolve', () => { + // Sync and deploy-with-progress pass no options, so they expand. + expect(git).toContain("parseEnvFileContent(envFileContent, gitStack.stackName)"); + expect(git).toContain("parseEnvFileContent(envContent, gitStack.stackName)"); + }); +}); + +describe('the forms that carry a default or an alternative', () => { + // Every expectation measured by running the same .env through a container, so these + // pin compose's behaviour rather than an assumption about it. + const known = { BASE: '/srv', EMPTY: '' }; + + test('a default fills in for a name with no value', () => { + expect(expandValue('${NOPE:-fallback}', known)).toBe('fallback'); + expect(expandValue('${NOPE-fallback}', known)).toBe('fallback'); + }); + + // Measured: compose treats an empty value as absent for BOTH forms here. + test('an empty value takes the default too', () => { + expect(expandValue('${EMPTY:-fallback}', known)).toBe('fallback'); + }); + + test('a set name keeps its own value', () => { + expect(expandValue('${BASE:-fallback}', known)).toBe('/srv'); + }); + + test('the alternative appears only when the name has a value', () => { + expect(expandValue('${BASE:+present}', known)).toBe('present'); + expect(expandValue('${NOPE:+present}', known)).toBe(''); + expect(expandValue('${EMPTY:+present}', known)).toBe(''); + }); + + test('a default may itself refer to another variable', () => { + expect(expandValue('${NOPE:-${BASE}/x}', known)).toBe('/srv/x'); + }); + + // Nobody is reading a message here, so the value is all this can give; the deploy + // reports the missing variable itself. + + + test('an unbalanced brace is left as written', () => { + expect(expandValue('${BASE', known)).toBe('${BASE'); + }); + + test('a dollar that starts no name is just text', () => { + expect(expandValue('cost$ 5', known)).toBe('cost$ 5'); + expect(expandValue('ends with $', known)).toBe('ends with $'); + }); +}); + +describe('writing a resolved value back for compose to read', () => { + // Compose reads the generated file with its own interpolation pass. Measured: the + // bare KEY=value form loses a hash after a space to a comment, trims surrounding + // spaces, eats a dollar, and a leading quote breaks the file. Quoting carries all + // of them through. + test('a dollar survives', () => { + expect(quoteForEnvFile('pa$word')).toBe('"pa$$word"'); + expect(quoteForEnvFile('$2y$10$abc')).toBe('"$$2y$$10$$abc"'); + }); + + test('a hash and surrounding spaces survive', () => { + expect(quoteForEnvFile('value # not a comment')).toBe('"value # not a comment"'); + expect(quoteForEnvFile(' padded ')).toBe('" padded "'); + }); + + test('a quote and a backslash are escaped, in that order', () => { + expect(quoteForEnvFile('has"quote')).toBe('"has\\"quote"'); + expect(quoteForEnvFile('back\\slash')).toBe('"back\\\\slash"'); + }); + + test('an empty value is still a value', () => { + expect(quoteForEnvFile('')).toBe('""'); + }); +}); + + +describe('escape sequences in a double-quoted value', () => { + test('the usual shell escapes', () => { + expect(expandValue('"a\\nb"', {})).toBe('a\nb'); + expect(expandValue('"a\\tb"', {})).toBe('a\tb'); + expect(expandValue('"a\\\\b"', {})).toBe('a\\b'); + expect(expandValue('"say \\"hi\\""', {})).toBe('say "hi"'); + }); + + // A backslash-dollar is the other way of writing a literal dollar. + test('a backslash before a dollar keeps it literal', () => { + expect(expandValue('"\\$literal"', {})).toBe('$literal'); + }); + + test('an octal escape is a character', () => { + expect(expandValue('"\\0101"', {})).toBe('A'); + }); + + // Single quotes are literal throughout, escapes included. + test('single quotes take no escapes', () => { + expect(expandValue("'a\\nb'", {})).toBe('a\\nb'); + }); +}); + +describe('a comment after an unquoted value', () => { + test('a hash preceded by a space starts one', () => { + expect(expandValue('value # a note', {})).toBe('value'); + }); + + // A hash inside the value - a colour, a URL fragment - is part of it. + test('a hash with no space before it is part of the value', () => { + expect(expandValue('value#notacomment', {})).toBe('value#notacomment'); + expect(expandValue('#aabbcc', {})).toBe('#aabbcc'); + }); + + test('a quoted value keeps its hash', () => { + expect(expandValue('"value # kept"', {})).toBe('value # kept'); + }); +}); + +describe('variables the author marked as required', () => { + // Nothing here can refuse a deploy, so `:?` resolves like the plain form and compose + // raises the missing value on its own pass. The explanation is not left in the value. + test('an unset required variable resolves empty, message and all', () => { + expect(expandValue('${NOPE:?set HOST_DATA}', {})).toBe(''); + }); + + test('a satisfied requirement resolves to the value', () => { + expect(expandValue('${A:?why}', { A: 'v' })).toBe('v'); + }); + + test('the form without a colon behaves the same way', () => { + expect(expandValue('${E?why}', { E: '' })).toBe(''); + }); +}); + +describe('what the editor is given', () => { + // The editor compares what is in the form against what came from the file to decide + // which variables are the user's own overrides. Handing it a RESOLVED value would + // make an untouched ${DIRECTORY}/immich look changed, and saving would write the + // path in place of the variable - the shape of the bug #1112 was closed for. + const git = readFileSync(new URL('../src/lib/server/git.ts', import.meta.url), 'utf8'); + + test('the editor read and the two previews keep the raw text', () => { + expect((git.match(/parseEnvFileContent\([^)]*expand: false[^)]*\)/g) ?? []).length).toBe(3); + }); + + test('an untouched variable is not mistaken for an override', () => { + const fileVars = { DIRECTORY: '/path/to/base', LOCATION: '"${DIRECTORY}/immich"' }; + const untouched = { key: 'LOCATION', value: '"${DIRECTORY}/immich"', isSecret: false }; + expect(isGitStackOverride(untouched, fileVars)).toBe(false); + }); + + test('a variable the user really changed still counts', () => { + const fileVars = { PORT: '8080' }; + expect(isGitStackOverride({ key: 'PORT', value: '9090', isSecret: false }, fileVars)).toBe(true); + }); +}); + +describe('what a Hawser agent is sent', () => { + // A Hawser stack's .env is read here and injected into the agent's compose process + // as shell environment, which compose treats as a substitution SOURCE and does not + // interpolate again. So this side has to resolve the file exactly as compose would + // when it reads the file itself - otherwise the same .env means one thing on a local + // stack and another on a remote one. + // + // Each expectation below was read out of a running container (env -0) after compose + // resolved the same line from an env_file, on docker compose v2. + const asComposeResolvesIt: [string, string, string][] = [ + ['an unquoted apr1 hash loses its variable-looking segments', + 'admin:$apr1$Xy9$8fJq', 'admin:$8fJq'], + ['a double-quoted value is still interpolated', + '"admin:$apr1$keep"', 'admin:'], + ['single quotes keep a hash intact, which is the fix to advise', + "'admin:$apr1$lit'", 'admin:$apr1$lit'], + ['a hash after a space starts a comment', + 'secret #1 pass', 'secret'], + ['$$ is the escape for one literal dollar', + 'pa$$word', 'pa$word'] + ]; + + for (const [what, raw, expected] of asComposeResolvesIt) { + test(what, () => { + expect(expandValue(raw, {})).toBe(expected); + }); + } + + test('no read on the way to an agent opts out of resolving', () => { + // Shipping the raw text would hand the agent a value the local path never + // produces, which is the divergence this parser exists to close. Only the editor + // and the repo previews keep the raw text, and those live in git.ts. + const stacks = readFileSync(new URL('../src/lib/server/stacks.ts', import.meta.url), 'utf8'); + const calls = stacks.match(/parseEnvFileContent\([^;]*?\)/g) ?? []; + expect(calls.length).toBeGreaterThan(0); + expect(calls.filter((c) => c.includes('expand: false'))).toEqual([]); + }); +}); diff --git a/tests/env-panel-core.test.ts b/tests/env-panel-core.test.ts new file mode 100644 index 000000000..3670a2dda --- /dev/null +++ b/tests/env-panel-core.test.ts @@ -0,0 +1,179 @@ +import { describe, test, expect } from 'bun:test'; +import { + parseRawContent, + generateRawContent, + textEditorContent, + keysInRawContent, + mergeParsedIntoVariables, + type EnvVarLike +} from '../src/lib/utils/env-panel-core'; + +/** + * Replays what the panel does on save, so the #1620 data loss is covered by an + * assertion rather than by reading the component. + */ +function saveFromTextView(rawContent: string, variables: EnvVarLike[]): EnvVarLike[] { + const source = textEditorContent(rawContent, variables); + const { vars } = parseRawContent(source); + return mergeParsedIntoVariables(vars, variables, keysInRawContent(source)).filter((v) => + v.key.trim() + ); +} + +/** Replays handleTextChange: what the user types replaces what the editor showed. */ +function typeIntoEditor( + typed: string, + rawContent: string, + variables: EnvVarLike[] +): { rawContent: string; variables: EnvVarLike[] } { + const shownBefore = keysInRawContent(textEditorContent(rawContent, variables)); + const { vars } = parseRawContent(typed); + return { rawContent: typed, variables: mergeParsedIntoVariables(vars, variables, shownBefore) }; +} + +describe('#1620 - a row added outside the panel survives a save in text view', () => { + test('the bulk selector is kept when the .env file is empty', () => { + // The secret provider picker writes only into `variables`; the file is still empty, + // so the editor is showing the generated text. + const variables: EnvVarLike[] = [ + { key: 'DOCKHAND_SECRET_SELECTOR', value: 'env-abc123', isSecret: false } + ]; + const saved = saveFromTextView('', variables); + expect(saved).toEqual([ + { key: 'DOCKHAND_SECRET_SELECTOR', value: 'env-abc123', isSecret: false } + ]); + }); + + test('secrets are carried through untouched alongside it', () => { + const variables: EnvVarLike[] = [ + { key: 'DOCKHAND_SECRET_SELECTOR', value: 'env-abc123', isSecret: false }, + { key: 'DB_PASSWORD', value: 's3cr3t', isSecret: true } + ]; + const saved = saveFromTextView('', variables); + expect(saved.map((v) => v.key).sort()).toEqual(['DB_PASSWORD', 'DOCKHAND_SECRET_SELECTOR']); + expect(saved.find((v) => v.key === 'DB_PASSWORD')?.value).toBe('s3cr3t'); + }); + + test('the selector is kept when the .env file already has content', () => { + // The commoner shape: a real .env exists, so the editor shows the file and the + // selector row is not even visible. It must still survive the save. + const variables: EnvVarLike[] = [ + { key: 'FOO', value: 'bar', isSecret: false }, + { key: 'DOCKHAND_SECRET_SELECTOR', value: 'env-abc123', isSecret: false } + ]; + const saved = saveFromTextView('FOO=bar\n', variables); + expect(saved.map((v) => v.key).sort()).toEqual(['DOCKHAND_SECRET_SELECTOR', 'FOO']); + }); + + test('the file wins on value for a key it shares with a row', () => { + const variables: EnvVarLike[] = [{ key: 'A', value: 'stale', isSecret: false }]; + const saved = saveFromTextView('A=fromfile\n', variables); + expect(saved).toEqual([{ key: 'A', value: 'fromfile', isSecret: false }]); + }); +}); + +describe('#1620 - a row the user deleted in the editor stays deleted', () => { + test('deleting a visible line removes it, and a save does not bring it back', () => { + const before: EnvVarLike[] = [ + { key: 'KEEP', value: '1', isSecret: false }, + { key: 'DROPME', value: '2', isSecret: false } + ]; + const afterTyping = typeIntoEditor('KEEP=1\n', 'KEEP=1\nDROPME=2\n', before); + expect(afterTyping.variables.map((v) => v.key)).toEqual(['KEEP']); + + const saved = saveFromTextView(afterTyping.rawContent, afterTyping.variables); + expect(saved.map((v) => v.key)).toEqual(['KEEP']); + }); + + test('clearing the whole editor clears the visible rows but keeps secrets', () => { + const before: EnvVarLike[] = [ + { key: 'GONE', value: 'x', isSecret: false }, + { key: 'DB_PASSWORD', value: 's3cr3t', isSecret: true } + ]; + const after = typeIntoEditor('', 'GONE=x\n', before); + expect(after.variables.map((v) => v.key)).toEqual(['DB_PASSWORD']); + }); + + test('an off-screen row survives an edit that never showed it', () => { + // The selector was written into variables while the editor showed only the file. + const before: EnvVarLike[] = [ + { key: 'FOO', value: 'bar', isSecret: false }, + { key: 'DOCKHAND_SECRET_SELECTOR', value: 'env-abc', isSecret: false } + ]; + const after = typeIntoEditor('FOO=changed\n', 'FOO=bar\n', before); + expect(after.variables.map((v) => v.key).sort()).toEqual(['DOCKHAND_SECRET_SELECTOR', 'FOO']); + expect(after.variables.find((v) => v.key === 'FOO')?.value).toBe('changed'); + }); +}); + +describe('textEditorContent', () => { + test('prefers the file when it has content', () => { + expect(textEditorContent('A=1\n', [{ key: 'B', value: '2' }])).toBe('A=1\n'); + }); + + test('falls back to the rows when the file is empty or blank', () => { + expect(textEditorContent('', [{ key: 'B', value: '2' }])).toBe('B=2\n'); + expect(textEditorContent(' \n ', [{ key: 'B', value: '2' }])).toBe('B=2\n'); + }); + + test('empty on both sides', () => { + expect(textEditorContent('', [])).toBe(''); + }); +}); + +describe('generateRawContent', () => { + test('renders non-secret rows and trims keys', () => { + expect(generateRawContent([{ key: ' A ', value: '1' }, { key: 'B', value: '2' }])).toBe( + 'A=1\nB=2\n' + ); + }); + + test('skips secrets and unnamed rows', () => { + expect( + generateRawContent([ + { key: 'A', value: '1' }, + { key: 'S', value: 'x', isSecret: true }, + { key: ' ', value: 'nameless' } + ]) + ).toBe('A=1\n'); + }); + + test('nothing to write', () => { + expect(generateRawContent([])).toBe(''); + expect(generateRawContent([{ key: 'S', value: 'x', isSecret: true }])).toBe(''); + }); +}); + +describe('parseRawContent', () => { + test('reads keys and values, keeping = inside the value', () => { + const { vars } = parseRawContent('A=1\nURL=postgres://u:p@h/db?x=1\n'); + expect(vars).toEqual([ + { key: 'A', value: '1', isSecret: false }, + { key: 'URL', value: 'postgres://u:p@h/db?x=1', isSecret: false } + ]); + }); + + test('skips blanks and comments without warning', () => { + const { vars, warnings } = parseRawContent('\n# note\n\nA=1\n'); + expect(vars).toHaveLength(1); + expect(warnings).toEqual([]); + }); + + test('warns on a line with no = and on an invalid name', () => { + const { vars, warnings } = parseRawContent('JUSTTEXT\n1BAD=x\nOK=y\n'); + expect(vars).toEqual([{ key: 'OK', value: 'y', isSecret: false }]); + expect(warnings).toHaveLength(2); + expect(warnings[0]).toContain('Line 1'); + expect(warnings[1]).toContain('Line 2'); + }); + + test('an empty value is a value', () => { + expect(parseRawContent('EMPTY=\n').vars).toEqual([{ key: 'EMPTY', value: '', isSecret: false }]); + }); + + test('round-trips with generateRawContent', () => { + const rows: EnvVarLike[] = [{ key: 'A', value: '1' }, { key: 'B', value: 'x=y' }]; + const { vars } = parseRawContent(generateRawContent(rows)); + expect(vars.map((v) => ({ key: v.key, value: v.value }))).toEqual(rows); + }); +}); diff --git a/tests/image-export-name.test.ts b/tests/image-export-name.test.ts new file mode 100644 index 000000000..8b8d68728 --- /dev/null +++ b/tests/image-export-name.test.ts @@ -0,0 +1,72 @@ +import { describe, expect, test } from 'bun:test'; +import { exportFileStem, shortId, exportRefFor } from '../src/lib/server/image-export-name'; + +const ID = 'sha256:9f2a4b1c3d5e7081926354a7b8c9d0e1f2a3b4c5d6e7f80912a3b4c5d6e7f809'; + +describe('naming an exported image', () => { + test('after the tag that was clicked', () => { + // The export is addressed by id, so without this an image with several tags + // always downloads under whichever one Docker happens to list first. + const stem = exportFileStem(ID, ['technitium/dns-server:15.5.1', 'technitium/dns-server:latest'], 'technitium/dns-server:latest'); + expect(stem).toBe('technitium_dns-server_latest'); + }); + + test('after the first tag when none was asked for', () => { + expect(exportFileStem(ID, ['nginx:latest', 'nginx:1.27'], null)).toBe('nginx_latest'); + }); + + test('slashes and colons do not reach the filename', () => { + // A registry-qualified name has both, and neither belongs in a download name. + expect(exportFileStem(ID, ['registry.example.com/team/app:v2'], null)).toBe( + 'registry.example.com_team_app_v2' + ); + }); +}); + +describe('a tag the image does not carry', () => { + test('is ignored in favour of a real one', () => { + // The value arrives in a query parameter and ends up in a Content-Disposition + // header, so it is checked against the image rather than trusted. + expect(exportFileStem(ID, ['nginx:latest'], 'somebody-elses:tag')).toBe('nginx_latest'); + }); + + test('cannot smuggle a path or a header break into the filename', () => { + for (const hostile of ['../../etc/passwd', 'x"; rm -rf /', 'a\r\nContent-Length: 0']) { + expect(exportFileStem(ID, ['nginx:latest'], hostile)).toBe('nginx_latest'); + } + }); +}); + +describe('an image with no tags at all', () => { + test('falls back to the short digest, as docker shows it', () => { + expect(exportFileStem(ID, [], null)).toBe('9f2a4b1c3d5e'); + expect(exportFileStem(ID, undefined, 'anything')).toBe('9f2a4b1c3d5e'); + }); + + test('shortId drops the algorithm prefix', () => { + expect(shortId(ID)).toBe('9f2a4b1c3d5e'); + expect(shortId('9f2a4b1c3d5e7081')).toBe('9f2a4b1c3d5e'); + }); +}); + +describe('what the daemon is asked to export', () => { + test('the clicked tag, so the tar keeps the name', () => { + // A tar saved by id records RepoTags: null and loads back as an unnamed image, + // which breaks the export/load round trip the feature exists for. + expect(exportRefFor(ID, ['nginx:1.27', 'nginx:latest'], 'nginx:latest')).toBe('nginx:latest'); + }); + + test('the first tag when none was asked for', () => { + expect(exportRefFor(ID, ['nginx:latest', 'nginx:1.27'], null)).toBe('nginx:latest'); + }); + + test('a tag the image does not carry never reaches the daemon', () => { + expect(exportRefFor(ID, ['nginx:latest'], 'somebody-elses:tag')).toBe('nginx:latest'); + expect(exportRefFor(ID, ['nginx:latest'], '../../etc/passwd')).toBe('nginx:latest'); + }); + + test('an untagged image falls back to its id', () => { + expect(exportRefFor(ID, [], null)).toBe(ID); + expect(exportRefFor(ID, undefined, 'anything')).toBe(ID); + }); +}); diff --git a/tests/notification-basic-auth.test.ts b/tests/notification-basic-auth.test.ts new file mode 100644 index 000000000..16814fd2c --- /dev/null +++ b/tests/notification-basic-auth.test.ts @@ -0,0 +1,139 @@ +/** + * Basic auth for apprise:// and json:// notification URLs (#1611). + * fetch() rejects URLs with credentials, so user:pass@ must become an Authorization header. + */ + +import { describe, test, expect, afterEach } from 'bun:test'; +import { splitBasicAuth } from '../src/lib/server/notifications/shared'; +import { sendApprise } from '../src/lib/server/notifications/apprise'; +import { sendGenericWebhook } from '../src/lib/server/notifications/generic-webhook'; + +const basic = (creds: string) => `Basic ${Buffer.from(creds).toString('base64')}`; + +describe('splitBasicAuth', () => { + test('no userinfo means no header', () => { + expect(splitBasicAuth('apprise.example.com:8000')).toEqual({ host: 'apprise.example.com:8000', authHeader: null }); + }); + + test('user:pass becomes a Basic header', () => { + expect(splitBasicAuth('user:pass@host:8000')).toEqual({ host: 'host:8000', authHeader: basic('user:pass') }); + }); + + test('percent-encoded special characters are decoded', () => { + expect(splitBasicAuth('us%40er:p%3Aa%2Fs%40s@host')).toEqual({ host: 'host', authHeader: basic('us@er:p:a/s@s') }); + }); + + test('encoded ? and % are decoded', () => { + expect(splitBasicAuth('u:a%3Fb%25c@host')).toEqual({ host: 'host', authHeader: basic('u:a?b%c') }); + }); + + test('user without password', () => { + expect(splitBasicAuth('user@host')).toEqual({ host: 'host', authHeader: basic('user:') }); + }); + + test('empty userinfo is dropped without a header', () => { + expect(splitBasicAuth('@host')).toEqual({ host: 'host', authHeader: null }); + }); + + test('non-latin1 credentials still yield an ASCII header', () => { + const { authHeader } = splitBasicAuth('%C5%BC%C3%B3%C5%82w:has%C5%82o@host'); + expect(authHeader).toBe(basic('żółw:hasło')); + }); + + test('an @ after a backslash is path, not userinfo', () => { + expect(splitBasicAuth('hooks.example.com\\in@x.example.org')).toEqual({ host: 'hooks.example.com\\in@x.example.org', authHeader: null }); + }); + + test('malformed percent-encoding throws without echoing the credentials', () => { + expect(() => splitBasicAuth('user:%E0%A4%A@host')).toThrow('malformed percent-encoding in URL credentials'); + }); +}); + +describe('senders put credentials in a header, not the URL', () => { + const realFetch = globalThis.fetch; + let calls: { url: string; headers: Record }[] = []; + + function stubFetch() { + calls = []; + globalThis.fetch = (async (input: string | URL, init?: RequestInit) => { + calls.push({ url: String(input), headers: (init?.headers ?? {}) as Record }); + return new Response('ok', { status: 200 }); + }) as typeof fetch; + } + + afterEach(() => { + globalThis.fetch = realFetch; + }); + + test('apprises://user:pass@host/prefix/key', async () => { + stubFetch(); + const result = await sendApprise('apprises://admin:s%40cret@apprise.example.com:8443/apprise/mykey?tag=ops', { title: 't', message: 'm' }); + expect(result).toEqual({ success: true }); + expect(calls[0].url).toBe('https://apprise.example.com:8443/apprise/notify/mykey'); + expect(calls[0].headers['Authorization']).toBe(basic('admin:s@cret')); + expect(calls[0].headers['Content-Type']).toBe('application/json'); + }); + + test('apprise:// without credentials sends no Authorization', async () => { + stubFetch(); + await sendApprise('apprise://apprise.example.com/mykey', { title: 't', message: 'm' }); + expect(calls[0].url).toBe('http://apprise.example.com/notify/mykey'); + expect(calls[0].headers['Authorization']).toBeUndefined(); + }); + + test('jsons://user:pass@host/path?query keeps path and query', async () => { + stubFetch(); + const result = await sendGenericWebhook('jsons://bot:p%2Fw@hooks.example.com/in/abc?x=1', { title: 't', message: 'm' }); + expect(result).toEqual({ success: true }); + expect(calls[0].url).toBe('https://hooks.example.com/in/abc?x=1'); + expect(calls[0].headers['Authorization']).toBe(basic('bot:p/w')); + }); + + test('json:// without credentials is unchanged', async () => { + stubFetch(); + await sendGenericWebhook('json://hooks.example.com/in', { title: 't', message: 'm' }); + expect(calls[0].url).toBe('http://hooks.example.com/in'); + expect(calls[0].headers['Authorization']).toBeUndefined(); + }); + + test('json://host with no path and an @ in the query is not treated as credentials', async () => { + stubFetch(); + await sendGenericWebhook('json://hooks.example.com?to=a@b.c', { title: 't', message: 'm' }); + expect(calls[0].url).toBe('http://hooks.example.com?to=a@b.c'); + expect(calls[0].headers['Authorization']).toBeUndefined(); + }); + + test('json:// with a backslash before @ still targets the original host', async () => { + stubFetch(); + await sendGenericWebhook('json://hooks.example.com\\in@x.example.org/p', { title: 't', message: 'm' }); + expect(new URL(calls[0].url).hostname).toBe('hooks.example.com'); + expect(calls[0].headers['Authorization']).toBeUndefined(); + }); + + test('malformed credentials fail the send without a request', async () => { + stubFetch(); + const result = await sendApprise('apprise://u:%ZZ@apprise.example.com/key', { title: 't', message: 'm' }); + expect(result.success).toBe(false); + expect(result.error).not.toContain('%ZZ'); + expect(calls).toHaveLength(0); + }); +}); + +describe('an @ inside the password', () => { + // The split must take the LAST @, as the URL parser does. Taking the first one + // sends truncated credentials to a host the password's own text chose - so this + // case is what pins the choice, and every other fixture passes either way. + test('the host is what follows the final @, and the password keeps its own', () => { + expect(splitBasicAuth('user:p@ss@host.example.com')).toEqual({ + host: 'host.example.com', + authHeader: basic('user:p@ss') + }); + }); + + test('several @ in the password still leave the real host', () => { + expect(splitBasicAuth('u:a@b@c@host.example.com')).toEqual({ + host: 'host.example.com', + authHeader: basic('u:a@b@c') + }); + }); +}); diff --git a/tests/openapi-body-annotations.test.ts b/tests/openapi-body-annotations.test.ts index b902bd71d..1146ba518 100644 --- a/tests/openapi-body-annotations.test.ts +++ b/tests/openapi-body-annotations.test.ts @@ -138,3 +138,30 @@ describe('buildSpec: emits requestBody for non-JSON bodies', () => { expect(mp.schema.required).toEqual(['files']); }); }); + +describe('a description survives into the spec whole', () => { + // The annotation parser is line-oriented: it reads `key: value` off one line and + // drops anything that follows on the next. A description wrapped across lines + // therefore publishes its first line and silently loses the rest - and the drift + // gates do not look at descriptions, so the build stays green while /api/docs + // serves half a sentence. + test('no shipped description ends mid-sentence', () => { + // Reads the generated spec rather than the sources, so it catches the mistake + // wherever it is made rather than in one file at a time. + const { readFileSync } = require('node:fs') as typeof import('node:fs'); + const spec = JSON.parse( + readFileSync(new URL('../src/lib/openapi.generated.json', import.meta.url), 'utf8') + ); + + const truncated: string[] = []; + for (const [path, ops] of Object.entries>(spec.paths)) { + for (const [method, op] of Object.entries(ops)) { + const d = op?.description; + if (typeof d === 'string' && d && !/[.)\]]$/.test(d.trimEnd())) { + truncated.push(`${method.toUpperCase()} ${path}`); + } + } + } + expect(truncated).toEqual([]); + }); +}); diff --git a/tests/podman-network-key.test.ts b/tests/podman-network-key.test.ts new file mode 100644 index 000000000..1ee5fad6d --- /dev/null +++ b/tests/podman-network-key.test.ts @@ -0,0 +1,111 @@ +import { describe, test, expect } from 'bun:test'; +import { isUnknownNetworkKeyError, retryEndpointKey } from '../src/lib/server/podman-network-key'; + +describe('isUnknownNetworkKeyError', () => { + test('matches what Podman actually returns for a rejected endpoint key', () => { + // Verbatim from Podman 5.4.2 on a create keyed by "bridge". + expect( + isUnknownNetworkKeyError( + 'container create: unable to find network with name or ID bridge: network not found' + ) + ).toBe(true); + }); + + test('matches regardless of which network name is named', () => { + expect( + isUnknownNetworkKeyError('unable to find network with name or ID my-net: network not found') + ).toBe(true); + }); + + test('other create failures are left alone, so the retry stays narrow', () => { + expect(isUnknownNetworkKeyError('No such image: alpine:latest')).toBe(false); + expect(isUnknownNetworkKeyError('Conflict. The container name is already in use')).toBe(false); + expect(isUnknownNetworkKeyError('invalid IP address in add-host')).toBe(false); + expect(isUnknownNetworkKeyError('network not found')).toBe(false); + }); + + test('Docker rejecting aliases on the default bridge does not trigger a retry', () => { + // Verbatim from Docker 29.4.1. It also names a network and also concerns + // endpoint settings, so it is the nearest thing to a false positive. + expect( + isUnknownNetworkKeyError( + 'invalid config for network bridge: invalid endpoint settings:\nnetwork-scoped aliases are only supported for user-defined networks' + ) + ).toBe(false); + }); + + test('a missing or non-string message never triggers a retry', () => { + expect(isUnknownNetworkKeyError(undefined)).toBe(false); + expect(isUnknownNetworkKeyError(null)).toBe(false); + expect(isUnknownNetworkKeyError('')).toBe(false); + expect(isUnknownNetworkKeyError(42 as never)).toBe(false); + }); +}); + +describe('retryEndpointKey', () => { + const PODMAN_ERR = + 'container create: unable to find network with name or ID bridge: network not found'; + + test('the aliased bridge retries by id', () => { + expect( + retryEndpointKey({ + message: PODMAN_ERR, + endpoints: { bridge: { Aliases: ['web'] } }, + key: 'bridge', + networkId: '2f259bab93aa' + }) + ).toBe('2f259bab93aa'); + }); + + test('a network that does not exist rethrows instead of retrying', () => { + // Podman words a typo exactly like the bridge bug, so the message alone + // cannot tell them apart - the absent id is what does. Both shapes the + // lookup can return for "no such network" must refuse the retry. + for (const networkId of [null, undefined, '']) { + expect( + retryEndpointKey({ + message: 'unable to find network with name or ID typo-net: network not found', + endpoints: { 'typo-net': {} }, + key: 'typo-net', + networkId + }) + ).toBeNull(); + } + }); + + test('an id equal to the key means there is nothing to retry with', () => { + expect( + retryEndpointKey({ + message: PODMAN_ERR, + endpoints: { bridge: {} }, + key: 'bridge', + networkId: 'bridge' + }) + ).toBeNull(); + }); + + test('a key absent from the payload is not ours to rewrite', () => { + expect( + retryEndpointKey({ + message: PODMAN_ERR, + endpoints: { 'some-other-net': {} }, + key: 'bridge', + networkId: '2f259bab93aa' + }) + ).toBeNull(); + expect( + retryEndpointKey({ message: PODMAN_ERR, endpoints: undefined, key: 'bridge', networkId: 'x' }) + ).toBeNull(); + }); + + test('an unrelated failure never retries, whatever the lookup returned', () => { + expect( + retryEndpointKey({ + message: 'No such image: alpine:latest', + endpoints: { bridge: {} }, + key: 'bridge', + networkId: '2f259bab93aa' + }) + ).toBeNull(); + }); +}); diff --git a/tests/provider-kind-core.test.ts b/tests/provider-kind-core.test.ts new file mode 100644 index 000000000..789d1f185 --- /dev/null +++ b/tests/provider-kind-core.test.ts @@ -0,0 +1,56 @@ +import { describe, expect, test } from 'bun:test'; +import { providerKind, oidcProviderName } from '../src/lib/server/provider-kind-core'; + +/** + * What a session's provider column means. + * + * It decides what the session reports itself as, and whether logging out also ends + * the session at the identity provider - so a provider whose name is read wrong + * quietly leaves the user signed in upstream. + */ + +describe('the kind of account a session belongs to', () => { + test('a named OIDC provider is an OIDC session', () => { + expect(providerKind('oidc:Keycloak')).toBe('oidc'); + }); + + test('a name containing colons is still one name', () => { + // "oidc:" is a prefix, not a separator to split on. + expect(providerKind('oidc:corp:eu:keycloak')).toBe('oidc'); + }); + + test('a bare kind is that kind', () => { + expect(providerKind('oidc')).toBe('oidc'); + expect(providerKind('ldap')).toBe('ldap'); + expect(providerKind('local')).toBe('local'); + }); + + test('no provider at all is a local account', () => { + expect(providerKind(null)).toBe('local'); + expect(providerKind(undefined)).toBe('local'); + expect(providerKind('')).toBe('local'); + }); + + test('something nothing writes grants nothing extra', () => { + expect(providerKind('saml')).toBe('local'); + expect(providerKind('oidcx')).toBe('local'); + }); +}); + +describe('which provider should end the session', () => { + test('the name after the prefix', () => { + expect(oidcProviderName('oidc:Keycloak')).toBe('Keycloak'); + expect(oidcProviderName('oidc:corp:eu')).toBe('corp:eu'); + }); + + test('a bare oidc names nobody, so logout stays local', () => { + expect(oidcProviderName('oidc')).toBeNull(); + expect(oidcProviderName('oidc:')).toBeNull(); + }); + + test('other kinds name nobody', () => { + expect(oidcProviderName('ldap:AD')).toBeNull(); + expect(oidcProviderName('local')).toBeNull(); + expect(oidcProviderName(null)).toBeNull(); + }); +}); diff --git a/tests/reader-abort-core.test.ts b/tests/reader-abort-core.test.ts new file mode 100644 index 000000000..3807ceb0d --- /dev/null +++ b/tests/reader-abort-core.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, test } from 'bun:test'; +import { PassThrough } from 'node:stream'; +import { cancelReaderOnAbort } from '../src/lib/server/reader-abort-core'; +import { toWebReadableStream } from '../src/lib/server/node-readable-stream'; + +const tick = () => new Promise((resolve) => setTimeout(resolve, 20)); + +async function collectUnhandled(fn: () => Promise): Promise { + const seen: unknown[] = []; + const onRejection = (err: unknown) => seen.push(err); + process.on('unhandledRejection', onRejection); + try { + await fn(); + await tick(); + } finally { + process.off('unhandledRejection', onRejection); + } + return seen; +} + +describe('cancelReaderOnAbort', () => { + test('cancels the reader when the signal aborts', async () => { + let cancelled = 0; + const ac = new AbortController(); + cancelReaderOnAbort({ cancel: async () => { cancelled++; } }, ac.signal); + expect(cancelled).toBe(0); + ac.abort(); + ac.abort(); + expect(cancelled).toBe(1); + }); + + test('swallows a rejected cancel() instead of leaving it unhandled', async () => { + const unhandled = await collectUnhandled(async () => { + const ac = new AbortController(); + cancelReaderOnAbort({ cancel: () => Promise.reject(new Error('aborted')) }, ac.signal); + ac.abort(); + }); + expect(unhandled).toEqual([]); + }); + + test('abort after the underlying stream errored does not throw', async () => { + const unhandled = await collectUnhandled(async () => { + const src = new PassThrough(); + const reader = toWebReadableStream(src).getReader(); + const ac = new AbortController(); + cancelReaderOnAbort(reader, ac.signal); + + src.destroy(Object.assign(new Error('aborted'), { code: 'ECONNRESET' })); + await expect(reader.read()).rejects.toThrow('aborted'); + + ac.abort(); + }); + expect(unhandled).toEqual([]); + }); + + test('no signal is a no-op', () => { + expect(() => cancelReaderOnAbort({ cancel: async () => {} }, undefined)).not.toThrow(); + }); +}); From d35f870327be16eb318993363f24de3e81375b1b Mon Sep 17 00:00:00 2001 From: Jarek Krochmalski Date: Tue, 29 Sep 2026 14:58:14 +0200 Subject: [PATCH 05/22] mark a secret provider as default so new stacks start with it selected --- src/lib/components/app-sidebar.svelte | 6 +- .../compose-validate/effective-compose.ts | 8 + src/lib/server/features.ts | 12 + src/lib/server/git.ts | 24 +- src/lib/server/provider-kind-core.ts | 23 + src/lib/server/stacks.ts | 6 +- src/lib/server/x509-display-core.ts | 40 ++ src/lib/utils/default-secret-provider.ts | 35 + src/lib/utils/validate-provider-keys.ts | 79 +++ .../api/secret-providers/default/+server.ts | 77 +++ .../api/stacks/[name]/validate/+server.ts | 14 +- src/routes/settings/secrets/SecretsTab.svelte | 73 ++ src/routes/stacks/StackModal.svelte | 50 +- tests/default-secret-provider.test.ts | 66 ++ tests/env-file-compose-corpus.test.ts | 36 + tests/fixtures/env-file-compose-corpus.json | 641 ++++++++++++++++++ tests/validate-provider-keys.test.ts | 119 ++++ tests/x509-display-core.test.ts | 124 ++++ 18 files changed, 1421 insertions(+), 12 deletions(-) create mode 100644 src/lib/server/provider-kind-core.ts create mode 100644 src/lib/server/x509-display-core.ts create mode 100644 src/lib/utils/default-secret-provider.ts create mode 100644 src/lib/utils/validate-provider-keys.ts create mode 100644 src/routes/api/secret-providers/default/+server.ts create mode 100644 tests/default-secret-provider.test.ts create mode 100644 tests/env-file-compose-corpus.test.ts create mode 100644 tests/fixtures/env-file-compose-corpus.json create mode 100644 tests/validate-provider-keys.test.ts create mode 100644 tests/x509-display-core.test.ts diff --git a/src/lib/components/app-sidebar.svelte b/src/lib/components/app-sidebar.svelte index 3f494b59b..f016b22ce 100644 --- a/src/lib/components/app-sidebar.svelte +++ b/src/lib/components/app-sidebar.svelte @@ -81,8 +81,10 @@ sidebar.setOpenMobile(false); // Per-user layout must not leak to the next user on this browser sidebarPreferencesStore.clearLocal(); - await authStore.logout(); - goto('/login'); + // The store navigates away itself when the provider has a session to end, and + // that destination is already this same login page. + const { logoutUrl, next } = await authStore.logout(); + if (!logoutUrl) goto(next); } /** diff --git a/src/lib/server/compose-validate/effective-compose.ts b/src/lib/server/compose-validate/effective-compose.ts index de79a68ee..073089ddd 100644 --- a/src/lib/server/compose-validate/effective-compose.ts +++ b/src/lib/server/compose-validate/effective-compose.ts @@ -26,6 +26,7 @@ import { resolveComposeDockerHost, buildComposeBaseArgs } from '../compose-docke import { parseCompose } from './parse'; import { extractMaterializableEnvFiles } from './env-file-paths'; import type { Finding } from './types'; +import { isPlaceholderArtefact, PLACEHOLDER } from '$lib/utils/validate-provider-keys'; const CONFIG_TIMEOUT_MS = 20_000; const OUTPUT_LIMIT = 8 * 1024 * 1024; @@ -128,6 +129,9 @@ export function parseConfigErrors(stderr: string): Finding[] { .replace(/"$/, '') .trim(); if (!msg || seen.has(msg)) continue; + // A complaint about the stand-in value is an artefact of previewing a secret we + // do not have, not a fault in the file. + if (isPlaceholderArtefact(msg)) continue; seen.add(msg); const lineMatch = /line\s+(\d+)/i.exec(msg); findings.push({ @@ -138,6 +142,10 @@ export function parseConfigErrors(stderr: string): Finding[] { line: lineMatch ? Number(lineMatch[1]) : undefined }); } + // Every message was a placeholder artefact: config only tripped over the stand-in, + // so there is nothing to report. + if (findings.length === 0 && stderr.includes(PLACEHOLDER)) return []; + // If config failed but printed nothing useful, still record one error. if (findings.length === 0) { findings.push({ diff --git a/src/lib/server/features.ts b/src/lib/server/features.ts index bfa84110a..597a9b230 100644 --- a/src/lib/server/features.ts +++ b/src/lib/server/features.ts @@ -28,3 +28,15 @@ export const API_DOCS_ENABLED = process.env.FEAT_API_DOCS === 'true'; * boot. Default false: a secret is required. */ export const ALLOW_WEBHOOKS_WITHOUT_SECRET = process.env.ALLOW_WEBHOOKS_WITHOUT_SECRET === 'true'; + +/** + * Whether logging out also ends the session at the OIDC provider. + * + * On by default, because leaving the provider's session open means the next visit + * signs straight back in without being asked for anything. Set + * OIDC_END_SESSION=false where a provider's end-session endpoint misbehaves - for + * instance one that rejects the return address and shows its own error page. The + * local session is destroyed either way, so turning this off never leaves anybody + * signed in to Dockhand. Read once at boot. + */ +export const OIDC_END_SESSION = process.env.OIDC_END_SESSION !== 'false'; diff --git a/src/lib/server/git.ts b/src/lib/server/git.ts index d8b90710e..0b89fb8ab 100644 --- a/src/lib/server/git.ts +++ b/src/lib/server/git.ts @@ -28,6 +28,7 @@ import { assertSafeRepoUrl, assertSafeGitRef, repoFilePath, repoBaseEnvPath } fr import { assertSafeRepoTarget } from './git-branch-lookup'; import { shouldDeployGitStack, shouldForceRecreateGitStack } from './git-deploy-policy'; import { resolveStackBranch } from '../git-stack-branch'; +import { expandValue } from '$lib/utils/env-file-values'; import { parseManifest, serializeManifest, @@ -2131,7 +2132,16 @@ export async function listGitStackEnvFiles(stackId: number): Promise<{ files: st * Parse a .env file content into key-value pairs. * Handles comments, empty lines, and quoted values. */ -export function parseEnvFileContent(content: string, stackName?: string): Record { +export function parseEnvFileContent( + content: string, + stackName?: string, + options?: { expand?: boolean } +): Record { + // Expanding is for a DEPLOY, which needs the values a service will see. A caller + // showing the file to somebody wants their own text back, references and all - + // resolving it there would turn ${DIRECTORY} into a path in front of them, and an + // edit saved from that view would flatten the variable for good. + const expand = options?.expand !== false; const logPrefix = stackName ? `[Stack:${stackName}]` : '[Git]'; const result: Record = {}; const skippedLines: string[] = []; @@ -2168,7 +2178,9 @@ export function parseEnvFileContent(content: string, stackName?: string): Record // Only add if key is valid env var name if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) { - result[key] = value; + // Quotes are the file's syntax, and a value may refer to an earlier line - + // both of which compose resolves before a service ever sees the value. + result[key] = expand ? expandValue(value, result) : value; } else { invalidKeys.push(`Line ${i + 1}: "${key}" (invalid key format)`); } @@ -2218,7 +2230,9 @@ export async function readGitStackEnvFile( try { const content = readFileSync(fullPath, 'utf-8'); - const vars = parseEnvFileContent(content); + // A view of the user's own file: their text, not what a deploy would resolve it + // to. This is also the diff base the editor decides overrides against. + const vars = parseEnvFileContent(content, undefined, { expand: false }); return { vars }; } catch (error: any) { return { vars: {}, error: error.message }; @@ -2324,7 +2338,7 @@ export async function previewRepoEnvFiles(options: PreviewEnvOptions): Promise

` session, or null for anything else. */ +export function oidcProviderName(provider: string | null | undefined): string | null { + if (!provider || !provider.startsWith('oidc:')) return null; + const name = provider.slice('oidc:'.length); + return name.length > 0 ? name : null; +} diff --git a/src/lib/server/stacks.ts b/src/lib/server/stacks.ts index 43591aff5..c33c53168 100644 --- a/src/lib/server/stacks.ts +++ b/src/lib/server/stacks.ts @@ -57,6 +57,7 @@ import { sendEventNotification } from './notifications'; import { deleteGitStackFiles, parseEnvFileContent } from './git'; import { isDeletableStackDir } from './stack-delete-guard'; import { cleanPem } from '$lib/utils/pem'; +import { quoteForEnvFile } from '$lib/utils/env-file-values'; import { rewriteComposeVolumePaths, getHostDataDir } from './host-path'; import { getOrderValue } from './container-labels'; import { pendingRowsToClear } from './pending-updates-core'; @@ -1666,8 +1667,11 @@ async function executeComposeViaHawser( console.log(`${logPrefix} Preserving existing .env from stackFiles (${files['.env'].length} chars), envVars passed separately for substitution`); } else { // No .env in stackFiles - generate one from NON-SECRET envVars only + // Compose interpolates this file when it reads it. Quoting the whole value + // carries it through intact - unquoted, a hash after a space becomes a + // comment, surrounding spaces are trimmed and a dollar is eaten. const envContent = Object.entries(envVars) - .map(([key, value]) => `${key}=${value}`) + .map(([key, value]) => `${key}=${quoteForEnvFile(value)}`) .join('\n'); files['.env'] = envContent; console.log(`${logPrefix} Generated .env file with ${Object.keys(envVars).length} non-secret variables`); diff --git a/src/lib/server/x509-display-core.ts b/src/lib/server/x509-display-core.ts new file mode 100644 index 000000000..6abf966a3 --- /dev/null +++ b/src/lib/server/x509-display-core.ts @@ -0,0 +1,40 @@ +/** + * Display helpers for the certificate metadata logged at HTTPS startup. + * + * Node leaves `subject` and `issuer` undefined when the certificate carries an + * empty distinguished name, which newer Let's Encrypt profiles produce - they + * identify the host through the SAN alone. Kept in sync with the inline copy in + * `server.js`, which runs against ./build and cannot import from src. + */ + +/** One-line form of an X.509 name, or a placeholder when the cert omits it. */ +export function formatCertName(name: string | undefined | null): string { + if (typeof name !== 'string') return '(none)'; + // Trim FIRST: a name that is only newlines would otherwise collapse to a + // string of separators rather than reading as absent. + const trimmed = name.trim(); + return trimmed === '' ? '(none)' : trimmed.replace(/\n/g, ', '); +} + +/** + * Whole-day countdown to expiry. Returns null when the date is unparseable, so + * the caller can stay quiet rather than reporting a NaN-day warning. + */ +export function daysUntilExpiry(validTo: string | undefined, now: number): number | null { + if (typeof validTo !== 'string') return null; + const expiresAt = new Date(validTo).getTime(); + if (Number.isNaN(expiresAt)) return null; + return Math.floor((expiresAt - now) / 86400000); +} + +/** The expiry line to log, and whether it deserves the operator's attention. */ +export function expiryLine(daysLeft: number | null): { text: string; warn: boolean } { + if (daysLeft === null) return { text: 'cert expiry: (unknown)', warn: false }; + if (daysLeft < 0) { + return { text: `WARNING: certificate expired ${-daysLeft} day(s) ago`, warn: true }; + } + if (daysLeft < 30) { + return { text: `WARNING: certificate expires in ${daysLeft} day(s)`, warn: true }; + } + return { text: `cert expires in ${daysLeft} day(s)`, warn: false }; +} diff --git a/src/lib/utils/default-secret-provider.ts b/src/lib/utils/default-secret-provider.ts new file mode 100644 index 000000000..c42213e45 --- /dev/null +++ b/src/lib/utils/default-secret-provider.ts @@ -0,0 +1,35 @@ +/** + * The global default secret provider, stored as a settings row rather than a + * column so no schema change is needed. + * + * The stored id is only a pointer: a provider can be deleted (or the row edited + * by hand) while the setting still names it, so a default is resolved against + * the live provider list and ignored when it no longer matches one. + */ +export const DEFAULT_SECRET_PROVIDER_SETTING = 'default_secret_provider_id'; + +/** Coerce a stored settings value to a provider id, or null if it names none. */ +export function parseDefaultProviderId(raw: unknown): number | null { + if (typeof raw === 'number') return Number.isSafeInteger(raw) && raw > 0 ? raw : null; + if (typeof raw === 'string') { + const trimmed = raw.trim(); + if (!trimmed) return null; + const parsed = Number(trimmed); + return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : null; + } + return null; +} + +/** + * The default to preselect, given the stored setting and the providers that + * actually exist. Returns null when nothing is configured or the stored id is + * stale, so a deleted provider silently falls back to no selection. + */ +export function resolveDefaultProviderId( + raw: unknown, + providers: Array<{ id: number }> +): number | null { + const id = parseDefaultProviderId(raw); + if (id === null) return null; + return providers.some((p) => p.id === id) ? id : null; +} diff --git a/src/lib/utils/validate-provider-keys.ts b/src/lib/utils/validate-provider-keys.ts new file mode 100644 index 000000000..eff2dd4a6 --- /dev/null +++ b/src/lib/utils/validate-provider-keys.ts @@ -0,0 +1,79 @@ +/** + * Treat keys a bound secret provider supplies as set when validating a compose file. + * + * `docker compose config` only asks whether a variable HAS a value, so a placeholder is + * enough to stop it reporting `${VAR:?...}` as missing. Without this, Validate disagrees + * with the deploy it is meant to preview: the deploy resolves the provider's bulk pull, + * Validate does not, and every provider-supplied required variable comes back as an + * error even though the stack deploys fine (#1621). + * + * Only key NAMES travel - the same names that already drive the editor's IN VAULT + * markers - so no secret value reaches the browser or the validation command. + */ + +/** Stand-in for a value only the provider knows. Never written anywhere. */ +export const PLACEHOLDER = 'dockhand-provider-value'; + +/** + * Whether a `docker compose config` error is about the placeholder rather than the + * file. + * + * The placeholder satisfies `${VAR:?}`, but compose type-checks some fields, and no + * single value fits them all: a port needs a number, `init:` needs a boolean. The real + * value is a secret this preview never sees, so a complaint naming the placeholder says + * nothing about whether the stack deploys - the deploy resolves the provider first. + */ +export function isPlaceholderArtefact(message: string): boolean { + return message.includes(PLACEHOLDER); +} + +/** + * Names that steer the validation subprocess rather than the compose file. They reach it + * as process environment, so a placeholder under one of these would break the run (a + * clobbered PATH loses the docker binary) instead of standing in for a value. Compose + * reads its own COMPOSE_* settings from the environment too. + */ +const RESERVED_KEYS = new Set([ + 'PATH', + 'HOME', + 'IFS', + 'LD_PRELOAD', + 'LD_LIBRARY_PATH', + 'DYLD_INSERT_LIBRARIES', + 'NODE_OPTIONS', + 'DOCKER_HOST', + 'DOCKER_CONFIG', + 'DOCKER_CERT_PATH', + 'DOCKER_TLS_VERIFY', + 'DOCKER_CONTEXT' +]); + +/** A provider key name safe to stand in for, as a shell variable the compose file reads. */ +function isUsableKey(key: unknown): key is string { + if (typeof key !== 'string' || !/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) return false; + return !RESERVED_KEYS.has(key) && !key.startsWith('COMPOSE_'); +} + +/** + * `envVars` with a placeholder added for every provider key that has no value yet. + * + * A real value always wins: an empty string is a deliberate setting, not an absence, so + * only keys absent from `envVars` are filled. The input is left untouched. + */ +export function withProviderKeysAsSet( + envVars: Record, + providerKeys: readonly string[] | null | undefined +): Record { + if (!providerKeys?.length) return { ...envVars }; + const out = { ...envVars }; + for (const key of providerKeys) { + if (isUsableKey(key) && !(key in out)) out[key] = PLACEHOLDER; + } + return out; +} + +/** Sanitize a provider-key list arriving over the wire. */ +export function sanitizeProviderKeys(value: unknown): string[] { + if (!Array.isArray(value)) return []; + return [...new Set(value.filter(isUsableKey))]; +} diff --git a/src/routes/api/secret-providers/default/+server.ts b/src/routes/api/secret-providers/default/+server.ts new file mode 100644 index 000000000..eba14acc6 --- /dev/null +++ b/src/routes/api/secret-providers/default/+server.ts @@ -0,0 +1,77 @@ +import { json } from '@sveltejs/kit'; +import type { RequestHandler } from './$types'; +import { getSetting, setSetting, deleteSetting, getSecretProviders } from '$lib/server/db'; +import { authorize } from '$lib/server/authorize'; +import { + DEFAULT_SECRET_PROVIDER_SETTING, + parseDefaultProviderId, + resolveDefaultProviderId +} from '$lib/utils/default-secret-provider'; + +/** + * @openapi + * summary: Get the global default secret provider preselected for new stacks + * description: Returns null when no default is set, or when the stored provider no longer exists. + * resp-200: {providerId:integer} + * resp-403: Permission denied (needs secrets:view) + * resp-500: Failed to read the default secret provider + */ +export const GET: RequestHandler = async ({ cookies }) => { + const auth = await authorize(cookies); + if (auth.authEnabled && !(await auth.can('secrets', 'view'))) { + return json({ error: 'Permission denied' }, { status: 403 }); + } + + try { + const [raw, providers] = await Promise.all([ + getSetting(DEFAULT_SECRET_PROVIDER_SETTING), + getSecretProviders() + ]); + return json({ providerId: resolveDefaultProviderId(raw, providers) }); + } catch (error) { + console.error('Error reading default secret provider:', error); + return json({ error: 'Failed to read the default secret provider' }, { status: 500 }); + } +}; + +/** + * @openapi + * summary: Set or clear the global default secret provider + * description: Pass a provider id to make it the default, or null to clear it. + * body: {providerId:integer} + * resp-200: {providerId:integer} + * resp-400: Invalid provider id, or the provider does not exist + * resp-403: Permission denied (needs secrets:edit) + * resp-500: Failed to save the default secret provider + */ +export const PUT: RequestHandler = async ({ request, cookies }) => { + const auth = await authorize(cookies); + if (auth.authEnabled && !(await auth.can('secrets', 'edit'))) { + return json({ error: 'Permission denied' }, { status: 403 }); + } + + try { + const body = await request.json(); + + if (body?.providerId === null || body?.providerId === undefined) { + await deleteSetting(DEFAULT_SECRET_PROVIDER_SETTING); + return json({ providerId: null }); + } + + const providerId = parseDefaultProviderId(body.providerId); + if (providerId === null) { + return json({ error: 'Invalid provider id' }, { status: 400 }); + } + + const providers = await getSecretProviders(); + if (!providers.some((p) => p.id === providerId)) { + return json({ error: 'Secret provider not found' }, { status: 400 }); + } + + await setSetting(DEFAULT_SECRET_PROVIDER_SETTING, providerId); + return json({ providerId }); + } catch (error) { + console.error('Error saving default secret provider:', error); + return json({ error: 'Failed to save the default secret provider' }, { status: 500 }); + } +}; diff --git a/src/routes/api/stacks/[name]/validate/+server.ts b/src/routes/api/stacks/[name]/validate/+server.ts index a9e6e284e..a46ae373d 100644 --- a/src/routes/api/stacks/[name]/validate/+server.ts +++ b/src/routes/api/stacks/[name]/validate/+server.ts @@ -4,6 +4,7 @@ import { runValidateWithConfig } from '$lib/server/compose-validate'; import { buildValidateContext } from '$lib/server/compose-validate/context'; import { getEnvironment } from '$lib/server/db'; import type { ValidateConfig } from '$lib/server/compose-validate/types'; +import { withProviderKeysAsSet, sanitizeProviderKeys } from '$lib/utils/validate-provider-keys'; import type { RequestHandler } from './$types'; /** @@ -38,7 +39,7 @@ async function resolveDockerHostForConfig(envIdNum: number | undefined): Promise * description: Set existing:true when validating an already-deployed stack, so the stack's own running containers/ports are excluded from the cross-stack collision checks; omit it (or false) for a brand-new stack, where a name/port clash with a running stack of the same name must still be reported. * path: name:string! Stack name (used to exclude the stack's own containers from cross-stack collision checks) * query: env:integer Environment ID for the context-aware checks (from GET /api/environments) - * body: {compose:string!, existing:boolean, config:{disabled:[string], severity:object}, envVars:object} + * body: {compose:string!, existing:boolean, config:{disabled:[string], severity:object}, envVars:object, providerKeys:array} * body-example: {"compose":"services:\n web:\n image: nginx:latest\n ports: [\"8080:80\"]\n"} * resp-200: {findings:[{ruleId:string!, severity:string!, message:string!, hint:string, service:string, line:integer, fix:object, fixDescription:string}]!, counts:{error:integer!, warn:integer!, info:integer!}!} * resp-200-example: {"findings":[{"ruleId":"LATEST_TAG","severity":"warn","message":"\"web\" uses `:latest` (nginx:latest)","hint":"Pin a version tag for reproducible deploys and to enable newer-version detection.","service":"web","line":3}],"counts":{"error":0,"warn":1,"info":0}} @@ -60,6 +61,7 @@ export const POST: RequestHandler = async ({ cookies, url, params, request }) => compose?: string; config?: ValidateConfig; envVars?: Record; + providerKeys?: string[]; existing?: boolean; }; const compose = typeof body.compose === 'string' ? body.compose : ''; @@ -69,13 +71,19 @@ export const POST: RequestHandler = async ({ cookies, url, params, request }) => // The editor's current env vars (incl. secrets) so `docker compose config` resolves // `${VAR}` the same way a deploy will, instead of reporting a spurious "VAR not set". - const envVars: Record = {}; + const editorEnvVars: Record = {}; if (body.envVars && typeof body.envVars === 'object') { for (const [k, v] of Object.entries(body.envVars)) { - if (typeof k === 'string' && typeof v === 'string') envVars[k] = v; + if (typeof k === 'string' && typeof v === 'string') editorEnvVars[k] = v; } } + // A bound provider supplies more keys at deploy time. The editor sends the NAMES it + // probed (the ones behind its IN VAULT markers) and they are filled with a + // placeholder, since config only asks whether a variable is set - no secret value is + // needed, so none is requested or transmitted (#1621). + const envVars = withProviderKeysAsSet(editorEnvVars, sanitizeProviderKeys(body.providerKeys)); + const stackName = decodeURIComponent(params.name); // Self-exclusion (don't flag the stack's own running containers/ports as collisions) // applies ONLY when editing an existing stack. A brand-new stack has no own containers, diff --git a/src/routes/settings/secrets/SecretsTab.svelte b/src/routes/settings/secrets/SecretsTab.svelte index 08b1ba4a6..a76f267e4 100644 --- a/src/routes/settings/secrets/SecretsTab.svelte +++ b/src/routes/settings/secrets/SecretsTab.svelte @@ -13,6 +13,7 @@ PlugZap, RefreshCw, Check, + Star, } from 'lucide-svelte'; import { scale } from 'svelte/transition'; import { backOut, cubicIn } from 'svelte/easing'; @@ -39,6 +40,9 @@ // Brief green tick on the tile's Test button right after a successful test. let testOkId = $state(null); let testOkTimer: ReturnType | undefined; + // Preselected on new stacks. Null = none, so every stack picks its own (#1609). + let defaultProviderId = $state(null); + let savingDefaultId = $state(null); async function fetchProviders() { loading = true; @@ -53,6 +57,45 @@ } } + async function fetchDefaultProvider() { + try { + const response = await fetch('/api/secret-providers/default'); + if (!response.ok) return; + const data = await response.json(); + defaultProviderId = data.providerId ?? null; + } catch (e) { + console.warn('Failed to load the default secret provider:', e); + } + } + + // Star the provider, or un-star it to go back to no default. + async function toggleDefault(provider: SecretProvider) { + const next = defaultProviderId === provider.id ? null : provider.id; + savingDefaultId = provider.id; + try { + const response = await fetch('/api/secret-providers/default', { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ providerId: next }), + }); + const data = await response.json(); + if (!response.ok) { + toast.error(data.error || 'Failed to save the default secret provider'); + return; + } + defaultProviderId = data.providerId ?? null; + toast.success( + next === null + ? 'No default secret provider' + : `${provider.name} is now the default for new stacks`, + ); + } catch { + toast.error('Failed to save the default secret provider'); + } finally { + savingDefaultId = null; + } + } + function openModal(provider?: SecretProvider) { editing = provider || null; showModal = true; @@ -81,6 +124,9 @@ }); if (response.ok) { await fetchProviders(); + // Deleting the default leaves the stored id dangling; the API resolves + // it against the live list, so re-read rather than assuming. + if (defaultProviderId === id) await fetchDefaultProvider(); toast.success('Secret provider deleted'); } else { const data = await response.json(); @@ -118,6 +164,7 @@ onMount(() => { fetchProviders(); + fetchDefaultProvider(); }); @@ -162,6 +209,15 @@ {provider.name} + {#if defaultProviderId === provider.id} + + + Default + + {/if}

{providerTypeLabel(provider.type)} {/if} {#if $canAccess("secrets", "edit")} + + {:else} - + {#if !duplicating} + + {/if}