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(agent): unit tests for MCP command parsing, skip flag values (#98)

Onur Ugur7c95157ec94f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a59badd..ef700f6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,9 @@
`user:${PASSWORD}` are skipped, `admin:<password>@${DB_HOST}` and `${TOKEN:-sk_live_...}` are
reported.
- Agent: `agent.unrestricted-shell` now reads `permissions.allow` in `.claude/settings*.json` instead of matching `"Bash"` anywhere in the file, so entries in `deny`, `ask`, or hook matchers are no longer flagged.
+- Agent: `agent.mcp-unpinned` skips the value of flags like `--python` (`uvx`, `pipx run`) and `--user`
+ or `--platform` (`docker run`, `podman run`), so `uvx --python 3.12 pkg==1.0` no longer reports `3.12`
+ and `docker run -u 1000:1000 acme/mcp` reports `acme/mcp`. An empty `docker run` argument is skipped.
- Secrets: `secret.env-not-ignored` is reported at the `.env` file when the project has no `.gitignore`
at all. Example files like `.env.example` alone no longer trigger it.
- Secrets: `secret.env-not-ignored` reads `.gitignore` the way git does, so only a line that really ignores
diff --git a/src/rules/agent.rs b/src/rules/agent.rs
index 9b27a9f..4a2a562 100644
--- a/src/rules/agent.rs
+++ b/src/rules/agent.rs
@@ -503,6 +503,89 @@ fn pat_word(part: &str) -> bool {
})
}
+/// Flags of `uvx` (`uv tool run`) and `pipx run` whose value is the next argument, so it is not
+/// taken for the package.
+const PYTHON_RUNNER_VALUE_FLAGS: &[&str] = &[
+ "-p",
+ "--python",
+ "--with-editable",
+ "--with-requirements",
+ "-c",
+ "--constraints",
+ "--overrides",
+ "--index",
+ "--default-index",
+ "-i",
+ "--index-url",
+ "--extra-index-url",
+ "-f",
+ "--find-links",
+ "--env-file",
+ "--directory",
+ "--project",
+ "--cache-dir",
+ "--config-file",
+ "--pip-args",
+];
+
+/// Flags of `docker run` and `podman run` whose value is the next argument, so it is not taken for
+/// the image.
+const CONTAINER_RUN_VALUE_FLAGS: &[&str] = &[
+ "-e",
+ "--env",
+ "--env-file",
+ "-v",
+ "--volume",
+ "--mount",
+ "-p",
+ "--publish",
+ "--name",
+ "--network",
+ "--net",
+ "-w",
+ "--workdir",
+ "-u",
+ "--user",
+ "--entrypoint",
+ "--platform",
+ "-h",
+ "--hostname",
+ "-l",
+ "--label",
+ "--add-host",
+ "-m",
+ "--memory",
+ "--cpus",
+ "--pull",
+ "--restart",
+ "--cap-add",
+ "--cap-drop",
+ "--device",
+ "--security-opt",
+ "--tmpfs",
+ "--gpus",
+];
+
+/// The first argument that is not a flag, not the value of one of `value_flags`, not empty and not
+/// one of `skip`.
+fn first_operand<'a>(
+ args: &'a [String],
+ value_flags: &[&str],
+ skip: &[&str],
+) -> Option<&'a String> {
+ let mut it = args.iter();
+ while let Some(a) = it.next() {
+ if a.starts_with('-') {
+ if value_flags.contains(&a.as_str()) {
+ it.next();
+ }
+ } else if !a.is_empty() && !skip.contains(&a.as_str()) {
+ return Some(a);
+ }
+ }
+ None
+}
+
fn unpinned(argv: &[String]) -> Option<String> {
let cmd = argv.first()?;
let base = cmd
@@ -512,30 +595,22 @@ fn unpinned(argv: &[String]) -> Option<String> {
.trim_end_matches(".cmd")
.trim_end_matches(".exe");
let rest = &argv[1..];
- let positional = |skip: &[&str]| {
- rest.iter()
- .find(|a| !a.is_empty() && !a.starts_with('-') && !skip.contains(&a.as_str()))
- .cloned()
- };
match base {
"npx" | "bunx" | "pnpx" => {
- let pkg = positional(&[])?;
+ let pkg = first_operand(rest, &[], &[])?.clone();
(!npm_pinned(&pkg)).then_some(pkg)
}
"pnpm" | "yarn" | "bun"
if rest.first().map(String::as_str) == Some("dlx")
|| rest.first().map(String::as_str) == Some("x") =>
{
- let pkg = rest[1..]
- .iter()
- .find(|a| !a.is_empty() && !a.starts_with('-'))
- .cloned()?;
+ let pkg = first_operand(&rest[1..], &[], &[])?.clone();
(!npm_pinned(&pkg)).then_some(pkg)
}
"uvx" | "pipx" => {
let pkg = match rest.iter().position(|a| a == "--from") {
Some(i) => rest.get(i + 1).cloned()?,
- None => positional(&["run"])?,
+ None => first_operand(rest, PYTHON_RUNNER_VALUE_FLAGS, &["run"])?.clone(),
};
(!(pkg.contains("==")
|| pkg.contains('@')
@@ -545,28 +620,7 @@ fn unpinned(argv: &[String]) -> Option<String> {
}
"docker" | "podman" => {
let i = rest.iter().position(|a| a == "run")?;
- let mut it = rest[i + 1..].iter();
- let image = loop {
- let a = it.next()?;
- if a.starts_with('-') {
- if matches!(
- a.as_str(),
- "-e" | "-v"
- | "-p"
- | "--env"
- | "--volume"
- | "--name"
- | "--network"
- | "-w"
- | "--mount"
- | "--env-file"
- ) {
- it.next();
- }
- continue;
- }
- break a.clone();
- };
+ let image = first_operand(&rest[i + 1..], CONTAINER_RUN_VALUE_FLAGS, &[])?.clone();
let last = image.rsplit('/').next().unwrap_or(&image);
let pinned =
image.contains("@sha256:") || (last.contains(':') && !last.ends_with(":latest"));
@@ -730,6 +784,216 @@ fn strip_jsonc(src: &str) -> String {
mod tests {
use super::*;
+ /// Runs every case and lists all that differ, as `argv: got X, want Y`.
+ fn check_unpinned(cases: &[(&[&str], Option<&str>)]) {
+ let wrong: Vec<String> = cases
+ .iter()
+ .filter_map(|(argv, want)| {
+ let argv_owned: Vec<String> = argv.iter().map(|a| a.to_string()).collect();
+ let got = unpinned(&argv_owned);
+ (got.as_deref() != *want).then(|| format!("{argv:?}: got {got:?}, want {want:?}"))
+ })
+ .collect();
+ assert!(wrong.is_empty(), "{wrong:#?}");
+ }
+
+ #[test]
+ fn unpinned_reads_npm_runners() {
+ let cases: &[(&[&str], Option<&str>)] = &[
+ (
+ &["npx", "-y", "@modelcontextprotocol/server-github"],
+ Some("@modelcontextprotocol/server-github"),
+ ),
+ (&["npx", "-y", "@scope/[email protected]"], None),
+ (&["npx", "pkg"], Some("pkg")),
+ (&["npx", "[email protected]", "--port", "3000"], None),
+ (&["npx", "pkg@latest"], Some("pkg@latest")),
+ (&["npx", "pkg@^1.2.0"], Some("pkg@^1.2.0")),
+ (&["npx", "pkg@~1.2.0"], Some("pkg@~1.2.0")),
+ (&["npx", "pkg@next"], Some("pkg@next")),
+ (&["npx", "@scope/pkg@latest"], Some("@scope/pkg@latest")),
+ (&["npx", "--yes", "--", "[email protected]"], None),
+ (&["npx", "./tools/server.js"], None),
+ (&["npx", "/opt/tools/server"], None),
+ (&["npx"], None),
+ (&["npx", ""], None),
+ (&["npx", "é@1.0.0"], None),
+ (&["npx", "é"], Some("é")),
+ (&["bunx", "pkg"], Some("pkg")),
+ (&["pnpx", "[email protected]"], None),
+ (&["/usr/local/bin/npx", "pkg"], Some("pkg")),
+ (&[r"C:\Program Files\nodejs\npx.cmd", "pkg"], Some("pkg")),
+ (&["npx.exe", "[email protected]"], None),
+ (&["pnpm", "dlx", "pkg"], Some("pkg")),
+ (&["pnpm", "dlx", "-s", "@scope/[email protected]"], None),
+ (&["pnpm", "dlx", ""], None),
+ (&["pnpm", "dlx"], None),
+ (&["yarn", "dlx", "pkg"], Some("pkg")),
+ (&["bun", "x", "pkg"], Some("pkg")),
+ (&["bun", "x", "[email protected]"], None),
+ // Not a one-off runner.
+ (&["pnpm", "install", "pkg"], None),
+ (&["bun", "run", "server.ts"], None),
+ (&["node", "server.js"], None),
+ (&[], None),
+ (&[""], None),
+ ];
+ check_unpinned(cases);
+ }
+
+ #[test]
+ fn unpinned_reads_python_runners() {
+ let cases: &[(&[&str], Option<&str>)] = &[
+ (&["uvx", "mcp-server-fetch"], Some("mcp-server-fetch")),
+ (&["uvx", "mcp-server-fetch==2025.4.7"], None),
+ (&["uvx", "[email protected]"], None),
+ (&["uvx", "pkg>=1.0"], Some("pkg>=1.0")),
+ (&["uvx", "pkg[extra]==1.0"], None),
+ (&["uvx", "--from", "pkg==1.0", "cmd"], None),
+ (&["uvx", "--from", "pkg", "cmd"], Some("pkg")),
+ (&["uvx", "--from", "./local", "cmd"], None),
+ (&["uvx", "--from", "/opt/tools/pkg", "cmd"], None),
+ (&["uvx", "--from"], None),
+ (&["uvx", "--python", "3.12", "pkg==1.0"], None),
+ (&["uvx", "-p", "3.12", "pkg"], Some("pkg")),
+ (&["uvx", "--python=3.12", "pkg"], Some("pkg")),
+ // `--with` is not read yet: its value is taken for the package, as on main, so an
+ // unpinned extra package is still reported (and a pinned one hides the main package).
+ (&["uvx", "--with", "httpx", "pkg==1.0"], Some("httpx")),
+ (
+ &[
+ "uvx",
+ "--index-url",
+ "https://pypi.example/simple",
+ "pkg==1.0",
+ ],
+ None,
+ ),
+ (&["uvx", ""], None),
+ (&["uvx"], None),
+ (&["uvx.exe", "pkg"], Some("pkg")),
+ (&["pipx", "run", "pkg"], Some("pkg")),
+ (&["pipx", "run", "pkg==1.0"], None),
+ (&["pipx", "run", "--python", "3.12", "pkg==1.0"], None),
+ (&["pipx", "run", "--spec", "pkg==1.0", "cmd"], None),
+ ];
+ check_unpinned(cases);
+ }
+
+ #[test]
+ fn unpinned_reads_container_runners() {
+ let cases: &[(&[&str], Option<&str>)] = &[
+ (
+ &["docker", "run", "-i", "--rm", "acme/mcp"],
+ Some("acme/mcp"),
+ ),
+ (&["docker", "run", "acme/mcp:1.0"], None),
+ (&["docker", "run", "--net", "host", "acme/mcp:1.0"], None),
+ (
+ &["docker", "run", "acme/mcp:latest"],
+ Some("acme/mcp:latest"),
+ ),
+ (&["docker", "run", "acme/mcp@sha256:0123abcd"], None),
+ (&["docker", "run", "ghcr.io/acme/mcp:1.0"], None),
+ (
+ &["docker", "run", "registry.example:5000/mcp"],
+ Some("registry.example:5000/mcp"),
+ ),
+ (&["docker", "run", "registry.example:5000/mcp:1.0"], None),
+ (
+ &[
+ "docker",
+ "run",
+ "-e",
+ "TOKEN",
+ "-v",
+ "/data:/data",
+ "acme/mcp",
+ ],
+ Some("acme/mcp"),
+ ),
+ (&["docker", "run", "--env=TOKEN=x", "acme/mcp:1.0"], None),
+ (
+ &[
+ "docker",
+ "run",
+ "--name",
+ "mcp",
+ "--network",
+ "host",
+ "acme/mcp:1.0",
+ ],
+ None,
+ ),
+ (&["docker", "run", "--user", "1000", "acme/mcp:1.0"], None),
+ (
+ &["docker", "run", "-u", "1000:1000", "acme/mcp"],
+ Some("acme/mcp"),
+ ),
+ (
+ &["docker", "run", "--platform", "linux/amd64", "acme/mcp:1.0"],
+ None,
+ ),
+ (
+ &["docker", "run", "--entrypoint", "/bin/sh", "acme/mcp:1.0"],
+ None,
+ ),
+ (
+ &[
+ "docker",
+ "run",
+ "-i",
+ "acme/mcp:1.0",
+ "serve",
+ "--port",
+ "80",
+ ],
+ None,
+ ),
+ (&["docker", "run", "", "acme/mcp:1.0"], None),
+ (&["docker", "run"], None),
+ (&["docker", "run", "-i"], None),
+ (&["docker", "pull", "acme/mcp"], None),
+ (&["docker"], None),
+ (&["podman", "run", "--rm", "acme/mcp"], Some("acme/mcp")),
+ (&["podman", "run", "--user", "1000", "acme/mcp:1.0"], None),
+ (&["docker.exe", "run", "acme/mcp"], Some("acme/mcp")),
+ ];
+ check_unpinned(cases);
+ }
+
+ #[test]
+ fn npm_pinned_needs_a_version_after_the_name() {
+ for pkg in [
+ "@scope/[email protected]",
+ "./local",
+ "/abs/path",
+ "é@1.0.0",
+ ] {
+ assert!(npm_pinned(pkg), "{pkg}");
+ }
+ for pkg in [
+ "",
+ "@",
+ "p",
+ "é",
+ "pkg",
+ "pkg@",
+ "pkg@latest",
+ "pkg@next",
+ "pkg@^1.2.3",
+ "pkg@~1.2.3",
+ "pkg@>=1",
+ "@scope/pkg",
+ "@scope/pkg@",
+ "@scope/pkg@latest",
+ ] {
+ assert!(!npm_pinned(pkg), "{pkg}");
+ }
+ }
+
#[test]
fn unrestricted_shell_only_flags_permissions_allow() {
let file = FileInfo::new(".claude/settings.json");