Course contentsShow
← Computer Science
Lesson 2470 of 2,87255. Web SecurityFree lesson

Origin and Referer Header Validation

Checking Origin and Referer headers to verify requests originate from trusted sources, with limitations and considerations.

Origin and Referer Header Validation

What you'll learn: How to use Origin and Referer HTTP headers as an additional layer of defense against CSRF attacks, along with their inherent limitations.

The Core Idea

When a browser makes a request, it often includes headers that indicate where the request came from. The Origin header contains the scheme and domain of the page that initiated the request (e.g., https://yourbank.com), while the Referer header includes the full URL of the referring page. By validating these headers on the server side, you can check whether requests are coming from your own trusted site or from an attacker's domain.

How It Works

When processing state-changing requests (POST, PUT, DELETE), your server can:

  1. Check the Origin header matches your expected domain
  2. Fall back to Referer validation if Origin is absent
  3. Reject the request if neither header is present or they indicate an untrusted source

Think of it like a bouncer checking IDs at a club entrance—you're verifying that visitors came from a legitimate location.

Limitations and Considerations

This approach has significant weaknesses:

  • Privacy tools and browser settings may strip these headers
  • Some proxies and firewalls remove or modify them
  • Legitimate users may be blocked if headers are missing
  • HTTPS-to-HTTP transitions often drop the Referer header
  • These headers can be spoofed in certain attack scenarios (though not in typical CSRF contexts where the attacker controls only their own site)

This technique works best as a defense-in-depth measure, not a primary CSRF protection. Always combine it with CSRF tokens or SameSite cookies.

Key Takeaway: Origin and Referer header validation provides a secondary defense layer against CSRF by verifying request source, but should never be relied upon alone due to legitimate cases where headers are missing or stripped.