Skip to content

Commit dcebdd6

Browse files
give better error messages
1 parent fe3dbb7 commit dcebdd6

File tree

6 files changed

+129
-6
lines changed

6 files changed

+129
-6
lines changed

lib/config-utils.js

Lines changed: 20 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.test.js

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config-utils.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,49 @@ doInvalidQueryUsesTest(
343343
doInvalidQueryUsesTest(
344344
"./..",
345345
c => configUtils.getLocalPathOutsideOfRepository(c, ".."));
346+
347+
const validPaths = [
348+
'foo',
349+
'foo/',
350+
'foo/**',
351+
'foo/**/',
352+
'foo/**/**',
353+
'foo/**/bar/**/baz',
354+
'**/',
355+
'**/foo',
356+
'/foo',
357+
];
358+
const invalidPaths = [
359+
'a/***/b',
360+
'a/**b',
361+
'a/b**',
362+
'**',
363+
];
364+
test('path validations', t => {
365+
// Dummy values to pass to validateAndSanitisePath
366+
const propertyName = 'paths';
367+
const configFile = './.github/codeql/config.yml';
368+
369+
for (const path of validPaths) {
370+
t.truthy(configUtils.validateAndSanitisePath(path, propertyName, configFile));
371+
}
372+
for (const path of invalidPaths) {
373+
t.throws(() => configUtils.validateAndSanitisePath(path, propertyName, configFile));
374+
}
375+
});
376+
377+
test('path sanitisation', t => {
378+
// Dummy values to pass to validateAndSanitisePath
379+
const propertyName = 'paths';
380+
const configFile = './.github/codeql/config.yml';
381+
382+
// Valid paths are not modified
383+
t.deepEqual(
384+
configUtils.validateAndSanitisePath('foo/bar', propertyName, configFile),
385+
'foo/bar');
386+
387+
// Trailing stars are stripped
388+
t.deepEqual(
389+
configUtils.validateAndSanitisePath('foo/**', propertyName, configFile),
390+
'foo/');
391+
});

src/config-utils.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ export class Config {
113113
}
114114
}
115115

116+
// Regex validating stars in paths or paths-ignore entries.
117+
// The intention is to only allow ** to appear when immediately
118+
// preceded and followed by a slash.
119+
const pathStarsRegex = /.*(?:\*\*[^/].*|\*\*$|[^/]\*\*.*)/;
120+
121+
// Checks that a paths of paths-ignore entry is valid, possibly modifying it
122+
// to make it valid, or if not possible then throws an error.
123+
export function validateAndSanitisePath(originalPath: string, propertyName: string, configFile: string): string {
124+
let path = originalPath;
125+
if (path.endsWith('/**')) {
126+
path = path.substring(0, path.length - 2);
127+
}
128+
if (path.match(pathStarsRegex)) {
129+
throw new Error(getConfigFilePropertyError(
130+
configFile,
131+
propertyName,
132+
'"' + originalPath + '" contains an invalid "**" wildcard. ' +
133+
'They must be immediately preceeded and followed by a slash as in "/**/".'));
134+
}
135+
return path;
136+
}
137+
116138
export function getNameInvalid(configFile: string): string {
117139
return getConfigFilePropertyError(configFile, NAME_PROPERTY, 'must be a non-empty string');
118140
}
@@ -243,7 +265,7 @@ async function initConfig(): Promise<Config> {
243265
if (typeof path !== "string" || path === '') {
244266
throw new Error(getPathsIgnoreInvalid(configFile));
245267
}
246-
config.pathsIgnore.push(path);
268+
config.pathsIgnore.push(validateAndSanitisePath(path, PATHS_IGNORE_PROPERTY, configFile));
247269
});
248270
}
249271

@@ -255,7 +277,7 @@ async function initConfig(): Promise<Config> {
255277
if (typeof path !== "string" || path === '') {
256278
throw new Error(getPathsInvalid(configFile));
257279
}
258-
config.paths.push(path);
280+
config.paths.push(validateAndSanitisePath(path, PATHS_PROPERTY, configFile));
259281
});
260282
}
261283

0 commit comments

Comments
 (0)