Skip to content

Commit 960cd5c

Browse files
committed
fix(rules:if): reject non-regex RHS and wrap jsep parse errors
- Assert that the RHS of =~/!~ is a regex pattern (not a plain quoted string), matching the guard already present in _evaluateRuleIf - Wrap jsep(evalStr) in try-catch so parse failures (e.g. invalid regex flags like /pattern/ur/) surface as "Error attempting to evaluate the following rules:" instead of a raw jsep exception
1 parent bde8d26 commit 960cd5c

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

src/utils.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ export class Utils {
297297
return `RE2JS.compile(${JSON.stringify(pattern)}, ${flagsBinary})`;
298298
});
299299

300+
const assertMsg = [
301+
"RHS (${rhs}) must be a regex pattern. Do not rely on this behavior!",
302+
"Refer to https://docs.gitlab.com/ee/ci/jobs/job_rules.html#unexpected-behavior-from-regular-expression-matching-with- for more info...",
303+
];
304+
assert(_rhs !== regexStr, assertMsg.join("\n"));
305+
300306
const _operator = n.operator === "=~" ? "!=" : "=="; // =~ -> !=; !~ -> ==
301307

302308
const evalStr = `${leftStr.raw}.matchRE2JS(${_rhs}) ${_operator} null`;
@@ -328,7 +334,22 @@ export class Utils {
328334
return res;
329335
};
330336

331-
return walk(jsep(evalStr));
337+
let ast;
338+
try {
339+
ast = jsep(evalStr);
340+
} catch {
341+
const assertMsg = [
342+
"Error attempting to evaluate the following rules:",
343+
" rules:",
344+
` - if: '${ruleIf}'`,
345+
"as",
346+
"```javascript",
347+
`${evalStr}`,
348+
"```",
349+
];
350+
assert(false, assertMsg.join("\n"));
351+
}
352+
return walk(ast!);
332353
}
333354

334355
static _evaluateRuleIf (ruleIf: string | undefined, envs: {[key: string]: string}): boolean {

0 commit comments

Comments
 (0)