You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: document engine-level read-only enforcement and least-privilege guidance
Update the execute_sql read-only section for 0.22.6: describe the two-layer
enforcement (keyword classifier + database-native read-only) and add a warning
that read-only mode cannot constrain privileged-role function abuse
(pg_read_file/lo_export/dblink/COPY TO PROGRAM on PostgreSQL; DDL implicit
commit on MySQL/MariaDB). Recommend connecting with a least-privilege read-only
database user for untrusted/agent-driven environments.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: docs/tools/execute-sql.mdx
+17-3Lines changed: 17 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ Execute SQL queries and statements on your database with support for transaction
9
9
-**Single statements**: Execute a single SQL query or command
10
10
-**Multiple statements**: Separate multiple statements with semicolons (`;`)
11
11
-**Transactions**: Wrap operations in `BEGIN`/`COMMIT` blocks for atomic execution
12
-
-**Read-only mode**: When enabled with `--readonly` flag, only SELECT and read-only operations are allowed
12
+
-**Read-only mode**: Configure a tool with `readonly = true` to restrict execution to read-only operations, enforced by both a keyword classifier and the database's own read-only mode
13
13
-**Row limiting**: Configure `--max-rows` to limit SELECT query results
In read-only mode, only [allowed SQL keywords](https://github.com/bytebase/dbhub/blob/main/src/utils/allowed-keywords.ts) are permitted, including:
120
+
In read-only mode, DBHub enforces read-only access in two layers:
121
+
122
+
**1. Keyword classifier.** Each statement is checked against an [allow-list of read-only keywords](https://github.com/bytebase/dbhub/blob/main/src/utils/allowed-keywords.ts) after stripping comments and strings:
121
123
122
124
-`SELECT` queries
123
125
-`SHOW` commands
124
126
-`DESCRIBE` commands
125
127
-`EXPLAIN` queries
126
128
- Other read-only operations
127
129
130
+
**2. Engine-level enforcement.** The statement is then executed under the database's own read-only mechanism, so the database rejects writes even if the classifier is evaded:
131
+
132
+
-**PostgreSQL** — runs inside a `BEGIN READ ONLY` transaction
Read-only mode validates the **first keyword** of each statement after stripping comments and strings. It is a safety net to prevent accidental modifications, not a security boundary. For untrusted environments, use database-level read-only users or permissions instead.
137
+
Engine-level enforcement was added in **0.22.6**. Earlier versions relied on the keyword classifier alone.
130
138
</Note>
131
139
140
+
<Warning>
141
+
Read-only mode prevents data modification (DML/DDL), but it cannot constrain everything a **privileged database role** can do through *functions*. On PostgreSQL, a superuser or specially-privileged role can still read or write server files and run commands via `pg_read_file`, `lo_export`, `dblink`, and `COPY ... TO PROGRAM` — these are not blocked by a read-only transaction. On MySQL/MariaDB, DDL performs an implicit commit and is stopped only by the keyword classifier.
142
+
143
+
For untrusted or agent-driven environments, **always connect DBHub with a least-privilege, read-only database user** scoped to the data it needs. Do not point a read-only tool at an admin/superuser DSN and rely on read-only mode alone.
144
+
</Warning>
145
+
132
146
## Row Limiting
133
147
134
148
Limit the number of rows returned from SELECT queries to prevent accidentally retrieving too much data:
0 commit comments