Skip to content

Commit 4a65d49

Browse files
committed
fix: Prevent empty stack trace
1 parent 5a13a32 commit 4a65d49

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

library/helpers/cleanupStackTrace.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,45 @@ Error
131131

132132
t.same(cleaned, expected);
133133
});
134+
135+
t.test(
136+
"it does not remove the complete stack trace if the library root is wrong",
137+
async () => {
138+
const stack = `
139+
Error
140+
at Collection.wrap (/Users/hansott/Code/node-RASP/build/agent/applyHooks.js:149:33)
141+
at Posts.all (/Users/hansott/Code/node-RASP/sample-apps/express-mongodb/posts.js:30:36)
142+
at /Users/hansott/Code/node-RASP/sample-apps/express-mongodb/app.js:49:41
143+
at asyncUtilWrap (/Users/hansott/Code/node-RASP/node_modules/express-async-handler/index.js:3:20)
144+
at /Users/hansott/Code/node-RASP/build/sources/express/wrapRequestHandler.js:22:20
145+
at runWithContext (/Users/hansott/Code/node-RASP/build/agent/Context.js:34:16)
146+
at /Users/hansott/Code/node-RASP/build/sources/express/wrapRequestHandler.js:12:45
147+
at Layer.handle [as handle_request] (/Users/hansott/Code/node-RASP/node_modules/express/lib/router/layer.js:95:5)
148+
at next (/Users/hansott/Code/node-RASP/node_modules/express/lib/router/route.js:149:13)
149+
at Route.dispatch (/Users/hansott/Code/node-RASP/node_modules/express/lib/router/route.js:119:3)
150+
`.trim();
151+
152+
const cleaned = cleanupStackTrace(stack, "/Users/hansott/Code/node-RASP/");
153+
154+
const expected = `
155+
Error
156+
at Collection.wrap (/Users/hansott/Code/node-RASP/build/agent/applyHooks.js:149:33)
157+
at Posts.all (/Users/hansott/Code/node-RASP/sample-apps/express-mongodb/posts.js:30:36)
158+
at /Users/hansott/Code/node-RASP/sample-apps/express-mongodb/app.js:49:41
159+
at asyncUtilWrap (/Users/hansott/Code/node-RASP/node_modules/express-async-handler/index.js:3:20)
160+
at /Users/hansott/Code/node-RASP/build/sources/express/wrapRequestHandler.js:22:20
161+
at runWithContext (/Users/hansott/Code/node-RASP/build/agent/Context.js:34:16)
162+
at /Users/hansott/Code/node-RASP/build/sources/express/wrapRequestHandler.js:12:45
163+
at Layer.handle [as handle_request] (/Users/hansott/Code/node-RASP/node_modules/express/lib/router/layer.js:95:5)
164+
at next (/Users/hansott/Code/node-RASP/node_modules/express/lib/router/route.js:149:13)
165+
at Route.dispatch (/Users/hansott/Code/node-RASP/node_modules/express/lib/router/route.js:119:3)
166+
`.trim();
167+
168+
t.same(cleaned, expected);
169+
}
170+
);
171+
172+
t.test("it catches exceptions", async (t) => {
173+
// @ts-expect-error Test with invalid input
174+
t.same(cleanupStackTrace("test", undefined), "test");
175+
});

library/helpers/cleanupStackTrace.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ import { escapeStringRegexp } from "./escapeStringRegexp";
22

33
export function cleanupStackTrace(stack: string, libraryRoot: string): string {
44
try {
5-
return stack
5+
const newStackLines = stack
66
.split("\n")
77
.filter(createLineFilter(libraryRoot))
8-
.map(createLineMapper(libraryRoot))
9-
.join("\n")
10-
.trim();
8+
.map(createLineMapper(libraryRoot));
9+
10+
// If the stack only has one line (the error message), we return the original stack trace.
11+
// This could happen if the detected library root is wrong
12+
if (newStackLines.length <= 1) {
13+
return stack;
14+
}
15+
16+
return newStackLines.join("\n").trim();
1117
} catch {
1218
// Safer to return the original stack trace in case of an error
1319
// than to crash the application

0 commit comments

Comments
 (0)