security
security
¶
Security middleware and utilities for Marianne Dashboard.
Provides security headers and input validation.
Classes¶
SecurityConfig
dataclass
¶
SecurityConfig(cors_origins=(lambda: ['http://localhost:8080', 'http://127.0.0.1:8080'])(), cors_allow_credentials=True, cors_allow_methods=(lambda: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])(), cors_allow_headers=(lambda: ['Accept', 'Authorization', 'Content-Type', 'X-Requested-With'])(), add_security_headers=True, content_security_policy="default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.tailwindcss.com https://cdn.jsdelivr.net https://unpkg.com; style-src 'self' 'unsafe-inline' https://cdn.tailwindcss.com https://cdn.jsdelivr.net https://unpkg.com; font-src 'self' https://cdn.jsdelivr.net; img-src 'self' data:; connect-src 'self'", strict_transport_security='max-age=31536000; includeSubDomains', x_content_type_options='nosniff', x_frame_options='SAMEORIGIN', x_xss_protection='1; mode=block', referrer_policy='strict-origin-when-cross-origin')
Security configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
cors_origins |
list[str]
|
Allowed CORS origins |
cors_allow_credentials |
bool
|
Allow credentials in CORS |
cors_allow_methods |
list[str]
|
Allowed HTTP methods |
cors_allow_headers |
list[str]
|
Allowed headers |
add_security_headers |
bool
|
Add security headers to responses |
content_security_policy |
str
|
CSP header value |
strict_transport_security |
str
|
HSTS header value |
x_content_type_options |
str
|
X-Content-Type-Options header |
x_frame_options |
str
|
X-Frame-Options header |
x_xss_protection |
str
|
X-XSS-Protection header |
referrer_policy |
str
|
Referrer-Policy header |
Functions¶
from_env
classmethod
¶
Create config from environment variables.
Environment variables
MZT_CORS_ORIGINS: Comma-separated origins MZT_CORS_CREDENTIALS: true/false
Source code in src/marianne/dashboard/auth/security.py
SecurityHeadersMiddleware
¶
Bases: BaseHTTPMiddleware
Middleware to add security headers to all responses.
Initialize middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
ASGIApp
|
ASGI application (FastAPI or Starlette app) |
required |
config
|
SecurityConfig | None
|
Security configuration |
None
|
Source code in src/marianne/dashboard/auth/security.py
Functions¶
dispatch
async
¶
Add security headers to response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
Incoming request |
required |
call_next
|
RequestResponseEndpoint
|
Next middleware/handler |
required |
Returns:
| Type | Description |
|---|---|
Response
|
Response with security headers |
Source code in src/marianne/dashboard/auth/security.py
Functions¶
validate_job_id
¶
Validate job ID format to prevent injection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_id
|
str
|
Job identifier to validate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid format |
Source code in src/marianne/dashboard/auth/security.py
validate_path_component
¶
Validate path component to prevent traversal attacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component
|
str
|
Path component to validate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if safe |
Source code in src/marianne/dashboard/auth/security.py
sanitize_filename
¶
Sanitize filename for safe use.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Original filename |
required |
Returns:
| Type | Description |
|---|---|
str
|
Sanitized filename |