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
[FEATURE][SECURITY]: Database-Level Security Constraints and SQL Injection Prevention
Goal
Add database-level CHECK constraints and validation to ensure data integrity and provide defense-in-depth against SQL injection attacks. While SQLAlchemy ORM provides parameterized queries, these additional constraints prevent malicious content storage even if application-layer validation is bypassed.
Path Traversal Prevention: Block ../ patterns in URIs
XSS Prevention: Block script tags in user-controlled fields
📖 User Stories
US-1: Security Engineer - Prevent Malicious Data Storage
As a security engineer I want CHECK constraints to reject malicious input So that even bypassed validation can't store harmful data
Acceptance Criteria:
Scenario: Reject HTML in prompt namesGiven a database with CHECK constraints
When inserting a prompt with name "<script>alert(1)</script>"Then the database should reject the insert
And raise an IntegrityError
Scenario: Reject path traversal in resource URIsGiven a database with CHECK constraints
When inserting a resource with uri "../../../etc/passwd"Then the database should reject the insert
And log the attempt
Scenario: Reject JavaScript URLs in toolsGiven a database with CHECK constraints
When inserting a tool with url "javascript:alert(1)"Then the database should reject the insert
And the error should indicate invalid URL format
Technical Requirements:
CHECK constraints on prompts, tools, resources, gateways, servers tables
Pattern matching for HTML tags, JavaScript URLs, path traversal
SQLite and PostgreSQL compatible regex patterns
US-2: Developer - Safe Query Construction
As a developer I want query sanitization utilities So that I can safely build dynamic queries
Acceptance Criteria:
Scenario: Sanitize LIKE patternsGiven a search term "test%drop"When sanitizing for LIKE query
Then the result should be "test\\%drop"And special characters should be escaped
Scenario: Validate column namesGiven a sort column from user input
When validating against whitelist ["name", "created_at"]
Then unknown columns should raise ValueError
And valid columns should pass through
Scenario: Build safe IN clausesGiven a list of IDs [1, 2, 3]
When building an IN clause
Then the result should use parameterized placeholders
And no raw values should be in the SQL
Technical Requirements:
QuerySanitizer class with utility methods
Column whitelist validation
LIKE pattern escaping
Parameterized IN clause builder
US-3: Operator - Database Security Configuration
As a platform operator I want configurable database security settings So that I can tune security for my environment
Acceptance Criteria:
Scenario: Configure statement timeoutGiven DB_STATEMENT_TIMEOUT=30000
When a query runs for 35 seconds
Then the query should be cancelled
And an error should be logged
Scenario: Configure max connectionsGiven DB_MAX_CONNECTIONS=100
When 101 connections are attempted
Then the 101st should be rejected
And connection pool should remain stable
Scenario: Enable query logging in debug modeGiven LOG_LEVEL=DEBUG
And DB_ENABLE_SQL_LOG=true
When a query executes
Then the SQL should be logged
Technical Requirements:
DB_STATEMENT_TIMEOUT setting
DB_MAX_CONNECTIONS setting
DB_ENABLE_SQL_LOG setting
Connection timeout configuration
US-4: Security Admin - Model-Level Validation
As a security administrator I want SQLAlchemy models to validate data before insert So that invalid data is caught at the application layer
Acceptance Criteria:
Scenario: Validate tool name formatGiven a Tool model instance
When setting name to "invalid name with spaces!"Then a ValueError should be raised
And the message should indicate valid format
Scenario: Validate URL schemeGiven a Tool model instance
When setting url to "file:///etc/passwd"Then a ValueError should be raised
And only http/https/ws/wss should be allowed
Scenario: Validate name lengthGiven a Tool model instance
When setting name longer than 255 characters
Then a ValueError should be raised
flowchart TD
A[API Request] --> B[Pydantic Validation]
B --> C[SQLAlchemy @validates]
C --> D[Database CHECK Constraints]
D --> E[Data Stored]
B -->|Invalid| F[HTTP 422]
C -->|Invalid| G[ValueError]
D -->|Invalid| H[IntegrityError]
Loading
CHECK Constraints by Table
erDiagram
PROMPTS {
string name "CHECK: no HTML, length <= 255"
string template "CHECK: no script tags"
}
TOOLS {
string name "CHECK: alphanumeric_underscore"
string url "CHECK: http(s)/ws(s) scheme"
}
RESOURCES {
string uri "CHECK: no path traversal"
string mime_type "CHECK: valid MIME format"
}
GATEWAYS {
string transport "CHECK: IN (SSE, STREAMABLEHTTP, STDIO)"
string url "CHECK: http(s)/ws(s) scheme"
}
SERVERS {
string name "CHECK: safe characters only"
string icon "CHECK: https or data:image"
}
Loading
📋 Implementation Tasks
Phase 1: CHECK Constraints Migration
Create alembic migration for CHECK constraints
Add prompts table constraints (name format, template safety)
Add tools table constraints (name format, URL scheme)
Add resources table constraints (URI format, no traversal)
[FEATURE][SECURITY]: Database-Level Security Constraints and SQL Injection Prevention
Goal
Add database-level CHECK constraints and validation to ensure data integrity and provide defense-in-depth against SQL injection attacks. While SQLAlchemy ORM provides parameterized queries, these additional constraints prevent malicious content storage even if application-layer validation is bypassed.
Why Now?
../patterns in URIs📖 User Stories
US-1: Security Engineer - Prevent Malicious Data Storage
As a security engineer
I want CHECK constraints to reject malicious input
So that even bypassed validation can't store harmful data
Acceptance Criteria:
Technical Requirements:
US-2: Developer - Safe Query Construction
As a developer
I want query sanitization utilities
So that I can safely build dynamic queries
Acceptance Criteria:
Technical Requirements:
US-3: Operator - Database Security Configuration
As a platform operator
I want configurable database security settings
So that I can tune security for my environment
Acceptance Criteria:
Technical Requirements:
US-4: Security Admin - Model-Level Validation
As a security administrator
I want SQLAlchemy models to validate data before insert
So that invalid data is caught at the application layer
Acceptance Criteria:
Technical Requirements:
🏗 Architecture
Validation Layers
flowchart TD A[API Request] --> B[Pydantic Validation] B --> C[SQLAlchemy @validates] C --> D[Database CHECK Constraints] D --> E[Data Stored] B -->|Invalid| F[HTTP 422] C -->|Invalid| G[ValueError] D -->|Invalid| H[IntegrityError]CHECK Constraints by Table
erDiagram PROMPTS { string name "CHECK: no HTML, length <= 255" string template "CHECK: no script tags" } TOOLS { string name "CHECK: alphanumeric_underscore" string url "CHECK: http(s)/ws(s) scheme" } RESOURCES { string uri "CHECK: no path traversal" string mime_type "CHECK: valid MIME format" } GATEWAYS { string transport "CHECK: IN (SSE, STREAMABLEHTTP, STDIO)" string url "CHECK: http(s)/ws(s) scheme" } SERVERS { string name "CHECK: safe characters only" string icon "CHECK: https or data:image" }📋 Implementation Tasks
Phase 1: CHECK Constraints Migration
Phase 2: Model-Level Validation
Phase 3: Query Sanitization Utilities
Phase 4: Database Connection Security
Phase 5: Testing
Phase 6: Data Cleanup Script
⚙️ Configuration Example
✅ Success Criteria
🏁 Definition of Done
make verify🔗 Related Issues