Skip to content

Commit b4cdbf0

Browse files
author
molty3000
committed
fix(security): register global error handler to prevent stack trace leakage
Previously config.errorHandler (an object) was passed to the router instead of a callable function, causing all unhandled exceptions to crash through to Bun's native error overlay which leaked: - Full internal file paths (gateway.ts, 0http-bun/lib/next.js) - Dependency chain details - Exception types and call stacks Now wraps user errorHandler configs into a proper function that: 1. Logs errors safely (without leaking to clients) 2. Returns sanitized 500 responses 3. Preserves custom error handler support for users who pass functions
1 parent 48a886a commit b4cdbf0

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

src/gateway/gateway.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import {
7070
TrustedProxyValidator,
7171
createTrustedProxyValidator,
7272
} from '../security/trusted-proxy'
73+
import { createErrorHandlerMiddleware } from '../security/error-handler-middleware'
7374

7475
/**
7576
* Production-grade API Gateway implementation
@@ -151,12 +152,35 @@ export class BunGateway implements Gateway {
151152
}
152153

153154
// Create 0http-bun router with configuration
155+
// Build a proper global error handler from user config (or use secure defaults)
156+
const errorHandlerFn =
157+
typeof config.errorHandler === 'function'
158+
? // User provided a custom error handler function — use it directly
159+
config.errorHandler
160+
: // Default: log the error, return safe 500 without leaking internals
161+
(err: Error, req?: any) => {
162+
this.config.logger?.error({
163+
error: err.message,
164+
stack: err.stack,
165+
url: req?.url,
166+
method: req?.method,
167+
}, 'Unhandled gateway error')
168+
169+
return new Response(
170+
JSON.stringify({ error: 'Internal server error' }),
171+
{
172+
status: 500,
173+
headers: { 'content-type': 'application/json' },
174+
},
175+
)
176+
}
177+
154178
const routerConfig: IRouterConfig = {
155179
// Map gateway config to router config
156180
defaultRoute: config.defaultRoute
157181
? (req: ZeroRequest) => config.defaultRoute!(req)
158182
: undefined,
159-
errorHandler: config.errorHandler,
183+
errorHandler: errorHandlerFn,
160184
port: config.server?.port,
161185
}
162186

0 commit comments

Comments
 (0)