diff --git a/README.md b/README.md index ddb19ee..02bd376 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ Native knowledge, plan, fix and configuration commands send the selected organiz `proofline review --path src/main.rs` reviews only that file's local changes against the normal merge-base. The path is literal and repository-relative; the command refuses directories, globs, and `..`. Combine it with `--staged` to read the index, or `--include-untracked` to select a new, non-ignored file. The command preserves the real index and applies the usual secret preflight. You cannot combine `--path` with pushed-commit, range, or pull-request review. +Before any local patch leaves the checkout, the secret preflight refuses a `.env` path, a private-key marker, or a line that adds a recognised credential: AWS access-key IDs, GitHub, Slack, Google API, Stripe live, OpenAI and Anthropic, and Proofline keys, Azure Storage account keys in connection strings (`AccountKey=`), GitLab personal, deploy and runner tokens, and npm, Hugging Face and PyPI tokens. Azurite's published development key is not refused. The refusal names the file, line and kind, never the value. Pass `--allow-path` with that exact path to upload it anyway. + `proofline reviews --repository-id UUID --cursor CURSOR` returns one page with its continuation metadata, and all flags are optional. `proofline routing REVIEW_ID` reads recorded routing. `proofline run --repo owner/name --pr 7 --run 2` reads one retained run, and `proofline history --repo owner/name --pr 7` reads all recorded runs. These commands can find the reviews your token can still read after a repository disconnects. With a clone URL or Git remote, they resolve the project on its registered host and read its runs by repository ID. A repository with no reviews in the organization exits nonzero. `proofline findings --pr 7` finds the pull request's newest review through the same route and counts only runs of the repository whose full name matches. None of these commands reads the organization's review list. `proofline rerun REVIEW_ID` requests the original immutable subject and waits, while `--no-wait` returns its link. Reruns require `reviews:rerun`. `proofline guidance --repo owner/name` requires `guidance:read`, and `proofline fix-prompt FINDING_ID` requires `findings:read`. History, guidance, and fix prompts print JSON; prompt text remains untrusted. diff --git a/src/preflight.rs b/src/preflight.rs index 592ec54..b0a6d61 100644 --- a/src/preflight.rs +++ b/src/preflight.rs @@ -185,27 +185,36 @@ fn base32(byte: u8) -> bool { byte.is_ascii_uppercase() || matches!(byte, b'2'..=b'7') } -fn has_token(text: &str, prefixes: &[&str], minimum: usize, body: fn(u8) -> bool) -> bool { - prefixes.iter().any(|prefix| { +/// The RFC 4648 base64 alphabet, with padding. +fn base64(byte: u8) -> bool { + byte.is_ascii_alphanumeric() || matches!(byte, b'+' | b'/' | b'=') +} + +fn has_token(text: &str, shape: &TokenShape) -> bool { + shape.prefixes.iter().any(|prefix| { text.match_indices(*prefix).any(|(start, prefix)| { text.get(start + prefix.len()..).is_some_and(|suffix| { - suffix - .bytes() - .take(minimum) - .take_while(|byte| body(*byte)) - .count() - == minimum + !shape.public.iter().any(|public| suffix.starts_with(public)) + && suffix + .bytes() + .take(shape.minimum) + .take_while(|byte| (shape.body)(*byte)) + .count() + == shape.minimum }) }) }) } /// A credential prefix followed by at least `minimum` bytes of `body`. +/// A body that starts with a `public` value is a published example, not a +/// credential. struct TokenShape { kind: &'static str, prefixes: &'static [&'static str], minimum: usize, body: fn(u8) -> bool, + public: &'static [&'static str], } /// Distinctive credential prefixes. When two rows match, the first names @@ -216,24 +225,28 @@ const TOKEN_SHAPES: &[TokenShape] = &[ prefixes: &["AKIA", "ASIA"], minimum: 16, body: upper_or_digit, + public: &[], }, TokenShape { kind: "a GitHub token", prefixes: &["ghp_", "gho_", "ghu_", "ghs_", "ghr_"], minimum: 36, body: alphanumeric, + public: &[], }, TokenShape { kind: "a GitHub token", prefixes: &["github_pat_"], minimum: 82, body: token_byte, + public: &[], }, TokenShape { kind: "a GitHub token", prefixes: &["ghs_"], minimum: 36, body: token_byte, + public: &[], }, TokenShape { kind: "a Slack token", @@ -242,36 +255,80 @@ const TOKEN_SHAPES: &[TokenShape] = &[ ], minimum: 8, body: token_byte, + public: &[], }, TokenShape { kind: "a Google API key", prefixes: &["AIza"], minimum: 35, body: token_byte, + public: &[], }, TokenShape { kind: "a Stripe live key", prefixes: &["sk_live_", "rk_live_"], minimum: 24, body: alphanumeric, + public: &[], }, TokenShape { kind: "a model-provider key", prefixes: &["sk-"], minimum: 48, body: alphanumeric, + public: &[], }, TokenShape { kind: "a model-provider key", prefixes: &["sk-proj-", "sk-svcacct-", "sk-admin-", "sk-ant-"], minimum: 40, body: token_byte, + public: &[], }, TokenShape { kind: "a Proofline token", prefixes: &["plp_", "plo_", "pld_"], minimum: 52, body: base32, + public: &[], + }, + TokenShape { + kind: "an Azure Storage account key", + prefixes: &["AccountKey="], + minimum: 88, + body: base64, + // Azurite's documented development key for `devstoreaccount1`. + public: &[ + "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==", + ], + }, + TokenShape { + kind: "a GitLab token", + prefixes: &["glpat-", "gldt-", "glrt-"], + minimum: 20, + body: token_byte, + public: &[], + }, + TokenShape { + kind: "an npm token", + prefixes: &["npm_"], + minimum: 36, + body: alphanumeric, + public: &[], + }, + TokenShape { + kind: "a Hugging Face token", + prefixes: &["hf_"], + minimum: 34, + body: alphanumeric, + public: &[], + }, + TokenShape { + kind: "a PyPI token", + prefixes: &["pypi-AgEIcHlwaS5vcmc"], + minimum: 50, + body: token_byte, + public: &[], }, ]; @@ -285,7 +342,7 @@ fn secret_shape(text: &str) -> Option<&'static str> { } TOKEN_SHAPES .iter() - .find(|shape| has_token(text, shape.prefixes, shape.minimum, shape.body)) + .find(|shape| has_token(text, shape)) .map(|shape| shape.kind) } @@ -344,6 +401,29 @@ mod tests { '8', "a Proofline token", ), + ( + &["AccountKey="][..], + 88, + "Az09+/=", + ';', + "an Azure Storage account key", + ), + ( + &["glpat-", "gldt-", "glrt-"][..], + 20, + "Az09_.-", + '!', + "a GitLab token", + ), + (&["npm_"][..], 36, "Az09", '_', "an npm token"), + (&["hf_"][..], 34, "Az09", '_', "a Hugging Face token"), + ( + &["pypi-AgEIcHlwaS5vcmc"][..], + 50, + "Az09_-", + '!', + "a PyPI token", + ), ]; for (prefixes, minimum, alphabet, invalid, label) in cases { let body: String = alphabet.chars().cycle().take(minimum).collect(); @@ -381,6 +461,33 @@ mod tests { assert_eq!(secret_shape("-----BEGIN CERTIFICATE-----"), None); } + #[test] + fn azure_connection_strings_refuse_account_keys_but_accept_placeholders() { + let key = format!("{}==", "Ab0+/".repeat(18).get(..86).unwrap()); + let connection = format!( + "DefaultEndpointsProtocol=https;AccountName=demo;AccountKey={key};EndpointSuffix=core.windows.net" + ); + assert_eq!( + secret_shape(&connection), + Some("an Azure Storage account key") + ); + for placeholder in [ + "DefaultEndpointsProtocol=https;AccountName=demo;AccountKey=", + "DefaultEndpointsProtocol=https;AccountName=demo;AccountKey=${AZURE_STORAGE_KEY}", + "AccountKey=;EndpointSuffix=core.windows.net", + ] { + assert_eq!(secret_shape(placeholder), None, "{placeholder}"); + } + } + + #[test] + fn azurite_development_key_is_not_a_credential() { + let azurite = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"; + assert_eq!(secret_shape(azurite), None); + let real = azurite.replacen("Eby8", "Fby8", 1); + assert_eq!(secret_shape(&real), Some("an Azure Storage account key")); + } + #[test] fn ordinary_text_has_no_secret_shape() { assert_eq!( diff --git a/tests/local_patch.rs b/tests/local_patch.rs index 1c41110..327d8f5 100644 --- a/tests/local_patch.rs +++ b/tests/local_patch.rs @@ -679,6 +679,51 @@ async fn added_context_deleted_and_quoted_content_refuses_before_http() { } } +#[tokio::test] +async fn cloud_and_registry_credentials_refuse_before_http() { + let azure = format!( + "DefaultEndpointsProtocol=https;AccountName=demo;AccountKey={}==;EndpointSuffix=core.windows.net", + "K".repeat(86) + ); + let gitlab = format!("glpat-{}", "g".repeat(20)); + let npm = format!("npm_{}", "n".repeat(36)); + let hugging_face = format!("hf_{}", "h".repeat(34)); + let pypi = format!("pypi-AgEIcHlwaS5vcmc{}", "p".repeat(50)); + for (path, secret, kind) in [ + ( + "appsettings.json", + azure.as_str(), + "an Azure Storage account key", + ), + (".gitlab-ci.yml", gitlab.as_str(), "a GitLab token"), + (".npmrc", npm.as_str(), "an npm token"), + ("train.py", hugging_face.as_str(), "a Hugging Face token"), + (".pypirc", pypi.as_str(), "a PyPI token"), + ] { + let checkout = Checkout::new(&[]).await; + checkout + .write(path, &format!("first\nvalue = \"{secret}\"\n")) + .await; + let index = checkout.index().await; + let server = Server::start(true).await; + let output = checkout + .review(&server, &[], &["--include-untracked"]) + .await; + assert_refusal( + &output, + &format!("contains {kind} at \"{path}\":2 (new line)"), + secret, + ); + server.assert_no_requests(); + assert_eq!(checkout.index().await, index); + let output = checkout + .review(&server, &[path], &["--include-untracked"]) + .await; + assert!(output.status.success(), "{output:?}"); + server.close().await; + } +} + #[tokio::test] async fn exact_overrides_keep_other_paths_blocked_and_upload_the_original_patch() { let github = format!("ghp_{}", "B".repeat(36));