# Secret shapes a published body is checked against before it is written to
# storage. Adapted from the deployment's own
# log secret-shapes list (patterns, not secrets) --
# keep the two in sync by hand, not by re-copying, because of the changes
# made getting from one to the other:
#
#   1. NAMED. The source is grep -f input: one bare pattern per line, no name
#      field. Every line here gets a stable `name<TAB>pattern` instead, so a
#      Finding can say *which* shape fired without saying what matched.
#   2. TRANSLATED. Two source lines use POSIX basic-regex syntax (`\(`, `\)`,
#      `\|`, `\+`, the `[:space:]` bracket class) for grep -f. Python's `re`
#      does not read BRE: an untranslated `\(` matches a literal "(" instead
#      of opening a group, and `[:space:]` inside a bracket expression is
#      not a whitespace class here, just the literal letters s/p/a/c/e. Both
#      were rewritten to plain `re` syntax below (url_query_secret,
#      url_inline_credentials). url_query_secret's trailing `[^&\s]` (one
#      consumed value character) became a lookahead, `(?=[^&\s])`: same
#      match/no-match verdicts, but the match now ends exactly where the
#      value starts. url_inline_credentials's password class is the named
#      group `(?P<value>...)` for the same reason -- scan.py's placeholder
#      check relies on both.
#   3. ADDED. aws_access_key_id and private_key_header do not exist in the
#      source list at all -- it is shaped by what has actually leaked from
#      this homelab's own logs, not by general-purpose credential shapes.
#      Added here because tests/test_scan.py depends on both names.
#   4. CASE-INSENSITIVE key-name shapes. `grep -f` has no `-i`, so the source
#      is case-sensitive throughout. The commonest real way a credential
#      ends up in a published page is an environment-variable dump
#      (`DB_PASSWORD=`, `GITHUB_TOKEN=`, `OPENAI_API_KEY=`), which a
#      case-sensitive lowercase `password=`/`token=`/`api_key` shape does
#      not catch. Shapes that match a KEY NAME are wrapped `(?i:...)` below;
#      url_query_secret was MISSED in that pass and fixed on 21 Sep 2026: an
#      uppercase KEY parameter in a URL query was caught by nothing at all --
#      not by this shape, which was case-sensitive, and not by key_assignment,
#      which wants a bare assignment rather than one after a query separator.
#      The uppercase TOKEN, SECRET and PASSWORD parameters were still caught
#      by their own assignment shapes, so only one of the four fell through,
#      which is exactly how a gap like this survives a careful reading. Found
#      by the shape-by-transformation matrix (homelab-ij8.7) on its first run,
#      which is the whole argument for that matrix.
#      The alternation is spelled out one literal per branch because the
#      shape loader refuses a `(?i:` that opens anything but plain literal
#      text with no quantifier after it -- `(?i:api_)?(?i:key|token|...)` is
#      rejected, and rightly: scan() searches a lowercased copy, and that is
#      only the same search for a group it can fold.
#      shapes that match a fixed-format VALUE (aws_access_key_id, the
#      PEM/age/PGP headers) are left case-sensitive, because those formats
#      are case-strict by spec and loosening them only invites noise.
#      url_query_secret also stays case-sensitive: it is reached through a
#      URL query string, not free text, where the key names in practice are
#      the lowercase ones already in the source.
#   5. bearer_token is no longer a straight translation of the source's
#      "Bearer " line at all -- it now requires a token-shaped value of 16+
#      base64url-ish characters right after "bearer", case-insensitive:
#      `(?i:bearer)\s+[A-Za-z0-9._~+/-]{16,}`. Two measured false starts on
#      the way here, kept in the history as a reminder not to repeat them:
#        - Case-sensitive `Bearer\s` missed lowercase and JSON/dict forms
#          (`{"authorization": "bearer <tok>"}`, curl `-H`), on the theory
#          that RFC-correctness made the scheme name case-sensitive. That
#          reasoning was wrong: RFC 9110 SS11.1 makes auth schemes
#          case-insensitive on the wire.
#        - Blanket `(?i:Bearer)\s` (no value requirement) fired on 32 of 214
#          homelab docs/*.md files, because "bearer" is ordinary prose ("the
#          bearer of this token must present it"). A token-shaped VALUE
#          requirement is what actually distinguishes a credential from
#          prose -- not case -- and it measures at 0 docs files matched
#          while still catching the JSON/dict forms above.
#
# source-sha256: 7cd9c3dd77e93f8fcce8f9b7226e5a77445ac9d420204fa979a9ebb0f500b0a0
#   Hash of the source file's own pattern lines: split the source on "\n",
#   drop only the line terminator (nothing else), keep a line if
#   `line.strip()` is non-empty and does not start with "#" -- i.e. exactly
#   the lines parse_shapes below would keep -- then hash the KEPT lines
#   VERBATIM (no `.strip()`), joined with "\n". 22 lines, in file order
#   (20 until 24 Sep 2026, when the two Mailgun shapes were added to both).
#   Hashing the stripped form (an earlier mistake) cannot see a
#   trailing-whitespace edit: dropping the trailing space from the source's
#   "Bearer " line, which widens grep to bare "Bearer", leaves a stripped
#   hash unchanged -- defeating the one thing this pin exists to catch. If
#   the source changes, this hash goes stale silently -- there is no CI
#   across the two repos to catch it, so re-derive it by hand (exactly as
#   above) when the source is next touched, diff the two files, and update
#   both this hash and the shapes below together.
#
# One shape per line: `name<TAB>pattern`. Blank lines and lines starting with
# # are ignored. A hit means the body is refused, not that the pattern is
# shown: scan.py never reports what matched, only which shape and which line.
# Patterns are compiled with re.MULTILINE and matched against two views of
# the body -- the raw body, and a tag-stripped/entity-unescaped text view --
# each in one pass (not line by line -- see scan.py's module docstring), so
# a pattern must never use \A or \Z (parse_shapes rejects both): those
# anchor the whole view, not a line, and scan() already means the whole
# view (now twice over, once per view).
authorization_header	(?i:Authorization:)
bearer_token	(?i:bearer)\s+[A-Za-z0-9._~+/-]{16,}
password_assignment	(?i:password=)
passwd_assignment	(?i:passwd=)
pass_assignment	(?i:pass=)
api_key_literal	(?i:api_key)
apikey_literal	(?i:apikey)
token_assignment	(?i:token=)
secret_assignment	(?i:secret=)
x_auth_token_header	(?i:X-Auth-Token)
age_encrypted_header	BEGIN AGE ENCRYPTED
pgp_private_key_header	BEGIN PGP PRIVATE
openssh_private_key_header	BEGIN OPENSSH PRIVATE
rsa_private_key_header	BEGIN RSA PRIVATE
ec_private_key_header	BEGIN EC PRIVATE
private_key_field	(?i:PrivateKey =)
preshared_key_field	(?i:PresharedKey =)
credential_set_literal	(?i:credential-set)
url_query_secret	[?&](?:(?i:api_key)|(?i:api_token)|(?i:api_secret)|(?i:api_password)|(?i:key)|(?i:token)|(?i:secret)|(?i:password))=(?=[^&\s])
url_inline_credentials	://[^/\s:]+:(?P<value>[^/\s@]+)@
aws_access_key_id	AKIA[0-9A-Z]{16}
private_key_header	-----BEGIN [A-Z ]*PRIVATE KEY-----
# Mailgun keys, from the source's two lines of 24 Sep 2026 (BRE \{n\} -> re {n}):
# a domain sending key (32 hex-8 hex-8 hex; a UUID is 8-4-4-4-12 and does not
# match) and a legacy private API key. Both cases of hex are spelled out (a
# (?i:) group may only wrap plain text, see parse_shapes).
mailgun_sending_key	\b[0-9a-fA-F]{32}-[0-9a-fA-F]{8}-[0-9a-fA-F]{8}\b
mailgun_private_key	(?i:key)-[0-9a-fA-F]{32}
# Slack tokens (bot xoxb-, user xoxp-, app xoxa-, refresh xoxr-, legacy xoxs-),
# 24 Sep 2026: ggshield caught one in a JS string that no shape here matched.
# Source spells the count in BRE (\{10,\}); translated. The prefix is
# case-insensitive here, as mailgun_private_key's is, so the shape survives the
# uppercased view every shape is tested through.
slack_token	(?i:xox)[abprs]-[0-9A-Za-z-]{10,}
mcp_access_token	(?i:rmo_)[0-9A-Za-z]{20,}
mcp_refresh_token	(?i:rmr_)[0-9A-Za-z]{20,}
