# THREAT MODEL: Personal Knowledge-Sharing Platform (Static-First on Shared Hosting)

Date: 2026-11-21  
Methodology: STRIDE + OWASP Top 10:2025 Mapping  
Target Environment: Shared Hosting (Apache/LiteSpeed + PHP 8.4+ + MariaDB) behind Cloudflare Proxy.

---

## 1. Asset & Scope Definition

### Primary Assets:
1. **Administrative Access**: Owner session tokens, Passkeys, TOTP secrets, Argon2id password hashes.
2. **Data Integrity**: Published posts, revisions, media uploads, database records, audit logs.
3. **Availability**: Public reading capability (must withstand high traffic spikes & DoS attempts).
4. **Secrets & Configurations**: Database credentials, Telegram bot token, HMAC/Encryption keys, Cloudflare API tokens.

### Out of Scope:
- Host-level zero-day privilege escalations inside the shared hosting kernel.
- Physical infrastructure attacks on the hosting data center or Cloudflare edge servers.

---

## 2. STRIDE Threat Analysis

### S - Spoofing (Giả mạo danh tính)
- **Threat S1: Attacker impersonates the admin to gain control.**
  - *Vector*: Credential stuffing, brute-force login, session hijacking.
  - *Mitigations*:
    - Primary WebAuthn / Passkeys (phishing-resistant FIDO2 credentials).
    - Secondary MFA (Argon2id + TOTP RFC 6238 + Single-use Recovery Codes).
    - Hardened sessions stored in Database (`__Host-` prefixed cookies, `HttpOnly`, `SameSite=Strict`, `Secure`).
    - Progressive IP/account backoff rate limiting, lockouts, and instant Telegram/Email alerts.
- **Threat S2: Attacker spoofs client IP address to bypass rate limits or country restrictions.**
  - *Vector*: Forged `X-Forwarded-For` or `CF-Connecting-IP` headers directly to origin.
  - *Mitigations*:
    - Only trust `CF-Connecting-IP` if the incoming connection originates from Cloudflare's verified IP ranges.
    - Origin `.htaccess` allows traffic strictly from Cloudflare CIDR blocks.

---

### T - Tampering (Can thiệp & làm sai lệch dữ liệu)
- **Threat T1: Cross-Site Scripting (Stored XSS via Markdown/Article content).**
  - *Vector*: Malicious script tags or event handlers injected via Markdown body.
  - *Mitigations*:
    - Server-side Markdown parsing with raw HTML disabled.
    - Strict HTML sanitizer allowlist (strips `javascript:`, `data:` URI schemes, all `on*` event handlers).
    - Static HTML pages served without user-supplied dynamic injections.
    - Strict CSP Level 3: `default-src 'none'; script-src 'self'; style-src 'self'; frame-ancestors 'none';`.
- **Threat T2: Malicious file upload executing code on shared host.**
  - *Vector*: PHP shell hidden inside image extension or EXIF metadata.
  - *Mitigations*:
    - Strict MIME & magic-byte validation (`finfo`, `getimagesize`).
    - Compulsory re-encoding of uploaded images via GD/Imagick (strips all EXIF, metadata, and embedded payloads).
    - Random filename generation (discard original filename).
    - `.htaccess` in `uploads/` disabling PHP execution engine completely (`SetHandler default-handler`, `php_flag engine off`).
- **Threat T3: Unauthorized file modification or web-shell injection.**
  - *Vector*: Neighboring compromised account on shared host modifying files.
  - *Mitigations*:
    - Application code placed outside `public_html` root where possible.
    - File permissions (0755 for dirs, 0644 for files, 0600 for configs).
    - Automated cron integrity scanner computing SHA-256 baseline hashes of all PHP files.

---

### R - Repudiation (Chối bỏ trách nhiệm)
- **Threat R1: Admin actions or compromised operations performed without audit trail.**
  - *Vector*: Attacker deletes posts, alters settings, or modifies users without traces.
  - *Mitigations*:
    - Dedicated, append-only `audit_log` table storing timestamp, actor UUID, action, entity, and salted/hashed IP.
    - Instant push alerts to Telegram bot on sensitive modifications (password reset, passkey changes, role elevation).

---

### I - Information Disclosure (Lộ lọt thông tin)
- **Threat I1: Exposure of `.env`, configuration files, database dumps, or logs.**
  - *Vector*: Direct web request to sensitive paths (e.g., `/.env`, `/storage/logs/`, `/backup.sql`).
  - *Mitigations*:
    - All code, configs, logs, and backups stored above `public_html` (`~/app/`, `~/storage/`).
    - Web root `.htaccess` explicitly blocks hidden files, `.env`, `.sql`, `.log`, `.ini`, `.bak`, `.json`, `.lock`.
    - `display_errors = Off` in PHP config; global exception handler returns sanitized generic error with correlation ID.
- **Threat I2: Discovery of admin panel location.**
  - *Vector*: Directory fuzzing and scanning tools.
  - *Mitigations*:
    - Dynamic secret URI path (`/[ADMIN_PATH]`).
    - Rate limit on 404s to detect and block aggressive directory scanners.
    - Cloudflare WAF Managed Challenge on admin URI from unexpected geographic zones.

---

### D - Denial of Service (Từ chối dịch vụ / Làm cạn kiệt tài nguyên)
- **Threat D1: HTTP flood or traffic spike exhausting shared hosting CPU/RAM limits.**
  - *Vector*: High-volume GET requests targeting article pages.
  - *Mitigations*:
    - **Static-First Architecture**: 95%+ of public requests are served as pre-rendered static HTML files (`public_html/cache/`) via direct web server rewrite without invoking PHP or MariaDB.
    - Cloudflare Edge Caching: `Cache-Control: public, s-maxage=31536000, stale-while-revalidate=86400, stale-if-error=604800`.
    - Emergency Mode flag: locks all dynamic endpoints, serves static files exclusively.
- **Threat D2: Resource exhaustion via dynamic search or dynamic image resizing.**
  - *Vector*: Repeated complex SQL queries or on-the-fly thumbnail generation requests.
  - *Mitigations*:
    - Client-side static JSON search: pre-rendered static index loaded on-demand, searched via local JS. No public dynamic search endpoint.
    - Pre-generated image thumbnails (`srcset`) created at upload time, never on-the-fly.
- **Threat D3: Admin brute-force DoS.**
  - *Vector*: Concurrently spamming the login endpoint.
  - *Mitigations*:
    - Cloudflare Rate Limiting Rule + Application token-bucket rate limiter.
    - Cloudflare Turnstile verification challenge.

---

### E - Elevation of Privilege (Leo thang đặc quyền / IDOR)
- **Threat E1: Unauthorized access to admin endpoints or individual resources.**
  - *Vector*: Bypassing authentication checks or manipulating object IDs (Insecure Direct Object References).
  - *Mitigations*:
    - Strict Deny-by-Default routing architecture.
    - Global Admin Authentication Middleware wrapping all management routes.
    - UUID / strict authorization verification on every mutation (posts, media, settings).
    - Step-up authentication (re-authenticating passkey/password) required for critical operations.

---

## 3. Residual Risks & Ongoing Controls

| Risk | Inherent Severity | Residual Risk Level | Mitigating Control |
|---|---|---|---|
| Hosting Neighbor Compromise | High | Low - Medium | File permissions (0600), isolated storage outside web root, file integrity monitoring cron |
| L3/L4 Terabit DDoS | Critical | Low | Cloudflare Enterprise-grade proxy & Anycast network absorbs network floods |
| Shared IP Blacklisting | Medium | Low | Outbound alerting via external Telegram API and SMTP relay, not local host IP |
| Zero-Day in Cloudflare Turnstile/WebAuthn | Low | Low | Defense-in-depth: Multiple layers (MFA, password, IP verification) |
