codefactory-cli 0.20.1__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.
Files changed (61) hide show
  1. codefactory/__init__.py +3 -0
  2. codefactory/__main__.py +5 -0
  3. codefactory/accept.py +68 -0
  4. codefactory/analyzers/__init__.py +55 -0
  5. codefactory/analyzers/generic.py +392 -0
  6. codefactory/analyzers/parallel.py +113 -0
  7. codefactory/analyzers/python.py +279 -0
  8. codefactory/analyzers/python_parse.py +402 -0
  9. codefactory/analyzers/treewalk.py +299 -0
  10. codefactory/analyzers/ts_helper.js +483 -0
  11. codefactory/analyzers/typescript.py +118 -0
  12. codefactory/apple.py +80 -0
  13. codefactory/audit.py +227 -0
  14. codefactory/baseline.py +58 -0
  15. codefactory/brief.py +330 -0
  16. codefactory/cache.py +93 -0
  17. codefactory/checks.py +608 -0
  18. codefactory/cli.py +782 -0
  19. codefactory/config.py +157 -0
  20. codefactory/coverage.py +104 -0
  21. codefactory/deps.py +182 -0
  22. codefactory/diff.py +197 -0
  23. codefactory/efficiency.py +250 -0
  24. codefactory/flags.py +263 -0
  25. codefactory/graph.py +95 -0
  26. codefactory/hotspots.py +344 -0
  27. codefactory/infra.py +242 -0
  28. codefactory/intent.py +106 -0
  29. codefactory/layers.py +143 -0
  30. codefactory/org.py +131 -0
  31. codefactory/owners.py +72 -0
  32. codefactory/patch.py +81 -0
  33. codefactory/paths.py +93 -0
  34. codefactory/plan.py +476 -0
  35. codefactory/privacy.py +221 -0
  36. codefactory/profile.py +667 -0
  37. codefactory/rerank.py +161 -0
  38. codefactory/review.py +1504 -0
  39. codefactory/rules.py +50 -0
  40. codefactory/sarif.py +661 -0
  41. codefactory/sbom.py +78 -0
  42. codefactory/security.py +537 -0
  43. codefactory/similar.py +199 -0
  44. codefactory/skill.py +144 -0
  45. codefactory/skill_template/SKILL.md +68 -0
  46. codefactory/skill_template/__init__.py +0 -0
  47. codefactory/sqlgraph.py +109 -0
  48. codefactory/suppress.py +92 -0
  49. codefactory/taint.py +439 -0
  50. codefactory/trace.py +72 -0
  51. codefactory/viewer/__init__.py +19 -0
  52. codefactory/viewer/dashboard.html +43 -0
  53. codefactory/viewer/factory.css +61 -0
  54. codefactory/viewer/factory.js +665 -0
  55. codefactory/viewer/template.html +47 -0
  56. codefactory_cli-0.20.1.dist-info/METADATA +250 -0
  57. codefactory_cli-0.20.1.dist-info/RECORD +61 -0
  58. codefactory_cli-0.20.1.dist-info/WHEEL +5 -0
  59. codefactory_cli-0.20.1.dist-info/entry_points.txt +2 -0
  60. codefactory_cli-0.20.1.dist-info/licenses/LICENSE +21 -0
  61. codefactory_cli-0.20.1.dist-info/top_level.txt +1 -0
codefactory/rerank.py ADDED
@@ -0,0 +1,161 @@
1
+ """Ranking findings for the project they're in: the same line means different things in a local-first app, a public
2
+ API and a command-line tool.
3
+
4
+ Each rule names the findings it applies to (by title), the projects it applies to (a test on the profile), the
5
+ severity they get, and why, in words a reader can check. A finding changed by a rule says so in the review, with the
6
+ old severity and the reason; nothing is hidden. Rules only move a finding one step at a time in the direction they
7
+ name: an upgrade never lowers a finding that's already above it, and a downgrade never raises one.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import re
13
+ from collections.abc import Callable
14
+ from dataclasses import dataclass
15
+
16
+ from .profile import Profile
17
+
18
+ SEV = ["Critical", "High", "Medium", "Low"]
19
+ HEAVY = ("health and fitness", "financial", "accounts and passwords") # data that law and app stores treat as special
20
+ WRITE_WORDS = re.compile(r"runAsync|execAsync|executemany|INSERT|UPDATE|DELETE|\b(?:save|write|insert|update|delete)\w*\(?")
21
+
22
+
23
+ @dataclass(frozen=True)
24
+ class Rule:
25
+ title: str # a regular expression matched against the finding's title
26
+ applies: Callable[[Profile], bool]
27
+ to: str # the severity it gets
28
+ why: Callable[[Profile], str]
29
+ evidence: str = "" # optionally, a regular expression one of its evidence lines must match
30
+
31
+
32
+ def _heavy(p: Profile) -> list[str]:
33
+ return [c for c in HEAVY if c in p.sensitive]
34
+
35
+
36
+ def _special(p: Profile) -> list[str]:
37
+ """Health and financial data: special under GDPR, HIPAA, PCI. An app that merely has passwords isn't."""
38
+ return [c for c in ("health and fitness", "financial") if c in p.sensitive]
39
+
40
+
41
+ def _data(p: Profile) -> str:
42
+ cats = _heavy(p)
43
+ return cats[0] if len(cats) == 1 else ", ".join(cats[:-1]) + " and " + cats[-1]
44
+
45
+
46
+ RULES = [
47
+ # ---- local-first, one user: no attacker on the other end of a request
48
+ Rule(
49
+ r"^SQL assembled from strings", lambda p: p.local_first and p.single_user and not p.server, "Low",
50
+ lambda p: "a local-first app with one user and no server: only what the user types or imports, or what the app "
51
+ "itself fetches, can reach these queries. Still worth binding, but not an attack surface.",
52
+ ),
53
+ Rule(
54
+ r"^Debug mode switched on in code", lambda p: p.deployment in ("user's machine", "local server"), "Low",
55
+ lambda p: f"it runs on the user's own machine ({p.deployment}): a debug page shows the code to the person who "
56
+ "already has it.",
57
+ ),
58
+ Rule(
59
+ r"^Request data reaches dangerous calls", lambda p: p.deployment == "local server", "High",
60
+ lambda p: "the server listens only on this machine: whoever can send it requests can already run code here. "
61
+ "It becomes critical the day it listens on a network.",
62
+ ),
63
+ Rule(
64
+ r"^(?:Shell commands built at runtime|Dynamic code execution)",
65
+ lambda p: p.kind in ("command-line tool", "data pipeline") and not p.server, "Medium",
66
+ lambda p: f"a {p.kind} runs with its user's own rights: this matters when it processes files or text "
67
+ "from someone else (downloads, other people's repositories), not for the user's own input.",
68
+ ),
69
+ Rule(
70
+ r"^Network calls without a timeout", lambda p: p.kind == "command-line tool" and not p.server, "Low",
71
+ lambda p: "a command-line tool has someone watching: a hang is seen and stopped. It matters more if it runs "
72
+ "unattended (cron, CI).",
73
+ ),
74
+ # ---- mobile: the platform's rules
75
+ Rule(
76
+ r"^Plain-HTTP URLs in code", lambda p: p.kind == "mobile app", "Medium",
77
+ lambda p: "iOS (App Transport Security) and Android 9+ block plain HTTP by default: these requests fail in "
78
+ "release builds unless an exception is configured, and with one they can be read on public Wi-Fi.",
79
+ ),
80
+ Rule(
81
+ r"^No error boundary around the UI", lambda p: p.kind == "mobile app", "High",
82
+ lambda p: "on a phone, an uncaught rendering error closes the screen or the app, and the user loses what they "
83
+ "were in the middle of.",
84
+ ),
85
+ Rule(
86
+ r"^App allows unencrypted HTTP", lambda p: bool(_heavy(p)), "High",
87
+ lambda p: f"the app holds {_data(p)} data, and an exception to encrypted traffic lets it travel in the clear.",
88
+ ),
89
+ # ---- special data: the law and the app stores ask for more
90
+ Rule(
91
+ r"^Personal data sent to analytics or crash reporting", lambda p: "health and fitness" in p.sensitive, "Critical",
92
+ lambda p: "the app holds health and fitness data: sending it to a third party can need explicit consent "
93
+ "(GDPR special category) and, in the US, can be a reportable breach (FTC Health Breach Notification Rule).",
94
+ ),
95
+ Rule(
96
+ r"^Personal data (?:written to logs|in unencrypted storage)", lambda p: bool(_special(p)), "High",
97
+ lambda p: f"the app holds {', '.join(_special(p))} data, which privacy law treats as a special category and the "
98
+ "app stores as needing the most care; logs and plain storage end up in backups, crash reports and support tickets.",
99
+ ),
100
+ Rule(
101
+ r"^Important code no test reaches", lambda p: p.local_first, "High",
102
+ lambda p: "local-first: the device holds the only copy of the user's data, so an untested write that goes wrong "
103
+ "can't be restored from a server.",
104
+ evidence=WRITE_WORDS.pattern,
105
+ ),
106
+ # ---- servers: who can reach them
107
+ Rule(
108
+ r"^Endpoints that change data without an auth check", lambda p: p.deployment == "deployed server", "High",
109
+ lambda p: "it's set up to be deployed (" + ", ".join(e.split(" (")[0] for e in p.deployment_evidence[:2]) + "), "
110
+ "so it runs where others can reach it, whatever address it listens on locally.",
111
+ ),
112
+ Rule(
113
+ r"^Any website may call this API \(CORS \*\)",
114
+ lambda p: p.deployment in ("local server", "LAN server") and p.open_writes > 0, "High",
115
+ lambda p: "a server on this machine or network that allows every origin can be driven by any web page the user "
116
+ f"opens, and {p.open_writes} of its endpoints change data without checking who's asking.",
117
+ ),
118
+ Rule(
119
+ r"^Session cookies without Secure / HttpOnly", lambda p: p.users == "multi-user" and p.reachable_by_others, "High",
120
+ lambda p: "it has user accounts and others can reach it: a session cookie read by a script or sent over plain "
121
+ "HTTP is someone's account.",
122
+ ),
123
+ ] # fmt: skip
124
+
125
+
126
+ @dataclass
127
+ class Change:
128
+ title: str
129
+ before: str
130
+ after: str
131
+ why: str
132
+
133
+
134
+ def rerank(findings: list, profile: Profile) -> list[Change]:
135
+ """Re-rank the review's findings in place for this profile; return what changed."""
136
+ changes = []
137
+ for f in findings:
138
+ for rule in RULES:
139
+ if not re.search(rule.title, f.title) or not rule.applies(profile):
140
+ continue
141
+ if rule.evidence and not any(re.search(rule.evidence, e) for e in f.evidence):
142
+ continue
143
+ before, after = f.severity, rule.to
144
+ if before == after:
145
+ continue
146
+ why = rule.why(profile)
147
+ f.severity = after
148
+ f.data["ranked"] = {"from": before, "why": why}
149
+ changes.append(Change(f.title, before, after, why))
150
+ break # one rule per finding: the first that fits is the most specific
151
+ return changes
152
+
153
+
154
+ def markdown(changes: list[Change]) -> list[str]:
155
+ if not changes:
156
+ return ["Nothing re-ranked: every finding keeps its default severity for this kind of project.", ""]
157
+ out = ["| Finding | Default | For this project | Why |", "|---|---|---|---|"]
158
+ for c in changes:
159
+ arrow = "↑" if SEV.index(c.after) < SEV.index(c.before) else "↓"
160
+ out.append(f"| {c.title} | {c.before} | {arrow} **{c.after}** | {c.why} |")
161
+ return out + [""]