Security Audit¶
Keywords: security report, vulnerability audit, security hardening, audit log, security features
Asok includes built-in security measures. This document summarizes the protections integrated into the framework to help protect applications from common web vulnerabilities.
Executive Summary¶
Asok includes modern security protections by default. In most cases, you do not need to write security-specific code to get baseline protection against SQL injection, XSS, CSRF, and related issues.
1. Protection Against Injections¶
SQL Injection¶
The Asok ORM uses parameterized queries for all data interactions. User input is never concatenated directly into SQL strings.
All
Querymethods (where,where_in, etc.) use?placeholders.Column names are strictly validated against model metadata.
Cross-Site Scripting (XSS)¶
The Asok template engine implements automatic HTML escaping by default.
Any variable rendered via
{{ user_input }}is escaped (e.g.,<becomes<).To render raw HTML, you must explicitly use the
|safefilter or theSafeStringclass.
2. Broken Access Control¶
CSRF Protection¶
Asok uses the Double Submit Cookie pattern to prevent Cross-Site Request Forgery.
All state-changing requests (POST, PUT, DELETE) must include a
csrf_token.The token is verified against an
HttpOnlycookie.Use
{{ request.csrf_input() }}in your forms to automatically include the token.
Mass Assignment¶
The ORM protects sensitive fields from being updated in bulk from user input.
Fields marked as
protectedin your Model are ignored byModel.create()andModel.update()unless the_trust=Trueflag is passed.
3. Authentication & Sessions¶
Password Storage¶
Asok uses PBKDF2-SHA256 with 600,000 iterations for password hashing. This is handled automatically by Field.Password().
Session Security¶
Secure IDs: Session identifiers are 32-byte cryptographically strong hex strings (
secrets.token_urlsafe(32)).Cookie Security:
Session cookies are
HttpOnlyandSameSite=Strictby defaultSecureflag added automatically on HTTPSCSRF cookies also use
HttpOnlyandSameSite=Strictfor maximum protection
HMAC Signing: Session IDs are signed with HMAC to prevent tampering
Rotation: IDs can be rotated using
session.regenerate()to prevent session fixationCSRF Token Rotation: CSRF tokens are automatically rotated after successful validation to prevent token reuse attacks
4. File and URL Safety¶
File Upload Validation ⭐ NEW in v0.1.6¶
Asok provides automatic MIME type validation using magic bytes detection to prevent malicious file uploads:
Magic Bytes Detection: Files are validated by their actual content (first bytes), not just extension
50+ Formats Supported: Images (JPEG, PNG, GIF, WebP, BMP, TIFF, SVG), Audio (MP3, WAV, FLAC, OGG, AAC), Video (MP4, WebM, MKV, AVI, MOV), Documents (PDF, ZIP, Office files)
Extension Matching: File extension must match the detected MIME type
Whitelist Enforcement:
allowed_typesparameter enforces strict MIME type whitelistSecure Filenames: Files are renamed with UUID by default (
secure_filename=True)Restrictive Permissions: Saved files get
0o644(rw-r–r–) permissionsSecurity Warnings: Logs warning if validation is disabled or no whitelist specified
# Secure file upload with validation
photo.save("uploads/", allowed_types=['image/jpeg', 'image/png'])
See File Storage for complete documentation.
Path Traversal¶
The secure_filename() utility is used automatically during file uploads to remove directory separators and illegal characters, ensuring files cannot be saved outside the intended directory.
Enhanced in v0.1.6:
Uses
pathlib.Path.resolve(strict=True)for robust path resolutionValidates all parent directories for symlinks
Only accepts filename (basename) to prevent any path traversal
Returns
403 Forbiddenon escape attempts
Open Redirects¶
The request.redirect() method uses is_safe_url() to block redirects to external domains or malformed URLs that could be used for phishing.
5. Security Headers¶
Asok automatically injects a set of “best-practice” security headers into every HTTP response:
Header |
Value |
Purpose |
|---|---|---|
|
|
Prevents MIME-sniffing |
|
|
Prevents Clickjacking |
|
|
Enables browser XSS filters |
|
|
Enforces HTTPS |
|
|
Protects privacy |
6. Content Security Policy (CSP)¶
Asok provides built-in support for Nonces. You can access a unique cryptographic nonce for the current request via request.nonce.
<script nonce="{{ request.nonce }}">
// This inline script is authorized by the CSP
</script>
7. Security Audit Results (v0.3.0)¶
The following notes summarize the main security mechanisms currently implemented in Asok v0.3.0:
SQL Injection Protection ✅¶
Parameterized Queries: All ORM queries use
?placeholders with separate arguments array.Strict Column Validation:
_valid_column()strictly validates all column names against model metadata, preventing injection in non-parameterizable clauses.Operator Whitelist: SQL operators are validated against a strict
_OPERATORSwhitelist.
XSS Protection ✅¶
Auto-Escaping: All template variables are automatically escaped via
_escape().UI Attribute Escaping: The centralized
_render_attrsutility ensures all HTML attributes (including nested ones likedropdown__class) are properly escaped.SafeString Class: Explicit opt-in required for raw HTML via
SafeStringclass or|safefilter.
CSRF Protection ✅¶
Cryptographic Tokens: 32-byte random tokens via
secrets.token_hex(32).HMAC Validation: Token comparison uses
hmac.compare_digest()to prevent timing attacks.Strict Origin Verification: For HTTPS requests, both
OriginandRefererheaders are validated against the host to prevent cross-origin state changes.SameSite Cookies: CSRF cookies use
SameSite=Strictfor maximum protection.
Server-Side Template Injection (SSTI) Protection ✅¶
Restricted Execution: Templates are executed in a restricted namespace with
__builtins__blocked.Sandbox Escape Prevention: Access to dangerous Python attributes (
__class__,__globals__,__subclasses__, etc.) is strictly blocked.Safe Attribute Whitelist: Only a specific set of safe underscore attributes (like
_tableor_fieldsfor internal needs) are accessible.
Path Traversal Protection ✅¶
Absolute Path Validation: Uses
os.path.abspath()withstartswith()checksSymlink Protection: Uses
os.path.realpath()andos.path.islink()to prevent symlink traversal attacksSafe Resolve Function:
_safe_resolve()utility ensures paths stay within allowed directories403 on Escape Attempts: Returns 403 Forbidden if path traversal is detected
Static File Validation: All static file serving validates paths before reading
Password Security ✅¶
PBKDF2-SHA256: Industry-standard password hashing algorithm
High Iteration Count: 600,000 iterations (OWASP 2023 compliant)
Random Salt: Each password gets a unique 16-byte random salt
Secure Format: Stored as
pbkdf2:sha256:600000$salt$hash
Data Encryption (GDPR / PCI-DSS) ✅¶
Asok provides a native Field.EncryptedString() for storing sensitive data (SSNs, credit card numbers, phone numbers, API keys) transparently encrypted in the database.
Algorithm: AES-256 via the
cryptographypackage’s Fernet engine (authenticated encryption — also protects integrity).Key Derivation: The encryption key is derived from the application’s
SECRET_KEYusing SHA-256, then base64url-encoded to produce a valid 32-byte Fernet key.Transparent: Values are automatically encrypted on
save()and decrypted on__init__()(when loading from the DB). Application code always sees plaintext.Graceful Degradation: If decryption fails (e.g., key rotation or mismatch), the method returns the raw ciphertext instead of crashing, and logs an error for investigation.
Non-queryable by design: Fernet tokens are non-deterministic (ciphertext differs each time), so direct database queries or indexes on encrypted columns are not possible — by design.
from asok import Model, Field
class Customer(Model):
name = Field.String()
ssn = Field.EncryptedString() # Encrypted transparently
c = Customer.create(name="Alice", ssn="123-45-6789")
print(c.ssn) # "123-45-6789" — plaintext in Python
# In the DB: "gAAAAAB..." — Fernet ciphertext
[!IMPORTANT] The
cryptographylibrary must be installed:pip install "asok[security]"
Zero-Eval Content Security Policy ✅¶
Least Privilege Principle: Asok implements a Content Security Policy that completely disables
'unsafe-eval'by default in production.Zero-Eval Reactive Architecture: Thanks to server-side precompilation of reactive expressions and dynamic registry injection via cryptographically nonced script elements, the browser never uses
eval()ornew Function().Granular Control: Developers can still force
'unsafe-eval'if they use third-party libraries requiring it viaCSP_UNSAFE_EVAL.
Command Injection Protection ✅¶
No Runtime Execution: No
os.system(),eval(), orexec()in runtime codeSubprocess Limited:
subprocessonly used in CLI tools and secure background optimization, never directly exposed to request routing.Subprocess Confinement: The image optimizer utility in
asok/utils/image.pystrictly restricts binary execution pathways inside the.asok/binsubdirectory usingos.path.commonpathchecks, preventing path traversal or execution of unauthorized binaries.No Dynamic Code: No dynamic code execution from user input
Log Injection Protection ✅¶
Newline Sanitization:
\nand\rcharacters removed from logged request dataStructured Logging: JSON format option for production with proper escaping
Limited Debug Info: Debug logs never expose sensitive session data or passwords
Validation & Input Handling ✅¶
Type Validation: Strong type checking via
FieldtypesEmail Validation: RFC-compliant email validation
URL Validation: Validates URL format and scheme
HTML Sanitization: Optional bleach integration for HTML whitelisting
Security Summary¶
Vulnerability Class |
Protection Status |
|---|---|
SQL Injection |
✅ Protected |
XSS |
✅ Protected |
CSRF |
✅ Protected |
Path Traversal |
✅ Protected |
Authentication |
✅ Hardened |
Session Management |
✅ Hardened |
Encrypted Fields (GDPR/PCI-DSS) |
✅ |
Command Injection |
✅ No direct runtime exposure |
Log Injection |
✅ Protected |
Input Validation |
✅ Comprehensive |
Conclusion¶
Asok provides secure defaults and layered defenses against common web vulnerabilities. As with any framework, applications still need sensible configuration, safe templates, and careful access control.