Skip to content

Commit 2e0b239

Browse files
authored
fix(api-gateway): Remove stack information from error responses when not in dev mode to prevent leaking internals in prod (#9862)
1 parent da4d7aa commit 2e0b239

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

packages/cubejs-api-gateway/src/gateway.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,9 @@ class ApiGateway {
260260
const jsonQuery = getJsonQueryFromGraphQLQuery(query, metaConfig, variables);
261261
res.json({ jsonQuery });
262262
} catch (e: any) {
263+
const stack = getEnv('devMode') ? e.stack : undefined;
263264
this.logger('GraphQL to JSON error', {
264-
error: (e.stack || e).toString(),
265+
error: (stack || e).toString(),
265266
});
266267
res.json({ jsonQuery: null });
267268
}
@@ -2180,6 +2181,7 @@ class ApiGateway {
21802181
e, context, query, res, requestStarted
21812182
}: HandleErrorOptions) {
21822183
const requestId = getEnv('devMode') || context?.signedWithPlaygroundAuthSecret ? context?.requestId : undefined;
2184+
const stack = getEnv('devMode') ? e.stack : undefined;
21832185

21842186
const plainError = e.plainMessages;
21852187

@@ -2190,7 +2192,7 @@ class ApiGateway {
21902192
error: e.message,
21912193
duration: this.duration(requestStarted)
21922194
}, context);
2193-
res({ error: e.message, stack: e.stack, requestId, plainError }, { status: e.status });
2195+
res({ error: e.message, stack, requestId, plainError }, { status: e.status });
21942196
} else if (e.error === 'Continue wait') {
21952197
this.log({
21962198
type: 'Continue wait',
@@ -2219,7 +2221,7 @@ class ApiGateway {
22192221
type: e.type,
22202222
error: e.message,
22212223
plainError,
2222-
stack: e.stack,
2224+
stack,
22232225
requestId
22242226
},
22252227
{ status: 400 }
@@ -2228,10 +2230,10 @@ class ApiGateway {
22282230
this.log({
22292231
type: 'Internal Server Error',
22302232
query,
2231-
error: e.stack || e.toString(),
2233+
error: stack || e.toString(),
22322234
duration: this.duration(requestStarted)
22332235
}, context);
2234-
res({ error: e.toString(), stack: e.stack, requestId, plainError, }, { status: 500 });
2236+
res({ error: e.toString(), stack, requestId, plainError, }, { status: 500 });
22352237
}
22362238
}
22372239

@@ -2497,24 +2499,26 @@ class ApiGateway {
24972499
} catch (e: unknown) {
24982500
if (e instanceof CubejsHandlerError) {
24992501
const error = e.originalError || e;
2502+
const stack = getEnv('devMode') ? error.stack : undefined;
25002503
this.log({
25012504
type: error.message,
25022505
url: req.url,
25032506
token,
2504-
error: error.stack || error.toString()
2507+
error: stack || error.toString()
25052508
}, <any>req);
25062509

25072510
res.status(e.status).json({ error: e.message });
25082511
} else if (e instanceof Error) {
2512+
const stack = getEnv('devMode') ? e.stack : undefined;
25092513
this.log({
25102514
type: 'Auth Error',
25112515
token,
2512-
error: e.stack || e.toString()
2516+
error: stack || e.toString()
25132517
}, <any>req);
25142518

25152519
res.status(500).json({
25162520
error: e.toString(),
2517-
stack: e.stack
2521+
stack,
25182522
});
25192523
}
25202524
}
@@ -2644,10 +2648,11 @@ class ApiGateway {
26442648
};
26452649

26462650
private logProbeError(e: any, type: string): void {
2651+
const stack = getEnv('devMode') ? (e as Error).stack : undefined;
26472652
this.log({
26482653
type,
26492654
driverType: e.driverType,
2650-
error: (e as Error).stack || (e as Error).toString(),
2655+
error: stack || (e as Error).toString(),
26512656
});
26522657
}
26532658

0 commit comments

Comments
 (0)