Repository navigation
Show how hidden complexity becomes real-world bugs: security pitfalls, vulnerability data and charts #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Show how hidden complexity becomes real-world bugs: security pitfalls, vulnerability data and charts #3
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e4ced36
Add dependency risk analysis of PyPI packages
f1fba0f
Tie pitfalls to real-world vulnerability data
369e9bf
Address review: retry failed fetches, PEP 691 yanks, stale roots
2d66084
Address review: Scorecard fetch recovers from a failed graph fetch
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Dependency Risk Analysis | ||
|
|
||
| Scripts behind [docs/dependency-risk.md](../docs/dependency-risk.md). They download public data, join it, and print every number used in the report. | ||
|
|
||
| `dependency_risk.csv` is the joined snapshot from 30 October 2026: one row per package that at least 5 of the top 1,000 PyPI packages depend on, with reach, advisory counts, yanked releases and Scorecard. | ||
|
|
||
| ## Reproduce | ||
|
|
||
| Downloaded data is untrusted, so keep it in its own directory and run the scripts from here with `python -I`. Network scripts need Python 3.11+ with working CA certificates. `fetch_yanked.py` also needs `packaging`, and `charts.py` needs `matplotlib`. | ||
|
|
||
| ```sh | ||
| DATA=/tmp/pypi-risk-data | ||
| mkdir -p $DATA/osv $DATA/top $DATA/deps $DATA/simple $DATA/scorecard | ||
|
|
||
| curl -L -o $DATA/osv/all.zip https://osv-vulnerabilities.storage.googleapis.com/PyPI/all.zip | ||
| unzip -q $DATA/osv/all.zip -d $DATA/osv/json | ||
| curl -L -o $DATA/top/top30d.json https://hugovk.github.io/top-pypi-packages/top-pypi-packages.min.json | ||
|
|
||
| python -I osv_agg.py $DATA # Merge advisories into unique vulnerabilities | ||
| python -I fetch_deps.py $DATA 1000 # Resolved dependency graphs from deps.dev | ||
| python -I fetch_yanked.py $DATA 1000 # Yanked releases from PyPI (needs packaging) | ||
| python -I analyze.py $DATA # Reach x vulnerabilities x yanks | ||
| python -I fetch_scorecard.py $DATA # OpenSSF Scorecard for the hidden layer | ||
| python -I report.py $DATA # Every table in the report | ||
| python -I charts.py $DATA ../docs/img # Light and dark SVG charts (needs matplotlib) | ||
| ``` | ||
|
|
||
| The fetch scripts cache one JSON file per package, so they can be rerun after a failure. Expect about five minutes in total. Results will drift as OSV, PyPI and deps.dev update. | ||
|
|
||
| | Script | Output | | ||
| |---|---| | ||
| | `osv_agg.py` | `vulns.json`: unique vulnerabilities (aliases merged) and malicious-package counts | | ||
| | `fetch_deps.py` | `deps/*.json`: resolved graph per top package, falling back to a recent release if deps.dev hasn't resolved the newest | | ||
| | `fetch_yanked.py` | `simple/*.json`: release count and yanked versions with reasons | | ||
| | `analyze.py` | `joined.json`: per-package reach, direct and indirect counts, advisories, yanks | | ||
| | `fetch_scorecard.py` | `scorecard/*.json`: Scorecard for packages with reach ≥ 20 and mostly indirect use | | ||
| | `report.py` | Prints the hidden-layer, burstiness, CWE, yanked, malicious-package, monthly and Scorecard tables | | ||
| | `charts.py` | `docs/img/*-{light,dark}.svg`: advisories per year, reach vs advisories, weakness mix. Colors are a palette validated for color-vision deficiency in both themes | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """Join vulnerabilities, dependency reach, downloads and yanked releases.""" | ||
| import collections, glob, json, os, re, sys | ||
|
|
||
| data = sys.argv[1] | ||
| norm = lambda n: re.sub(r"[-_.]+", "-", n).lower() | ||
| top = json.load(open(f"{data}/top/top30d.json"))["rows"] | ||
| downloads = {norm(r["project"]): r["download_count"] for r in top} | ||
| rank = {norm(r["project"]): i + 1 for i, r in enumerate(top)} | ||
| roots = {norm(r["project"]) for r in top[:1000]} | ||
|
|
||
| # Reach: how many top-1000 packages pull each package in, directly or only indirectly. | ||
| direct, indirect, nodes_per_root = collections.defaultdict(set), collections.defaultdict(set), [] | ||
| for f in glob.glob(f"{data}/deps/*.json"): | ||
| d = json.load(open(f)) | ||
| if "graph" not in d: | ||
| continue | ||
| root = norm(os.path.basename(f)[:-5]) | ||
| if root not in roots: # Ignore graphs cached for packages no longer in the top 1,000. | ||
| continue | ||
| nodes = d["graph"]["nodes"] | ||
| nodes_per_root.append(len(nodes) - 1) | ||
| for node in nodes: | ||
| name = norm(node["versionKey"]["name"]) | ||
| if node["relation"] == "DIRECT": | ||
| direct[name].add(root) | ||
| elif node["relation"] == "INDIRECT": | ||
| indirect[name].add(root) | ||
| reach = {p: direct[p] | indirect[p] for p in set(direct) | set(indirect)} | ||
| only_indirect = {p: reach[p] - direct[p] for p in reach} | ||
|
|
||
| v = json.load(open(f"{data}/vulns.json")) | ||
| vulns, malicious = v["vulns"], v["malicious"] | ||
| by_pkg = collections.defaultdict(list) | ||
| for x in vulns: | ||
| for p in x["pkgs"]: | ||
| by_pkg[p].append(x) | ||
|
|
||
| yanked = {} | ||
| for f in glob.glob(f"{data}/simple/*.json"): | ||
| d = json.load(open(f)) | ||
| if "error" not in d: | ||
| yanked[norm(os.path.basename(f)[:-5])] = d | ||
|
|
||
| def row(p): | ||
| vs = by_pkg.get(p, []) | ||
| recent = [x for x in vs if x["published"] >= "2023-01-01"] | ||
| y = yanked.get(p, {}) | ||
| return dict( | ||
| pkg=p, rank=rank.get(p), downloads=downloads.get(p, 0), | ||
| reach=len(reach.get(p, ())), direct=len(direct.get(p, ())), only_indirect=len(only_indirect.get(p, ())), | ||
| vulns=len(vs), vulns_2023=len(recent), | ||
| high_crit=sum(1 for x in vs if x["sev"] >= 3), | ||
| cwes=collections.Counter(c for x in vs for c in x["cwes"]).most_common(3), | ||
| yanked=len(y.get("yanked", [])), versions=y.get("n_versions"), | ||
| ) | ||
|
|
||
| rows = [row(p) for p in reach] | ||
| json.dump({"rows": rows, "roots_with_graph": len(nodes_per_root), | ||
| "median_deps": sorted(nodes_per_root)[len(nodes_per_root) // 2], | ||
| "mean_deps": sum(nodes_per_root) / len(nodes_per_root)}, open(f"{data}/joined.json", "w")) | ||
|
|
||
| print(f"roots with graphs: {len(nodes_per_root)}, median transitive deps {sorted(nodes_per_root)[len(nodes_per_root)//2]}, mean {sum(nodes_per_root)/len(nodes_per_root):.1f}") | ||
| fmt = "{pkg:22} rank={rank!s:>5} reach={reach:>4} direct={direct:>4} only_ind={only_indirect:>4} vulns={vulns:>3} since23={vulns_2023:>3} hi/crit={high_crit:>3} yanked={yanked:>3}/{versions!s:<4} {cwes}" | ||
| print("\n== Dependencies with reach >= 10, by all-time vulns") | ||
| for r in sorted((r for r in rows if r["reach"] >= 10 and r["vulns"]), key=lambda r: -r["vulns"])[:30]: | ||
| print(fmt.format(**r)) | ||
| print("\n== Exposure (vulns since 2023 x reach), reach >= 10") | ||
| for r in sorted((r for r in rows if r["reach"] >= 10 and r["vulns_2023"]), key=lambda r: -r["vulns_2023"] * r["reach"])[:20]: | ||
| print(fmt.format(**r), "score=", r["vulns_2023"] * r["reach"]) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.