Skip to content

Preflight more credential families - #97

Open
antonyth18 wants to merge 6 commits into
prooflinedev:mainfrom
antonyth18:preflight-more-credential-families
Open

antonyth18 wants to merge 6 commits into
prooflinedev:mainfrom
antonyth18:preflight-more-credential-families

Conversation

@antonyth18

Copy link
Copy Markdown

Closes #86

Problem

Before proofline review uploads local changes, preflight::check refuses patches containing known credential shapes. It didn't recognise Azure Storage account keys, GitLab PATs, npm tokens, Hugging Face tokens or PyPI tokens, so a patch adding one was uploaded without warning, both by review and by the git hooks. Azure is the notable gap, since Proofline supports Azure.

Approach

GitLab, npm, Hugging Face, PyPI are new TOKEN_SHAPES rows, following the existing prefix plus minimum-length design (no right-hand boundary, like every other row). Two new body functions match the real encodings: base64 for Azure and base64url for GitLab and PyPI.

Azure is a dedicated check in secret_shape rather than a table row, for two reasons:

  • Microsoft documents connection-string keywords as case-insensitive, which has_token's exact prefix matching can't express without touching every row.
  • The Azurite exception (below) has to skip an exact span, and doing that in the table meant rewriting the line before matching.

It finds accountkey= in any casing, then requires 86 base64 characters. A real key is 88 characters including == padding; the padding isn't checked. Field names ending in AccountKey, such as StorageAccountKey, are matched deliberately, since they commonly hold real keys.

Azurite. The emulator's well-known key is published by Microsoft and isn't a secret. The Azure check skips that exact key and nothing else; other families are still checked against the original text. I chose to exempt it because hooks call preflight::check without --allow-path, so flagging it would leave any repo with Azurite fixtures permanently "unreviewed" by the hook whenever those lines appear in a diff. The cost is a special case in a deliberately simple file, which is why it has the most tests.

The Azure check runs after the token rows, so the original rows keep their label priority.

Sources

Family Rule Source
Azure accountkey= (any casing) + ≥86 base64 Connection strings; Azurite key from Connect to Azurite
GitLab glpat- + ≥20 base64url Personal access tokens
npm npm_ + ≥36 alphanumeric About access tokens
Hugging Face hf_ + ≥34 alphanumeric User access tokens documents only the prefix. I measured one real read-only token (since revoked): 34 letters, mixed case, no digits, matching the gitleaks rule. Alphanumeric is a deliberate superset in case the format changes.
PyPI pypi- + ≥85 base64url API tokens. I used the shorter pypi- prefix instead of the longer one in the issue: with an 85-character minimum, false positives are negligible, and the rule doesn't depend on the encoded bytes that follow the prefix.

Verification

Tests first. The tests were committed before the implementation (git log --oneline -7) and failed for the right reason:

Test Before the implementation
credential_families_refuse_minimum_bodies_… failed at the Azure row: left: None, right: Some("an Azure Storage account key")
an_azure_storage_connection_string_refuses_before_http failed: expected exit 1, got exit 0, and the patch was uploaded
identifiers_and_placeholders_near_credential_prefixes_… passed, as expected; it guards against the rules catching too much

Tests added

  • A table row per family: refused at exactly the minimum length, accepted one character short, and accepted when the last byte is invalid. Extra rows use the other alphabet's punctuation (-/_ for Azure, +// for GitLab and PyPI), so base64 and base64url can't be confused.
  • Strings that must not be flagged: npm_config_registry, self.hf_model, AccountKey=<your-key>, AccountKey=${STORAGE_KEY}, and the Azurite connection string.
  • The Azurite exception: extra bytes on either side, two Azurite keys on one line, a real key before or after an Azurite key, and a GitHub token overlapping the key.
  • StorageAccountKey= is flagged, and a GitHub token's label wins over an Azure key on the same line.
  • End-to-end in tests/local_patch.rs: a tracked appsettings.json gains an Azure connection string. proofline review exits 1, names the location and the label, doesn't echo the key, and the fake server receives zero requests. The file is tracked and isn't named .env, so the test exercises the new detection rather than the untracked-file or path rules.

Mutation testing. I disabled or altered each check in turn to confirm a test catches it:

Mutation Caught by
Right boundary off a base64 byte after the padding
= check on the right off a = byte after the padding
= counted on the left two Azurite keys on one line, and the Azurite negative case
Only the first Azurite occurrence skipped two Azurite keys on one line
Only the last AccountKey= checked a real key before an Azurite key
Case-sensitive keyword minimum body for AccountKey=
Only the old two casings minimum body for ACCOUNTKEY=
Exception applied to every family a GitHub token overlapping the key
Azure checked before the token rows label priority test
Boundary required before the keyword StorageAccountKey= test
base64url accepts + or / GitLab rows with + and /
base64 accepts - or _ Azure rows with - and _

Mutation testing also changed the code: a left-boundary check turned out to be unreachable, since a candidate always follows the keyword's =, so I removed it and documented the invariant.

CI checks. fmt, clippy, test and deny all pass, rebased onto current main. One test, unattended_registration_preserves_settings_and_uses_the_installed_binary, also fails on main on macOS (/var versus /private/var). It's unrelated, and CI doesn't run on macOS.

Reproduce

git switch preflight-more-credential-families
cargo test --locked preflight
cargo test --locked --test local_patch an_azure_storage_connection_string_refuses_before_http

Proofline review

I installed Proofline on my account and reviewed this branch as a PR in my fork (review). It took four runs:

  • Run 1. Proofline found that the Azurite exception blanked the key before every family was checked, so a token overlapping it was missed. My tests covered a token next to the key, not one overlapping it. I fixed it and added a test. It also found that the alphabet tests couldn't tell base64 and base64url apart; I added rows for that. I dismissed a warning about cloning the line as negligible.
  • Run 2. Three findings (only two casings matched, and the line clone, reported twice) all came from keeping Azure in the table. Together they justified moving Azure into a dedicated check, which fixed all three. I also added a test with two Azurite keys on one line.
  • Run 3. I fixed comments that implied fixed lengths and added a label-priority test. I dismissed a suggested boundary before AccountKey=, because it would miss StorageAccountKey; the new test fails under that change.
  • Run 4. I added a test for a real key before an Azurite key.

One finding came up in every run: npm_ followed by a 64-character hex digest is flagged. I dismissed it each time. No row in this file has a right-hand boundary, including fixed-length ghp_, so exact-length matching would be a design change for the whole table. Happy to open a separate issue if that's wanted.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Local secret preflight doesn't recognise Azure storage keys, GitLab, npm, Hugging Face or PyPI tokens

1 participant