Skip to content

Scanning Folders and Text

By the end of this chapter you will have scanned things that are not repositories at all — a loose folder of files, the working tree of the fixture as it stands today, and a line of text piped through the terminal — and you will know exactly what each kind of scan can and cannot see. The detector is the same one from chapter 2; only the feedstock changes.

A folder with no history: gitleaks dir

Not every pile of secrets lives in git. Exports, backups, a folder someone zipped and sent you, the contents of a machine being retired: these are files without a past, and gitleaks dir scans them as they are. The lab made a small one (transcript gitleaks-lab/transcripts/t05-dir-and-stdin.txt):

$ cat loose-files/.env
SKYWATCH_API_TOKEN=skw_live_aaaabbbbccccdddd00001111222233334
AWS_ACCESS_KEY_ID=AKIAZ3X5CV7BN2MK4Q6R
$ gitleaks dir --no-banner --no-color loose-files
4:49AM WRN leaks found: 2
exit code: 1

A .env file — the classic place real projects keep the values they were told not to commit — holding two planted values, and both caught: the project's token by the generic rule, the key ID by its shape. A neighbouring notes.txt declaring that nothing in it is a credential was scanned in the same run and correctly produced nothing. Findings from a dir scan carry file and line but no commit, author or date: there is no history to attribute them to, which is the honest shape of the evidence.

The working tree: what is true today

Point dir at the fixture repository itself and it scans the files as they stand at HEAD, ignoring the history entirely:

$ gitleaks dir --no-banner --no-color leaky-app
4:49AM WRN leaks found: 3
exit code: 1

Three findings, and it is worth saying exactly which three, because together with chapter 4 they map the project's whole situation:

  • The weather token in api_client.py and the GitHub token in tools/release.sh — never deleted, sitting in today's tree in plain sight.
  • The tests' dummy token — also in today's tree, also indistinguishable from a real one.

And which three of the history scan's findings are missing: the cloud key ID and the private key, both deleted from the tree, both alive only in history — plus the cloud secret half, which no scan has ever reported. So the two commands divide the world cleanly:

  • gitleaks dir answers what is exposed in these files right now?
  • gitleaks git answers what has this repository ever contained?

A tidy working tree over a leaky history is the normal condition of projects that "fixed it by deleting" — which is to say, most projects with a leak at all. Run both.

Text on a pipe: gitleaks stdin

The third command scans whatever is piped into it:

printf 'token = "skw_live_5555aaaabbbbccccddddeeeeffff000011112222"\n' \
  | gitleaks stdin
4:49AM WRN leaks found: 1
exit code: 1

A small command with an outsized number of uses, because anything that produces text can now be checked: the output of another tool, a configuration file generated by a script before it is written down, the contents of your clipboard's source file, a chat log you are about to paste somewhere public. Findings from stdin carry no file or commit — the line is the location — and the exit code carries the verdict, which makes stdin the easiest of the three to wire into other tools.

Choosing the feedstock

The pattern to keep: scan the history before a repository changes hands or changes visibility; scan the tree when you want today's truth about a folder, repository or not; scan text at the moment of producing it, when a finding is still cheap to act on. The detector does not care which you choose. The findings' context — commit and author, or nothing but a line — is what changes, and the context is what turns "there is a secret" into "there is a secret, it arrived in March, and rotating it is this person's job".