The SQL statement to sanitize
Options to selectively allow certain patterns
The original SQL if safe
import { sanitizeSql, DuckDBError } from '@ducklings/workers';
// In a request handler
try {
const safeSql = sanitizeSql(userInput);
const result = await conn.query(safeSql);
return Response.json({ data: result });
} catch (e) {
if (e instanceof DuckDBError && e.code === 'SANITIZE_ERROR') {
return Response.json({ error: e.message }, { status: 400 });
}
throw e;
}
Sanitizes a SQL statement by checking for dangerous patterns.
This function throws a DuckDBError if the SQL contains dangerous patterns. Use this in request handlers to automatically reject unsafe queries.
Blocked patterns:
duckdb_secrets()- Exposes database credentialsPRAGMA- Can modify database settingsCOPY ... TO- Writes files to disk (COPY FROM is allowed)EXPORT DATABASE- Exports database to filesNote: SET commands are blocked separately by
lockConfiguration: truein DuckDBConfig.