vibeaudit 0.9.3__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.
Files changed (66) hide show
  1. vibeaudit/__init__.py +41 -0
  2. vibeaudit/__main__.py +5 -0
  3. vibeaudit/accessscan.py +140 -0
  4. vibeaudit/agentscan.py +150 -0
  5. vibeaudit/aiscan.py +116 -0
  6. vibeaudit/apiscan.py +98 -0
  7. vibeaudit/bundle.py +260 -0
  8. vibeaudit/cli.py +600 -0
  9. vibeaudit/compliance.py +136 -0
  10. vibeaudit/config.py +124 -0
  11. vibeaudit/contracts.py +129 -0
  12. vibeaudit/data/compliance_map.json +256 -0
  13. vibeaudit/data/npm_top.txt.gz +0 -0
  14. vibeaudit/data/pypi_top.txt.gz +0 -0
  15. vibeaudit/data/retired_models.txt +37 -0
  16. vibeaudit/data/threat_data.json +170 -0
  17. vibeaudit/datascan.py +442 -0
  18. vibeaudit/deps.py +737 -0
  19. vibeaudit/explain.py +274 -0
  20. vibeaudit/external.py +169 -0
  21. vibeaudit/feed.py +256 -0
  22. vibeaudit/feedback.py +72 -0
  23. vibeaudit/files.py +537 -0
  24. vibeaudit/fix.py +831 -0
  25. vibeaudit/gitdiff.py +89 -0
  26. vibeaudit/guard.py +170 -0
  27. vibeaudit/heuristics.py +215 -0
  28. vibeaudit/hook.py +207 -0
  29. vibeaudit/iacscan.py +164 -0
  30. vibeaudit/jsscan.py +1229 -0
  31. vibeaudit/lexer.py +302 -0
  32. vibeaudit/llm.py +236 -0
  33. vibeaudit/lsp.py +335 -0
  34. vibeaudit/mcp_server.py +499 -0
  35. vibeaudit/migrations.py +260 -0
  36. vibeaudit/model.py +117 -0
  37. vibeaudit/monorepo.py +153 -0
  38. vibeaudit/opsscan.py +146 -0
  39. vibeaudit/plugins.py +143 -0
  40. vibeaudit/projectscan.py +456 -0
  41. vibeaudit/promptscan.py +173 -0
  42. vibeaudit/pyscan.py +1292 -0
  43. vibeaudit/qualityscan.py +593 -0
  44. vibeaudit/relscan.py +133 -0
  45. vibeaudit/report.py +410 -0
  46. vibeaudit/review.py +216 -0
  47. vibeaudit/rules.py +892 -0
  48. vibeaudit/scanner.py +327 -0
  49. vibeaudit/secretscan.py +273 -0
  50. vibeaudit/slopscan.py +256 -0
  51. vibeaudit/stdlib.py +26 -0
  52. vibeaudit/storagescan.py +193 -0
  53. vibeaudit/store.py +131 -0
  54. vibeaudit/studio.py +254 -0
  55. vibeaudit/studio_assets/studio.css +50 -0
  56. vibeaudit/studio_assets/studio.html +28 -0
  57. vibeaudit/studio_assets/studio.js +166 -0
  58. vibeaudit/uiscan.py +399 -0
  59. vibeaudit/updater.py +149 -0
  60. vibeaudit/watch.py +89 -0
  61. vibeaudit-0.9.3.dist-info/METADATA +803 -0
  62. vibeaudit-0.9.3.dist-info/RECORD +66 -0
  63. vibeaudit-0.9.3.dist-info/WHEEL +5 -0
  64. vibeaudit-0.9.3.dist-info/entry_points.txt +3 -0
  65. vibeaudit-0.9.3.dist-info/licenses/LICENSE +21 -0
  66. vibeaudit-0.9.3.dist-info/top_level.txt +1 -0
vibeaudit/__init__.py ADDED
@@ -0,0 +1,41 @@
1
+ """vibeaudit: security and quality audit for AI-generated and LLM-powered applications.
2
+
3
+ Library use:
4
+
5
+ from vibeaudit import scan, project_options
6
+ result = scan("path/to/project") # the project's .vibeaudit.toml / [tool.vibeaudit] applies
7
+ result = scan("path/to/project", project_options("path/to/project", offline=True)) # with overrides
8
+ for finding in result.findings:
9
+ print(finding.severity, finding.rule_id, finding.file, finding.line, finding.message)
10
+
11
+ An explicit `Options(...)` is used exactly as given, without the project's configuration file.
12
+ Custom rules: see `vibeaudit.plugins` (`rule`, `route_rule`, `file_rule`).
13
+ """
14
+
15
+ __version__ = "0.9.3"
16
+
17
+ __all__ = ["__version__", "scan", "Options", "project_options", "Result", "Finding", "Route", "rule", "route_rule", "file_rule"]
18
+
19
+
20
+ def __getattr__(name): # lazy, so `import vibeaudit` stays cheap
21
+ if name == "project_options":
22
+ from .config import project_options
23
+
24
+ return project_options
25
+ if name in ("scan", "Options"):
26
+ from . import scanner
27
+
28
+ return getattr(scanner, name)
29
+ if name == "Result":
30
+ from .report import Result
31
+
32
+ return Result
33
+ if name in ("Finding", "Route"):
34
+ from . import model
35
+
36
+ return getattr(model, name)
37
+ if name in ("rule", "route_rule", "file_rule"):
38
+ from . import plugins
39
+
40
+ return getattr(plugins, name)
41
+ raise AttributeError(f"module 'vibeaudit' has no attribute {name!r}")
vibeaudit/__main__.py ADDED
@@ -0,0 +1,5 @@
1
+ import sys
2
+
3
+ from .cli import main
4
+
5
+ sys.exit(main())
@@ -0,0 +1,140 @@
1
+ """Access-control checks added in 0.8.7: AUTH015-AUTH017 and FE008 (all preview).
2
+
3
+ AUTH015: a password change or reset that leaves the other sessions and refresh tokens valid.
4
+ AUTH016: a signed-in route that reads or changes a tenant-scoped table without filtering by tenant (extends AUTH003).
5
+ AUTH017: an admin or feature-flag endpoint that any signed-in user can call (no role check).
6
+ FE008: a server action that returns a server secret, or closes over one (Next.js sends closures to the client).
7
+ """
8
+ from __future__ import annotations
9
+
10
+ import re
11
+ from typing import Callable, Dict, List, Set
12
+
13
+ from .files import Project, SourceFile
14
+ from .guard import GUARD, finditer
15
+ from .model import Route
16
+
17
+ Add = Callable[..., None] # ProjectScanner.add(sf_or_rel, line, rule, message, severity="")
18
+
19
+ PASSWORD_ROUTE = re.compile(r"(?i)(?:reset|change|update|set|new|forgot)[-_/]?password|password[-_/]?(?:reset|change|update)")
20
+ PASSWORD_WRITE = re.compile(
21
+ r"(?i)\bset_password\s*\(|\w{0,20}password\w{0,10}['\"]?\s*[:=]\s*(?:await\s+)?[\w.]{0,40}(?:hash|crypt|argon|make_password)"
22
+ r"|\bpassword\s*[=:]\s*(?:body|data|payload|form|request|req|input|params|values)\w{0,10}\.\w{0,20}password"
23
+ r"|\bupdateUser\s*\(\s*\{[^}]{0,200}\bpassword\b|\bupdate_user\s*\([^)]{0,200}\bpassword\b|\bupdatePassword\s*\(")
24
+ REVOKE = re.compile(
25
+ r"(?i)(?:revoke|invalidat\w{0,10}|expire)\w{0,10}[ \t(._]{0,3}\w{0,20}(?:sessions?|tokens?|refresh|all)"
26
+ r"|log_?out_?(?:all|other|everywhere)|sign_?out\s*\(\s*\{?\s*scope|delete\w*sessions?"
27
+ # session records (a Session model, a sessions table or store), not an ORM's database session
28
+ r"|(?-i:Session|Sessions|sessions|sessionStore|session_store|UserSession\w*)\s*\.\s*(?:delete\w*|destroy\w*|remove\w*)\s*\("
29
+ r"|update_session_auth_hash|token_?version|password_?changed_?at|security_?stamp"
30
+ r"|refresh_?tokens?\s*\.\s*(?:delete|destroy|remove)|deny_?list|block_?list|black_?list|logout_user|deleteMany\s*\(\s*\{\s*where\s*:\s*\{\s*userId")
31
+ # frameworks that drop the other sessions on a password change by themselves
32
+ SELF_REVOKING = re.compile(r"(?i)\bfrom\s+django\b|\bimport\s+django\b|firebase|@clerk/|next-auth.{0,40}database|\bdevise\b")
33
+
34
+ TENANT_COL = re.compile(r"(?i)\b(?:tenant|org|organi[sz]ation|team|workspace|company)_?id\b|\b(?:tenantId|orgId|organizationId|teamId|workspaceId|companyId)\b")
35
+ TENANT_WORD = re.compile(
36
+ r"(?i)tenant|\borg(?:ani[sz]ation)?_?id\b|\borgId\b|organi[sz]ation|team_?id|teamId|workspace|company_?id|companyId"
37
+ r"|current_?org|currentOrg|\.org\b|\.team\b|scoped|for_tenant|forTenant")
38
+ SQL_TABLE = re.compile(r"(?is)\bcreate\s+table\s+(?:if\s+not\s+exists\s+)?(?:\"?\w+\"?\.)?\"?(\w+)\"?\s*\(([^;]{0,6000}?)\)\s*;")
39
+ PRISMA_MODEL = re.compile(r"(?m)^model\s+(\w+)\s*\{([^}]{0,6000})\}")
40
+ PY_MODEL = re.compile(r"(?m)^class\s+(\w+)\s*\([^)]*(?:Model|Base|SQLModel|Document)[^)]*\)\s*:\n((?:[ \t]+[^\n]*\n|[ \t]*\n){1,200})")
41
+ RLS = re.compile(r"(?i)enable\s+row\s+level\s+security")
42
+ DB_TOUCH = r"(?:find\w*|filter|get|all|select|update\w*|delete\w*|insert|create\w*|query|exec\w*|raw|aggregate|count)"
43
+
44
+ ADMIN_ROUTE = re.compile(r"(?i)/admin\b|/feature[-_]?flags?\b|/flags?\b|/toggles?\b|/maintenance\b|/site[-_]?settings\b")
45
+ ROLE_CHECK = re.compile(
46
+ r"(?i)admin|\brole|is_?staff|is_?superuser|superuser|permission|\bcan\s*\(|authori[sz]e|ability|polic(?:y|ies)|has_?perm"
47
+ r"|requireRole|require_role|is_?owner|isOwner|owner_?only|staff_?member_required|user_passes_test|\bscopes?\b|\bclaims?\b")
48
+
49
+ USE_SERVER_FILE = re.compile(r"""\A\s*(?:/\*[\s\S]*?\*/\s*|//[^\n]*\n\s*)*['"]use server['"]""")
50
+ SECRET_ENV = re.compile(r"\bprocess\.env\.(?!NEXT_PUBLIC_)(\w*(?:KEY|SECRET|TOKEN|PASSWORD|PRIVATE)\w*)")
51
+ RETURN_SECRET = re.compile(r"\breturn\b[^;\n]{0,200}?\bprocess\.env\.(?!NEXT_PUBLIC_)(\w*(?:KEY|SECRET|TOKEN|PASSWORD|PRIVATE)\w*)")
52
+ INLINE_ACTION = re.compile(r"""\b(?:async\s+function\s+\w+\s*\([^)]{0,200}\)|=\s*async\s*\([^)]{0,200}\)\s*=>)\s*\{\s*['"]use server['"]""")
53
+
54
+
55
+ def run(project: Project, routes: List[Route], add: Add) -> None:
56
+ files = [f for f in project.files if f.text is not None and not f.is_test and not f.is_example]
57
+ GUARD.run("<project>", "password changes", _password_changes, project, routes, add)
58
+ GUARD.run("<project>", "tenant scoping", _tenant_scoping, project, files, routes, add)
59
+ for r in routes:
60
+ if ADMIN_ROUTE.search(r.path) and r.has_auth and not ROLE_CHECK.search(r.body) \
61
+ and set(m.upper() for m in r.methods) & {"POST", "PUT", "PATCH", "DELETE"}:
62
+ add(r.file, r.line, "AUTH017", f"{r.path} changes admin or feature-flag state, and any signed-in user can call it: "
63
+ "nothing checks a role.")
64
+ for sf in files:
65
+ if sf.kind == "js":
66
+ GUARD.run(sf.rel, "server action secrets", _server_actions, sf, add)
67
+
68
+
69
+ def _password_changes(project: Project, routes: List[Route], add: Add) -> None:
70
+ code = "\n".join(f.text or "" for f in project.files if f.kind in ("py", "js") and not f.is_test)
71
+ if SELF_REVOKING.search(code) or REVOKE.search(code):
72
+ return # sessions are dropped somewhere (or by the framework); per route that is too fine to tell
73
+ for r in routes:
74
+ if PASSWORD_ROUTE.search(r.path + " " + r.body[:300]) and PASSWORD_WRITE.search(r.body):
75
+ add(r.file, r.line, "AUTH015", f"{r.path} sets a new password but keeps every existing session and refresh token "
76
+ "valid, so whoever had the account stays signed in.")
77
+
78
+
79
+ def _tenant_tables(project: Project) -> Set[str]:
80
+ tables: Set[str] = set()
81
+ for sf in project.files:
82
+ text = sf.text or ""
83
+ if sf.kind == "sql" or sf.rel.endswith(".sql"):
84
+ for m in finditer(SQL_TABLE, text):
85
+ if TENANT_COL.search(m.group(2)):
86
+ tables.add(m.group(1).lower())
87
+ elif sf.rel.endswith(".prisma"):
88
+ for m in finditer(PRISMA_MODEL, text):
89
+ if TENANT_COL.search(m.group(2)):
90
+ tables.add(m.group(1).lower())
91
+ elif sf.kind == "py" and not sf.is_test:
92
+ for m in finditer(PY_MODEL, text):
93
+ if TENANT_COL.search(m.group(2)):
94
+ tables.add(m.group(1).lower())
95
+ return tables
96
+
97
+
98
+ def _tenant_scoping(project: Project, files: List[SourceFile], routes: List[Route], add: Add) -> None:
99
+ tables = _tenant_tables(project)
100
+ if not tables:
101
+ return
102
+ rls = any(RLS.search(f.text or "") for f in project.files if f.rel.endswith(".sql"))
103
+ names = "|".join(sorted(re.escape(t) for t in tables))
104
+ touch = re.compile(
105
+ r"(?i)(?:\b(?:%s)s?\s*\.\s*(?:objects\s*\.\s*)?%s\s*\(|\b(?:query|select|filter)\s*\(\s*(?:%s)\b"
106
+ r"|\.from\s*\(\s*['\"`](?:%s)s?['\"`]\s*\)|\b(?:from|into|update)\s+\"?(?:%s)s?\"?\b)"
107
+ % (names, DB_TOUCH, names, names, names))
108
+ for r in routes:
109
+ if not r.has_auth or TENANT_WORD.search(r.body):
110
+ continue
111
+ m = touch.search(r.body)
112
+ if not m or (rls and ".from(" in m.group(0)):
113
+ continue # Supabase queries under row level security are filtered by the database
114
+ add(r.file, r.line, "AUTH016", f"{r.path} queries a tenant-scoped table without filtering by tenant, so users "
115
+ "can read or change other organizations' rows.")
116
+
117
+
118
+ def _server_actions(sf: SourceFile, add: Add) -> None:
119
+ v = sf.cache.get("views")
120
+ if v is None or "use server" not in (sf.text or ""):
121
+ return
122
+ code = v.code # type: ignore[attr-defined]
123
+ if USE_SERVER_FILE.search(sf.text or ""):
124
+ for m in finditer(RETURN_SECRET, code):
125
+ add(sf, sf.line_at(m.start()), "FE008", f"A server action returns `{m.group(1)}`; every client that calls it "
126
+ "receives the secret.")
127
+ secrets: Dict[str, str] = {}
128
+ for m in finditer(re.compile(r"\b(?:const|let)\s+(\w+)\s*=\s*process\.env\.(?!NEXT_PUBLIC_)(\w*(?:KEY|SECRET|TOKEN|PASSWORD|PRIVATE)\w*)"), code):
129
+ secrets[m.group(1)] = m.group(2)
130
+ if not secrets:
131
+ return
132
+ for m in finditer(INLINE_ACTION, code):
133
+ brace = code.find("{", m.start())
134
+ body = code[brace:brace + 2000]
135
+ for name, env in secrets.items():
136
+ if re.search(r"\b%s\b" % re.escape(name), body):
137
+ add(sf, sf.line_at(m.start()), "FE008",
138
+ f"An inline server action closes over `{name}` ({env}); Next.js sends closed-over values to the "
139
+ "browser (encrypted) and back. Read the secret inside the action instead.")
140
+ break
vibeaudit/agentscan.py ADDED
@@ -0,0 +1,150 @@
1
+ """AI agent and LLM checks: LLM016-LLM018 (added in 0.8.1) and LLM025 (all preview)."""
2
+ from __future__ import annotations
3
+
4
+ import re
5
+ from typing import Callable, Iterable, List, Tuple
6
+
7
+ from . import heuristics as H
8
+ from .files import Project, SourceFile
9
+ from .guard import GUARD, finditer
10
+ from .model import Route
11
+ from .store import loads_json
12
+
13
+ Add = Callable[..., None] # ProjectScanner.add(sf_or_rel, line, rule, message, severity="")
14
+
15
+ AGENT_SETTINGS = re.compile(
16
+ r"(?:^|/)(?:\.claude/settings(?:\.local)?\.json|\.cursor/(?:mcp|settings|cli)\.json|\.vscode/(?:settings|mcp)\.json"
17
+ r"|\.gemini/settings\.json|\.mcp\.json|mcp\.json|\.codex/config\.toml|\.windsurf/[\w.-]+\.json|\.roo/mcp\.json)$")
18
+ # files a coding agent reads as instructions from the developer
19
+ AGENT_INSTRUCTIONS = re.compile(
20
+ r"(?:^|/)(?:(?:CLAUDE|AGENTS|GEMINI)(?:\.local)?\.md|\.cursorrules|\.windsurfrules|\.clinerules"
21
+ r"|\.github/copilot-instructions\.md|\.github/instructions/[^/]+\.md|\.cursor/rules/[^/]+\.mdc?"
22
+ r"|\.windsurf/rules/[^/]+\.md|\.claude/(?:commands|agents)/[^/]+\.md|\.claude/skills/.+/SKILL\.md)$")
23
+ SKIP_PERMISSION_FLAGS = re.compile(
24
+ r"--dangerously-skip-permissions\b|--dangerously-bypass-approvals-and-sandbox\b|--yolo\b"
25
+ r"|--permission-mode[ =]+bypassPermissions\b|--approval-mode[ =]+yolo\b")
26
+ RUNS_COMMANDS = re.compile(r"\.(?:sh|bash|zsh|ps1|cmd|bat|mk|toml)$", re.I)
27
+ CODEX_UNSAFE = re.compile( # [ \t]* not \s*: with (?m)^ a \s* would rescan every following blank line
28
+ r"""(?m)^[ \t]*(?:approval_policy[ \t]*=[ \t]*["']never["']|sandbox_mode[ \t]*=[ \t]*["']danger-full-access["'])""")
29
+ # shell or every-tool grants: "Bash", "Bash(*)", "Bash(:*)", "*"
30
+ BROAD_TOOL = re.compile(r"^(?:Bash|Shell|run_terminal_cmd|terminal|\*|mcp__\*|Bash\(\s*\*?\s*(?::\s*\*)?\s*\))$")
31
+
32
+ STREAM_PY = re.compile(r"\bstream\s*=\s*True\b|\.stream\s*\(|\bastream\w*\s*\(")
33
+ STREAM_RESPONSE_PY = re.compile(r"\b(?:StreamingResponse|EventSourceResponse|stream_with_context)\s*\(")
34
+ DISCONNECT_PY = re.compile(r"is_disconnected|CancelledError|client_disconnected|ClientDisconnect|GeneratorExit")
35
+ STREAM_JS = re.compile(r"\bstream\s*:\s*true\b|\bstreamText\s*\(|\bstreamObject\s*\(|\.stream\s*\(|\bcreateDataStream\w*\s*\(")
36
+ DISCONNECT_JS = re.compile(r"\b(?:abortSignal|signal)\s*[:,)]|\bAbortController\b|\.on\(\s*['\"]close['\"]|\brequest\.signal\b|\breq\.signal\b"
37
+ r"|\bonAbort\b|\bcancel\s*\(")
38
+
39
+ LOG_CALL = re.compile(
40
+ r"\b(?:logger|logging|log|_logger|LOGGER|console|app\.logger|self\.logger)\s*\.\s*(?:debug|info|log|warn|warning|error|exception)\s*\(")
41
+ LLM_TEXT_NAME = re.compile(
42
+ r"(?<![\w.'\"])(?:prompt|prompts|user_prompt|system_prompt|full_prompt|messages|chat_history|completion|completions"
43
+ r"|llm_response|model_output|response\.choices|\w+\.choices\[\d+\]\.message(?:\.content)?)(?![\w'\"])")
44
+
45
+
46
+ def run(project: Project, routes: List[Route], add: Add) -> None:
47
+ for sf in project.files:
48
+ if sf.text is None or sf.is_test:
49
+ continue
50
+ GUARD.run(sf.rel, "agent file checks", _file, sf, add)
51
+ for r in routes:
52
+ _stream_cancel(r, add)
53
+
54
+
55
+ def _file(sf: SourceFile, add: Add) -> None:
56
+ if AGENT_SETTINGS.search(sf.rel):
57
+ _agent_settings(sf, add)
58
+ if AGENT_INSTRUCTIONS.search(sf.rel):
59
+ _injection(sf, add)
60
+ # files that run commands (not docs that only mention the flag)
61
+ if sf.kind in ("yaml", "workflow", "docker", "compose") or RUNS_COMMANDS.search(sf.rel) \
62
+ or sf.rel.rsplit("/", 1)[-1] in ("package.json", "Makefile", "justfile", "Procfile"):
63
+ for m in finditer(SKIP_PERMISSION_FLAGS, sf.text or ""):
64
+ add(sf, sf.line_at(m.start()), "LLM016", f"A coding agent is started with `{m.group(0)}`, so it runs every tool without asking.")
65
+ v = sf.cache.get("views")
66
+ if v is not None and sf.kind in ("py", "js"):
67
+ _logged_llm_text(sf, v.code, v.bare, add) # type: ignore[attr-defined]
68
+
69
+
70
+ def _injection(sf: SourceFile, add: Add) -> None:
71
+ from . import feed
72
+
73
+ for entry, pattern in feed.injection_patterns():
74
+ m = next(iter(finditer(pattern, sf.text or "")), None) # one finding per pattern and file
75
+ if m is not None:
76
+ add(sf, sf.line_at(m.start()), "LLM025",
77
+ f"{entry['description']} ({entry['id']}): the agent reads this file as the developer's instructions.")
78
+
79
+
80
+ def _agent_settings(sf: SourceFile, add: Add) -> None:
81
+ text = sf.text or ""
82
+ if sf.rel.endswith(".toml"):
83
+ for m in finditer(CODEX_UNSAFE, text):
84
+ add(sf, sf.line_at(m.start()), "LLM016", "The Codex agent runs commands without approval or outside its sandbox.")
85
+ return
86
+ try:
87
+ data = loads_json(text)
88
+ except ValueError:
89
+ return
90
+ if not isinstance(data, dict):
91
+ return
92
+
93
+ def line_of(needle: str) -> int:
94
+ return next((i for i, ln in enumerate(sf.lines, 1) if needle in ln), 1)
95
+
96
+ perms = data.get("permissions") if isinstance(data.get("permissions"), dict) else {}
97
+ if perms.get("defaultMode") == "bypassPermissions":
98
+ add(sf, line_of("bypassPermissions"), "LLM016", "The agent's permission mode is bypassPermissions: it never asks before acting.")
99
+ for tool in _strings(perms.get("allow")):
100
+ if BROAD_TOOL.match(tool.strip()):
101
+ add(sf, line_of(f'"{tool}"'), "LLM016", f"`{tool}` lets the agent run any shell command without asking.")
102
+ for key, value in _walk(data):
103
+ low = key.lower()
104
+ if low in ("chat.tools.autoapprove", "chat.tools.global.autoapprove", "github.copilot.chat.agent.autoapprove",
105
+ "autoaccept", "yolo", "alwaysallowexecute") and value is True:
106
+ add(sf, line_of(f'"{key}"'), "LLM016", f"`{key}` is on: the agent runs tools without asking.")
107
+ elif low in ("autoapprove", "alwaysallow") and isinstance(value, list) and any(str(v).strip() in ("*", "**") for v in value):
108
+ add(sf, line_of(f'"{key}"'), "LLM016", "Every tool of this MCP server is approved in advance.")
109
+
110
+
111
+ def _strings(value) -> List[str]:
112
+ return [v for v in value if isinstance(v, str)] if isinstance(value, list) else []
113
+
114
+
115
+ def _walk(value, depth: int = 0) -> Iterable[Tuple[str, object]]:
116
+ if depth > 8:
117
+ return
118
+ if isinstance(value, dict):
119
+ for k, v in value.items():
120
+ yield str(k), v
121
+ yield from _walk(v, depth + 1)
122
+ elif isinstance(value, list):
123
+ for v in value[:500]:
124
+ yield from _walk(v, depth + 1)
125
+
126
+
127
+ def _stream_cancel(r: Route, add: Add) -> None:
128
+ body = r.body
129
+ if not r.calls_llm:
130
+ return
131
+ if r.lang == "py":
132
+ if STREAM_PY.search(body) and STREAM_RESPONSE_PY.search(body) and not DISCONNECT_PY.search(body):
133
+ add(r.file, r.line, "LLM017", f"{r.path} streams a completion but never checks for a closed connection, so the model keeps generating.")
134
+ elif STREAM_JS.search(body) and not DISCONNECT_JS.search(body):
135
+ add(r.file, r.line, "LLM017", f"{r.path} streams a completion without passing the request's abort signal to the model call.")
136
+
137
+
138
+ def _logged_llm_text(sf: SourceFile, code: str, bare: str, add: Add) -> None:
139
+ if not (H.LLM_CALL_JS.search(code) if sf.kind == "js" else re.search(r"openai|anthropic|genai|langchain|litellm|llama_index|ollama", code)):
140
+ return
141
+ seen = set()
142
+ for m in finditer(LOG_CALL, bare):
143
+ end = code.find("\n", m.end())
144
+ args = bare[m.end():end if end > 0 else len(bare)][:300]
145
+ n = LLM_TEXT_NAME.search(args)
146
+ line = sf.line_at(m.start())
147
+ if n and line not in seen and not re.search(r"\blen\s*\(\s*$|\.length\s*$", args[:n.start()]) \
148
+ and not re.match(r"\s*\.\s*(?:length|count)\b|\s*\)\s*\.\s*length", args[n.end():]):
149
+ seen.add(line)
150
+ add(sf, line, "LLM018", f"`{n.group(0)}` is written to the logs: prompts and completions often hold personal data.")
vibeaudit/aiscan.py ADDED
@@ -0,0 +1,116 @@
1
+ """More AI checks: vector search without a tenant filter, retired model ids, and agents that combine untrusted
2
+ input with dangerous tools (the "lethal trifecta")."""
3
+ from __future__ import annotations
4
+
5
+ import datetime as dt
6
+ import re
7
+ from typing import Callable, List
8
+
9
+ from .files import Project, SourceFile
10
+ from .guard import GUARD, finditer
11
+ from .model import Finding, Route
12
+
13
+ Emit = Callable[[Finding], None]
14
+
15
+ VECTOR_QUERY = re.compile(
16
+ r"\.\s*similarity_search(?:_with_score|_by_vector)?\s*\(|\.\s*as_retriever\s*\(|\.\s*max_marginal_relevance_search\s*\("
17
+ r"|\bcollection\s*\.\s*query\s*\(|\bindex\s*\.\s*query\s*\(|\.\s*search\s*\(\s*collection_name\b"
18
+ r"|\.\s*rpc\s*\(\s*['\"]match_\w*['\"]|\bORDER\s+BY\s+\w*embedding\w*\s*(?:<->|<=>|<#>)", re.I)
19
+ TENANT_FILTER = re.compile(r"(?i)\bfilter\b|\bwhere\b|\bnamespace\b|query_filter|user_?id|tenant|org_?id|owner|account_?id") # search_kwargs alone is not a filter
20
+ SERVICE_ROLE = re.compile(r"(?i)service_?role|supabaseAdmin|admin_?client")
21
+ _RETIRED: List["re.Pattern[str]"] = []
22
+
23
+
24
+ def retired_models() -> "re.Pattern[str]":
25
+ """A quoted model id from data/retired_models.txt or the threat feed's sunset calendar (both refreshed by
26
+ `vibeaudit update`); `*` is any id suffix."""
27
+ if not _RETIRED:
28
+ from . import feed
29
+ from .updater import data_bytes, model_ids
30
+
31
+ ids = set(model_ids(data_bytes("retired_models.txt"))) | set(feed.sunsets())
32
+ alts = sorted((re.escape(i[:-1]) + r"[\w.@-]*" if i.endswith("*") else re.escape(i) for i in ids), key=len, reverse=True)
33
+ _RETIRED.append(re.compile(r"""['"`](""" + "|".join(alts) + r""")['"`]"""))
34
+ return _RETIRED[0]
35
+ AGENT = re.compile(r"\b(?:create_react_agent|initialize_agent|AgentExecutor|create_openai_tools_agent|create_tool_calling_agent"
36
+ r"|createReactAgent|createToolCallingAgent|AgentExecutor\.fromAgentAndTools|Agent)\s*\(|\bnew\s+Agent\s*\(")
37
+ TOOL_NAME = re.compile(r"""@tool(?:\(\s*['"]([\w-]+)['"])?\s*\n\s*(?:async\s+)?def\s+(\w+)|\bTool\s*\(\s*name\s*=\s*['"]([\w-]+)['"]"""
38
+ r"""|\b(\w+)\s*:\s*tool\s*\(|name\s*:\s*['"]([\w-]+)['"]\s*,\s*description""")
39
+ UNTRUSTED_TOOL = re.compile(r"(?i)search|browse|browser|fetch|scrape|crawl|web|url|read_page|read_?email|inbox|rss|youtube|wikipedia|tavily|serp|duckduckgo|http_get|requests_get|read_file|download")
40
+ DANGEROUS_TOOL = re.compile(r"(?i)shell|bash|terminal|exec|python|run_code|run_command|send_?email|email_?send|write_?file|delete|remove|sql|query_?db|transfer|pay|post_|tweet|slack_?send|create_?issue|git_?push|deploy")
41
+
42
+
43
+ def _retired_message(model: str) -> str:
44
+ from . import feed
45
+
46
+ entry = feed.sunsets().get(model)
47
+ if entry is None:
48
+ return f"Model `{model}` has been retired or deprecated by its provider."
49
+ when = "was retired on" if entry["date"] <= dt.date.today().isoformat() else "is scheduled for retirement on"
50
+ instead = f"; the provider recommends `{entry['replacement']}`" if entry.get("replacement") else ""
51
+ return f"Model `{model}` {when} {entry['date']}{instead}."
52
+
53
+
54
+ class AiScanner:
55
+ def __init__(self, project: Project, emit: Emit, routes: List[Route]):
56
+ self.p = project
57
+ self.emit = emit
58
+ self.routes = routes
59
+ self.retired_seen: set = set()
60
+
61
+ def add(self, sf: SourceFile, rule: str, line: int, msg: str, sev: str = "") -> None:
62
+ self.emit(Finding(rule, sf.rel, line, msg, sev, sf.line(line).strip()[:200]))
63
+
64
+ def run(self) -> None:
65
+ GUARD.run("<project>", "vector search filters", self._vector)
66
+ for sf in self.p.files:
67
+ if sf.text is None or sf.is_test or sf.kind not in ("py", "js"):
68
+ continue
69
+ GUARD.run(sf.rel, "AI checks", self._file, sf)
70
+
71
+ def _vector(self) -> None:
72
+ for r in self.routes:
73
+ if not r.has_auth:
74
+ continue # a public docs bot shares one index on purpose
75
+ for m in finditer(VECTOR_QUERY, r.body):
76
+ if ".rpc" in m.group(0) and not SERVICE_ROLE.search(r.body):
77
+ continue # a Supabase client carrying the user's JWT is filtered by row-level security
78
+ # the filter is often built on the lines before the call (`scoped = {...}; search(q, **scoped)`)
79
+ window = r.body[max(0, m.start() - 300):m.start() + 400]
80
+ if not TENANT_FILTER.search(window):
81
+ sf = self.p.get(r.file)
82
+ if sf is not None:
83
+ self.add(sf, "LLM010", self._call_line(sf, r, m),
84
+ f"{r.path} searches the vector store without a user or tenant filter.")
85
+ break
86
+
87
+ @staticmethod
88
+ def _call_line(sf: SourceFile, r: Route, m: "re.Match[str]") -> int:
89
+ """The line of the search call itself (stable when the route above it grows), else the route line."""
90
+ text = sf.text or ""
91
+ nth = r.body.count(m.group(0), 0, m.start()) # earlier calls of the same kind in this route
92
+ pos = len("\n".join(sf.lines[:max(0, r.line - 4)])) # from just above the route (decorators)
93
+ for _ in range(nth + 1):
94
+ pos = text.find(m.group(0), pos)
95
+ if pos < 0:
96
+ return r.line
97
+ pos += 1
98
+ return sf.line_at(pos - 1)
99
+
100
+ def _file(self, sf: SourceFile) -> None:
101
+ v = sf.cache.get("views")
102
+ code = v.code if v is not None else (sf.text or "") # type: ignore[attr-defined]
103
+ for m in finditer(retired_models(), code):
104
+ if m.group(1) not in self.retired_seen: # model lists repeat ids across files; once per project is enough
105
+ self.retired_seen.add(m.group(1))
106
+ self.add(sf, "LLM012", sf.line_at(m.start()), _retired_message(m.group(1)))
107
+ agent = AGENT.search(code)
108
+ if agent is None:
109
+ return
110
+ names = {next(g for g in m.groups() if g) for m in TOOL_NAME.finditer(code) if any(m.groups())}
111
+ untrusted = sorted(n for n in names if UNTRUSTED_TOOL.search(n))
112
+ dangerous = sorted(n for n in names if DANGEROUS_TOOL.search(n) and n not in untrusted)
113
+ if untrusted and dangerous:
114
+ self.add(sf, "LLM015", sf.line_at(agent.start()),
115
+ f"The agent reads untrusted content ({', '.join(untrusted[:3])}) and can act ({', '.join(dangerous[:3])}); "
116
+ "injected instructions in that content can trigger those actions.")
vibeaudit/apiscan.py ADDED
@@ -0,0 +1,98 @@
1
+ """API and auth checks added in 0.7.2: AUTH011-AUTH014, BE019, BE020 (all preview)."""
2
+ from __future__ import annotations
3
+
4
+ import re
5
+ from typing import Callable, List
6
+
7
+ from . import heuristics as H
8
+ from .files import Project, SourceFile
9
+ from .guard import GUARD, finditer
10
+ from .lexer import match_close
11
+ from .model import Route
12
+
13
+ Add = Callable[..., None] # ProjectScanner.add(sf_or_rel, line, rule, message, severity="")
14
+
15
+ MIXED_ALGS = re.compile(r"algorithms['\"]?\s*[:=]\s*\[(?=[^\]]{0,200}\bHS\d)(?=[^\]]{0,200}\b(?:RS|ES|PS)\d)[^\]]{0,200}\]")
16
+ PY_JWT_DECODE = re.compile(r"\bjwt\s*\.\s*decode\s*\(")
17
+ SET_COOKIE = re.compile(
18
+ r"""(?:\bres(?:ponse)?\s*\.\s*cookie|\bcookies\s*\(\s*\)\s*\.\s*set|\b(?:cookies|cookieStore)\s*\.\s*set|\.\s*set_cookie)\s*\(""")
19
+ AUTH_COOKIE_NAME = re.compile(r"""^\s*(?:key\s*=\s*)?['"`][\w.-]*(?:session|token|auth|jwt|sid|access|refresh)[\w.-]*['"`]""", re.I)
20
+ CSRF_EVIDENCE = re.compile(r"(?i)csrf|xsrf|csurf|double.?submit")
21
+ MANUAL_HMAC = re.compile(r"createHmac|hmac\s*\.\s*new|compare_digest|timingSafeEqual|crypto\.subtle\.(?:sign|verify)")
22
+ REPLAY_GUARD = re.compile(
23
+ r"(?i)timestamp|tolerance|replay|nonce|max_?age|constructEvent|construct_event|svix|webhooks\s*\.\s*(?:verify|unwrap)"
24
+ r"|Date\.now|time\.time|\bt=|event_?id|idempoten|delivery")
25
+ FORWARDED_FOR = re.compile(r"""(?i)headers?\s*(?:\[|\.\s*get\s*\(|\.)\s*['"]?x-forwarded-for""")
26
+ LAST_HOP = re.compile(r"\.pop\(\)|\[\s*-\s*1\s*\]|\.at\(\s*-\s*1\s*\)|\.slice\(\s*-\s*1|trust.?proxy|ProxyFix|TRUSTED_PROXIES|forwarded_allow_ips")
27
+ PAYMENT = re.compile(r"(?i)stripe|paypal|razorpay|paymentIntent|payment_intent|checkout\.sessions|mollie|square|braintree|paddle")
28
+ REQUEST_SOURCE = r"(?:req\.body|request\.body|body|request\.(?:json|form|data|POST|get_json\(\))|data|payload|input|params)"
29
+ CLIENT_AMOUNT = re.compile(
30
+ r"(?i)\b(?:unit_amount(?:_decimal)?|amount|price|total)['\"]?\s*[:=]\s*(?:(?:Math\.\w+|int|float|Number|parseInt|parseFloat|Decimal)\(\s*){0,2}"
31
+ + REQUEST_SOURCE + r"\s*(?:\??\.|\[|\.get\()\s*['\"]?\w*(?:amount|price|total|cost)\w*")
32
+ DESTRUCTURED_AMOUNT = re.compile(
33
+ r"\{[^{}]{0,200}\b(amount|price|total)\b[^{}]{0,200}\}\s*=\s*(?:await\s+)?(?:req\.body\b|req\.json\(\)|request\.json\(\)|body\b)")
34
+ RAW_MAIL_HEADER = re.compile(r"""["'`](?:Subject|To|From|Cc|Bcc|Reply-To):\s*(?:\{|\$\{|["'`]\s*\+|%s|["'`]\s*%)""")
35
+ MIME_HEADER_FROM_REQUEST = re.compile(
36
+ r"""\[\s*['"](?:Subject|To|From|Cc|Bcc|Reply-To)['"]\s*\]\s*=\s*[^\n]{0,120}?\b(?:request\.(?:form|json|args|data|POST|GET)|req\.body)\b""")
37
+ RAW_MAIL = re.compile(r"\bsmtplib\b|\bsendmail\s*\(|\bSMTP(?:_SSL)?\s*\(|\bMIME(?:Text|Multipart)\b|\bnet\s*\.\s*createConnection|\bmail\s*\(")
38
+
39
+
40
+ def _args(bare: str, code: str, open_paren: int) -> str:
41
+ """The text between a call's parentheses; a bounded slice when the call does not close nearby."""
42
+ close = match_close(bare, open_paren)
43
+ if close < 0 or close - open_paren > 4000:
44
+ close = min(len(code), open_paren + 600)
45
+ return code[open_paren + 1:close]
46
+
47
+
48
+ def run(project: Project, routes: List[Route], csrf: bool, add: Add) -> None:
49
+ """`csrf`: the project has CSRF protection somewhere (CSRF_EVIDENCE in its code)."""
50
+ for sf in project.files:
51
+ v = sf.cache.get("views")
52
+ if v is None or sf.is_test or sf.kind not in ("py", "js"):
53
+ continue
54
+ GUARD.run(sf.rel, "API file checks", _file, sf, v.code, v.bare, csrf, add) # type: ignore[attr-defined]
55
+ for r in routes:
56
+ _route(r, add)
57
+
58
+
59
+ def _file(sf: SourceFile, code: str, bare: str, csrf: bool, add: Add) -> None:
60
+ for m in finditer(MIXED_ALGS, code):
61
+ add(sf, sf.line_at(m.start()), "AUTH011", "The token is accepted with both HMAC and public-key algorithms (key confusion).")
62
+ if sf.kind == "py":
63
+ for m in finditer(PY_JWT_DECODE, bare):
64
+ args = _args(bare, code, m.end() - 1)
65
+ if "algorithms" not in args and "verify" not in args and "**" not in args: # verify=False is BE016
66
+ add(sf, sf.line_at(m.start()), "AUTH011", "jwt.decode() does not pin the accepted algorithms.")
67
+ if not csrf:
68
+ for m in finditer(SET_COOKIE, bare):
69
+ args = _args(bare, code, m.end() - 1)
70
+ auth_cookie = AUTH_COOKIE_NAME.search(args) or re.search(r"(?i)\bhttp_?only\b", args)
71
+ none = re.search(r"""(?i)same_?site['"]?\s*[:=]\s*['"]none""", args)
72
+ if auth_cookie and (none or not re.search(r"(?i)same_?site", args)) and "..." not in args and "**" not in args:
73
+ add(sf, sf.line_at(m.start()), "AUTH012",
74
+ "The session cookie is set with SameSite=None and the project has no CSRF protection." if none else
75
+ "The session cookie sets no SameSite attribute and the project has no CSRF protection.")
76
+ if H.RATE_LIMIT_EVIDENCE.search(code) and not LAST_HOP.search(code):
77
+ m = FORWARDED_FOR.search(code)
78
+ if m:
79
+ add(sf, sf.line_at(m.start()), "AUTH014", "The rate limit key comes from X-Forwarded-For, which the client can set.")
80
+ if RAW_MAIL.search(code):
81
+ for m in finditer(RAW_MAIL_HEADER, code):
82
+ add(sf, sf.line_at(m.start()), "BE020", "An email header is built by formatting a value into the header line.")
83
+ for m in finditer(MIME_HEADER_FROM_REQUEST, code):
84
+ add(sf, sf.line_at(m.start()), "BE020", "An email header is set straight from request data.")
85
+
86
+
87
+ def _route(r: Route, add: Add) -> None:
88
+ body = r.body
89
+ if H.WEBHOOK_PATH.search(r.path) and MANUAL_HMAC.search(body) and not REPLAY_GUARD.search(body):
90
+ add(r.file, r.line, "AUTH013", f"Webhook {r.path} checks a signature but no timestamp, so a captured request can be replayed.")
91
+ if PAYMENT.search(body):
92
+ m = CLIENT_AMOUNT.search(body)
93
+ if m is None:
94
+ d = DESTRUCTURED_AMOUNT.search(body)
95
+ if d and re.search(r"(?i)\b(?:unit_amount|amount)['\"]?\s*:\s*(?:Math\.\w+\(\s*)?%s\b|[{,]\s*amount\s*[,}]" % d.group(1), body[d.end():]):
96
+ m = d
97
+ if m is not None:
98
+ add(r.file, r.line, "BE019", "The amount charged comes from the request, so the client can set its own price.")