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
2 changes: 1 addition & 1 deletion docs/CI.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ It runs from cron on the dev box (the machine with Docker, `gh` and the `CI_RUNN

`cilocal secret-scan` copies the files a change touches (the branch's diff since the merge-base in `make secret-scan`; the pull request's file list in the `Secret scan` job, or every file of the head when GitHub cannot list them, read from the head's objects: `--head`) and scans them with gitleaks 8.30.1, downloaded once into the user cache directory and checked against its release SHA-256. A finding fails the step or job, printing the rule, file and line with the value redacted.

[.gitleaks.toml](../.gitleaks.toml) keeps gitleaks' own rules and allows only the dev-loop bench script's known synthetic values: a rule-scoped allowlist for `generic-api-key` that pairs the script's exact path with those exact values (`condition = "AND"`), never a path on its own. Tests, `testdata` and example env files get no exemption, so a real credential in one fails the scan. Over the whole tree it reports nothing (`make secret-scan ARGS=--all`). A new deliberate fixture gets its own obviously fake value and the same kind of exact entry; `scripts/cilocal/verify`'s `TestScannerPolicyAllowlistsValuesOnly` fails a blanket path allowlist. A real secret is removed and rotated, because history keeps it. **The scan's policy is never the change's.** `.gitleaks.toml` and `.gitleaksignore` (`verify.ScannerPolicy`) come only from the trusted commit: `main` in the `Secret scan` job ([Trusted code only](#trusted-code-only)), the merge-base in `make secret-scan`, with gitleaks' default rules and an empty ignore list where it has none. They are written outside the scanned directory, because gitleaks loads `.gitleaksignore` from the root of what it scans whatever `--gitleaks-ignore-path` says. The change's own copies of those files are scanned as content under another name, inline `gitleaks:allow` comments are ignored (`--ignore-gitleaks-allow`), and so are `GITLEAKS_*` variables. A change that adds, edits or removes a policy file fails the scan even when it finds nothing (`verify.PolicyProblem`): the new policy was never checked, so a maintainer reviews it and merges it on its own, past the failed check.
[.gitleaks.toml](../.gitleaks.toml) keeps gitleaks' own rules and allows only the dev-loop bench script's known synthetic values: a rule-scoped allowlist for `generic-api-key` that pairs the script's exact path with those exact values (`condition = "AND"`), never a path on its own. Tests, `testdata` and example env files get no exemption, so a real credential in one fails the scan. The one file skipped is templ's generated Go: `x_templ.go` beside an `x.templ` (`verify.GeneratedTempl`). Its strings come from the `.templ`, which is scanned, and its generated identifiers read to gitleaks as API keys; a `_templ.go` with no `.templ` beside it is scanned, and `make check-generated` fails one that templ did not generate. Over the whole tree it reports nothing (`make secret-scan ARGS=--all`). A new deliberate fixture gets its own obviously fake value and the same kind of exact entry; `scripts/cilocal/verify`'s `TestScannerPolicyAllowlistsValuesOnly` fails a blanket path allowlist. A real secret is removed and rotated, because history keeps it. **The scan's policy is never the change's.** `.gitleaks.toml` and `.gitleaksignore` (`verify.ScannerPolicy`) come only from the trusted commit: `main` in the `Secret scan` job ([Trusted code only](#trusted-code-only)), the merge-base in `make secret-scan`, with gitleaks' default rules and an empty ignore list where it has none. They are written outside the scanned directory, because gitleaks loads `.gitleaksignore` from the root of what it scans whatever `--gitleaks-ignore-path` says. The change's own copies of those files are scanned as content under another name, inline `gitleaks:allow` comments are ignored (`--ignore-gitleaks-allow`), and so are `GITLEAKS_*` variables. A change that adds, edits or removes a policy file fails the scan even when it finds nothing (`verify.PolicyProblem`): the new policy was never checked, so a maintainer reviews it and merges it on its own, past the failed check.

## Required checks and staging

Expand Down
14 changes: 9 additions & 5 deletions scripts/cilocal/secretscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,15 @@ func fetchCommit(ctx context.Context, rev string) error {
// copyCheckout copies the named files (every tracked one when nil) from the
// checkout into `scan`.
func copyCheckout(ctx context.Context, files []string, scan string) (int, error) {
out, err := git(ctx, "", "ls-files")
if err != nil {
return 0, err
}
tracked := map[string]bool{}
for _, file := range strings.Split(out, "\n") {
tracked[file] = true
}
if files == nil {
out, err := git(ctx, "", "ls-files")
if err != nil {
return 0, err
}
files = strings.Split(out, "\n")
}
root, err := git(ctx, "", "rev-parse", "--show-toplevel")
Expand All @@ -315,7 +319,7 @@ func copyCheckout(ctx context.Context, files []string, scan string) (int, error)
}
copied := 0
for _, file := range files {
if file == "" {
if file == "" || verify.GeneratedTempl(file, func(path string) bool { return tracked[path] }) {
continue
}
// Symlinks and submodules are not content of their own.
Expand Down
28 changes: 24 additions & 4 deletions scripts/cilocal/verify/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,46 @@ const regularMode = "100"

// ScanTargets is the files of a tree the secret scan copies out: the regular
// files among `entries` named in `wanted`, or all of them when `wanted` is
// nil. A path that would leave the scan's directory (absolute, or with a
// `..` element) is a problem, not a file to skip: git never checks one out,
// so only a crafted tree holds one, and the scan fails closed on it.
// nil, less templ's generated Go (GeneratedTempl). A path that would leave
// the scan's directory (absolute, or with a `..` element) is a problem, not
// a file to skip: git never checks one out, so only a crafted tree holds
// one, and the scan fails closed on it.
func ScanTargets(entries []TreeEntry, wanted []string) ([]TreeEntry, string) {
named := map[string]bool{}
for _, file := range wanted {
named[file] = true
}
tree := map[string]bool{}
for _, entry := range entries {
tree[entry.Path] = true
}
has := func(path string) bool { return tree[path] }
targets := []TreeEntry{}
for _, entry := range entries {
if !filepath.IsLocal(filepath.FromSlash(entry.Path)) {
return nil, fmt.Sprintf("the head's tree has the path %q, which leaves the scan's directory", entry.Path)
}
if strings.HasPrefix(entry.Mode, regularMode) && (wanted == nil || named[entry.Path]) {
if strings.HasPrefix(entry.Mode, regularMode) && (wanted == nil || named[entry.Path]) && !GeneratedTempl(entry.Path, has) {
targets = append(targets, entry)
}
}
return targets, ""
}

// templOutput is the suffix templ gives the Go it generates from x.templ.
const templOutput = "_templ.go"

// GeneratedTempl is whether the secret scan skips `path`: templ's generated
// Go (`x_templ.go`) for a `x.templ` the same tree holds (`has`). Its strings
// are the source's, which the scan reads, and its generated identifiers
// (`apiclient.X:` then `templ_7745c5c3_Err`) read to gitleaks as API keys. A
// `_templ.go` with no `.templ` beside it is scanned like any file, and
// make check-generated fails one that is not what templ generates.
func GeneratedTempl(path string, has func(string) bool) bool {
source, found := strings.CutSuffix(path, templOutput)
return found && has(source+".templ")
}

// ScannerPolicy are the files gitleaks reads as policy rather than content:
// its config, and the ignore list it loads from the root of the directory it
// scans whatever --gitleaks-ignore-path says (gitleaks 8.30.1). The scan
Expand Down
21 changes: 21 additions & 0 deletions scripts/cilocal/verify/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ func TestScanTargets(test *testing.T) {
if want := []string{".gitleaks.toml", "scripts/cilocal/bypass.go"}; problem != "" || !slices.Equal(paths(listed), want) {
test.Fatalf("ScanTargets(listed) = %q, %q; want %q", paths(listed), problem, want)
}
generated := append(slices.Clone(entries),
verify.TreeEntry{Mode: "100644", Blob: "t1", Path: "ui/page.templ"},
verify.TreeEntry{Mode: "100644", Blob: "t2", Path: "ui/page_templ.go"},
verify.TreeEntry{Mode: "100644", Blob: "t3", Path: "ui/orphan_templ.go"})
scanned, problem := verify.ScanTargets(generated, []string{"ui/page.templ", "ui/page_templ.go", "ui/orphan_templ.go"})
if want := []string{"ui/page.templ", "ui/orphan_templ.go"}; problem != "" || !slices.Equal(paths(scanned), want) {
test.Fatalf("ScanTargets(templ) = %q, %q; want %q (generated Go skipped only beside its source)", paths(scanned), problem, want)
}
for _, path := range []string{"../outside", "/etc/passwd", "a/../../b"} {
crafted := append(slices.Clone(entries), verify.TreeEntry{Mode: "100644", Blob: "b7", Path: path})
targets, problem := verify.ScanTargets(crafted, nil)
Expand Down Expand Up @@ -139,3 +147,16 @@ func TestGitleaksArgs(test *testing.T) {
}
}
}

func TestGeneratedTempl(test *testing.T) {
test.Parallel()
tree := map[string]bool{"ui/page.templ": true, "ui/page_templ.go": true, "ui/orphan_templ.go": true, "ui/plain.go": true}
has := func(path string) bool { return tree[path] }
for path, want := range map[string]bool{
"ui/page_templ.go": true, "ui/orphan_templ.go": false, "ui/plain.go": false, "ui/page.templ": false, "page_templ.go": false,
} {
if got := verify.GeneratedTempl(path, has); got != want {
test.Errorf("GeneratedTempl(%q) = %v, want %v", path, got, want)
}
}
}
Loading