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
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
"""Examples for the security pack."""
|
|
2
|
+
|
|
3
|
+
from codeveto.rules.base import bad, good
|
|
4
|
+
|
|
5
|
+
EXAMPLES = {
|
|
6
|
+
"security/sql-interpolation": [
|
|
7
|
+
bad(
|
|
8
|
+
"app/orders.py",
|
|
9
|
+
"def find(cur, order_id):\n"
|
|
10
|
+
' cur.execute(f"SELECT * FROM orders WHERE id = {order_id}") # expect\n'
|
|
11
|
+
" return cur.fetchone()\n",
|
|
12
|
+
note="An f-string puts the value into the SQL text.",
|
|
13
|
+
),
|
|
14
|
+
bad(
|
|
15
|
+
"app/orders.py",
|
|
16
|
+
"def search(cur, name):\n"
|
|
17
|
+
' query = "SELECT id FROM users WHERE name = \'" + name + "\'"\n'
|
|
18
|
+
" cur.execute(query) # expect\n",
|
|
19
|
+
old="def search(cur, name):\n"
|
|
20
|
+
' cur.execute("SELECT id FROM users WHERE name = %s", (name,))\n',
|
|
21
|
+
note="Built by concatenation into a variable, then executed.",
|
|
22
|
+
),
|
|
23
|
+
bad(
|
|
24
|
+
"src/repo.js",
|
|
25
|
+
"export async function byId(db, id) {\n"
|
|
26
|
+
" const sql = `SELECT * FROM items WHERE id = ${id}`;\n"
|
|
27
|
+
" return db.query(sql); // expect\n"
|
|
28
|
+
"}\n",
|
|
29
|
+
),
|
|
30
|
+
bad(
|
|
31
|
+
"src/db/users.ts",
|
|
32
|
+
"export async function byOrg(db: Pool, org: string) {\n"
|
|
33
|
+
" return db.query<UserRow>(`SELECT * FROM users WHERE org = ${org}`); // expect\n"
|
|
34
|
+
"}\n",
|
|
35
|
+
note="A typed query call: the type argument does not hide the sink.",
|
|
36
|
+
),
|
|
37
|
+
bad(
|
|
38
|
+
"src/main/kotlin/com/acme/UserDao.kt",
|
|
39
|
+
"fun find(jdbc: JdbcTemplate, name: String) =\n"
|
|
40
|
+
' jdbc.query<User>("SELECT * FROM users WHERE name = \'$name\'", mapper) // expect\n',
|
|
41
|
+
note="A Kotlin generic call with a string template.",
|
|
42
|
+
),
|
|
43
|
+
bad(
|
|
44
|
+
"internal/store/users.go",
|
|
45
|
+
"package store\n\n"
|
|
46
|
+
"func (s *Store) Find(name string) (*sql.Rows, error) {\n"
|
|
47
|
+
"\treturn s.db.Query(fmt.Sprintf(\"SELECT * FROM users WHERE name = '%s'\", "
|
|
48
|
+
"name)) // expect\n"
|
|
49
|
+
"}\n",
|
|
50
|
+
),
|
|
51
|
+
bad(
|
|
52
|
+
"src/main/java/com/acme/UserDao.java",
|
|
53
|
+
"class UserDao {\n"
|
|
54
|
+
" ResultSet find(Statement st, String name) throws SQLException {\n"
|
|
55
|
+
' return st.executeQuery("SELECT * FROM users WHERE name = \'" + name + '
|
|
56
|
+
'"\'"); // expect\n'
|
|
57
|
+
" }\n"
|
|
58
|
+
"}\n",
|
|
59
|
+
),
|
|
60
|
+
good(
|
|
61
|
+
"app/orders.py",
|
|
62
|
+
"def find(cur, order_id):\n"
|
|
63
|
+
' cur.execute("SELECT * FROM orders WHERE id = %s", (order_id,))\n'
|
|
64
|
+
" return cur.fetchone()\n",
|
|
65
|
+
note="Parameterised: the SQL text is constant.",
|
|
66
|
+
),
|
|
67
|
+
good(
|
|
68
|
+
"app/orders.py",
|
|
69
|
+
"def find_many(cur, ids):\n"
|
|
70
|
+
' marks = ", ".join("?" * len(ids))\n'
|
|
71
|
+
' cur.execute(f"SELECT * FROM orders WHERE id IN ({marks})", ids)\n',
|
|
72
|
+
note="Only placeholders are interpolated; the values are parameters.",
|
|
73
|
+
),
|
|
74
|
+
good(
|
|
75
|
+
"src/repo.ts",
|
|
76
|
+
"export async function byId(id: string) {\n"
|
|
77
|
+
" return prisma.$queryRaw`SELECT * FROM items WHERE id = ${id}`;\n"
|
|
78
|
+
"}\n",
|
|
79
|
+
note="Tagged templates turn values into parameters.",
|
|
80
|
+
),
|
|
81
|
+
good(
|
|
82
|
+
"internal/store/users.go",
|
|
83
|
+
"package store\n\n"
|
|
84
|
+
"func (s *Store) Find(name string) (*sql.Rows, error) {\n"
|
|
85
|
+
'\treturn s.db.Query("SELECT * FROM users WHERE name = $1", name)\n'
|
|
86
|
+
"}\n",
|
|
87
|
+
),
|
|
88
|
+
good(
|
|
89
|
+
"app/report.py",
|
|
90
|
+
'def summary(df, threshold):\n return df.query(f"amount > {threshold}")\n',
|
|
91
|
+
note="Not SQL, and pandas query() is not a SQL sink.",
|
|
92
|
+
),
|
|
93
|
+
bad(
|
|
94
|
+
"app/stats.py",
|
|
95
|
+
"def row_count(cur, table):\n"
|
|
96
|
+
' cur.execute(f"SELECT COUNT(*) FROM {table}") # expect\n'
|
|
97
|
+
" return cur.fetchone()[0]\n",
|
|
98
|
+
note="Only a table name is interpolated: reported as a warning, since such names "
|
|
99
|
+
"usually come from configuration.",
|
|
100
|
+
),
|
|
101
|
+
],
|
|
102
|
+
"security/shell-injection": [
|
|
103
|
+
bad(
|
|
104
|
+
"app/backup.py",
|
|
105
|
+
"import os\n\n\ndef backup(path):\n"
|
|
106
|
+
' os.system(f"tar czf /backups/latest.tgz {path}") # expect\n',
|
|
107
|
+
),
|
|
108
|
+
bad(
|
|
109
|
+
"app/deploy.py",
|
|
110
|
+
"import subprocess\n\n\ndef run(cmd):\n"
|
|
111
|
+
" subprocess.run(cmd, shell=True, check=True) # expect\n",
|
|
112
|
+
note="A variable command line handed to a shell.",
|
|
113
|
+
),
|
|
114
|
+
bad(
|
|
115
|
+
"src/convert.js",
|
|
116
|
+
'const { exec } = require("node:child_process");\n\n'
|
|
117
|
+
"function convert(file) {\n"
|
|
118
|
+
" exec(`convert ${file} out.png`); // expect\n"
|
|
119
|
+
"}\n",
|
|
120
|
+
),
|
|
121
|
+
bad(
|
|
122
|
+
"internal/run/run.go",
|
|
123
|
+
"package run\n\n"
|
|
124
|
+
"func Grep(pattern string) ([]byte, error) {\n"
|
|
125
|
+
'\treturn exec.Command("sh", "-c", "grep "+pattern+" '
|
|
126
|
+
'/var/log/app.log").Output() // expect\n'
|
|
127
|
+
"}\n",
|
|
128
|
+
),
|
|
129
|
+
good(
|
|
130
|
+
"app/backup.py",
|
|
131
|
+
"import shlex\nimport subprocess\n\n\ndef backup(path):\n"
|
|
132
|
+
' subprocess.run(["tar", "czf", "/backups/latest.tgz", path], check=True)\n'
|
|
133
|
+
' subprocess.run(f"du -sh {shlex.quote(path)}", shell=True)\n',
|
|
134
|
+
note="An argument list needs no shell; quoted values cannot inject.",
|
|
135
|
+
),
|
|
136
|
+
good(
|
|
137
|
+
"src/convert.js",
|
|
138
|
+
'const { execFile } = require("node:child_process");\n\n'
|
|
139
|
+
"function convert(file) {\n"
|
|
140
|
+
' execFile("convert", [file, "out.png"]);\n'
|
|
141
|
+
"}\n",
|
|
142
|
+
),
|
|
143
|
+
good(
|
|
144
|
+
"internal/run/run.go",
|
|
145
|
+
"package run\n\n"
|
|
146
|
+
"func Grep(pattern string) ([]byte, error) {\n"
|
|
147
|
+
'\treturn exec.Command("grep", pattern, "/var/log/app.log").Output()\n'
|
|
148
|
+
"}\n",
|
|
149
|
+
),
|
|
150
|
+
good(
|
|
151
|
+
"app/status.py",
|
|
152
|
+
"import os\n\n\ndef status():\n"
|
|
153
|
+
' cmd = "git status --short"\n'
|
|
154
|
+
" return os.popen(cmd).read()\n",
|
|
155
|
+
note="The variable only ever holds a literal.",
|
|
156
|
+
),
|
|
157
|
+
],
|
|
158
|
+
"security/eval": [
|
|
159
|
+
bad(
|
|
160
|
+
"app/calc.py",
|
|
161
|
+
"def compute(expression):\n return eval(expression) # expect\n",
|
|
162
|
+
),
|
|
163
|
+
bad(
|
|
164
|
+
"app/plugins.py",
|
|
165
|
+
'def load(source, name):\n exec(f"from plugins import {name}") # expect\n',
|
|
166
|
+
),
|
|
167
|
+
bad(
|
|
168
|
+
"src/formula.js",
|
|
169
|
+
"export function evaluate(formula) {\n"
|
|
170
|
+
" return new Function('row', 'return ' + formula); // expect\n"
|
|
171
|
+
"}\n",
|
|
172
|
+
),
|
|
173
|
+
good(
|
|
174
|
+
"app/calc.py",
|
|
175
|
+
"import ast\n\n\ndef compute(expression):\n return ast.literal_eval(expression)\n",
|
|
176
|
+
note="literal_eval only accepts literals.",
|
|
177
|
+
),
|
|
178
|
+
good(
|
|
179
|
+
"app/train.py",
|
|
180
|
+
"def evaluate(model, batch):\n model.eval()\n return model(batch)\n",
|
|
181
|
+
note="An attribute call, not the builtin.",
|
|
182
|
+
),
|
|
183
|
+
good(
|
|
184
|
+
"src/calc.ts",
|
|
185
|
+
"class Calculator {\n"
|
|
186
|
+
" eval(expr: string): number {\n"
|
|
187
|
+
" return parse(expr).value;\n"
|
|
188
|
+
" }\n"
|
|
189
|
+
"}\n",
|
|
190
|
+
note="A method named eval is a definition, not a call.",
|
|
191
|
+
),
|
|
192
|
+
],
|
|
193
|
+
"security/unsafe-deserialization": [
|
|
194
|
+
bad(
|
|
195
|
+
"app/cache.py",
|
|
196
|
+
"import pickle\n\n\ndef read(blob):\n return pickle.loads(blob) # expect\n",
|
|
197
|
+
),
|
|
198
|
+
bad(
|
|
199
|
+
"app/config.py",
|
|
200
|
+
"import yaml\n\n\ndef load(path):\n"
|
|
201
|
+
" with open(path) as fh:\n"
|
|
202
|
+
" return yaml.load(fh, Loader=yaml.Loader) # expect\n",
|
|
203
|
+
),
|
|
204
|
+
bad(
|
|
205
|
+
"src/main/java/com/acme/SessionStore.java",
|
|
206
|
+
"class SessionStore {\n"
|
|
207
|
+
" Object read(InputStream in) throws Exception {\n"
|
|
208
|
+
" ObjectInputStream ois = new ObjectInputStream(in); // expect\n"
|
|
209
|
+
" return ois.readObject();\n"
|
|
210
|
+
" }\n"
|
|
211
|
+
"}\n",
|
|
212
|
+
),
|
|
213
|
+
good(
|
|
214
|
+
"app/config.py",
|
|
215
|
+
"import yaml\n\n\ndef load(path):\n"
|
|
216
|
+
" with open(path) as fh:\n"
|
|
217
|
+
" return yaml.safe_load(fh)\n",
|
|
218
|
+
),
|
|
219
|
+
good(
|
|
220
|
+
"app/model.py",
|
|
221
|
+
"import pickle\n\n\ndef is_picklable(obj):\n"
|
|
222
|
+
" return pickle.loads(pickle.dumps(obj)) == obj\n",
|
|
223
|
+
note="A round trip of the program's own object.",
|
|
224
|
+
),
|
|
225
|
+
bad(
|
|
226
|
+
"app/model.py",
|
|
227
|
+
"import torch\n\n\ndef restore(path):\n return torch.load(path) # expect\n",
|
|
228
|
+
note="torch.load unpickles unless weights_only=True: a warning.",
|
|
229
|
+
),
|
|
230
|
+
bad(
|
|
231
|
+
"tests/conftest.py",
|
|
232
|
+
"import pickle\n\n\ndef load_fixture(path):\n"
|
|
233
|
+
" with open(path, 'rb') as fh:\n"
|
|
234
|
+
" return pickle.load(fh) # expect\n",
|
|
235
|
+
note="Test code still runs, so it is checked, but its hits are capped at warning.",
|
|
236
|
+
),
|
|
237
|
+
bad(
|
|
238
|
+
"vendor/cachelib/store.py",
|
|
239
|
+
"import pickle\n\n\ndef get(blob):\n return pickle.loads(blob) # expect\n",
|
|
240
|
+
note="Vendored code runs as part of the program: full severity (error).",
|
|
241
|
+
),
|
|
242
|
+
good(
|
|
243
|
+
"app/config.py",
|
|
244
|
+
"import yaml\n\n\ndef load(text):\n"
|
|
245
|
+
" return yaml.load(text, Loader=yaml.CSafeLoader)\n",
|
|
246
|
+
),
|
|
247
|
+
],
|
|
248
|
+
"security/tls-verification-disabled": [
|
|
249
|
+
bad(
|
|
250
|
+
"app/client.py",
|
|
251
|
+
"import requests\n\n\ndef fetch(url):\n"
|
|
252
|
+
" return requests.get(url, timeout=10, verify=False) # expect\n",
|
|
253
|
+
),
|
|
254
|
+
bad(
|
|
255
|
+
"src/http.js",
|
|
256
|
+
'const https = require("node:https");\n\n'
|
|
257
|
+
"const agent = new https.Agent({ rejectUnauthorized: false }); // expect\n",
|
|
258
|
+
),
|
|
259
|
+
bad(
|
|
260
|
+
"internal/client/client.go",
|
|
261
|
+
"package client\n\n"
|
|
262
|
+
"var transport = &http.Transport{\n"
|
|
263
|
+
"\tTLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // expect\n"
|
|
264
|
+
"}\n",
|
|
265
|
+
),
|
|
266
|
+
bad(
|
|
267
|
+
"scripts/fetch.sh",
|
|
268
|
+
"#!/bin/sh\nset -eu\ncurl -fsSLk https://downloads.example.com/tool.tgz -o "
|
|
269
|
+
"tool.tgz # expect\n",
|
|
270
|
+
),
|
|
271
|
+
good(
|
|
272
|
+
"app/client.py",
|
|
273
|
+
"import requests\n\n\ndef fetch(url, bundle):\n"
|
|
274
|
+
" return requests.get(url, timeout=10, verify=bundle)\n",
|
|
275
|
+
note="A CA bundle path keeps verification on.",
|
|
276
|
+
),
|
|
277
|
+
good(
|
|
278
|
+
"scripts/health.sh",
|
|
279
|
+
"#!/bin/sh\ncurl -fsk https://localhost:8443/healthz\n",
|
|
280
|
+
note="A self-signed local endpoint.",
|
|
281
|
+
),
|
|
282
|
+
good(
|
|
283
|
+
"app/db.py",
|
|
284
|
+
"import asyncpg\n\n\nasync def connect(dsn):\n return await "
|
|
285
|
+
"asyncpg.connect(dsn, ssl=False)\n",
|
|
286
|
+
note="Disabling TLS on a database driver is a different decision.",
|
|
287
|
+
),
|
|
288
|
+
bad(
|
|
289
|
+
"app/tls.py",
|
|
290
|
+
"import ssl\n\n\ndef context():\n"
|
|
291
|
+
" ctx = ssl.create_default_context()\n"
|
|
292
|
+
" ctx.check_hostname = False # expect\n"
|
|
293
|
+
" return ctx\n",
|
|
294
|
+
note="Host name checks off but certificates still verified: a warning.",
|
|
295
|
+
),
|
|
296
|
+
],
|
|
297
|
+
"security/jwt-verification-disabled": [
|
|
298
|
+
bad(
|
|
299
|
+
"app/auth.py",
|
|
300
|
+
"import jwt\n\n\ndef claims(token):\n"
|
|
301
|
+
' return jwt.decode(token, options={"verify_signature": False}) # expect\n',
|
|
302
|
+
),
|
|
303
|
+
bad(
|
|
304
|
+
"src/auth.js",
|
|
305
|
+
'const jwt = require("jsonwebtoken");\n\n'
|
|
306
|
+
"function check(token, key) {\n"
|
|
307
|
+
' return jwt.verify(token, key, { algorithms: ["HS256", "none"] }); // expect\n'
|
|
308
|
+
"}\n",
|
|
309
|
+
),
|
|
310
|
+
bad(
|
|
311
|
+
"src/main/java/com/acme/Tokens.java",
|
|
312
|
+
"class Tokens {\n"
|
|
313
|
+
" Claims read(String token) {\n"
|
|
314
|
+
" return Jwts.parser().parseClaimsJwt(token).getBody(); // expect\n"
|
|
315
|
+
" }\n"
|
|
316
|
+
"}\n",
|
|
317
|
+
),
|
|
318
|
+
good(
|
|
319
|
+
"app/auth.py",
|
|
320
|
+
"import jwt\n\n\ndef claims(token, key):\n"
|
|
321
|
+
' return jwt.decode(token, key, algorithms=["RS256"])\n',
|
|
322
|
+
),
|
|
323
|
+
good(
|
|
324
|
+
"app/auth.py",
|
|
325
|
+
"import jwt\n\n\ndef key_id(token):\n"
|
|
326
|
+
' return jwt.get_unverified_header(token)["kid"]\n',
|
|
327
|
+
note="Reading the key ID before verifying is the intended use.",
|
|
328
|
+
),
|
|
329
|
+
],
|
|
330
|
+
"security/archive-extraction": [
|
|
331
|
+
bad(
|
|
332
|
+
"app/unpack.py",
|
|
333
|
+
"import tarfile\n\n\ndef unpack(path, dest):\n"
|
|
334
|
+
" with tarfile.open(path) as tar:\n"
|
|
335
|
+
" tar.extractall(dest) # expect\n",
|
|
336
|
+
),
|
|
337
|
+
bad(
|
|
338
|
+
"internal/unzip/unzip.go",
|
|
339
|
+
'package unzip\n\nimport (\n\t"archive/zip"\n\t"path/filepath"\n)\n\n'
|
|
340
|
+
"func target(dest string, f *zip.File) string {\n"
|
|
341
|
+
"\treturn filepath.Join(dest, f.Name) // expect\n"
|
|
342
|
+
"}\n",
|
|
343
|
+
),
|
|
344
|
+
good(
|
|
345
|
+
"app/unpack.py",
|
|
346
|
+
"import tarfile\n\n\ndef unpack(path, dest):\n"
|
|
347
|
+
" with tarfile.open(path) as tar:\n"
|
|
348
|
+
' tar.extractall(dest, filter="data")\n',
|
|
349
|
+
),
|
|
350
|
+
good(
|
|
351
|
+
"app/unpack.py",
|
|
352
|
+
"import zipfile\n\n\ndef unpack(path, dest):\n"
|
|
353
|
+
" with zipfile.ZipFile(path) as zf:\n"
|
|
354
|
+
" zf.extractall(dest)\n",
|
|
355
|
+
note="zipfile strips .. and absolute names itself.",
|
|
356
|
+
),
|
|
357
|
+
],
|
|
358
|
+
"security/xss-sink": [
|
|
359
|
+
bad(
|
|
360
|
+
"src/render.js",
|
|
361
|
+
"export function show(el, comment) {\n"
|
|
362
|
+
" el.innerHTML = `<p>${comment.body}</p>`; // expect\n"
|
|
363
|
+
"}\n",
|
|
364
|
+
),
|
|
365
|
+
bad(
|
|
366
|
+
"app/views.py",
|
|
367
|
+
"from django.utils.safestring import mark_safe\n\n\n"
|
|
368
|
+
"def bio(user):\n return mark_safe(user.bio) # expect\n",
|
|
369
|
+
),
|
|
370
|
+
good(
|
|
371
|
+
"src/render.js",
|
|
372
|
+
"export function show(el, comment) {\n"
|
|
373
|
+
" el.textContent = comment.body;\n"
|
|
374
|
+
' el.innerHTML = "";\n'
|
|
375
|
+
"}\n",
|
|
376
|
+
),
|
|
377
|
+
good(
|
|
378
|
+
"src/render.js",
|
|
379
|
+
"export function show(el, html) {\n el.innerHTML = DOMPurify.sanitize(html);\n}\n",
|
|
380
|
+
),
|
|
381
|
+
],
|
|
382
|
+
"security/autoescape-disabled": [
|
|
383
|
+
bad(
|
|
384
|
+
"app/templates.py",
|
|
385
|
+
"from jinja2 import Environment, FileSystemLoader\n\n"
|
|
386
|
+
'env = Environment(loader=FileSystemLoader("templates"), autoescape=False) # expect\n',
|
|
387
|
+
),
|
|
388
|
+
bad(
|
|
389
|
+
"src/views.js",
|
|
390
|
+
'nunjucks.configure("views", { autoescape: false, express: app }); // expect\n',
|
|
391
|
+
),
|
|
392
|
+
good(
|
|
393
|
+
"app/templates.py",
|
|
394
|
+
"from jinja2 import Environment, FileSystemLoader, select_autoescape\n\n"
|
|
395
|
+
'env = Environment(loader=FileSystemLoader("templates"), '
|
|
396
|
+
"autoescape=select_autoescape())\n",
|
|
397
|
+
),
|
|
398
|
+
good(
|
|
399
|
+
"app/codegen.py",
|
|
400
|
+
"from jinja2 import Environment, PackageLoader\n\n"
|
|
401
|
+
'env = Environment(loader=PackageLoader("codegen"), keep_trailing_newline=True)\n',
|
|
402
|
+
note="A code generator: no HTML templates in the module.",
|
|
403
|
+
),
|
|
404
|
+
],
|
|
405
|
+
"security/csrf-disabled": [
|
|
406
|
+
bad(
|
|
407
|
+
"app/views.py",
|
|
408
|
+
"from django.views.decorators.csrf import csrf_exempt\n\n\n"
|
|
409
|
+
"@csrf_exempt # expect\n"
|
|
410
|
+
"def webhook(request):\n return handle(request)\n",
|
|
411
|
+
),
|
|
412
|
+
bad(
|
|
413
|
+
"src/main/java/com/acme/SecurityConfig.java",
|
|
414
|
+
"class SecurityConfig {\n"
|
|
415
|
+
" SecurityFilterChain chain(HttpSecurity http) throws Exception {\n"
|
|
416
|
+
" return http.csrf(AbstractHttpConfigurer::disable).build(); // expect\n"
|
|
417
|
+
" }\n"
|
|
418
|
+
"}\n",
|
|
419
|
+
),
|
|
420
|
+
good(
|
|
421
|
+
"app/views.py",
|
|
422
|
+
"from django.views.decorators.csrf import csrf_protect\n\n\n"
|
|
423
|
+
"@csrf_protect\n"
|
|
424
|
+
"def update(request):\n return handle(request)\n",
|
|
425
|
+
),
|
|
426
|
+
good(
|
|
427
|
+
"src/main/java/com/acme/SecurityConfig.java",
|
|
428
|
+
"class SecurityConfig {\n"
|
|
429
|
+
" SecurityFilterChain chain(HttpSecurity http) throws Exception {\n"
|
|
430
|
+
" return http.csrf(Customizer.withDefaults()).build();\n"
|
|
431
|
+
" }\n"
|
|
432
|
+
"}\n",
|
|
433
|
+
),
|
|
434
|
+
],
|
|
435
|
+
"security/insecure-cookie": [
|
|
436
|
+
bad("app/settings.py", "SESSION_COOKIE_SECURE = False # expect\n"),
|
|
437
|
+
bad(
|
|
438
|
+
"src/session.js",
|
|
439
|
+
"app.use(session({ secret: config.secret, cookie: { secure: false } })); // expect\n",
|
|
440
|
+
),
|
|
441
|
+
good("app/settings.py", "SESSION_COOKIE_SECURE = True\nSESSION_COOKIE_HTTPONLY = True\n"),
|
|
442
|
+
good(
|
|
443
|
+
"src/mail.js",
|
|
444
|
+
"const transport = nodemailer.createTransport({ host, port: 587, secure: false });\n",
|
|
445
|
+
note="SMTP STARTTLS options, not a cookie.",
|
|
446
|
+
),
|
|
447
|
+
],
|
|
448
|
+
"security/permissive-cors": [
|
|
449
|
+
bad(
|
|
450
|
+
"src/server.js",
|
|
451
|
+
"app.use(cors({ origin: true, credentials: true })); // expect\n",
|
|
452
|
+
note="origin: true reflects any Origin.",
|
|
453
|
+
),
|
|
454
|
+
bad(
|
|
455
|
+
"app/main.py",
|
|
456
|
+
"app.add_middleware(\n"
|
|
457
|
+
" CORSMiddleware,\n"
|
|
458
|
+
' allow_origins=["*"], # expect\n'
|
|
459
|
+
" allow_credentials=True,\n"
|
|
460
|
+
")\n",
|
|
461
|
+
),
|
|
462
|
+
good(
|
|
463
|
+
"src/server.js",
|
|
464
|
+
'app.use(cors({ origin: ["https://app.example.com"], credentials: true }));\n',
|
|
465
|
+
),
|
|
466
|
+
good(
|
|
467
|
+
"app/main.py",
|
|
468
|
+
'app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["GET"])\n',
|
|
469
|
+
note="A public read-only API without credentials.",
|
|
470
|
+
),
|
|
471
|
+
],
|
|
472
|
+
"security/debug-enabled": [
|
|
473
|
+
bad("app/settings.py", 'DEBUG = True # expect\nALLOWED_HOSTS = ["*"]\n'),
|
|
474
|
+
bad(
|
|
475
|
+
"app/server.py",
|
|
476
|
+
'if __name__ == "__main__":\n app.run(host="0.0.0.0", debug=True) # expect\n',
|
|
477
|
+
),
|
|
478
|
+
good("app/settings.py", 'DEBUG = os.environ.get("DJANGO_DEBUG") == "1"\n'),
|
|
479
|
+
good("app/settings/dev.py", "DEBUG = True\n", note="Development settings."),
|
|
480
|
+
],
|
|
481
|
+
"security/weak-hash": [
|
|
482
|
+
bad(
|
|
483
|
+
"app/accounts.py",
|
|
484
|
+
"import hashlib\n\n\ndef store(user, password):\n"
|
|
485
|
+
" user.pw = hashlib.md5(password.encode()).hexdigest() # expect\n",
|
|
486
|
+
),
|
|
487
|
+
bad(
|
|
488
|
+
"src/tokens.js",
|
|
489
|
+
"function resetToken(user) {\n"
|
|
490
|
+
" return crypto.createHash('sha1').update(user.id + Date.now()).digest('hex'); "
|
|
491
|
+
" // expect\n"
|
|
492
|
+
"}\n",
|
|
493
|
+
),
|
|
494
|
+
good(
|
|
495
|
+
"app/cache.py",
|
|
496
|
+
"import hashlib\n\n\ndef cache_key(url):\n"
|
|
497
|
+
" return hashlib.md5(url.encode(), usedforsecurity=False).hexdigest()\n",
|
|
498
|
+
),
|
|
499
|
+
good(
|
|
500
|
+
"app/assets.py",
|
|
501
|
+
"import hashlib\n\n\ndef etag(data):\n return hashlib.sha1(data).hexdigest()\n",
|
|
502
|
+
note="A checksum, not a secret.",
|
|
503
|
+
),
|
|
504
|
+
],
|
|
505
|
+
"security/insecure-random": [
|
|
506
|
+
bad(
|
|
507
|
+
"app/tokens.py",
|
|
508
|
+
"import random\nimport string\n\n\ndef make_token():\n"
|
|
509
|
+
' return "".join(random.choice(string.ascii_letters) for _ in range(32)) # '
|
|
510
|
+
"expect\n",
|
|
511
|
+
),
|
|
512
|
+
bad(
|
|
513
|
+
"src/session.js",
|
|
514
|
+
"const sessionId = Math.random().toString(36).slice(2); // expect\n",
|
|
515
|
+
),
|
|
516
|
+
good(
|
|
517
|
+
"app/tokens.py",
|
|
518
|
+
"import secrets\n\n\ndef make_token():\n return secrets.token_urlsafe(32)\n",
|
|
519
|
+
),
|
|
520
|
+
good(
|
|
521
|
+
"src/retry.js",
|
|
522
|
+
"const delay = base * 2 ** attempt + Math.random() * 100;\n",
|
|
523
|
+
note="Jitter is not a secret.",
|
|
524
|
+
),
|
|
525
|
+
],
|
|
526
|
+
"security/world-writable": [
|
|
527
|
+
bad(
|
|
528
|
+
"app/install.py",
|
|
529
|
+
"import os\n\n\ndef publish(path):\n os.chmod(path, 0o777) # expect\n",
|
|
530
|
+
),
|
|
531
|
+
bad(
|
|
532
|
+
"scripts/setup.sh",
|
|
533
|
+
"#!/bin/sh\nset -eu\nmkdir -p /srv/app/uploads\nchmod -R a+w /srv/app/uploads "
|
|
534
|
+
"# expect\n",
|
|
535
|
+
),
|
|
536
|
+
bad(
|
|
537
|
+
"internal/fsutil/fsutil.go",
|
|
538
|
+
"package fsutil\n\nfunc Share(p string) error {\n\treturn os.Chmod(p, 0666) // "
|
|
539
|
+
"expect\n}\n",
|
|
540
|
+
),
|
|
541
|
+
good(
|
|
542
|
+
"app/install.py",
|
|
543
|
+
"import os\n\n\ndef publish(path):\n os.chmod(path, 0o755)\n "
|
|
544
|
+
"os.makedirs(path, 0o777, exist_ok=True)\n",
|
|
545
|
+
note="makedirs modes are reduced by the umask.",
|
|
546
|
+
),
|
|
547
|
+
good(
|
|
548
|
+
"scripts/setup.sh",
|
|
549
|
+
"#!/bin/sh\nset -eu\nmkdir -p /srv/shared/tmp\nchmod 1777 "
|
|
550
|
+
"/srv/shared/tmp\nchmod g+w /srv/app\n",
|
|
551
|
+
note="A sticky shared directory, and group write only.",
|
|
552
|
+
),
|
|
553
|
+
],
|
|
554
|
+
"security/insecure-tempfile": [
|
|
555
|
+
bad(
|
|
556
|
+
"app/export.py",
|
|
557
|
+
'import tempfile\n\n\ndef scratch():\n return tempfile.mktemp(suffix=".csv") '
|
|
558
|
+
" # expect\n",
|
|
559
|
+
),
|
|
560
|
+
bad(
|
|
561
|
+
"internal/export/export.go",
|
|
562
|
+
"package export\n\nfunc Write(data []byte) error {\n"
|
|
563
|
+
'\treturn os.WriteFile("/tmp/export.csv", data, 0o600) // expect\n'
|
|
564
|
+
"}\n",
|
|
565
|
+
),
|
|
566
|
+
good(
|
|
567
|
+
"app/export.py",
|
|
568
|
+
"import tempfile\n\n\ndef scratch():\n"
|
|
569
|
+
' return tempfile.NamedTemporaryFile(suffix=".csv", delete=False)\n',
|
|
570
|
+
),
|
|
571
|
+
good(
|
|
572
|
+
"internal/export/export.go",
|
|
573
|
+
"package export\n\nfunc Write(data []byte) error {\n"
|
|
574
|
+
'\tf, err := os.CreateTemp("", "export-*.csv")\n'
|
|
575
|
+
"\tif err != nil {\n\t\treturn err\n\t}\n"
|
|
576
|
+
"\t_, err = f.Write(data)\n"
|
|
577
|
+
"\treturn err\n"
|
|
578
|
+
"}\n",
|
|
579
|
+
),
|
|
580
|
+
],
|
|
581
|
+
"security/request-path-traversal": [
|
|
582
|
+
bad(
|
|
583
|
+
"app/files.py",
|
|
584
|
+
"from flask import request, send_file\n\n\n"
|
|
585
|
+
"def download():\n"
|
|
586
|
+
' name = request.args["file"]\n'
|
|
587
|
+
" return send_file(os.path.join(UPLOADS, name)) # expect\n",
|
|
588
|
+
),
|
|
589
|
+
bad(
|
|
590
|
+
"src/files.js",
|
|
591
|
+
"app.get('/files', (req, res) => {\n"
|
|
592
|
+
" fs.createReadStream(path.join(ROOT, req.query.name)).pipe(res); // expect\n"
|
|
593
|
+
"});\n",
|
|
594
|
+
),
|
|
595
|
+
good(
|
|
596
|
+
"app/files.py",
|
|
597
|
+
"from flask import request, send_from_directory\n\n\n"
|
|
598
|
+
"def download():\n"
|
|
599
|
+
' return send_from_directory(UPLOADS, request.args["file"])\n',
|
|
600
|
+
),
|
|
601
|
+
good(
|
|
602
|
+
"src/files.js",
|
|
603
|
+
"app.get('/files', (req, res) => {\n"
|
|
604
|
+
" res.sendFile(path.basename(req.query.name), { root: ROOT });\n"
|
|
605
|
+
"});\n",
|
|
606
|
+
),
|
|
607
|
+
],
|
|
608
|
+
}
|