alexgreensh/repo-forensics / skills/repo-forensics
repo-forensics
FlaggedSecurity forensics for git repos, AI skills, and MCP servers. Audits dependencies, detects prompt injection, credential theft, runtime dynamism, manifest drift, known CVEs, CISA KEV (actively exploited) vulns, and 2026 attack patterns. Not for fixing vulnerabilities or pentesting.
scripts/scan_dependencies.py
View on GitHub#!/usr/bin/env python3
"""
scan_dependencies.py - Dependency Scanner (v3: NPM + Python + Go + transitive, 500+ packages)
Detects typosquatting, untrusted registries, version anomalies, and transitive supply chain attacks.
Created by Alex Greenshpun
"""
import sys
import os
import time
import json
import difflib
import math
import re
import unicodedata
from datetime import datetime, timezone
from urllib.parse import urlparse
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import forensics_core as core
import pypi_pins
SCANNER_NAME = "dependencies"
_LLMO_SUSPICIOUS_RE = re.compile(
r'(?i)(?:^|[-_])(?:ai|llm|gpt|chatgpt|openai|claude|langchain|vector|embedding|'
r'neural|transformer|prompt|blockchain|web3|nft|defi|crypto|dao|'
r'smart.?contract|token.?bridge)(?:[-_]|$)'
)
# Top 200 NPM packages
POPULAR_NPM = [
"react", "react-dom", "lodash", "underscore", "express", "moment", "axios", "chalk", "commander",
"debug", "fs-extra", "bluebird", "async", "request", "prop-types", "classnames", "uuid", "mkdirp",
"body-parser", "glob", "minimist", "colors", "inquirer", "yeoman-generator", "through2", "cheerio",
"shelljs", "rimraf", "yargs", "cookie-parser", "jsonwebtoken", "mongoose", "sequelize",
"vue", "angular", "rxjs", "tslib", "zone-js", "core-js", "webpack", "babel-core", "typescript",
"tailwindcss", "postcss", "autoprefixer", "eslint", "prettier", "vite", "next", "nuxt",
"dotenv", "cors", "morgan", "helmet", "passport", "bcrypt", "nodemon", "concurrently",
"cross-env", "webpack-cli", "babel-loader", "css-loader", "style-loader", "file-loader",
"url-loader", "html-webpack-plugin", "mini-css-extract-plugin", "terser-webpack-plugin",
"jest", "mocha", "chai", "sinon", "supertest", "nyc", "istanbul", "karma",
"socket-io", "redis", "pg", "mysql2", "mongodb", "knex", "prisma", "typeorm",
"graphql", "apollo-server", "apollo-client", "relay-runtime",
"react-router", "react-router-dom", "react-redux", "redux", "redux-thunk", "redux-saga",
"mobx", "zustand", "recoil", "immer", "ramda", "date-fns", "dayjs", "luxon",
"formik", "yup", "zod", "ajv", "joi", "validator",
"winston", "bunyan", "pino", "loglevel", "log4js",
"aws-sdk", "firebase", "stripe", "twilio", "sendgrid",
"sharp", "jimp", "canvas", "puppeteer", "playwright", "cypress",
"electron", "tauri", "capacitor", "ionic", "react-native", "expo",
"d3", "chart-js", "three", "p5", "fabric", "konva", "pixi-js",
"material-ui", "ant-design", "chakra-ui", "headlessui", "radix-ui",
"storybook", "docusaurus", "gatsby", "astro", "remix", "svelte", "sveltekit",
"fastify", "koa", "hapi", "nest", "adonis",
"esbuild", "rollup", "parcel", "swc", "turbopack",
"lerna", "nx", "turborepo", "changesets", "semantic-release",
"husky", "lint-staged", "commitlint", "conventional-changelog",
"nodemailer", "bull", "agenda", "cron", "node-cron",
"multer", "busboy", "formidable", "express-fileupload",
"compression", "serve-static", "http-proxy-middleware",
"i18next", "intl", "globalize", "polyglot",
"nanoid", "cuid", "shortid", "ulid",
"cheerio", "jsdom", "node-fetch", "got", "superagent", "ky",
"ora", "listr", "progress", "cli-progress", "boxen", "figlet",
"execa", "zx", "shelljs", "cross-spawn",
"semver", "compare-versions", "node-semver",
"yaml", "toml", "ini", "properties-parser",
"gray-matter", "front-matter", "markdown-it", "marked", "remark", "rehype",
]
# Top 200 PyPI packages
POPULAR_PYPI = [
"requests", "numpy", "pandas", "flask", "django", "fastapi", "sqlalchemy", "celery",
"boto3", "botocore", "pillow", "scipy", "matplotlib", "scikit-learn", "tensorflow",
"pytorch", "torch", "transformers", "beautifulsoup4", "lxml", "scrapy",
"pytest", "unittest2", "nose", "tox", "coverage", "hypothesis",
"click", "typer", "argparse", "fire", "docopt",
"pydantic", "attrs", "dataclasses", "marshmallow",
"aiohttp", "httpx", "urllib3", "certifi", "chardet",
"cryptography", "pycryptodome", "paramiko", "fabric",
"redis", "pymongo", "psycopg2", "mysql-connector-python",
"gunicorn", "uvicorn", "waitress", "twisted", "tornado",
"jinja2", "mako", "chameleon",
"setuptools", "pip", "wheel", "twine", "build",
"black", "flake8", "mypy", "pylint", "isort", "autopep8", "yapf",
"python-dotenv", "decouple", "environs",
"pyyaml", "toml", "tomli", "configparser",
"jsonschema", "simplejson", "orjson", "ujson",
"arrow", "pendulum", "python-dateutil", "pytz",
"rich", "colorama", "termcolor", "tqdm", "alive-progress",
"loguru", "structlog", "python-json-logger",
"jwt", "pyjwt", "oauthlib", "authlib",
"stripe", "twilio", "sendgrid",
"opencv-python", "imageio", "scikit-image",
"networkx", "igraph", "graph-tool",
"sympy", "mpmath", "statsmodels",
"selenium", "playwright", "pyppeteer",
"docker", "kubernetes", "ansible",
"grpcio", "protobuf", "thrift",
"graphene", "strawberry-graphql", "ariadne",
"alembic", "migrate", "peewee", "tortoise-orm",
"sentry-sdk", "newrelic", "datadog",
"openai", "anthropic", "langchain", "llama-index",
"streamlit", "gradio", "dash", "panel",
"sphinx", "mkdocs", "pdoc",
"regex", "more-itertools", "toolz", "funcy",
"tenacity", "retrying", "backoff",
"apscheduler", "schedule", "rq",
"watchdog", "inotify", "pyinotify",
"psutil", "py-cpuinfo", "gputil",
"pexpect", "ptyprocess", "subprocess32",
"pygments", "asttokens", "astunparse",
]
# Top 100 Go modules (short names for import path matching)
POPULAR_GO = [
"gin", "echo", "fiber", "chi", "mux", "gorilla", "httprouter",
"gorm", "sqlx", "pgx", "go-redis", "mongo-driver",
"cobra", "viper", "pflag", "urfave-cli",
"zap", "logrus", "zerolog",
"testify", "gomock", "gocheck",
"grpc", "protobuf", "twirp",
"jwt", "oauth2", "casbin",
"aws-sdk-go", "azure-sdk-for-go", "google-cloud-go",
"docker", "kubernetes", "helm", "terraform",
"prometheus", "opentelemetry", "jaeger",
"uuid", "ulid", "xid",
"validator", "ozzo-validation",
"go-kit", "micro",
"fx", "wire", "dig",
"colly", "goquery", "rod",
"ginkgo", "gomega", "goconvey",
"afero", "embed",
"graphql", "gqlgen", "graphql-go",
]
TRUSTED_REGISTRIES = [
"registry.npmjs.org", "registry.yarnpkg.com", "codeload.github.com",
"github.com", "cdn.jsdelivr.net", "pnpm.io",
"pypi.org", "files.pythonhosted.org",
"proxy.golang.org", "sum.golang.org",
"deno.land", "jsr.io",
]
# Funding/sponsorship domains that appear in lockfile metadata (not registries)
KNOWN_BENIGN_DOMAINS = [
"opencollective.com", "tidelift.com", "paulmillr.com",
"feross.org", "patreon.com", "buymeacoffee.com",
"eslint.org", "ko-fi.com", "paypal.me",
]
# IOC packages - lazy loaded from ioc_manager (single source of truth)
_SANDWORM_KNOWN_IOC_PACKAGES = None
_FALLBACK_IOC_PACKAGES = {
"rimarf", "yarsg", "suport-color", "naniod", "opencraw",
"claud-code", "cloude-code", "cloude", "mcp-cliient", "mcp-serever",
"anthropic-sdk-node", "claude-code-cli", "clawclient",
"anthopic", "antrhopic", "claudes", "mcp-python-sdk",
# Axios supply chain RAT dropper (March 31, 2026)
"plain-crypto-js",
# Companion malware packages (March 2026)
"@shadanai/openclaw", "@qqbrowser/openclaw-qbot",
}
def _get_ioc_packages():
"""Lazy-load IOC packages from ioc_manager."""
global _SANDWORM_KNOWN_IOC_PACKAGES
if _SANDWORM_KNOWN_IOC_PACKAGES is None:
try:
import ioc_manager as _ioc
_iocs = _ioc.get_iocs()
_SANDWORM_KNOWN_IOC_PACKAGES = {p.lower() for p in (_iocs.get('malicious_npm', set()) | _iocs.get('malicious_pypi', set()))}
except (ImportError, OSError, json.JSONDecodeError, ValueError) as e:
print(f"[!] IOC loading failed, using fallback: {e}", file=sys.stderr)
_SANDWORM_KNOWN_IOC_PACKAGES = _FALLBACK_IOC_PACKAGES
return _SANDWORM_KNOWN_IOC_PACKAGES
def _apply_l33t(name):
"""Normalize l33t substitutions for typosquatting comparison.
Maps: 0->o, 1->l, 3->e, @->a (common character swaps in malicious packages).
"""
return name.replace('0', 'o').replace('1', 'l').replace('3', 'e').replace('@', 'a')
# Match version strings in a tolerant-but-parseable way. Captures:
# 1. major (required)
# 2. minor (optional)
# 3. patch (optional)
# 4. suffix (optional pre-release/build metadata starting with - or +)
_VERSION_RE = re.compile(
r'^\s*[vV]?'
r'[~^>=<!]*\s*'
r'(\d+)(?:\.(\d+))?(?:\.(\d+))?'
r'([-+][0-9A-Za-z.\-+]*)?'
r'\s*$'
)
def normalize_version(version, strict_pin=False):
"""Canonicalize a version string for IOC comparison.
Returns a normalized string (e.g. '5.6.1') or None if the input cannot
be parsed as a version. The goal is to make bypass attempts like 'v5.6.1',
'5.06.1', ' 5.6.1 ', '^5.6.1', NFC-unnormalized unicode, and leading
zeros all map to the same key as '5.6.1' so they get caught by the IOC
set lookup.
Args:
version: string version to normalize.
strict_pin: when True (used by package.json callers), rejects ANY
range operator — `^`, `~`, `>=`, `<=` — because those are range
constraints that may or may not resolve to the IOC version.
`<=5.6.1` and `~5.6.1` both INCLUDE 5.6.1 in their range but do
NOT pin it, so matching the chalk@5.6.1 IOC on a package.json
that writes `"chalk": "<=5.6.1"` is a false positive. (Code
review CRC-F1, 2026-04-05.) Lockfile resolved versions never
contain operators and call with strict_pin=False (default).
Strips:
- Leading/trailing whitespace
- 'v' or 'V' prefix (npm tag aliases, GitHub release style)
- Range hint operators: ^ ~ (approximately equal) — when strict_pin=False
- Leading zeros in each numeric segment ('5.06.1' -> '5.6.1')
- Build metadata ('+sha.abc123'). Per SemVer 2.0.0 §10 build metadata
MUST be ignored when determining version precedence, so
`5.6.1+mirror.hash` must normalize to `5.6.1` and match the chalk
IOC. (Security review SS-F3, 2026-04-05.)
Preserves:
- Pre-release metadata ('-beta.0', '-rc.1', '-next.0')
Rejects (returns None):
- None, non-string types
- Empty string
- Non-version strings ('latest', 'main', git refs)
- Strings with embedded control characters
- Exclusion operators (<, >, !, !=). These definitively EXCLUDE the
target version — e.g. '<5.6.1' means 'anything less than 5.6.1'.
(Security review B1, 2026-04-05.)
- Inclusion range operators (>=, <=, ^, ~) WHEN strict_pin=True.
"""
if not isinstance(version, str) or not version:
return None
# Defensive: reject strings with control chars (prevents log-injection
# via version field and YAML/JSON parser quirks)
if any(ord(c) < 0x20 and c not in '\t' for c in version):
return None
# NFC normalize to collapse homoglyph-style unicode variants
version = unicodedata.normalize('NFC', version)
# Reject exclusion operators that definitively EXCLUDE the target version
# (security review B1, 2026-04-05):
# <5.6.1 means "anything below 5.6.1" — excludes 5.6.1
# >5.6.1 means "anything above 5.6.1" — excludes 5.6.1
# !5.6.1 means "not 5.6.1" — excludes 5.6.1
# !=5.6.1 means "not equal to 5.6.1" — excludes 5.6.1
# Inclusion operators like >=5.6.1, <=5.6.1, ^5.6.1, ~5.6.1 all include
# 5.6.1 and are legitimately normalized.
stripped = version.strip()
if stripped.startswith(('!=', '!')):
return None
# `<` or `>` followed by anything OTHER than `=` is an exclusion operator
if stripped.startswith('<') and not stripped.startswith('<='):
return None
if stripped.startswith('>') and not stripped.startswith('>='):
return None
# In strict-pin mode (package.json manifest callers), reject ANY range
# operator. These are NOT pins — they're constraints that the lockfile
# resolves to some specific version, which may or may not be the IOC.
# Flagging them as critical IOC hits is a false positive. (CRC-F1.)
if strict_pin:
if stripped.startswith(('<=', '>=', '^', '~')):
return None
m = _VERSION_RE.match(version)
if not m:
return None
major, minor, patch, suffix = m.groups()
parts = [str(int(major))]
if minor is not None:
parts.append(str(int(minor)))
if patch is not None:
parts.append(str(int(patch)))
normalized = '.'.join(parts)
# Preserve pre-release ('-beta.0') but drop build metadata ('+sha.abc')
# per SemVer §10. `-beta.0+build.1` keeps `-beta.0`, drops `+build.1`.
if suffix:
plus_idx = suffix.find('+')
if plus_idx >= 0:
suffix = suffix[:plus_idx]
if suffix:
normalized += suffix
return normalized
def check_typosquatting(dependencies, popular_list):
# Pre-compute lowercase mapping for O(1) lookup instead of O(n) per dep
popular_lower_map = {p.lower(): p for p in popular_list}
popular_lower_set = set(popular_lower_map.keys())
suspicious = []
for dep in dependencies:
dep_lower = dep.lower()
dep_l33t = _apply_l33t(dep_lower)
if dep_lower in popular_lower_set:
continue
# Check raw similarity (pre-filter by length to avoid O(n*m) SequenceMatcher on large lockfiles)
for pop_lower, popular in popular_lower_map.items():
if abs(len(dep_lower) - len(pop_lower)) > max(3, int(len(pop_lower) * 0.15)):
continue # Length difference too large for >0.85 similarity
ratio = difflib.SequenceMatcher(None, dep_lower, pop_lower).ratio()
if ratio > 0.85 and dep_lower != pop_lower:
suspicious.append((dep, popular, ratio))
break
# Check l33t-normalized similarity
if dep_l33t != dep_lower:
ratio_l33t = difflib.SequenceMatcher(None, dep_l33t, pop_lower).ratio()
if ratio_l33t > 0.85 and dep_l33t != pop_lower:
suspicious.append((dep, popular, ratio_l33t))
break
return suspicious
HALLUCINATION_NPM = frozenset({
"llm-cache-proxy", "ai-response-validator", "gpt4-api-wrapper",
"claude-sdk-node", "llm-stream-parser", "ai-prompt-builder",
"ai-chain-runner", "prompt-template-engine", "llm-output-parser",
"ai-context-window", "model-inference-client", "ai-safety-filter",
"blockchain-token-validator", "nft-metadata-parser",
"defi-swap-router", "crypto-price-oracle",
})
HALLUCINATION_PYPI = frozenset({
"gpt4-python", "ai-response-parser", "claude-python-sdk",
"prompt-engineering-toolkit", "neural-text-splitter",
"llm-chain-runner", "model-serving-client",
"ai-guardrails", "chatbot-context-manager",
"defi-protocol-sdk", "crypto-wallet-sdk",
"nft-generator-toolkit", "token-bridge-client",
"smart-contract-auditor",
})
def check_slopsquatting(dependencies, ecosystem):
"""Flag packages matching AI-hallucinated package names (Aikido Security, March 2026)."""
if ecosystem == "npm":
corpus = HALLUCINATION_NPM
elif ecosystem == "PyPI":
corpus = HALLUCINATION_PYPI
else:
return []
findings = []
for dep in dependencies:
if dep.lower() in corpus:
findings.append(dep)
return findings
def check_version_anomaly(version_str):
"""Detect dependency confusion signatures like v99.x.x"""
m = re.match(r'[~^>=<]*(\d+)', version_str)
if m:
major = int(m.group(1))
if major >= 90:
return True
return False
# Canonical scope -> identity markers that scope's official packages legitimately
# carry. A package OUTSIDE the scope carrying these markers is passing itself
# off as the org's official package. Motivating case: @atom8n/inspector, a
# security-regression squat of @modelcontextprotocol/inspector that declared
# author "Anthropic, PBC" and homepage modelcontextprotocol.io while shipping
# an inverted auth gate (Sept 2026 verified-malicious npm batch).
_CANONICAL_SCOPE_IDENTITY = {
'@modelcontextprotocol': ('anthropic, pbc', 'modelcontextprotocol.io'),
'@anthropic-ai': ('anthropic, pbc',),
'@openai': ('openai',),
}
def check_provenance_squat(data, rel_path):
"""Flag scoped packages whose author/homepage metadata claims a canonical
org identity the package's own scope does not belong to (high)."""
findings = []
name = data.get('name')
if not isinstance(name, str) or not name.startswith('@'):
return findings
author = data.get('author', '')
if isinstance(author, dict):
author = author.get('name', '')
if not isinstance(author, str):
author = ''
homepage = data.get('homepage', '')
if not isinstance(homepage, str):
homepage = ''
identity_text = (author + ' ' + homepage).lower()
if not identity_text.strip():
return findings
# A package inside ANY canonical scope is that org's own publish; only
# packages outside every canonical scope can be squatting the identity.
if any(name.lower().startswith(scope + '/') for scope in _CANONICAL_SCOPE_IDENTITY):
return findings
for scope, markers in _CANONICAL_SCOPE_IDENTITY.items():
if name.lower().startswith(scope + '/'):
continue
for marker in markers:
if marker in identity_text:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Provenance Squat: '{name}' claims {scope} identity",
description=(
f"Package metadata (author/homepage) references '{marker}', "
f"the identity of canonical scope {scope}, but this package "
"is published outside that scope. Security-regression squats "
"use official-looking provenance to pass as the upstream package."
),
file=rel_path, line=0,
snippet=f"name={name} author={author} homepage={homepage}",
category="provenance-squat"
))
break
return findings
def check_known_ioc_packages(dependencies, rel_path):
"""Flag packages matching SANDWORM_MODE campaign known-IOC list (critical)."""
findings = []
for dep in dependencies:
if dep.lower() in _get_ioc_packages():
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="critical",
title=f"Known Malicious Package: '{dep}'",
description="Package matches SANDWORM_MODE campaign IOC list (source: Socket Research, Snyk ToxicSkills 2026)",
file=rel_path, line=0, snippet=f"'{dep}' is a known malicious package",
category="known-ioc"
))
return findings
# Compromised versions of legitimate packages (supply chain hijack, not typosquatting).
#
# DESIGN NOTE (2026-04-05): This in-module dict is the LEGACY baseline that
# ships with the scanner for offline/air-gapped use. It is DISJOINT from the
# larger ioc_manager-loaded database (skills/repo-forensics/data/compromised_versions.json)
# which carries Marc Gadsdon's 127 IOCs with campaign attribution. The two
# sources are union-merged by _get_compromised_versions_db() below. When
# adding new IOCs, prefer the JSON file (richer metadata, reviewable in git
# history, easier to update). Only touch this dict if you need a detection
# that must work even when the JSON file is missing/corrupt.
COMPROMISED_PACKAGE_VERSIONS = {
# Axios supply chain compromise (March 31, 2026)
"axios": {"1.14.1", "0.30.4"},
# plain-crypto-js RAT dropper (March 31, 2026)
"plain-crypto-js": {"4.2.1"},
# Companion malware packages (March 2026)
"@shadanai/openclaw": {"2026.3.28-2", "2026.3.28-3", "2026.3.31-1", "2026.3.31-2"},
"@qqbrowser/openclaw-qbot": {"0.0.130"},
# liteLLM supply chain attack (March 24, 2026)
"litellm": {"1.82.7", "1.82.8", "1.107.0"},
}
# Names in the fallback dict above that are PyPI packages. The shipped JSON
# knows each campaign's ecosystem; this offline fallback does not, and the npm
# entries must not be matched against PyPI requirements.
_PYPI_FALLBACK_NAMES = ("litellm",)
# Suspicious npm scopes (systematic MCP server forking campaigns)
SUSPICIOUS_NPM_SCOPES = {
"@iflow-mcp", # Systematic MCP server forking campaign (March 2026)
}
# --- Vuln enrichment (OSV + CISA KEV) state -------------------------------
#
# Populated on first use by main() (or lazily on first _check_vulns call).
# Scanner behavior: for every (ecosystem, package, version) seen in any
# manifest, query OSV for known vulnerabilities. Cross-reference CVE aliases
# against the CISA KEV catalog and escalate severity to CRITICAL when a vuln
# is actively exploited in the wild. Full offline-safe: network failures
# degrade silently to the existing IOC/compromised-version checks.
_VULN_STATE = {
"enabled": True, # --no-vulns disables
"offline": False, # --offline avoids any network call
"kev": None, # lazy-loaded set[str] of KEV CVE IDs
"queried": set(), # dedup tuples (ecosystem, name_lower, normalized_version)
}
# --- Freshness detection state -----------------------------------------------
#
# Parallel to _VULN_STATE. For every (ecosystem, package, version) seen in a
# manifest, query the registry for publication metadata (publish date,
# version count, maintainer identity). Flag very-new versions, brand-new
# packages, and maintainer changes -- all key supply-chain attack indicators.
_FRESHNESS_STATE = {
"enabled": True, # --skip-freshness disables
"offline": False, # --offline disables
"queried": set(), # dedup (ecosystem, name_lower, version)
"query_count": 0, # hard cap tracking
"max_queries": 100, # hard cap
# Wall-clock budget for all registry lookups. run_forensics.sh kills a
# scanner at 120s, and a killed scanner turns the whole report into a
# scanner failure (exit 99) with no dependency findings at all. Stopping
# the lookups early keeps every offline check and reports the gap instead.
"time_budget_s": 45.0,
"started_at": None, # set by main(); direct callers run without a budget
"time_skipped": 0,
}
def _enrichment_status_findings():
"""Emit scanner-level enrichment-degraded findings.
These are routed as findings in the `enrichment-degraded` category so the
aggregator can build `enrichment_status` without `aggregate_json.py` ever
importing `rule_loader` (leaf-law).
"""
findings = []
if _VULN_STATE["offline"]:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="Vulnerability enrichment offline (`--offline`)",
description=("Network fetches are disabled; OSV + CISA KEV "
"vulnerability enrichment was not performed."),
file="", line=0, snippet="",
category="enrichment-degraded",
confidence=0.0,
))
elif not _VULN_STATE["enabled"]:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="Vulnerability enrichment disabled (`--no-vulns`)",
description=("Vulnerability enrichment was explicitly disabled; "
"OSV + CISA KEV checks were not performed."),
file="", line=0, snippet="",
category="enrichment-degraded",
confidence=0.0,
))
try:
import rule_loader
if rule_loader.get_rulepack_degraded():
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="Rulepack feed degraded",
description=("A cached rulepack bundle is present but failed "
"verification or overlay. Pack updates may be stale."),
file="", line=0, snippet="",
category="enrichment-degraded",
confidence=0.0,
))
except (ImportError, OSError):
pass
return findings
def _vuln_cache_freshness_status():
"""Return the worst freshness status across the KEV and OSV caches.
Mirrors `cache_freshness` semantics: missing/unparseable cache is
RECHECK_REQUIRED; stale-but-present is STALE; otherwise empty string.
"""
try:
import vuln_feed
except ImportError:
return ""
try:
kev = vuln_feed.cache_freshness(
vuln_feed._cache_path(vuln_feed.KEV_CACHE_FILENAME),
vuln_feed.KEV_CACHE_MAX_AGE_HOURS,
)
osv = vuln_feed.cache_freshness(
vuln_feed._cache_path(vuln_feed.OSV_CACHE_FILENAME),
vuln_feed.OSV_CACHE_MAX_AGE_HOURS,
)
except (OSError, ValueError, TypeError):
return ""
statuses = [kev.get("status", ""), osv.get("status", "")]
if "RECHECK_REQUIRED" in statuses:
return "RECHECK_REQUIRED"
if "STALE" in statuses:
return "STALE"
return ""
def _check_vulns(ecosystem, pkg_versions, rel_path):
"""Enrich findings with OSV + CISA KEV data for each (pkg, version) seen.
Design:
- Pure: no mutation of caller state beyond returning findings.
- Offline-safe: missing/failed feeds -> [] (scanner keeps running).
- Deduped: each (eco, name, version) triple queries OSV at most once
per scan run. OSV itself caches 24h on disk across runs.
- Severity escalation: any vuln whose CVE alias is in CISA KEV is
marked 'critical' regardless of CVSS score.
"""
if not _VULN_STATE["enabled"]:
return []
try:
import vuln_feed
except ImportError:
return []
if _VULN_STATE["kev"] is None:
_VULN_STATE["kev"] = vuln_feed.get_kev_cves(offline=_VULN_STATE["offline"])
findings = []
for pkg, version in pkg_versions.items():
if not isinstance(pkg, str) or not isinstance(version, str):
continue
normalized = normalize_version(version)
if normalized is None:
continue
canonical = vuln_feed.canonicalize_pkg_name(ecosystem, pkg)
key = (ecosystem, canonical, normalized)
if key in _VULN_STATE["queried"]:
continue
_VULN_STATE["queried"].add(key)
try:
vulns = vuln_feed.check_package_vulnerabilities(
ecosystem, canonical, normalized,
kev_set=_VULN_STATE["kev"],
offline=_VULN_STATE["offline"],
)
except (OSError, ValueError, RuntimeError) as e:
# vuln_feed is designed to swallow errors internally, but guard
# the boundary anyway so a single bad package can't kill the scan.
print(f"[!] Vuln enrichment error for {canonical}@{normalized}: {e}",
file=sys.stderr)
continue
# Sanitize pkg identity before embedding in Finding output
# (defense against terminal escape / BIDI injection via manifest files)
try:
safe_pkg = vuln_feed._sanitize_display_text(canonical, max_len=214)
except (AttributeError, TypeError):
safe_pkg = canonical[:214]
for v in vulns:
aliases = v.get("aliases") or []
cve_label = ", ".join(aliases) if aliases else v.get("id", "unknown")
kev_flag = " [CISA KEV - actively exploited]" if v.get("in_kev") else ""
fixed_in = v.get("fixed_in") or []
fixed_label = ", ".join(fixed_in) if fixed_in else "unknown"
summary = v.get("summary") or "(no summary)"
findings.append(core.Finding(
scanner=SCANNER_NAME,
severity=v.get("suggested_severity", "medium"),
title=f"Known Vulnerability: {safe_pkg}@{normalized} — {cve_label}{kev_flag}",
description=(
f"{summary} | Source: OSV (id={v.get('id')}) "
f"| Fixed in: {fixed_label}"
),
file=rel_path, line=0,
snippet=f"{safe_pkg}@{normalized} matches {v.get('id')}",
category="cve-kev" if v.get("in_kev") else "cve",
))
return findings
def _check_freshness(ecosystem, pkg_versions, rel_path):
"""Enrich findings with publication-age and maintainer-change signals.
Design mirrors _check_vulns:
- Pure: no mutation of caller state beyond returning findings.
- Offline-safe: disabled/offline -> [].
- Deduped: each (eco, name, version) triple checked at most once.
- Popular packages skipped (POPULAR_NPM / POPULAR_PYPI).
- Hard query cap prevents runaway network usage on huge monorepos.
"""
if not _FRESHNESS_STATE["enabled"]:
return []
try:
import vuln_feed
except ImportError:
return []
# Dispatch to the right registry fetcher
_FETCH = {
"npm": vuln_feed.fetch_npm_freshness,
"PyPI": vuln_feed.fetch_pypi_freshness,
}
fetcher = _FETCH.get(ecosystem)
if fetcher is None:
return []
# Pick the popular-package skip list for this ecosystem
popular = POPULAR_NPM if ecosystem == "npm" else POPULAR_PYPI
popular_lower = {p.lower() for p in popular}
findings = []
skipped_count = 0
time_skipped = 0
for pkg, version in pkg_versions.items():
if not isinstance(pkg, str) or not isinstance(version, str):
continue
normalized = normalize_version(version)
if normalized is None:
continue
canonical = vuln_feed.canonicalize_pkg_name(ecosystem, pkg)
key = (ecosystem, canonical, normalized)
if key in _FRESHNESS_STATE["queried"]:
continue
_FRESHNESS_STATE["queried"].add(key)
if canonical.lower() in popular_lower:
continue
# Enforce hard query cap
if _FRESHNESS_STATE["query_count"] >= _FRESHNESS_STATE["max_queries"]:
skipped_count += 1
continue
started = _FRESHNESS_STATE["started_at"]
if started is not None and time.monotonic() - started > _FRESHNESS_STATE["time_budget_s"]:
_FRESHNESS_STATE["time_skipped"] += 1
time_skipped += 1
continue
_FRESHNESS_STATE["query_count"] += 1
try:
result = fetcher(canonical, normalized,
offline=_FRESHNESS_STATE["offline"])
except (OSError, ValueError, RuntimeError) as e:
print(f"[!] Freshness check error for {canonical}@{normalized}: {e}",
file=sys.stderr)
continue
if result is None:
continue
published_str = result.get("published")
if not published_str:
continue
try:
publish_dt = datetime.fromisoformat(
published_str.replace("Z", "+00:00")
)
except (ValueError, TypeError):
continue
age = datetime.now(timezone.utc) - publish_dt
version_count = result.get("version_count", 0)
# Signal 1: Very new (< 24 hours)
if age.total_seconds() < 86400:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Very new package version: {pkg}@{version}",
description=(
f"Published {age.total_seconds()/3600:.0f}h ago. "
f"Malicious packages are often published and consumed within hours."
),
file=rel_path, line=0,
snippet=f"{pkg}@{version} published {result['published']}",
category="freshness-very-new",
))
# Signal 2: Recent (< 7 days)
elif age.days < 7:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"Recently published package version: {pkg}@{version}",
description=(
f"Published {age.days}d ago. "
f"Consider waiting before adopting new versions."
),
file=rel_path, line=0,
snippet=f"{pkg}@{version} published {result['published']}",
category="freshness-recent",
))
# Signal 3: Brand new single-version package (< 30 days)
if version_count == 1 and age.days < 30:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Brand new package with single version: {pkg}@{version}",
description=(
f"First-ever version published {age.days}d ago. "
f"Brand new packages with no history are higher risk."
),
file=rel_path, line=0,
snippet=f"{pkg}@{version} (1 version, published {result['published']})",
category="freshness-brand-new-package",
))
# Signal 3b: LLMO suspicious naming (brand-new + AI/crypto name)
if _LLMO_SUSPICIOUS_RE.search(canonical):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"LLMO Suspicious Name: {pkg}@{version}",
description=(
"Brand-new single-version package with AI/crypto buzzword name. "
"LLMs frequently hallucinate package names matching this pattern, "
"which attackers register (PromptMink/ReversingLabs, April 2026)."
),
file=rel_path, line=0,
snippet=f"{pkg}@{version} (AI/crypto name + brand new)",
category="llmo-suspicious",
))
# Signal 4: Maintainer changed
prev = result.get("prev_maintainer")
curr = result.get("maintainer")
if prev and curr and curr != prev:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Maintainer changed: {pkg}@{version}",
description=(
f"Publishing author changed from '{prev}' to '{curr}'. "
f"Maintainer takeover is a common supply chain attack vector."
),
file=rel_path, line=0,
snippet=f"{pkg}@{version}: {prev} -> {curr}",
category="freshness-maintainer-takeover",
))
# Emit cap-hit finding if any packages were skipped
if skipped_count > 0:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title="Freshness check query limit reached",
description=(
f"{skipped_count} packages skipped due to "
f"{_FRESHNESS_STATE['max_queries']} query limit."
),
file=rel_path, line=0,
snippet=f"Query cap: {_FRESHNESS_STATE['max_queries']}",
category="freshness-query-cap",
))
if time_skipped > 0:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title="Freshness check time limit reached",
description=(
f"{time_skipped} packages were not checked for publish age or "
f"maintainer changes: registry lookups hit the "
f"{_FRESHNESS_STATE['time_budget_s']:.0f}s budget. Offline checks "
f"(known-bad versions, typosquats, lockfile tampering) still ran."
),
file=rel_path, line=0,
snippet=f"{time_skipped} packages not checked",
category="freshness-query-cap",
))
return findings
def _merge_freshness_cap_findings(findings):
"""One "limit reached" notice per scan, not one per manifest.
The query cap and time budget are scan-wide, so a repo with many lockfiles
used to repeat the same notice for each. Counts are summed and every
affected manifest is still named.
"""
caps = [f for f in findings if f.category == "freshness-query-cap"]
if len(caps) < 2:
return findings
merged = {}
for f in caps:
m = re.match(r"(\d+)", f.description or "") or re.match(r"(\d+)", f.snippet or "")
entry = merged.setdefault(f.title, {"first": f, "count": 0, "files": []})
entry["count"] += int(m.group(1)) if m else 0
if f.file not in entry["files"]:
entry["files"].append(f.file)
out = [f for f in findings if f.category != "freshness-query-cap"]
for title, entry in merged.items():
first = entry["first"]
files = entry["files"]
out.append(core.Finding(
scanner=first.scanner, severity=first.severity, title=title,
description=(f"{entry['count']} packages were not checked for publish age "
f"or maintainer changes across {len(files)} manifest(s): "
+ ", ".join(files[:10])
+ (f" and {len(files) - 10} more" if len(files) > 10 else "")
+ ". Offline checks still ran."),
file=files[0], line=0,
snippet=f"{entry['count']} packages not checked",
category="freshness-query-cap",
))
return out
def _get_compromised_versions_db():
"""Return the live compromised-versions database.
Merges the in-module COMPROMISED_PACKAGE_VERSIONS fallback (kept for
offline/air-gapped use) with the richer ioc_manager dataset loaded from
skills/repo-forensics/data/compromised_versions.json. When ioc_manager provides campaign
attribution, the fallback dict's bare version strings are replaced by
{version: campaign_id} maps for better reporting; when it does not, we
synthesize 'local-fallback' as the campaign id so the downstream code
path stays uniform.
Returns: dict mapping package_name_lower -> {normalized_version: campaign_id}
"""
# Seed from in-module fallback (always available)
db = {}
for pkg, versions in COMPROMISED_PACKAGE_VERSIONS.items():
pkg_lower = pkg.lower()
db[pkg_lower] = {}
for v in versions:
normalized = normalize_version(v) or v
db[pkg_lower][normalized] = "local-fallback"
# Merge ioc_manager data (JSON file ships with the tool + optional OSV)
try:
import ioc_manager as _ioc
ioc_data = _ioc.get_iocs()
remote_cv = ioc_data.get('compromised_versions', {})
for pkg_lower, version_map in remote_cv.items():
if pkg_lower not in db:
db[pkg_lower] = {}
for version, campaign_id in version_map.items():
# Normalize the DB-side key so lookups match regardless of
# how the IOC was typed into the JSON file.
normalized = normalize_version(version) or version
db[pkg_lower][normalized] = campaign_id
except (ImportError, OSError, json.JSONDecodeError, ValueError, KeyError) as e:
print(f"[!] Compromised-version IOC load failed, using fallback: {e}",
file=sys.stderr)
return db
# Lazy-loaded cache of the merged DB. Populated on first call and cleared
# by tests that need a fresh load.
_COMPROMISED_VERSIONS_DB = None
def _compromised_versions_db():
global _COMPROMISED_VERSIONS_DB
if _COMPROMISED_VERSIONS_DB is None:
_COMPROMISED_VERSIONS_DB = _get_compromised_versions_db()
return _COMPROMISED_VERSIONS_DB
def check_compromised_versions(all_deps_with_versions, rel_path, db=None, strict_pin=False):
"""Flag specific compromised versions of legitimate packages.
Matches against the merged IOC database (in-module fallback + shipped
JSON via ioc_manager + optional OSV feed). Uses normalize_version() on
the dependency-side version string to defeat prefix/leading-zero/v-prefix
bypass attempts.
Args:
all_deps_with_versions: dict of {package_name: version_string}
rel_path: file path for the finding (for attribution)
db: optional pre-loaded database (for testing); defaults to the
lazy-loaded module cache.
strict_pin: pass True when the caller is scanning a manifest (like
package.json) whose version strings are RANGE CONSTRAINTS, not
resolved versions. With strict_pin=True, normalize_version rejects
operators like `<=`, `>=`, `^`, `~` to avoid false-positive IOC
matches on ranges that merely include the compromised version.
Lockfile callers pass strict_pin=False (default). Code review
CRC-F1, 2026-04-05.
"""
if db is None:
db = _compromised_versions_db()
findings = []
for pkg, version in all_deps_with_versions.items():
pkg_lower = pkg.lower()
if pkg_lower not in db:
continue
normalized = normalize_version(version, strict_pin=strict_pin)
if normalized is None:
# Unparseable version — skip silently rather than flag or crash
continue
if normalized in db[pkg_lower]:
campaign_id = db[pkg_lower][normalized]
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="critical",
title=f"Compromised Package Version: {pkg}@{normalized}",
description=(
f"Version {normalized} of '{pkg}' is known compromised "
f"(supply chain attack IOC, campaign: {campaign_id})"
),
file=rel_path, line=0,
snippet=f"{pkg}@{normalized} (known compromised, {campaign_id})",
category="supply-chain"
))
return findings
def _pypi_compromised_db():
"""{PEP 503 name: {version: campaign}} for PyPI only: shipped JSON campaigns
marked ecosystem "pypi", plus the offline fallback for PyPI names."""
db = {}
for name in _PYPI_FALLBACK_NAMES:
for v in COMPROMISED_PACKAGE_VERSIONS.get(name, ()):
db.setdefault(pypi_pins.normalize_name(name), {})[v] = "local-fallback"
try:
import ioc_manager as _ioc
for name, vmap in _ioc.get_iocs().get('compromised_versions_pypi', {}).items():
db.setdefault(name, {}).update(vmap)
except (ImportError, OSError, json.JSONDecodeError, ValueError, KeyError) as e:
print(f"[!] PyPI compromised-version IOC load failed, using fallback: {e}",
file=sys.stderr)
return db
def _python_manifest_requirements(basename, content):
"""(name, spec, line_no) for every requirement in a Python manifest that
could pin a version: requirements-style lines, PEP 508 strings in
pyproject.toml, and `name = "==x"` entries in Pipfile / poetry tables."""
reqs = []
lines = content.split('\n')
if basename in ('requirements.txt', 'requirements-dev.txt', 'requirements-test.txt',
'constraints.txt'):
# pip joins a line ending in a backslash with the next one, so a pin
# split across lines (`litellm \` / `==1.107.0`) is one requirement.
joined, start, buf = [], 0, ''
for n, line in enumerate(lines, 1):
line = re.sub(r'(^|\s)#.*$', '', line).rstrip()
if not buf:
start = n
if line.endswith('\\'):
buf += line[:-1] + ' '
continue
joined.append((start, buf + line))
buf = ''
if buf:
joined.append((start, buf))
for n, line in joined:
line = line.strip()
if not line or line.startswith('-'):
continue
for name, spec in pypi_pins.parse_install_args(line):
reqs.append((name, spec, n))
return reqs
table_entry = re.compile(
r'^\s*["\']?([A-Za-z0-9][A-Za-z0-9._-]*)["\']?\s*=\s*'
r'(?:["\']([^"\']*)["\']|\{[^}]*\bversion\s*=\s*["\']([^"\']*)["\'])')
for n, line in enumerate(lines, 1):
m = table_entry.match(line)
if m:
spec = (m.group(2) if m.group(2) is not None else m.group(3)) or ''
# Poetry reads a bare version ("1.107.0") and "=1.107.0" as exact pins.
if re.match(r'^\s*=?\s*v?\d[\d.]*(?:\.\*)?\s*$', spec):
spec = '==' + spec.strip().lstrip('=').strip()
reqs.append((pypi_pins.normalize_name(m.group(1)), spec, n))
if basename == 'pyproject.toml':
for q in re.finditer(r'"([^"\n]+)"|\'([^\'\n]+)\'', line):
req = pypi_pins.parse_requirement(q.group(1) or q.group(2))
if req and req[1]:
reqs.append((req[0], req[1], n))
return reqs
def check_pypi_pins(requirements, rel_path, db=None):
"""Findings for requirements (name, spec, line) that pin a compromised
PyPI release. Exact pins and `==X.*` prefixes only; see pypi_pins."""
if db is None:
db = _pypi_compromised_db()
findings = []
seen = set()
for name, spec, line_no in requirements:
bad = db.get(name)
if not bad or not spec:
continue
for ver in pypi_pins.spec_hits(spec, bad):
if (name, ver, line_no) in seen:
continue
seen.add((name, ver, line_no))
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="critical",
title=f"Compromised Package Version: {name}=={ver}",
description=(
f"'{name}' is pinned to {ver} ('{spec.strip()}'), a release known to be "
f"compromised (supply chain attack IOC, campaign: {bad[ver]}). "
f"Do not install it; if it was installed, treat the environment as exposed."
),
file=rel_path, line=line_no,
snippet=f"{name}{re.sub(chr(32), '', spec)} (known compromised, {bad[ver]})",
category="supply-chain"
))
return findings
_REQ_FILE_RE = re.compile(
r'^(?:.*[-_.])?(?:requirements?|constraints?)(?:[-_.][\w.-]*)?\.(?:txt|in)$', re.I)
def is_extra_requirements_file(file_path):
"""True for requirements-style files outside the four names that get the
full Python dependency checks: requirements-prod.txt, dev-requirements.txt,
requirements.in, constraints-3.12.txt, requirements/base.txt."""
base = os.path.basename(file_path)
if base in ('requirements.txt', 'requirements-dev.txt', 'requirements-test.txt',
'constraints.txt'):
return False
if _REQ_FILE_RE.match(base):
return True
parent = os.path.basename(os.path.dirname(file_path)).lower()
return parent in ('requirements', 'requirement', 'reqs') and base.lower().endswith(('.txt', '.in'))
def scan_pypi_pins_only(filepath, rel_path):
"""Compromised-pin check alone, for requirements-style files that do not get
the rest of scan_python_deps (no unbounded-range or typosquat noise)."""
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
except (UnicodeDecodeError, OSError) as e:
print(f"[!] Skipped {rel_path}: {e}", file=sys.stderr)
return []
return check_pypi_pins(_python_manifest_requirements('requirements.txt', content), rel_path)
def check_suspicious_scopes(dependencies, rel_path):
"""Flag packages from suspicious npm scopes (systematic forking campaigns)."""
findings = []
for dep in dependencies:
for scope in SUSPICIOUS_NPM_SCOPES:
if dep.lower().startswith(scope + "/"):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Suspicious Scope Package: '{dep}'",
description=f"Package from '{scope}' scope (systematic MCP server forking campaign, March 2026)",
file=rel_path, line=0,
snippet=f"'{dep}' from suspicious scope '{scope}'",
category="suspicious-scope"
))
return findings
_OVERRIDES_MAX_DEPTH = 32 # real npm/yarn/pnpm overrides never nest beyond ~3
def _flatten_overrides(overrides_map, result=None, _depth=0, _seen=None):
"""Recursively flatten npm `overrides` / yarn `resolutions` nested maps.
npm overrides can be nested: {"foo": {".": "1.0.0", "bar": "2.0.0"}} means
override foo to 1.0.0 AND override foo's transitive bar to 2.0.0. Yarn
resolutions use **/package syntax. This flattener extracts all
package@version pairs regardless of nesting depth.
Depth-guarded against adversarial package.json with thousands of nested
levels (security review SS-F1, 2026-04-05). A RecursionError here aborts
the entire scanner walk and silently suppresses chalk@5.6.1 detection.
"""
if result is None:
result = {}
if _seen is None:
_seen = set()
if _depth > _OVERRIDES_MAX_DEPTH:
return result
if not isinstance(overrides_map, dict):
return result
# Cycle guard: Python dicts from json.load cannot be cyclic, but raw dicts
# from test harnesses or future YAML anchor loaders could be.
obj_id = id(overrides_map)
if obj_id in _seen:
return result
_seen.add(obj_id)
for key, value in overrides_map.items():
if isinstance(value, str):
# Leaf: package name -> version string
# Strip yarn resolution glob prefix (**/) and leading/trailing slashes
pkg_name = key.replace('**/', '').strip('/')
if pkg_name and pkg_name != '.':
result[pkg_name] = value
elif isinstance(value, dict):
# Nested: {'.': '1.0.0', 'bar': '2.0.0'}
if '.' in value and isinstance(value['.'], str):
pkg_name = key.replace('**/', '').strip('/')
if pkg_name:
result[pkg_name] = value['.']
_flatten_overrides(value, result, _depth=_depth + 1, _seen=_seen)
return result
_JSON_NESTING_HARD_LIMIT = 10000 # refuse outright; real package.json nests ~10 deep
def _json_nesting_depth(text):
"""Maximum `{`/`[` nesting depth of a JSON document, computed in a single
pass without parsing (string- and escape-aware). Used to size the bounded
recursion-limit retry for adversarially deep package.json files and to
refuse documents too deep to parse safely."""
depth = max_depth = 0
in_string = False
escaped = False
for ch in text:
if in_string:
if escaped:
escaped = False
elif ch == '\\':
escaped = True
elif ch == '"':
in_string = False
elif ch == '"':
in_string = True
elif ch in '{[':
depth += 1
if depth > max_depth:
max_depth = depth
elif ch in '}]':
depth -= 1
return max_depth
def scan_package_json(filepath, rel_path):
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
raw = f.read()
try:
data = json.loads(raw)
except RecursionError:
# Adversarial package.json with pathologically nested structure
# (e.g. thousands of nested `overrides` levels) blows the default
# recursion limit inside the JSON parser itself, before the depth
# guard in _flatten_overrides can even run. Flag the file, then
# retry under a bounded, temporarily raised recursion limit so the
# IOC / compromised-version checks below still run for this file
# instead of being silently suppressed. (SS-F1, 2026-04-05.)
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title="Adversarial package.json structure",
description="Refused to parse hostile package.json: RecursionError",
file=rel_path, line=0, snippet="excessive JSON nesting depth",
category="parser-dos"
))
nesting = _json_nesting_depth(raw)
if nesting > _JSON_NESTING_HARD_LIMIT:
return findings
old_limit = sys.getrecursionlimit()
sys.setrecursionlimit(max(old_limit, nesting * 2 + 1024))
try:
data = json.loads(raw)
except RecursionError:
return findings
finally:
sys.setrecursionlimit(old_limit)
all_deps = {}
for key in ('dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'):
all_deps.update(data.get(key, {}))
# Parse npm `overrides`, yarn `resolutions`, and pnpm `pnpm.overrides`.
# These override the resolved version of a transitive dep and are a
# real bypass vector: a single-field PR can inject chalk@5.6.1 as an
# override without changing any top-level dependency.
override_deps = {}
override_deps.update(_flatten_overrides(data.get('overrides', {})))
override_deps.update(_flatten_overrides(data.get('resolutions', {})))
pnpm_section = data.get('pnpm', {})
if isinstance(pnpm_section, dict):
override_deps.update(_flatten_overrides(pnpm_section.get('overrides', {})))
# pnpm catalog: (pnpm-workspace.yaml has catalogs, package.json can
# reference them; we scan declared catalogs in package.json too)
catalogs = data.get('catalog', {})
if isinstance(catalogs, dict):
for pkg, ver in catalogs.items():
if isinstance(ver, str):
override_deps[pkg] = ver
# Merge overrides into all_deps for the downstream checks, but also
# flag them as override-sourced for attribution
if override_deps:
for pkg, ver in override_deps.items():
if pkg not in all_deps:
all_deps[pkg] = ver
# Run compromised-version check specifically on overrides so the
# finding snippet can call out the override vector
override_findings = check_compromised_versions(override_deps, rel_path, strict_pin=True)
for f in override_findings:
f.description = f.description + " [via overrides/resolutions/catalog]"
f.category = "supply-chain-override"
findings.extend(override_findings)
findings.extend(_check_vulns("npm", override_deps, rel_path))
findings.extend(_check_freshness("npm", override_deps, rel_path))
# Flag .pnpmfile.cjs as an install-time rewriting vector (advisory only)
pkg_dir = os.path.dirname(filepath)
pnpmfile = os.path.join(pkg_dir, '.pnpmfile.cjs')
if os.path.exists(pnpmfile):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=".pnpmfile.cjs present",
description=(
".pnpmfile.cjs can rewrite dependency specs at install "
"time. Review for malicious rewrites (e.g. redirecting "
"chalk to a compromised version)."
),
file=os.path.join(os.path.dirname(rel_path), '.pnpmfile.cjs'),
line=0,
snippet=".pnpmfile.cjs install-time hook",
category="install-time-rewriter"
))
dep_names = list(all_deps.keys())
# Missing lockfile detection
if all_deps:
pkg_dir = os.path.dirname(filepath)
lockfile_names = ('package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'bun.lockb', 'bun.lock')
has_lockfile = any(os.path.exists(os.path.join(pkg_dir, lf)) for lf in lockfile_names)
# Monorepo check: look up 2 parent directories
if not has_lockfile:
parent = os.path.dirname(pkg_dir)
for _ in range(2):
if any(os.path.exists(os.path.join(parent, lf)) for lf in lockfile_names):
has_lockfile = True
break
parent = os.path.dirname(parent)
if not has_lockfile:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title="Missing Lockfile",
description="No lockfile found (package-lock.json, yarn.lock, pnpm-lock.yaml, bun.lockb). Dependencies resolve to latest in range on every install.",
file=rel_path, line=0,
snippet=f"{len(all_deps)} dependencies without lockfile",
category="missing-lockfile"
))
# Git/HTTP/file dependency flagging
for pkg, ver in all_deps.items():
if not isinstance(ver, str):
continue
if ver.startswith(('git+', 'git://', 'github:')):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Git Dependency: {pkg}",
description="Git dependency bypasses registry integrity checks (PackageGate .npmrc injection vector)",
file=rel_path, line=0, snippet=f"{pkg}: {ver[:120]}",
category="git-dependency"
))
elif ver.startswith('http://'):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="critical",
title=f"HTTP Dependency: {pkg}",
description="Dependency fetched over unencrypted HTTP (MITM attack vector)",
file=rel_path, line=0, snippet=f"{pkg}: {ver[:120]}",
category="insecure-protocol"
))
elif ver.startswith('file:'):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"Local File Dependency: {pkg}",
description="Dependency references local filesystem path",
file=rel_path, line=0, snippet=f"{pkg}: {ver[:120]}",
category="local-dependency"
))
# Orphan commit references in any dependency field (TanStack worm vector).
# The worm injected @tanstack/setup as an optionalDependency pointing
# to a GitHub URL with a specific orphan commit hash. Scanning all
# dependency fields catches copycat variants.
_ORPHAN_RE = re.compile(r'(?:github\.com/|github:)[^/\s]+/[^/\s]+#[a-fA-F0-9]{40}')
for dep_key in ('dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'):
for pkg, ver in data.get(dep_key, {}).items():
if isinstance(ver, str) and _ORPHAN_RE.search(ver):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="critical",
title=f"Orphan Commit Reference: {pkg}",
description=(
f"{dep_key} references a GitHub commit SHA. "
"The TanStack worm used this to pull malicious code from "
"orphan commits invisible to branch inspection "
"(TeamPCP Wave 7, May 2026)"
),
file=rel_path, line=0,
snippet=f"{pkg}: {ver[:120]}",
category="orphan-commit"
))
# Self-IOC check: the scanned package's OWN name against the known
# malicious package list. Dependency checks cover what a package pulls
# in; a directly-downloaded malicious tarball names itself, and that
# name is the IOC.
own_name = data.get('name')
if isinstance(own_name, str) and own_name.lower() in _get_ioc_packages():
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="critical",
title=f"Known Malicious Package (self): '{own_name}'",
description="This package's own name matches the known-malicious package IOC list",
file=rel_path, line=0,
snippet=f"'{own_name}' is a known malicious package",
category="known-ioc"
))
# Provenance squat: identity metadata claims a canonical org while the
# package lives outside that org's canonical scope.
findings.extend(check_provenance_squat(data, rel_path))
# Known IOC check (critical, before typosquatting)
findings.extend(check_known_ioc_packages(dep_names, rel_path))
# Compromised versions of legitimate packages. strict_pin=True rejects
# range constraints like `<=5.6.1`, `~5.6.1` which would otherwise
# false-positive. Lockfile scanners pass strict_pin=False because
# their values are already resolved. (CRC-F1, 2026-04-05.)
findings.extend(check_compromised_versions(all_deps, rel_path, strict_pin=True))
findings.extend(_check_vulns("npm", all_deps, rel_path))
findings.extend(_check_freshness("npm", all_deps, rel_path))
# Suspicious npm scopes (forking campaigns)
findings.extend(check_suspicious_scopes(dep_names, rel_path))
# Typosquatting
typos = check_typosquatting(dep_names, POPULAR_NPM)
for suspect, target, score in typos:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Typosquat Risk: '{suspect}' ~ '{target}'",
description=f"Package name similarity {score:.0%} to popular package",
file=rel_path, line=0, snippet=f"'{suspect}' might be typosquatting '{target}'",
category="typosquatting"
))
# Slopsquatting (AI-hallucinated package names)
slops = check_slopsquatting(dep_names, "npm")
for pkg in slops:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Slopsquatting: '{pkg}'",
description="Package name matches known AI-hallucinated package corpus. "
"These names were never legitimate but are frequently suggested by LLMs, "
"then registered by attackers (Aikido Security, March 2026).",
file=rel_path, line=0, snippet=f"'{pkg}' is a known AI-hallucinated package name",
category="slopsquatting"
))
# Version anomaly
for pkg, ver in all_deps.items():
if isinstance(ver, str) and check_version_anomaly(ver):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Version Anomaly: {pkg}@{ver}",
description="Abnormally high major version (dependency confusion indicator)",
file=rel_path, line=0, snippet=f"{pkg}: {ver}",
category="dependency-confusion"
))
# StarJacking detection: package claims GitHub repo that doesn't match package name
# (Checkmarx, April 2022). 7.23% of npm packages have non-unique git references.
repo = data.get('repository', {})
repo_url = repo.get('url', '') if isinstance(repo, dict) else (repo if isinstance(repo, str) else '')
pkg_name = data.get('name', '')
if repo_url and pkg_name:
repo_url_lower = repo_url.lower().rstrip('.git').rstrip('/')
pkg_base = pkg_name.split('/')[-1].lower() # strip scope
# Skip generic scoped package bases that always mismatch repo names
_GENERIC_BASES = {'core', 'cli', 'common', 'utils', 'types', 'helpers', 'config', 'shared', 'base', 'lib'}
if pkg_base not in _GENERIC_BASES and ('github.com/' in repo_url_lower or 'gitlab.com/' in repo_url_lower):
repo_name = repo_url_lower.split('/')[-1] if '/' in repo_url_lower else ''
if repo_name and pkg_base and repo_name != pkg_base:
similarity = difflib.SequenceMatcher(None, repo_name, pkg_base).ratio()
if similarity < 0.5:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"StarJacking Signal: Repo '{repo_name}' != Package '{pkg_base}'",
description=f"Package name doesn't match its claimed repository. Could be StarJacking (Checkmarx, April 2022) where a package links to a popular repo to steal its star count. Similarity: {similarity:.0%}",
file=rel_path, line=0,
snippet=f"repo: {repo_url[:80]}, pkg: {pkg_name}",
category="starjacking"
))
except (json.JSONDecodeError, KeyError, TypeError) as e:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="Package manifest parse error",
description=f"Could not fully parse package.json: {e}",
file=rel_path, line=0, snippet=str(e)[:120],
category="parse-error"
))
except (RecursionError, MemoryError) as e:
# Adversarial package.json with pathologically nested overrides or
# huge arrays can exhaust the interpreter. Without this handler a
# single malicious file aborts the entire scanner walk loop and every
# subsequent IOC / compromised-version check silently fails. The
# depth guard in _flatten_overrides already protects the normal path;
# this is the belt-and-suspenders catch for any future recursive
# helper that slips through. (Security review SS-F1, 2026-04-05.)
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title="Adversarial package.json structure",
description=f"Refused to parse hostile package.json: {type(e).__name__}",
file=rel_path, line=0, snippet=str(e)[:120],
category="parser-dos"
))
except OSError as e:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="Package manifest read error",
description=f"Could not read package.json: {e}",
file=rel_path, line=0, snippet=str(e)[:120],
category="parse-error"
))
return findings
def scan_python_deps(filepath, rel_path):
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
deps = []
basename = os.path.basename(filepath)
# Check for companion lockfiles (suppress unbounded-range warnings if locked)
lock_dir = os.path.dirname(filepath)
py_lockfiles = ('requirements.lock', 'requirements-lock.txt', 'Pipfile.lock', 'poetry.lock')
has_py_lockfile = any(os.path.exists(os.path.join(lock_dir, lf)) for lf in py_lockfiles)
pinned_pkg_versions = {}
if basename in ('requirements.txt', 'requirements-dev.txt', 'requirements-test.txt', 'constraints.txt'):
for line in content.split('\n'):
line = line.strip()
if line and not line.startswith('#') and not line.startswith('-'):
pkg = re.split(r'[>=<!\[\];~]', line)[0].strip()
if pkg:
deps.append(pkg)
# Extract version if pinned (==X.Y.Z) for vuln check
pin_m = re.search(r'==\s*([A-Za-z0-9._+\-]+)', line)
if pin_m:
pinned_pkg_versions[pkg] = pin_m.group(1)
# Check version
ver_m = re.search(r'[>=<]=?(\d+)', line)
if ver_m and int(ver_m.group(1)) >= 90:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Version Anomaly: {pkg}",
description="Abnormally high version in requirements",
file=rel_path, line=0, snippet=line[:120],
category="dependency-confusion"
))
# Unbounded range detection (only if no lockfile)
if not has_py_lockfile and pkg:
# Bare package name with no version constraint
if pkg and line.strip() == pkg:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"No Version Constraint: {pkg}",
description="Package has no version constraint (installs latest on every install)",
file=rel_path, line=0, snippet=line[:120],
category="no-version-constraint"
))
# >= without upper bound
elif '>=' in line and '<' not in line and '~=' not in line and '==' not in line:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"Unbounded Version Range: {pkg}",
description=">=X.Y.Z with no upper bound allows arbitrary future versions",
file=rel_path, line=0, snippet=line[:120],
category="unbounded-range"
))
elif basename in ('pyproject.toml', 'Pipfile'):
for m in re.finditer(r'["\']([a-zA-Z0-9_-]+)["\']', content):
deps.append(m.group(1))
# Known IOC check first
findings.extend(check_known_ioc_packages(deps, rel_path))
# Pins on compromised releases (litellm==1.107.0 and friends)
findings.extend(check_pypi_pins(
_python_manifest_requirements(basename, content), rel_path))
# Vuln enrichment on pinned versions only (ranges can't be queried)
if pinned_pkg_versions:
findings.extend(_check_vulns("PyPI", pinned_pkg_versions, rel_path))
findings.extend(_check_freshness("PyPI", pinned_pkg_versions, rel_path))
typos = check_typosquatting(deps, POPULAR_PYPI)
for suspect, target, score in typos:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Typosquat Risk: '{suspect}' ~ '{target}'",
description=f"Package name similarity {score:.0%} to popular PyPI package",
file=rel_path, line=0, snippet=f"'{suspect}' might be typosquatting '{target}'",
category="typosquatting"
))
# Slopsquatting (AI-hallucinated package names)
slops = check_slopsquatting(deps, "PyPI")
for pkg in slops:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Slopsquatting: '{pkg}'",
description="Package name matches known AI-hallucinated package corpus. "
"These names were never legitimate but are frequently suggested by LLMs, "
"then registered by attackers (Aikido Security, March 2026).",
file=rel_path, line=0, snippet=f"'{pkg}' is a known AI-hallucinated package name",
category="slopsquatting"
))
except (UnicodeDecodeError, OSError) as e:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="Requirements file read error",
description=f"Could not fully parse requirements: {e}",
file=rel_path, line=0, snippet=str(e)[:120],
category="parse-error"
))
return findings
# --- Composer (Packagist) ------------------------------------------------------
#
# Platform requisites are not Packagist packages: "php", "hhvm", "composer*",
# "ext-*", "lib-*". They name the runtime the code needs, never something
# `composer install` downloads, so IOC/vuln package checks must skip them.
# (The "php" requirement itself is checked by scan_runtime_floors.py.)
_COMPOSER_PLATFORM_RE = re.compile(
r'^(php|hhvm|composer|composer-plugin-api|composer-runtime-api|ext-[^/]+|lib-[^/]+)$',
re.IGNORECASE)
def _is_composer_platform_req(name):
return bool(_COMPOSER_PLATFORM_RE.match(name))
def scan_composer_json(filepath, rel_path):
"""Scan composer.json require/require-dev packages against the IOC,
compromised-version and OSV (Packagist) checks.
Version strings here are CONSTRAINTS (^3.0, >=1.2 <2.0), so the
compromised-version check runs strict_pin=True and OSV only sees values
that normalize to an exact version.
"""
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
except (UnicodeDecodeError, OSError) as e:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="composer.json read error",
description=f"Could not read composer.json: {e}",
file=rel_path, line=0, snippet=str(e)[:120],
category="parse-error"))
return findings
try:
data = json.loads(content)
except json.JSONDecodeError as e:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="Package manifest parse error",
description=f"Could not fully parse composer.json: {e}",
file=rel_path, line=0, snippet=str(e)[:120],
category="parse-error"))
return findings
except RecursionError:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title="Adversarial composer.json structure",
description="Refused to parse hostile composer.json: RecursionError",
file=rel_path, line=0, snippet="",
category="parse-error"))
return findings
if not isinstance(data, dict):
return findings
all_deps = {}
for section in ('require', 'require-dev'):
reqs = data.get(section)
if not isinstance(reqs, dict):
continue
for name, constraint in reqs.items():
if not isinstance(name, str) or not isinstance(constraint, str):
continue
if _is_composer_platform_req(name):
continue
all_deps[name] = constraint
dep_names = list(all_deps.keys())
findings.extend(check_known_ioc_packages(dep_names, rel_path))
findings.extend(check_compromised_versions(all_deps, rel_path, strict_pin=True))
findings.extend(_check_vulns("Packagist", all_deps, rel_path))
# No Packagist freshness fetcher, popular corpus or slopsquat corpus yet;
# those checks are npm/PyPI-only by design.
return findings
def parse_composer_lock(filepath, rel_path):
"""Scan composer.lock resolved packages. Versions here are exact pins, so
the compromised-version check runs non-strict and every pin feeds OSV."""
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
except (UnicodeDecodeError, OSError) as e:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="composer.lock read error",
description=f"Could not read composer.lock: {e}",
file=rel_path, line=0, snippet=str(e)[:120],
category="parse-error"))
return findings
try:
data = json.loads(content)
except json.JSONDecodeError as e:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="Lockfile parse error",
description=f"Could not fully parse composer.lock: {e}",
file=rel_path, line=0, snippet=str(e)[:120],
category="parse-error"))
return findings
except RecursionError:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title="Adversarial composer.lock structure",
description="Refused to parse hostile composer.lock: RecursionError",
file=rel_path, line=0, snippet="",
category="parse-error"))
return findings
if not isinstance(data, dict):
return findings
pins = {}
for section in ('packages', 'packages-dev'):
entries = data.get(section)
if not isinstance(entries, list):
continue
for entry in entries:
if not isinstance(entry, dict):
continue
name, version = entry.get('name'), entry.get('version')
if isinstance(name, str) and isinstance(version, str):
if not _is_composer_platform_req(name):
pins[name] = version
dep_names = list(pins.keys())
findings.extend(check_known_ioc_packages(dep_names, rel_path))
findings.extend(check_compromised_versions(pins, rel_path))
findings.extend(_check_vulns("Packagist", pins, rel_path))
return findings
def scan_lockfile(filepath, rel_path):
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
resolved_urls = None
if os.path.basename(filepath) == 'package-lock.json':
try:
data = json.loads(content)
except json.JSONDecodeError:
data = None
if isinstance(data, dict):
resolved_urls = []
packages = data.get('packages', {})
if isinstance(packages, dict):
resolved_urls.extend(
info['resolved'] for info in packages.values()
if isinstance(info, dict) and isinstance(info.get('resolved'), str)
)
dependencies = data.get('dependencies', {})
pending = list(dependencies.values()) if isinstance(dependencies, dict) else []
while pending:
info = pending.pop()
if not isinstance(info, dict):
continue
if isinstance(info.get('resolved'), str):
resolved_urls.append(info['resolved'])
nested = info.get('dependencies', {})
if isinstance(nested, dict):
pending.extend(nested.values())
urls = (
[url for url in resolved_urls if url.startswith(('http://', 'https://'))]
if resolved_urls is not None
else re.findall(r'https?://[^\s"\'}]+', content)
)
suspicious = set()
for url in urls:
# Check hostname only to prevent path-based bypass (e.g., evil.com/registry.npmjs.org/)
hostname = urlparse(url).hostname or ''
is_trusted = any(hostname == t or hostname.endswith('.' + t) for t in TRUSTED_REGISTRIES)
is_benign = any(hostname == d or hostname.endswith('.' + d) for d in KNOWN_BENIGN_DOMAINS)
if not is_trusted and (resolved_urls is not None or (not is_benign and "schema.org" not in hostname)):
suspicious.add(url)
# Flag git+ and git:// resolved URLs in lockfiles (separate regex since https? doesn't match these)
git_urls = (
[url for url in resolved_urls if url.startswith(('git+http://', 'git+https://', 'git://'))]
if resolved_urls is not None
else re.findall(r'(?:git\+https?://|git://)[^\s"\'}]+', content)
)
for url in git_urls:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title="Lockfile: Git-Resolved Dependency",
description="Lockfile resolves dependency via git (bypasses registry integrity)",
file=rel_path, line=0, snippet=url[:120],
category="git-dependency"
))
# Flag http:// resolved URLs (MITM risk)
for url in urls:
if url.startswith('http://'):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="critical",
title="Lockfile: HTTP-Resolved Dependency",
description="Lockfile resolves dependency over unencrypted HTTP (MITM risk)",
file=rel_path, line=0, snippet=url[:120],
category="insecure-protocol"
))
if suspicious:
for url in list(suspicious)[:5]:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="critical",
title="Untrusted Registry URL",
description="Lockfile resolves to non-standard registry (supply chain risk)",
file=rel_path, line=0, snippet=url[:120],
category="untrusted-registry"
))
except (UnicodeDecodeError, OSError) as e:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="low",
title="Lockfile read error",
description=f"Could not read lockfile: {e}",
file=rel_path, line=0, snippet=str(e)[:120],
category="parse-error"
))
return findings
# --- Transitive Dependency Scanning ---
# Parses lockfiles to extract ALL package names (not just top-level)
# and checks them against the IOC list for supply chain attacks.
def parse_package_lock_json(filepath, rel_path):
"""Parse package-lock.json (v1, v2, v3) to extract all transitive dependencies."""
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
all_packages = set()
lockfile_version = data.get('lockfileVersion', 1)
# v2/v3 format: "packages" key with flattened node_modules paths
if 'packages' in data and lockfile_version >= 2:
for pkg_path, pkg_info in data.get('packages', {}).items():
if pkg_path == '':
continue # Skip root package
# Extract package name from path (e.g., "node_modules/foo" -> "foo")
parts = pkg_path.split('node_modules/')
if parts:
name = parts[-1]
if name:
all_packages.add(name)
# Integrity hash verification (v2/v3 only)
# Skip workspace links and root package
if pkg_info.get('link'):
continue
if 'integrity' not in pkg_info and pkg_path:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"Missing Integrity Hash: {name if parts else pkg_path}",
description="Package in lockfile has no integrity hash (tampered lockfile indicator)",
file=rel_path, line=0,
snippet=f"{pkg_path}: no integrity field",
category="missing-integrity"
))
# v1 format: "dependencies" key with nested structure
if 'dependencies' in data:
_collect_npm_deps_recursive(data['dependencies'], all_packages)
# Check ALL transitive deps against IOC list
if all_packages:
findings.extend(check_known_ioc_packages(list(all_packages), rel_path))
# Suspicious npm scopes in transitive deps
findings.extend(check_suspicious_scopes(list(all_packages), rel_path))
# Build pkg -> version map and feed it through check_compromised_versions
# so transitive deps use the same merged IOC database (ioc_manager JSON
# + in-module fallback) and normalize_version() bypass defenses as every
# other code path. Without this, chalk@5.6.1 in a package-lock.json
# transitive dep would silently not flag — the #1 blocker caught in
# the 2026-04-05 code review.
pkg_versions = {}
for pkg in all_packages:
info = _find_package_info(data, pkg)
version = info.get('version', '') if info else ''
if version:
pkg_versions[pkg] = version
if pkg_versions:
findings.extend(check_compromised_versions(pkg_versions, rel_path))
findings.extend(_check_vulns("npm", pkg_versions, rel_path))
findings.extend(_check_freshness("npm", pkg_versions, rel_path))
# Typosquatting check on transitive deps too
typos = check_typosquatting(list(all_packages), POPULAR_NPM)
for suspect, target, score in typos:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Transitive Typosquat Risk: '{suspect}' ~ '{target}'",
description=f"Transitive dependency name similarity {score:.0%} to popular package",
file=rel_path, line=0,
snippet=f"'{suspect}' (transitive) might be typosquatting '{target}'",
category="typosquatting"
))
except (json.JSONDecodeError, KeyError, TypeError, OSError) as e:
print(f"[!] Skipped {rel_path}: {e}", file=sys.stderr)
return findings
def _collect_npm_deps_recursive(deps_dict, all_packages, depth=0):
"""Recursively collect package names from v1 package-lock.json dependencies."""
if depth > 20 or len(all_packages) > 50000: # Prevent infinite recursion and memory exhaustion
return
for name, info in deps_dict.items():
all_packages.add(name)
if isinstance(info, dict) and 'dependencies' in info:
_collect_npm_deps_recursive(info['dependencies'], all_packages, depth + 1)
def _find_package_info(data, package_name):
"""Find package info in package-lock.json (works with v1 and v2/v3)."""
# v2/v3: check packages
packages = data.get('packages', {})
for pkg_path, info in packages.items():
if pkg_path.endswith(f'node_modules/{package_name}'):
return info
# v1: check dependencies
deps = data.get('dependencies', {})
if package_name in deps:
return deps[package_name]
return None
def parse_yarn_lock(filepath, rel_path):
"""Parse yarn.lock to extract all package names."""
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
all_packages = set()
# yarn.lock format: "package@version:" or "package@^version, package@~version:"
for match in re.finditer(r'^"?(@?[^@\s"]+)@', content, re.MULTILINE):
name = match.group(1)
if name and not name.startswith('#'):
all_packages.add(name)
if all_packages:
findings.extend(check_known_ioc_packages(list(all_packages), rel_path))
findings.extend(check_suspicious_scopes(list(all_packages), rel_path))
# Extract RESOLVED versions by parsing yarn.lock as blocks. Each
# block starts with a header line like `"chalk@^5.6.0":` (which
# contains the CONSTRAINT, not the resolved version) followed by
# a `version "x.y.z"` line with the RESOLVED version.
#
# Regex-only parsing (the old approach) extracted the constraint
# because the header regex matched first. Result: chalk@^5.6.0
# resolving to 5.6.1 during the Sep 2025 compromise window was
# NOT detected because the scanner saw "5.6.0" not "5.6.1".
# Security review A2 (2026-04-05).
pkg_versions = {}
current_header_names = []
for line in content.split('\n'):
stripped = line.rstrip()
if not stripped or stripped.startswith('#'):
continue
# Header lines are at column 0 and end with `:`
if not stripped[0].isspace() and stripped.endswith(':'):
header = stripped[:-1].strip('"')
# Multiple names separated by ", " share the resolved version
current_header_names = []
for entry in header.split(', '):
entry = entry.strip().strip('"')
# Extract bare package name (strip @version constraint)
# Scoped: @scope/name@^1.0.0 -> @scope/name
if entry.startswith('@'):
# second @ separates scope/name from version
second_at = entry.find('@', 1)
name = entry[:second_at] if second_at > 0 else entry
else:
at = entry.find('@')
name = entry[:at] if at > 0 else entry
if name:
current_header_names.append(name)
# Indented `version "x.y.z"` line carries the resolved version
elif current_header_names and stripped.lstrip().startswith('version'):
m = re.search(r'version\s+"([^"]+)"', stripped)
if m:
resolved = m.group(1)
for name in current_header_names:
pkg_versions[name] = resolved
current_header_names = []
if pkg_versions:
findings.extend(check_compromised_versions(pkg_versions, rel_path))
findings.extend(_check_vulns("npm", pkg_versions, rel_path))
findings.extend(_check_freshness("npm", pkg_versions, rel_path))
typos = check_typosquatting(list(all_packages), POPULAR_NPM)
for suspect, target, score in typos:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Transitive Typosquat Risk: '{suspect}' ~ '{target}'",
description=f"Transitive dependency (yarn.lock) similarity {score:.0%}",
file=rel_path, line=0,
snippet=f"'{suspect}' (transitive) might be typosquatting '{target}'",
category="typosquatting"
))
except (UnicodeDecodeError, OSError) as e:
print(f"[!] Skipped {rel_path}: {e}", file=sys.stderr)
return findings
def parse_poetry_lock(filepath, rel_path):
"""Parse poetry.lock to extract all package names."""
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
all_packages = set()
pkg_versions = {}
# poetry.lock format: [[package]]\nname = "package-name"\nversion = "x.y.z"
for match in re.finditer(
r'^\[\[package\]\]\s*\nname\s*=\s*"([^"]+)"\s*\nversion\s*=\s*"([^"]+)"',
content, re.MULTILINE
):
name, version = match.group(1), match.group(2)
all_packages.add(name)
pkg_versions[name] = version
# Also catch packages without version on next line
for match in re.finditer(r'^\s*name\s*=\s*"([^"]+)"', content, re.MULTILINE):
all_packages.add(match.group(1))
if all_packages:
findings.extend(check_known_ioc_packages(list(all_packages), rel_path))
# Check all packages against known compromised versions
if pkg_versions:
findings.extend(check_compromised_versions(pkg_versions, rel_path))
findings.extend(_check_vulns("PyPI", pkg_versions, rel_path))
findings.extend(_check_freshness("PyPI", pkg_versions, rel_path))
typos = check_typosquatting(list(all_packages), POPULAR_PYPI)
for suspect, target, score in typos:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Transitive Typosquat Risk: '{suspect}' ~ '{target}'",
description=f"Transitive dependency (poetry.lock) similarity {score:.0%}",
file=rel_path, line=0,
snippet=f"'{suspect}' (transitive) might be typosquatting '{target}'",
category="typosquatting"
))
except (UnicodeDecodeError, OSError) as e:
print(f"[!] Skipped {rel_path}: {e}", file=sys.stderr)
return findings
def parse_pnpm_lock_file(filepath, rel_path):
"""Parse pnpm-lock.yaml (v6+v9) and check packages against IOC lists.
This closes the gap flagged in Marc Gadsdon's issue #5 suggestion #4:
pnpm lockfiles previously got only URL-level scanning via scan_lockfile,
so version-pinned IOCs (chalk@5.6.1, @nx/devkit@20.9.0, etc) inside
pnpm transitive deps were never checked.
"""
findings = []
try:
import parse_pnpm_lock as _pnpm
deps = _pnpm.parse_pnpm_lock(filepath)
except ImportError as e:
print(f"[!] parse_pnpm_lock unavailable: {e}", file=sys.stderr)
return findings
except (OSError, ValueError) as e:
print(f"[!] Skipped {rel_path}: {e}", file=sys.stderr)
return findings
if not deps:
return findings
# All the standard checks
findings.extend(check_known_ioc_packages(list(deps.keys()), rel_path))
findings.extend(check_suspicious_scopes(list(deps.keys()), rel_path))
findings.extend(check_compromised_versions(deps, rel_path))
findings.extend(_check_vulns("npm", deps, rel_path))
findings.extend(_check_freshness("npm", deps, rel_path))
# Typosquatting on the package names
typos = check_typosquatting(list(deps.keys()), POPULAR_NPM)
for suspect, target, score in typos:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Transitive Typosquat Risk: '{suspect}' ~ '{target}'",
description=f"Transitive dependency (pnpm-lock.yaml) similarity {score:.0%}",
file=rel_path, line=0,
snippet=f"'{suspect}' (transitive) might be typosquatting '{target}'",
category="typosquatting"
))
return findings
def parse_pipfile_lock(filepath, rel_path):
"""Parse Pipfile.lock to extract all package names."""
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
all_packages = set()
pkg_versions = {}
for section in ('default', 'develop'):
for pkg_name, pkg_info in data.get(section, {}).items():
all_packages.add(pkg_name)
version = pkg_info.get('version', '').lstrip('=')
if version:
pkg_versions[pkg_name] = version
if all_packages:
# Check all packages against known compromised versions
if pkg_versions:
findings.extend(check_compromised_versions(pkg_versions, rel_path))
findings.extend(_check_vulns("PyPI", pkg_versions, rel_path))
findings.extend(_check_freshness("PyPI", pkg_versions, rel_path))
findings.extend(check_known_ioc_packages(list(all_packages), rel_path))
typos = check_typosquatting(list(all_packages), POPULAR_PYPI)
for suspect, target, score in typos:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Transitive Typosquat Risk: '{suspect}' ~ '{target}'",
description=f"Transitive dependency (Pipfile.lock) similarity {score:.0%}",
file=rel_path, line=0,
snippet=f"'{suspect}' (transitive) might be typosquatting '{target}'",
category="typosquatting"
))
except (json.JSONDecodeError, KeyError, TypeError, OSError) as e:
print(f"[!] Skipped {rel_path}: {e}", file=sys.stderr)
return findings
# --- User-supplied package list (--package-list) ---
# Hard limits for user-supplied package list files. These are defense-in-
# depth: users should only ever load their own lists, but a malicious repo
# can plant a .package-list file in hopes an automation blindly loads it.
_PACKAGE_LIST_MAX_BYTES = 256 * 1024 # 256KB
_PACKAGE_LIST_MAX_WILDCARDS = 100
_PACKAGE_LIST_MAX_ENTRIES = 10_000
# Strict package entry regex. Enforces:
# - Optional leading @scope/
# - Package name: letters, digits, dot, dash, underscore
# - Optional @version suffix (or @* wildcard for entirely-malicious)
_PACKAGE_ENTRY_RE = re.compile(
r"^(?P<name>@[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+|[a-zA-Z0-9_.-]+)"
r"(?:@(?P<version>\*|[A-Za-z0-9_.\-+]+))?$"
)
def load_package_list(path, scanned_repo_path=None):
"""Load a user-supplied package list with strict path and content hardening.
Format: one `package@version` (or `package@*` wildcard) per line. Lines
starting with `#` are comments, blank lines are skipped.
Hardening (all P0 per 2026-04-05 security review):
- Requires absolute path
- Rejects symlinks (os.lstat S_ISLNK)
- Rejects if the realpath is inside the scanned repo (plantable file)
- Rejects if the file is not owned by the current user (UNIX only)
- 256KB file size cap
- Strict regex parser (rejects shell metacharacters, path separators)
- Wildcard entry cap: 100 max (DoS prevention)
- Entry cap: 10,000 max
- NFC unicode normalization
- Missing file is a hard error (raises), never silently proceeds
Returns (version_pinned, entirely_malicious) tuple suitable for merging
into the IOC database:
- version_pinned: dict[str, dict[str, str]] keyed by lower-cased package
name -> {version: "user-list-file"}
- entirely_malicious: set[str] of lower-cased package names
Raises:
ValueError: for any hardening violation or parse error.
"""
import stat as _stat
if not isinstance(path, str) or not path:
raise ValueError("--package-list path must be a non-empty string")
if not os.path.isabs(path):
raise ValueError(
f"--package-list path must be absolute, got {path!r}. "
f"Use an absolute path like /path/to/iocs.txt"
)
# Reject symlinks at the top-level (os.lstat to NOT follow)
try:
lstat_result = os.lstat(path)
except OSError as e:
raise ValueError(f"--package-list file not found or unreadable: {e}")
if _stat.S_ISLNK(lstat_result.st_mode):
raise ValueError(
f"--package-list refuses symlinks (got {path!r}). "
f"Use the real path directly."
)
# Resolve real path to check repo containment + size
real_path = os.path.realpath(path)
# If we know the scanned repo, reject lists planted inside it
if scanned_repo_path:
scanned_real = os.path.realpath(scanned_repo_path)
try:
# commonpath raises if paths are on different drives (Windows)
common = os.path.commonpath([real_path, scanned_real])
except ValueError:
common = None # different drives; not inside repo by definition
if common == scanned_real:
raise ValueError(
f"--package-list refuses files inside the scanned repo "
f"(planted-file attack defense). Got {path!r} inside "
f"{scanned_repo_path!r}."
)
# Ownership check (best-effort; skip on non-POSIX)
try:
current_uid = os.getuid()
if lstat_result.st_uid != current_uid:
raise ValueError(
f"--package-list refuses files not owned by the current "
f"user (expected uid {current_uid}, got {lstat_result.st_uid}). "
f"Likely a planted file."
)
except AttributeError:
pass # Windows: no uid concept
# Size cap
if lstat_result.st_size > _PACKAGE_LIST_MAX_BYTES:
raise ValueError(
f"--package-list file too large "
f"({lstat_result.st_size} bytes, max {_PACKAGE_LIST_MAX_BYTES})"
)
# Read and parse. Use O_NOFOLLOW to prevent TOCTOU attacks: between the
# lstat above and this open, an attacker could symlink the path to a
# different file. O_NOFOLLOW refuses the open if the path is (now) a
# symlink, and we verify the opened file's inode matches the lstat
# result so the underlying file hasn't been swapped. Security review
# B on load_package_list TOCTOU (2026-04-05).
try:
nofollow = getattr(os, 'O_NOFOLLOW', 0) # 0 on Windows
fd = os.open(real_path, os.O_RDONLY | nofollow)
except OSError as e:
raise ValueError(f"--package-list could not open {path!r}: {e}")
fd_owned = False
try:
fstat_result = os.fstat(fd)
if (
fstat_result.st_dev != lstat_result.st_dev
or fstat_result.st_ino != lstat_result.st_ino
):
raise ValueError(
f"--package-list file changed between lstat and open "
f"(possible TOCTOU attack): {path!r}"
)
try:
fd_owned = True # os.fdopen takes ownership of fd from this point
with os.fdopen(fd, 'r', encoding='utf-8') as f:
content = f.read()
except UnicodeDecodeError as e:
raise ValueError(f"--package-list could not decode {path!r}: {e}")
except BaseException:
if not fd_owned:
try:
os.close(fd)
except OSError:
pass
raise
version_pinned = {}
entirely_malicious = set()
wildcard_count = 0
entry_count = 0
for line_no, raw_line in enumerate(content.split('\n'), start=1):
line = unicodedata.normalize('NFC', raw_line.strip())
if not line or line.startswith('#'):
continue
entry_count += 1
if entry_count > _PACKAGE_LIST_MAX_ENTRIES:
raise ValueError(
f"--package-list has too many entries "
f"(max {_PACKAGE_LIST_MAX_ENTRIES})"
)
m = _PACKAGE_ENTRY_RE.match(line)
if not m:
raise ValueError(
f"--package-list line {line_no}: invalid entry {line!r}. "
f"Expected 'package@version' or 'package@*' format."
)
name = m.group('name').lower()
version = m.group('version')
if version is None or version == '*':
wildcard_count += 1
if wildcard_count > _PACKAGE_LIST_MAX_WILDCARDS:
raise ValueError(
f"--package-list has too many wildcard entries "
f"(max {_PACKAGE_LIST_MAX_WILDCARDS})"
)
entirely_malicious.add(name)
else:
# Normalize the version so lookups match the scanner's normalized form
normalized = normalize_version(version) or version
if name not in version_pinned:
version_pinned[name] = {}
version_pinned[name][normalized] = "user-list-file"
return version_pinned, entirely_malicious
def _merge_user_package_list_into_db(path, repo_path):
"""Load a user-supplied package list and merge into the runtime IOC db.
Called from main() when --package-list is passed. Mutates the module-level
_COMPROMISED_VERSIONS_DB and the _FALLBACK_IOC_PACKAGES set so subsequent
calls to check_compromised_versions / check_known_ioc_packages see the
user's entries.
"""
global _COMPROMISED_VERSIONS_DB, _SANDWORM_KNOWN_IOC_PACKAGES
version_pinned, entirely_malicious = load_package_list(path, scanned_repo_path=repo_path)
# Force the lazy-loaded db and ioc-packages set to populate, then extend
db = _compromised_versions_db()
for pkg, versions in version_pinned.items():
if pkg not in db:
db[pkg] = {}
db[pkg].update(versions)
if entirely_malicious:
# Mutate the cached ioc packages set so check_known_ioc_packages sees it
current = _get_ioc_packages()
_SANDWORM_KNOWN_IOC_PACKAGES = current | entirely_malicious
core.emit_status(
"text",
f"[*] Loaded {sum(len(v) for v in version_pinned.values())} version-pinned + "
f"{len(entirely_malicious)} wildcard IOCs from {path}"
)
# ============================================================
# Manifest Confusion Detection (Item 2)
# Detects package.json inconsistencies that indicate manifest confusion:
# the attack where npm metadata differs from tarball content.
# ============================================================
def _compute_entropy(data):
"""Compute Shannon entropy of a byte string or text string."""
if not data:
return 0.0
if isinstance(data, str):
data = data.encode('utf-8', errors='replace')
byte_counts = [0] * 256
for b in data:
byte_counts[b] += 1
length = len(data)
entropy = 0.0
for count in byte_counts:
if count == 0:
continue
p = count / length
entropy -= p * math.log2(p)
return entropy
def _check_suspicious_bin_content(content):
"""Check if bin script content has suspicious shell commands."""
suspicious_patterns = [
re.compile(r'\bcurl\b.*\|.*\b(ba)?sh\b'),
re.compile(r'\bwget\b.*\|.*\b(ba)?sh\b'),
re.compile(r'\bcurl\b.*-[oO]\s'),
re.compile(r'\bwget\b.*-O\s'),
re.compile(r'\beval\b.*\$\('),
]
for pattern in suspicious_patterns:
if pattern.search(content):
return True
return False
def scan_manifest_confusion(filepath, rel_path):
"""Detect manifest confusion indicators in package.json.
Checks for:
- scripts referencing non-existent files
- main/exports pointing to high-entropy (obfuscated) files
- bin entries with suspicious content (curl, wget piped to shell)
"""
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
except (json.JSONDecodeError, OSError):
return findings
if not isinstance(data, dict):
return findings
pkg_dir = os.path.dirname(filepath)
# Check scripts referencing non-existent files
scripts = data.get('scripts', {})
if isinstance(scripts, dict):
for script_name, script_cmd in scripts.items():
if not isinstance(script_cmd, str):
continue
# Extract file references from script commands (node file.js, ./file.sh, etc)
file_refs = re.findall(r'(?:node\s+|\.\/|sh\s+|bash\s+)([a-zA-Z0-9_./\-]+\.\w+)', script_cmd)
for ref in file_refs:
ref_path = os.path.join(pkg_dir, ref)
if not os.path.exists(ref_path) and not ref.startswith('node_modules'):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"Manifest Confusion: Script '{script_name}' references missing file",
description=(
f"Script '{script_name}' references '{ref}' which does not exist "
f"in the repo. May indicate tarball content differs from metadata."
),
file=rel_path, line=0,
snippet=f"{script_name}: {script_cmd[:100]}",
category="manifest-confusion"
))
# Check main/exports pointing to high-entropy files
entry_fields = []
main_field = data.get('main', '')
if isinstance(main_field, str) and main_field:
entry_fields.append(('main', main_field))
exports = data.get('exports', {})
if isinstance(exports, str):
entry_fields.append(('exports', exports))
elif isinstance(exports, dict):
for key, val in exports.items():
if isinstance(val, str):
entry_fields.append((f'exports[{key}]', val))
for field_name, entry_path in entry_fields:
full_path = os.path.join(pkg_dir, entry_path)
if os.path.exists(full_path):
try:
with open(full_path, 'r', encoding='utf-8', errors='replace') as ef:
entry_content = ef.read(4096)
entropy = _compute_entropy(entry_content)
# High entropy (>5.5) suggests obfuscation/minification with
# potential hidden payloads
if entropy > 5.5 and len(entry_content) > 500:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"Manifest Confusion: '{field_name}' points to high-entropy file",
description=(
f"Entry point '{entry_path}' has Shannon entropy {entropy:.2f} "
f"(threshold 5.5). May be obfuscated to hide malicious payload."
),
file=rel_path, line=0,
snippet=f"{field_name}: {entry_path} (entropy: {entropy:.2f})",
category="manifest-confusion"
))
except OSError:
pass
# Check bin entries for suspicious content
bin_field = data.get('bin', {})
if isinstance(bin_field, str):
bin_field = {data.get('name', 'unknown'): bin_field}
if isinstance(bin_field, dict):
for bin_name, bin_path in bin_field.items():
if not isinstance(bin_path, str):
continue
full_bin_path = os.path.join(pkg_dir, bin_path)
if os.path.exists(full_bin_path):
try:
with open(full_bin_path, 'r', encoding='utf-8', errors='replace') as bf:
bin_content = bf.read(4096)
if _check_suspicious_bin_content(bin_content):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title=f"Manifest Confusion: Suspicious bin entry '{bin_name}'",
description=(
f"Binary '{bin_path}' contains suspicious shell commands "
f"(curl/wget piped to shell). May be a supply chain attack vector."
),
file=rel_path, line=0,
snippet=f"bin[{bin_name}]: {bin_path}",
category="manifest-confusion"
))
except OSError:
pass
return findings
# ============================================================
# Lockfile Injection Detection (Item 3)
# Detects integrity issues in lockfiles that indicate tampering.
# ============================================================
def scan_lockfile_injection(filepath, rel_path):
"""Detect lockfile integrity issues that indicate injection or tampering.
Checks for:
- pnpm-lock.yaml entries with HTTP (not HTTPS) tarball URLs
- package-lock.json entries missing integrity field
- yarn.lock entries with resolved pointing to non-registry URLs
- Any lockfile entry where resolved URL domain doesn't match expected registry
"""
findings = []
basename = os.path.basename(filepath)
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
except (OSError, UnicodeDecodeError):
return findings
if basename == 'pnpm-lock.yaml':
# Check for HTTP tarball URLs in pnpm-lock.yaml
http_tarballs = re.findall(r'(?:resolution|tarball):\s*["\']?(http://[^\s"\']+\.tgz)["\']?', content)
for url in http_tarballs:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title="Lockfile Injection: HTTP tarball in pnpm-lock.yaml",
description=(
"pnpm-lock.yaml resolves a package via unencrypted HTTP. "
"This enables MITM attacks to inject malicious code at install time."
),
file=rel_path, line=0,
snippet=url[:120],
category="lockfile-injection"
))
elif basename == 'package-lock.json':
# Parse JSON and check for missing integrity fields
try:
data = json.load(open(filepath, 'r', encoding='utf-8'))
packages = data.get('packages', {})
lockfile_version = data.get('lockfileVersion', 1)
if lockfile_version >= 2:
for pkg_path, pkg_info in packages.items():
if not pkg_path or pkg_path == '':
continue
# Skip workspace links
if pkg_info.get('link'):
continue
# Check for missing integrity with a resolved URL
resolved = pkg_info.get('resolved', '')
if resolved and 'integrity' not in pkg_info:
pkg_name = pkg_path.split('node_modules/')[-1] if 'node_modules/' in pkg_path else pkg_path
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"Lockfile Injection: Missing integrity hash for '{pkg_name}'",
description=(
"Package has a resolved URL but no integrity (SHA) hash. "
"The lockfile may have been tampered with to point to a "
"different tarball without detection."
),
file=rel_path, line=0,
snippet=f"{pkg_name}: resolved={resolved[:80]}, no integrity",
category="lockfile-injection"
))
except (json.JSONDecodeError, OSError):
pass
elif basename == 'yarn.lock':
# Check for resolved URLs pointing to non-registry domains
resolved_urls = re.findall(r'resolved\s+"(https?://[^"]+)"', content)
for url in resolved_urls:
parsed = urlparse(url)
hostname = parsed.hostname or ''
is_trusted = any(
hostname == t or hostname.endswith('.' + t)
for t in TRUSTED_REGISTRIES
)
if not is_trusted and hostname:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="high",
title="Lockfile Injection: Non-registry resolved URL in yarn.lock",
description=(
f"yarn.lock resolves to '{hostname}' which is not a known "
f"package registry. May indicate a lockfile injection attack."
),
file=rel_path, line=0,
snippet=url[:120],
category="lockfile-injection"
))
return findings
# ============================================================
# Behavioral Scoring (Item 7)
# Simplified behavioral model flagging packages with suspicious
# signal combinations (network + fs writes, obfuscated install scripts,
# postinstall download-and-exec).
# ============================================================
def scan_behavioral_signals(filepath, rel_path):
"""Flag packages with suspicious behavioral signal combinations.
A simplified version of Socket.dev's behavioral model. Checks for:
- Package with both network access AND filesystem write in install scripts
- Obfuscated install scripts (high entropy)
- postinstall that downloads and executes something
"""
findings = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
except (json.JSONDecodeError, OSError):
return findings
if not isinstance(data, dict):
return findings
scripts = data.get('scripts', {})
if not isinstance(scripts, dict):
return findings
install_scripts = {}
for key in ('preinstall', 'install', 'postinstall', 'prepare'):
if key in scripts and isinstance(scripts[key], str):
install_scripts[key] = scripts[key]
if not install_scripts:
return findings
# Network patterns in install scripts
network_patterns = re.compile(
r'\b(curl|wget|fetch|http|https|axios|request|net\.connect|socket)\b'
)
# Filesystem write patterns in install scripts
fs_write_patterns = re.compile(
r'(\bwrite\b|\bwriteFile\b|\bwriteFileSync\b|\bfs\.open\b|\bopen\s*\(|[^-]>|>>|\bcp\s|\bmv\s|\binstall\s)'
)
# Download-and-execute patterns
download_exec_patterns = re.compile(
r'(curl|wget|fetch).*\|.*(sh|bash|node|python|exec|eval)|'
r'(curl|wget).*-[oO].*&&.*(sh|bash|node|chmod\s+\+x)'
)
for script_name, script_cmd in install_scripts.items():
has_network = bool(network_patterns.search(script_cmd))
has_fs_write = bool(fs_write_patterns.search(script_cmd))
# Signal 1: Both network AND filesystem write in install script
if has_network and has_fs_write:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"Behavioral Signal: '{script_name}' has network + filesystem access",
description=(
f"Install script '{script_name}' combines network access with "
f"filesystem writes. This signal combination is characteristic of "
f"supply chain malware (downloads then persists payload)."
),
file=rel_path, line=0,
snippet=f"{script_name}: {script_cmd[:100]}",
category="behavioral-signal"
))
# Signal 2: Obfuscated install script (high entropy)
if len(script_cmd) > 60:
entropy = _compute_entropy(script_cmd)
if entropy > 4.5:
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title=f"Behavioral Signal: '{script_name}' appears obfuscated",
description=(
f"Install script '{script_name}' has high entropy ({entropy:.2f}). "
f"Obfuscated install scripts are a strong indicator of malicious "
f"intent (hiding the true behavior from code review)."
),
file=rel_path, line=0,
snippet=f"{script_name}: entropy={entropy:.2f}, len={len(script_cmd)}",
category="behavioral-signal"
))
# Signal 3: postinstall that downloads and executes
if script_name == 'postinstall' and download_exec_patterns.search(script_cmd):
findings.append(core.Finding(
scanner=SCANNER_NAME, severity="medium",
title="Behavioral Signal: postinstall downloads and executes",
description=(
"postinstall script downloads content from the network and "
"immediately executes it. This is the #1 npm malware pattern "
"(Socket.dev 2025-2026 research)."
),
file=rel_path, line=0,
snippet=f"postinstall: {script_cmd[:100]}",
category="behavioral-signal"
))
return findings
def main():
# Follow the same pattern as scan_integrity.py: when a scanner needs
# extra CLI flags beyond those in core.parse_common_args, build a local
# argparse.ArgumentParser in main() with all flags (repo_path, --format,
# and scanner-specific). That makes --help list every flag and avoids
# hand-rolled argv pre-filtering.
import argparse
parser = argparse.ArgumentParser(description="repo-forensics: Dependency Scanner")
parser.add_argument('repo_path', help="Path to repository to scan")
parser.add_argument('--format', choices=['text', 'json', 'summary'],
default='text', help="Output format (default: text)")
parser.add_argument('--package-list', default=None, metavar='FILE',
help=("Absolute path to a user-supplied IOC file "
"(one 'package@version' or 'package@*' per "
"line, # comments allowed). File must be "
"outside the scanned repo, owned by current "
"user, not a symlink, and under 256KB."))
parser.add_argument('--no-vulns', action='store_true',
help="Skip OSV + CISA KEV vulnerability enrichment")
parser.add_argument('--offline', action='store_true',
help=("Skip all network fetches. Use only cached KEV/"
"OSV data if present. Implies no live updates."))
parser.add_argument('--update-vulns', action='store_true',
help="Refresh CISA KEV cache before scanning")
parser.add_argument('--skip-freshness', action='store_true',
help="Skip package freshness checks (publication age, maintainer changes)")
args = parser.parse_args()
_VULN_STATE["queried"] = set()
_VULN_STATE["kev"] = None
_VULN_STATE["enabled"] = not args.no_vulns
_VULN_STATE["offline"] = bool(args.offline)
_FRESHNESS_STATE["enabled"] = not args.skip_freshness and not args.no_vulns
_FRESHNESS_STATE["started_at"] = time.monotonic()
_FRESHNESS_STATE["offline"] = bool(args.offline)
_FRESHNESS_STATE["queried"] = set()
_FRESHNESS_STATE["query_count"] = 0
if args.update_vulns and not args.offline:
try:
import vuln_feed
ok, msg = vuln_feed.update_kev_cache()
core.emit_status(args.format, f"[*] {msg}")
except (ImportError, OSError) as e:
print(f"[!] KEV update failed: {e}", file=sys.stderr)
args.repo_path = os.path.abspath(args.repo_path)
repo_path = args.repo_path
# Apply --package-list before any scanning so the IOC db reflects user entries
if args.package_list:
try:
_merge_user_package_list_into_db(args.package_list, repo_path)
except ValueError as e:
print(f"[!] --package-list rejected: {e}", file=sys.stderr)
sys.exit(2)
core.emit_status(args.format, f"[*] Scanning dependencies in {repo_path}...")
ignore_patterns = core.load_ignore_patterns(repo_path)
all_findings = []
for file_path, rel_path in core.walk_repo(repo_path, ignore_patterns, skip_binary=True, skip_lockfiles=False):
basename = os.path.basename(file_path)
if basename == 'package.json':
all_findings.extend(scan_package_json(file_path, rel_path))
all_findings.extend(scan_manifest_confusion(file_path, rel_path))
all_findings.extend(scan_behavioral_signals(file_path, rel_path))
elif basename == 'package-lock.json':
all_findings.extend(scan_lockfile(file_path, rel_path))
all_findings.extend(parse_package_lock_json(file_path, rel_path))
all_findings.extend(scan_lockfile_injection(file_path, rel_path))
elif basename == 'yarn.lock':
all_findings.extend(scan_lockfile(file_path, rel_path))
all_findings.extend(parse_yarn_lock(file_path, rel_path))
all_findings.extend(scan_lockfile_injection(file_path, rel_path))
elif basename == 'pnpm-lock.yaml':
all_findings.extend(scan_lockfile(file_path, rel_path))
all_findings.extend(parse_pnpm_lock_file(file_path, rel_path))
all_findings.extend(scan_lockfile_injection(file_path, rel_path))
elif basename in ('bun.lock', 'deno.lock'):
# Text lockfiles without a dedicated parser: every resolved URL
# still goes through the registry, git and plain-HTTP checks.
all_findings.extend(scan_lockfile(file_path, rel_path))
elif basename in ('poetry.lock', 'uv.lock', 'pdm.lock'):
# uv.lock and pdm.lock use the same [[package]] name/version layout
all_findings.extend(parse_poetry_lock(file_path, rel_path))
elif basename == 'Pipfile.lock':
all_findings.extend(parse_pipfile_lock(file_path, rel_path))
elif basename in ('requirements.txt', 'requirements-dev.txt', 'requirements-test.txt',
'constraints.txt', 'pyproject.toml', 'Pipfile'):
all_findings.extend(scan_python_deps(file_path, rel_path))
elif basename == 'composer.json':
all_findings.extend(scan_composer_json(file_path, rel_path))
elif basename == 'composer.lock':
all_findings.extend(parse_composer_lock(file_path, rel_path))
all_findings.extend(scan_lockfile_injection(file_path, rel_path))
elif is_extra_requirements_file(file_path):
all_findings.extend(scan_pypi_pins_only(file_path, rel_path))
# Surface enrichment state (offline/no-vulns/rulepack-degraded) as
# a scanner-emitted finding, never as a cross-import from aggregate_json.
all_findings.extend(_enrichment_status_findings())
all_findings = _merge_freshness_cap_findings(all_findings)
# Surface vuln-feed cache freshness on each CVE finding so the aggregator
# can set enrichment_status.vulns without importing vuln_feed.
vuln_freshness = _vuln_cache_freshness_status()
if vuln_freshness:
for f in all_findings:
if f.category in ("cve", "cve-kev"):
f.freshness_status = vuln_freshness
core.output_findings(all_findings, args.format, SCANNER_NAME)
if __name__ == "__main__":
main()
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |