Course contentsShow
← Security
Lesson 879 of 3,05218. Web Security - CSRF and Request ForgeryFree lesson

Subdomain and Partial Origin Validation Bypasses

Exploiting weak regex patterns and substring matching in origin validation logic.

Subdomain and Partial Origin Validation Bypasses

What you'll learn: How attackers exploit weak pattern matching in origin validation to bypass CORS protections.

The Core Problem

When applications validate the Origin header to prevent CORS attacks, some developers use flawed validation logic that checks for partial string matches or weak regular expressions instead of exact origin comparison. This creates bypasses where attackers can craft malicious origins that "pass" the validation even though they shouldn't.

How Weak Validation Works

Imagine a bouncer checking if someone's name contains "Smith" to let them into a VIP party meant only for the Smith family. Someone named "John Smitherson" or "Blacksmith" would get in—clearly not the intent!

Similarly, servers might validate origins like this:

Vulnerable pattern: Check if origin contains trusted-site.com

This allows attackers to register domains like:

  • trusted-site.com.evil.com
  • evil-trusted-site.com
  • trusted-site.com.attacker.net

All these would pass a naive substring check or poorly written regex like .*trusted-site\.com.*

Common Weak Patterns

  • Substring matching: if (origin.includes('trusted.com'))
  • Prefix-only checks: if (origin.startsWith('https://trusted.com'))
    Bypassed by: https://trusted.com.evil.com
  • Suffix-only checks: if (origin.endsWith('trusted.com'))
    Bypassed by: https://evil-trusted.com
  • Loose regex: /trusted\.com/ without anchors
  • Subdomain wildcards: Accepting *.trusted.com when some subdomains are untrusted

Real Attack Scenario

If api.bank.com allows any origin containing "bank.com", an attacker controlling bank.com.phishing.net could make cross-origin requests with credentials, stealing sensitive data.

Key Takeaway: Origin validation must use exact matching or properly anchored regex patterns that verify the complete origin string, not just substring presence. Always validate the full scheme, domain, and port combination.