mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Alert 564 (regex injection): releases/platform-native-packages.mjs compiled
its command-line platform and arch into a RegExp. The canvas package names
are now compared as strings. The upload naming of attachments and avatars,
the attachment rename popup and tools/mirror-menu.mjs built a RegExp from a
file extension or a name the same way; they use literal suffix and prefix
checks now. tests/noUnescapedDynamicRegExp.test.cjs parses every source file
and fails on a RegExp built from a value that is not escaped, unless the
site is listed with the reason its values are constants.
Alert 563 (incomplete escaping): name.replace('/', '%2F') replaced only the
first slash. registryPackagePath() replaces every one and refuses anything
that is not an npm package name. Seven tests escaped a regex the same
first-only way; 'g++'.replace('+', '\\+') let "- g+" pass a guard. They use
the complete escapeRegExp, and tests/noIncompleteRegExpEscaping.test.cjs
fails on any string-pattern replace that performs an escape.
Alert 562 (ReDoS): the CasaOS reverse-domain id pattern in
tests/platformTemplates.test.cjs let '.' be matched by both groups, so
'0.0.0...!' backtracked exponentially. Each separator now belongs to one
group, and the test runs CodeQL's attack string against it.
Thanks to GitHub CodeQL and xet7 !
114 lines
4.5 KiB
JavaScript
114 lines
4.5 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;
|
|
if (!fs.existsSync(file)) 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), []);
|
|
});
|
|
|
|
// GitHub CodeQL alerts #563 (js/incomplete-sanitization) and the test-file
|
|
// copies of it: `.replace('/', '%2F')` or `.replace('.', '\\.')` with a STRING
|
|
// pattern replaces only the first occurrence, so `@a/b/c` kept its second `/`
|
|
// and `'g++'.replace('+', '\\+')` left a live `+` that let `- g+` pass a guard.
|
|
// An escape is a regex with the g flag, the complete escapeRegExp, or no
|
|
// escaping at all (literal comparison). Minified vendor copies are not ours.
|
|
const FIRST_ONLY_ESCAPE = /\.replace\(\s*(['"])((?:\\.|(?!\1)[^\\]){1,4})\1\s*,\s*(['"`])(?:\\\\|%[0-9A-Fa-f]{2}|&#?\w+;)/;
|
|
|
|
// Files that quote the shape on purpose, as a sample for their own guard.
|
|
const FIRST_ONLY_SAMPLES = new Set([
|
|
'tests/noIdentityReplacement.test.cjs', // CodeQL's own example, as a string
|
|
]);
|
|
|
|
function firstOnlyEscapes(source) {
|
|
return source.split('\n').flatMap((line, index) => {
|
|
if (/^\s*(?:\/\/|\*)/.test(line)) return [];
|
|
return FIRST_ONLY_ESCAPE.test(line) ? [index + 1] : [];
|
|
});
|
|
}
|
|
|
|
test('no string-pattern replace performs an escape (it would escape only the first one)', () => {
|
|
const offenders = [];
|
|
for (const file of sourceFiles()) {
|
|
if (file === __filename || /\.min\.js$/.test(file)) continue;
|
|
if (FIRST_ONLY_SAMPLES.has(path.relative(ROOT, file).split(path.sep).join('/'))) continue;
|
|
if (!fs.existsSync(file)) continue;
|
|
for (const line of firstOnlyEscapes(fs.readFileSync(file, 'utf8'))) {
|
|
offenders.push(`${path.relative(ROOT, file)}:${line}`);
|
|
}
|
|
}
|
|
assert.deepStrictEqual(offenders, [],
|
|
`use a /g regex, the complete escapeRegExp, or literal matching:\n${offenders.join('\n')}`);
|
|
});
|
|
|
|
test('negative: the guard catches each first-only escape it was written for', () => {
|
|
for (const bad of [
|
|
"fetch(`${REGISTRY}/${name.replace('/', '%2F')}/${version}`);",
|
|
"new RegExp(`${file.replace('.', '\\\\.')}`);",
|
|
"new RegExp(term.replace('/', '\\\\/'));",
|
|
'new RegExp(`- ${pkg.replace("+", "\\\\+")}`);',
|
|
"html.replace('<', '<');",
|
|
]) {
|
|
assert.deepStrictEqual(firstOnlyEscapes(bad), [1], bad);
|
|
}
|
|
for (const good of [
|
|
"name.replace(/\\//g, '%2F');",
|
|
"parseFloat(v.replace(',', '.'));",
|
|
"iso.replace('T', ' ');",
|
|
"text.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');",
|
|
]) {
|
|
assert.deepStrictEqual(firstOnlyEscapes(good), [], good);
|
|
}
|
|
});
|
|
|
|
console.log(`\nnoIncompleteRegExpEscaping: ${passed} tests passed`);
|