Repository navigation
fix: never send a stack trace in a server error response; an unavailable remote config is a 502 #1788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: never send a stack trace in a server error response; an unavailable remote config is a 502 #1788
Changes from all commits
41b454e
b378ee0
91235eb
236186b
a0d4696
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { NodeFileSystem } from "@effect/platform-node" | ||
| import { fileLogger } from "@opencode-ai/core/observability/logging" | ||
| import { NamedError } from "@opencode-ai/core/util/error" | ||
| import { Cause, Effect, Layer, Logger } from "effect" | ||
|
|
||
| // What the server's error handlers send back. For an error they don't recognise, a fixed message and a ref: never | ||
| // the error's own text, stack or file paths. The full error is recorded under the same ref (recordUnknownError). | ||
|
|
||
| /** HTTP status for an error the handlers know by name: a remote the server depends on failed (502). */ | ||
| export function upstreamStatus(name: string): 502 | undefined { | ||
| return name === "ConfigRemoteUnavailableError" ? 502 : undefined | ||
|
ralphstodomingo marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| // An indented `at …` line: how V8, JSC and Bun print every stack frame, with or without a source location | ||
| // (`at new Promise (<anonymous>)`, `at async Promise.all (index 0)`). | ||
| const FRAME = /^\s+at\s/ | ||
|
|
||
| export const UNEXPECTED = "Unexpected server error. Check server logs for details." | ||
|
ralphstodomingo marked this conversation as resolved.
Resolved
|
||
|
|
||
| /** A reference for one failure: the handler logs the error under it, and an unknown error's response carries it. */ | ||
| export function errorRef(): string { | ||
| return `err_${crypto.randomUUID().slice(0, 8)}` | ||
| } | ||
|
|
||
| /** A NamedError keeps its text in `data.message`; its own `message` is only its name. */ | ||
| function namedErrorMessage(err: unknown): string | undefined { | ||
| if (typeof err !== "object" || err === null || !("data" in err)) return undefined | ||
| const data = (err as { data?: unknown }).data | ||
| if (typeof data !== "object" || data === null) return undefined | ||
| const message = (data as { message?: unknown }).message | ||
| return typeof message === "string" ? message : undefined | ||
| } | ||
|
|
||
| /** | ||
| * An error's message without stack-frame lines (some errors carry frames in the message itself), for a client | ||
| * that has to tell a user why their own request failed, such as a session's error event. | ||
| */ | ||
| export function clientMessage(err: unknown): string { | ||
| const raw = namedErrorMessage(err) ?? (err instanceof Error ? err.message : String(err)) | ||
| const message = raw | ||
| .split("\n") | ||
| .filter((line) => !FRAME.test(line)) | ||
| .join("\n") | ||
| .trim() | ||
| return message || UNEXPECTED | ||
| } | ||
|
|
||
| /** The response body for an error no handler recognises: the fixed message and the ref it was logged under. */ | ||
| export function unknownErrorBody(ref: string) { | ||
| return new NamedError.Unknown({ message: UNEXPECTED, ref }).toObject() | ||
| } | ||
|
|
||
| const unknownErrorLog = Logger.layer([fileLogger()], { mergeWithExisting: false }).pipe( | ||
| Layer.provide(NodeFileSystem.layer), | ||
| ) | ||
|
|
||
| /** | ||
| * Record an error no handler recognises under its ref in opencode.log: the file the /api error middleware's refs go | ||
| * to, written whether or not --print-logs is on. Only the file, never stderr, which belongs to the TUI when it runs | ||
| * the server in-process. A failed write never fails the response. | ||
| */ | ||
| export function recordUnknownError(service: string, ref: string, error: unknown): Promise<void> { | ||
| return Effect.runPromise( | ||
| Effect.logError("unexpected error", { ref, cause: Cause.pretty(Cause.die(error)) }).pipe( | ||
| Effect.annotateLogs({ service }), | ||
| Effect.provide(unknownErrorLog), | ||
| ), | ||
| ).catch(() => undefined) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,12 +6,20 @@ import type { ContentfulStatusCode } from "hono/utils/http-status" | |
| import type { ErrorHandler } from "hono" | ||
| import { HTTPException } from "hono/http-exception" | ||
| import type { Log } from "../util/log" | ||
| // altimate_change start — client-safe error responses | ||
| import { NamedError as CoreNamedError } from "@opencode-ai/core/util/error" | ||
| import { errorRef, recordUnknownError, unknownErrorBody, upstreamStatus } from "../altimate/server/error-response" | ||
| // altimate_change end | ||
|
|
||
| export function errorHandler(log: Log.Logger): ErrorHandler { | ||
| return (err, c) => { | ||
| // altimate_change start — one log line per error, under the ref an unknown error's response carries | ||
| const ref = errorRef() | ||
| log.error("failed", { | ||
| ref, | ||
| error: err, | ||
| }) | ||
|
Comment on lines
+17
to
21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MAJOR (bug): the ref this response points to isn't recorded under default settings. const ref = errorRef()
log.error("failed", {
ref,
error: err,
})The unknown-error response now sends only a fixed message and a Suggested fix: always write referenced unknown errors to a sink that is kept, for example by bypassing the print gate for this one error line in serve mode while keeping the TUI isolated. Then add a test with the print flags unset that checks the response This is the same issue as in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same fix in a0d4696:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified fixed at a0d4696: |
||
| // altimate_change end | ||
| if (err instanceof NamedError) { | ||
| let status: ContentfulStatusCode | ||
| if (err instanceof NotFoundError) status = 404 | ||
|
|
@@ -25,9 +33,13 @@ export function errorHandler(log: Log.Logger): ErrorHandler { | |
| return c.json(new NamedError.Unknown({ message: err.message }).toObject(), { status: 400 }) | ||
| } | ||
| if (err instanceof HTTPException) return err.getResponse() | ||
| const message = err instanceof Error && err.stack ? err.stack : err.toString() | ||
| return c.json(new NamedError.Unknown({ message }).toObject(), { | ||
| status: 500, | ||
| }) | ||
| // altimate_change start — a NamedError from @opencode-ai/core (config, remote config) isn't an instance of this | ||
| // package's NamedError: answer with its own name and fields, never the stack trace below. | ||
| if (err instanceof CoreNamedError) { | ||
| return c.json(err.toObject(), { status: upstreamStatus(err.name) ?? 500 }) | ||
| } | ||
| // Never send an error's text or stack to the client: a fixed message and a ref, recorded in full first. | ||
| return recordUnknownError("server", ref, err).then(() => c.json(unknownErrorBody(ref), { status: 500 })) | ||
| // altimate_change end | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,9 @@ import { basicAuth } from "hono/basic-auth" | |
| import z from "zod" | ||
| import { Provider } from "../provider/provider" | ||
| import { NamedError } from "@opencode-ai/core/util/error" | ||
| // altimate_change start — client-safe error responses | ||
| import { errorRef, recordUnknownError, unknownErrorBody, upstreamStatus } from "../altimate/server/error-response" | ||
| // altimate_change end | ||
| import { LSP } from "../lsp" | ||
| import { Format } from "../format" | ||
| import { TuiRoutes } from "./routes/tui" | ||
|
|
@@ -263,9 +266,13 @@ export namespace Server { | |
| // altimate_change end | ||
| return app | ||
| .onError((err, c) => { | ||
| // altimate_change start — one log line per error, under the ref an unknown error's response carries | ||
| const ref = errorRef() | ||
| log.error("failed", { | ||
| ref, | ||
| error: err, | ||
| }) | ||
|
Comment on lines
+269
to
274
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MAJOR (bug): the ref this response points to isn't recorded under default settings. // altimate_change start — one log line per error, under the ref an unknown error's response carries
const ref = errorRef()
log.error("failed", {
ref,
error: err,
})The unknown-error response now sends only a fixed message and a Suggested fix: always write referenced unknown errors to a sink that is kept, for example by bypassing the print gate for this one error line in serve mode while keeping the TUI isolated. Then add a test with the print flags unset that checks the response
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, fixed in a0d4696. Before answering, both handlers now write the full error under the ref to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified fixed at a0d4696 by both re-reviewers (gpt-6-astra and gpt-6.1-sol). |
||
| // altimate_change end | ||
| if (err instanceof NamedError) { | ||
| let status: ContentfulStatusCode | ||
| if (err instanceof NotFoundError) status = 404 | ||
|
|
@@ -275,6 +282,9 @@ export namespace Server { | |
| // server fault; without this it surfaced from /find as a 500. | ||
| else if (err.name === "RipgrepInvalidPatternError") status = 400 | ||
| // altimate_change end | ||
| // altimate_change start — a remote the server depends on failed: 502, not a server fault. | ||
| else if (upstreamStatus(err.name)) status = 502 | ||
| // altimate_change end | ||
| else status = 500 | ||
| return c.json(err.toObject(), { status }) | ||
| } | ||
|
|
@@ -286,15 +296,20 @@ export namespace Server { | |
| // altimate_change start — see above: an invalid search pattern is a client error. | ||
| else if (err.name === "RipgrepInvalidPatternError") status = 400 | ||
| // altimate_change end | ||
| else if (upstreamStatus(err.name)) status = 502 | ||
| else status = 500 | ||
| return c.json(err.toObject(), { status }) | ||
| } | ||
| // altimate_change end | ||
| if (err instanceof HTTPException) return err.getResponse() | ||
| const message = err instanceof Error && err.stack ? err.stack : err.toString() | ||
| return c.json(new NamedError.Unknown({ message }).toObject(), { | ||
| status: 500, | ||
| }) | ||
| // altimate_change start — a busy session answers 400 with its message, as the instance routes' handler does | ||
| // (middleware.ts); everything else unrecognised gets a fixed message and the ref it was logged under, never | ||
| // the error's text or stack. | ||
| if (err instanceof Session.BusyError) { | ||
|
ralphstodomingo marked this conversation as resolved.
Resolved
|
||
| return c.json(new NamedError.Unknown({ message: err.message }).toObject(), { status: 400 }) | ||
| } | ||
| return recordUnknownError("server", ref, err).then(() => c.json(unknownErrorBody(ref), { status: 500 })) | ||
| // altimate_change end | ||
| }) | ||
| .use((c, next) => { | ||
| // Allow CORS preflight requests to succeed without auth. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.