Skip to content

Commit d8ec543

Browse files
committed
Selectively short-circuit rendering instead of always doing so when requires are detected [SEC-1323] [INS-4963]
1 parent 56f37cc commit d8ec543

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

packages/insomnia/src/common/render.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,24 @@ export async function render<T>(
293293
) {
294294
// Do nothing to these types
295295
} else if (typeof x === 'string') {
296-
// Detect if the string contains a require statement
297-
if (/require\s*\(/ig.test(x)) {
298-
console.warn('Short-circuiting `render`; string contains possible "require" invocation:', x);
299-
Sentry.captureException(new Error(`Short-circuiting 'render'; string contains possible "require" invocation: ${x}`));
300-
return x;
296+
// Allowed modules could have valid use cases within templates, and we know for a fact that these modules cannot be
297+
// used as part of an exploit.
298+
const allowedModules = ['crypto', 'path'];
299+
const matches = [...x.matchAll(/require\s*\(\s*["'`]([^'"`]*)['"`]/gi)].map(match => match[1]);
300+
301+
if (matches.length) {
302+
// Only allow the string to be rendered if required modules are *all* in the allowed modules list. If any modules
303+
// outside of the allowed list is detected, we short-circuit rendering and return the raw string.
304+
for (const match of matches) {
305+
if (allowedModules.includes(match)) {
306+
continue;
307+
} else {
308+
console.warn('Short-circuiting `render`; string contains at least one disallowed "require" invocation:', x);
309+
Sentry.captureException(new Error(`Short-circuiting 'render'; string contains at least one disallowed "require" invocation: ${x}`));
310+
311+
return x;
312+
}
313+
}
301314
}
302315

303316
try {

0 commit comments

Comments
 (0)