Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.
TypeScript browser-shellcontrol-planefile-managerhomelabmobile-firstnextjsptyreactself-hostedserver-managementshadcn-uisysadmintailwindcssterminaltypescriptvpsweb-desktopweb-terminal
fix(security): read only .message from a caught error, never stringify it
Abdurrahman Fakhrul8c450f41a6c2
diff --git a/lib/agent/organization-service.ts b/lib/agent/organization-service.tsindex 0a042833..b7155127 100644--- a/lib/agent/organization-service.ts+++ b/lib/agent/organization-service.ts@@ -1,4 +1,5 @@import { json } from "@/lib/api/route";+import { clientMessage, errorMessage } from "@/lib/host/error-message";import { isOrganizationFlowAction } from "@/lib/contracts/organization-flow";import { getSessionContext } from "@/lib/auth/require-session";import { roleAtLeast } from "@/lib/auth/roles";@@ -18,11 +19,14 @@ import { organizationRuntime, resolveOrganizationSeat } from "@/lib/agent/organiconst headers = { "Cache-Control": "private, no-store" };+// `fail` is called two ways in this file: with a caught error, and with a literal code the+// route chose ("operator_required", "owner_required"). Keeping them on separate paths is what+// stops a propagated string from reaching a response body — see lib/host/error-message.ts.const fail = (error: unknown, status = 400) =>- json(- { error: error instanceof Error ? error.message.slice(0, 500) : String(error).slice(0, 500) },- { status, headers },- );+ json({ error: errorMessage(error) }, { status, headers });++const failCode = (code: string, status = 400) =>+ json({ error: clientMessage(code) }, { status, headers });async function auth(minimum: "viewer" | "operator" | "owner" = "viewer") {const context = await getSessionContext();@@ -32,7 +36,7 @@ async function auth(minimum: "viewer" | "operator" | "owner" = "viewer") {export async function getOrganizationResponse(req: Request): Promise<Response> {const session = await auth("viewer");- if (!session) return fail("unauthorized", 401);+ if (!session) return failCode("unauthorized", 401);try {const chart = await getOrganizationChart();const url = new URL(req.url);@@ -61,12 +65,12 @@ export async function getOrganizationResponse(req: Request): Promise<Response> {export async function handleOrganizationAction(req: Request): Promise<Response> {const session = await auth("operator");- if (!session) return fail("operator_required", 403);+ if (!session) return failCode("operator_required", 403);try {const body = await readSetupJson(req, 2 * 1024 * 1024);const action = String(body.action || "");if (rateLimited(`organization:${action}:${session.context.session.device_id}`, 40, 60_000)) {- return fail("rate_limited", 429);+ return failCode("rate_limited", 429);}const revision = String(body.expected_revision || "");if (!revision) throw new Error("expected_revision is required");@@ -82,7 +86,7 @@ export async function handleOrganizationAction(req: Request): Promise<Response>} else if (isOrganizationFlowAction(action)) {chart = await mutateOrganizationFlow(revision, action, (body.data ?? {}) as Record<string, unknown>, { principal: session.principal });} else if (action === "replace") {- if (!roleAtLeast(session.context.role, "owner")) return fail("owner_required", 403);+ if (!roleAtLeast(session.context.role, "owner")) return failCode("owner_required", 403);chart = await replaceOrganization(revision, body.chart as never, "exec");} else {throw new Error("unknown organization action");diff --git a/lib/host/error-message.test.ts b/lib/host/error-message.test.tsnew file mode 100644index 00000000..a1e42e6f--- /dev/null+++ b/lib/host/error-message.test.ts@@ -0,0 +1,44 @@+import { describe, expect, it } from "vitest";+import { clientMessage, errorMessage } from "./error-message";++// These two helpers exist because `String(error)` is a taint source for+// js/stack-trace-exposure, not a sanitiser. The behaviour being pinned is the split: an Error+// contributes its message, a literal code passes through, and nothing else is stringified.+// Verified against the real query: a version that forwarded arbitrary strings re-opened both+// alerts, so `clientMessage` is not a convenience wrapper — it is the boundary.++describe("errorMessage", () => {+ it("returns the message of a real Error", () => {+ expect(errorMessage(new Error("store read failed"))).toBe("store read failed");+ });++ it("never stringifies a thrown non-Error", () => {+ // A thrown object whose toString would leak internals. Stringify it and a stack can ride along.+ const sneaky = { toString: () => "Error: boom\n at secret (/srv/app/lib/x.ts:1:1)" };+ expect(errorMessage(sneaky)).toBe("unexpected error");+ expect(errorMessage(undefined)).toBe("unexpected error");+ expect(errorMessage({ message: "looks like an error but is not" })).toBe("unexpected error");+ });++ it("does NOT forward a thrown string, which would re-introduce taint", () => {+ // The tempting version of this helper returns a thrown string verbatim. Against the real+ // CodeQL query that re-opens js/stack-trace-exposure, so the contract is: a string that+ // arrived as a value is not a client message.+ expect(errorMessage("Error: boom\n at x (/srv/app/lib/file.ts:1:1)")).toBe("unexpected error");+ });++ it("bounds the response body", () => {+ expect(errorMessage(new Error("x".repeat(900)))).toHaveLength(500);+ expect(errorMessage(new Error("x".repeat(900)), 20)).toHaveLength(20);+ });+});++describe("clientMessage", () => {+ it("passes a code the caller wrote", () => {+ expect(clientMessage("operator_required")).toBe("operator_required");+ });++ it("bounds the response body", () => {+ expect(clientMessage("y".repeat(900))).toHaveLength(500);+ });+});diff --git a/lib/host/error-message.ts b/lib/host/error-message.tsnew file mode 100644index 00000000..90f1161a--- /dev/null+++ b/lib/host/error-message.ts@@ -0,0 +1,28 @@+// A message safe to put in an HTTP response body.+//+// The reason this exists rather than `String(error)` at each call site: `String(error)` is a+// taint source, not a sanitiser. CodeQL's js/stack-trace-exposure reports it because a value+// that is not an Error serialises via its own `toString`, and the analysis cannot prove the+// value carries no stack, so it treats the whole result as stack-bearing. Two alerts sat+// permanently open on that: `String(error)` in lib/agent/organization-service.ts and+// lib/host/services.ts, surfacing through lib/api/route.ts.+//+// Reading `.message` off a value already narrowed by `instanceof Error` is the narrow, provable+// form: a property read whose name is not `stack`, which the query treats as a barrier.+//+// Two things this deliberately does NOT do, both verified against the real query:+// - it does not pass an arbitrary string through. Callers legitimately want `fail("operator_required")`,+// and for that there is `clientMessage` below, which only ever returns a literal the caller wrote.+// Forwarding a string that arrived as a value re-introduces the taint, because the analysis cannot+// tell a literal from a stringified exception.+// - it does not stringify anything. A thrown non-Error becomes a fixed string.+export function errorMessage(error: unknown, max = 500): string {+ const text = error instanceof Error ? error.message : "unexpected error";+ return text.slice(0, max);+}++/** A literal code chosen by the caller, for routes that answer with `fail("operator_required")`.+ * Separate from `errorMessage` so no path can hand a propagated string to a response body. */+export function clientMessage(code: string, max = 500): string {+ return code.slice(0, max);+}diff --git a/lib/host/services.ts b/lib/host/services.tsindex 0d4f6e24..c351daa6 100644--- a/lib/host/services.ts+++ b/lib/host/services.ts@@ -9,6 +9,7 @@ import type {SystemService,} from "@/lib/os-api/types";import { userBusEnv } from "@/lib/managed-apps/user-bus";+import { errorMessage } from "./error-message";import { childEnv } from "./child-env";import { HostError } from "./host-error";@@ -191,7 +192,7 @@ export async function serviceLogs(scope: ServiceScope, unit: string, limit = 120scope,entries: [],available: false,- diagnostic: error instanceof Error ? error.message : String(error),+ diagnostic: errorMessage(error),};}}