diff --git a/agents.md b/agents.md index db9f5fe..b02a94c 100644 --- a/agents.md +++ b/agents.md @@ -115,3 +115,9 @@ This file is the single source of truth for codebase obstacles, oddities, and th **Obstacle:** Theme switching and persisted localStorage theme state conflicted with the current Firebreak brand pass, especially on the cinematic landing page. **Solution/Workaround:** Force the root document to use the `.dark` token set and remove the theme toggle UI/scripts. **Preference:** Keep semantic dark tokens; only reintroduce theme switching after the dark Firebreak identity is stable. + +### Forgejo PAT could be sent over remote HTTP +**Area:** cli/src/utils/forgejo-checks.ts, cli/src/commands/config/init.ts +**Obstacle:** CLI setup and verification accepted remote `http://` Forgejo URLs, allowing the PAT to be sent without transport encryption. +**Solution/Workaround:** Centralize Forgejo URL validation, require HTTPS for non-local endpoints, and allow HTTP only for the documented `forgejo` service and loopback hosts. Validate again inside authenticated checks before constructing requests. +**Preference:** Reject insecure remote transport instead of adding an acknowledgement flag. diff --git a/cli/src/commands/config/init.ts b/cli/src/commands/config/init.ts index 03e5fd5..130fb9c 100644 --- a/cli/src/commands/config/init.ts +++ b/cli/src/commands/config/init.ts @@ -5,36 +5,9 @@ import * as c from "yoctocolors"; import * as p from "@clack/prompts"; import { generateEnvFile, SECRET_ENV_KEYS } from "../../utils/config.js"; +import { validateForgejoBaseUrl } from "../../utils/forgejo-checks.js"; import { CommandConfig } from "../../cli-router.js"; -function validateHttpUrlOrHost( - value: string | undefined, - label: string, -): string | undefined { - const trimmed = value?.trim(); - if (!trimmed) return `${label} is required`; - - const hasProtocol = /^[a-z]+:\/\//i.test(trimmed); - const isHttp = /^https?:\/\//i.test(trimmed); - - if (hasProtocol && !isHttp) { - return `${label} must use http:// or https://`; - } - - if (!hasProtocol) { - return `${label} must include a scheme — e.g. http://forgejo:3000 or https://git.example.com`; - } - - try { - const url = new URL(trimmed); - if (!url.hostname) return `${label} must include a domain or hostname`; - } catch { - return `${label} must be a valid URL`; - } - - return undefined; -} - async function init(opts: { output?: string }) { p.intro(c.bold("firebreak config init")); p.note( @@ -118,7 +91,7 @@ async function init(opts: { output?: string }) { message: "Forgejo API URL (how Firebreak reaches Forgejo, not your browser URL)?", placeholder: "http://forgejo:3000", - validate: (v) => validateHttpUrlOrHost(v, "Forgejo base URL"), + validate: (v) => validateForgejoBaseUrl(v), }), forgejoPat: () => p.password({ @@ -178,7 +151,7 @@ async function init(opts: { output?: string }) { ].join("\n"), ); p.log.success( - "Done. Verify firebreak's connection using the 'verify' command.\nOnce verified, deploy!", + "Done. Verify Firebreak's connection using 'firebreak config verify'.\nOnce verified, deploy!", ); } catch (error) { s.stop(); diff --git a/cli/src/commands/verify.ts b/cli/src/commands/verify.ts index 8da5c47..373ba88 100644 --- a/cli/src/commands/verify.ts +++ b/cli/src/commands/verify.ts @@ -162,7 +162,7 @@ async function runForgejoChecks( async function verifyCommand(opts: VerifyOptions): Promise { const isDiagnostic = opts.diagnostic ?? false; - p.intro(c.bold("firebreak verify")); + p.intro(c.bold("firebreak config verify")); const config = loadConfig(); if (!config) { diff --git a/cli/src/utils/forgejo-checks.ts b/cli/src/utils/forgejo-checks.ts index 95cca86..dfb57af 100644 --- a/cli/src/utils/forgejo-checks.ts +++ b/cli/src/utils/forgejo-checks.ts @@ -1,5 +1,13 @@ const TIMEOUT = 5000; +const LOCAL_HTTP_HOSTS = new Set([ + "forgejo", + "localhost", + "127.0.0.1", + "::1", + "[::1]", +]); + export type CheckResult = { name: string; success: boolean; @@ -18,6 +26,42 @@ export function createCheckResult( return result; } +export function validateForgejoBaseUrl( + value: string | undefined, + label = "Forgejo base URL", +): string | undefined { + const trimmed = value?.trim(); + if (!trimmed) return `${label} is required`; + + const hasProtocol = /^[a-z]+:\/\//i.test(trimmed); + const isHttp = /^https?:\/\//i.test(trimmed); + + if (hasProtocol && !isHttp) { + return `${label} must use http:// or https://`; + } + + if (!hasProtocol) { + return `${label} must include a scheme — e.g. http://forgejo:3000 or https://git.example.com`; + } + + try { + const url = new URL(trimmed); + if (!url.hostname) return `${label} must include a domain or hostname`; + + const isLocalHttp = + url.protocol === "http:" && + (LOCAL_HTTP_HOSTS.has(url.hostname) || + url.hostname.endsWith(".localhost")); + if (url.protocol === "http:" && !isLocalHttp) { + return `${label} must use https:// unless it is a local Forgejo endpoint`; + } + } catch { + return `${label} must be a valid URL`; + } + + return undefined; +} + async function fetchWithTimeout( url: string, init: RequestInit, @@ -47,6 +91,11 @@ function getErrorMessage(error: unknown, context: string): string { export async function checkForgejoReachability( baseUrl: string, ): Promise { + const validationError = validateForgejoBaseUrl(baseUrl); + if (validationError) { + return createCheckResult("Forgejo", false, validationError); + } + try { const response = await fetchWithTimeout(baseUrl, { method: "GET" }); if (response.ok) { @@ -70,6 +119,11 @@ export async function checkRepoAccess( baseUrl: string, pat: string, ): Promise { + const validationError = validateForgejoBaseUrl(baseUrl); + if (validationError) { + return createCheckResult("Forgejo", false, validationError); + } + try { const responses = await Promise.all([ fetchWithTimeout(`${baseUrl}/api/v1/user`, { diff --git a/examples/firebreak.config.example.json b/examples/firebreak.config.example.json deleted file mode 100644 index d534c5e..0000000 --- a/examples/firebreak.config.example.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "forgejoBaseUrl": "http://forgejo:3000" -}