I have discovered a critical vulnerability in the safety mechanism of Berth that allows bypassing the 'read-only' and 'write' modes.
The current safety check in safety.py uses regular expressions to detect the type of SQL statement by checking only the start of the query (e.g., ^\s*SELECT\b).
Vulnerability: Stacked Queries
An attacker can execute destructive operations (like DROP TABLE) even in read-only mode by prepending a valid SELECT statement.
Example: 'SELECT 1; DROP TABLE users;'
Since the server only validates the start of the query, it will identify this as a 'SELECT' statement and allow it. If the underlying database driver and database (e.g., PostgreSQL, MySQL) support stacked queries, the destructive command will be executed.
Impact:
Full database compromise, including unauthorized data modification and deletion.
Recommendation:
- Disable stacked queries at the database driver level.
- Use a proper SQL parser to analyze all statements in a query instead of relying on start-of-string regex.
- Implement strict role-based access control (RBAC) at the database level rather than relying on application-level filters.
I have discovered a critical vulnerability in the safety mechanism of Berth that allows bypassing the 'read-only' and 'write' modes.
The current safety check in safety.py uses regular expressions to detect the type of SQL statement by checking only the start of the query (e.g., ^\s*SELECT\b).
Vulnerability: Stacked Queries
An attacker can execute destructive operations (like DROP TABLE) even in read-only mode by prepending a valid SELECT statement.
Example: 'SELECT 1; DROP TABLE users;'
Since the server only validates the start of the query, it will identify this as a 'SELECT' statement and allow it. If the underlying database driver and database (e.g., PostgreSQL, MySQL) support stacked queries, the destructive command will be executed.
Impact:
Full database compromise, including unauthorized data modification and deletion.
Recommendation: