Secret scanning over gitleaks' rule set
README.md

leaks #

Find credentials in text with the rules of gitleaks.

leaks is an OCaml library and a command that scan text for API keys,
access tokens and private keys. They read a gitleaks rule file. Each rule in
it has a Go regular expression and a minimum Shannon entropy for the secret.
A rule may also name keywords that must occur in the text before the
expression is tried, and allowlists of values that only look like secrets.
The rule file of gitleaks v8.30.1 ships with the library, so a scan needs
neither Go nor the gitleaks binary. Go's regular expressions are matched
without backtracking, and a scan takes time linear in the length of the
text.

Installation #

leaks needs OCaml 5.1 or later. The package is not in the public
opam-repository; add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install leaks

Usage #

Library #

Leaks.gitleaks is the compiled rule file that ships with the library.
Leaks.scan returns one finding per match, with the identifier of the rule
and the byte offsets of the match:

# let rules = Lazy.force Leaks.gitleaks;;
val rules : Leaks.t = <abstr>
# let text = "export GITHUB_TOKEN=ghp_R7xqLk2M9vTn4Wp8Ys1Hd6Fj3Bc5Gz0Ea2Qu";;
val text : string =
  "export GITHUB_TOKEN=ghp_R7xqLk2M9vTn4Wp8Ys1Hd6Fj3Bc5Gz0Ea2Qu"
# let findings = Leaks.scan rules text;;
val findings : Leaks.finding list =
  [{Leaks.rule = "github-pat"; start = 20; stop = 60; secret_start = 20;
    secret_stop = 60; entropy = 5.22192809488736387}]
# List.map (Fmt.str "%a" Leaks.pp_finding) findings;;
- : string list = ["20-60 github-pat"]

A finding also carries the byte offsets of the secret and its entropy.
Leaks.pp_finding never prints the secret.

Leaks.of_toml compiles another rule file in gitleaks' format. It refuses a
rule it cannot run as gitleaks runs it, and the error names the rule and the
construct:

# let scan toml text =
    Result.map
      (fun t -> List.map (fun (f : Leaks.finding) -> f.rule) (Leaks.scan t text))
      (Leaks.of_toml toml);;
val scan : string -> string -> (string list, string) result = <fun>
# scan "[[rules]]\nid = \"ticket\"\nregex = '''TICKET-[0-9]{4}'''" "see TICKET-0042";;
- : (string list, string) result = Ok ["ticket"]
# scan "[[rules]]\nid = \"letters\"\nregex = '''\\pL+'''" "";;
- : (string list, string) result =
Error "letters: Unicode class at offset 0: \\pL+"

Leaks.Regex is the matcher for Go's regular expressions, usable on its
own. A match and its groups are the ones Go's regexp package returns:

# let r = Result.get_ok (Leaks.Regex.compile "(?i)key=([a-z0-9]+)");;
val r : Leaks.Regex.t = <abstr>
# Leaks.Regex.exec_groups r "API KEY=abc123;";;
- : (int * int) option array option = Some [|Some (4, 14); Some (8, 14)|]

Command line #

leaks scan FILE... prints one line per match: the file, the byte offsets
of the match and the rule. It never prints the secret. With no file, or with
-, it reads standard input. It exits 1 when a rule matched and 0 when none
did.

$ leaks scan session.txt
session.txt:20-60 github-pat

--rules FILE reads another gitleaks rule file, and leaks rules lists
the identifiers of the rules.

Limitations #

  • A scan sees text only, with no file path and no commit. A rule with a
    path matches only when its path expression accepts the empty path, and
    an allowlist of paths or commits allows nothing.
  • gitleaks decodes base64, hex and percent-encoded spans and scans the
    result. leaks scans the text as it is.
  • A match is never checked against the service that issued the credential.
  • Leaks.Regex.compile refuses a Unicode class (\p{..}), a backreference
    and case folding outside ASCII.

Performance #

A scan reads the text once for the keywords and once for the literals each
rule's expression must hold, and runs an expression only near those
literals. The table gives the CPU time of leaks scan on an arm64 Mac, over
session transcripts in JSON Lines that hold OCaml sources and documents.

Input Findings CPU
10 MiB 27 2.0 s
the same 10 MiB ten times 270 19.5 s
100 MiB of distinct transcript 186 29.6 s

The commands that timed it are:

$ head -c 10485760 transcripts.jsonl > 10m.jsonl
$ head -c 104857600 transcripts.jsonl > 100m.jsonl
$ for i in 1 2 3 4 5 6 7 8 9 10; do cat 10m.jsonl; done > 10x10m.jsonl
$ time leaks scan 10m.jsonl > /dev/null
$ time leaks scan 10x10m.jsonl > /dev/null
$ time leaks scan 100m.jsonl > /dev/null

On the 2 MiB of gitleaks' own rule fixtures, gitleaks stdin (v8.30.1)
takes 7.3 s of CPU and leaks scan 3.3 s.

API #

lib/leaks.mli documents the library:

  • Leaks.of_toml, Leaks.gitleaks, Leaks.gitleaks_version and
    Leaks.rules build and describe a rule set.
  • Leaks.scan, Leaks.finding and Leaks.pp_finding scan a text and
    report the matches.
  • Leaks.entropy is gitleaks' Shannon entropy of a string.
  • Leaks.Regex (lib/regex.mli) compiles and runs Go's
    regular expressions.

Licence #

ISC. See LICENSE.md. The rule file in rules/ is gitleaks'
(MIT, Copyright (c) 2019 Zachary Rice), with its licence beside it.