Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to base64 encode apache header?

I'm using apache as a frontend http proxy. I need to send a header with a value from a specific environment variable to all proxied request. Like this:

RequestHeader set myheader %{FOO}e

The issue is that the value from the env variable FOO must be base64 encoded. Is it possible to b64-encode that header value on the fly?

mod_rewrite? subrequest? a custom module? ...

Btw, the env variable is created by another apache module, which I cannot fix unfortunately.

like image 589
Martin Wickman Avatar asked Jan 12 '23 12:01

Martin Wickman


1 Answers

I figured it out. Using mod_rewrite to read/write stdin/stdout from an external program like this:

ProxyRequests on
RewriteEngine on

# 1 
RewriteMap base64map "prg:/bin/b64e" 

# 2
RewriteRule .* - [E=WIC:${base64map:%{QUERY_STRING}},NE]  

# 3
RequestHeader set x-b64encoded "%{ENV:WIC}e" 

# 4
RewriteRule ^proxy/.*$ http://localhost:9999 [P]
  1. Create a rewrite-map named base64map which runs the executable (/bin/b64e) which reads stdin and encodes on stdout. The executable is loaded when apache starts and you have to make a loop reading from the stdin and passing it through the / bin / base64 for it to work well
  2. The rewrite rule passes the query string (or whatever you want) to the mapping named base64map. The output is used to set the apache variable WIC to the encoded value.
  3. The request header x-b64encoded is created from the value in WIC To correctly read the WIC variable we had to change the syntax to ENV: WIC
  4. Finally, the request is proxied to the destination and the header is automatically included with the request.
like image 125
Martin Wickman Avatar answered Jan 21 '23 21:01

Martin Wickman