Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/src/content/docs/lint-rules/audit-rules.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,36 @@ Authority: [GitHub Security Lab — Preventing pwn requests](https://securitylab

Add a cooldown so new releases aren't merged instantly.

### GHA059

**Stale or missing pin annotation** — merge-worthy · guidance

Add or correct the trailing version comment on the SHA-pinned reference so it matches the digest.

Authority: [OSSF Scorecard — Pinned-Dependencies](https://github.com/ossf/scorecard/blob/main/docs/checks.md#pinned-dependencies) · [GitHub — Using third-party actions](https://docs.github.com/en/actions/security-for-github-actions/security-guides/security-hardening-for-github-actions#using-third-party-actions)

### GHA060

**Over-scoped generated token** — merge-worthy · guidance

Narrow the GitHub App token's permission-* inputs to the scopes its consuming steps actually use, or remove the unused ones.

Authority: [OSSF Scorecard — Token-Permissions](https://github.com/ossf/scorecard/blob/main/docs/checks.md#token-permissions) · [GitHub — Automatic token authentication](https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication)

### GHA061

**Action reference outside the configured usage policy** — merge-worthy · guidance

Replace the reference with one from the approved allowlist, or get it added if it should be trusted.

### GHA062

**Pinned reference matches a known-vulnerability advisory** — merge-worthy · guidance

Bump the reference to a patched ref/commit named by the advisory.

Authority: [OSSF Scorecard — Vulnerabilities](https://github.com/ossf/scorecard/blob/main/docs/checks.md#vulnerabilities)

## GitLab CI (WGL)

### WGL010
Expand Down
2 changes: 1 addition & 1 deletion lexicons/github/src/codegen/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ npm install --save-dev @intentius/chant-lexicon-github

{{file:docs-snippets/src/quickstart.ts}}

The lexicon provides **3 resources** (Workflow, Job, Dependabot config), **14 composites** (Checkout, SetupNode, SetupGo, SetupPython, CacheAction, UploadArtifact, DownloadArtifact, NodeCI, NodePipeline, PythonCI, DockerBuild, DeployEnvironment, GoCI, Dependabot) + **3 presets** (BunPipeline, PnpmPipeline, YarnPipeline), a typed **Expression** system with 24 GitHub and 5 Runner context variables, and **13 lint rules** + **45 post-synth checks** (including a CI/CD supply-chain security pass, GHA029–058).
The lexicon provides **3 resources** (Workflow, Job, Dependabot config), **14 composites** (Checkout, SetupNode, SetupGo, SetupPython, CacheAction, UploadArtifact, DownloadArtifact, NodeCI, NodePipeline, PythonCI, DockerBuild, DeployEnvironment, GoCI, Dependabot) + **3 presets** (BunPipeline, PnpmPipeline, YarnPipeline), a typed **Expression** system with 24 GitHub and 5 Runner context variables, and **13 lint rules** + **49 post-synth checks** (including a CI/CD supply-chain security pass, GHA029–062).
`;

const outputFormat = `The GitHub Actions lexicon serializes resources into **\`.github/workflows/*.yml\`** YAML files.
Expand Down
8 changes: 7 additions & 1 deletion lexicons/github/src/lint/audit-catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The github lexicon's chant audit catalog — metadata for its post-synth rules
* (GHA GitHub Actions rules). Contributed via githubPlugin.auditCatalog() (#687).
*/
import { auditRule, GH_INJECTION, GH_PWN, GH_SECRETS, GH_THIRD_PARTY, GH_TOKEN, SCORECARD_PINNED, SCORECARD_TOKEN, type RuleMeta } from "@intentius/chant/audit/catalog";
import { auditRule, GH_INJECTION, GH_PWN, GH_SECRETS, GH_THIRD_PARTY, GH_TOKEN, SCORECARD_PINNED, SCORECARD_TOKEN, SCORECARD_VULN, type RuleMeta } from "@intentius/chant/audit/catalog";

export const githubAuditCatalog: Record<string, RuleMeta> = {
GHA006: auditRule("GHA006", "report-only", "guidance", "Duplicate workflow name", "Give each workflow a unique `name:`.", { category: "correctness" }),
Expand Down Expand Up @@ -50,4 +50,10 @@ export const githubAuditCatalog: Record<string, RuleMeta> = {
GHA056: auditRule("GHA056", "report-only", "guidance", "Workflow without a name", "Add a `name:` to the workflow.", { category: "best-practice" }),
GHA057: auditRule("GHA057", "merge-worthy", "guidance", "Dependency update can execute untrusted code", "Disable the option that lets dependency updates run external code.", { authority: [GH_PWN] }),
GHA058: auditRule("GHA058", "report-only", "guidance", "Dependency update has no cooldown window", "Add a cooldown so new releases aren't merged instantly.", { category: "best-practice" }),

// Supply-chain and token-scoping expansion (#445).
GHA059: auditRule("GHA059", "merge-worthy", "guidance", "Stale or missing pin annotation", "Add or correct the trailing version comment on the SHA-pinned reference so it matches the digest.", { authority: [SCORECARD_PINNED, GH_THIRD_PARTY] }),
GHA060: auditRule("GHA060", "merge-worthy", "guidance", "Over-scoped generated token", "Narrow the GitHub App token's permission-* inputs to the scopes its consuming steps actually use, or remove the unused ones.", { authority: [SCORECARD_TOKEN, GH_TOKEN] }),
GHA061: auditRule("GHA061", "merge-worthy", "guidance", "Action reference outside the configured usage policy", "Replace the reference with one from the approved allowlist, or get it added if it should be trusted.", { category: "security" }),
GHA062: auditRule("GHA062", "merge-worthy", "guidance", "Pinned reference matches a known-vulnerability advisory", "Bump the reference to a patched ref/commit named by the advisory.", { authority: [SCORECARD_VULN] }),
};
147 changes: 147 additions & 0 deletions lexicons/github/src/lint/post-synth/gha059.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { describe, test, expect } from "vitest";
import type { PostSynthContext } from "@intentius/chant/lint/post-synth";
import { gha059, findStalePinAnnotations } from "./gha059";

function makeCtx(yaml: string): PostSynthContext {
return {
outputs: new Map([["github", yaml]]),
entities: new Map(),
buildResult: {
outputs: new Map([["github", yaml]]),
entities: new Map(),
warnings: [],
errors: [],
sourceFileCount: 1,
},
};
}

const SHA_A = "1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b";
const SHA_B = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

describe("GHA059: stale or missing pin annotation", () => {
test("flags a SHA-pinned ref with no trailing comment", () => {
const yaml = `name: CI
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@${SHA_A}
`;
const diags = gha059.check(makeCtx(yaml));
expect(diags).toHaveLength(1);
expect(diags[0].checkId).toBe("GHA059");
expect(diags[0].severity).toBe("warning");
expect(diags[0].message).toContain("no trailing version comment");
});

test("does not flag a SHA-pinned ref with a comment", () => {
const yaml = `name: CI
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@${SHA_A} # v4.0.2
`;
expect(gha059.check(makeCtx(yaml))).toHaveLength(0);
});

test("treats a bare '# ' with nothing after it as missing", () => {
const yaml = `name: CI
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@${SHA_A} #
`;
const diags = gha059.check(makeCtx(yaml));
expect(diags).toHaveLength(1);
expect(diags[0].message).toContain("no trailing version comment");
});

test("flags the same commit annotated with two different labels (mismatch)", () => {
const yaml = `name: CI
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@${SHA_A} # v4.0.2
test:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@${SHA_A} # v4.0.3
`;
const diags = gha059.check(makeCtx(yaml));
expect(diags).toHaveLength(2);
expect(diags.every((d) => d.severity === "warning")).toBe(true);
expect(diags.every((d) => d.message.includes("internally inconsistent"))).toBe(true);
});

test("flags the same label attached to two different commits (mismatch)", () => {
const yaml = `name: CI
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@${SHA_A} # v4.0.2
test:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@${SHA_B} # v4.0.2
`;
const diags = gha059.check(makeCtx(yaml));
expect(diags).toHaveLength(2);
expect(diags.every((d) => d.message.includes("internally inconsistent"))).toBe(true);
});

test("does not flag two different actions annotated independently", () => {
const yaml = `name: CI
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@${SHA_A} # v4.0.2
- uses: actions/setup-go@${SHA_B} # v5.0.0
`;
expect(findStalePinAnnotations(yaml)).toHaveLength(0);
});

test("does not flag a tag/branch-pinned ref (owned by GHA029)", () => {
const yaml = `name: CI
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v4
`;
expect(findStalePinAnnotations(yaml)).toHaveLength(0);
});

test("does not flag local or docker references", () => {
const yaml = `name: CI
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./.github/actions/local
- uses: docker://alpine@sha256:abc
`;
expect(findStalePinAnnotations(yaml)).toHaveLength(0);
});
});
132 changes: 132 additions & 0 deletions lexicons/github/src/lint/post-synth/gha059.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* GHA059: Stale or Missing Pin Annotation
*
* A ref pinned to a commit SHA is only as trustworthy as the label a reviewer
* reads next to it — `uses: actions/setup-node@1a2b…9a0b # v4.0.2` lets a
* reviewer sanity-check the digest without resolving it themselves. This check
* flags two ways that label goes stale:
*
* 1. Missing — a SHA-pinned ref with no trailing version comment at all.
* 2. Mismatched — inferred without any network call, from internal
* inconsistency in the workflow itself: the same action is pinned to the
* same commit SHA in one place and a *different* label in another, or the
* same label is attached to two *different* commit SHAs. Either shape
* proves at least one of the labels is wrong — a real digest has exactly
* one correct human-readable name.
*
* Cross-referencing a label against the actual git history (to catch a lone,
* internally-consistent but still-wrong annotation) needs a network call and
* is out of scope here — see GHA062 for the feed-based advisory check, which
* takes the same "no network in the rule itself" stance via an injected feed.
*/

import type { PostSynthCheck, PostSynthContext, PostSynthDiagnostic } from "@intentius/chant/lint/post-synth";
import { getPrimaryOutput, extractActionRefs, extractUsesComment, parseActionUses } from "./yaml-helpers";

const SHA_RE = /^[0-9a-f]{40}$/;

export interface StalePinFinding {
job: string;
ref: string;
slug: string;
sha: string;
kind: "missing" | "mismatched";
/** For "mismatched": the other label/SHA this one conflicts with. */
conflict?: string;
}

/**
* Find every SHA-pinned `uses:` with a missing or internally-inconsistent
* version annotation. Scoped to one workflow document — cross-file
* consistency isn't attempted since two independent workflows pinning the
* same action differently isn't evidence of drift.
*/
export function findStalePinAnnotations(yaml: string): StalePinFinding[] {
const refs = extractActionRefs(yaml)
.map(({ job, ref }) => ({ job, ref, parsed: parseActionUses(ref) }))
.filter((r): r is { job: string; ref: string; parsed: NonNullable<ReturnType<typeof parseActionUses>> } => !!r.parsed && SHA_RE.test(r.parsed.gitRef));

const findings: StalePinFinding[] = [];

// First pass: missing annotations.
const withComment: Array<{ job: string; ref: string; slug: string; sha: string; comment: string }> = [];
for (const { job, ref, parsed } of refs) {
const comment = extractUsesComment(ref);
if (!comment) {
findings.push({ job, ref, slug: parsed.slug, sha: parsed.gitRef, kind: "missing" });
} else {
withComment.push({ job, ref, slug: parsed.slug, sha: parsed.gitRef, comment });
}
}

// Second pass: internal mismatch. Group by slug — the same action's
// commit-SHA↔label mapping must be one-to-one across the whole file.
const bySlug = new Map<string, Array<{ job: string; ref: string; slug: string; sha: string; comment: string }>>();
for (const entry of withComment) {
const list = bySlug.get(entry.slug) ?? [];
list.push(entry);
bySlug.set(entry.slug, list);
}

for (const [, entries] of bySlug) {
const shaToComments = new Map<string, Set<string>>();
const commentToShas = new Map<string, Set<string>>();
for (const e of entries) {
(shaToComments.get(e.sha) ?? shaToComments.set(e.sha, new Set()).get(e.sha)!).add(e.comment);
(commentToShas.get(e.comment) ?? commentToShas.set(e.comment, new Set()).get(e.comment)!).add(e.sha);
}
for (const e of entries) {
const otherComments = [...(shaToComments.get(e.sha) ?? [])].filter((c) => c !== e.comment);
const otherShas = [...(commentToShas.get(e.comment) ?? [])].filter((s) => s !== e.sha);
if (otherComments.length > 0) {
findings.push({
job: e.job,
ref: e.ref,
slug: e.slug,
sha: e.sha,
kind: "mismatched",
conflict: `commit ${e.sha} is also annotated "# ${otherComments[0]}" elsewhere`,
});
} else if (otherShas.length > 0) {
findings.push({
job: e.job,
ref: e.ref,
slug: e.slug,
sha: e.sha,
kind: "mismatched",
conflict: `label "# ${e.comment}" is also attached to commit ${otherShas[0]} elsewhere`,
});
}
}
}

return findings;
}

export const gha059: PostSynthCheck = {
id: "GHA059",
description: "SHA-pinned action reference has a missing or internally-inconsistent version annotation",

check(ctx: PostSynthContext): PostSynthDiagnostic[] {
const diagnostics: PostSynthDiagnostic[] = [];

for (const [, output] of ctx.outputs) {
const yaml = getPrimaryOutput(output);
for (const finding of findStalePinAnnotations(yaml)) {
const message =
finding.kind === "missing"
? `Job "${finding.job}" pins ${finding.ref} to a commit SHA with no trailing version comment (e.g. "# v4.0.2") — add one so reviewers can sanity-check the digest.`
: `Job "${finding.job}" pins ${finding.ref} whose annotation is internally inconsistent (${finding.conflict}) — one of these labels no longer matches the digest it's attached to.`;
diagnostics.push({
checkId: "GHA059",
severity: "warning",
message,
entity: finding.job,
lexicon: "github",
});
}
}

return diagnostics;
},
};
Loading
Loading