Skip to content

Commit 1e30dd2

Browse files
ekohilasANGkeithfirecow
authored
Fixes undefined vars becoming null and erroring in conditions (#1356)
* 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]>
1 parent bd6d2ec commit 1e30dd2

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class Utils {
218218
default:
219219
throw operator;
220220
}
221-
return `.match(${rhs}${flags})${remainingTokens} ${_operator} null`;
221+
return `.match(${rhs}${flags})${remainingTokens} ${_operator} null `;
222222
});
223223

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

252252
// Convert all null.match functions to false
253-
evalStr = evalStr.replace(/null.match\(.+?\) != null/g, "false");
254-
evalStr = evalStr.replace(/null.match\(.+?\) == null/g, "false");
253+
evalStr = evalStr.replace(/null.match\(.+?\)\s*!=\s*null/g, "false");
254+
evalStr = evalStr.replace(/null.match\(.+?\)\s*==\s*null/g, "false");
255+
256+
evalStr = evalStr.trim();
255257

256258
let res;
257259
try {

tests/rules-regex.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ const tests = [
7272
jsExpression: '"23".match(/1234/) != null',
7373
evalResult: false,
7474
},
75+
{
76+
rule: '$CI_COMMIT_BRANCH && $GITLAB_FEATURES =~ /\bdependency_scanning\b/ && $CI_GITLAB_FIPS_MODE == "true"',
77+
jsExpression: 'null && false && null == "true"',
78+
evalResult: false,
79+
},
7580
];
7681
/* eslint-enable @typescript-eslint/quotes */
7782

0 commit comments

Comments
 (0)