# Cybersecurity Architecture and Defense Plan
## Kampong Speu Provincial Demographic Management System (KSP-DMS)

**Document Code:** KSP-DMS-DOC-SEC-01  
**Security Standard:** OWASP Top 10 (2021) & CIS Controls Compliant  
**Version:** 1.0 (Draft Proposal)  

---

## 1. Threat Modeling and Defense-in-Depth

As a provincial demographic platform handling civic records, KSP-DMS employs a comprehensive defense-in-depth model spanning infrastructure, transport, application, and database layers:

```
+-----------------------------------------------------------------------------------+
| 1. NETWORK & INFRASTRUCTURE DEFENSE                                               |
|    - Local Intranet Binding (127.0.0.1 / Provincial Subnet only)                  |
|    - Apache Hardening: Disabled Directory Listing, Server Signature Suppressed    |
+-----------------------------------------------------------------------------------+
                                          |
+-----------------------------------------v-----------------------------------------+
| 2. TRANSPORT & SESSION SECURITY                                                   |
|    - Strict HTTPS / TLS 1.3 Transport Encryption                                  |
|    - Secure, HttpOnly, SameSite=Lax Session Cookies                               |
|    - Session Invalidation on Privilege Elevation / Logout                         |
+-----------------------------------------------------------------------------------+
                                          |
+-----------------------------------------v-----------------------------------------+
| 3. APPLICATION & INPUT HYGIENE                                                    |
|    - Automatic CSRF Token Verification on 100% of State-Modifying POST/PUT/DELETE |
|    - Strict FormRequest Whitelisting (Mass-Assignment Protection)                 |
|    - Blade Automatic Contextual HTML Escaping (XSS Prevention)                    |
|    - Rate Limiting (Throttle Middleware: 60 req/min Web, 30 req/min API)          |
+-----------------------------------------------------------------------------------+
                                          |
+-----------------------------------------v-----------------------------------------+
| 4. DATA & DATABASE LAYER DEFENSE                                                  |
|    - 100% Prepared Statements via PDO (Complete SQL Injection Immunity)           |
|    - Bcrypt Password Hashing (Work factor 12)                                     |
|    - Geographic Global Scopes (Prevents Insecure Direct Object References - IDOR) |
|    - Immutable Append-Only Audit Logging with Client IP and User Agent            |
+-----------------------------------------------------------------------------------+
```

---

## 2. Specific Security Controls & Mitigations

### 2.1 SQL Injection Defense
- **Control:** Raw SQL queries are strictly prohibited in application business logic.
- **Standard:** All database interactions utilize Laravel's Eloquent ORM or parameterized Query Builder with PDO prepared statements.

### 2.2 Cross-Site Scripting (XSS) Prevention
- **Control:** Blade templating syntax `{{ $variable }}` is utilized universally, which invokes `htmlspecialchars(..., ENT_QUOTES, 'UTF-8')`.
- **Content-Security-Policy (CSP):** The application sends security headers restricting script sources to local domains.

### 2.3 Insecure Direct Object Reference (IDOR) Mitigation
- **Vulnerability Scenario:** A malicious user modifies a URL parameter (e.g., `/communes/14/households` to `/communes/28/households`).
- **Mitigation:** Laravel Authorization Policies (`HouseholdPolicy`, `SubmissionPolicy`) verify both role permissions and geographic ownership before returning any records.

### 2.4 Brute Force & Credential Stuffing Defense
- **Control:** Laravel rate limiter restricts authentication attempts to a maximum of 5 failed attempts per IP/username per minute.
- **Account Lockout:** Accounts experiencing repeated failures enter a temporary 15-minute cooling period, logged in `audit_logs`.

### 2.5 Secret Management
- **Rule:** No API keys, passwords, database credentials, or Sanctum tokens are committed to source control.
- **Enforcement:** `.env` is strictly git-ignored; `.env.example` contains only non-sensitive placeholders.

---

## 3. Security Auditing and Incident Response

### 3.1 Immutable Audit Log Schema
The `audit_logs` table records:
- `user_id`: Operator performing the action.
- `action`: Created, Updated, Deleted, Submitted, Approved, Rejected.
- `model_type` & `model_id`: Affected entity.
- `old_values`: JSON serialized representation of previous state.
- `new_values`: JSON serialized representation of resulting state.
- `ip_address`: Source network identifier.
- `user_agent`: Client browser signature.
- `created_at`: Cryptographically verified server timestamp.

### 3.2 Security Incident Escalation Flow
1. **Detection:** Automated anomaly detection or repeated 403 authorization failures trigger an alert.
2. **Containment:** System Administrator can instantly revoke the user session or disable the account.
3. **Investigation:** Data Protection Officer reviews audit trails and diff logs.
4. **Remediation & Review:** Vulnerability patched, test cases added to prevent regression.
