gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
Write ModSecurity rule to block request with 403 if no Referer or Origin header present
- User
Write ModSecurity rule to block request with 403 if no Referer or Origin header present
- Assistant
Certainly! Below is a ModSecurity rule that will block HTTP requests with a 403 Forbidden status if neither the
Referernor theOriginheaders are present.SecRule &REQUEST_HEADERS:Referer "@eq 0" \ "chain, \ SecRule &REQUEST_HEADERS:Origin \"@eq 0\" \ ,id:12345,phase:1,deny,status:403,msg:'Missing Referer and Origin headers'"Explanation:
&REQUEST_HEADERS:Referer "@eq 0"checks if theRefererheader is not present (zero occurrences).chainmeans the next rule applies only if the current condition is true.&REQUEST_HEADERS:Origin "@eq 0"checks if theOriginheader is not present.id:12345is a unique identifier for the rule (change this ID as per your numbering scheme).phase:1executes the rule in the request headers phase.deny,status:403blocks the request with HTTP 403 Forbidden status.msgprovides a message for the audit log.
If you want a more compact single line version:
SecRule &(REQUEST_HEADERS:Referer) + &(REQUEST_HEADERS:Origin) "@eq 0" "id:12345,phase:1,deny,status:403,msg:'Missing Referer and Origin headers'"This checks if the sum of the occurrence counts of
RefererandOriginheaders is zero, effectively meaning both headers are absent.
Let me know if you need it adapted for a specific context or ModSecurity version!