Skip to content

Commit 7b89438

Browse files
authored
fix: reject ${VAR} in rules:if (#1854)
1 parent eb5a5d9 commit 7b89438

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/utils.ts

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

216216
static evaluateRuleIf (ruleIf: string | undefined, envs: {[key: string]: string}): boolean {
217217
if (ruleIf === undefined) return true;
218+
assert(!/\$\{\w+\}/.test(ruleIf), chalk`rules:rule if invalid expression syntax: {blueBright ${ruleIf}}\nuse {green $VAR} not {red \${VAR\}} in rules:if`);
218219
let evalStr = ruleIf;
219220

220221
const flagsToBinary = (flags: string): number => {

tests/rules.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ test.concurrent("VAR exists positive", () => {
8585
expect(val).toBe(true);
8686
});
8787

88+
test.concurrent("rejects ${VAR} curly-bracket reference", () => {
89+
expect(() => Utils.evaluateRuleIf("${VAR} == 'true'", {VAR: "true"}))
90+
.toThrow(/rules:rule if invalid expression syntax/);
91+
});
92+
8893
test.concurrent("VAR exists fail", () => {
8994
const ruleIf = "$VAR";
9095
const val = Utils.evaluateRuleIf(ruleIf, {});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
job:
3+
rules:
4+
- if: '${GITLAB_CI} == "true"'
5+
script:
6+
- echo "should not run"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {WriteStreamsMock} from "../../../src/write-streams.js";
2+
import {handler} from "../../../src/handler.js";
3+
import {initSpawnSpy} from "../../mocks/utils.mock.js";
4+
import {WhenStatics} from "../../mocks/when-statics.js";
5+
6+
beforeAll(() => {
7+
initSpawnSpy(WhenStatics.all);
8+
});
9+
10+
test.concurrent("rules-curly-bracket-if rejects ${VAR} in rules:if", async () => {
11+
const writeStreams = new WriteStreamsMock();
12+
13+
await expect(handler({
14+
cwd: "tests/test-cases/rules-curly-bracket-if",
15+
stateDir: ".gitlab-ci-local-rules-curly-bracket-if",
16+
}, writeStreams)).rejects.toThrow(/rules:rule if invalid expression syntax/);
17+
});

0 commit comments

Comments
 (0)