Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: regex should match both single and doubles quoted rhs #1435

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ as rhs contains unescaped \`/\``);
return `.match(new RE2(${_rhs}, "${flags}")) ${_operator} null${remainingTokens}`;
});

// Scenario when RHS is surrounded by double-quotes
// Scenario when RHS is surrounded by single/double-quotes
// https://regexr.com/85t0g
const pattern2 = /\s*(?<operator>(?:=~)|(?:!~))\s*"(?<rhs>[^"\\]*(?:\\.[^"\\]*)*)"/g;
evalStr = evalStr.replace(pattern2, (_, operator, rhs) => {
const pattern2 = /\s*(?<operator>(?:=~)|(?:!~))\s*(?<quote_type>["'])(?<rhs>(?:\\.|[^\\])*?)\2/g;
evalStr = evalStr.replace(pattern2, (_, operator, _quote_type, rhs) => {
let _operator;
switch (operator) {
case "=~":
Expand All @@ -247,10 +247,9 @@ as rhs contains unescaped \`/\``);
throw operator;
}

if (!/\/(.*)\/([\w]*)/.test(rhs)) {
throw Error(`RHS (${rhs}) must be a regex pattern. Do not rely on this behavior!
Refer to https://docs.gitlab.com/ee/ci/jobs/job_rules.html#unexpected-behavior-from-regular-expression-matching-with- for more info...`);
}
assert((/\/(.*)\/([\w]*)/.test(rhs)), (`RHS (${rhs}) must be a regex pattern. Do not rely on this behavior!
Refer to https://docs.gitlab.com/ee/ci/jobs/job_rules.html#unexpected-behavior-from-regular-expression-matching-with- for more info...`));

const regex = /\/(?<pattern>.*)\/(?<flags>[igmsuy]*)/;
const _rhs = rhs.replace(regex, (_: string, pattern: string, flags: string) => {
return `new RE2("${pattern}", "${flags}")`;
Expand All @@ -270,16 +269,15 @@ Refer to https://docs.gitlab.com/ee/ci/jobs/job_rules.html#unexpected-behavior-f
res = (0, eval)(evalStr); // https://esbuild.github.io/content-types/#direct-eval
delete (global as any).RE2; // Cleanup
} catch (err) {
console.log(`
assert(false, (`
Error attempting to evaluate the following rules:
rules:
- if: '${expandedEvalStr}'
as
\`\`\`javascript
${evalStr}
\`\`\`
`);
throw err;
`));
}
return Boolean(res);
}
Expand Down
17 changes: 11 additions & 6 deletions tests/rules-regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const tests = [
},
{
rule: '"test/url" =~ /test/ur/',
expectErr: true,
expectedErrSubStr: "Error attempting to evaluate the following rules:",
},
{
rule: '"master" =~ /master$/',
Expand All @@ -87,7 +87,11 @@ const tests = [
},
{
rule: '"23" =~ "1234"',
expectErr: true,
expectedErrSubStr: "must be a regex pattern. Do not rely on this behavior!",
},
{
rule: '"23" =~ \'1234\'',
expectedErrSubStr: "must be a regex pattern. Do not rely on this behavior!",
},
{
rule: '"23" =~ /1234/',
Expand All @@ -113,7 +117,7 @@ const tests = [
/* eslint-enable @typescript-eslint/quotes */

describe("gitlab rules regex", () => {
tests.filter(t => !t.expectErr)
tests.filter(t => !t.expectedErrSubStr)
.forEach((t) => {
test(`- if: '${t.rule}'\n\t => ${t.evalResult}`, async () => {
const rules = [ {if: t.rule} ];
Expand All @@ -128,14 +132,15 @@ describe("gitlab rules regex", () => {
});

describe("gitlab rules regex [invalid]", () => {
tests.filter(t => t.expectErr)
tests.filter(t => t.expectedErrSubStr)
.forEach((t) => {
test(`- if: '${t.rule}'\n\t => error`, async () => {
test(`- if: '${t.rule}'\n\t to throws error that contains \`${t.expectedErrSubStr}\``, async () => {
const rules = [ {if: t.rule} ];

try {
Utils.getRulesResult({argv, cwd: "", rules, variables: {}}, gitData);
} catch (e) {
} catch (e: any) {
expect(e.message).toContain(t.expectedErrSubStr);
return;
}

Expand Down