repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(check): add secret scan ignore config

144e9081bcff1490529bb33ccea817c9f516502c

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (5)

mise.toml

Modified
65 65 [tasks.check]
66 66 description = "Static extension checks"
67 -run = "npm run check"
67 +run = [
68 + "python3 scripts/check_skills.py",
69 + "python3 scripts/test_extensions_static_check.py",
70 + "python3 scripts/extensions.py check",
69 73 [tasks.ci]
70 74 description = "Release gate"

package.json

Modified
12 12 "test": "vitest run",
13 13 "test:watch": "vitest",
14 14 "test:brainstorming": "node --test skills/super/brainstorming/scripts/companion.test.mjs",
15 - "typecheck": "tsc --noEmit",
16 - "check": "python3 scripts/check_skills.py && python3 scripts/extensions.py check"
15 + "typecheck": "tsc --noEmit"
18 17 "devDependencies": {
19 18 "@playwright/test": "^1.60.0",

scripts/extensions.json

Added
new file mode 100644
1 +{
2 + "secretScanIgnore": ["extensions/leaks/__tests__/ux.test.ts"]
3 +}

scripts/extensions.py

Modified
32 32 r"(?:import|export)\s+(?:[^'\"]+?\s+from\s+)?['\"]([^'\"]+)['\"]|import\(['\"]([^'\"]+)['\"]\)"
34 34 SECRET_RE = re.compile(r"(api[_-]?key|secret|token|password)\s*[:=]\s*['\"][^'\"]{12,}", re.I)
35 +SECRET_SCAN_IGNORE_KEY = "secretScanIgnore"
35 36 OSC_RE = re.compile(r"\x1b\][^\x07]*(?:\x07|\x1b\\)")
36 37 ANSI_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]")
239 240 errors.append(f"{ext.name}: {msg}")
243 +def secret_scan_ignore_paths(root: Path = ROOT) -> set[Path]:
244 + try:
245 + config = json.loads((root / "scripts" / "extensions.json").read_text(encoding="utf-8"))
246 + except (OSError, json.JSONDecodeError):
247 + return set()
248 + paths = config.get(SECRET_SCAN_IGNORE_KEY)
249 + if not isinstance(paths, list):
250 + return set()
251 + return {Path(path) for path in paths if isinstance(path, str) and path}
254 +def is_secret_scan_ignored(file: Path, ignores: set[Path], root: Path = ROOT) -> bool:
255 + try:
256 + rel = file.resolve().relative_to(root.resolve())
257 + except ValueError:
258 + return False
259 + return rel in ignores
242 262 def static_check(ext: Path) -> list[str]:
263 + secret_scan_ignores = secret_scan_ignore_paths()
243 264 errors: list[str] = []
245 266 pkg = read_pkg(ext)
312 333 f"cross-extension relative import {spec} in {file.relative_to(ext)}",
314 - if SECRET_RE.search(text):
335 + if SECRET_RE.search(text) and not is_secret_scan_ignored(file, secret_scan_ignores):
315 336 err(errors, ext, f"possible secret in {file.relative_to(ext)}")
317 338 for dep in sorted(packages & PI_PEERS):

scripts/test_extensions_static_check.py

Added
new file mode 100644
1 +#!/usr/bin/env python3
3 +from __future__ import annotations
5 +import importlib.util
6 +import json
7 +import tempfile
8 +import unittest
9 +from pathlib import Path
11 +SCRIPT = Path(__file__).with_name("extensions.py")
12 +SPEC = importlib.util.spec_from_file_location("extensions", SCRIPT)
13 +assert SPEC is not None and SPEC.loader is not None
14 +extensions = importlib.util.module_from_spec(SPEC)
15 +SPEC.loader.exec_module(extensions)
18 +class SecretScanIgnoreTest(unittest.TestCase):
19 + def test_reads_exact_secret_scan_ignore_paths_from_script_config(self) -> None:
20 + with tempfile.TemporaryDirectory() as tmp:
21 + root = Path(tmp)
22 + config_dir = root / "scripts"
23 + config_dir.mkdir()
24 + (config_dir / "extensions.json").write_text(
25 + json.dumps(
26 + {
27 + "secretScanIgnore": [
28 + "extensions/leaks/__tests__/ux.test.ts",
29 + ],
30 + }
31 + ),
32 + encoding="utf-8",
33 + )
35 + ignores = extensions.secret_scan_ignore_paths(root)
37 + self.assertEqual(
38 + ignores,
39 + {Path("extensions/leaks/__tests__/ux.test.ts")},
40 + )
42 + def test_secret_scan_ignore_requires_exact_repo_relative_path(self) -> None:
43 + ignores = {Path("extensions/leaks/__tests__/ux.test.ts")}
44 + root = Path("/repo")
46 + self.assertTrue(
47 + extensions.is_secret_scan_ignored(
48 + root / "extensions/leaks/__tests__/ux.test.ts",
49 + ignores,
50 + root,
51 + )
52 + )
53 + self.assertFalse(
54 + extensions.is_secret_scan_ignored(
55 + root / "extensions/leaks/index.ts",
56 + ignores,
57 + root,
58 + )
59 + )
62 +if __name__ == "__main__":
63 + unittest.main()