Home > Mobile >  Filter (echo/copy) request headers to response headers with regular expressions in apache and perfor
Filter (echo/copy) request headers to response headers with regular expressions in apache and perfor

Time:06-16

I have a requirement to send a response header by filtering the request header string in apache. To filter the string, a regular expression can be used.

The echo command is working fine to send the response header from the request header.

Reference URL to use echo command https://httpd.apache.org/docs/current/mod/mod_headers.html#page-header

Ex.

Header echo ^group 

Now we have a requirement to filter the groups with regular expression.

Ex.

Header echo ^group <filter group with regular expression>

Regex needs to be used is: /(?<=,)[^,]*MYROLE[^,] (?=,)/gm

example of group string: "AV=en:CN=(A) MYROLE - Application, AV=en:CN=(A) DATA - Application";

I tried given below syntax but it's not working

Header echo group "%{HTTP:group}i m#/(?<=,)[^,]*MYROLE[^,] (?=,)/#"

Expected Request and Response Dump.

Request

HTTP/1.1 200 OK

Cache-Control: private

group: OU=Groups Azure Virtual Desktop,OU=PROD,DC=prod,DC=test,DC=be:CN=(A) MYROLE - BO - TC BO Admin,OU=Groups XYZ Application Entitlements,OU=PROD,DC=prod,DC=test,DC=be:CN=(A) MYROLE - BO - ABO,OU=Groups XYZ Application Entitlements,OU=PROD,DC=prod,DC=test,DC=be:CN=(A) MYROLE - BO - TBO,OU=Groups XYZ Application Entitlements,OU=PROD,DC=prod,DC=test,DC=be:CN=(A) MYROLE - BO - B and C,OU=Groups XYZ Application Entitlements,OU=PROD,DC=prod,DC=test,DC=be:CN=(C) Adobe Fullstack Engineers

Expected Response

HTTP/1.1 200 OK

Cache-Control: private

group: DC=be:CN=(A) MYROLE - BO - TC BO Admin,DC=be:CN=(A) MYROLE - BO - ABO,DC=be:CN=(A) MYROLE - BO - TBO,CN=(A) MYROLE - BO - B and C

Please suggest how to use regular expression to filter the string(group) and send it back as as comma separated groups as response header.

CodePudding user response:

This was a challenge, I must admit first.

After digging through the official docs for more than an hour, I have come up to this solution. Please read my inline comments to understand the solution:

# copy header named group from request to response
Header echo "^group$"

# use regex to remove all comma separated substrings
# as long as those substrings don't contain string MYROLE
Header edit* group "(?:^|,)(?![^,]*?MYROLE)[^,] " ""

# remove first comma if it exists
Header edit group "^," ""

On my local Apache I tested it with command line curl and here are the results:

curl -H 'group: OU=Groups Azure Virtual Desktop,OU=PROD,DC=prod,DC=test,DC=be:CN=(A) MYROLE - BO - TC BO Admin,OU=Groups XYZ Application Entitlements,OU=PROD,DC=prod,DC=test,DC=be:CN=(A) MYROLE - BO - ABO,OU=Groups XYZ Application Entitlements,OU=PROD,DC=prod,DC=test,DC=be:CN=(A) MYROLE - BO - TBO,OU=Groups XYZ Application Entitlements,OU=PROD,DC=prod,DC=test,DC=be:CN=(A) MYROLE - BO - B and C,OU=Groups XYZ Application Entitlements,OU=PROD,DC=prod,DC=test,DC=be:CN=(C) Adobe Fullstack Engineers' -H 'mygroup: foobar' -IL 'http://localhost/'
HTTP/1.1 200 OK
Date: Wed, 15 Jun 2022 19:15:19 GMT
Server: Apache/2.4.53 (Unix) OpenSSL/1.1.1o PHP/8.1.6
X-Powered-By: PHP/8.1.6
group: DC=be:CN=(A) MYROLE - BO - TC BO Admin,DC=be:CN=(A) MYROLE - BO - ABO,DC=be:CN=(A) MYROLE - BO - TBO,DC=be:CN=(A) MYROLE - BO - B and C
Content-Type: text/html; charset=UTF-8

RegEx Demo of regex used in 2nd Header command

  • Related