Security Boundary Review: XSS, CORS, and Authentication Boundaries
Practical 13 - Security Boundary Review: XSS, CORS, and Authentication Boundaries
Related: Chapter 13 · Lecture slides
Objective
Conduct a rigorous threat-model review and hardening exercise on a simulated front-end application boundary. You will identify real-world vulnerabilities across Cross-Site Scripting (XSS), Cross-Origin Resource Sharing (CORS), Cross-Site Request Forgery (CSRF), and authentication token management.
By completing this laboratory, you will:
- Trace Untrusted Input from Source to Sink: Track untrusted strings from URL parameters, API payloads, and form inputs into dangerous DOM execution sinks.
- Eliminate DOM-Based XSS: Replace dangerous injection sinks with safe text rendering, context-aware encoding, and reviewed sanitization.
- Demystify the CORS Boundary: Demonstrate why CORS is a browser-enforced response isolation mechanism rather than a server authorization check.
- Harden Mutation Endpoints against CSRF: Configure
SameSitecookie attributes and custom request headers to eliminate cross-site forged mutations. - Architect Secure Credential Storage: Evaluate the security trade-offs of browser-held bearer tokens in
localStorageversusHttpOnly, Secure, SameSitecookies mediated by a Backend-for-Frontend (BFF).
Workspace Setup
Create a minimal Node.js / TypeScript security test harness:
Ensure your tsconfig.json targets ES2022 with "lib": ["DOM", "ES2022"].
Stage-by-Stage Implementation
Stage 1: Tracing Untrusted Input to Dangerous DOM Sinks
In src/vulnerableSearch.ts, examine this typical search results component:
Exploit Simulation:
If an attacker crafts a malicious link:
https://portal.erbil.gov.krd/search?q=<img src=x onerror="alert(document.cookie)">
When renderSearchSummary receives this string, the browser parses the <img> tag, fails to load src=x, and immediately executes the onerror JavaScript payload inside the trusted origin’s context.
Stage 2: Hardening the Sink (Safe Text & Sanitization)
Refactor renderSearchSummary to enforce Safe Sinks by Default:
Stage 3: Demystifying CORS and Preflight Handshakes
A frequent security misconception is believing that CORS protects APIs against unauthorized access.
In src/corsVerification.ts, simulate a cross-origin HTTP interaction:
Key Architectural Lesson:
If an attacker sends a cross-origin POST /api/permits/104/delete from https://malicious-site.com, a naive server without CSRF defenses will execute the deletion in its database before sending the response back. The browser will then block malicious-site.com from reading the response due to CORS, but the damage is already done!
Stage 4: CSRF Hardening for Cookie-Based Authentication
To prevent cross-site forged mutations, enforce a two-tier defense:
Stage 5: Credential Storage Architecture (BFF vs. LocalStorage)
Compare the security boundaries of single-page application credential architectures:
Verification and Testing Matrix
Validate your implementations against these required test assertions in tests/security.test.ts:
| Test ID | Vulnerability / Target | Verification Procedure | Expected Security Outcome |
|---|---|---|---|
| SEC-01 | Reflected XSS in search input | Feed <script>alert(1)</script> into renderSearchSummarySafe | Rendered as plain text; zero script elements in DOM. |
| SEC-02 | HTML Attribute Event Injection | Feed <img src=x onerror=alert(1)> into renderRichMunicipalAnnouncement | DOMPurify strips onerror; tag rendered safely or removed. |
| SEC-03 | javascript: URI Injection | Feed <a href="javascript:steal()">Click</a> into DOMPurify | javascript: protocol stripped; link neutralized. |
| SEC-04 | Cookie Security Attributes | Inspect generateSessionCookie output | httpOnly === true, secure === true, sameSite === 'Lax'. |
| SEC-05 | CSRF Header Gate | Pass request headers without x-csrf-token to mutation verifier | Request rejected with HTTP 403 Forbidden. |
| SEC-06 | Server-Side Authorization | Inspect simulated client-side route guard | Verify client guard only controls UI display; API verifies JWT scopes. |
Deliverables & Submission Checklist
-
src/secureSearch.ts: Hardened rendering functions utilizing safe text nodes and DOMPurify. -
src/corsVerification.ts: Documented analysis of browser CORS enforcement mechanics. -
src/csrfProtection.ts: Secure cookie generator and custom header CSRF validation logic. -
tests/security.test.ts: Automated test suite passing all 6 assertions in the verification matrix. - Architectural brief summarizing why client-side route guards can never enforce security authorization.