codeveto 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.
- codeveto/__init__.py +3 -0
- codeveto/__main__.py +8 -0
- codeveto/ai/__init__.py +19 -0
- codeveto/ai/harness.py +303 -0
- codeveto/ai/packets.py +855 -0
- codeveto/ai/policy.py +222 -0
- codeveto/ai/prompt.py +101 -0
- codeveto/ai/providers/__init__.py +178 -0
- codeveto/ai/providers/claude.py +247 -0
- codeveto/ai/providers/openai_compat.py +246 -0
- codeveto/ai/runner.py +293 -0
- codeveto/ai/verify.py +324 -0
- codeveto/anchor.py +211 -0
- codeveto/cli.py +247 -0
- codeveto/cli_explain.py +49 -0
- codeveto/cli_hook.py +31 -0
- codeveto/cli_init.py +254 -0
- codeveto/cli_mcp.py +29 -0
- codeveto/cli_packets.py +35 -0
- codeveto/cli_publish.py +29 -0
- codeveto/cli_review.py +362 -0
- codeveto/cli_rules.py +78 -0
- codeveto/cli_verify.py +47 -0
- codeveto/config.py +1229 -0
- codeveto/data/README.md +16 -0
- codeveto/data/ai-wire.schema.json +53 -0
- codeveto/data/prompts/system.md +57 -0
- codeveto/data/report-v1.schema.json +268 -0
- codeveto/data/templates/forgejo-codeveto.yml +104 -0
- codeveto/data/templates/github-codeveto.yml +18 -0
- codeveto/embedded.py +311 -0
- codeveto/engine.py +1570 -0
- codeveto/git.py +1549 -0
- codeveto/globs.py +348 -0
- codeveto/integrations/__init__.py +1 -0
- codeveto/integrations/hooks.py +327 -0
- codeveto/integrations/mcp.py +770 -0
- codeveto/integrations/mcp_ai.py +77 -0
- codeveto/integrations/prepush.py +222 -0
- codeveto/jsonsafe.py +60 -0
- codeveto/languages.py +539 -0
- codeveto/lexers.py +2560 -0
- codeveto/model.py +372 -0
- codeveto/publish/__init__.py +28 -0
- codeveto/publish/action.py +330 -0
- codeveto/publish/common.py +84 -0
- codeveto/publish/diff.py +208 -0
- codeveto/publish/event.py +241 -0
- codeveto/publish/forgejo.py +184 -0
- codeveto/publish/github.py +380 -0
- codeveto/publish/http.py +263 -0
- codeveto/publish/pull.py +218 -0
- codeveto/publish/untrusted.py +110 -0
- codeveto/py.typed +0 -0
- codeveto/redact.py +113 -0
- codeveto/render/__init__.py +64 -0
- codeveto/render/agent.py +43 -0
- codeveto/render/github.py +101 -0
- codeveto/render/json.py +23 -0
- codeveto/render/markdown.py +208 -0
- codeveto/render/safe.py +249 -0
- codeveto/render/sarif.py +217 -0
- codeveto/render/text.py +210 -0
- codeveto/report.py +252 -0
- codeveto/rules/__init__.py +84 -0
- codeveto/rules/agent.py +2972 -0
- codeveto/rules/base.py +1026 -0
- codeveto/rules/ci.py +1708 -0
- codeveto/rules/config.py +493 -0
- codeveto/rules/deps.py +1693 -0
- codeveto/rules/docker.py +479 -0
- codeveto/rules/examples/__init__.py +148 -0
- codeveto/rules/examples/agent.py +839 -0
- codeveto/rules/examples/ci.py +1046 -0
- codeveto/rules/examples/config.py +191 -0
- codeveto/rules/examples/deps.py +369 -0
- codeveto/rules/examples/docker.py +157 -0
- codeveto/rules/examples/go.py +241 -0
- codeveto/rules/examples/hygiene.py +466 -0
- codeveto/rules/examples/js.py +462 -0
- codeveto/rules/examples/python.py +595 -0
- codeveto/rules/examples/rust.py +268 -0
- codeveto/rules/examples/secrets.py +561 -0
- codeveto/rules/examples/security.py +608 -0
- codeveto/rules/examples/shell.py +174 -0
- codeveto/rules/examples/sql.py +194 -0
- codeveto/rules/examples/tests.py +497 -0
- codeveto/rules/go.py +328 -0
- codeveto/rules/hygiene.py +1094 -0
- codeveto/rules/js.py +768 -0
- codeveto/rules/python.py +1642 -0
- codeveto/rules/rust.py +209 -0
- codeveto/rules/secrets.py +1335 -0
- codeveto/rules/security.py +3680 -0
- codeveto/rules/shell.py +867 -0
- codeveto/rules/sql.py +629 -0
- codeveto/rules/tests.py +1404 -0
- codeveto/schema.py +384 -0
- codeveto/statements.py +607 -0
- codeveto/worktree.py +1176 -0
- codeveto/yamlish.py +1179 -0
- codeveto-0.1.0.dist-info/METADATA +253 -0
- codeveto-0.1.0.dist-info/RECORD +106 -0
- codeveto-0.1.0.dist-info/WHEEL +4 -0
- codeveto-0.1.0.dist-info/entry_points.txt +2 -0
- codeveto-0.1.0.dist-info/licenses/LICENSE +21 -0
codeveto/rules/go.py
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"""Go pack (SPEC 6.1, 6.5): language bug patterns.
|
|
2
|
+
|
|
3
|
+
Every rule reads ``masked_code``, so strings, raw strings and comments that
|
|
4
|
+
only mention a construct never match. Block structure comes from a single
|
|
5
|
+
brace scan over the masked text (:func:`_scan`): each ``{`` is classified by
|
|
6
|
+
the header before it as a loop (``for``), a function (``func``, literal or
|
|
7
|
+
declaration) or any other block.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import re
|
|
13
|
+
from collections.abc import Iterator
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
|
|
16
|
+
from codeveto.rules.base import FileContext, Hit, rule
|
|
17
|
+
|
|
18
|
+
_PACKAGE = re.compile(r"^[ \t]*package[ \t]+([A-Za-z_]\w{0,100})")
|
|
19
|
+
_FOR = re.compile(r"^\s{0,200}(?:[A-Za-z_]\w{0,100}[ \t]*:\s{0,200})?for\b")
|
|
20
|
+
_FUNC_WORD = re.compile(r"(?<![\w.])func\b")
|
|
21
|
+
_FUNC_NAME = re.compile(r"^\s{0,200}func[ \t]*(?:\([^)\n]{0,300}\)[ \t]*)?([A-Za-z_]\w{0,100})")
|
|
22
|
+
_DEFER = re.compile(r"(?<![\w.])defer\b")
|
|
23
|
+
_PANIC = re.compile(r"(?<![\w.])panic[ \t]*\(")
|
|
24
|
+
_PRINT = re.compile(
|
|
25
|
+
r"(?<![\w.])(?:fmt\.Print(?:ln|f)?|println|print|spew\.(?:Dump|Printf|Println))[ \t]*\("
|
|
26
|
+
)
|
|
27
|
+
_IGNORED = re.compile(
|
|
28
|
+
r"^[ \t]*(?:[A-Za-z_]\w{0,100}[ \t]*,[ \t]*){0,8}_[ \t]*:?=[ \t]*"
|
|
29
|
+
r"([A-Za-z_][\w.]{0,200})[ \t]*\("
|
|
30
|
+
)
|
|
31
|
+
_TOKENS = {"defer": _DEFER, "panic": _PANIC}
|
|
32
|
+
|
|
33
|
+
# Calls whose dropped last result is a bool, a size or a value, not an error.
|
|
34
|
+
_NOT_ERRORS = frozenset(
|
|
35
|
+
{
|
|
36
|
+
"Load",
|
|
37
|
+
"LoadOrStore",
|
|
38
|
+
"LoadAndDelete",
|
|
39
|
+
"Swap",
|
|
40
|
+
"CompareAndSwap",
|
|
41
|
+
"LookupEnv",
|
|
42
|
+
"Cut",
|
|
43
|
+
"CutPrefix",
|
|
44
|
+
"CutSuffix",
|
|
45
|
+
"DecodeRune",
|
|
46
|
+
"DecodeRuneInString",
|
|
47
|
+
"DecodeLastRune",
|
|
48
|
+
"DecodeLastRuneInString",
|
|
49
|
+
"Peek",
|
|
50
|
+
"Get",
|
|
51
|
+
"Lookup",
|
|
52
|
+
"Value",
|
|
53
|
+
"Deadline",
|
|
54
|
+
"Caller",
|
|
55
|
+
"ReadBuildInfo",
|
|
56
|
+
"FromContext",
|
|
57
|
+
"Frexp",
|
|
58
|
+
"Modf",
|
|
59
|
+
"Sincos",
|
|
60
|
+
"Bits",
|
|
61
|
+
"Add",
|
|
62
|
+
"Sub",
|
|
63
|
+
"Mul",
|
|
64
|
+
"Uint64",
|
|
65
|
+
"Int64",
|
|
66
|
+
"Float64",
|
|
67
|
+
"Next",
|
|
68
|
+
"Pop",
|
|
69
|
+
"TrySend",
|
|
70
|
+
"TryRecv",
|
|
71
|
+
"Recv",
|
|
72
|
+
"Stop",
|
|
73
|
+
}
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclass(frozen=True, slots=True)
|
|
78
|
+
class _Token:
|
|
79
|
+
offset: int
|
|
80
|
+
name: str
|
|
81
|
+
kinds: tuple[str, ...] # enclosing block kinds, innermost last
|
|
82
|
+
top: str | None # name of the enclosing top-level function
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _header_start(masked: str, brace: int, floor: int) -> int:
|
|
86
|
+
"""Start of the header of the ``{`` at ``brace``: back to a newline outside parens."""
|
|
87
|
+
depth = 0
|
|
88
|
+
i = brace - 1
|
|
89
|
+
while i >= floor:
|
|
90
|
+
c = masked[i]
|
|
91
|
+
if c in ")]":
|
|
92
|
+
depth += 1
|
|
93
|
+
elif c in "([":
|
|
94
|
+
depth = max(depth - 1, 0)
|
|
95
|
+
elif c == "\n" and depth == 0:
|
|
96
|
+
# A line that ends in an operator or comma continues the header.
|
|
97
|
+
j = i - 1
|
|
98
|
+
while j >= floor and masked[j] in " \t\r":
|
|
99
|
+
j -= 1
|
|
100
|
+
if j < floor or masked[j] not in ",+-&|=<>!(:":
|
|
101
|
+
return i + 1
|
|
102
|
+
i -= 1
|
|
103
|
+
return floor
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _literal_brace(masked: str, brace: int, stack: list[tuple[str, str | None]]) -> bool:
|
|
107
|
+
"""Is the ``{`` at ``brace`` a composite or type literal, not a block?
|
|
108
|
+
|
|
109
|
+
gofmt puts a space (or a ``)``) before a block's brace; a literal's brace
|
|
110
|
+
follows its type directly (``[]T{``, ``map[K]V{``, ``struct{``), and any
|
|
111
|
+
brace inside a literal belongs to it. Literal braces never reset a header.
|
|
112
|
+
"""
|
|
113
|
+
if stack and stack[-1][0] == "literal":
|
|
114
|
+
return True
|
|
115
|
+
before = masked[brace - 1] if brace > 0 else " "
|
|
116
|
+
return before not in " \t\r\n)"
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _scan(masked: str) -> tuple[list[_Token], str | None]:
|
|
120
|
+
"""Tokens of interest with their enclosing blocks, and the package name."""
|
|
121
|
+
tokens: list[_Token] = []
|
|
122
|
+
package: str | None = None
|
|
123
|
+
stack: list[tuple[str, str | None]] = [] # (kind, top-level function name)
|
|
124
|
+
last_brace = 0
|
|
125
|
+
pos = 0
|
|
126
|
+
for line in masked.split("\n"):
|
|
127
|
+
if package is None:
|
|
128
|
+
m = _PACKAGE.match(line)
|
|
129
|
+
if m is not None:
|
|
130
|
+
package = m.group(1)
|
|
131
|
+
found: list[tuple[int, str]] = []
|
|
132
|
+
for name, pattern in _TOKENS.items():
|
|
133
|
+
found.extend((m.start(), name) for m in pattern.finditer(line))
|
|
134
|
+
found.sort()
|
|
135
|
+
k = 0
|
|
136
|
+
for col, c in enumerate(line):
|
|
137
|
+
while k < len(found) and found[k][0] == col:
|
|
138
|
+
top = stack[0][1] if stack else None
|
|
139
|
+
kinds = tuple(kind for kind, _ in stack)
|
|
140
|
+
tokens.append(_Token(pos + col, found[k][1], kinds, top))
|
|
141
|
+
k += 1
|
|
142
|
+
if c == "{" and _literal_brace(masked, pos + col, stack):
|
|
143
|
+
stack.append(("literal", None))
|
|
144
|
+
elif c == "{":
|
|
145
|
+
header = masked[_header_start(masked, pos + col, last_brace) : pos + col]
|
|
146
|
+
if _FOR.match(header):
|
|
147
|
+
kind = "loop"
|
|
148
|
+
elif _FUNC_WORD.search(header):
|
|
149
|
+
kind = "func"
|
|
150
|
+
else:
|
|
151
|
+
kind = "block"
|
|
152
|
+
name = None
|
|
153
|
+
if not stack and kind == "func":
|
|
154
|
+
m = _FUNC_NAME.match(header)
|
|
155
|
+
name = m.group(1) if m is not None else None
|
|
156
|
+
stack.append((kind, name))
|
|
157
|
+
last_brace = pos + col + 1
|
|
158
|
+
elif c == "}":
|
|
159
|
+
if stack and stack.pop()[0] == "literal":
|
|
160
|
+
continue
|
|
161
|
+
last_brace = pos + col + 1
|
|
162
|
+
pos += len(line) + 1
|
|
163
|
+
return tokens, package
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _in_loop(kinds: tuple[str, ...]) -> bool:
|
|
167
|
+
for kind in reversed(kinds):
|
|
168
|
+
if kind == "func":
|
|
169
|
+
return False
|
|
170
|
+
if kind == "loop":
|
|
171
|
+
return True
|
|
172
|
+
return False
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _hit(ctx: FileContext, offset: int, evidence: str, message: str | None = None) -> Hit:
|
|
176
|
+
line, column = ctx.line_index.position(offset)
|
|
177
|
+
return Hit(line, column=column, message=message, evidence=evidence)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _code_line(ctx: FileContext, line: int) -> str:
|
|
181
|
+
"""The masked text of ``line``, whitespace collapsed (a stable evidence key)."""
|
|
182
|
+
index = ctx.line_index
|
|
183
|
+
start = index.line_start(line)
|
|
184
|
+
end = ctx.masked_code.find("\n", start)
|
|
185
|
+
text = ctx.masked_code[start : end if end >= 0 else len(ctx.masked_code)]
|
|
186
|
+
return " ".join(text.split())[:300]
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _changed_masked_lines(ctx: FileContext) -> Iterator[tuple[int, int, str]]:
|
|
190
|
+
"""``(line, offset of its start, masked text)`` of each added line."""
|
|
191
|
+
masked = ctx.masked_code
|
|
192
|
+
index = ctx.line_index
|
|
193
|
+
for number in sorted(ctx.added_lines):
|
|
194
|
+
if number > index.line_count:
|
|
195
|
+
continue
|
|
196
|
+
start = index.line_start(number)
|
|
197
|
+
end = masked.find("\n", start)
|
|
198
|
+
yield number, start, masked[start : end if end >= 0 else len(masked)]
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@rule(
|
|
202
|
+
id="go/defer-in-loop",
|
|
203
|
+
severity="warning",
|
|
204
|
+
title="defer inside a loop",
|
|
205
|
+
languages={"go"},
|
|
206
|
+
rationale=(
|
|
207
|
+
"A deferred call runs when the function returns, not at the end of the loop "
|
|
208
|
+
"iteration, so files, locks and connections opened in the loop stay held until "
|
|
209
|
+
"the whole loop finishes. A defer inside a function literal called in the loop "
|
|
210
|
+
"runs per iteration and is not reported."
|
|
211
|
+
),
|
|
212
|
+
hint="Move the loop body into a function (or a func literal) and defer there.",
|
|
213
|
+
tags={"go", "correctness", "resources"},
|
|
214
|
+
)
|
|
215
|
+
def defer_in_loop(ctx: FileContext) -> Iterator[Hit]:
|
|
216
|
+
"""``defer`` whose nearest enclosing loop is nearer than its nearest function."""
|
|
217
|
+
if not ctx.masked_code:
|
|
218
|
+
return
|
|
219
|
+
tokens, _ = _scan(ctx.masked_code)
|
|
220
|
+
for token in tokens:
|
|
221
|
+
if token.name == "defer" and _in_loop(token.kinds):
|
|
222
|
+
hit = _hit(ctx, token.offset, "")
|
|
223
|
+
yield Hit(hit.line, column=hit.column, evidence=_code_line(ctx, hit.line))
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
@rule(
|
|
227
|
+
id="go/panic",
|
|
228
|
+
severity="warning",
|
|
229
|
+
title="panic in library code",
|
|
230
|
+
languages={"go"},
|
|
231
|
+
classes={"source"},
|
|
232
|
+
rationale=(
|
|
233
|
+
"A panic in a library crashes every program that calls it unless the caller "
|
|
234
|
+
"recovers; Go code returns errors instead. Not reported: package main (a "
|
|
235
|
+
"program may stop itself), init functions, functions named Must... (whose "
|
|
236
|
+
"contract is to panic), test files and test functions."
|
|
237
|
+
),
|
|
238
|
+
hint="Return an error, or name the function Must... if panicking is its contract.",
|
|
239
|
+
tags={"go", "robustness"},
|
|
240
|
+
)
|
|
241
|
+
def panic(ctx: FileContext) -> Iterator[Hit]:
|
|
242
|
+
"""``panic(...)`` outside ``package main``, ``init`` and ``Must...`` functions."""
|
|
243
|
+
if not ctx.masked_code:
|
|
244
|
+
return
|
|
245
|
+
tokens, package = _scan(ctx.masked_code)
|
|
246
|
+
if package == "main":
|
|
247
|
+
return
|
|
248
|
+
for token in tokens:
|
|
249
|
+
if token.name != "panic" or token.top is None:
|
|
250
|
+
continue
|
|
251
|
+
if token.top == "init" or token.top.startswith(("Must", "must")):
|
|
252
|
+
continue
|
|
253
|
+
hit = _hit(ctx, token.offset, "")
|
|
254
|
+
if ctx.in_test_region(hit.line):
|
|
255
|
+
continue
|
|
256
|
+
yield Hit(hit.line, column=hit.column, evidence=_code_line(ctx, hit.line))
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
@rule(
|
|
260
|
+
id="go/ignored-error",
|
|
261
|
+
severity="note",
|
|
262
|
+
title="Call result discarded with _",
|
|
263
|
+
languages={"go"},
|
|
264
|
+
classes={"source"},
|
|
265
|
+
rationale=(
|
|
266
|
+
"Heuristic: an assignment whose last target is `_` and whose value is a call "
|
|
267
|
+
"(`_ = f()`, `n, _ := f()`, `_, _ = f()`) usually drops an error, which by Go "
|
|
268
|
+
"convention is the last result. Calls known to return a bool, size or value "
|
|
269
|
+
"last (sync.Map.Load, os.LookupEnv, strings.Cut, utf8.DecodeRune...) and map "
|
|
270
|
+
"lookups, type assertions and channel receives are not reported. Test files "
|
|
271
|
+
"are skipped."
|
|
272
|
+
),
|
|
273
|
+
hint="Handle the error, or comment why dropping it is safe.",
|
|
274
|
+
tags={"go", "correctness"},
|
|
275
|
+
)
|
|
276
|
+
def ignored_error(ctx: FileContext) -> Iterator[Hit]:
|
|
277
|
+
"""``x, _ := pkg.F(...)`` and ``_ = f(...)``."""
|
|
278
|
+
for number, _start, text in _changed_masked_lines(ctx):
|
|
279
|
+
m = _IGNORED.match(text)
|
|
280
|
+
if m is None:
|
|
281
|
+
continue
|
|
282
|
+
callee = m.group(1)
|
|
283
|
+
if callee.endswith(".") or callee.rsplit(".", 1)[-1] in _NOT_ERRORS:
|
|
284
|
+
continue
|
|
285
|
+
if ctx.in_test_region(number):
|
|
286
|
+
continue
|
|
287
|
+
yield Hit(
|
|
288
|
+
number,
|
|
289
|
+
column=m.start(1) + 1,
|
|
290
|
+
message=f"Result of {callee}(...) discarded with _",
|
|
291
|
+
evidence=" ".join(text.split())[:300],
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
@rule(
|
|
296
|
+
id="go/print-debug",
|
|
297
|
+
severity="note",
|
|
298
|
+
title="Debug print in library code",
|
|
299
|
+
languages={"go"},
|
|
300
|
+
classes={"source"},
|
|
301
|
+
rationale=(
|
|
302
|
+
"fmt.Print*, the print/println builtins and spew dumps in a library write to "
|
|
303
|
+
"the program's stdout or stderr, usually left over from debugging. Package main "
|
|
304
|
+
"(where printing is the program's output), test files and test functions are "
|
|
305
|
+
"not reported; fmt.Fprint* to a chosen writer is fine."
|
|
306
|
+
),
|
|
307
|
+
hint="Remove it, or log through the package's logger.",
|
|
308
|
+
tags={"go", "hygiene"},
|
|
309
|
+
)
|
|
310
|
+
def print_debug(ctx: FileContext) -> Iterator[Hit]:
|
|
311
|
+
"""``fmt.Println``, ``println`` and friends outside ``package main``."""
|
|
312
|
+
if not ctx.masked_code:
|
|
313
|
+
return
|
|
314
|
+
_, package = _scan(ctx.masked_code)
|
|
315
|
+
if package == "main":
|
|
316
|
+
return
|
|
317
|
+
for number, _, text in _changed_masked_lines(ctx):
|
|
318
|
+
if ctx.in_test_region(number):
|
|
319
|
+
continue
|
|
320
|
+
for m in _PRINT.finditer(text):
|
|
321
|
+
call = m.group().split("(")[0].strip()
|
|
322
|
+
yield Hit(
|
|
323
|
+
number,
|
|
324
|
+
column=m.start() + 1,
|
|
325
|
+
message=f"{call} left in library code",
|
|
326
|
+
evidence=" ".join(text.split())[:300],
|
|
327
|
+
)
|
|
328
|
+
break
|