# Security Architecture
## Overview
This document provides a visual representation of the security architecture implemented in the Survey Custom Certificate Template module.
## Security Layers
```
┌─────────────────────────────────────────────────────────────────┐
│ USER INTERFACE │
│ - Form validation │
│ - Field visibility based on groups │
│ - Client-side input constraints │
└────────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ ACCESS CONTROL LAYER │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Record Rules (ir.rule) │ │
│ │ - Survey managers: Full access │ │
│ │ - Survey users: Read-only, own records │ │
│ └──────────────────────────────────────────────────────────┘ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Model Access (ir.model.access) │ │
│ │ - CRUD permissions per group │ │
│ └──────────────────────────────────────────────────────────┘ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Field-Level Security │ │
│ │ - Sensitive fields restricted to managers │ │
│ └──────────────────────────────────────────────────────────┘ │
└────────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ APPLICATION LAYER │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Input Validation │ │
│ │ - _validate_placeholder_key() │ │
│ │ - _validate_json_structure() │ │
│ │ - _validate_and_sanitize_placeholders() │ │
│ └──────────────────────────────────────────────────────────┘ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Input Sanitization │ │
│ │ - _sanitize_placeholder_value() │ │
│ │ - _sanitize_certificate_value() │ │
│ │ - HTML escaping, tag stripping │ │
│ │ - Control character removal │ │
│ └──────────────────────────────────────────────────────────┘ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Business Logic Validation │ │
│ │ - File format validation │ │
│ │ - File size limits │ │
│ │ - Template structure validation │ │
│ └──────────────────────────────────────────────────────────┘ │
└────────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ DATABASE LAYER │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Database Constraints (@api.constrains) │ │
│ │ - _check_source_key() │ │
│ │ - _check_value_field() │ │
│ │ - _check_custom_text() │ │
│ │ - _check_custom_cert_mappings() │ │
│ └──────────────────────────────────────────────────────────┘ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Data Integrity │ │
│ │ - Foreign key constraints │ │
│ │ - Required field enforcement │ │
│ │ - Data type validation │ │
│ └──────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
```
## Data Flow with Security Checks
```
User Input
│
▼
┌─────────────────────┐
│ UI Validation │ ← Field constraints, required fields
└──────┬──────────────┘
│
▼
┌─────────────────────┐
│ Access Control │ ← Check user permissions
└──────┬──────────────┘
│
▼
┌─────────────────────┐
│ Input Validation │ ← Validate format, length, pattern
└──────┬──────────────┘
│
▼
┌─────────────────────┐
│ Input Sanitization │ ← Escape HTML, remove control chars
└──────┬──────────────┘
│
▼
┌─────────────────────┐
│ Business Logic │ ← Apply business rules
└──────┬──────────────┘
│
▼
┌─────────────────────┐
│ Database Constraint │ ← Final validation at DB level
└──────┬──────────────┘
│
▼
Database
```
## Attack Surface and Mitigations
```
┌──────────────────────────────────────────────────────────────────┐
│ ATTACK VECTORS │
└──────────────────────────────────────────────────────────────────┘
1. XSS (Cross-Site Scripting)
Input:
↓
Mitigation: HTML escaping + tag stripping
↓
Output: <script>alert('XSS')</script> → (stripped)
2. SQL Injection
Input: field'; DROP TABLE users--
↓
Mitigation: Character whitelisting (alphanumeric, _, .)
↓
Result: REJECTED (invalid characters)
3. Command Injection
Input: field; rm -rf /
↓
Mitigation: Pattern validation + character whitelisting
↓
Result: REJECTED (invalid format)
4. Path Traversal
Input: ../../etc/passwd
↓
Mitigation: No path separators allowed in field names
↓
Result: REJECTED (invalid characters)
5. DoS (Denial of Service)
Input: 100MB file or 1,000,000 character text
↓
Mitigation: File size limit (10MB) + text length limit (1000 chars)
↓
Result: REJECTED (exceeds limits)
6. Data Corruption
Input: Malformed JSON structure
↓
Mitigation: JSON validation + structure validation
↓
Result: REJECTED (invalid structure)
```
## Access Control Matrix
```
┌────────────────────────────────────────────────────────────────┐
│ PERMISSION MATRIX │
├────────────────┬───────────┬───────────┬──────────┬────────────┤
│ Resource │ Manager │ User │ Portal │ Public │
├────────────────┼───────────┼───────────┼──────────┼────────────┤
│ Wizard │ CRUD │ R (own) │ None │ None │
│ Placeholder │ CRUD │ R (own) │ None │ None │
│ Survey (cert) │ CRUD │ R (own) │ None │ None │
│ Template File │ RW │ None │ None │ None │
│ Mappings │ RW │ None │ None │ None │
│ Has Cert Flag │ RW │ R │ None │ None │
└────────────────┴───────────┴───────────┴──────────┴────────────┘
Legend:
C = Create, R = Read, U = Update, D = Delete, W = Write
(own) = Only records created by the user
```
## Validation Pipeline
```
┌─────────────────────────────────────────────────────────────────┐
│ VALIDATION PIPELINE │
└─────────────────────────────────────────────────────────────────┘
Placeholder Key: {key.field_name}
│
├─► Length Check (max 200 chars) ──────────► PASS/FAIL
│
├─► Pattern Match (regex) ─────────────────► PASS/FAIL
│ Pattern: ^\{key\.[a-zA-Z0-9_]+\}$
│
└─► Character Whitelist ───────────────────► PASS/FAIL
Allowed: letters, numbers, _, {, }, .
Value Field: partner_id.name
│
├─► Length Check (max 200 chars) ──────────► PASS/FAIL
│
├─► Character Whitelist ───────────────────► PASS/FAIL
│ Allowed: letters, numbers, _, .
│
└─► No Special Chars ──────────────────────► PASS/FAIL
Rejected: -, ', ", ;, <, >, etc.
Custom Text: "Sample text"
│
├─► Length Check (max 1000 chars) ─────────► PASS/FAIL
│
├─► HTML Escape ───────────────────────────► SANITIZED
│ < → <, > → >, etc.
│
├─► Control Char Removal ──────────────────► SANITIZED
│ Remove: \x00-\x08, \x0B-\x0C, etc.
│
└─► Tag Stripping ─────────────────────────► SANITIZED
Remove: