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/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/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/components/ScanTab.svelte b/src/lib/components/ScanTab.svelte index e20c99adb..c6df75899 100644 --- a/src/lib/components/ScanTab.svelte +++ b/src/lib/components/ScanTab.svelte @@ -6,6 +6,7 @@ import { onMount } from 'svelte'; import { appendEnvParam } from '$lib/stores/environment'; import { watchJob } from '$lib/utils/sse-fetch'; + import { scanProgressLines } from '$lib/utils/scan-progress-lines'; import ScanResultsView from '../../routes/images/ScanResultsView.svelte'; export interface ScanResult { @@ -176,13 +177,10 @@ function handleScanProgress(data: any) { if (data.message) { scanMessage = data.message; - const scanner = data.scanner || 'dockhand'; - addOutputLine(`[${scanner}] ${data.message}`); } - if (data.output) { - const scanner = data.scanner || 'dockhand'; - addOutputLine(`[${scanner}] ${data.output}`); + for (const line of scanProgressLines(data)) { + addOutputLine(line); } if (data.stage === 'complete' || data.status === 'complete') { 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/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/components/data-grid/grouping-core.ts b/src/lib/components/data-grid/grouping-core.ts index 4564dae8d..4719e68bc 100644 --- a/src/lib/components/data-grid/grouping-core.ts +++ b/src/lib/components/data-grid/grouping-core.ts @@ -14,8 +14,9 @@ export interface GroupDescriptor { /** Optional icon names shown in the header (e.g. one per tag in the combo). */ icons?: (string | null)[]; /** - * Sort weight for ordering groups against each other. Lower comes first. - * Ungrouped rows use +Infinity so they always trail. Defaults to 0. + * Sort weight for ordering groups against each other. Lower comes first; equal + * weights fall back to label order. Ungrouped rows use +Infinity so they always + * trail. Defaults to 0. */ order?: number; } @@ -34,16 +35,15 @@ export const UNGROUPED_KEY = '__ungrouped__'; /** * Partition `data` into ordered groups. Row order WITHIN a group is preserved * (the caller pre-sorts `data`), so grouping composes with column sorting. - * Group order: by `order` weight, then first-appearance, so it is stable and - * independent of object identity. The ungrouped bucket always trails. + * Group order: by `order` weight, then label (case-insensitive), then key, so it + * does not depend on row order. The ungrouped bucket always trails. */ export function groupData( data: T[], groupBy: (item: T) => GroupDescriptor | null, ungroupedLabel = 'Untagged' ): DataGridGroup[] { - const groups = new Map & { order: number; seq: number }>(); - let seq = 0; + const groups = new Map & { order: number }>(); for (const item of data) { const desc = groupBy(item); @@ -56,8 +56,7 @@ export function groupData( color: desc?.color ?? null, icons: desc?.icons ?? [], items: [], - order: desc ? (desc.order ?? 0) : Number.POSITIVE_INFINITY, - seq: seq++ + order: desc ? (desc.order ?? 0) : Number.POSITIVE_INFINITY }; groups.set(key, g); } @@ -65,6 +64,11 @@ export function groupData( } return [...groups.values()] - .sort((a, b) => a.order - b.order || a.seq - b.seq) - .map(({ order: _order, seq: _seq, ...g }) => g); + .sort( + (a, b) => + a.order - b.order || + a.label.localeCompare(b.label, undefined, { sensitivity: 'base' }) || + (a.key < b.key ? -1 : a.key > b.key ? 1 : 0) + ) + .map(({ order: _order, ...g }) => g); } diff --git a/src/lib/data/changelog.json b/src/lib/data/changelog.json index 5b4e3f35d..b4affdacf 100644 --- a/src/lib/data/changelog.json +++ b/src/lib/data/changelog.json @@ -1,4 +1,35 @@ [ + { + "version": "1.0.50", + "date": "2026-09-30", + "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", diff --git a/src/lib/openapi.generated.json b/src/lib/openapi.generated.json index 3c9f41e00..0368d2098 100644 --- a/src/lib/openapi.generated.json +++ b/src/lib/openapi.generated.json @@ -1605,6 +1605,9 @@ "properties": { "success": { "type": "boolean" + }, + "logoutUrl": { + "type": "string" } }, "required": [ @@ -1621,7 +1624,8 @@ "description": "Unexpected error while destroying the session" } }, - "security": [] + "security": [], + "description": "When the session came from an OIDC provider that publishes an end-session endpoint, the response carries a logoutUrl the caller should navigate to so the provider ends its own session too. Never present when the instance sets OIDC_END_SESSION=false." } }, "/api/auth/oidc": { @@ -2132,16 +2136,10 @@ "description": "Successful response" }, "302": { - "description": "Redirect to the IdP's authorization URL" + "description": "Redirect to the IdP's authorization URL, or back to /login?local=1&error=... when the provider is unknown, disabled or unreachable" }, "400": { "description": "Authentication is not enabled, or the configuration id is invalid" - }, - "404": { - "description": "OIDC provider not found or disabled" - }, - "500": { - "description": "Failed to build the authorization URL / initiate SSO" } }, "security": [] @@ -2317,7 +2315,7 @@ "parameters": [], "responses": { "200": { - "description": "Successful response", + "description": "autoLoginUrl is present only when OIDC_AUTOLOGIN is set and exactly one OIDC provider is enabled; the login page then goes straight there", "content": { "application/json": { "schema": { @@ -2350,6 +2348,9 @@ }, "defaultProvider": { "type": "string" + }, + "autoLoginUrl": { + "type": "string" } } }, @@ -2844,6 +2845,9 @@ } } }, + "403": { + "description": "Permission denied (needs schedules:view, and access to the environment)" + }, "500": { "description": "Failed to get auto-update settings" } @@ -2921,6 +2925,9 @@ } } }, + "403": { + "description": "Permission denied, or no access to the environment" + }, "500": { "description": "Failed to get auto-update setting" } @@ -3004,6 +3011,9 @@ } } }, + "403": { + "description": "Permission denied, or no access to the environment" + }, "500": { "description": "Failed to save auto-update setting" } @@ -3097,6 +3107,9 @@ } } }, + "403": { + "description": "Permission denied, or no access to the environment" + }, "500": { "description": "Failed to delete auto-update setting" } @@ -10720,9 +10733,19 @@ ] } } + }, + "401": { + "description": "Not authenticated" } }, - "security": [] + "security": [ + { + "cookieAuth": [] + }, + { + "bearerAuth": [] + } + ] } }, "/api/docs": { @@ -11314,6 +11337,12 @@ }, "thresholdGb": { "type": "integer" + }, + "percentageSupported": { + "type": "boolean" + }, + "storageDriver": { + "type": "string" } }, "required": [ @@ -11327,7 +11356,9 @@ "enabled": true, "mode": "percentage", "threshold": 80, - "thresholdGb": 50 + "thresholdGb": 50, + "percentageSupported": false, + "storageDriver": "overlay2" } } } @@ -11349,7 +11380,8 @@ { "bearerAuth": [] } - ] + ], + "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." }, "post": { "operationId": "post_api_environments_id_disk-warning", @@ -12975,6 +13007,9 @@ } } } + }, + "403": { + "description": "Permission denied (needs environments:create or environments:edit)" } }, "security": [ @@ -13144,6 +13179,9 @@ "responses": { "200": { "description": "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)" + }, + "403": { + "description": "Permission denied, or no access to the environment" } }, "security": [ @@ -15631,6 +15669,12 @@ } } } + }, + "401": { + "description": "Not authenticated" + }, + "403": { + "description": "Permission denied (missing environments:view)" } }, "security": [ @@ -15640,7 +15684,8 @@ { "bearerAuth": [] } - ] + ], + "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." }, "post": { "operationId": "post_api_hawser_connect", @@ -16455,7 +16500,7 @@ "schema": { "type": "string" }, - "description": "Image ID or name to export (from GET /api/images)" + "description": "Image ID to export (from GET /api/images); a name containing a slash cannot be used here, since it would split the route" }, { "name": "env", @@ -16466,6 +16511,15 @@ }, "description": "ID of the environment the image belongs to (from GET /api/environments)" }, + { + "name": "tag", + "in": "query", + "required": false, + "schema": { + "type": "string" + }, + "description": "Which of the image's tags to name the download after (defaults to its first tag)" + }, { "name": "compress", "in": "query", @@ -17573,7 +17627,8 @@ "description": "Failed to read the license status" } }, - "security": [] + "security": [], + "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." }, "post": { "operationId": "post_api_license", @@ -18184,7 +18239,7 @@ "description": "Container ID is required" }, "403": { - "description": "Permission denied" + "description": "Permission denied (needs networks:connect; the Dockhand container's own networks are admin-only)" }, "500": { "description": "Failed to connect container to network" @@ -18279,7 +18334,7 @@ "description": "Container ID is required" }, "403": { - "description": "Permission denied" + "description": "Permission denied (needs networks:disconnect; the Dockhand container's own networks are admin-only)" }, "500": { "description": "Failed to disconnect container from network" @@ -19426,6 +19481,12 @@ "400": { "description": "eventType required, payload with title and message required, unknown event type, or environmentId missing for a non-system event" }, + "401": { + "description": "Not authenticated" + }, + "403": { + "description": "Permission denied (missing notifications:test, or no access to the environment)" + }, "500": { "description": "Unknown error while sending the notification" } @@ -19438,7 +19499,7 @@ "bearerAuth": [] } ], - "description": "environmentId from GET /api/environments.", + "description": "Delivers to the configured channels for real, so it requires notifications:test and access to the environment. environmentId from GET /api/environments.", "requestBody": { "required": true, "content": { @@ -22086,7 +22147,7 @@ "tags": [ "roles" ], - "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", "parameters": [], "responses": { "200": { @@ -22135,7 +22196,7 @@ } }, "403": { - "description": "Enterprise license required" + "description": "Enterprise license required, or the account may not view users" }, "500": { "description": "Failed to read the roles" @@ -22237,7 +22298,7 @@ "tags": [ "roles" ], - "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", "parameters": [ { "name": "id", @@ -22294,7 +22355,7 @@ "description": "Role id is required" }, "403": { - "description": "Enterprise license required" + "description": "Enterprise license required, or the account may not view users" }, "404": { "description": "Role not found" @@ -22539,6 +22600,9 @@ } } }, + "403": { + "description": "Permission denied (missing schedules:view)" + }, "500": { "description": "Unexpected error while assembling the schedule list" } @@ -22605,6 +22669,9 @@ "400": { "description": "Invalid schedule id, invalid/unsupported type, or system_cleanup (cannot be removed)" }, + "403": { + "description": "Permission denied" + }, "404": { "description": "Schedule (or backup destination, for repo_* types) not found" }, @@ -22679,6 +22746,9 @@ "400": { "description": "Invalid schedule id/type, or the triggered task itself reported failure" }, + "403": { + "description": "Permission denied" + }, "404": { "description": "Schedule, backup config, or backup destination not found (or backup feature disabled)" }, @@ -22753,6 +22823,9 @@ "400": { "description": "Invalid schedule id, unsupported type, or system_cleanup/deploy_log_reconcile (cannot be paused)" }, + "403": { + "description": "Permission denied" + }, "404": { "description": "Schedule, backup config, or backup destination not found" }, @@ -23913,6 +23986,115 @@ } } }, + "/api/secret-providers/default": { + "get": { + "operationId": "get_api_secret-providers_default", + "tags": [ + "secret-providers" + ], + "summary": "Get the global default secret provider preselected for new stacks", + "parameters": [], + "responses": { + "200": { + "description": "Successful response", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "providerId": { + "type": "integer" + } + } + }, + "example": { + "providerId": 0 + } + } + } + }, + "403": { + "description": "Permission denied (needs secrets:view)" + }, + "500": { + "description": "Failed to read the default secret provider" + } + }, + "security": [ + { + "cookieAuth": [] + }, + { + "bearerAuth": [] + } + ], + "description": "Returns null when no default is set, or when the stored provider no longer exists." + }, + "put": { + "operationId": "put_api_secret-providers_default", + "tags": [ + "secret-providers" + ], + "summary": "Set or clear the global default secret provider", + "parameters": [], + "responses": { + "200": { + "description": "Successful response", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "providerId": { + "type": "integer" + } + } + }, + "example": { + "providerId": 0 + } + } + } + }, + "400": { + "description": "Invalid provider id, or the provider does not exist" + }, + "403": { + "description": "Permission denied (needs secrets:edit)" + }, + "500": { + "description": "Failed to save the default secret provider" + } + }, + "security": [ + { + "cookieAuth": [] + }, + { + "bearerAuth": [] + } + ], + "description": "Pass a provider id to make it the default, or null to clear it.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "providerId": { + "type": "integer" + } + } + }, + "example": { + "providerId": 0 + } + } + } + } + } + }, "/api/secret-providers/test": { "post": { "operationId": "post_api_secret-providers_test", @@ -24223,7 +24405,8 @@ { "bearerAuth": [] } - ] + ], + "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." }, "post": { "operationId": "post_api_settings_general", @@ -25023,6 +25206,9 @@ } } } + }, + "403": { + "description": "Permission denied (needs settings:view)" } }, "security": [ @@ -27654,6 +27840,12 @@ "envVars": { "type": "object", "properties": {} + }, + "providerKeys": { + "type": "array", + "items": { + "type": "string" + } } }, "required": [ @@ -27829,6 +28021,9 @@ } } } + }, + "403": { + "description": "Permission denied (needs stacks:view, and access to the environment)" } }, "security": [ @@ -27916,6 +28111,9 @@ }, "400": { "description": "Stack name is required" + }, + "403": { + "description": "Permission denied (needs stacks:view, and access to the environment)" } }, "security": [ @@ -29444,7 +29642,7 @@ "tags": [ "users" ], - "summary": "List all local/SSO users (any authenticated user may view — only mutations are RBAC-gated)", + "summary": "List all local/SSO users", "parameters": [], "responses": { "200": { @@ -29509,6 +29707,9 @@ "401": { "description": "Not authenticated" }, + "403": { + "description": "Permission denied (missing users:view)" + }, "500": { "description": "Unexpected error while loading users" } @@ -29520,7 +29721,8 @@ { "bearerAuth": [] } - ] + ], + "description": "Requires users:view. The list carries every account's email address and says which ones are administrators." }, "post": { "operationId": "post_api_users", @@ -29713,6 +29915,9 @@ "401": { "description": "Authentication required (auth is enabled and the caller is not authenticated)" }, + "403": { + "description": "Permission denied (missing users:view, reading another user's record)" + }, "404": { "description": "User not found" }, @@ -29727,7 +29932,8 @@ { "bearerAuth": [] } - ] + ], + "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." }, "put": { "operationId": "put_api_users_id", @@ -30129,8 +30335,11 @@ "400": { "description": "User id is required" }, + "401": { + "description": "Not authenticated" + }, "403": { - "description": "Enterprise license required" + "description": "Enterprise license required, or permission denied (missing users:view)" }, "404": { "description": "User not found" @@ -30146,7 +30355,8 @@ { "bearerAuth": [] } - ] + ], + "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." }, "post": { "operationId": "post_api_users_id_roles", 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/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/backups/locks.ts b/src/lib/server/backups/locks.ts index 941c9227d..273778efc 100644 --- a/src/lib/server/backups/locks.ts +++ b/src/lib/server/backups/locks.ts @@ -19,6 +19,8 @@ * unit-tested directly; the module also holds the small in-memory state. */ +import { KeyedSerializer } from '../keyed-serializer'; + /** * Build the live-target lock key from the env and the set of data paths the * operation touches. Callers pass the docker bind strings they will use @@ -92,33 +94,12 @@ export class LiveTargetLocks { } /** - * Per-destination serialization: operations on the same destination id run one - * at a time, in arrival order. `run` chains onto the destination's current tail - * promise, so a second caller waits for the first to settle (success or failure) - * before starting. Cleared when a destination's chain drains. + * Per-destination serialization: operations on the same restic REPOSITORY run + * one at a time, in arrival order. The key is the repository rather than the + * destination id, because the restic lock is per-repo: two different destination + * rows pointing at the same repo must serialize together or they collide on that + * lock (a backup then waits out `--retry-lock`, ~5-10 min). + * + * The queueing itself is the shared KeyedSerializer. */ -export class DestinationSerializer { - // One tail promise per serialization KEY. The key is the restic REPOSITORY, - // not the destination id: the restic lock is per-repo, so two DIFFERENT - // destination rows that point at the SAME repo must serialize together or they - // collide on the repo lock (a backup then waits out `--retry-lock`, ~5-10 min). - // The map holds at most one entry per distinct repo, so we never prune it — - // simpler and obviously correct. The stored tail always resolves (never - // rejects) so one failed op can't reject an unrelated op that chains behind it. - private tails = new Map>(); - - async run(key: string | number, fn: () => Promise): Promise { - const k = String(key); - const prev = this.tails.get(k) ?? Promise.resolve(); - // Gate that opens when our turn is fully done, so the next caller waits. - let opened!: () => void; - const done = new Promise((r) => { opened = r; }); - this.tails.set(k, prev.then(() => done)); - await prev; // wait for our turn (prev never rejects) - try { - return await fn(); - } finally { - opened(); // release the next caller regardless of our outcome - } - } -} +export class DestinationSerializer extends KeyedSerializer {} diff --git a/src/lib/server/backups/request-timeout.ts b/src/lib/server/backups/request-timeout.ts index 705cdf187..b6e346480 100644 --- a/src/lib/server/backups/request-timeout.ts +++ b/src/lib/server/backups/request-timeout.ts @@ -19,10 +19,15 @@ export interface RequestTimeoutInput { composeTimeoutSecs?: number; } +/** Prune endpoints get the long timeout; the query string (image prune filters) is ignored. #1630 */ +export function isPrunePath(path: string): boolean { + return path.split('?')[0].endsWith('/prune'); +} + export function computeRequestTimeoutMs(input: RequestTimeoutInput): number | null { // No idle timeout when either end streams: a large body upload or a long-lived response. if (input.streamingBody || input.streamingResponse) return null; if (input.path === '/_hawser/compose') return (input.composeTimeoutSecs ?? 900) * 1000; - if (input.path.endsWith('/prune')) return 300000; + if (isPrunePath(input.path)) return 300000; return 30000; } 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/db.ts b/src/lib/server/db.ts index e269af93f..8da44bc83 100644 --- a/src/lib/server/db.ts +++ b/src/lib/server/db.ts @@ -3598,40 +3598,6 @@ export async function getCombinedScanForImage( return combined; } -export async function getAllLatestScans(environmentId?: number | null): Promise { - // This complex query requires raw SQL or multiple queries - // For simplicity, we'll fetch all and filter in JS - let results; - if (environmentId !== undefined) { - if (environmentId === null) { - results = await db.select().from(vulnerabilityScans) - .where(isNull(vulnerabilityScans.environmentId)) - .orderBy(desc(vulnerabilityScans.scannedAt)); - } else { - results = await db.select().from(vulnerabilityScans) - .where(eq(vulnerabilityScans.environmentId, environmentId)) - .orderBy(desc(vulnerabilityScans.scannedAt)); - } - } else { - results = await db.select().from(vulnerabilityScans) - .orderBy(desc(vulnerabilityScans.scannedAt)); - } - - // Group by imageId + scanner and take latest - const latestMap = new Map(); - for (const row of results) { - const key = `${row.imageId}:${row.scanner}`; - if (!latestMap.has(key)) { - latestMap.set(key, row); - } - } - - return Array.from(latestMap.values()).map(row => ({ - ...row, - vulnerabilities: row.vulnerabilities ? JSON.parse(row.vulnerabilities) : [] - })) as VulnerabilityScanData[]; -} - /** * Scan freshness for the metrics endpoint: how stale the OLDEST scan is * (surfaces environments whose scans have gone stale) and the average scan @@ -5896,6 +5862,24 @@ export async function removePendingContainerUpdate(environmentId: number, contai )); } +/** + * Clear a pending update by container NAME. + * + * An update recreates the container under a new id, so a caller that ran the update + * no longer holds the id the row was written with. The name survives the recreate, + * which is what the auto-update schedule identifies a container by anyway. + */ +export async function removePendingContainerUpdateByName( + environmentId: number, + containerName: string +): Promise { + await db.delete(pendingContainerUpdates) + .where(and( + eq(pendingContainerUpdates.environmentId, environmentId), + eq(pendingContainerUpdates.containerName, containerName) + )); +} + // ============================================================================= // BACKUP DESTINATION OPERATIONS // ============================================================================= diff --git a/src/lib/server/docker.ts b/src/lib/server/docker.ts index c24f85c53..282739223 100644 --- a/src/lib/server/docker.ts +++ b/src/lib/server/docker.ts @@ -17,12 +17,17 @@ import { pumpWebStreamToWritable } from './stream-pump'; import { toWebReadableStream } from './node-readable-stream'; import { buildImagePruneFilters } from './image-prune-core'; import { demuxDockerStream } from './docker-demux-core'; -import { computeRequestTimeoutMs } from './backups/request-timeout'; +import { computeRequestTimeoutMs, isPrunePath } from './backups/request-timeout'; import { helperWaitDeadline, helperExitFromState } from './helper-wait-core'; +import { cancelReaderOnAbort } from './reader-abort-core'; +import { configuredMacAddress, endpointWithoutGeneratedMac } from './endpoint-mac-core'; +import { rescopeForPrimarySwitch } from './primary-network-switch-core'; +import { mapContainerNetworks } from '$lib/utils/network-identity'; import type { Environment } from './db'; import { getSetting } from './db'; import { getAdditionalVolumeBinds, dedupeVolumesForRecreate } from './mount-dedupe'; import { resolveNanoCpusConflict, resolvePodmanUsernsMode } from './hostconfig-recreate'; +import { isUnknownNetworkKeyError, retryEndpointKey } from './podman-network-key'; import { expectedEvents, EXPECTED_EVENT_TTL_MS } from './expected-events-core'; import { decideRespawnOutcome, isExactNameMatch } from './systemd-recreate-core'; // Import-light image parsing shared with the semver layer; re-exported below for callers. @@ -30,6 +35,7 @@ import { parseImageReference } from './registry/image-ref'; export { parseImageReference } from './registry/image-ref'; import { rebaseEnvOntoImage, rebaseLabelsOntoImage, rebaseCommand, describeEnvRebase, describeLabelRebase, type ImageEnvLabels } from './container-env-merge'; import { encodeRegistryAuth, fetchRegistryToken, isSafeRegistryHost } from './registry-auth'; +import { describeRegistryFailure } from './registry-failure-core'; import { resolveRegistryScheme, type StoredRegistryScheme } from './registry-scheme-core'; import { classifyManifest, type ArtifactKind } from './semver/manifest-artifact'; import { isSystemContainer, classifyEmptyDigestImage, localDigestIsIndexChild, indexChildDigests } from './scheduler/tasks/update-utils'; @@ -644,7 +650,9 @@ function buildResponse(body: Buffer | ReadableStream, status: number, statusText if (NULL_BODY_STATUSES.has(status)) { return new Response(null, { status, statusText, headers }); } - return new Response(body, { status, statusText, headers }); + // A Node Buffer and a Node stream are both valid runtime bodies; the DOM lib's + // BodyInit does not describe either, so the cast is the narrowest way to say so. + return new Response(body as BodyInit, { status, statusText, headers }); } /** @@ -856,7 +864,7 @@ export async function dockerFetch( body, headers, streaming || false, - (streaming || path === '/_hawser/compose' || path.endsWith('/prune')) ? 300000 : 30000, // 5 min for streaming/compose/prune, 30s for normal + (streaming || path === '/_hawser/compose' || isPrunePath(path)) ? 300000 : 30000, // 5 min for streaming/compose/prune, 30s for normal isBinary, fetchOptions.signal ?? undefined, onLine @@ -933,7 +941,7 @@ export async function dockerFetch( if (!streaming && !finalOptions.signal) { const isComposeOperation = path === '/_hawser/compose'; const composeTimeoutMs = parseInt(process.env.COMPOSE_TIMEOUT || '900') * 1000; - const isPrune = path.endsWith('/prune'); + const isPrune = isPrunePath(path); finalOptions.signal = AbortSignal.timeout(isComposeOperation ? composeTimeoutMs : isPrune ? 300000 : 30000); } @@ -1512,6 +1520,8 @@ export async function createContainer(options: CreateContainerOptions, envId?: n // Backward-compat: callers (and the auto-update inspect path) may still pass // the legacy "networks" array which conflated primary + extras. Normalize to // the new model: anything that isn't the primary becomes an additional network. + // Names are all this array carries, so a Podman alias of the primary cannot be + // recognised here; callers that know the ids send additionalNetworks instead. if (options.networks && !options.additionalNetworks) { const primary = options.networkMode || ''; options.additionalNetworks = options.networks.filter(n => n !== primary); @@ -1835,14 +1845,49 @@ export async function createContainer(options: CreateContainerOptions, envId?: n } } - const result = await dockerJsonRequest<{ Id: string }>( - `/containers/create?name=${encodeURIComponent(options.name)}`, - { - method: 'POST', - body: JSON.stringify(containerConfig) - }, - envId - ); + let result: { Id: string }; + try { + result = await dockerJsonRequest<{ Id: string }>( + `/containers/create?name=${encodeURIComponent(options.name)}`, + { + method: 'POST', + body: JSON.stringify(containerConfig) + }, + envId + ); + } catch (err: any) { + // Podman refuses an EndpointsConfig keyed by the network NAME even though + // it accepts that same name in NetworkMode and lists it under that name + // (#1619). Its id works on both engines, so retry by id - but only here, + // where the name has already proven unusable, since Docker echoes the id + // back as the inspect key and would leave ids in the UI. + const key = primaryMode && !isSharedMode ? primaryMode : ''; + const endpoints = containerConfig.NetworkingConfig?.EndpointsConfig; + if (!key || !endpoints?.[key] || !isUnknownNetworkKeyError(err?.message)) throw err; + + // The lookup is what tells the aliased bridge apart from a name that is + // simply wrong: it resolves for the former and 404s for the latter, where + // rethrowing keeps the daemon's own "network not found" for the user. + const net = await inspectNetwork(key, envId).catch(() => null); + const retryKey = retryEndpointKey({ + message: err?.message, + endpoints, + key, + networkId: (net as { Id?: string } | null)?.Id + }); + if (!retryKey) throw err; + + console.warn(`[create] "${key}" rejected as an endpoint key, retrying by id (Podman)`); + containerConfig.NetworkingConfig.EndpointsConfig = { [retryKey]: endpoints[key] }; + result = await dockerJsonRequest<{ Id: string }>( + `/containers/create?name=${encodeURIComponent(options.name)}`, + { + method: 'POST', + body: JSON.stringify(containerConfig) + }, + envId + ); + } // Attach additional networks now that the container exists. Docker only allows // a single network at create time, so anything beyond the primary is connected here. @@ -2255,16 +2300,18 @@ export async function recreateContainerFromInspect( // from the old container's settings to avoid getting a random bridge IP. // Skip for shared network modes — EndpointsConfig conflicts with container:/host/none modes. if (!isSharedNetwork && initialNetworkName && initialNetworkConfig) { - const endpointConfig = { ...initialNetworkConfig }; + // An auto-assigned MAC must not follow the container: on daemons that derive it + // from the IP, the replacement gets a new address and the carried MAC then + // collides with whoever inherits the old one (#1618). Only a configured MAC + // survives; otherwise Docker derives a fresh one. + const endpointConfig = endpointWithoutGeneratedMac({ ...initialNetworkConfig }); createConfig.NetworkingConfig = { EndpointsConfig: { [initialNetworkName]: endpointConfig } }; - // Container-level MacAddress conflicts with endpoint-level MacAddress. - // Docker requires them to match or the top-level one to be empty. - // The MAC is preserved in the endpoint config (correct location per API v1.44+), - // so clear the top-level one to avoid the conflict error. + // Container-level MacAddress conflicts with the endpoint-level one; Docker wants + // them equal or the top-level empty, so the endpoint config is the only carrier. delete createConfig.MacAddress; } @@ -2296,7 +2343,9 @@ export async function recreateContainerFromInspect( const nc = netConfig as any; if (nc.NetworkID) { try { - await connectContainerToNetworkRaw(nc.NetworkID, newContainerId, nc, envId); + // Same rule as the creation endpoint above: never carry a generated MAC. + const settings = endpointWithoutGeneratedMac({ ...nc }); + await connectContainerToNetworkRaw(nc.NetworkID, newContainerId, settings, envId); } catch (netError: any) { log?.(`Warning: Failed to connect to network "${netName}": ${netError.message}`); } @@ -2581,56 +2630,18 @@ export function extractContainerOptions(inspectData: any): CreateContainerOption const networkSettings = inspectData.NetworkSettings?.Networks || {}; const primaryNetwork = hostConfig.NetworkMode || 'bridge'; - // Extract primary network aliases, static IP, and gateway priority - let networkAliases: string[] | undefined; - let networkIpv4Address: string | undefined; - let networkIpv6Address: string | undefined; - let macAddress: string | undefined; - let networkGwPriority: number | undefined; - const containerId = inspectData.Id || ''; - const shortContainerId = containerId.substring(0, 12); - - // Extract compose labels for alias reconstruction - const composeProject = config.Labels?.['com.docker.compose.project']; - const composeService = config.Labels?.['com.docker.compose.service']; - - for (const [netName, netConfig] of Object.entries(networkSettings)) { - const netConf = netConfig as any; - const isPrimary = netName === primaryNetwork || - (primaryNetwork === 'bridge' && (netName === 'bridge' || netName === 'default')); - - if (isPrimary) { - // Filter out auto-generated container IDs - const allAliases = (netConf.Aliases?.length > 0 ? netConf.Aliases : netConf.DNSNames) || []; - networkAliases = allAliases.filter((a: string) => - a !== containerId && a !== shortContainerId - ); - // For compose containers, ensure service name and project-service aliases - if (composeProject && composeService) { - if (!networkAliases) networkAliases = []; - if (!networkAliases.includes(composeService)) { - networkAliases.push(composeService); - } - const projectService = `${composeProject}-${composeService}`; - if (!networkAliases.includes(projectService)) { - networkAliases.push(projectService); - } - } - - if (!networkAliases || networkAliases.length === 0) { - networkAliases = undefined; - } - - networkIpv4Address = netConf.IPAMConfig?.IPv4Address || undefined; - networkIpv6Address = netConf.IPAMConfig?.IPv6Address || undefined; - macAddress = netConf.MacAddress || undefined; - networkGwPriority = netConf.GwPriority !== undefined && netConf.GwPriority !== 0 - ? netConf.GwPriority : undefined; - break; - } - } + const mapped = mapContainerNetworks(networkSettings, primaryNetwork, containerId, { + project: config.Labels?.['com.docker.compose.project'], + service: config.Labels?.['com.docker.compose.service'] + }); + const { primaryAliases: networkAliases, primaryIpv4Address: networkIpv4Address, + primaryIpv6Address: networkIpv6Address, primaryGwPriority: networkGwPriority, + additionalNetworks, networkConfigs } = mapped; + // The live endpoint MAC is Docker's own, not the user's; surfacing it here would save + // it back as an explicit setting and pin the container to it (#1618). + const macAddress = configuredMacAddress(inspectData, mapped.primaryEndpoint as any) ?? undefined; // Device requests (GPU, etc.) const deviceRequests = hostConfig.DeviceRequests?.length > 0 @@ -2672,6 +2683,8 @@ export function extractContainerOptions(inspectData: any): CreateContainerOption networkIpv4Address, networkIpv6Address, networkGwPriority, + additionalNetworks: additionalNetworks.length > 0 ? additionalNetworks : undefined, + networkConfigs: Object.keys(networkConfigs).length > 0 ? networkConfigs : undefined, // User and hostname user: config.User || undefined, @@ -2797,17 +2810,17 @@ export async function updateContainer(id: string, options: Partial { + return (await getRegistryManifestDigestDetailed(imageName)).digest; +} + +/** Like getRegistryManifestDigest, but a null digest carries the specific reason (#1486). */ +export async function getRegistryManifestDigestDetailed( + imageName: string +): Promise<{ digest: string | null; reason?: string }> { + let registry = ''; try { - const { registry, repo, tag } = parseImageReference(imageName); - if (!isSafeRegistryHost(registry).ok) return null; + const parsed = parseImageReference(imageName); + registry = parsed.registry; + const { repo, tag } = parsed; + if (!isSafeRegistryHost(registry).ok) { + return { digest: null, reason: describeRegistryFailure({ kind: 'blocked-host', registry }) }; + } const token = await getRegistryBearerToken(registry, repo); // Honour the stored registry scheme for the manifest fetch too (#1580), not just // the token challenge - otherwise a plain-HTTP registry still gets an HTTPS request. @@ -3777,18 +3802,22 @@ export async function getRegistryManifestDigest(imageName: string): Promise('/info', {}, envId); } export async function getDockerVersion(envId?: number | null) { - return dockerJsonRequest('/version', {}, envId); + return dockerJsonRequest<{ ApiVersion?: string; Version?: string }>('/version', {}, envId); } /** @@ -4209,10 +4250,11 @@ export async function getNegotiatedApiVersion(envId: number): Promise { + // Edge connections go WebSocket -> agent -> Docker daemon, which adds latency on slow + // hosts. Use a longer timeout for edge to avoid false negatives on overloaded NAS/VPS + // devices. Declared out here so the catch below can name the host in its warning. + const config = await getDockerConfig(envId).catch(() => null); try { - // Edge connections go WebSocket → agent → Docker daemon, which adds latency on slow hosts. - // Use a longer timeout for edge to avoid false negatives on overloaded NAS/VPS devices. - const config = await getDockerConfig(envId).catch(() => null); const timeoutMs = config?.connectionType === 'hawser-edge' ? 20000 : 5000; const response = await dockerFetch('/_ping', { signal: AbortSignal.timeout(timeoutMs) @@ -5513,7 +5555,7 @@ async function streamLocalStderr( if (!reader) return; // Cancel reader when abort signal fires (container exited) - signal?.addEventListener('abort', () => reader.cancel(), { once: true }); + cancelReaderOnAbort(reader, signal); try { let buffer: Buffer = Buffer.alloc(0); diff --git a/src/lib/server/endpoint-mac-core.ts b/src/lib/server/endpoint-mac-core.ts new file mode 100644 index 000000000..cd77eec9f --- /dev/null +++ b/src/lib/server/endpoint-mac-core.ts @@ -0,0 +1,87 @@ +/** + * Decide whether a container's MAC address may be carried to a replacement. + * + * Older daemons derive a bridge MAC from the container's IP (02:42 followed by the + * four address octets). A recreate gives the replacement a new IP - the old container + * still holds the old one while the new one is created - so carrying that MAC pins it + * to an address it no longer matches, and once another container inherits the old IP + * the two share a MAC on the bridge and roughly half the frames are misdelivered + * (#1618). + * + * The MAC's own shape is what settles it. A derived MAC is recognisable from the + * endpoint's IP alone, and it is also the only kind that can collide, since newer + * daemons assign random addresses instead. So a MAC is dropped exactly when it spells + * out the IP it was generated from; anything the user pinned is carried through. + * + * `Config.MacAddress` is not usable for this: daemons below API 1.44 omit the field + * when the MAC was generated, and API 1.44+ omits it for every container, so its + * absence carries no signal either way. + */ + +/** The bits of a container inspect this decision needs. */ +export interface MacInspectLike { + Config?: { MacAddress?: string | null } | null; +} + +/** An endpoint's settings as they appear in inspect / as sent on create. */ +export interface EndpointLike { + MacAddress?: string | null; + IPAddress?: string | null; + [key: string]: unknown; +} + +function normalizeMac(mac: unknown): string | null { + if (typeof mac !== 'string') return null; + const trimmed = mac.trim(); + return trimmed === '' ? null : trimmed.toLowerCase(); +} + +/** The MAC Docker generates for `ip` on a bridge, or null if `ip` is not IPv4. */ +export function macDerivedFromIp(ip: unknown): string | null { + if (typeof ip !== 'string') return null; + const octets = ip.trim().split('.'); + if (octets.length !== 4) return null; + const hex: string[] = []; + for (const octet of octets) { + if (!/^\d{1,3}$/.test(octet)) return null; + const value = Number(octet); + if (value > 255) return null; + hex.push(value.toString(16).padStart(2, '0')); + } + return `02:42:${hex.join(':')}`; +} + +/** + * True when the endpoint's MAC is the one Docker would have generated from its own IP, + * i.e. an address the user never chose and that must not follow the container. + */ +export function isGeneratedEndpointMac(endpoint: EndpointLike | null | undefined): boolean { + const mac = normalizeMac(endpoint?.MacAddress); + if (mac === null) return false; + return mac === macDerivedFromIp(endpoint?.IPAddress); +} + +/** + * The MAC to prefill an edit form with, or null when the container has none the user + * chose. Never reports a generated address as if it were a setting. + */ +export function configuredMacAddress( + inspect: MacInspectLike | null | undefined, + endpoint?: EndpointLike | null +): string | null { + const declared = normalizeMac(inspect?.Config?.MacAddress); + if (declared !== null) return declared; + if (!endpoint || isGeneratedEndpointMac(endpoint)) return null; + return normalizeMac(endpoint.MacAddress); +} + +/** + * Copy of `endpoint` safe to send on create/connect: a MAC Docker derived from the old + * IP is dropped so the replacement gets one matching its own address; a MAC the user + * pinned is carried through. + */ +export function endpointWithoutGeneratedMac(endpoint: T): T { + const out = { ...endpoint }; + if (isGeneratedEndpointMac(endpoint)) delete out.MacAddress; + return out; +} 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/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/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

>(); + + /** + * `waitMs` bounds only the WAIT for the key to come free, never the operation + * itself: a long-running turn is the normal case and must not abort itself. + * On expiry the caller gets a QueueTimeoutError and its turn is skipped, so + * the queue keeps moving rather than stalling behind an abandoned slot. + */ + async run(key: string | number, fn: () => Promise, waitMs?: number): Promise { + const k = String(key); + const prev = this.tails.get(k) ?? Promise.resolve(); + // Gate that opens when our turn is fully done, so the next caller waits. + let opened!: () => void; + const done = new Promise((r) => { + opened = r; + }); + this.tails.set(k, prev.then(() => done)); + + try { + await this.awaitTurn(prev, k, waitMs); // prev never rejects + } catch (error) { + opened(); // give the slot up rather than holding the chain + throw error; + } + + try { + return await fn(); + } finally { + opened(); // release the next caller regardless of our outcome + } + } + + private async awaitTurn(prev: Promise, key: string, waitMs?: number): Promise { + if (waitMs === undefined) return prev; + + let timer: ReturnType | undefined; + try { + await Promise.race([ + prev, + new Promise((_, reject) => { + timer = setTimeout( + () => reject(new QueueTimeoutError(`timed out waiting for "${key}" after ${waitMs}ms`)), + waitMs + ); + }) + ]); + } finally { + if (timer) clearTimeout(timer); + } + } +} 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/server/notifications/apprise.ts b/src/lib/server/notifications/apprise.ts index 50977e87b..e9d5dd2e8 100644 --- a/src/lib/server/notifications/apprise.ts +++ b/src/lib/server/notifications/apprise.ts @@ -11,10 +11,11 @@ * apprises://host[:port]/key → HTTPS variant * apprise://host[:port]/prefix/key → path-prefixed Apprise behind a reverse proxy * apprise://host[:port]/key?tag=devops → optional tag filter + * apprise://user:pass@host[:port]/key - HTTP Basic auth (sent as a header) * * Setup docs: https://github.com/caronc/apprise-api */ -import { notificationFetch, drainResponse, type NotificationPayload, type NotificationResult } from './shared'; +import { notificationFetch, drainResponse, splitBasicAuth, type NotificationPayload, type NotificationResult } from './shared'; export async function sendApprise(appriseUrl: string, payload: NotificationPayload): Promise { const isSecure = appriseUrl.startsWith('apprises'); @@ -32,7 +33,13 @@ export async function sendApprise(appriseUrl: string, payload: NotificationPaylo if (parts.length < 2) { return { success: false, error: 'Invalid Apprise URL. Expected: apprise://host[:port]/key' }; } - const hostPort = parts[0]; + let hostPort: string; + let authHeader: string | null; + try { + ({ host: hostPort, authHeader } = splitBasicAuth(parts[0])); + } catch (error) { + return { success: false, error: `Invalid Apprise URL: ${error instanceof Error ? error.message : String(error)}` }; + } // The Apprise key is the last path segment. Anything between host and key // is a path prefix (some users mount Apprise behind a reverse proxy // at /apprise/ — we preserve that). @@ -65,9 +72,11 @@ export async function sendApprise(appriseUrl: string, payload: NotificationPaylo if (format) body.format = format; // text | markdown | html try { + const headers: Record = { 'Content-Type': 'application/json' }; + if (authHeader) headers['Authorization'] = authHeader; const response = await notificationFetch(`${baseUrl}/notify/${encodeURIComponent(key)}`, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers, body: JSON.stringify(body) }); diff --git a/src/lib/server/notifications/generic-webhook.ts b/src/lib/server/notifications/generic-webhook.ts index d6df28095..4cd8dc902 100644 --- a/src/lib/server/notifications/generic-webhook.ts +++ b/src/lib/server/notifications/generic-webhook.ts @@ -1,14 +1,28 @@ -/** Generic JSON webhook. json:// or jsons:// (HTTPS). */ -import { notificationFetch, drainResponse, type NotificationPayload, type NotificationResult } from './shared'; +/** Generic JSON webhook. json:// or jsons:// (HTTPS); json://user:pass@host/path sends Basic auth. */ +import { notificationFetch, drainResponse, splitBasicAuth, type NotificationPayload, type NotificationResult } from './shared'; export async function sendGenericWebhook(appriseUrl: string, payload: NotificationPayload): Promise { // json://hostname/path or jsons://hostname/path - const url = appriseUrl.replace(/^jsons?:\/\//, appriseUrl.startsWith('jsons') ? 'https://' : 'http://'); + const scheme = appriseUrl.startsWith('jsons') ? 'https://' : 'http://'; + const rest = appriseUrl.replace(/^jsons?:\/\//, ''); + const authorityEnd = rest.search(/[/?#]/); + const authority = authorityEnd === -1 ? rest : rest.slice(0, authorityEnd); + const remainder = authorityEnd === -1 ? '' : rest.slice(authorityEnd); + let host: string; + let authHeader: string | null; try { - const response = await notificationFetch(url, { + ({ host, authHeader } = splitBasicAuth(authority)); + } catch (error) { + return { success: false, error: `Invalid webhook URL: ${error instanceof Error ? error.message : String(error)}` }; + } + const headers: Record = { 'Content-Type': 'application/json' }; + if (authHeader) headers['Authorization'] = authHeader; + + try { + const response = await notificationFetch(`${scheme}${host}${remainder}`, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers, body: JSON.stringify({ title: payload.title, message: payload.message, diff --git a/src/lib/server/notifications/mattermost.ts b/src/lib/server/notifications/mattermost.ts index 49dc58dc3..b1a82b937 100644 --- a/src/lib/server/notifications/mattermost.ts +++ b/src/lib/server/notifications/mattermost.ts @@ -1,6 +1,49 @@ /** Mattermost incoming webhook. mmost:// or mmosts:// (HTTPS). */ import { notificationFetch, drainResponse, type NotificationPayload, type NotificationResult } from './shared'; +/** Severity colours, matching the Discord embed so the two channels read alike. */ +const ATTACHMENT_COLORS: Record = { + error: '#FF0000', + warning: '#FFAA00', + success: '#00FF00', + info: '#0099FF' +}; + +/** + * The webhook body for one notification. + * + * Mattermost accepts Slack-style attachments, which carry a severity colour and a + * footer the plain `text` form cannot. The attachment replaces `text` rather than + * accompanying it - Mattermost renders both, so sending both shows the message + * twice (#1607). `fallback` is what push notifications and email digests read, so + * it carries the message for anyone not looking at the channel. + */ +export function buildMattermostBody( + payload: NotificationPayload, + username?: string, + nowSeconds: number = Math.floor(Date.now() / 1000) +): Record { + const title = payload.environmentName + ? `${payload.title} [${payload.environmentName}]` + : payload.title; + + const body: Record = { + attachments: [ + { + color: ATTACHMENT_COLORS[payload.type ?? 'info'] ?? ATTACHMENT_COLORS.info, + fallback: `${title}\n${payload.message}`, + title, + text: payload.message, + mrkdwn_in: ['text', 'pretext'], + ts: nowSeconds, + ...(payload.environmentName && { footer: `Environment: ${payload.environmentName}` }) + } + ] + }; + if (username) body.username = username; + return body; +} + export async function sendMattermost(appriseUrl: string, payload: NotificationPayload): Promise { // mmost://[botname@]hostname[:port][/path]/token or mmosts://... const isSecure = appriseUrl.startsWith('mmosts'); @@ -27,14 +70,7 @@ export async function sendMattermost(appriseUrl: string, payload: NotificationPa const url = `${protocol}://${hostAndPath}/hooks/${token}`; - const envTag = payload.environmentName ? ` \`${payload.environmentName}\`` : ''; - const body: Record = { - text: `*${payload.title}*${envTag}\n${payload.message}` - }; - - if (username) { - body.username = username; - } + const body = buildMattermostBody(payload, username); try { const response = await notificationFetch(url, { diff --git a/src/lib/server/notifications/shared.ts b/src/lib/server/notifications/shared.ts index e308b7694..f96051333 100644 --- a/src/lib/server/notifications/shared.ts +++ b/src/lib/server/notifications/shared.ts @@ -78,3 +78,28 @@ export async function drainResponse(response: Response): Promise { export function titleWithEnv(payload: NotificationPayload): string { return payload.environmentName ? `${payload.title} [${payload.environmentName}]` : payload.title; } + +/** + * Move `user:pass@` userinfo off a URL authority into a Basic Authorization header: + * fetch() refuses URLs that carry credentials (#1611). user/pass are percent-decoded, + * so `/ ? # %` (and ideally `@ :`) inside them must be encoded. Throws on malformed encoding. + */ +export function splitBasicAuth(authority: string): { host: string; authHeader: string | null } { + // URL parsing ends the authority at a backslash, so an '@' after one is path, not userinfo. + const backslash = authority.indexOf('\\'); + const at = authority.lastIndexOf('@', backslash === -1 ? authority.length : backslash); + if (at === -1) return { host: authority, authHeader: null }; + const userinfo = authority.slice(0, at); + const host = authority.slice(at + 1); + const colon = userinfo.indexOf(':'); + let user: string; + let pass: string; + try { + user = decodeURIComponent(colon === -1 ? userinfo : userinfo.slice(0, colon)); + pass = colon === -1 ? '' : decodeURIComponent(userinfo.slice(colon + 1)); + } catch { + throw new Error('malformed percent-encoding in URL credentials'); + } + if (!user && !pass) return { host, authHeader: null }; + return { host, authHeader: `Basic ${Buffer.from(`${user}:${pass}`).toString('base64')}` }; +} 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/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/primary-network-switch-core.ts b/src/lib/server/primary-network-switch-core.ts new file mode 100644 index 000000000..3c191e5a9 --- /dev/null +++ b/src/lib/server/primary-network-switch-core.ts @@ -0,0 +1,77 @@ +/** + * Re-scope a container's per-network settings when its primary network changes. + * + * Aliases, static addresses, MAC and gateway priority belong to ONE network. Carrying + * the old primary's values onto a different network fails outright - compose service + * aliases applied to the default bridge come back as "invalid endpoint settings". + * + * The new primary is often one of the extras the container is already on, and then its + * own settings are the ones that survive: createContainer builds the primary endpoint + * from the `network*` fields and skips the extras entry whose name equals the primary, + * so a static address held on that network is lost unless it is handed over here. + * + * Pure, so the rules can be tested without a daemon. + */ + +/** One endpoint's settings, as extractContainerOptions reports them. */ +export interface EndpointConfig { + ipv4Address?: string; + ipv6Address?: string; + aliases?: string[]; +} + +/** The subset of container options this decision reads and rewrites. */ +export interface NetworkScopedOptions { + networkAliases?: string[]; + networkIpv4Address?: string; + networkIpv6Address?: string; + networkGwPriority?: number; + macAddress?: string; + additionalNetworks?: string[]; + networkConfigs?: Record; +} + +/** + * The options a recreate should use once `newPrimary` becomes the primary network. + * + * `incoming` is the request's own options, when it carries any. The primary endpoint is + * built from the `network*` fields and the extras entry for the primary is skipped, so a + * caller that edits the new primary's address the same way it edits any other network - + * through `networkConfigs`, which is all the edit form sends - would otherwise have that + * edit overwritten by the value read from the container. The caller's entry therefore + * wins, and its absence means cleared rather than "use the old one". + * + * Returns a new object; the input is left alone. `gwPriority` and the MAC are always + * dropped - a priority is meaningless against a different set of attachments, and a + * carried MAC collides once another container inherits the old address. + */ +export function rescopeForPrimarySwitch( + options: T, + newPrimary: string, + incoming?: Pick +): T & NetworkScopedOptions { + const promoted = incoming?.networkConfigs + ? incoming.networkConfigs[newPrimary] + : options.networkConfigs?.[newPrimary]; + + const next: T & NetworkScopedOptions = { + ...options, + networkAliases: promoted?.aliases, + networkIpv4Address: promoted?.ipv4Address, + networkIpv6Address: promoted?.ipv6Address, + networkGwPriority: undefined, + macAddress: undefined + }; + + // The old primary was never in these, so removing the new primary leaves exactly + // the networks that remain extras. + if (options.additionalNetworks) { + next.additionalNetworks = options.additionalNetworks.filter((n) => n !== newPrimary); + } + if (options.networkConfigs) { + const { [newPrimary]: _promoted, ...rest } = options.networkConfigs; + next.networkConfigs = rest; + } + + return next; +} diff --git a/src/lib/server/provider-kind-core.ts b/src/lib/server/provider-kind-core.ts new file mode 100644 index 000000000..e808fa4e0 --- /dev/null +++ b/src/lib/server/provider-kind-core.ts @@ -0,0 +1,23 @@ +/** + * Reading a session's provider column. + * + * A session records which OIDC provider issued it - `oidc:Keycloak` - so logging out + * can end that provider's session too. LDAP and local sessions record the bare kind, + * since nothing is ended at their end. Callers asking what sort of account this is + * want the kind, not the name. + */ +export function providerKind(provider: string | null | undefined): 'local' | 'ldap' | 'oidc' { + if (!provider) return 'local'; + if (provider === 'oidc' || provider.startsWith('oidc:')) return 'oidc'; + if (provider === 'ldap' || provider.startsWith('ldap:')) return 'ldap'; + // An unrecognised value is treated as a local account: it grants nothing extra, + // and the alternative is trusting a string nothing in the app writes. + return 'local'; +} + +/** The provider name in an `oidc:` 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/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/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/registry-failure-core.ts b/src/lib/server/registry-failure-core.ts new file mode 100644 index 000000000..60603186d --- /dev/null +++ b/src/lib/server/registry-failure-core.ts @@ -0,0 +1,94 @@ +/** + * Pure helpers turning a failed registry manifest lookup into a specific, human-readable + * reason for update-check logs and tooltips (#1486). Kept out of docker.ts so they can be + * unit-tested without pulling in docker/db. + */ + +export type RegistryFailure = + | { kind: 'blocked-host'; registry: string } + | { kind: 'http'; status: number; retryAfter?: string | null } + | { kind: 'no-digest' } + | { kind: 'error'; registry: string; error: unknown }; + +const DNS_CODES = new Set(['ENOTFOUND', 'EAI_AGAIN']); +const TLS_PROTOCOL_CODES = new Set(['ERR_SSL_WRONG_VERSION_NUMBER', 'EPROTO', 'ERR_SSL_PACKET_LENGTH_TOO_LONG']); +const TLS_CERT_CODES = new Set([ + 'DEPTH_ZERO_SELF_SIGNED_CERT', + 'SELF_SIGNED_CERT_IN_CHAIN', + 'UNABLE_TO_VERIFY_LEAF_SIGNATURE', + 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY', + 'CERT_HAS_EXPIRED', + 'ERR_TLS_CERT_ALTNAME_INVALID' +]); +const UNREACHABLE_CODES = new Set([ + 'ECONNREFUSED', + 'ECONNRESET', + 'ETIMEDOUT', + 'EHOSTUNREACH', + 'ENETUNREACH', + 'UND_ERR_CONNECT_TIMEOUT', + 'UND_ERR_HEADERS_TIMEOUT', + 'UND_ERR_SOCKET' +]); + +/** Error codes and messages along the cause chain (Node's fetch wraps the socket error in `cause`). */ +function collectErrorInfo(error: unknown): { codes: string[]; text: string } { + const codes: string[] = []; + const messages: string[] = []; + const seen = new Set(); + const queue: unknown[] = [error]; + while (queue.length > 0 && seen.size < 10) { + const e = queue.shift(); + if (typeof e === 'string') messages.push(e); + if (!e || typeof e !== 'object' || seen.has(e)) continue; + seen.add(e); + const err = e as { code?: unknown; message?: unknown; cause?: unknown; errors?: unknown }; + if (typeof err.code === 'string') codes.push(err.code); + if (typeof err.message === 'string') messages.push(err.message); + if (err.cause !== undefined) queue.push(err.cause); + if (Array.isArray(err.errors)) queue.push(...err.errors); + } + return { codes, text: messages.join(' ') }; +} + +/** Retry-After is registry-controlled: only echo a seconds count or a short HTTP-date. */ +function formatRetryAfter(value: string | null | undefined): string { + const v = value?.trim(); + if (!v) return ''; + if (/^\d+$/.test(v)) return `, retry after ${v}s`; + if (/^[\w ,:+-]{1,40}$/.test(v)) return `, retry after ${v}`; + return ''; +} + +export function describeRegistryFailure(failure: RegistryFailure): string { + switch (failure.kind) { + case 'blocked-host': + return `Registry host not allowed (${failure.registry})`; + case 'no-digest': + return 'Registry response had no Docker-Content-Digest header'; + case 'http': { + const s = failure.status; + if (s === 429) return `Rate limited by registry (429${formatRetryAfter(failure.retryAfter)})`; + // A locally built image and an image deleted upstream both 404 - don't claim to know which. + if (s === 404) return 'Registry returned 404 (image or tag not found - deleted upstream, private, or a locally built image)'; + if (s === 401 || s === 403) return `Registry denied access (${s}) - private image or missing credentials`; + if (s >= 500) return `Registry server error (${s})`; + return `Could not query registry (${s})`; + } + case 'error': { + const { registry } = failure; + const { codes, text } = collectErrorInfo(failure.error); + if (codes.some((c) => DNS_CODES.has(c)) || /\b(ENOTFOUND|EAI_AGAIN)\b/.test(text)) { + return `DNS resolution failed for ${registry}`; + } + if (codes.some((c) => TLS_PROTOCOL_CODES.has(c)) || text.toLowerCase().includes('wrong version number')) { + return `TLS handshake with ${registry} failed - registry may be HTTP-only (set its URL to http:// in Settings > Registries)`; + } + const cert = codes.find((c) => TLS_CERT_CODES.has(c)); + if (cert) return `TLS certificate not trusted for ${registry} (${cert})`; + const unreachable = codes.find((c) => UNREACHABLE_CODES.has(c)); + if (unreachable) return `Registry unreachable: ${registry} (${unreachable})`; + return codes.length > 0 ? `Could not query registry (${codes[0]})` : 'Could not query registry'; + } + } +} diff --git a/src/lib/server/scan-queries.ts b/src/lib/server/scan-queries.ts new file mode 100644 index 000000000..6c788972d --- /dev/null +++ b/src/lib/server/scan-queries.ts @@ -0,0 +1,48 @@ +import { db, vulnerabilityScans, eq, desc, isNull, sql } from './db/drizzle.js'; +import type { VulnerabilityScanData } from './db'; + +/** + * The newest scan per image and scanner. `undefined` reads every environment, + * `null` the local one, a number that environment. + * + * Every scan keeps its whole findings document, so the superseded rows are + * dropped by the database rather than read and discarded here. + */ +export async function getAllLatestScans( + environmentId?: number | null +): Promise { + const envFilter = + environmentId === undefined + ? undefined + : environmentId === null + ? isNull(vulnerabilityScans.environmentId) + : eq(vulnerabilityScans.environmentId, environmentId); + + // The ranking and the filter stay in ONE statement: reading the ids out and + // passing them back would bind a parameter per row, and the driver's variable + // limit (32766 on SQLite) would turn a large history into a query error. + // `id desc` breaks a tie on scanned_at, so which row wins is deterministic. + const ranked = db + .select({ + id: vulnerabilityScans.id, + rn: sql`row_number() over ( + partition by ${vulnerabilityScans.imageId}, ${vulnerabilityScans.scanner} + order by ${vulnerabilityScans.scannedAt} desc, ${vulnerabilityScans.id} desc + )`.as('dockhand_rn') + }) + .from(vulnerabilityScans) + .where(envFilter) + .as('ranked'); + + const rows = await db + .select({ scan: vulnerabilityScans }) + .from(vulnerabilityScans) + .innerJoin(ranked, eq(vulnerabilityScans.id, ranked.id)) + .where(eq(ranked.rn, 1)) + .orderBy(desc(vulnerabilityScans.scannedAt)); + + return rows.map(({ scan }: { scan: Record }) => ({ + ...scan, + vulnerabilities: scan.vulnerabilities ? JSON.parse(scan.vulnerabilities as string) : [] + })) as VulnerabilityScanData[]; +} diff --git a/src/lib/server/scanner-registry-core.ts b/src/lib/server/scanner-registry-core.ts index f6cfdfa4c..6a1a025f2 100644 --- a/src/lib/server/scanner-registry-core.ts +++ b/src/lib/server/scanner-registry-core.ts @@ -28,17 +28,78 @@ export function isDaemonExportFailure(output: string | null | undefined): boolea ); } +/** A bare image ID: `sha256:<64 hex>` or the digest alone, as the auto-update passes. */ +function isBareImageId(ref: string): boolean { + return /^(sha256:)?[0-9a-f]{64}$/.test(ref); +} + +/** A tag Dockhand applies to itself while judging a pending update. */ +function isTempTag(tag: string): boolean { + return /-dockhand-(pending|update)$/.test(tag); +} + +/** + * The ref to ask a registry for, given the ref the daemon was asked for. + * + * Neither of the refs an auto-update scans with means anything to a registry. One path + * scans a temporary local tag (`repo:tag-dockhand-pending`) so the new image can be + * judged before it takes the real tag; another scans the bare image ID, which keys the + * scan cache. Sending either one asks for something that was never pushed, and the + * registry answers MANIFEST_UNKNOWN. + * + * A temp tag is resolved by removing the suffix, which getTempImageTag appends verbatim + * and only to the final tag component - a repository or registry port containing the + * same words is left alone. A bare ID carries no repository at all, so it is resolved + * from `repoTags` (an image inspect), preferring a real tag over a temp one. + * + * Returns the ref unchanged when it is already something a registry can answer, or when + * nothing better is known. + */ +export function toRegistryRef(image: string, repoTags?: string[] | null): string { + if (isBareImageId(image)) { + const tags = (repoTags || []).filter( + (t) => typeof t === 'string' && t && t !== ':' + ); + // A temp tag is no better than the ID here, so take a real one or nothing. + const real = tags.find((t) => !isTempTag(t)); + return real ? toRegistryRef(real) : image; + } + + // A digest pins the content and resolves in the registry as-is. + if (image.includes('@')) return image; + + const lastColon = image.lastIndexOf(':'); + if (lastColon === -1) return image; + + const tag = image.slice(lastColon + 1); + // A colon before a slash is a port, not a tag separator (registry:5000/repo). + if (tag.includes('/')) return image; + + for (const suffix of ['-dockhand-pending', '-dockhand-update']) { + if (tag.endsWith(suffix) && tag.length > suffix.length) { + return `${image.slice(0, lastColon)}:${tag.slice(0, -suffix.length)}`; + } + } + return image; +} + /** * Rewrite a daemon-mode scanner command into a registry-mode one for the same image. - * grype needs the source prefix `registry:`; trivy scans the registry ref - * as-is once the docker socket is absent, so only the image token needs to be the - * plain ref (it already is). The image token is identified by exact match against - * `image` (parseCliArgs substitutes {image} with the ref verbatim). + * grype needs the source prefix `registry:`; trivy scans the registry ref as-is + * once the docker socket is absent. The image token is identified by exact match + * against `image` (parseCliArgs substitutes {image} with the ref verbatim), while + * `registryRef` is what the registry is actually asked for - they differ when the + * daemon was scanning a temporary local tag. */ -export function toRegistryScanCmd(scannerType: ScannerType, cmd: string[], image: string): string[] { +export function toRegistryScanCmd( + scannerType: ScannerType, + cmd: string[], + image: string, + registryRef: string = image +): string[] { return cmd.map((tok) => { if (tok !== image) return tok; - return scannerType === 'grype' ? `registry:${image}` : image; + return scannerType === 'grype' ? `registry:${registryRef}` : registryRef; }); } diff --git a/src/lib/server/scanner.ts b/src/lib/server/scanner.ts index 027a7e616..9d0838a53 100644 --- a/src/lib/server/scanner.ts +++ b/src/lib/server/scanner.ts @@ -19,6 +19,7 @@ import { import { isDaemonExportFailure, toRegistryScanCmd, + toRegistryRef, registryAuthEnv, imageRegistryAuthority, RegistryFallbackMemory @@ -836,7 +837,26 @@ async function runScannerContainerCore( // and was previously reachable only via the daemon, the registry scan can't reach // it. Surface that so a split-network user knows why. if (registryMode) { - cmd = toRegistryScanCmd(scannerType, cmd, imageName); + // An auto-update scans either a temporary local tag or the bare image ID, and a + // registry knows neither. A tag resolves by itself; an ID needs the inspect's + // RepoTags to name a repository at all, and that inspect is only worth paying + // for on this path. + let repoTags: string[] | undefined; + if (/^(sha256:)?[0-9a-f]{64}$/.test(imageName)) { + try { + repoTags = ((await inspectImage(imageName, envId)) as any)?.RepoTags ?? undefined; + } catch { + // Leave it unresolved; the scan fails with the registry's own message + // rather than an inspect error that says nothing about the scan. + } + } + const registryRef = toRegistryRef(imageName, repoTags); + if (registryRef !== imageName) { + console.log(`[Scanner] Registry mode - scanning ${registryRef} (${imageName} is local)`); + } else if (repoTags !== undefined) { + console.warn(`[Scanner] Registry mode - no pushed tag known for ${imageName}; the registry cannot resolve it`); + } + cmd = toRegistryScanCmd(scannerType, cmd, imageName, registryRef); if (isHawser) { console.warn(`[Scanner] Registry-mode fallback on a remote (hawser) env - the scanner container must be able to reach the registry from the remote host's default network.`); } diff --git a/src/lib/server/scheduler/tasks/container-update.ts b/src/lib/server/scheduler/tasks/container-update.ts index 95e88a022..ed6829dc2 100644 --- a/src/lib/server/scheduler/tasks/container-update.ts +++ b/src/lib/server/scheduler/tasks/container-update.ts @@ -15,7 +15,8 @@ import { createScheduleExecution, updateScheduleExecution, appendScheduleExecutionLog, - saveVulnerabilityScan + saveVulnerabilityScan, + removePendingContainerUpdateByName } from '../../db'; import { pullImage, @@ -589,6 +590,14 @@ export async function runContainerUpdate( if (result.success) { await updateAutoUpdateLastUpdated(containerName, envId); + // The container now runs the new image, so its pending row is spent. Left + // behind it keeps the container in the "updates available" list holding an id + // the recreate has already replaced, and the next batch update fails on it. + if (envId != null) { + await removePendingContainerUpdateByName(envId, containerName).catch((e) => + log(`Could not clear the pending update row: ${e?.message ?? e}`) + ); + } log(`Successfully updated container: ${containerName}`); await updateScheduleExecution(execution.id, { diff --git a/src/lib/server/secretproviders/proton.ts b/src/lib/server/secretproviders/proton.ts index 07b32dbc1..da66a7d6c 100644 --- a/src/lib/server/secretproviders/proton.ts +++ b/src/lib/server/secretproviders/proton.ts @@ -27,6 +27,7 @@ import { spawn } from 'node:child_process'; import { chmod, mkdtemp, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { isAbsolute, join } from 'node:path'; +import { KeyedSerializer, QueueTimeoutError } from '../keyed-serializer'; import type { ProtonConfig, SecretProvider, TestConnectionResult } from './shared'; import { stripSurroundingQuotes } from './shared'; @@ -187,6 +188,19 @@ async function executePassCli( reject(failure ?? spawnFailure(error)); }); + // A killed process can leave `close` pending indefinitely: the shell dies + // but a grandchild inherits the pipes and holds them open. Once the child + // itself is gone and we have already decided this call failed, there is no + // output left to wait for, so settle on `exit` instead of blocking the + // caller (and everything queued behind it) on those streams. + child.once('exit', () => { + if (settled || !failure) return; + settled = true; + clearTimeout(timeout); + if (killTimer) clearTimeout(killTimer); + reject(failure); + }); + child.once('close', (code) => { if (settled) return; settled = true; @@ -208,6 +222,28 @@ async function executePassCli( } } +/** + * Sessions run one at a time per pass-cli binary. The session directory is + * private per call, but pass-cli keeps one session per user underneath, so a + * second `login` blocks on the first: the stack editor's live probe and a deploy + * would otherwise race and one of them would spend its whole login timeout + * waiting. Keyed on the executable so two configured Proton providers, which + * share that binary, queue together. + */ +const sessions = new KeyedSerializer(); + +/** + * How long a caller waits for the binary before giving up. + * + * Derived from the pass-cli timeouts rather than picked, so it cannot drift + * below them: a session ahead of us is a login, its per-reference lookups and a + * logout, each already individually bounded, so it always ends on its own. The + * lookup allowance is generous because a reference-heavy stack legitimately + * holds the binary for a while, and waiting is what keeps that deploy working. + * A caller that exceeds even this is behind something wedged, not something slow. + */ +const SESSION_QUEUE_TIMEOUT_MS = LOGIN_TIMEOUT_MS + 20 * COMMAND_TIMEOUT_MS + LOGOUT_TIMEOUT_MS; + /** * Runs `fn` inside a fresh, private pass-cli session: login, then the callback, * then an unconditional logout, then the session dir is removed. The PAT never @@ -216,6 +252,22 @@ async function executePassCli( async function withSession( token: string, fn: (session: string) => Promise +): Promise { + try { + return await sessions.run(executablePath(), () => runSession(token, fn), SESSION_QUEUE_TIMEOUT_MS); + } catch (error: unknown) { + if (error instanceof QueueTimeoutError) { + throw new PassCliError( + 'Proton Pass is busy: another vault operation is still using pass-cli' + ); + } + throw error; + } +} + +async function runSession( + token: string, + fn: (session: string) => Promise ): Promise { let sessionDir: string | undefined; try { 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/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/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/server/vulnerabilities.ts b/src/lib/server/vulnerabilities.ts index e053e72de..35ad17548 100644 --- a/src/lib/server/vulnerabilities.ts +++ b/src/lib/server/vulnerabilities.ts @@ -5,7 +5,7 @@ * * Used by both GET /api/vulnerabilities (grid) and GET /api/vulnerabilities/export. */ -import { getAllLatestScans } from '$lib/server/db'; +import { getAllLatestScans } from '$lib/server/scan-queries'; import { listImages, listContainers, DockerConnectionError, EnvironmentNotFoundError } from '$lib/server/docker'; import { flattenScansToFindings, filterFindings, sortFindings } from '$lib/utils/vulnerability'; import type { Finding, FindingContainer, VulnerabilitySummary, ScanRow, FindingFilter, SortField } from '$lib/utils/vulnerability'; 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/stores/auth.ts b/src/lib/stores/auth.ts index ed598f90e..8dcf6db4f 100644 --- a/src/lib/stores/auth.ts +++ b/src/lib/stores/auth.ts @@ -129,8 +129,13 @@ function createAuthStore() { * Logout and clear session */ async logout() { + let logoutUrl: string | undefined; try { - await fetch('/api/auth/logout', { method: 'POST' }); + const response = await fetch('/api/auth/logout', { method: 'POST' }); + // An OIDC session ends at the provider too, or the next sign-in walks + // straight back into the same account without being asked. + const data = await response.json().catch(() => null); + if (data?.logoutUrl) logoutUrl = data.logoutUrl; } finally { // Clear auth state set({ @@ -142,6 +147,15 @@ function createAuthStore() { // Clear environment data to prevent showing stale info on login screen environments.clear(); } + + // After the local state is clear, so a provider that never redirects back + // still leaves this tab signed out. + if (logoutUrl) window.location.href = logoutUrl; + + // Where the caller should send somebody who has just signed out. The login + // form, explicitly: a provider whose session is still open would sign them + // straight back in if the page went looking for one on its own. + return { logoutUrl: logoutUrl ?? null, next: '/login?local=1' }; }, /** 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/stores/selfhst-refs.ts b/src/lib/stores/selfhst-refs.ts index b404aa4bb..400bb3af2 100644 --- a/src/lib/stores/selfhst-refs.ts +++ b/src/lib/stores/selfhst-refs.ts @@ -3,7 +3,7 @@ import { createSelfhstMatcher } from '$lib/utils/selfhst-match'; /** * A single shared matcher ((image, name?) -> selfh.st reference) built from the manifest. - * Resolves by image first, then by container name. The manifest is fetched at most once + * Resolves by image first, then container name, then image namespace. The manifest is fetched at most once * per session, only after something first needs it (i.e. the selfh.st-icons toggle is on). * Until it loads, the matcher returns null for everything, so containers keep the generic * icon. 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/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/disk-percentage-support.ts b/src/lib/utils/disk-percentage-support.ts new file mode 100644 index 000000000..a911b163c --- /dev/null +++ b/src/lib/utils/disk-percentage-support.ts @@ -0,0 +1,82 @@ +/** + * Whether percentage disk warnings can work on a host. + * + * The percentage mode needs a total to divide by, and takes it from `/info` + * `DriverStatus` -> `Data Space Total`. Only the devicemapper driver reports that + * key; overlay2 keeps images on an ordinary filesystem and has no pool of its own, + * so there is nothing to report. Without the key the check has no denominator and + * silently does nothing, which looks exactly like "measured, all fine" (#1631). + * + * Asked of the same field the metrics path reads, so the answer the UI shows and + * the behaviour at collection time cannot disagree. + */ + +/** The `DriverStatus` key holding the storage pool size. */ +export const POOL_SIZE_KEY = 'Data Space Total'; + +type DriverStatus = unknown; + +/** + * The pool size in bytes, or 0 when the value is not one the collector can divide by. + * + * The same shape the collector accepts, so a value it would reject - "0 B", "unknown", + * a unit nobody parses - is not reported here as a working denominator. + */ +export function poolSizeBytes(value: unknown): number { + if (typeof value !== 'string') return 0; + // Matched without trimming first, because the collector does not trim either: a + // padded value it would reject must not be reported here as a working denominator. + const match = value.match(/^([\d.]+)\s*([KMGT]?B)$/i); + if (!match) return 0; + const units: Record = { + B: 1, + KB: 1024, + MB: 1024 ** 2, + GB: 1024 ** 3, + TB: 1024 ** 4 + }; + return parseFloat(match[1]) * (units[match[2].toUpperCase()] || 1); +} + +/** + * Whether this host reports a pool size, so percentage warnings would fire. + * + * `null` means unknown - the host could not be asked. That is deliberately not + * `false`: a momentary outage must not present itself as an unsupported host. + */ +export function supportsPercentageWarnings(driverStatus: DriverStatus): boolean | null { + if (driverStatus == null) return null; + if (!Array.isArray(driverStatus)) return null; + + for (const entry of driverStatus) { + if (!Array.isArray(entry) || entry.length < 2) continue; + const [key, value] = entry; + if (key !== POOL_SIZE_KEY) continue; + // A size the collector cannot divide by is the same as absent. + return poolSizeBytes(value) > 0; + } + return false; +} + +/** What to tell somebody whose environment cannot use the mode it is set to. */ +export function percentageUnsupportedNote(storageDriver?: string | null): string { + const driver = storageDriver?.trim(); + return driver + ? `This host's ${driver} storage driver reports no total size, so percentage warnings never fire.` + : 'This host reports no total size, so percentage warnings never fire.'; +} + +/** + * Whether the percentage option should be unselectable. + * + * Only on a host that definitely cannot use it, and only when it is not the mode the + * environment already has. Reading the STORED mode rather than the form's current value + * is what keeps the escape hatch open: gating on the live value would disable the option + * the moment somebody switched away, trapping them in their own click. + */ +export function percentageOptionDisabled( + percentageSupported: boolean | null | undefined, + storedMode: string | null | undefined +): boolean { + return percentageSupported === false && storedMode !== 'percentage'; +} diff --git a/src/lib/utils/disk-segment-path.ts b/src/lib/utils/disk-segment-path.ts new file mode 100644 index 000000000..ad8bbd84b --- /dev/null +++ b/src/lib/utils/disk-segment-path.ts @@ -0,0 +1,16 @@ +// Dashboard disk-usage segment -> page it links to. Build cache has no page of +// its own, so it returns null and the click falls through to the tile (#1314). +export type DiskSegmentKey = 'images' | 'containers' | 'volumes' | 'buildCache'; + +export function diskSegmentPath(key: DiskSegmentKey): string | null { + switch (key) { + case 'images': + return '/images'; + case 'containers': + return '/containers'; + case 'volumes': + return '/volumes'; + default: + return null; + } +} 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/lib/utils/file-browser-start.ts b/src/lib/utils/file-browser-start.ts new file mode 100644 index 000000000..e033f62b4 --- /dev/null +++ b/src/lib/utils/file-browser-start.ts @@ -0,0 +1,10 @@ +// Where the container file browser opens: the image/container WORKDIR, else root (#1285). +// Docker stores WorkingDir absolute; anything relative or odd falls back to '/'. +export function fileBrowserStartPath(workingDir: unknown): string { + if (typeof workingDir !== 'string') return '/'; + const trimmed = workingDir.trim(); + if (!trimmed.startsWith('/')) return '/'; + const parts = trimmed.split('/').filter((p) => p && p !== '.'); + if (parts.includes('..')) return '/'; + return '/' + parts.join('/'); +} 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/lib/utils/network-identity.ts b/src/lib/utils/network-identity.ts new file mode 100644 index 000000000..830b21944 --- /dev/null +++ b/src/lib/utils/network-identity.ts @@ -0,0 +1,198 @@ +/** + * Decide whether two references point at the same Docker/Podman network. + * + * Podman exposes one network under two names: a container's inspect reports the default + * bridge as `podman`, while `GET /networks` and `HostConfig.NetworkMode` call it + * `bridge`. Both carry the SAME id. Comparing names therefore treats the primary network + * as an extra one, which shows it under "Additional networks" and makes an edit re-attach + * a network the container is already on (`403 network is already connected`) (#1619). + * + * Ids settle it when both sides have one; otherwise the name is all there is, which is + * the pre-existing behaviour and is correct on Docker, where the two never disagree. + */ + +/** A network as the container inspect or the network list reports it. */ +export interface NetworkRef { + name: string; + /** Absent on older API versions, and on a name-only reference. */ + id?: string | null; +} + +function normalizeId(id: unknown): string | null { + return typeof id === 'string' && id.trim() ? id.trim() : null; +} + +/** True when both refs are the same network, by id when known and by name otherwise. */ +export function isSameNetwork(a: NetworkRef, b: NetworkRef): boolean { + const idA = normalizeId(a.id); + const idB = normalizeId(b.id); + if (idA && idB) return idA === idB; + return a.name === b.name; +} + +/** + * Whether `netName` (a key of the inspect's Networks map) is the container's primary + * network, given `HostConfig.NetworkMode` and the full set of attached network names. + * + * Matching on the name alone misses Podman, which calls the default bridge "podman" in + * the inspect and "bridge" in NetworkMode - the primary is then never found and its + * aliases and static IPs are dropped on the next save (#1619). A container attached to + * exactly one network is on its primary whatever that network is called, which settles + * the alias without needing the network list. With several attachments there is nothing + * to infer from, so only the names are compared. + */ +export function isPrimaryNetworkName( + netName: string, + networkMode: string, + attachedNames: readonly string[] +): boolean { + if (netName === networkMode) return true; + if (networkMode === 'bridge' && (netName === 'bridge' || netName === 'default')) return true; + return attachedNames.length === 1 && attachedNames[0] === netName; +} + +/** The per-network settings a recreate has to carry over for one endpoint. */ +export interface EndpointSettings { + aliases?: string[]; + ipv4Address?: string; + ipv6Address?: string; +} + +/** + * The user-meaningful settings of one endpoint, as the inspect reports it. + * + * Docker seeds every endpoint with the container's own id and short id as aliases; those + * are its bookkeeping, not something to send back on create, so they are dropped. An + * endpoint with nothing left to say returns null, which keeps the caller's map free of + * empty entries. + */ +export function endpointSettingsFromInspect( + endpoint: { Aliases?: string[] | null; DNSNames?: string[] | null; IPAMConfig?: { IPv4Address?: string; IPv6Address?: string } | null } | null | undefined, + containerId: string +): EndpointSettings | null { + if (!endpoint) return null; + const shortId = containerId.substring(0, 12); + const raw = (endpoint.Aliases?.length ? endpoint.Aliases : endpoint.DNSNames) || []; + const aliases = raw.filter((a) => a !== containerId && a !== shortId); + + const settings: EndpointSettings = {}; + if (aliases.length > 0) settings.aliases = aliases; + if (endpoint.IPAMConfig?.IPv4Address) settings.ipv4Address = endpoint.IPAMConfig.IPv4Address; + if (endpoint.IPAMConfig?.IPv6Address) settings.ipv6Address = endpoint.IPAMConfig.IPv6Address; + return Object.keys(settings).length > 0 ? settings : null; +} + +/** + * The networks a container is attached to BESIDES its primary one. + * + * `attached` comes from the container inspect (name plus the id the daemon reported); + * `primaryName` is `HostConfig.NetworkMode`, a bare name. `known` is the network list, + * when the caller has it: it supplies the primary's id so a Podman alias still matches. + */ +export function additionalNetworkNames( + attached: NetworkRef[], + primaryName: string, + known: NetworkRef[] = [] +): string[] { + const primary: NetworkRef = { + name: primaryName, + id: known.find((n) => n.name === primaryName)?.id ?? null + }; + return attached.filter((net) => !isSameNetwork(net, primary)).map((net) => net.name); +} + +/** Everything a recreate needs to know about a container's network attachments. */ +export interface MappedNetworks { + /** The primary endpoint's aliases, or undefined when it has none worth sending. */ + primaryAliases?: string[]; + primaryIpv4Address?: string; + primaryIpv6Address?: string; + primaryGwPriority?: number; + /** The endpoint object the primary was read from, for the caller's MAC decision. */ + primaryEndpoint?: unknown; + /** Attachments other than the primary, in inspect order. */ + additionalNetworks: string[]; + /** Per-network settings for those extras, keyed by network name. */ + networkConfigs: Record; +} + +/** One network's entry under NetworkSettings.Networks in a container inspect. */ +type InspectEndpoint = { + Aliases?: string[] | null; + DNSNames?: string[] | null; + IPAMConfig?: { IPv4Address?: string; IPv6Address?: string } | null; + GwPriority?: number; +} | null; + +/** + * Split a container's attachments into its primary endpoint and the extras. + * + * The extras are what a recreate has to reconnect: without them an update that does not + * name the networks itself drops every secondary attachment, because there is nothing in + * the existing options to preserve them from. + * + * Both "bridge" and "default" answer to NetworkMode "bridge", so the FIRST match wins the + * primary and any later one is treated as an extra - otherwise it would overwrite the + * primary's aliases and IPs with its own. + */ +export function mapContainerNetworks( + networks: Record, + networkMode: string, + containerId: string, + compose?: { project?: string; service?: string } +): MappedNetworks { + const attachedNames = Object.keys(networks ?? {}); + const out: MappedNetworks = { additionalNetworks: [], networkConfigs: {} }; + let primarySeen = false; + + for (const [netName, netConfig] of Object.entries(networks ?? {})) { + const endpoint = netConfig as InspectEndpoint; + + if (primarySeen || !isPrimaryNetworkName(netName, networkMode, attachedNames)) { + out.additionalNetworks.push(netName); + const settings = endpointSettingsFromInspect(endpoint, containerId); + if (settings) out.networkConfigs[netName] = settings; + continue; + } + + primarySeen = true; + assignPrimary(out, endpoint, containerId, compose); + } + + return out; +} + +/** Fill in the primary half of the result from its endpoint. */ +function assignPrimary( + out: MappedNetworks, + endpoint: InspectEndpoint, + containerId: string, + compose?: { project?: string; service?: string } +): void { + out.primaryEndpoint = endpoint; + + const settings = endpointSettingsFromInspect(endpoint, containerId); + const aliases = composeAliases(settings?.aliases, compose); + + if (aliases.length > 0) out.primaryAliases = aliases; + if (settings?.ipv4Address) out.primaryIpv4Address = settings.ipv4Address; + if (settings?.ipv6Address) out.primaryIpv6Address = settings.ipv6Address; + if (endpoint?.GwPriority) out.primaryGwPriority = endpoint.GwPriority; +} + +/** + * A compose container is reached by its service name and project-service name; the + * daemon does not always list both, so make sure a recreate keeps them. + */ +function composeAliases( + existing: string[] | undefined, + compose?: { project?: string; service?: string } +): string[] { + const aliases = existing ? [...existing] : []; + if (!compose?.project || !compose?.service) return aliases; + + for (const alias of [compose.service, `${compose.project}-${compose.service}`]) { + if (!aliases.includes(alias)) aliases.push(alias); + } + return aliases; +} diff --git a/src/lib/utils/oidc-autologin.ts b/src/lib/utils/oidc-autologin.ts new file mode 100644 index 000000000..b37fddb65 --- /dev/null +++ b/src/lib/utils/oidc-autologin.ts @@ -0,0 +1,43 @@ +/** + * Whether the login page should go straight to the identity provider. + * + * Skipping the login form is a convenience for an instance where everyone signs in + * the same way, and a trap everywhere else: an identity provider that is down, or a + * redirect loop, must never make the login page unreachable. So the decision is + * narrow on purpose, and every way of saying "not this time" wins. + * + * Pure, so the rules are a unit test rather than something only a broken provider + * reveals. + */ + +export interface AutoLoginInput { + /** OIDC_AUTOLOGIN on the server. */ + enabled: boolean; + /** Initiate URLs of the enabled OIDC providers. */ + oidcInitiateUrls: string[]; + /** An error carried back from a previous attempt, if any. */ + error?: string | null; + /** `?local=1` on the login page: the operator asking for the form. */ + localRequested?: boolean; +} + +/** + * The URL to send the browser to, or null to render the login form. + * + * Requires exactly one provider: with two on screen there is a choice to make, and + * choosing for the user is not a redirect, it is a decision. + */ +export function autoLoginTarget(input: AutoLoginInput): string | null { + if (!input.enabled) return null; + + // A failed attempt leaves the form up, so one broken sign-in cannot become a + // loop between here and the provider. + if (input.error) return null; + + // The escape hatch an operator can type when the provider is unreachable. + if (input.localRequested) return null; + + if (input.oidcInitiateUrls.length !== 1) return null; + + return input.oidcInitiateUrls[0]; +} diff --git a/src/lib/utils/scan-log-scanners.ts b/src/lib/utils/scan-log-scanners.ts new file mode 100644 index 000000000..2cef7cd22 --- /dev/null +++ b/src/lib/utils/scan-log-scanners.ts @@ -0,0 +1,15 @@ +// Scanner columns for the scan-all log. Only scanners that actually ran get a column (#1628). + +export type ScannerId = 'grype' | 'trivy'; + +export const SCANNERS: readonly { id: ScannerId; label: string }[] = [ + { id: 'grype', label: 'Grype' }, + { id: 'trivy', label: 'Trivy' } +]; + +// Scanners with a result for at least one image, in the fixed SCANNERS order. +export function scannersInLog(entries: readonly { scanners: readonly { scanner: string }[] }[]) { + const seen = new Set(); + for (const e of entries) for (const s of e.scanners) seen.add(s.scanner); + return SCANNERS.filter((sc) => seen.has(sc.id)); +} diff --git a/src/lib/utils/scan-progress-lines.ts b/src/lib/utils/scan-progress-lines.ts new file mode 100644 index 000000000..48b79ac55 --- /dev/null +++ b/src/lib/utils/scan-progress-lines.ts @@ -0,0 +1,14 @@ +// Console lines for one scan progress event. A scanner output line carries the +// status-bar `message` too, so only the output is logged for it (#1629). +export interface ScanProgressEvent { + scanner?: string; + message?: string; + output?: string; +} + +export function scanProgressLines(data: ScanProgressEvent): string[] { + const scanner = data.scanner || 'dockhand'; + if (data.output) return [`[${scanner}] ${data.output}`]; + if (data.message) return [`[${scanner}] ${data.message}`]; + return []; +} diff --git a/src/lib/utils/selfhst-match.ts b/src/lib/utils/selfhst-match.ts index 62f15fd9e..280d39e6c 100644 --- a/src/lib/utils/selfhst-match.ts +++ b/src/lib/utils/selfhst-match.ts @@ -1,7 +1,8 @@ // Pure image-name -> selfh.st reference matcher. Conservative by design: a // confident hit returns the reference, anything uncertain returns null so the UI -// keeps its generic icon (never a wrong logo). Memoized per image string so a -// list of 100 containers costs one lookup per unique image, not per render. +// keeps its generic icon (never a wrong logo; the namespace fallback knowingly shows +// the vendor logo). Memoized per (image, name) pair so a list of 100 containers costs one +// lookup per unique pair, not per render. /** * Aliases ONLY for cases where the Docker image basename differs from the selfh.st @@ -21,7 +22,9 @@ const IMAGE_ALIASES: Record = { 'portainer-ee': 'portainer', 'qbittorrentvpn': 'qbittorrent', 'postgres': 'postgresql', - 'mongo': 'mongodb' + 'mongo': 'mongodb', + vmalert: 'victoriametrics', + vmagent: 'victoriametrics' }; /** @@ -41,6 +44,49 @@ export function imageBasename(image: string): string { return s; } +/** + * Namespaces that republish other projects' images, so their name says nothing about + * the app inside (`linuxserver/sonarr` is Sonarr, not LinuxServer). + */ +const DISTRIBUTOR_NAMESPACES = new Set([ + 'library', + 'linuxserver', + 'bitnami', + 'hotio', + 'binhex', + 'jlesage', + '11notes', + 'ubuntu', + 'debian', + 'centos', + 'fedora', + 'alpine', + 'rockylinux', + 'almalinux', + 'opensuse', + 'archlinux', + 'kalilinux', + 'nixos', + 'gentoo', + 'amazonlinux', + 'oraclelinux', + 'redhat' +]); + +/** + * The image's namespace for a plain `namespace/name` path (after an optional registry + * host), or '' otherwise. Deeper paths are ambiguous (org vs a mirror/proxy project like + * `harbor.corp/docker/grafana/promtail`), so they get none. Registry host rule is Docker's: + * a first segment containing `.` or `:`, or `localhost`. + * `victoriametrics/vmalert` -> `victoriametrics`, `linuxserver/sonarr` -> ''. + */ +export function imageNamespace(image: string): string { + const parts = (image || '').trim().toLowerCase().split('@')[0].split('/'); + if (parts.length > 1 && (/[.:]/.test(parts[0]) || parts[0] === 'localhost')) parts.shift(); + if (parts.length !== 2 || DISTRIBUTOR_NAMESPACES.has(parts[0])) return ''; + return parts[0]; +} + /** Drop separators so `thelounge` can match the reference `the-lounge`. */ function stripSeparators(s: string): string { return s.replace(/[-_]/g, ''); @@ -115,9 +161,66 @@ export function matchSelfhstByName( return resolveBase(containerNameBase(name), knownRefs, separatorless); } +/** + * Last-resort match on a `namespace/name` image, in two steps. + * + * First the vendor-qualified name (`apache/superset` -> `apache-superset`), which is how + * selfh.st names an app whose vendor publishes several. Only if that misses does the bare + * namespace count, and then only when the vendor looks like a single-product one: a + * namespace that also publishes OTHER apps would paint every one of them with the same + * vendor logo, and the matcher's whole contract is never to show a wrong one. + */ +export function matchSelfhstByNamespace( + image: string, + knownRefs: Set, + separatorless: Map = buildSeparatorlessMap(knownRefs) +): string | null { + const ns = imageNamespace(image); + if (!ns) return null; + + const qualified = resolveBase(`${ns}-${imageBasename(image)}`, knownRefs, separatorless); + if (qualified) return qualified; + + if (!imageIsTheVendor(ns, image)) return null; + return resolveBase(ns, knownRefs, separatorless); +} + +/** + * Whether the image IS the vendor's own product rather than one of several it publishes. + * + * True when the image name is the vendor's name or grows out of it - + * `home-assistant/home-assistant`, `victoriametrics/victoria-logs`, `nixos/nix`. False for + * `elastic/filebeat` or `mailu/postfix`, where the vendor logo would say nothing about + * the container and would be the same for every one of its siblings. + */ +function imageIsTheVendor(ns: string, image: string): boolean { + const bare = (v: string) => v.replace(/[^a-z0-9]/g, ''); + const vendor = bare(ns); + const app = bare(imageBasename(image)); + if (!vendor || !app) return false; + + // One is the other, or one grows out of it: home-assistant/home-assistant, + // victoriametrics/victoria-logs, nixos/nix. + if (app.startsWith(vendor) || vendor.startsWith(app)) return true; + + // Or they share enough of a beginning to be the same brand. Four letters is the + // line that lets victoriametrics/vmalert through while keeping mailu/postfix and + // elastic/filebeat out, where the image names nothing the vendor is called. + return sharedPrefix(vendor, app) >= 4; +} + +/** How many letters two names begin with in common. */ +function sharedPrefix(a: string, b: string): number { + const max = Math.min(a.length, b.length); + let n = 0; + while (n < max && a[n] === b[n]) n++; + return n; +} + + /** * Build a memoized matcher bound to a manifest's reference set. Resolves by image first - * (authoritative), then falls back to the container name when the image did not match. + * (authoritative), then the container name, then the image namespace. * Memoized per (image, name) pair so a list of 100 containers costs one lookup per unique * pair, not per render. */ @@ -131,7 +234,8 @@ export function createSelfhstMatcher( if (cache.has(key)) return cache.get(key)!; const ref = matchSelfhstRef(image, knownRefs, separatorless) ?? - matchSelfhstByName(name, knownRefs, separatorless); + matchSelfhstByName(name, knownRefs, separatorless) ?? + matchSelfhstByNamespace(image, knownRefs, separatorless); cache.set(key, ref); return ref; }; diff --git a/src/lib/utils/stale-container-id.ts b/src/lib/utils/stale-container-id.ts new file mode 100644 index 000000000..9ea8f182e --- /dev/null +++ b/src/lib/utils/stale-container-id.ts @@ -0,0 +1,33 @@ +/** + * Finding a container whose id has changed under a stale selection. + * + * An update recreates the container with a new id, so a list of containers to update + * that was gathered before one ran holds an id nothing answers to any more. The + * pending row records the NAME, which survives a recreate - the same thing the + * auto-update schedule identifies a container by - so the container is still + * reachable when the id alone is not. + */ + +export type Candidate = { id: string; name: string }; + +/** + * The container this id meant, by id first and by its recorded name second. + * + * Returns null when neither finds it, which is a container that is genuinely gone + * rather than merely recreated. + */ +export function resolveContainer( + containers: readonly T[], + containerId: string, + recordedName: string | null | undefined +): T | null { + const byId = containers.find((c) => c.id === containerId); + if (byId) return byId; + if (!recordedName) return null; + return containers.find((c) => c.name === recordedName) ?? null; +} + +/** What to call a container that could not be found: its recorded name, or nothing. */ +export function labelForMissing(recordedName: string | null | undefined): string { + return recordedName?.trim() || 'unknown'; +} diff --git a/src/lib/utils/tags-core.ts b/src/lib/utils/tags-core.ts index bb35a7450..5a037d6ea 100644 --- a/src/lib/utils/tags-core.ts +++ b/src/lib/utils/tags-core.ts @@ -97,13 +97,18 @@ export function matchesTagFilter( : filterTagIds.every((id) => have.has(id)); } +/** Case-insensitive name order, tie-broken by id so it is total and stable. #1625 */ +export function compareTagNames(a: Tag, b: Tag): number { + return a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }) || a.id - b.id; +} + /** * Build a DataGrid group descriptor for an item's unique tag COMBINATION (used by * the containers/stacks "group by tag" mode). Returns null for an untagged item so - * it falls into the trailing "Untagged" bucket. The combination key is the sorted - * tag ids joined with '+', so a container tagged prod+infra forms its own group, - * distinct from just prod. Order weight puts fewer-tag groups first, tie-broken by - * first tag id, so ordering is stable. + * it falls into the trailing "Untagged" bucket. The key is the numerically sorted + * tag ids joined with '+' (stable across renames, so collapsed state survives); the + * label, colour and icons follow tag-name order. Order weight puts fewer-tag groups + * first; groupData breaks ties by label, so groups read alphabetically. */ export function tagGroupDescriptor(tags: Tag[]): { key: string; @@ -113,12 +118,12 @@ export function tagGroupDescriptor(tags: Tag[]): { order: number; } | null { if (!tags.length) return null; - const sorted = tags.slice().sort((a, b) => a.id - b.id); + const byName = tags.slice().sort(compareTagNames); return { - key: sorted.map((t) => t.id).join('+'), - label: sorted.map((t) => t.name).join(' + '), - color: tagHex(sorted[0].color), - icons: sorted.map((t) => t.icon ?? null), - order: sorted.length * 1e6 + sorted[0].id + key: tags.map((t) => t.id).sort((a, b) => a - b).join('+'), + label: byName.map((t) => t.name).join(' + '), + color: tagHex(byName[0].color), + icons: byName.map((t) => t.icon ?? null), + order: tags.length }; } 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/+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/+page.svelte b/src/routes/+page.svelte index 2a216a07f..b5c62d420 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -25,6 +25,7 @@ import MultiSelectFilter from '$lib/components/MultiSelectFilter.svelte'; import { appSettings } from '$lib/stores/settings'; import { mergePartialStats, definedPartialForStore } from '$lib/utils/merge-partial-stats'; + import { diskSegmentPath, type DiskSegmentKey } from '$lib/utils/disk-segment-path'; const LABEL_FILTER_STORAGE_KEY = 'dockhand-dashboard-label-filter'; @@ -664,6 +665,16 @@ } } + // Handle disk usage segment click - select environment and open that resource's page + function handleDiskClick(envId: number, key: DiskSegmentKey) { + const tile = getTileById(envId); + const route = diskSegmentPath(key); + if (tile?.stats && route) { + currentEnvironment.set({ id: envId, name: tile.stats.name }); + goto(route); + } + } + function toggleLocked() { locked = !locked; dashboardPreferences.setLocked(locked); @@ -1200,6 +1211,7 @@ width={2} height={Math.max(item.h, 2)} oneventsclick={() => handleEventsClick(tile.stats!.id)} + ondiskclick={(key) => handleDiskClick(tile.stats!.id, key)} showStacksBreakdown={false} /> @@ -1235,7 +1247,7 @@ /> {:else if tile.stats} - handleEventsClick(tile.stats!.id)} /> + handleEventsClick(tile.stats!.id)} ondiskclick={(key) => handleDiskClick(tile.stats!.id, key)} /> {/if} {/if} {/snippet} 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/auth/logout/+server.ts b/src/routes/api/auth/logout/+server.ts index c0e4b4d03..cc609d22f 100644 --- a/src/routes/api/auth/logout/+server.ts +++ b/src/routes/api/auth/logout/+server.ts @@ -1,6 +1,6 @@ import { json } from '@sveltejs/kit'; import type { RequestHandler } from '@sveltejs/kit'; -import { destroySession } from '$lib/server/auth'; +import { destroySession, getOidcLogoutRedirect, getSessionOidcProvider } from '$lib/server/auth'; import { authorize } from '$lib/server/authorize'; import { auditAuth } from '$lib/server/audit'; import { getClientIp } from '$lib/server/client-ip'; @@ -8,25 +8,37 @@ import { getClientIp } from '$lib/server/client-ip'; /** * @openapi * summary: Destroy the current session (clears the dockhand_session cookie) - * resp-200: {success:boolean!} + * description: When the session came from an OIDC provider that publishes an end-session endpoint, the response carries a logoutUrl the caller should navigate to so the provider ends its own session too. Never present when the instance sets OIDC_END_SESSION=false. + * resp-200: {success:boolean!, logoutUrl:string} * resp-200-example: {"success":true} * resp-500: Unexpected error while destroying the session */ export const POST: RequestHandler = async (event) => { - const { cookies } = event; + const { cookies, url } = event; try { // Get current user before destroying session for audit log const auth = await authorize(cookies); const username = auth.user?.username || 'unknown'; const clientIp = getClientIp(event); + // Which provider issued this session, read while the session row still exists. + // Signing out of Dockhand alone leaves the provider's session open, so the + // next sign-in walks straight back in without being asked (#562, #1318). + const providerName = await getSessionOidcProvider(cookies); + + // Signed out here FIRST, before anything reaches for the network: a provider + // that hangs or errors must never be able to leave somebody logged in. await destroySession(cookies); + + // Back to the form, not to auto-login: a page that sends somebody who just + // signed out straight to a provider with a live session signs them back in. + const logoutUrl = await getOidcLogoutRedirect(providerName, `${url.origin}/login?local=1`); console.log(`[Auth] Logout: user=${username} ip=${clientIp}`); // Audit log await auditAuth(event, 'logout', username); - return json({ success: true }); + return json(logoutUrl ? { success: true, logoutUrl } : { success: true }); } catch (error) { console.error('Logout error:', error); return json({ error: 'Logout failed' }, { status: 500 }); diff --git a/src/routes/api/auth/oidc/[id]/initiate/+server.ts b/src/routes/api/auth/oidc/[id]/initiate/+server.ts index 9fb4fc1e3..bf8814237 100644 --- a/src/routes/api/auth/oidc/[id]/initiate/+server.ts +++ b/src/routes/api/auth/oidc/[id]/initiate/+server.ts @@ -1,21 +1,30 @@ import { json, redirect } from '@sveltejs/kit'; import type { RequestHandler } from '@sveltejs/kit'; -import { buildOidcAuthorizationUrl, isAuthEnabled } from '$lib/server/auth'; +import { buildOidcAuthorizationUrl, isAuthEnabled, setLoginStateCookie } from '$lib/server/auth'; import { getOidcConfig } from '$lib/server/db'; import { safeRedirectOrRoot } from '$lib/utils/safe-redirect'; +/** + * Send the browser back to the login form with the reason. + * + * This endpoint is reached by navigation, not fetch, so a JSON error body would be + * rendered as the page - and with auto-login on, reloading it just repeats the + * failure. `local=1` keeps the form up instead of bouncing straight out again. + */ +function backToLogin(message: string) { + return redirect(302, `/login?local=1&error=${encodeURIComponent(message)}`); +} + // GET /api/auth/oidc/[id]/initiate - Start OIDC authentication flow /** * @openapi * summary: Start the OIDC login flow for a provider — on success throws a 302 redirect to the IdP authorization URL * path: id:integer! Numeric id of the OIDC provider (from GET /api/auth/oidc) * query: redirect:string Post-login destination path to return to (defaults to /) - * resp-302: Redirect to the IdP's authorization URL + * resp-302: Redirect to the IdP's authorization URL, or back to /login?local=1&error=... when the provider is unknown, disabled or unreachable * resp-400: Authentication is not enabled, or the configuration id is invalid - * resp-404: OIDC provider not found or disabled - * resp-500: Failed to build the authorization URL / initiate SSO */ -export const GET: RequestHandler = async ({ params, url }) => { +export const GET: RequestHandler = async ({ params, url, cookies, request }) => { // Check if auth is enabled if (!await isAuthEnabled()) { return json({ error: 'Authentication is not enabled' }, { status: 400 }); @@ -32,15 +41,19 @@ export const GET: RequestHandler = async ({ params, url }) => { try { const config = await getOidcConfig(id); if (!config || !config.enabled) { - return json({ error: 'OIDC provider not found or disabled' }, { status: 404 }); + throw backToLogin('OIDC provider not found or disabled'); } const result = await buildOidcAuthorizationUrl(id, redirectUrl); if ('error' in result) { - return json({ error: result.error }, { status: 500 }); + throw backToLogin(result.error); } + // The login travels with the browser, so the callback can land on any + // instance behind the proxy and still be resumable. + setLoginStateCookie(cookies, result.loginState, request); + // Redirect to the IdP throw redirect(302, result.url); } catch (error: any) { @@ -49,7 +62,7 @@ export const GET: RequestHandler = async ({ params, url }) => { throw error; } console.error('Failed to initiate OIDC:', error); - return json({ error: error.message || 'Failed to initiate SSO' }, { status: 500 }); + throw backToLogin(error.message || 'Failed to initiate SSO'); } }; @@ -65,7 +78,7 @@ export const GET: RequestHandler = async ({ params, url }) => { * resp-404: OIDC provider not found or disabled * resp-500: Failed to build the authorization URL / initiate SSO */ -export const POST: RequestHandler = async ({ params, request }) => { +export const POST: RequestHandler = async ({ params, request, cookies }) => { // Check if auth is enabled if (!await isAuthEnabled()) { return json({ error: 'Authentication is not enabled' }, { status: 400 }); @@ -91,6 +104,10 @@ export const POST: RequestHandler = async ({ params, request }) => { return json({ error: result.error }, { status: 500 }); } + // Same cookie as the redirect form of this endpoint: the browser is about to + // follow result.url itself, and has to come back with the login it started. + setLoginStateCookie(cookies, result.loginState, request); + return json({ url: result.url }); } catch (error: any) { console.error('Failed to get OIDC authorization URL:', error); 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/auth/providers/+server.ts b/src/routes/api/auth/providers/+server.ts index d0a95d4b6..18d6ba77d 100644 --- a/src/routes/api/auth/providers/+server.ts +++ b/src/routes/api/auth/providers/+server.ts @@ -1,4 +1,5 @@ import { json } from '@sveltejs/kit'; +import { autoLoginTarget } from '$lib/utils/oidc-autologin'; import type { RequestHandler } from '@sveltejs/kit'; import { isAuthEnabled, getEnabledLdapConfigs, getEnabledOidcConfigs } from '$lib/server/auth'; import { getAuthSettings } from '$lib/server/db'; @@ -8,7 +9,8 @@ import { isEnterprise } from '$lib/server/license'; /** * @openapi * summary: List the authentication providers offered on the login page (local, LDAP, OIDC), plus the default provider - * resp-200: {providers:array<{id:string!, name:string!, type:string!, initiateUrl:string}>, defaultProvider:string} + * resp-200: {providers:array<{id:string!, name:string!, type:string!, initiateUrl:string}>, defaultProvider:string, autoLoginUrl:string} + * resp-200-desc: autoLoginUrl is present only when OIDC_AUTOLOGIN is set and exactly one OIDC provider is enabled; the login page then goes straight there * resp-200-example: {"providers":[{"id":"local","name":"Local","type":"local"},{"id":"oidc:1","name":"Authentik","type":"oidc","initiateUrl":"/api/auth/oidc/1/initiate"}],"defaultProvider":"local"} */ export const GET: RequestHandler = async () => { @@ -51,9 +53,21 @@ export const GET: RequestHandler = async () => { }); } + // Whether the login page should go straight to the provider (OIDC_AUTOLOGIN). + // The error and ?local=1 escape hatches are the page's to apply - it is the + // only side that knows how the last attempt went. + const autoLoginUrl = autoLoginTarget({ + enabled: process.env.OIDC_AUTOLOGIN === 'true', + oidcInitiateUrls: providers + .filter((p) => p.type === 'oidc') + .map((p) => p.initiateUrl) + .filter((u): u is string => !!u) + }); + return json({ providers, - defaultProvider: settings.defaultProvider || 'local' + defaultProvider: settings.defaultProvider || 'local', + ...(autoLoginUrl ? { autoLoginUrl } : {}) }); } catch (error) { console.error('Failed to get auth providers:', error); 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/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/containers/batch-update-stream/+server.ts b/src/routes/api/containers/batch-update-stream/+server.ts index 5b6ebb407..e4aefa852 100644 --- a/src/routes/api/containers/batch-update-stream/+server.ts +++ b/src/routes/api/containers/batch-update-stream/+server.ts @@ -14,7 +14,8 @@ import { } from '$lib/server/docker'; import { auditContainer } from '$lib/server/audit'; import { getScannerSettings, scanImage } from '$lib/server/scanner'; -import { saveVulnerabilityScan, removePendingContainerUpdate, type VulnerabilityCriteria } from '$lib/server/db'; +import { saveVulnerabilityScan, removePendingContainerUpdateByName, getPendingContainerUpdates, type VulnerabilityCriteria } from '$lib/server/db'; +import { resolveContainer, labelForMissing } from '$lib/utils/stale-container-id'; import { parseImageNameAndTag, shouldBlockUpdate, combineScanSummaries, isSystemContainer } from '$lib/server/scheduler/tasks/update-utils'; import { isUpdateDisabledByLabel } from '$lib/server/container-labels'; import { recreateContainer } from '$lib/server/scheduler/tasks/container-update'; @@ -70,6 +71,17 @@ export interface UpdateProgress { }>; } +/** id -> name from the pending rows, read once for the whole batch. */ +async function pendingNames(envId: number | null): Promise> { + if (envId == null) return new Map(); + try { + const rows = await getPendingContainerUpdates(envId); + return new Map(rows.map((r) => [r.containerId, r.containerName])); + } catch { + return new Map(); + } +} + /** * Batch update containers with streaming progress. * Expects JSON body: { containerIds: string[], vulnerabilityCriteria?: VulnerabilityCriteria } @@ -135,6 +147,8 @@ export const POST: RequestHandler = async (event) => { message: `Starting update of ${containerIds.length} container${containerIds.length > 1 ? 's' : ''}${shouldScan ? ' with vulnerability scanning' : ''}` }); + const namesById = await pendingNames(envIdNum ?? null); + // Process containers sequentially for (let i = 0; i < containerIds.length; i++) { const containerId = containerIds[i]; @@ -143,13 +157,17 @@ export const POST: RequestHandler = async (event) => { try { // Find container const containers = await listContainers(true, envIdNum); - const container = containers.find(c => c.id === containerId); + // An update recreates the container under a new id, so a selection made + // before one ran holds an id that no longer exists. The pending row keeps + // the NAME, which survives, so the same container is still reachable. + const pendingName = namesById.get(containerId) ?? null; + const container = resolveContainer(containers, containerId, pendingName); if (!container) { sendData({ type: 'progress', containerId, - containerName: 'unknown', + containerName: labelForMissing(pendingName), step: 'failed', current: i + 1, total: containerIds.length, @@ -161,9 +179,13 @@ export const POST: RequestHandler = async (event) => { } containerName = container.name; + // Everything from here talks to the daemon, so it uses the id the + // container HAS. `containerId` is what the caller selected and stays the + // key in the progress events the UI is keyed on. + const liveId = container.id; // Get full container config - const inspectData = await inspectContainer(containerId, envIdNum) as any; + const inspectData = await inspectContainer(liveId, envIdNum) as any; const config = inspectData.Config; const imageName = config.Image; const currentImageId = inspectData.Image; @@ -469,7 +491,7 @@ export const POST: RequestHandler = async (event) => { }); }; - let newContainerId = containerId; + let newContainerId = liveId; sendData({ type: 'progress', @@ -525,9 +547,11 @@ export const POST: RequestHandler = async (event) => { }); successCount++; - // Clear pending update indicator from database + // Clear pending update indicator from database. By name, because the + // row may have been written against an id this container no longer has - + // which is how a spent row survives to fail the next batch. if (envIdNum) { - await removePendingContainerUpdate(envIdNum, containerId).catch(() => { + await removePendingContainerUpdateByName(envIdNum, containerName).catch(() => { // Ignore errors - record may not exist }); } diff --git a/src/routes/api/containers/stats/+server.ts b/src/routes/api/containers/stats/+server.ts index a4fd9d3bc..83f433ad9 100644 --- a/src/routes/api/containers/stats/+server.ts +++ b/src/routes/api/containers/stats/+server.ts @@ -70,7 +70,11 @@ export const GET: RequestHandler = async ({ url, cookies }) => { return json({ error: 'Container not found' }, { status: 404 }); } - // Get stats for each running container (in parallel with timeout) + // All at once, deliberately. The daemon samples CPU twice per call, so each + // one costs about two seconds no matter what; anything narrower than "every + // container" multiplies that into the caller's wait. The collection worker + // paces its own fan-out because it runs in the background, where waiting + // is free. const statsPromises = runningContainers.map(async (container) => { try { const stats = await withTimeout( diff --git a/src/routes/api/containers/stats/stream/+server.ts b/src/routes/api/containers/stats/stream/+server.ts index bb0d57e3f..8e6981c7f 100644 --- a/src/routes/api/containers/stats/stream/+server.ts +++ b/src/routes/api/containers/stats/stream/+server.ts @@ -69,6 +69,11 @@ export const GET: RequestHandler = async ({ url, cookies }) => { ); const runningContainers = containers.filter(c => c.state === 'running'); + // All at once, deliberately. The daemon samples CPU twice per call, so + // each one costs about two seconds no matter what; anything narrower + // than "every container" multiplies that into the wait for a table the + // user is watching. The collection worker paces its own fan-out because + // it runs in the background, where waiting is free. const statsPromises = runningContainers.map(async (container) => { try { const stats = await withTimeout( diff --git a/src/routes/api/dashboard/stats/stream/+server.ts b/src/routes/api/dashboard/stats/stream/+server.ts index f612dbbdf..7823ae8f1 100644 --- a/src/routes/api/dashboard/stats/stream/+server.ts +++ b/src/routes/api/dashboard/stats/stream/+server.ts @@ -480,7 +480,7 @@ async function getEnvironmentStatsProgressive( onPartialUpdate({ id: env.id, - topContainers: [...envStats.topContainers], + topContainers: [...envStats.topContainers!], loading: { ...envStats.loading! } }); 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/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/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/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/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/notifications/trigger-test/+server.ts b/src/routes/api/notifications/trigger-test/+server.ts index a3046af54..40ff831b4 100644 --- a/src/routes/api/notifications/trigger-test/+server.ts +++ b/src/routes/api/notifications/trigger-test/+server.ts @@ -1,22 +1,37 @@ import { json, type RequestHandler } from '@sveltejs/kit'; import { sendEventNotification, sendEnvironmentNotification } from '$lib/server/notifications'; import { NOTIFICATION_EVENT_TYPES, type NotificationEventType } from '$lib/server/db'; +import { authorize } from '$lib/server/authorize'; /** - * Test endpoint to trigger notifications for any event type. - * This is intended for development/testing purposes only. + * Trigger a notification for a given event type. + * + * Named a test helper, but it delivers for real: whatever channels the environment + * has configured receive the message, so it is gated like sending is. * * @openapi * summary: Trigger a real notification for a given event type (development/testing helper) - * description: environmentId from GET /api/environments. + * description: Delivers to the configured channels for real, so it requires notifications:test and access to the environment. environmentId from GET /api/environments. * body: {eventType:string!, environmentId:integer, payload:{title:string!, message:string!, type:string}} * body-example: {"eventType":"container_unhealthy","environmentId":1,"payload":{"title":"Container unhealthy","message":"web-1 is unhealthy","type":"warning"}} * resp-200: {success:boolean!, sent:integer, eventType:string!, environmentId:integer} * resp-200-example: {"success":true,"sent":1,"eventType":"container_unhealthy","environmentId":1} * resp-400: eventType required, payload with title and message required, unknown event type, or environmentId missing for a non-system event + * resp-401: Not authenticated + * resp-403: Permission denied (missing notifications:test, or no access to the environment) * resp-500: Unknown error while sending the notification */ -export const POST: RequestHandler = async ({ request }) => { +export const POST: RequestHandler = async ({ request, cookies }) => { + const auth = await authorize(cookies); + if (auth.authEnabled && !auth.isAuthenticated) { + return json({ error: 'Authentication required' }, { status: 401 }); + } + // Anyone who reaches this can send mail, chat messages and phone alerts from this + // instance to addresses they cannot see, so it asks for the same permission that + // testing a channel does. + const denied = await auth.requirePermission('notifications', 'test'); + if (denied) return denied; + try { const body = await request.json(); const { eventType, environmentId, payload } = body; @@ -29,6 +44,19 @@ export const POST: RequestHandler = async ({ request }) => { return json({ error: 'payload with title and message is required' }, { status: 400 }); } + // The environment arrives in the body, so it is checked here rather than beside + // the permission: notifying through an environment the caller cannot see would + // reach channels they were never shown. A non-number is refused rather than + // waved past, because the branch that picks the target below only tests for a + // truthy value - "5" would slip the check and still deliver. + if (environmentId !== undefined && environmentId !== null) { + if (!Number.isInteger(environmentId)) { + return json({ error: 'environmentId must be a number' }, { status: 400 }); + } + const noAccess = await auth.requireEnvAccess(environmentId); + if (noAccess) return noAccess; + } + // Validate event type - NOTIFICATION_EVENT_TYPES is array of {id, label, ...} const validEventIds = NOTIFICATION_EVENT_TYPES.map(e => e.id); if (!validEventIds.includes(eventType)) { 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/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/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/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/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/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/[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/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/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/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} /> (null); + /** + * The form was opened by "duplicate". A run-once from here would look up the + * config by destination and overwrite the very schedule being copied, so that + * action is not offered: copying a schedule means saving one. + */ + let duplicating = $state(false); let showAdvanced = $state(false); // Live restic progress for a manual backup run (Run once / Save & run now, and @@ -211,6 +217,7 @@ function startNewConfig() { editingConfig = null; isNew = true; + duplicating = false; editDestinationId = destinations[0]?.id || 0; editEnabled = true; editSchedule = '0 2 * * *'; @@ -252,9 +259,24 @@ showAdvanced = false; } + /** Open a NEW-schedule form prefilled from an existing config (#1578). */ + function startDuplicateConfig(cfg: BackupConfig) { + startEditConfig(cfg); + editingConfig = null; + isNew = true; + duplicating = true; + editEnabled = duplicateStartsEnabled(cfg); + editDestinationId = pickDuplicateDestinationId( + cfg.destinationId, + destinations.map((d) => d.id), + configs.map((c) => c.destinationId) + ); + } + function cancelEdit() { editingConfig = null; isNew = false; + duplicating = false; } /** @@ -383,7 +405,7 @@ if (runsBackup) progressStatus = 'success'; if (action === 'save') toast.success(isNew ? 'Backup schedule added' : 'Backup schedule updated'); else toast.success(`Backup completed for ${containerName}`); - editingConfig = null; isNew = false; + editingConfig = null; isNew = false; duplicating = false; fetchConfigs(); // 'save-run'/'run-once' just wrote a snapshot — reload the list so it appears // without a manual refresh. A plain 'save' writes none, so skip it. @@ -618,6 +640,9 @@ + {:else} - + {#if !duplicating} + + {/if} - - - - + + {#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/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/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/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/src/routes/settings/notifications/NotificationModal.svelte b/src/routes/settings/notifications/NotificationModal.svelte index 655cea66f..6a5843157 100644 --- a/src/routes/settings/notifications/NotificationModal.svelte +++ b/src/routes/settings/notifications/NotificationModal.svelte @@ -469,7 +469,7 @@ zabbixs://hostname/api_jsonrpc.php?token=TOKEN&host=HOST&key=ITEM_KEY"

- Need a provider that is not in the list (Matrix, Nextcloud, Pushbullet, Home Assistant, ...)? Run a caronc/apprise-api server, configure the provider there, and point Dockhand at it with apprise://host/key (or apprises:// for TLS). Every provider Apprise supports is then reachable. + Need a provider that is not in the list (Matrix, Nextcloud, Pushbullet, Home Assistant, ...)? Run a caronc/apprise-api server, configure the provider there, and point Dockhand at it with apprise://host/key (or apprises:// for TLS). Every provider Apprise supports is then reachable. If the Apprise server is behind basic auth, use apprise://user:pass@host/key (percent-encode special characters in the password).

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")} +