codewatch-scan 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.
- codewatch/__init__.py +0 -0
- codewatch/__main__.py +14 -0
- codewatch/asgi.py +16 -0
- codewatch/settings.py +175 -0
- codewatch/urls.py +40 -0
- codewatch/wsgi.py +16 -0
- codewatch_scan-0.1.0.dist-info/METADATA +81 -0
- codewatch_scan-0.1.0.dist-info/RECORD +136 -0
- codewatch_scan-0.1.0.dist-info/WHEEL +5 -0
- codewatch_scan-0.1.0.dist-info/entry_points.txt +4 -0
- codewatch_scan-0.1.0.dist-info/licenses/LICENSE +21 -0
- codewatch_scan-0.1.0.dist-info/top_level.txt +2 -0
- core/__init__.py +0 -0
- core/analyzers/__init__.py +19 -0
- core/analyzers/accessibility.py +199 -0
- core/analyzers/base.py +192 -0
- core/analyzers/body_models.py +154 -0
- core/analyzers/codewatch_checks.py +394 -0
- core/analyzers/constant_branches.py +251 -0
- core/analyzers/containers.py +316 -0
- core/analyzers/coverage.py +131 -0
- core/analyzers/dependencies.py +347 -0
- core/analyzers/drf_serializers.py +65 -0
- core/analyzers/else_branches.py +106 -0
- core/analyzers/extra.py +400 -0
- core/analyzers/flask_resources.py +65 -0
- core/analyzers/inline_scripts.py +50 -0
- core/analyzers/jinja_sandboxes.py +63 -0
- core/analyzers/match_statements.py +148 -0
- core/analyzers/multi_tools.py +888 -0
- core/analyzers/php_template_sinks.py +444 -0
- core/analyzers/project_checks.py +150 -0
- core/analyzers/python_tools.py +333 -0
- core/analyzers/route_converters.py +237 -0
- core/analyzers/slices.py +94 -0
- core/analyzers/starred_targets.py +42 -0
- core/analyzers/stored_fields.py +386 -0
- core/analyzers/summaries.py +1387 -0
- core/analyzers/template_sinks.py +162 -0
- core/api.py +360 -0
- core/apps.py +6 -0
- core/benchmark.py +955 -0
- core/bundled/catalog/codewatch/rules/javascript-crypto.yml +224 -0
- core/bundled/catalog/codewatch/rules/javascript-injection.yml +116 -0
- core/bundled/catalog/codewatch/rules/javascript-taint.yml +1539 -0
- core/bundled/catalog/codewatch/rules/javascript-web.yml +562 -0
- core/bundled/catalog/codewatch/rules/php-web.yml +719 -0
- core/bundled/catalog/codewatch/rules/python-crypto.yml +203 -0
- core/bundled/catalog/codewatch/rules/python-django.yml +267 -0
- core/bundled/catalog/codewatch/rules/python-injection.yml +115 -0
- core/bundled/catalog/codewatch/rules/python-web.yml +3382 -0
- core/bundled/catalog/custom/mappings.json +360 -0
- core/bundled/catalog/custom/overrides.json +4 -0
- core/bundled/catalog/custom/rules/django-settings.yml +19 -0
- core/bundled/catalog/imported/changes.json +5 -0
- core/bundled/catalog/imported/docs.json +1143 -0
- core/bundled/catalog/imported/meta.json +54 -0
- core/bundled/catalog/imported/rules.json +85421 -0
- core/bundled/catalog/imported/standards.json +11191 -0
- core/bundled/tools/analyzers.lock +786 -0
- core/bundled/tools/axe-scan.mjs +45 -0
- core/bundled/tools/checkov.lock +2463 -0
- core/bundled/tools/codewatch-checks.mjs +99 -0
- core/bundled/tools/eslint-a11y.config.js +16 -0
- core/bundled/tools/eslint-quality.config.js +63 -0
- core/bundled/tools/eslint-typed.config.js +18 -0
- core/bundled/tools/package-lock.json +6102 -0
- core/bundled/tools/package.json +32 -0
- core/bundled/tools/rule-metadata.mjs +56 -0
- core/bundled/tools/semgrep.lock +1232 -0
- core/bundled/tools/stylelint.config.json +11 -0
- core/catalog/__init__.py +190 -0
- core/catalog/build.py +84 -0
- core/catalog/merge.py +122 -0
- core/catalog/sources.py +418 -0
- core/catalog/standards.py +119 -0
- core/catalog/update.py +99 -0
- core/catalog_views.py +115 -0
- core/client.py +645 -0
- core/collect.py +518 -0
- core/duplicates.py +60 -0
- core/fingerprint.py +25 -0
- core/folders.py +73 -0
- core/gate.py +77 -0
- core/git.py +149 -0
- core/jobs.py +86 -0
- core/management/__init__.py +0 -0
- core/management/commands/__init__.py +0 -0
- core/management/commands/addproject.py +40 -0
- core/management/commands/buildcatalog.py +15 -0
- core/management/commands/scan.py +74 -0
- core/management/commands/updatecatalog.py +14 -0
- core/mcp_server.py +140 -0
- core/middleware.py +17 -0
- core/migrations/0001_initial.py +115 -0
- core/migrations/0002_auth_tokens_a11y.py +60 -0
- core/migrations/0003_default_admin.py +43 -0
- core/migrations/0004_project_members.py +28 -0
- core/migrations/0005_uploads_and_sources.py +55 -0
- core/migrations/0006_thresholds.py +28 -0
- core/migrations/0007_also_reported_by.py +18 -0
- core/migrations/0008_new_code_period.py +18 -0
- core/migrations/0009_resolution_safe.py +18 -0
- core/migrations/0010_analysis_progress.py +18 -0
- core/migrations/0011_overall_gate_conditions.py +20 -0
- core/migrations/__init__.py +0 -0
- core/models.py +323 -0
- core/network.py +41 -0
- core/paths.py +29 -0
- core/ratings.py +67 -0
- core/runner.py +168 -0
- core/templates/core/account.html +30 -0
- core/templates/core/analysis.html +49 -0
- core/templates/core/base.html +121 -0
- core/templates/core/form_table.html +12 -0
- core/templates/core/gate_badge.html +1 -0
- core/templates/core/index.html +26 -0
- core/templates/core/issues.html +38 -0
- core/templates/core/login.html +13 -0
- core/templates/core/new_project.html +27 -0
- core/templates/core/password_change.html +10 -0
- core/templates/core/project.html +40 -0
- core/templates/core/rating.html +1 -0
- core/templates/core/ratings.html +32 -0
- core/templates/core/rule.html +33 -0
- core/templates/core/rules.html +42 -0
- core/templates/core/scan_progress.html +2 -0
- core/templates/core/settings.html +36 -0
- core/templates/core/source.html +15 -0
- core/templates/core/standards.html +30 -0
- core/templates/core/user_edit.html +15 -0
- core/templates/core/users.html +18 -0
- core/templatetags/__init__.py +0 -0
- core/templatetags/codewatch.py +42 -0
- core/tool_setup.py +68 -0
- core/views.py +564 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""Rewrite Python `match` statements as `if` blocks for Semgrep.
|
|
2
|
+
|
|
3
|
+
Semgrep OSS parses `match` but silently skips the rest of the function after one, so findings below it are lost.
|
|
4
|
+
In the copy it scans (never run; every line keeps its place) `match x:` becomes `if True:` and each `case p:`
|
|
5
|
+
becomes `if x == (p):`, which keeps every case reachable and the data flow through them.
|
|
6
|
+
"""
|
|
7
|
+
import ast
|
|
8
|
+
import re
|
|
9
|
+
|
|
10
|
+
CASE_RE = re.compile(r"^(\s*)case\b")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def expression(text):
|
|
14
|
+
try:
|
|
15
|
+
ast.parse(text, mode="eval")
|
|
16
|
+
except SyntaxError:
|
|
17
|
+
return False
|
|
18
|
+
return True
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def colon_after(line, col):
|
|
22
|
+
"""Index of the first ":" at or after col that ends the header (the rest is blank or a comment)."""
|
|
23
|
+
i = line.find(":", col)
|
|
24
|
+
while i != -1:
|
|
25
|
+
if not line[i + 1:].strip() or line[i + 1:].lstrip().startswith("#"):
|
|
26
|
+
return i
|
|
27
|
+
i = line.find(":", i + 1)
|
|
28
|
+
return -1
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def case_line(lines, case):
|
|
32
|
+
"""(row, match) of the `case` keyword's line: the pattern's own line or one of the two before it, since
|
|
33
|
+
`case (` can stand alone with the pattern on the next line; None if not found."""
|
|
34
|
+
first = case.pattern.lineno
|
|
35
|
+
while first > 1 and not CASE_RE.match(lines[first - 1]) and lines[first - 1].strip() not in ("", ")"):
|
|
36
|
+
first -= 1
|
|
37
|
+
if case.pattern.lineno - first > 2:
|
|
38
|
+
return None
|
|
39
|
+
m = CASE_RE.match(lines[first - 1])
|
|
40
|
+
return (first, m) if m else None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def header_colon(lines, case):
|
|
44
|
+
"""(row, column) of the colon that ends the case header: the first after the pattern (or guard), on a later
|
|
45
|
+
line if a bracket the pattern sits in closes outside the pattern's own span; None if there is none."""
|
|
46
|
+
after = case.guard or case.pattern
|
|
47
|
+
last, end = after.end_lineno, colon_after(lines[after.end_lineno - 1], after.end_col_offset)
|
|
48
|
+
while end == -1 and last < len(lines) and last < case.body[0].lineno - 1:
|
|
49
|
+
last += 1
|
|
50
|
+
end = colon_after(lines[last - 1], 0)
|
|
51
|
+
return None if end == -1 else (last, end)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def header_parses(pattern_text, guarded):
|
|
55
|
+
"""The header text between `case` and the colon is a pattern (and ` if ` guard) Python reads as expressions."""
|
|
56
|
+
if guarded:
|
|
57
|
+
guard_start = pattern_text.rfind(" if ")
|
|
58
|
+
if guard_start == -1:
|
|
59
|
+
return False
|
|
60
|
+
pattern_text, guard = pattern_text[:guard_start], pattern_text[guard_start + 4:]
|
|
61
|
+
if not expression(f"({guard})"):
|
|
62
|
+
return False
|
|
63
|
+
return bool(expression(f"({pattern_text})"))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def closed_last_line(tail, end, guarded):
|
|
67
|
+
"""The header's last line with the pattern's bracket closed: before the guard's `if`, or before the colon."""
|
|
68
|
+
if not guarded:
|
|
69
|
+
return f"{tail[:end]}){tail[end:]}"
|
|
70
|
+
cut = tail.rfind(" if ", 0, end)
|
|
71
|
+
return None if cut == -1 else f"{tail[:cut]}) and ({tail[cut + 4:end]}){tail[end:]}"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def multiline_case_header(lines, case, subject):
|
|
75
|
+
"""{row: new line} for a case whose pattern spans lines (LightRAG GHSA-v9w6-9hq9-33ch:
|
|
76
|
+
`case (\n ".txt"\n | ".md"\n ):`): `case` becomes `if subject == (` on the first line and `)` closes it before
|
|
77
|
+
the colon on the last, so the lines between stay as they are, inside the brackets."""
|
|
78
|
+
start, colon = case_line(lines, case), header_colon(lines, case)
|
|
79
|
+
if start is None or colon is None or colon[0] == start[0]:
|
|
80
|
+
return None
|
|
81
|
+
(first, m), (last, end) = start, colon
|
|
82
|
+
head = lines[first - 1][len(m.group(0)):]
|
|
83
|
+
if not header_parses("".join([head, *lines[first:last - 1], lines[last - 1][:end]]), case.guard is not None):
|
|
84
|
+
return None
|
|
85
|
+
closed = closed_last_line(lines[last - 1], end, case.guard is not None)
|
|
86
|
+
return None if closed is None else {first: f"{m.group(1)}if {subject} == ({head}", last: closed}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def case_header(source, lines, case, subject):
|
|
90
|
+
if case.pattern.lineno != case.pattern.end_lineno or (case.guard and case.guard.end_lineno != case.pattern.lineno):
|
|
91
|
+
return None
|
|
92
|
+
pattern = ast.get_source_segment(source, case.pattern)
|
|
93
|
+
if pattern is None or not expression(pattern):
|
|
94
|
+
return None
|
|
95
|
+
row = case.pattern.lineno
|
|
96
|
+
line = lines[row - 1]
|
|
97
|
+
if not (m := CASE_RE.match(line)):
|
|
98
|
+
return None
|
|
99
|
+
end = colon_after(line, len(m.group(0)))
|
|
100
|
+
if end == -1:
|
|
101
|
+
return None
|
|
102
|
+
condition = f"{subject} == ({pattern})"
|
|
103
|
+
if case.guard:
|
|
104
|
+
condition += f" and ({ast.get_source_segment(source, case.guard)})"
|
|
105
|
+
return row, f"{m.group(1)}if {condition}{line[end:]}"
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def match_edits(source, lines, node):
|
|
109
|
+
"""{row: new line} for one match statement, or {} if any part of it can't be rewritten."""
|
|
110
|
+
subject = ast.get_source_segment(source, node.subject)
|
|
111
|
+
line = lines[node.lineno - 1]
|
|
112
|
+
head = re.match(r"^(\s*)match\b", line)
|
|
113
|
+
if subject is None or node.subject.lineno != node.lineno or node.subject.end_lineno != node.lineno or not head:
|
|
114
|
+
return {}
|
|
115
|
+
end = colon_after(line, len(head.group(0)))
|
|
116
|
+
if end == -1:
|
|
117
|
+
return {}
|
|
118
|
+
edits = {node.lineno: f"{head.group(1)}if True{line[end:]}"}
|
|
119
|
+
for case in node.cases:
|
|
120
|
+
header = case_header(source, lines, case, f"({subject})")
|
|
121
|
+
if header is not None:
|
|
122
|
+
edits[header[0]] = header[1]
|
|
123
|
+
continue
|
|
124
|
+
multiline = multiline_case_header(lines, case, f"({subject})")
|
|
125
|
+
if not multiline:
|
|
126
|
+
return {}
|
|
127
|
+
edits.update(multiline)
|
|
128
|
+
return edits
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def match_as_if(source):
|
|
132
|
+
"""The source with match statements rewritten, or None if it has none (or doesn't parse)."""
|
|
133
|
+
if "match" not in source or "case" not in source:
|
|
134
|
+
return None
|
|
135
|
+
try:
|
|
136
|
+
tree = ast.parse(source)
|
|
137
|
+
except (SyntaxError, ValueError):
|
|
138
|
+
return None
|
|
139
|
+
lines = source.splitlines(keepends=True)
|
|
140
|
+
edits = {}
|
|
141
|
+
for node in ast.walk(tree):
|
|
142
|
+
if isinstance(node, ast.Match):
|
|
143
|
+
edits.update(match_edits(source, lines, node))
|
|
144
|
+
if not edits:
|
|
145
|
+
return None
|
|
146
|
+
for row, text in edits.items():
|
|
147
|
+
lines[row - 1] = text
|
|
148
|
+
return "".join(lines)
|