predisclose 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- predisclose/__init__.py +3 -0
- predisclose/__main__.py +3 -0
- predisclose/checks/__init__.py +1 -0
- predisclose/checks/activity.py +66 -0
- predisclose/checks/fixes.py +215 -0
- predisclose/checks/known.py +79 -0
- predisclose/checks/route.py +238 -0
- predisclose/checks/similarity.py +63 -0
- predisclose/cli.py +267 -0
- predisclose/core.py +78 -0
- predisclose/ecosystems.py +101 -0
- predisclose/http.py +171 -0
- predisclose/ledger.py +170 -0
- predisclose/models.py +82 -0
- predisclose/output.py +92 -0
- predisclose/sources/__init__.py +1 -0
- predisclose/sources/advisory.py +69 -0
- predisclose/sources/ghsa.py +76 -0
- predisclose/sources/github.py +79 -0
- predisclose/sources/osv.py +69 -0
- predisclose/sources/registry.py +53 -0
- predisclose-0.1.0.dist-info/METADATA +163 -0
- predisclose-0.1.0.dist-info/RECORD +26 -0
- predisclose-0.1.0.dist-info/WHEEL +4 -0
- predisclose-0.1.0.dist-info/entry_points.txt +2 -0
- predisclose-0.1.0.dist-info/licenses/LICENSE +202 -0
predisclose/__init__.py
ADDED
predisclose/__main__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""The four checks. Each returns a CheckResult and never raises for missing data."""
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Check 3: has this area had advisories recently? Busy areas get more parallel reports."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import UTC, datetime, timedelta
|
|
6
|
+
|
|
7
|
+
from predisclose.checks.similarity import score
|
|
8
|
+
from predisclose.http import Http
|
|
9
|
+
from predisclose.models import CheckResult, Evidence, Finding
|
|
10
|
+
from predisclose.sources import ghsa, osv
|
|
11
|
+
from predisclose.sources.advisory import Advisory, merge
|
|
12
|
+
|
|
13
|
+
NAME = "recent_activity"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def check(http: Http, finding: Finding, *, days: int = 365, now: datetime | None = None) -> CheckResult:
|
|
17
|
+
if not (finding.package or finding.repo):
|
|
18
|
+
return CheckResult(NAME, "skipped", "none", "Need --pkg or --repo.")
|
|
19
|
+
if http.dry_run:
|
|
20
|
+
return CheckResult(NAME, "skipped", "none", "Dry run: nothing was sent.")
|
|
21
|
+
|
|
22
|
+
advisories: list[Advisory] = []
|
|
23
|
+
if finding.package and finding.ecosystem:
|
|
24
|
+
advisories = osv.query_package(http, finding.ecosystem, finding.package)
|
|
25
|
+
if finding.repo:
|
|
26
|
+
advisories += ghsa.repo_advisories(http, finding.repo)
|
|
27
|
+
advisories = merge(advisories)
|
|
28
|
+
|
|
29
|
+
cutoff = ((now or datetime.now(UTC)) - timedelta(days=days)).strftime("%Y-%m-%d")
|
|
30
|
+
recent = [a for a in advisories if (a.published or "")[:10] >= cutoff]
|
|
31
|
+
has_hints = bool(finding.cwe or finding.files or finding.functions)
|
|
32
|
+
|
|
33
|
+
evidence: list[Evidence] = []
|
|
34
|
+
similar = 0
|
|
35
|
+
for adv in recent:
|
|
36
|
+
level, reasons = score(adv, finding)
|
|
37
|
+
is_similar = has_hints and level in ("medium", "strong")
|
|
38
|
+
similar += is_similar
|
|
39
|
+
evidence.append(
|
|
40
|
+
Evidence(
|
|
41
|
+
source="+".join(sorted(adv.sources)),
|
|
42
|
+
id=adv.display_id(),
|
|
43
|
+
title=adv.title(),
|
|
44
|
+
url=adv.url,
|
|
45
|
+
date=(adv.published or "")[:10] or None,
|
|
46
|
+
level="medium" if is_similar else "info",
|
|
47
|
+
reasons=[r for r in reasons if r not in ("affects this version", "same project")]
|
|
48
|
+
or ["published recently"],
|
|
49
|
+
)
|
|
50
|
+
)
|
|
51
|
+
evidence.sort(key=lambda e: e.date or "", reverse=True)
|
|
52
|
+
|
|
53
|
+
if not recent:
|
|
54
|
+
return CheckResult(
|
|
55
|
+
NAME, "none", "none", f"No advisories published in the last {days} days."
|
|
56
|
+
)
|
|
57
|
+
summary = f"{len(recent)} advisories published in the last {days} days"
|
|
58
|
+
notes: list[str] = []
|
|
59
|
+
if similar:
|
|
60
|
+
summary += f"; {similar} look similar to your finding"
|
|
61
|
+
notes.append(
|
|
62
|
+
"Areas with recent similar advisories attract parallel reports. Check the "
|
|
63
|
+
"public records carefully before writing your report."
|
|
64
|
+
)
|
|
65
|
+
level = "medium" if similar else "info"
|
|
66
|
+
return CheckResult(NAME, "found", level, summary, evidence, notes)
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"""Check 2: has the code been changed publicly since the tested version?"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import posixpath
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
from predisclose.checks.similarity import score
|
|
9
|
+
from predisclose.http import Http
|
|
10
|
+
from predisclose.models import LEVELS, CheckResult, Evidence, Finding, max_level
|
|
11
|
+
from predisclose.sources import github
|
|
12
|
+
from predisclose.sources.advisory import Advisory
|
|
13
|
+
|
|
14
|
+
NAME = "public_fix"
|
|
15
|
+
|
|
16
|
+
SECURITY_TERMS = re.compile(
|
|
17
|
+
r"\b(security|vulnerab\w*|cve-\d{4}-\d+|ghsa-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}|exploit\w*|"
|
|
18
|
+
r"injection|xss|csrf|ssrf|xxe|traversal|overflow|sanitiz\w*|escap\w*|unsafe|bypass|"
|
|
19
|
+
r"denial of service|redos|leak\w*|privilege\w*|deserializ\w*|authentication|authorization)\b",
|
|
20
|
+
re.IGNORECASE,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
ALWAYS = "Re-test on the latest release yourself before reporting."
|
|
24
|
+
COMMIT_URL = re.compile(r"/commit/([0-9a-f]{7,40})", re.IGNORECASE)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def known_fix_commits(advisories: list[Advisory]) -> dict[str, str]:
|
|
28
|
+
"""Commit sha (lowercase, any length >= 7) -> advisory id, from public advisory references."""
|
|
29
|
+
out: dict[str, str] = {}
|
|
30
|
+
for adv in advisories:
|
|
31
|
+
for url in adv.fix_refs:
|
|
32
|
+
m = COMMIT_URL.search(url)
|
|
33
|
+
if m:
|
|
34
|
+
out.setdefault(m.group(1).lower(), adv.display_id())
|
|
35
|
+
return out
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def referenced_fixes(finding: Finding, advisories: list[Advisory], seen: set[str]) -> list[Evidence]:
|
|
39
|
+
"""Fix commits named by advisories that resemble the finding (they may sit on a release branch)."""
|
|
40
|
+
out: list[Evidence] = []
|
|
41
|
+
for adv in advisories:
|
|
42
|
+
level, _ = score(adv, finding)
|
|
43
|
+
if level not in ("medium", "strong"):
|
|
44
|
+
continue
|
|
45
|
+
for url in adv.fix_refs:
|
|
46
|
+
m = COMMIT_URL.search(url)
|
|
47
|
+
if not m:
|
|
48
|
+
continue
|
|
49
|
+
sha = m.group(1).lower()
|
|
50
|
+
if any(s.startswith(sha) or sha.startswith(s) for s in seen):
|
|
51
|
+
continue
|
|
52
|
+
seen.add(sha)
|
|
53
|
+
out.append(
|
|
54
|
+
Evidence(
|
|
55
|
+
source="advisory",
|
|
56
|
+
id=sha[:12],
|
|
57
|
+
title=f"Fix commit referenced by {adv.display_id()}",
|
|
58
|
+
url=url,
|
|
59
|
+
level="medium",
|
|
60
|
+
reasons=[f"public fix for {adv.display_id()}", "compare it with the code you tested"],
|
|
61
|
+
)
|
|
62
|
+
)
|
|
63
|
+
return out
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _advisory_for(sha: str, fixes: dict[str, str]) -> str | None:
|
|
67
|
+
s = sha.lower()
|
|
68
|
+
for known_sha, adv_id in fixes.items():
|
|
69
|
+
if s.startswith(known_sha) or known_sha.startswith(s[: len(known_sha)]):
|
|
70
|
+
return adv_id
|
|
71
|
+
return None
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _first_line(text: str) -> str:
|
|
75
|
+
return (text or "").strip().split("\n", 1)[0][:140]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def resolve_paths(http: Http, repo: str, refs: list[str], path: str) -> tuple[list[str], list[str]]:
|
|
79
|
+
"""
|
|
80
|
+
Map what the user typed (a full path, a partial path, or a bare file name) to real paths
|
|
81
|
+
at the tested version and on the default branch, so moved files are still followed.
|
|
82
|
+
Returns (paths, notes).
|
|
83
|
+
"""
|
|
84
|
+
wanted = path.strip().strip("/")
|
|
85
|
+
found: list[str] = []
|
|
86
|
+
notes: list[str] = []
|
|
87
|
+
for ref in refs:
|
|
88
|
+
listing = github.tree_paths(http, repo, ref)
|
|
89
|
+
if listing is None:
|
|
90
|
+
continue
|
|
91
|
+
paths, truncated = listing
|
|
92
|
+
if truncated:
|
|
93
|
+
notes.append(f"File list at {ref} is truncated; pass the full path to be exact.")
|
|
94
|
+
exact = [p for p in paths if p == wanted]
|
|
95
|
+
suffix = [p for p in paths if p.endswith("/" + wanted)]
|
|
96
|
+
same_name = [p for p in paths if posixpath.basename(p) == posixpath.basename(wanted)]
|
|
97
|
+
for p in (exact or suffix or same_name)[:5]:
|
|
98
|
+
if p not in found:
|
|
99
|
+
found.append(p)
|
|
100
|
+
if not found:
|
|
101
|
+
found = [wanted]
|
|
102
|
+
notes.append(f"{wanted}: not found in the file list; searching history for it as typed.")
|
|
103
|
+
elif len(found) > 1:
|
|
104
|
+
notes.append(f"{wanted}: following {', '.join(found)}.")
|
|
105
|
+
return found, notes
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def check(http: Http, finding: Finding, advisories: list[Advisory] | None = None) -> CheckResult:
|
|
109
|
+
if http.dry_run:
|
|
110
|
+
return CheckResult(NAME, "skipped", "none", "Dry run: nothing was sent.")
|
|
111
|
+
if not finding.repo:
|
|
112
|
+
return CheckResult(NAME, "skipped", "none", "Need a repository (--repo, or a package we can map to one).")
|
|
113
|
+
if not finding.version:
|
|
114
|
+
return CheckResult(NAME, "skipped", "none", "Need --version to know where to start.")
|
|
115
|
+
|
|
116
|
+
resolved = github.resolve_version(http, finding.repo, finding.version, finding.package)
|
|
117
|
+
if not resolved:
|
|
118
|
+
return CheckResult(
|
|
119
|
+
NAME,
|
|
120
|
+
"skipped",
|
|
121
|
+
"none",
|
|
122
|
+
f"No tag or commit {finding.version!r} found in {finding.repo}.",
|
|
123
|
+
notes=["Pass the exact tag or commit sha with --version.", ALWAYS],
|
|
124
|
+
)
|
|
125
|
+
ref, sha, since = resolved
|
|
126
|
+
fixes = known_fix_commits(advisories or [])
|
|
127
|
+
evidence: list[Evidence] = []
|
|
128
|
+
notes = [ALWAYS, "Only commits on the default branch are listed."]
|
|
129
|
+
|
|
130
|
+
if finding.files:
|
|
131
|
+
by_sha: dict[str, Evidence] = {}
|
|
132
|
+
for typed in finding.files:
|
|
133
|
+
paths, path_notes = resolve_paths(http, finding.repo, [sha, "HEAD"], typed)
|
|
134
|
+
notes += path_notes
|
|
135
|
+
for path in paths:
|
|
136
|
+
commits = github.commits_since(http, finding.repo, since, path=path)
|
|
137
|
+
if len(commits) >= 100:
|
|
138
|
+
notes.append(f"{path}: showing the latest 100 commits only.")
|
|
139
|
+
for c in commits:
|
|
140
|
+
csha = str(c.get("sha", ""))
|
|
141
|
+
if csha == sha:
|
|
142
|
+
continue
|
|
143
|
+
if csha in by_sha:
|
|
144
|
+
reason = f"changed {path}"
|
|
145
|
+
if reason not in by_sha[csha].reasons:
|
|
146
|
+
by_sha[csha].reasons.insert(0, reason)
|
|
147
|
+
continue
|
|
148
|
+
message = (c.get("commit") or {}).get("message") or ""
|
|
149
|
+
hit = SECURITY_TERMS.search(message)
|
|
150
|
+
linked = _advisory_for(csha, fixes)
|
|
151
|
+
committed = ((c.get("commit") or {}).get("committer") or {}).get("date") or ""
|
|
152
|
+
reasons = [f"changed {path}"]
|
|
153
|
+
if linked:
|
|
154
|
+
reasons.append(f"public fix for {linked}")
|
|
155
|
+
if hit:
|
|
156
|
+
reasons.append(f"message mentions '{hit.group(0)}'")
|
|
157
|
+
by_sha[csha] = Evidence(
|
|
158
|
+
source="github",
|
|
159
|
+
id=csha[:12],
|
|
160
|
+
title=_first_line(message),
|
|
161
|
+
url=str(c.get("html_url") or ""),
|
|
162
|
+
date=committed[:10] or None,
|
|
163
|
+
level="medium" if (hit or linked) else "info",
|
|
164
|
+
reasons=reasons,
|
|
165
|
+
)
|
|
166
|
+
evidence = list(by_sha.values())
|
|
167
|
+
evidence += referenced_fixes(finding, advisories or [], seen=set(by_sha))
|
|
168
|
+
flagged = sum(e.level == "medium" for e in evidence)
|
|
169
|
+
linked_count = sum(any(r.startswith("public fix for") for r in e.reasons) for e in evidence)
|
|
170
|
+
if not evidence:
|
|
171
|
+
return CheckResult(
|
|
172
|
+
NAME,
|
|
173
|
+
"none",
|
|
174
|
+
"none",
|
|
175
|
+
f"No commits changed {', '.join(finding.files)} since {ref} ({since[:10]}).",
|
|
176
|
+
notes=notes,
|
|
177
|
+
)
|
|
178
|
+
summary = (
|
|
179
|
+
f"{len(evidence)} commits changed {', '.join(finding.files)} since {ref} ({since[:10]}); "
|
|
180
|
+
f"{flagged} look security-related"
|
|
181
|
+
+ (f" ({linked_count} linked to public advisories)" if linked_count else "")
|
|
182
|
+
)
|
|
183
|
+
else:
|
|
184
|
+
for rel in github.releases(http, finding.repo):
|
|
185
|
+
published = str(rel.get("published_at") or "")
|
|
186
|
+
if not published or published <= since:
|
|
187
|
+
continue
|
|
188
|
+
body = f"{rel.get('name') or ''}\n{rel.get('body') or ''}"
|
|
189
|
+
hit = SECURITY_TERMS.search(body)
|
|
190
|
+
if not hit:
|
|
191
|
+
continue
|
|
192
|
+
evidence.append(
|
|
193
|
+
Evidence(
|
|
194
|
+
source="github",
|
|
195
|
+
id=str(rel.get("tag_name") or ""),
|
|
196
|
+
title=_first_line(str(rel.get("name") or rel.get("tag_name") or "")),
|
|
197
|
+
url=str(rel.get("html_url") or ""),
|
|
198
|
+
date=published[:10],
|
|
199
|
+
level="medium",
|
|
200
|
+
reasons=[f"release notes mention '{hit.group(0)}'"],
|
|
201
|
+
)
|
|
202
|
+
)
|
|
203
|
+
notes.append("Add --file to list commits that touched the affected code.")
|
|
204
|
+
if not evidence:
|
|
205
|
+
return CheckResult(
|
|
206
|
+
NAME,
|
|
207
|
+
"none",
|
|
208
|
+
"none",
|
|
209
|
+
f"No release since {ref} ({since[:10]}) mentions security terms.",
|
|
210
|
+
notes=notes,
|
|
211
|
+
)
|
|
212
|
+
summary = f"{len(evidence)} releases since {ref} ({since[:10]}) mention security terms"
|
|
213
|
+
|
|
214
|
+
evidence.sort(key=lambda e: (LEVELS.index(e.level), e.date or ""), reverse=True)
|
|
215
|
+
return CheckResult(NAME, "found", max_level([e.level for e in evidence]), summary, evidence, notes)
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Check 1: is there already a public record of this issue?"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from predisclose.checks.similarity import score
|
|
6
|
+
from predisclose.ecosystems import GHSA_ECOSYSTEM
|
|
7
|
+
from predisclose.http import Http
|
|
8
|
+
from predisclose.models import LEVELS, CheckResult, Evidence, Finding, max_level
|
|
9
|
+
from predisclose.sources import ghsa, github, osv
|
|
10
|
+
from predisclose.sources.advisory import Advisory, merge
|
|
11
|
+
|
|
12
|
+
NAME = "public_record"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def gather(http: Http, finding: Finding) -> tuple[list[Advisory], list[str]]:
|
|
16
|
+
"""Collect advisories that affect the target. Returns (advisories, notes)."""
|
|
17
|
+
notes: list[str] = []
|
|
18
|
+
found: list[Advisory] = []
|
|
19
|
+
if finding.package and finding.ecosystem:
|
|
20
|
+
found += osv.query_package(http, finding.ecosystem, finding.package, finding.version)
|
|
21
|
+
ghsa_eco = GHSA_ECOSYSTEM.get(finding.ecosystem)
|
|
22
|
+
if ghsa_eco:
|
|
23
|
+
found += ghsa.query_affects(http, ghsa_eco, finding.package, finding.version)
|
|
24
|
+
if not finding.version:
|
|
25
|
+
notes.append("No version given: showing every advisory for the package.")
|
|
26
|
+
elif finding.repo:
|
|
27
|
+
if finding.version:
|
|
28
|
+
resolved = github.resolve_version(http, finding.repo, finding.version)
|
|
29
|
+
if resolved:
|
|
30
|
+
found += osv.query_commit(http, resolved[1])
|
|
31
|
+
else:
|
|
32
|
+
notes.append(f"Could not find tag or commit {finding.version!r} in {finding.repo}.")
|
|
33
|
+
found += ghsa.repo_advisories(http, finding.repo)
|
|
34
|
+
notes.append("Repository advisories are not filtered by version.")
|
|
35
|
+
return merge(found), notes
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def check(http: Http, finding: Finding, advisories: list[Advisory], notes: list[str]) -> CheckResult:
|
|
39
|
+
if not (finding.package or finding.repo):
|
|
40
|
+
return CheckResult(NAME, "skipped", "none", "Need --pkg or --repo.")
|
|
41
|
+
if http.dry_run:
|
|
42
|
+
return CheckResult(NAME, "skipped", "none", "Dry run: nothing was sent.")
|
|
43
|
+
|
|
44
|
+
evidence: list[Evidence] = []
|
|
45
|
+
for adv in advisories:
|
|
46
|
+
level, reasons = score(adv, finding)
|
|
47
|
+
evidence.append(
|
|
48
|
+
Evidence(
|
|
49
|
+
source="+".join(sorted(adv.sources)),
|
|
50
|
+
id=adv.display_id(),
|
|
51
|
+
title=adv.title(),
|
|
52
|
+
url=adv.url,
|
|
53
|
+
date=(adv.published or "")[:10] or None,
|
|
54
|
+
level=level,
|
|
55
|
+
reasons=reasons,
|
|
56
|
+
)
|
|
57
|
+
)
|
|
58
|
+
evidence.sort(key=lambda e: (LEVELS.index(e.level), e.date or ""), reverse=True)
|
|
59
|
+
|
|
60
|
+
if not evidence:
|
|
61
|
+
return CheckResult(
|
|
62
|
+
NAME,
|
|
63
|
+
"none",
|
|
64
|
+
"none",
|
|
65
|
+
f"No public advisory found for {finding.target()}.",
|
|
66
|
+
notes=notes,
|
|
67
|
+
)
|
|
68
|
+
strong = sum(e.level == "strong" for e in evidence)
|
|
69
|
+
medium = sum(e.level == "medium" for e in evidence)
|
|
70
|
+
summary = f"{len(evidence)} public advisories affect {finding.target()}"
|
|
71
|
+
if finding.cwe or finding.files or finding.functions or finding.keywords:
|
|
72
|
+
summary += f"; {strong} strong and {medium} medium matches"
|
|
73
|
+
if strong:
|
|
74
|
+
summary += " - likely already known"
|
|
75
|
+
else:
|
|
76
|
+
summary += "; add --cwe/--file/--function to rank them"
|
|
77
|
+
return CheckResult(
|
|
78
|
+
NAME, "found", max_level([e.level for e in evidence]), summary, evidence, notes
|
|
79
|
+
)
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"""Check 4: where does this project want security reports?"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from urllib.parse import urlparse
|
|
8
|
+
|
|
9
|
+
from predisclose.http import Http
|
|
10
|
+
from predisclose.models import CheckResult, Evidence
|
|
11
|
+
from predisclose.sources import github
|
|
12
|
+
|
|
13
|
+
NAME = "where_to_report"
|
|
14
|
+
|
|
15
|
+
POLICY_PATHS = ("SECURITY.md", ".github/SECURITY.md", "docs/SECURITY.md")
|
|
16
|
+
EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+")
|
|
17
|
+
MD_LINK = re.compile(r"\[([^\]]+)\]\((https?://[^)\s]+)\)")
|
|
18
|
+
BARE_URL = re.compile(r"(?<![(\[])https?://[^\s)>\]\"'`]+")
|
|
19
|
+
GITHUB_REPORTING = re.compile(
|
|
20
|
+
r"security advisor(y|ies)|advisories/new|private vulnerability report|privately report|"
|
|
21
|
+
r"report a vulnerability|security tab|\"security\" at the top",
|
|
22
|
+
re.IGNORECASE,
|
|
23
|
+
)
|
|
24
|
+
PREFERRED = re.compile(r"preferred|preferably|primary|please use|recommended way", re.IGNORECASE)
|
|
25
|
+
FALLBACK = re.compile(
|
|
26
|
+
r"escalat|if you do not (receive|hear)|no (response|acknowledg)|fallback|alternatively|otherwise|\bcna\b",
|
|
27
|
+
re.IGNORECASE,
|
|
28
|
+
)
|
|
29
|
+
REPORT_WORDS = re.compile(r"report|disclos|vulnerab|security|bounty|contact|send|e-?mail", re.IGNORECASE)
|
|
30
|
+
STRONG_WORDS = re.compile(r"report|disclos|vulnerab|bounty", re.IGNORECASE)
|
|
31
|
+
SECURITY_LOCAL = re.compile(r"secur|vuln|psirt|cert|disclos|bounty", re.IGNORECASE)
|
|
32
|
+
NOT_A_CHANNEL = re.compile(r"copyright|\(c\)|©|spdx|license|licence|all rights reserved", re.IGNORECASE)
|
|
33
|
+
PLATFORMS = {
|
|
34
|
+
"hackerone": "HackerOne",
|
|
35
|
+
"bugcrowd": "Bugcrowd",
|
|
36
|
+
"intigriti": "Intigriti",
|
|
37
|
+
"huntr": "huntr",
|
|
38
|
+
"yeswehack": "YesWeHack",
|
|
39
|
+
"immunefi": "Immunefi",
|
|
40
|
+
"tidelift": "Tidelift",
|
|
41
|
+
}
|
|
42
|
+
IGNORED_EMAIL_DOMAINS = ("example.com", "example.org", "users.noreply.github.com")
|
|
43
|
+
IGNORED_URL_HOSTS = ("img.shields.io", "badge", "spdx.org", "opensource.org", "creativecommons.org")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class Channel:
|
|
48
|
+
kind: str # github | platform | email | link
|
|
49
|
+
value: str
|
|
50
|
+
score: int
|
|
51
|
+
position: int
|
|
52
|
+
context: str
|
|
53
|
+
|
|
54
|
+
def describe(self) -> str:
|
|
55
|
+
return {
|
|
56
|
+
"github": f"GitHub private vulnerability reporting -> {self.value}",
|
|
57
|
+
"platform": f"report through {self.value}",
|
|
58
|
+
"email": f"email {self.value}",
|
|
59
|
+
"link": f"follow {self.value}",
|
|
60
|
+
}[self.kind]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
KIND_ORDER = {"github": 0, "platform": 1, "email": 2, "link": 3}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def find_policy(http: Http, repo: str) -> tuple[str, str] | None:
|
|
67
|
+
"""SECURITY.md in the repository, else the organization's .github repository."""
|
|
68
|
+
for path in POLICY_PATHS:
|
|
69
|
+
text = github.raw_file(http, repo, path)
|
|
70
|
+
if text:
|
|
71
|
+
return f"https://github.com/{repo}/blob/HEAD/{path}", text
|
|
72
|
+
owner = repo.split("/", 1)[0]
|
|
73
|
+
for path in ("SECURITY.md", ".github/SECURITY.md"):
|
|
74
|
+
text = github.raw_file(http, f"{owner}/.github", path)
|
|
75
|
+
if text:
|
|
76
|
+
return f"https://github.com/{owner}/.github/blob/HEAD/{path}", text
|
|
77
|
+
return None
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def parse_policy(text: str, repo: str, pvr: bool | None) -> list[Channel]:
|
|
81
|
+
"""
|
|
82
|
+
Every reporting channel a SECURITY.md mentions, best first.
|
|
83
|
+
Order of preference: a channel the policy marks as preferred, then the strongest kind of
|
|
84
|
+
channel, then the one mentioned first. Escalation-only contacts rank low.
|
|
85
|
+
"""
|
|
86
|
+
channels: dict[tuple[str, str], Channel] = {}
|
|
87
|
+
pvr_url = f"https://github.com/{repo}/security/advisories/new"
|
|
88
|
+
heading = ""
|
|
89
|
+
|
|
90
|
+
def add(kind: str, value: str, base: int, pos: int, line: str) -> None:
|
|
91
|
+
score = base
|
|
92
|
+
if PREFERRED.search(line) or PREFERRED.search(heading):
|
|
93
|
+
score += 5
|
|
94
|
+
if STRONG_WORDS.search(heading):
|
|
95
|
+
score += 1
|
|
96
|
+
if FALLBACK.search(line):
|
|
97
|
+
score -= 3
|
|
98
|
+
key = (kind, value.lower())
|
|
99
|
+
if key not in channels or channels[key].score < score:
|
|
100
|
+
channels[key] = Channel(kind, value, score, pos, line.strip()[:160])
|
|
101
|
+
|
|
102
|
+
for pos, line in enumerate(text.splitlines()):
|
|
103
|
+
if line.lstrip().startswith("#"):
|
|
104
|
+
heading = line
|
|
105
|
+
if NOT_A_CHANNEL.search(line):
|
|
106
|
+
continue
|
|
107
|
+
relevant_line = bool(REPORT_WORDS.search(line) or REPORT_WORDS.search(heading))
|
|
108
|
+
if GITHUB_REPORTING.search(line):
|
|
109
|
+
add("github", pvr_url, 4 if pvr else 1, pos, line)
|
|
110
|
+
for label, url in MD_LINK.findall(line):
|
|
111
|
+
host = urlparse(url).netloc.lower()
|
|
112
|
+
if any(h in host or h in url.lower() for h in IGNORED_URL_HOSTS):
|
|
113
|
+
continue
|
|
114
|
+
platform = next((name for key, name in PLATFORMS.items() if key in host), None)
|
|
115
|
+
if platform:
|
|
116
|
+
add("platform", f"{platform} ({url})", 4, pos, line)
|
|
117
|
+
elif "/security/advisories" in url or GITHUB_REPORTING.search(label):
|
|
118
|
+
add("github", pvr_url, 4 if pvr else 1, pos, line)
|
|
119
|
+
else:
|
|
120
|
+
if STRONG_WORDS.search(label) or STRONG_WORDS.search(url):
|
|
121
|
+
base = 4
|
|
122
|
+
elif REPORT_WORDS.search(label) or REPORT_WORDS.search(url):
|
|
123
|
+
base = 3
|
|
124
|
+
else:
|
|
125
|
+
base = 2 if relevant_line else 0
|
|
126
|
+
add("link", url, base, pos, line)
|
|
127
|
+
for url in BARE_URL.findall(line):
|
|
128
|
+
url = url.rstrip(".,;:")
|
|
129
|
+
host = urlparse(url).netloc.lower()
|
|
130
|
+
if any(h in host for h in IGNORED_URL_HOSTS):
|
|
131
|
+
continue
|
|
132
|
+
platform = next((name for key, name in PLATFORMS.items() if key in host), None)
|
|
133
|
+
if platform:
|
|
134
|
+
add("platform", f"{platform} ({url})", 4, pos, line)
|
|
135
|
+
else:
|
|
136
|
+
if STRONG_WORDS.search(url):
|
|
137
|
+
base = 4
|
|
138
|
+
elif REPORT_WORDS.search(url):
|
|
139
|
+
base = 3
|
|
140
|
+
else:
|
|
141
|
+
base = 2 if relevant_line else 0
|
|
142
|
+
add("link", url, base, pos, line)
|
|
143
|
+
for email in EMAIL.findall(line):
|
|
144
|
+
if any(email.lower().endswith(d) for d in IGNORED_EMAIL_DOMAINS):
|
|
145
|
+
continue
|
|
146
|
+
local = email.split("@", 1)[0]
|
|
147
|
+
add("email", email, 3 + (1 if SECURITY_LOCAL.search(local) else 0), pos, line)
|
|
148
|
+
lower = line.lower()
|
|
149
|
+
for key, name in PLATFORMS.items():
|
|
150
|
+
if key in lower and not any(c.kind == "platform" and name in c.value for c in channels.values()):
|
|
151
|
+
add("platform", name, 3, pos, line)
|
|
152
|
+
|
|
153
|
+
ranked = [c for c in channels.values() if c.score > 0]
|
|
154
|
+
ranked.sort(key=lambda c: (-c.score, c.position, KIND_ORDER[c.kind]))
|
|
155
|
+
return ranked
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def security_txt_contacts(http: Http, homepage: str) -> tuple[str, list[str]] | None:
|
|
159
|
+
parsed = urlparse(homepage)
|
|
160
|
+
if parsed.scheme not in ("http", "https") or not parsed.netloc or "github.com" in parsed.netloc:
|
|
161
|
+
return None
|
|
162
|
+
url = f"https://{parsed.netloc}/.well-known/security.txt"
|
|
163
|
+
status, text = http.get_text(url)
|
|
164
|
+
if status != 200 or not text or "contact:" not in text.lower():
|
|
165
|
+
return None
|
|
166
|
+
contacts = [
|
|
167
|
+
line.split(":", 1)[1].strip()
|
|
168
|
+
for line in text.splitlines()
|
|
169
|
+
if line.lower().startswith("contact:")
|
|
170
|
+
]
|
|
171
|
+
return (url, contacts) if contacts else None
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def check(http: Http, repo: str | None) -> CheckResult:
|
|
175
|
+
if http.dry_run:
|
|
176
|
+
return CheckResult(NAME, "skipped", "none", "Dry run: nothing was sent.")
|
|
177
|
+
if not repo:
|
|
178
|
+
return CheckResult(NAME, "skipped", "none", "Need a repository (--repo, or a package we can map to one).")
|
|
179
|
+
|
|
180
|
+
info = github.repo_info(http, repo)
|
|
181
|
+
if info is None:
|
|
182
|
+
return CheckResult(NAME, "error", "none", f"Repository {repo} not found or not public.")
|
|
183
|
+
pvr = github.private_reporting_enabled(http, repo)
|
|
184
|
+
pvr_url = f"https://github.com/{repo}/security/advisories/new"
|
|
185
|
+
policy = find_policy(http, repo)
|
|
186
|
+
homepage = str(info.get("homepage") or "")
|
|
187
|
+
sectxt = security_txt_contacts(http, homepage) if homepage else None
|
|
188
|
+
|
|
189
|
+
evidence: list[Evidence] = []
|
|
190
|
+
notes: list[str] = []
|
|
191
|
+
|
|
192
|
+
if policy:
|
|
193
|
+
url, text = policy
|
|
194
|
+
channels = parse_policy(text, repo, pvr)
|
|
195
|
+
evidence.append(Evidence("security.md", "SECURITY.md", "Security policy", url, level="info"))
|
|
196
|
+
if channels:
|
|
197
|
+
summary = "Follow SECURITY.md: " + channels[0].describe()
|
|
198
|
+
for c in channels[1:4]:
|
|
199
|
+
evidence.append(
|
|
200
|
+
Evidence("security.md", c.kind, "also mentioned: " + c.describe(), url, level="info")
|
|
201
|
+
)
|
|
202
|
+
elif pvr:
|
|
203
|
+
summary = f"Read SECURITY.md; private reporting is also enabled -> {pvr_url}"
|
|
204
|
+
else:
|
|
205
|
+
summary = f"Read SECURITY.md for instructions -> {url}"
|
|
206
|
+
if any(c.kind == "github" for c in channels) and pvr is False:
|
|
207
|
+
notes.append("SECURITY.md mentions GitHub reporting, but private reporting is turned off.")
|
|
208
|
+
status = "found"
|
|
209
|
+
elif pvr:
|
|
210
|
+
summary = f"GitHub private vulnerability reporting -> {pvr_url}"
|
|
211
|
+
status = "found"
|
|
212
|
+
elif sectxt:
|
|
213
|
+
summary = "security.txt contact: " + ", ".join(sectxt[1][:3])
|
|
214
|
+
status = "found"
|
|
215
|
+
else:
|
|
216
|
+
summary = (
|
|
217
|
+
"No channel in the standard places (SECURITY.md, GitHub private reporting, security.txt). "
|
|
218
|
+
"Check the project's documentation; if there is none, ask the maintainers for a private "
|
|
219
|
+
"contact without sharing any details of the issue."
|
|
220
|
+
)
|
|
221
|
+
status = "none"
|
|
222
|
+
|
|
223
|
+
if pvr is not None:
|
|
224
|
+
evidence.append(
|
|
225
|
+
Evidence(
|
|
226
|
+
"github",
|
|
227
|
+
"private-reporting",
|
|
228
|
+
"GitHub private vulnerability reporting: " + ("on" if pvr else "off"),
|
|
229
|
+
pvr_url if pvr else f"https://github.com/{repo}/security",
|
|
230
|
+
level="info",
|
|
231
|
+
)
|
|
232
|
+
)
|
|
233
|
+
if sectxt:
|
|
234
|
+
evidence.append(Evidence("security.txt", "security.txt", ", ".join(sectxt[1][:3]), sectxt[0], level="info"))
|
|
235
|
+
if info.get("archived"):
|
|
236
|
+
notes.append("The repository is archived; maintainers may not respond.")
|
|
237
|
+
notes.append("GitHub-hosted projects can request a CVE through a GitHub security advisory.")
|
|
238
|
+
return CheckResult(NAME, status, "info", summary, evidence, notes)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""How closely an advisory resembles the user's finding. Deterministic and explainable."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import posixpath
|
|
6
|
+
|
|
7
|
+
from predisclose.models import Finding
|
|
8
|
+
from predisclose.sources.advisory import Advisory
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _mentions_file(text: str, path: str) -> bool:
|
|
12
|
+
p = path.strip().lower()
|
|
13
|
+
if not p:
|
|
14
|
+
return False
|
|
15
|
+
if p in text:
|
|
16
|
+
return True
|
|
17
|
+
base = posixpath.basename(p)
|
|
18
|
+
# A bare basename counts only when it is specific enough (has an extension, not tiny).
|
|
19
|
+
return "." in base and len(base) >= 5 and base in text
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _mentions_function(text: str, functions: set[str], name: str) -> bool:
|
|
23
|
+
n = name.strip().lower()
|
|
24
|
+
if not n:
|
|
25
|
+
return False
|
|
26
|
+
for f in functions:
|
|
27
|
+
fl = f.lower()
|
|
28
|
+
if fl == n or fl.endswith("." + n) or fl.endswith("::" + n):
|
|
29
|
+
return True
|
|
30
|
+
return len(n) >= 4 and n in text
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def score(adv: Advisory, finding: Finding) -> tuple[str, list[str]]:
|
|
34
|
+
text = f"{adv.summary}\n{adv.details}".lower()
|
|
35
|
+
reasons: list[str] = []
|
|
36
|
+
|
|
37
|
+
cwe_hit = sorted({c.upper() for c in finding.cwe} & adv.cwes)
|
|
38
|
+
file_hit = [f for f in finding.files if _mentions_file(text, f)]
|
|
39
|
+
func_hit = [fn for fn in finding.functions if _mentions_function(text, adv.functions, fn)]
|
|
40
|
+
kw_hit = [k for k in finding.keywords if len(k.strip()) >= 3 and k.strip().lower() in text]
|
|
41
|
+
|
|
42
|
+
if cwe_hit:
|
|
43
|
+
reasons.append("same CWE: " + ", ".join(cwe_hit))
|
|
44
|
+
if file_hit:
|
|
45
|
+
reasons.append("mentions file: " + ", ".join(file_hit))
|
|
46
|
+
if func_hit:
|
|
47
|
+
reasons.append("mentions function: " + ", ".join(func_hit))
|
|
48
|
+
if kw_hit:
|
|
49
|
+
reasons.append("mentions keyword: " + ", ".join(kw_hit))
|
|
50
|
+
|
|
51
|
+
same_place = bool(file_hit or func_hit)
|
|
52
|
+
if (cwe_hit and same_place) or (file_hit and func_hit):
|
|
53
|
+
level = "strong"
|
|
54
|
+
elif cwe_hit or same_place or kw_hit:
|
|
55
|
+
level = "medium"
|
|
56
|
+
else:
|
|
57
|
+
level = "weak"
|
|
58
|
+
reasons.append("affects this version" if adv.version_checked else "same project")
|
|
59
|
+
|
|
60
|
+
if not adv.version_checked and level == "strong":
|
|
61
|
+
level = "medium"
|
|
62
|
+
reasons.append("version not checked")
|
|
63
|
+
return level, reasons
|