vulpfest 1.0.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.
@@ -0,0 +1,434 @@
1
+ """Mode C: pemeriksaan repositori (hanya lewat cli.py, di komputer pemilik kode).
2
+
3
+ Membaca folder proyek: dependensi rentan (OSV.dev), paket yang tidak ada di registry (sering
4
+ dikarang AI), kunci rahasia di kode dan riwayat git, file .env yang ter-commit, workflow CI/CD
5
+ berisiko, Dockerfile, dan beberapa pola kode berisiko. Isi kode tidak dikirim ke mana pun; yang
6
+ dikirim ke internet hanya nama dan versi paket (ke OSV.dev dan registry npm/PyPI), dan itu dapat
7
+ dimatikan dengan online=False.
8
+ """
9
+ import json
10
+ import os
11
+ import re
12
+ import subprocess
13
+ import time
14
+ from concurrent.futures import ThreadPoolExecutor
15
+ from pathlib import Path
16
+
17
+ import requests
18
+
19
+ from deep_web import find_secrets
20
+
21
+ SKIP_DIRS = {'node_modules', '.git', '.next', 'dist', 'build', 'out', 'venv', '.venv', 'env', '__pycache__', '.vercel',
22
+ 'coverage', '.turbo', '.cache', 'prisma-generated', '.pytest_cache', '.svelte-kit', '.nuxt', '.output'}
23
+ MAX_FILE_BYTES = 1_000_000
24
+ MAX_FILES = 5000
25
+ HISTORY_MAX_LINES = 400_000
26
+ LOCK_NAMES = ('package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'bun.lockb', 'bun.lock', 'npm-shrinkwrap.json')
27
+ TEXT_SKIP_NAMES = set(LOCK_NAMES) | {'poetry.lock', 'Pipfile.lock'}
28
+ ENV_FILE_RE = re.compile(r'(^|/)\.env(\.[\w.-]+)?$')
29
+ ENV_SAFE_RE = re.compile(r'(example|sample|template|dist|defaults?)$', re.I)
30
+ PUBLIC_SECRET_RE = re.compile(r'\b(?:NEXT_PUBLIC|VITE|REACT_APP|EXPO_PUBLIC|NUXT_PUBLIC|PUBLIC)_[A-Z0-9_]*(?:SECRET|SERVICE_ROLE|PRIVATE_KEY|PASSWORD)[A-Z0-9_]*\b')
31
+ REQ_PINNED_RE = re.compile(r'^\s*([A-Za-z0-9][A-Za-z0-9_.\-]*)(?:\[[^\]]*\])?\s*==\s*([A-Za-z0-9_.\-]+)')
32
+ REQ_NAME_RE = re.compile(r'^\s*([A-Za-z0-9][A-Za-z0-9_.\-]*)')
33
+ SKIP_SPECS = ('file:', 'link:', 'workspace:', 'git', 'http', 'npm:', 'github:', 'portal:')
34
+ INJECT_RE = re.compile(r'\$\{\{\s*(github\.event\.(?:issue|pull_request|comment|review|review_comment|discussion|head_commit|pages)\.[\w.]*(?:title|body|message|label|name|ref)|github\.head_ref)\s*\}\}')
35
+ SEV_POINTS = {'CRITICAL': ('Tinggi', 20), 'HIGH': ('Tinggi', 10), 'MODERATE': ('Menengah', 5), 'MEDIUM': ('Menengah', 5), 'LOW': ('Rendah', 2)}
36
+ SEV_ORDER = ['CRITICAL', 'HIGH', 'MODERATE', 'MEDIUM', 'LOW']
37
+ DEP_POINTS_CAP = 40
38
+
39
+
40
+ def _files(root):
41
+ n = 0
42
+ for dirpath, dirnames, filenames in os.walk(root):
43
+ dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS]
44
+ for name in filenames:
45
+ yield Path(dirpath) / name
46
+ n += 1
47
+ if n >= MAX_FILES:
48
+ return
49
+
50
+
51
+ def _read_text(p):
52
+ try:
53
+ if p.stat().st_size > MAX_FILE_BYTES:
54
+ return None
55
+ data = p.read_bytes()
56
+ except OSError:
57
+ return None
58
+ if data.startswith((b'\xff\xfe', b'\xfe\xff')):
59
+ return data.decode('utf-16', 'ignore') # misalnya file yang ditulis PowerShell 5.1 dengan ">"
60
+ if data.startswith(b'\xef\xbb\xbf'):
61
+ data = data[3:]
62
+ if b'\0' in data[:2048]:
63
+ return None
64
+ return data.decode('utf-8', 'ignore')
65
+
66
+
67
+ def _git(root, *args, timeout=60):
68
+ try:
69
+ r = subprocess.run(['git', '-C', str(root), *args], capture_output=True, timeout=timeout)
70
+ except (OSError, subprocess.TimeoutExpired):
71
+ return None
72
+ return r.stdout.decode('utf-8', 'ignore') if r.returncode == 0 else None
73
+
74
+
75
+ def _dedupe(items):
76
+ seen, out = set(), []
77
+ for x in items:
78
+ key = (x['type'], x['masked'])
79
+ if key not in seen:
80
+ seen.add(key)
81
+ out.append(x)
82
+ return out
83
+
84
+
85
+ def _history_secrets(root, deadline):
86
+ try:
87
+ proc = subprocess.Popen(['git', '-C', str(root), 'log', '--all', '-p', '--no-color', '-U0', '--no-ext-diff',
88
+ '--format=commit %H'], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
89
+ except OSError:
90
+ return [], False
91
+ found, commit, path, lines, partial = [], '', '', 0, False
92
+ try:
93
+ for raw in proc.stdout:
94
+ lines += 1
95
+ if lines > HISTORY_MAX_LINES or time.monotonic() > deadline:
96
+ partial = True
97
+ break
98
+ line = raw.decode('utf-8', 'ignore').rstrip('\r\n')
99
+ if line.startswith('commit '):
100
+ commit = line[7:15]
101
+ elif line.startswith('+++ '):
102
+ path = line[6:] if line.startswith('+++ b/') else line[4:]
103
+ elif line.startswith('+') and path:
104
+ for f in find_secrets(line[1:], path):
105
+ f['commit'] = commit
106
+ found.append(f)
107
+ finally:
108
+ if proc.poll() is None:
109
+ proc.kill()
110
+ proc.stdout.close()
111
+ proc.wait()
112
+ return found, partial
113
+
114
+
115
+ def _npm_packages(pkg_dir):
116
+ pj = json.loads((pkg_dir / 'package.json').read_text(encoding='utf-8', errors='ignore') or '{}')
117
+ direct = {**(pj.get('dependencies') or {}), **(pj.get('devDependencies') or {})}
118
+ pkgs, approx = [], False
119
+ lock = pkg_dir / 'package-lock.json'
120
+ if lock.exists():
121
+ data = json.loads(lock.read_text(encoding='utf-8', errors='ignore') or '{}')
122
+ for key, v in (data.get('packages') or {}).items():
123
+ if not key or not isinstance(v, dict) or v.get('dev') or v.get('link') or 'version' not in v:
124
+ continue
125
+ pkgs.append((v.get('name') or key.rsplit('node_modules/', 1)[-1], v['version'], 'npm'))
126
+ else:
127
+ approx = True
128
+ for name, spec in (pj.get('dependencies') or {}).items():
129
+ m = re.match(r'^[\^~=v]?\s*(\d+\.\d+\.\d+)', str(spec).strip())
130
+ if m:
131
+ pkgs.append((name, m.group(1), 'npm'))
132
+ has_lock = any((pkg_dir / n).exists() for n in LOCK_NAMES)
133
+ return pkgs, direct, has_lock, approx
134
+
135
+
136
+ def _osv_query(pkgs):
137
+ """{(nama, versi, ekosistem): {'ids', 'severity', 'summary'}} untuk paket yang punya kerentanan."""
138
+ hits = {}
139
+ for i in range(0, len(pkgs), 500):
140
+ chunk = pkgs[i:i + 500]
141
+ r = requests.post('https://api.osv.dev/v1/querybatch', timeout=25, json={
142
+ 'queries': [{'package': {'name': n, 'ecosystem': e}, 'version': v} for n, v, e in chunk]})
143
+ r.raise_for_status()
144
+ for key, res in zip(chunk, r.json().get('results', [])):
145
+ ids = [x['id'] for x in (res or {}).get('vulns', []) or []]
146
+ if ids:
147
+ hits[key] = ids
148
+ uniq = list(dict.fromkeys(i for ids in hits.values() for i in ids))[:80]
149
+
150
+ def detail(vid):
151
+ try:
152
+ d = requests.get(f'https://api.osv.dev/v1/vulns/{vid}', timeout=10).json()
153
+ except (requests.RequestException, ValueError):
154
+ return vid, ('MODERATE', '')
155
+ sev = str((d.get('database_specific') or {}).get('severity') or 'MODERATE').upper()
156
+ return vid, (sev if sev in SEV_ORDER else 'MODERATE', (d.get('summary') or '')[:160])
157
+
158
+ with ThreadPoolExecutor(max_workers=8) as ex:
159
+ details = dict(ex.map(detail, uniq))
160
+ out = {}
161
+ for key, ids in hits.items():
162
+ worst = min((details.get(i, ('MODERATE', '')) for i in ids), key=lambda s: SEV_ORDER.index(s[0]))
163
+ out[key] = {'ids': ids, 'severity': worst[0], 'summary': worst[1]}
164
+ return out
165
+
166
+
167
+ def _registry_exists(name, eco):
168
+ """True/False bila registry menjawab jelas; None bila tidak bisa dipastikan."""
169
+ if eco == 'npm':
170
+ url = 'https://registry.npmjs.org/' + name.replace('/', '%2F')
171
+ else:
172
+ url = f'https://pypi.org/pypi/{name}/json'
173
+ try:
174
+ r = requests.get(url, timeout=6, headers={'Accept': 'application/json'})
175
+ except requests.RequestException:
176
+ return None
177
+ if r.status_code == 404:
178
+ return False
179
+ return True if r.status_code < 400 else None
180
+
181
+
182
+ def _workflow_issues(root):
183
+ issues = []
184
+ wdir = root / '.github' / 'workflows'
185
+ if not wdir.is_dir():
186
+ return issues
187
+ for f in sorted(list(wdir.glob('*.yml')) + list(wdir.glob('*.yaml'))):
188
+ text = _read_text(f) or ''
189
+ rel = f.relative_to(root).as_posix()
190
+ if 'pull_request_target' in text and re.search(r'ref:\s*\$\{\{\s*github\.event\.pull_request\.head\.(?:sha|ref)', text):
191
+ issues.append({'id': 'CI-PRT-CHECKOUT', 'file': rel})
192
+ m = INJECT_RE.search(text)
193
+ if m:
194
+ issues.append({'id': 'CI-SCRIPT-INJECTION', 'file': rel, 'detail': m.group(1)})
195
+ if re.search(r'echo[^\n]*\$\{\{\s*secrets\.', text):
196
+ issues.append({'id': 'CI-SECRET-ECHO', 'file': rel})
197
+ unpinned = sorted({f'{a}@{b}' for a, b in re.findall(r'uses:\s*([\w.-]+/[\w./-]+)@([\w.-]+)', text)
198
+ if not re.fullmatch(r'[0-9a-f]{40}', b) and not a.startswith(('actions/', 'github/'))})
199
+ if unpinned:
200
+ issues.append({'id': 'CI-UNPINNED-ACTION', 'file': rel, 'detail': ', '.join(unpinned[:5])})
201
+ if not re.search(r'^permissions\s*:', text, re.M) or re.search(r'permissions\s*:\s*write-all', text):
202
+ issues.append({'id': 'CI-BROAD-PERMISSIONS', 'file': rel})
203
+ return issues
204
+
205
+
206
+ def _docker_issues(root, files):
207
+ issues = []
208
+ dockerfiles = [p for p in files if p.name == 'Dockerfile' or p.name.startswith('Dockerfile.') or p.name.endswith('.Dockerfile')][:5]
209
+ ignore = _read_text(root / '.dockerignore') or ''
210
+ has_env = any(ENV_FILE_RE.search(p.relative_to(root).as_posix()) and not ENV_SAFE_RE.search(p.name) for p in files)
211
+ for f in dockerfiles:
212
+ text = _read_text(f) or ''
213
+ rel = f.relative_to(root).as_posix()
214
+ users = re.findall(r'^\s*USER\s+(\S+)', text, re.M | re.I)
215
+ if not users or users[-1].lower() in ('root', '0'):
216
+ issues.append({'id': 'DOCKER-ROOT', 'file': rel})
217
+ m = re.search(r'^\s*(?:ENV|ARG)\s+([A-Z0-9_]*(?:SECRET|PASSWORD|TOKEN|API_KEY|PRIVATE_KEY)[A-Z0-9_]*)\s*[= ]\s*(\S+)', text, re.M | re.I)
218
+ if m and not m.group(2).startswith('$'):
219
+ issues.append({'id': 'DOCKER-SECRET-ENV', 'file': rel, 'detail': m.group(1)})
220
+ if has_env and re.search(r'^\s*(?:COPY|ADD)\s+\.\s+', text, re.M | re.I) and not re.search(r'(^|\n)\s*\.env', ignore):
221
+ issues.append({'id': 'DOCKER-ENV-COPY', 'file': rel})
222
+ return issues
223
+
224
+
225
+ def _code_issues(texts):
226
+ hits = []
227
+ for rel, text in texts:
228
+ if rel.endswith(('.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', '.vue', '.svelte')) or ENV_FILE_RE.search(rel):
229
+ for m in PUBLIC_SECRET_RE.finditer(text):
230
+ hits.append({'id': 'PUBLIC-ENV-SECRET', 'file': rel, 'detail': m.group(0)})
231
+ if rel.endswith('.py'):
232
+ if re.search(r'^\s*[A-Za-z_][\w.]*\.run\([^)\n]*debug\s*=\s*True', text, re.M):
233
+ hits.append({'id': 'DEBUG-IN-CODE', 'file': rel, 'detail': 'app.run(debug=True)'})
234
+ if rel.endswith('settings.py') and re.search(r'^\s*DEBUG\s*=\s*True\b', text, re.M):
235
+ hits.append({'id': 'DEBUG-IN-CODE', 'file': rel, 'detail': 'DEBUG = True'})
236
+ return hits
237
+
238
+
239
+ def run(path, online=True, history=True, budget=90):
240
+ t0 = time.monotonic()
241
+ root = Path(path).expanduser().resolve()
242
+ if not root.is_dir():
243
+ return {'error': f'Folder tidak ditemukan: {root}'}
244
+ out = {'path': str(root), 'git': (root / '.git').exists(), 'online': online, 'files_scanned': 0, 'manifests': [],
245
+ 'deps': {'checked': 0, 'vulnerable': [], 'approx': False}, 'missing_packages': [], 'scoped_missing': [],
246
+ 'lockfile_missing': [], 'secrets': [], 'history_secrets': [], 'history_partial': False, 'env_files': [],
247
+ 'env_files_history': [], 'workflows': [], 'docker': [], 'code': [], 'notes': []}
248
+ files = list(_files(root))
249
+ texts = []
250
+ for p in files:
251
+ if p.name in TEXT_SKIP_NAMES:
252
+ continue
253
+ t = _read_text(p)
254
+ if t is not None:
255
+ texts.append((p.relative_to(root).as_posix(), t))
256
+ out['files_scanned'] = len(texts)
257
+
258
+ current = [f for rel, t in texts for f in find_secrets(t, rel)]
259
+ history_found = []
260
+ if out['git'] and history:
261
+ history_found, out['history_partial'] = _history_secrets(root, t0 + budget * 0.6)
262
+ tracked = (_git(root, 'ls-files') or '').splitlines()
263
+ out['env_files'] = [n for n in tracked if ENV_FILE_RE.search(n) and not ENV_SAFE_RE.search(n)]
264
+ added = (_git(root, 'log', '--all', '--diff-filter=A', '--name-only', '--format=') or '').splitlines()
265
+ out['env_files_history'] = sorted({n for n in added if n and ENV_FILE_RE.search(n) and not ENV_SAFE_RE.search(n)} - set(out['env_files']))
266
+ out['secrets'] = _dedupe(current)
267
+ current_keys = {(x['type'], x['masked']) for x in out['secrets']}
268
+ out['history_secrets'] = _dedupe([x for x in history_found if (x['type'], x['masked']) not in current_keys])
269
+
270
+ pkgs, direct_npm, direct_py = [], {}, []
271
+ for p in files:
272
+ rel = p.relative_to(root).as_posix()
273
+ if p.name == 'package.json':
274
+ try:
275
+ pk, direct, has_lock, approx = _npm_packages(p.parent)
276
+ except (OSError, ValueError):
277
+ continue
278
+ out['manifests'].append(rel)
279
+ pkgs += pk
280
+ direct_npm.update(direct)
281
+ if not has_lock:
282
+ out['lockfile_missing'].append(rel)
283
+ out['deps']['approx'] = out['deps']['approx'] or approx
284
+ elif re.fullmatch(r'requirements.*\.txt', p.name):
285
+ out['manifests'].append(rel)
286
+ for line in (_read_text(p) or '').splitlines():
287
+ if line.strip().startswith(('#', '-')) or '://' in line:
288
+ continue
289
+ m = REQ_PINNED_RE.match(line)
290
+ if m:
291
+ pkgs.append((m.group(1), m.group(2), 'PyPI'))
292
+ n = REQ_NAME_RE.match(line)
293
+ if n:
294
+ direct_py.append(n.group(1))
295
+ pkgs = list(dict.fromkeys(pkgs))[:3000]
296
+ out['deps']['checked'] = len(pkgs)
297
+
298
+ if online and pkgs:
299
+ try:
300
+ vulns = _osv_query(pkgs)
301
+ except (requests.RequestException, ValueError) as e:
302
+ vulns = {}
303
+ out['notes'].append(f'OSV.dev tidak dapat dihubungi ({e.__class__.__name__}); dependensi tidak diperiksa.')
304
+ out['deps']['vulnerable'] = sorted(
305
+ [{'name': n, 'version': v, 'ecosystem': e, **info} for (n, v, e), info in vulns.items()],
306
+ key=lambda x: SEV_ORDER.index(x['severity']))
307
+ if online:
308
+ names = [(n, 'npm') for n, spec in direct_npm.items() if not str(spec).startswith(SKIP_SPECS)]
309
+ names += [(n, 'PyPI') for n in direct_py]
310
+ names = list(dict.fromkeys(names))[:150]
311
+ with ThreadPoolExecutor(max_workers=8) as ex:
312
+ checked = list(ex.map(lambda x: (x, _registry_exists(*x)), names))
313
+ for (name, eco), ok in checked:
314
+ if ok is False:
315
+ (out['scoped_missing'] if name.startswith('@') else out['missing_packages']).append({'name': name, 'ecosystem': eco})
316
+
317
+ out['workflows'] = _workflow_issues(root)
318
+ out['docker'] = _docker_issues(root, files)
319
+ out['code'] = _code_issues(texts)
320
+ out['duration_s'] = round(time.monotonic() - t0, 1)
321
+ return out
322
+
323
+
324
+ CI_RULES = {
325
+ 'CI-PRT-CHECKOUT': (20, 'Tinggi', 'Workflow memakai pull_request_target sambil men-checkout kode dari pull request ({file}).',
326
+ 'Jangan checkout kode PR di pull_request_target; pakai pull_request, atau pisahkan job tanpa akses secret.'),
327
+ 'CI-SCRIPT-INJECTION': (15, 'Tinggi', 'Input dari pengguna dipakai langsung di workflow ({file}: {detail}).',
328
+ 'Simpan nilai itu ke variabel env lalu rujuk sebagai "$VAR" di skrip, bukan langsung lewat ekspresi ${{ }}.'),
329
+ 'CI-SECRET-ECHO': (10, 'Menengah', 'Workflow mencetak secret ke log ({file}).',
330
+ 'Jangan echo secret; GitHub hanya menyamarkan nilai yang persis sama.'),
331
+ 'CI-UNPINNED-ACTION': (3, 'Rendah', 'Action pihak ketiga tidak di-pin ke commit SHA ({file}: {detail}).',
332
+ 'Pin action ke SHA 40 karakter agar perubahan versi tidak masuk diam-diam.'),
333
+ 'CI-BROAD-PERMISSIONS': (3, 'Rendah', 'Workflow tidak membatasi permissions token ({file}).',
334
+ "Tambahkan 'permissions: contents: read' di tingkat atas dan naikkan per job bila perlu."),
335
+ }
336
+ DOCKER_RULES = {
337
+ 'DOCKER-ROOT': (10, 'Menengah', 'Container berjalan sebagai root ({file}).', 'Tambahkan pengguna non-root dan instruksi USER di Dockerfile.'),
338
+ 'DOCKER-SECRET-ENV': (15, 'Tinggi', 'Rahasia ditulis langsung di Dockerfile ({file}: {detail}).',
339
+ 'Gunakan secret saat runtime atau build secret, bukan ENV/ARG berisi nilai.'),
340
+ 'DOCKER-ENV-COPY': (10, 'Menengah', 'COPY . ikut menyalin file .env ke image ({file}).', 'Tambahkan .env ke .dockerignore.'),
341
+ }
342
+
343
+
344
+ def findings(r):
345
+ """Daftar temuan (id, poin, tingkat, teks, rekomendasi, kategori, track, keyakinan)."""
346
+ out = []
347
+ for x in r.get('secrets', []):
348
+ if x.get('severity') == 'critical':
349
+ out.append(('REPO-SECRET', 40, 'Kritis', f"Kunci rahasia di kode: {x['type']} ({x['masked']}, {x['file']}).",
350
+ 'Cabut (revoke) kunci itu, buat yang baru, simpan di variabel lingkungan atau secret manager, lalu hapus dari kode.',
351
+ 'code', 'beginner', x.get('confidence', 'high')))
352
+ else:
353
+ out.append(('REPO-PUBLIC-KEY', 3, 'Rendah', f"Kunci publik di kode: {x['type']} ({x['masked']}, {x['file']}).",
354
+ 'Pastikan kunci ini dibatasi (HTTP referrer, API yang diizinkan, kuota) di konsol penyedianya.', 'code', 'beginner', 'medium'))
355
+ for x in r.get('history_secrets', []):
356
+ if x.get('severity') == 'critical':
357
+ out.append(('REPO-SECRET-HISTORY', 40, 'Kritis',
358
+ f"Kunci rahasia masih tersimpan di riwayat git: {x['type']} ({x['masked']}, {x['file']}, commit {x.get('commit', '?')}).",
359
+ 'Cabut dan ganti kunci itu. Menghapusnya dari kode terbaru tidak menghapusnya dari riwayat, dan salinannya mungkin sudah tersebar.',
360
+ 'code', 'beginner', x.get('confidence', 'high')))
361
+ for n in r.get('env_files', []):
362
+ out.append(('REPO-ENV-COMMITTED', 40, 'Kritis', f'File {n} ter-commit ke repositori.',
363
+ 'Hapus dari git (git rm --cached), tambahkan ke .gitignore, lalu ganti semua rahasia di dalamnya.', 'code', 'beginner', 'high'))
364
+ for n in r.get('env_files_history', []):
365
+ out.append(('REPO-ENV-HISTORY', 15, 'Tinggi', f'File {n} pernah ter-commit dan masih ada di riwayat git.',
366
+ 'Ganti semua rahasia yang pernah ada di file itu.', 'code', 'beginner', 'high'))
367
+ used, extra = 0, 0
368
+ for d in r.get('deps', {}).get('vulnerable', []):
369
+ level, pts = SEV_POINTS.get(d['severity'], ('Menengah', 5))
370
+ if used + pts > DEP_POINTS_CAP:
371
+ extra += 1
372
+ continue
373
+ used += pts
374
+ summary = f": {d['summary']}" if d.get('summary') else ''
375
+ out.append(('DEP-VULN', pts, level, f"Dependensi rentan: {d['name']} {d['version']} ({d['ecosystem']}, {', '.join(d['ids'][:2])}){summary}.",
376
+ f"Perbarui {d['name']} ke versi yang sudah diperbaiki (lihat {d['ids'][0]} di osv.dev).", 'code', 'beginner', 'high'))
377
+ if extra:
378
+ out.append(('DEP-VULN-MORE', 0, 'Rendah', f'Ada {extra} dependensi rentan lain (tidak dihitung lagi ke skor agar tidak berlebihan).',
379
+ 'Jalankan npm audit atau pip-audit untuk daftar lengkap.', 'code', 'beginner', 'high'))
380
+ for x in r.get('missing_packages', []):
381
+ out.append(('PKG-NOT-FOUND', 20, 'Tinggi', f"Paket {x['name']} ({x['ecosystem']}) tidak ada di registry publik; nama paket bisa jadi dikarang AI.",
382
+ 'Pastikan nama paket benar. Penyerang bisa mendaftarkan nama yang belum ada lalu menyisipkan kode berbahaya.', 'code', 'beginner', 'high'))
383
+ if r.get('scoped_missing'):
384
+ names = ', '.join(x['name'] for x in r['scoped_missing'][:3])
385
+ out.append(('PKG-SCOPED-PRIVATE', 3, 'Rendah', f'Paket bercakupan tidak ada di registry publik ({names}).',
386
+ 'Bila itu paket privat, pastikan .npmrc mengarah ke registry privat untuk scope tersebut agar tidak terkena dependency confusion.',
387
+ 'code', 'senior', 'medium'))
388
+ if r.get('lockfile_missing'):
389
+ out.append(('LOCKFILE-MISSING', 3, 'Rendah', f"Tidak ada lockfile untuk {', '.join(r['lockfile_missing'][:3])}.",
390
+ 'Commit lockfile (package-lock.json, pnpm-lock.yaml, atau yarn.lock) agar versi dependensi terkunci.', 'code', 'senior', 'high'))
391
+ seen = set()
392
+ for x in r.get('workflows', []):
393
+ if x['id'] in ('CI-UNPINNED-ACTION', 'CI-BROAD-PERMISSIONS') and x['id'] in seen:
394
+ continue
395
+ seen.add(x['id'])
396
+ pts, level, text, rec = CI_RULES[x['id']]
397
+ out.append((x['id'], pts, level, text.format(file=x['file'], detail=x.get('detail', '')), rec, 'code', 'senior',
398
+ 'medium' if x['id'] == 'CI-SCRIPT-INJECTION' else 'high'))
399
+ for x in r.get('docker', []):
400
+ pts, level, text, rec = DOCKER_RULES[x['id']]
401
+ out.append((x['id'], pts, level, text.format(file=x['file'], detail=x.get('detail', '')), rec, 'ops', 'beginner', 'high'))
402
+ seen_vars = set()
403
+ for x in r.get('code', []):
404
+ if x['id'] == 'PUBLIC-ENV-SECRET' and x['detail'] not in seen_vars:
405
+ seen_vars.add(x['detail'])
406
+ out.append(('PUBLIC-ENV-SECRET', 15, 'Tinggi', f"Variabel rahasia diberi awalan publik: {x['detail']} ({x['file']}).",
407
+ 'Variabel berawalan NEXT_PUBLIC_, VITE_, atau REACT_APP_ ikut dikirim ke browser. Hapus awalannya dan pakai hanya di sisi server.',
408
+ 'code', 'beginner', 'high'))
409
+ debug = [x for x in r.get('code', []) if x['id'] == 'DEBUG-IN-CODE']
410
+ if debug:
411
+ out.append(('DEBUG-IN-CODE', 10, 'Menengah', f"Mode debug menyala di kode ({debug[0]['file']}: {debug[0]['detail']}).",
412
+ 'Matikan debug untuk produksi, misalnya lewat variabel lingkungan.', 'code', 'beginner', 'medium'))
413
+ return out
414
+
415
+
416
+ def score(r):
417
+ """Penilaian untuk pemeriksaan repositori saja (tanpa situs)."""
418
+ from scanner import CATEGORY_LABELS, SEVERITY, coverage_info, grade_from
419
+ items, total = [], 100
420
+ for fid, pts, level, text, rec, cat, track, conf in findings(r):
421
+ total -= pts
422
+ items.append({'id': fid, 'finding': f'Risiko {level}: {text}', 'title': text, 'recommendation': rec, 'poin': -pts,
423
+ 'points': -pts, 'level': level, 'severity': SEVERITY[level], 'category': cat, 'track': track, 'confidence': conf})
424
+ total = max(0, total)
425
+ criticals = sum(1 for x in items if x['severity'] == 'critical')
426
+ grade = grade_from(total, criticals)
427
+ if not items:
428
+ items.append({'id': 'NO-ISSUES', 'level': 'Info', 'severity': 'info', 'points': 0, 'category': 'code', 'track': 'beginner',
429
+ 'confidence': 'high', 'finding': 'Tidak ditemukan masalah pada pemeriksaan repositori.',
430
+ 'recommendation': 'Jalankan ulang secara berkala, misalnya di CI.'})
431
+ subscores = {c: (max(0, 100 + sum(x['points'] for x in items if x['category'] == c)) if c in ('code', 'ops') else None)
432
+ for c in CATEGORY_LABELS}
433
+ return {'score': total, 'grade': grade, 'grade_capped': grade != grade_from(total, 0), 'findings': items,
434
+ 'coverage': coverage_info(('repo',)), 'subscores': subscores, 'category_labels': CATEGORY_LABELS}