mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-24 09:01:28 +01:00
Secrets check flags keys in Rust let, const and static bindings and stops flagging Rust type annotations (GST-3474) (#4675)
The secrets check now catches a key written as `let token = "";` (or `let mut`, `const`, `static`) in a Rust file, and no longer flags Rust type annotations like `token: Option,` or match arms like `Token::Char(c) =>`. Why: a real key in that Rust form passed the check, while every Rust change needed allowlist entries for type annotations (seen on garrytan/gbrain#10028). Nothing to do: files in other languages are checked as before. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/specialist-batched#1 reader=specialist-batched Review-Finding: p1/red-team#1 reader=red-team Review-Finding: p1/red-team#2 reader=red-team Review-Finding: p1/red-team#3 reader=red-team
This commit is contained in:
1 parent
7df6466b99
commit
75410da71f
6 files changed
+223
No files matched your search
@@ -129,6 +129,16 @@ describe('pregate secrets: source context and diff integrity', () => {
|
||||
expect({ status: r.status, rows: r.lines.filter((l) => l.includes('env.kv')).sort() }).toEqual({ status: 'fail', rows: [expect.stringContaining('(app/storage.py, line 5)'), expect.stringContaining('(src/launch.ts, added line 6)')] });
|
||||
});
|
||||
|
||||
// Value: protects=pregate's secrets check passes a Rust file of type annotations and fails a key bound with let or const in a .rs file; fails_when=Rust
|
||||
// type annotations block a push again, or a let or const binding of a key passes it; why_new=GST-3474 came from gbrain #10028's pregate runs; seam=fixture repo; no_claim=other languages
|
||||
test('Rust type annotations pass; a key bound with let or const in a .rs file still fails (GST-3474)', () => {
|
||||
const secret = ['sk-live-FAKE-', 'Qx9vL2mZ', 'p8Rt4Kw7', 'Yn3Bh2Jd'].join('');
|
||||
const rs = "pub struct Index<'a> {\n token: Option<&'a str>,\n key: &'a RelationshipKey,\n by_key: Mutex<HashMap<String, u32>>,\n}\n";
|
||||
expect(repo({ 'src/index.rs': rs }).secrets()).toMatchObject({ status: 'pass', lines: [] });
|
||||
const r = repo({ 'src/index.rs': rs + 'fn connect() {\n let token = "' + secret + '";\n}\nconst API_KEY: &str = "' + secret + '";\n' }).secrets();
|
||||
expect({ status: r.status, rows: r.lines.filter((l) => l.includes('env.kv')).sort() }).toEqual({ status: 'fail', rows: [expect.stringContaining('(src/index.rs, added line 7)'), expect.stringContaining('(src/index.rs, added line 9)')] });
|
||||
});
|
||||
|
||||
test('parseAddedLineNumbers numbers each added line from its hunk header', () => {
|
||||
const diff = ['diff --git a/a.py b/a.py', '--- a/a.py', '+++ b/a.py', '@@ -3,0 +4,2 @@', '+one', '+two', '@@ -9 +11 @@', '-old', '+new'].join('\n');
|
||||
expect([...parseAddedLineNumbers(diff)]).toEqual([['a.py', [4, 5, 11]]]);
|
||||
|
||||
@@ -51,6 +51,7 @@ const SAMPLES: Record<string, string> = {
|
||||
"google.api_key": j("AIza", "SyA1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6Q"),
|
||||
"jwt": ["eyJhbGciOiJIUzI1NiJ9", "eyJzdWIiOiIxMjM0NTY3ODkwIn0", "dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U"].join("."),
|
||||
"env.kv": j("API_TOKEN=", "8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ"),
|
||||
"env.kv.rust": j('let api_token = "', "8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ", '";'),
|
||||
"auth.bearer": j("Authorization: Bearer ", "a1B2c3D4e5F6g7H8i9J0k1L2"),
|
||||
"pii.email": j("dana.whitfield@", "acmecorp.io"),
|
||||
"pii.phone.e164": j("+1415", "5552671"),
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* GST-3474 (gbrain #10028): env.kv did not read Rust. A secret bound with
|
||||
* `let`, `let mut`, `const` or `static` (`let token = "<key>";`) gave no
|
||||
* finding, because the name had to start the line. And a Rust type annotation
|
||||
* (`token: Option<&str>,`, `key: &RelationshipKey,`) or a match arm's path
|
||||
* (`Token::Char(c) => {`) read as a secret-named value, so every Rust landing
|
||||
* needed allowlist entries. The binding now reports as env.kv.rust in a .rs
|
||||
* file or a scan without a path, and a type annotation in a .rs file passes;
|
||||
* every hold case below puts a secret in the same position and still reports.
|
||||
*/
|
||||
// Value: protects=a secret in a Rust let, const or static binding reports and a Rust type annotation passes; fails_when=the binding miss returns, a type annotation reports again, the binding rule fires in another language's file, or the annotation exemption lets a literal, a random name or a second field through; why_new=GST-3474 had no Rust case; seam=none; no_claim=scanner-level only, the pregate case lives in pregate-secrets-source-context.test.ts
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { scan } from "../lib/redact-engine";
|
||||
|
||||
const ids = (text: string, sourcePath?: string) =>
|
||||
scan(text, { repoVisibility: "private", processEnv: {}, ...(sourcePath ? { sourcePath } : {}) }).findings.map((f) => f.id);
|
||||
const S = "Qx9vL2mZp8Rt4Kw7Yn3Bh2Jd";
|
||||
const FAKE = ["sk", "live", "FAKE", S].join("-");
|
||||
const LETTERS = "QxvLmZpRtKwYnBhJdTqWsV";
|
||||
const has = (id: string, cases: ReadonlyArray<readonly [string, string | undefined]>, want: boolean) => {
|
||||
for (const [code, file] of cases) expect({ code, file, hit: ids(code, file).includes(id) }).toEqual({ code, file, hit: want });
|
||||
};
|
||||
|
||||
describe("env.kv.rust: a secret in a Rust binding reports (GST-3474)", () => {
|
||||
test("let, let mut, const, static and their visibility, type and literal forms report", () => {
|
||||
has("env.kv.rust", [
|
||||
['let token = "' + FAKE + '";', "src/lib.rs"],
|
||||
[' let token = "' + S + '";', "src/lib.rs"],
|
||||
['let mut api_key = "' + S + '";', "src/lib.rs"],
|
||||
['const API_KEY: &str = "' + S + '";', "src/lib.rs"],
|
||||
["pub const API_KEY: &'static str = \"" + S + '";', "src/lib.rs"],
|
||||
['pub(crate) static SESSION_SECRET: &str = "' + S + '";', "src/lib.rs"],
|
||||
['static mut AUTH_TOKEN: &str = "' + S + '";', "src/lib.rs"],
|
||||
[' let token: &str = "' + S + '";', "src/lib.rs"],
|
||||
[' let client_secret = String::from("' + S + '");', "src/lib.rs"],
|
||||
[' let token = "' + S + '".to_string();', "src/lib.rs"],
|
||||
[' let token = r#"' + S + '"#;', "src/lib.rs"],
|
||||
[' let password = b"' + S + '";', "src/lib.rs"],
|
||||
[' // let token = "' + S + '";', "src/lib.rs"],
|
||||
['let token = "' + S + '";', undefined],
|
||||
['diff --git a/src/lib.rs b/src/lib.rs\n+++ b/src/lib.rs\n@@ -0,0 +1 @@\n+ let token = "' + S + '";\n', undefined],
|
||||
], true);
|
||||
});
|
||||
|
||||
test("benign: a placeholder, an environment read, a short or low-entropy value and a non-credential name give no finding", () => {
|
||||
has("env.kv.rust", [
|
||||
['let token = "changeme";', "src/lib.rs"],
|
||||
['let token = "${API_TOKEN}";', "src/lib.rs"],
|
||||
['let token = env::var("API_TOKEN")?;', "src/lib.rs"],
|
||||
['let token = std::env::var("GITHUB_TOKEN").unwrap_or_default();', "src/lib.rs"],
|
||||
['let token = "short";', "src/lib.rs"],
|
||||
["let token = None;", "src/lib.rs"],
|
||||
['let monkey = "' + S + '";', "src/lib.rs"],
|
||||
['let token_count = "' + S + '";', "src/lib.rs"],
|
||||
], false);
|
||||
});
|
||||
|
||||
test("scoped to Rust: the same binding in another language's file is not read by the Rust rule", () => {
|
||||
has("env.kv.rust", [
|
||||
['let token = "' + S + '";', "src/client.ts"],
|
||||
['const token = "' + S + '";', "src/client.js"],
|
||||
['static token = "' + S + '"', "app/client.py"],
|
||||
], false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("env.kv: a Rust type annotation is code, not a secret (GST-3474)", () => {
|
||||
test("struct fields, parameters and match-arm paths in a .rs file pass", () => {
|
||||
has("env.kv", [
|
||||
[" token: Option<&str>,", "src/lib.rs"],
|
||||
[" key: &RelationshipKey,", "src/lib.rs"],
|
||||
[" by_key: Mutex<HashMap<String, u32>>,", "src/lib.rs"],
|
||||
[" Token::Char(c) => {", "src/lib.rs"],
|
||||
[" session: Arc<Mutex<Session>>,", "src/lib.rs"],
|
||||
[" auth: Box<dyn Fn(&str) -> bool + Send + Sync>,", "src/lib.rs"],
|
||||
[" private_key: &'a [u8; 32],", "src/lib.rs"],
|
||||
[" session_token: Option<SessionToken>, // refreshed on login", "src/lib.rs"],
|
||||
[" private_key: [u8; KEY_LEN],", "src/lib.rs"],
|
||||
], false);
|
||||
});
|
||||
|
||||
test("hold: a literal, a random name, a second field, a quoted name, a comment and other files still report", () => {
|
||||
has("env.kv", [
|
||||
[' token: "' + S + '",', "src/lib.rs"],
|
||||
[" key: &" + S + ",", "src/lib.rs"],
|
||||
[" token: " + LETTERS + ",", "src/lib.rs"],
|
||||
[' token: Option<&str> = "' + S + '",', "src/lib.rs"],
|
||||
[" token: Option<&str>, other: " + S + ",", "src/lib.rs"],
|
||||
[' "token": Option<&str>,', "src/lib.rs"],
|
||||
[" token: Option<&str>, // token: " + S, "src/lib.rs"],
|
||||
["// token: " + S, "src/lib.rs"],
|
||||
[" token: Option<&str>,", "deploy/values.yaml"],
|
||||
[" key: &RelationshipKey,", undefined],
|
||||
[" Token::Char(c) => {", "src/lexer.ts"],
|
||||
], true);
|
||||
});
|
||||
|
||||
test("hold: a credential line inside a string, a struct literal's value and a comma-less line still report", () => {
|
||||
has("env.kv", [
|
||||
['const CFG: &str = r#"\nstripe:\n token: qxvlmzprtkwynbhjdtwa\n"#;\n', "src/cfg.rs"],
|
||||
['const CFG: &str = "\nauth:\n token: QXVLMZPRTKWYNBHJ\n";\n', "src/cfg.rs"],
|
||||
[" password: Hunter2password", "src/cfg.rs"],
|
||||
[" token: auth_token_value,", "src/lib.rs"],
|
||||
], true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user