Skip to content

Find the holes before someone else does. A fast, 100% Rust vulnerability scanner: XSS, SQL injection, leaked secrets, dependency CVEs and AI agent setups.

test: build fake tokens at run time so the repo holds none

Kam162e38f1efe6
diff --git a/tests/cli.rs b/tests/cli.rs
index 87a495e..db355ef 100644
--- a/tests/cli.rs
+++ b/tests/cli.rs
@@ -1,3 +1,5 @@
+mod common;
+
use std::path::Path;
use std::process::Command;
@@ -14,11 +16,7 @@ fn run(args: &[&str]) -> (i32, String) {
}
fn fixture(name: &str) -> String {
- Path::new(env!("CARGO_MANIFEST_DIR"))
- .join("tests/fixtures")
- .join(name)
- .to_string_lossy()
- .into_owned()
+ common::fixtures().join(name).to_string_lossy().into_owned()
}
#[test]
@@ -181,19 +179,21 @@ fn mcp_server_scans_over_stdio() {
#[test]
fn secrets_are_redacted_in_every_output() {
- let token_tail = "Z9y8X7w6V5u4T3s2";
+ let token = common::fake("github");
+ let token_tail = &token[token.len() - 16..];
+ let password = common::fake("password");
+ let aws = common::fake("aws");
for args in [
vec![fixture("agent"), "--offline".into()],
vec![fixture("agent"), "--offline".into(), "--json".into()],
vec![fixture("secrets"), "--offline".into(), "--sarif".into()],
+ vec![fixture("secrets"), "--offline".into()],
] {
let args: Vec<&str> = args.iter().map(String::as_str).collect();
let (_, out) = run(&args);
assert!(!out.contains(token_tail), "token leaked with {args:?}");
- assert!(
- !out.contains("hunter2hunter2"),
- "password leaked with {args:?}"
- );
+ assert!(!out.contains(&password), "password leaked with {args:?}");
+ assert!(!out.contains(&aws[4..]), "AWS key leaked with {args:?}");
}
let (_, out) = run(&[&fixture("agent"), "--offline"]);
assert!(
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
new file mode 100644
index 0000000..5cb7192
--- /dev/null
+++ b/tests/common/mod.rs
@@ -0,0 +1,89 @@
+use std::path::{Path, PathBuf};
+use std::sync::OnceLock;
+
+fn body(n: usize, alphabet: &str) -> String {
+ alphabet.chars().cycle().take(n).collect()
+}
+
+const MIXED: &str = "Q7mZ2xK9pL4vR8tN3wB6yH1cF5jD0sG";
+const UPPER: &str = "Q7MZ2XK9PL4VR8TN3WB6YH1CF5JD0SG";
+
+pub fn fake(name: &str) -> String {
+ match name {
+ "aws" => ["AK", "IA", &body(16, UPPER)].concat(),
+ "github" => ["gh", "p_", &body(36, MIXED)].concat(),
+ "github-pat" => ["github", "_pat_", &body(82, MIXED)].concat(),
+ "slack" => [
+ "xo",
+ "xb-",
+ "4815162342-",
+ "1123581321345-",
+ &body(24, MIXED),
+ ]
+ .concat(),
+ "stripe-live" => ["sk", "_live_", &body(24, MIXED)].concat(),
+ "stripe-test" => ["sk", "_test_", &body(24, MIXED)].concat(),
+ "anthropic" => ["sk-", "ant-api03-", &body(40, MIXED)].concat(),
+ "openai" => ["sk-", "proj-", &body(48, MIXED)].concat(),
+ "google" => ["AI", "za", &body(35, MIXED)].concat(),
+ "password" => ["hunter2", "hunter2"].concat(),
+ "db-password" => ["S3cret", "Passw0rd"].concat(),
+ "random" => body(40, MIXED),
+ "private-key" => [
+ "-----BEGIN RSA ",
+ "PRIVATE KEY-----\n",
+ "MIIEowIBAAKCAQEA",
+ &body(48, MIXED),
+ ]
+ .concat(),
+ other => panic!("no fake value named {other}"),
+ }
+}
+
+fn fill(text: &str) -> String {
+ let mut out = String::with_capacity(text.len());
+ let mut rest = text;
+ while let Some(start) = rest.find("{{fake:") {
+ out.push_str(&rest[..start]);
+ let tail = &rest[start + 7..];
+ let end = tail.find("}}").expect("closing }} for fake placeholder");
+ out.push_str(&fake(&tail[..end]));
+ rest = &tail[end + 2..];
+ }
+ out.push_str(rest);
+ out
+}
+
+fn copy(from: &Path, to: &Path) {
+ std::fs::create_dir_all(to).unwrap();
+ for entry in std::fs::read_dir(from).unwrap() {
+ let entry = entry.unwrap();
+ let target = to.join(entry.file_name());
+ if entry.file_type().unwrap().is_dir() {
+ copy(&entry.path(), &target);
+ } else {
+ let bytes = std::fs::read(entry.path()).unwrap();
+ match String::from_utf8(bytes) {
+ Ok(text) => std::fs::write(&target, fill(&text)).unwrap(),
+ Err(e) => std::fs::write(&target, e.into_bytes()).unwrap(),
+ }
+ }
+ }
+}
+
+pub fn fixtures() -> &'static Path {
+ static DIR: OnceLock<PathBuf> = OnceLock::new();
+ DIR.get_or_init(|| {
+ let dir = std::env::temp_dir().join(format!(
+ "safe-anything-fixtures-{}-{}",
+ env!("CARGO_CRATE_NAME"),
+ std::process::id()
+ ));
+ let _ = std::fs::remove_dir_all(&dir);
+ copy(
+ &Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures"),
+ &dir,
+ );
+ dir
+ })
+}
diff --git a/tests/fixtures/agent/.mcp.json b/tests/fixtures/agent/.mcp.json
index 5a2a368..d9afb4c 100644
--- a/tests/fixtures/agent/.mcp.json
+++ b/tests/fixtures/agent/.mcp.json
@@ -4,7 +4,7 @@
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
- "GITHUB_TOKEN": "ghp_Z9y8X7w6V5u4T3s2R1q0P9o8N7m6L5k4J3i2"
+ "GITHUB_TOKEN": "{{fake:github}}"
}
},
"pinned": {
diff --git a/tests/fixtures/agent/projects/x/session.jsonl b/tests/fixtures/agent/projects/x/session.jsonl
index 4df81d3..27c2af4 100644
--- a/tests/fixtures/agent/projects/x/session.jsonl
+++ b/tests/fixtures/agent/projects/x/session.jsonl
@@ -1 +1 @@
-{"password":"hunter2hunter2"}
+{"password":"{{fake:password}}"}
diff --git a/tests/fixtures/code/web/login.spec.ts b/tests/fixtures/code/web/login.spec.ts
index 5ab5a9c..3d0cd79 100644
--- a/tests/fixtures/code/web/login.spec.ts
+++ b/tests/fixtures/code/web/login.spec.ts
@@ -1,3 +1,3 @@
-const password = 'hunter2hunter2';
+const password = '{{fake:password}}';
localStorage.setItem('token', 'abc');
-const k = 'AKIAIOSFODNN7EXAMPLQ';
+const k = '{{fake:aws}}';
diff --git a/tests/fixtures/secrets/app/db.js b/tests/fixtures/secrets/app/db.js
index 63b4e3e..62e6928 100644
--- a/tests/fixtures/secrets/app/db.js
+++ b/tests/fixtures/secrets/app/db.js
@@ -1,4 +1,4 @@
-const url = "postgres://admin:[email protected]:5432/app";
+const url = "postgres://admin:{{fake:db-password}}@db.acme-prod.io:5432/app";
const local = "postgres://user:password@localhost:5432/app";
const tpl = `postgres://${user}:${pass}@${host}/db`;
// a URL such as https://user:token@host/repo.git is redacted
diff --git a/tests/fixtures/secrets/config.py b/tests/fixtures/secrets/config.py
index 9708a76..a7abab9 100644
--- a/tests/fixtures/secrets/config.py
+++ b/tests/fixtures/secrets/config.py
@@ -1,14 +1,14 @@
-aws_key = "AKIAIOSFODNN7EXAMPLQ"
+aws_key = "{{fake:aws}}"
not_a_key = "AKIA-short"
-GITHUB = "ghp_A1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6Q7r8"
-pat = "github_pat_11ABCDEFG0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
-slack = "xoxb-123456789012-1234567890123-AbCdEfGhIjKlMnOpQrStUvWx"
-stripe = "sk_live_4eC39HqLyjWDarjtT1zdp7dc"
-stripe_test = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
-ANTHROPIC = "sk-ant-api03-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
-OPENAI = "sk-proj-Ab1Ab1Ab1Ab1Ab1Ab1Ab1Ab1Ab1Ab1Ab1Ab1Ab1Ab1Ab1Ab1"
-google = "AIzaSyA1234567890abcdefghijklmnopqrstuv"
-password = "hunter2hunter2"
+GITHUB = "{{fake:github}}"
+pat = "{{fake:github-pat}}"
+slack = "{{fake:slack}}"
+stripe = "{{fake:stripe-live}}"
+stripe_test = "{{fake:stripe-test}}"
+ANTHROPIC = "{{fake:anthropic}}"
+OPENAI = "{{fake:openai}}"
+google = "{{fake:google}}"
+password = "{{fake:password}}"
password = os.environ["DB_PASSWORD"]
password = "changeme"
api_key = "${API_KEY}"
diff --git a/tests/fixtures/secrets/keys/deploy.pem b/tests/fixtures/secrets/keys/deploy.pem
index 46ed73e..11e7866 100644
--- a/tests/fixtures/secrets/keys/deploy.pem
+++ b/tests/fixtures/secrets/keys/deploy.pem
@@ -1,3 +1,2 @@
------BEGIN RSA PRIVATE KEY-----
-MIIEowIBAAKCAQEAFAKEfake0123456789
+{{fake:private-key}}
-----END RSA PRIVATE KEY-----
diff --git a/tests/scan.rs b/tests/scan.rs
index 2e95b4e..29a9335 100644
--- a/tests/scan.rs
+++ b/tests/scan.rs
@@ -1,14 +1,13 @@
+mod common;
+
use std::collections::BTreeSet;
-use std::path::Path;
fn check(family: &str) {
check_with(family, safe_anything::Options::offline(), None);
}
fn check_with(family: &str, options: safe_anything::Options, packages: Option<usize>) {
- let root = Path::new(env!("CARGO_MANIFEST_DIR"))
- .join("tests/fixtures")
- .join(family);
+ let root = common::fixtures().join(family);
let expected: BTreeSet<String> = std::fs::read_to_string(root.join("EXPECTED"))
.unwrap()
.lines()