Skip to content

Commit 71f1282

Browse files
committed
fix: reject non-string rules:if
A rules entry with an empty if key parses as null and hit string methods directly, so a config mistake surfaced as a TypeError.
1 parent 4c365da commit 71f1282

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export class Utils {
228228

229229
static evaluateRuleIf (ruleIf: string | undefined, envs: {[key: string]: string}): boolean {
230230
if (ruleIf === undefined) return true;
231+
assert(typeof ruleIf === "string", chalk`This GitLab CI configuration is invalid: {blueBright rules:if} must be a string, but got {red ${JSON.stringify(ruleIf)}}`);
231232
assert(!/\$\{\w+\}/.test(ruleIf), chalk`rules:rule if invalid expression syntax: {blueBright ${ruleIf}}\nuse {green $VAR} not {red \${VAR\}} in rules:if`);
232233
let evalStr = ruleIf;
233234

tests/rules.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,11 @@ test.concurrent("https://github.com/firecow/gitlab-ci-local/issues/1252", () =>
334334
const rulesResult = Utils.getRulesResult({argv, cwd: "", rules, variables}, gitData);
335335
expect(rulesResult).toEqual({when: "on_success", allowFailure: false, variables: undefined});
336336
});
337+
338+
test.concurrent("https://github.com/firecow/gitlab-ci-local/issues/1841", () => {
339+
for (const ruleIf of [null, 1, true, ["a"]]) {
340+
const rules = [ {if: ruleIf} as any ];
341+
expect(() => Utils.getRulesResult({argv, cwd: "", rules, variables: {}}, gitData))
342+
.toThrow(/must be a string, but got/);
343+
}
344+
});

0 commit comments

Comments
 (0)