Files
wekan/tests/noIncompleteRegExpEscaping.test.cjs
T
Lauri Ojansivu ed4b8cd648 RouteBleed: remove incomplete route-pattern escaping.
CodeQL alert #434 found that boardExportScope escaped only forward slashes before interpolating an export path into a RegExp. Replace the pattern with an exact route-literal inclusion check, so there is nothing to escape and metacharacters cannot change the match.

Add positive and negative cases with backslashes and regex metacharacters, reject the reported partial-escape shape across tracked JavaScript, and remove a second partial dynamic pattern from release bundle coverage. This was test-only code with no runtime request path, so there is no attributable attempt for Admin Panel Problems.

Thanks to GitHub CodeQL code scanning and xet7 !
2026-08-17 14:50:07 +03:00

59 lines
2.1 KiB
JavaScript

'use strict';
// RouteBleed: GitHub CodeQL alert #434 found a value interpolated into a RegExp
// after only `/` was escaped. A backslash can neutralize that escape, and every other
// metacharacter remains active. This guard catches the whole source shape: a
// replace performed inline in a dynamically-built regular expression is either
// the complete, established escape or it must be replaced with literal matching.
// Run: node tests/noIncompleteRegExpEscaping.test.cjs
const assert = require('assert');
const { execFileSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const ROOT = path.resolve(__dirname, '..');
const JS = /\.(?:cjs|mjs|js)$/;
const SLASH_ONLY_ESCAPE = ".replace(/\\//g, '\\\\/')";
function sourceFiles() {
return execFileSync('git', ['ls-files', '-z'], { cwd: ROOT })
.toString()
.split('\0')
.filter(file => JS.test(file))
.map(file => path.join(ROOT, file));
}
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
function incompleteInlineEscapes(source) {
return source.split('\n').flatMap((line, index) => {
return line.includes(SLASH_ONLY_ESCAPE) ? [index + 1] : [];
});
}
test('no inline partial escape reaches a dynamic RegExp', () => {
const offenders = [];
for (const file of sourceFiles()) {
if (file === __filename) continue;
for (const line of incompleteInlineEscapes(fs.readFileSync(file, 'utf8'))) {
offenders.push(`${path.relative(ROOT, file)}:${line}`);
}
}
assert.deepStrictEqual(offenders, [],
`replace partial escaping or use literal matching:\n${offenders.join('\n')}`);
});
test('negative: the guard catches slash-only escaping with a backslash gap', () => {
const bad = "const r = new RegExp(`${p.replace(/\\//g, '\\\\/')}`);";
assert.deepStrictEqual(incompleteInlineEscapes(bad), [1]);
});
test('the established full metacharacter escape remains allowed', () => {
const good = "const r = new RegExp(`${value.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')}`);";
assert.deepStrictEqual(incompleteInlineEscapes(good), []);
});
console.log(`\nnoIncompleteRegExpEscaping: ${passed} tests passed`);