Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

Write ModSecurity rule to block request with 403 if no Referer or Origin header present

  1. User

    Write ModSecurity rule to block request with 403 if no Referer or Origin header present

  2. Assistant

    Certainly! Below is a ModSecurity rule that will block HTTP requests with a 403 Forbidden status if neither the Referer nor the Origin headers 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 the Referer header is not present (zero occurrences).
    • chain means the next rule applies only if the current condition is true.
    • &REQUEST_HEADERS:Origin "@eq 0" checks if the Origin header is not present.
    • id:12345 is a unique identifier for the rule (change this ID as per your numbering scheme).
    • phase:1 executes the rule in the request headers phase.
    • deny,status:403 blocks the request with HTTP 403 Forbidden status.
    • msg provides 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 Referer and Origin headers is zero, effectively meaning both headers are absent.


    Let me know if you need it adapted for a specific context or ModSecurity version!