Bug 38019 - SetEnv can't be used before SetEnvIf
Summary: SetEnv can't be used before SetEnvIf
Status: RESOLVED WONTFIX
Alias: None
Product: Apache httpd-2
Classification: Unclassified
Component: mod_env (show other bugs)
Version: 2.5-HEAD
Hardware: All All
: P3 normal (vote)
Target Milestone: ---
Assignee: Apache HTTPD Bugs Mailing List
URL:
Keywords: PatchAvailable
Depends on:
Blocks:
 
Reported: 2005-12-22 22:44 UTC by Chris Darroch
Modified: 2022-11-19 18:33 UTC (History)
1 user (show)



Attachments
run during post_read_request, before mod_setenvif (714 bytes, patch)
2005-12-22 22:46 UTC, Chris Darroch
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Darroch 2005-12-22 22:44:57 UTC
I have a particular setup where what I'd like to do is reject all requests
that contain a particular HTTP header (in this case, a header injected by
hardware that means the request is coming from outside our private network).
Here's what I thought I could do:

SetEnv FOO 1
SetEnvIf Http-X-Foo .+ !FOO
<Directory /foo>
    Allow from env=FOO
</Directory>

The logic being, set FOO=1, then unset FOO if the HTTP header is present,
and only allow access to a resource if FOO is still present.  Unfortunately,
it seems that mod_env adds its values to r->subprocess_env during the
fixups phase, which means that any values it sets come after those set
by mod_setenvif, which runs during the post_read_request phase.  Also, in
this particular instance, it also means that it's too late for the
auth_checker phase as well, and the request is rejected before mod_env's
fixups handler even runs.

The attached patch simply changes mod_env to run during the post_read_request
phase, before mod_setenvif.  This allows the logic I outlined above to
work nicely.

Now, there may be historical reasons why this isn't desired, and it might,
I suppose, also cause existing users' configurations to malfunction.
Still, I'd propose it as a 2.3/2.4 change, because it does enable
functionality like that I outlined above, and more generally, it makes
sense that all of the SetEnv* and PassEnv*, etc., directives would take
effect at the same time.

If this change isn't accepted, then I would strongly suggest changing the
documentation regarding environment variables to indicate the order in which
directives take effect; at the moment, that's not clear.  In fact, the
current documentation says the following, which may be true for the
special purpose environment variables listed on the page, but isn't true
in general for variables used in other ways (e.g., in "Allow from env").

"To make these mechanisms as flexible as possible, they are invoked by
defining environment variables, typically with BrowserMatch, though SetEnv
and PassEnv could also be used, for example."
Comment 1 Chris Darroch 2005-12-22 22:46:12 UTC
Created attachment 17264 [details]
run during post_read_request, before mod_setenvif
Comment 2 Chris Darroch 2005-12-22 22:52:18 UTC
This bug has been reported a few times before, most recently as 36908.
It always gets marked WON'T FIX, which is tolerable, I suppose, but then
I strongly urge that the documentation be updated, in particular the section
I outlined, which suggests to the reader that SetEnv and SetEnvIf take
effect at the same time.
Comment 3 Aleksey Midenkov 2007-10-03 04:37:44 UTC
43540 patch adds SetEnv2 to mod_setenvif to fix this problem
Comment 5 Eric Covener 2015-01-16 20:41:23 UTC
(old ticket cleanup)

It's too disruptive to change the order.  The manual now states in env.html:

The SetEnv directive runs late during request processing meaning that directives such as SetEnvIf and RewriteCond will not see the variables set with it.
Comment 6 Steven 2022-11-13 07:43:06 UTC
It remains annoying that the instinctive configuration 

SetEnv VAR_ALPHA mydefault
SetEnvIf Host example\.com VAR_ALPHA=notmydefault

does not work as expected. It steals time from many developers until you notice it.

And the sentence "It's too disruptive to change the order." suggests that something would have to be fundamentally revised here.
Comment 7 Christophe JAILLET 2022-11-19 18:33:18 UTC
(In reply to Steven from comment #6)
> It remains annoying that the instinctive configuration does not work as
> expected.

Maybe, but the behaviour is documented.
Should the wording be improved or should something be explained elsewhere, could you please make a proposal?


Also, does the following (untested) achieve what you need?

SetEnvIf Host .*           VAR_ALPHA=mydefault
SetEnvIf Host example\.com VAR_ALPHA=notmydefault

If yes, maybe it could be added as an example to the doc.