Skip to content

Commit

Permalink
Fixes undefined vars becoming null and erroring in conditions (#1356)
Browse files Browse the repository at this point in the history
* fixed null vars

* fix: handle rules regex edge case

* Update tests/rules-regex.test.ts

* Update tests/rules-regex.test.ts

* fix

---------

Co-authored-by: ANGkeith <[email protected]>
Co-authored-by: Mads Jon Nielsen <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2024
1 parent bd6d2ec commit 1e30dd2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class Utils {
default:
throw operator;
}
return `.match(${rhs}${flags})${remainingTokens} ${_operator} null`;
return `.match(${rhs}${flags})${remainingTokens} ${_operator} null `;
});

// Scenario when RHS is surrounded by double-quotes
Expand Down Expand Up @@ -250,8 +250,10 @@ Refer to https://docs.gitlab.com/ee/ci/jobs/job_rules.html#unexpected-behavior-f
});

// Convert all null.match functions to false
evalStr = evalStr.replace(/null.match\(.+?\) != null/g, "false");
evalStr = evalStr.replace(/null.match\(.+?\) == null/g, "false");
evalStr = evalStr.replace(/null.match\(.+?\)\s*!=\s*null/g, "false");
evalStr = evalStr.replace(/null.match\(.+?\)\s*==\s*null/g, "false");

evalStr = evalStr.trim();

let res;
try {
Expand Down
5 changes: 5 additions & 0 deletions tests/rules-regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ const tests = [
jsExpression: '"23".match(/1234/) != null',
evalResult: false,
},
{
rule: '$CI_COMMIT_BRANCH && $GITLAB_FEATURES =~ /\bdependency_scanning\b/ && $CI_GITLAB_FIPS_MODE == "true"',
jsExpression: 'null && false && null == "true"',
evalResult: false,
},
];
/* eslint-enable @typescript-eslint/quotes */

Expand Down

0 comments on commit 1e30dd2

Please sign in to comment.