Read-Only Mode Enforcement
SQL Keyword Filtering
DBHub enforces read-only restrictions at the SQL parsing level using keyword validation. Fromsrc/utils/allowed-keywords.ts:9-15, allowed operations by database type:
How Read-Only Validation Works
Fromsrc/utils/allowed-keywords.ts:24-40:
- Strip comments and strings - Prevents
SELECT '-- DROP TABLE'attacks - Extract first keyword - Analyzes the actual SQL operation
- Compare against whitelist - Only allowed keywords pass validation
Multi-Statement Protection
Fromsrc/tools/execute-sql.ts:25-28, all statements in a multi-statement query must be read-only:
Configuring Read-Only Mode
Per-tool configuration (recommended):DSN Obfuscation
Password Redaction in Logs
Fromsrc/utils/dsn-obfuscate.ts:91-141, passwords are automatically redacted:
- Passwords replaced with asterisks (8 characters max)
- Usernames preserved for debugging
- Host, port, database preserved
- Query parameters preserved (for
sslmode, etc.)
SSH Configuration Obfuscation
Fromsrc/utils/dsn-obfuscate.ts:148-168, SSH credentials are also redacted:
Query Guardrails
Row Limiting
Fromsrc/types/config.ts:76, configure max_rows per tool:
src/utils/sql-row-limiter.ts:126-144):
- Existing
LIMITuses minimum of configured and specified - Parameterized
LIMIT($1, ?, @p1) triggers subquery wrapping - Only applies to
SELECTqueries
Query Timeouts
Fromsrc/types/config.ts:51, set timeouts per source:
- PostgreSQL: Uses
query_timeoutin pool config (src/connectors/postgres/index.ts:76-79) - MySQL/MariaDB: Client-side timeout
- SQL Server: Query-level timeout
- SQLite: Not supported at protocol level
- Runaway queries consuming database resources
- Long-running operations blocking other users
- Accidental full table scans on large datasets
Connection Timeouts
Fromsrc/types/config.ts:50:
- Hanging on unreachable databases
- Indefinite blocking during startup
- Resource exhaustion from stuck connections
SSH Tunnel Security
Key-Based Authentication
Recommended approach:- Ed25519 - Modern, secure, fast (preferred)
- RSA 4096 - Widely compatible
- ECDSA - Good balance of security and performance
- Avoid: RSA 2048 or DSA keys
Password vs Key-Based Auth
Fromsrc/utils/ssh-tunnel.ts:50-53:
Passphrase-Protected Keys
Best practice for production:SSH Keepalive for Long Connections
Fromsrc/types/config.ts:20-23, prevent idle disconnections:
src/utils/ssh-tunnel.ts:174-185):
- Converts interval from seconds to milliseconds
- Validates interval is non-negative
- Default: Keepalive disabled (
interval = 0)
ProxyJump Security
Fromsrc/types/config.ts:16-19, multi-hop SSH:
- Each hop must trust the SSH key
- Connections established sequentially (bastion → jump2 → target)
- Same credentials used for all hops (from
src/utils/ssh-tunnel.ts:70-138) - Each jump host can see traffic to next hop
SSL/TLS Encryption
SSL Mode Configuration
From CLAUDE.md:150, supported SSL modes:SSL for Network Databases
Applies to: PostgreSQL, MySQL, MariaDB, SQL Server Not applicable: SQLite (local file-based) Recommendations by environment:- Production: Always use SSL (
sslmode=requireor stricter) - Staging: Use SSL (
sslmode=require) - Local development: SSL optional (
sslmode=disablefor simplicity) - SSH tunnels: SSL optional (tunnel already encrypted)
Credential Management
Never Commit Secrets
Update .gitignore:Environment Variables for Secrets
TOML with environment variable references (manual substitution):Cloud Secret Managers
For production deployments:-
AWS Secrets Manager
-
HashiCorp Vault
-
Google Secret Manager
Network Security
SSH Tunnels for Firewall Traversal
Fromsrc/utils/ssh-tunnel.ts:1-348, secure access to private databases:
- DBHub connects to bastion via SSH
- SSH tunnel forwards local port to database server
- Database connection goes through encrypted tunnel
- Database sees connection from bastion (trusted IP)
Bastion Host Architecture
- Database not exposed to internet
- Single point of SSH access control
- Audit trail of all connections
- Network segmentation enforced
Per-Tool Read-Only Configuration
Granular Access Control
Fromsrc/types/config.ts:71-98, configure readonly per tool:
- Default read-only for general queries
- Explicit write permission for specific operations
- Prevents accidental data modifications
- Audit trail of write-capable tools
SQL Injection Prevention
Parameterized Queries in Custom Tools
Use parameter placeholders, not string concatenation:- Parameter binding to prevent injection
- Type validation for parameters
- Proper escaping based on database type
Parameter Types and Validation
Fromsrc/types/config.ts:60-67:
string, integer, float, boolean, array
Security Checklist
Database Connection
- Use
readonly = truefor production databases - Enable SSL/TLS (
sslmode=require) for network databases - Set
connection_timeoutfor remote databases - Set
query_timeoutto prevent runaway queries - Configure
max_rowslimits per tool
SSH Tunnels
- Prefer key-based authentication over passwords
- Use passphrase-protected SSH keys for production
- Use Ed25519 keys (or RSA 4096) for new deployments
- Configure SSH keepalive for long-running connections
- Set appropriate file permissions on SSH keys (
chmod 600)
Credential Management
- Never commit
.envor TOML files with credentials - Use
.gitignoreto exclude secret files - Keep example files (
.env.example,dbhub.toml.example) in version control - Use environment variables or secret managers for production
- Rotate database credentials regularly
- Use separate credentials per environment (dev/staging/prod)
Access Control
- Configure
readonly = trueas default - Explicitly mark write-capable tools with
readonly = false - Use database-level users with minimal permissions
- Grant only necessary privileges (SELECT vs. ALL)
- Use separate database users per application/environment
Custom Tools
- Always use parameterized queries (never string concatenation)
- Validate parameter types in tool configuration
- Mark read-only tools with
readonly = true - Document tool purpose and parameters clearly
- Review custom tools for security before deployment
Network Security
- Use SSH tunnels for databases behind firewalls
- Implement bastion host architecture for production
- Restrict database network access to known IPs
- Enable database audit logging
- Monitor unusual query patterns
Monitoring and Auditing
- Review request traces in workbench for suspicious activity
- Enable database query logging
- Monitor connection attempts and failures
- Set up alerts for failed authentication attempts
- Regularly review granted database permissions