In my Apache configuration file, I am currently blocking potentially malicious user agents using the following config:
SetEnvIfNoCase User-Agent "^\W" badagent
<Location />
Deny from env=badagent
</Location>
This will deny all requests with a user agent that begins with anything except for a-Z or 0-9. A 403 Forbidden error is displayed.
I would like to change my configuration in order to be able to provide a different 403 ErrorDocument for different blocking reasons.
I am aware that it might not always be a good idea to tell somebody the exact security reason for their request being blocked, but this is just in case a legitimate user has their request blocked. I want them to know what the problem is instead of just seeing a standard 403. If somebody is attacking my site, they most likely know why their request is being blocked.
For example:
Request blocked due to malformed user agent.
Request blocked due to disallowed request method.
Request blocked due to hotlinking.
In order to do this, I must use If statements within my config:
<If "%{HTTP_USER_AGENT} == '^\W'">
ErrorDocument 403 "403 Forbidden - Request blocked due to malformed user agent."
Require all denied
</If>
My concern with this is that the HTTP_USER_AGENT variable could be used for command injection. I ran commix on an offline test server with this configuration and it did not detect anything, but I want to be sure.
I struggled to find any conclusive documentation on if/how Apache sanitizes variables from a HTTP request.
I am running a plain HTML/CSS website, no PHP or anything like that. Standard Apache installation with various hardening configurations and unneeded modules disabled.
Could the If statement used above be vulnerable to command injection, and if so, how do I protect against it?