tomMoulard / htransformation

A Traefik plugin to change on the fly header's value of a request
MIT License
81 stars 13 forks source link

Setting X-Real-Ip from values in X-Forwarded-For #54

Open alecor191 opened 10 months ago

alecor191 commented 10 months ago

I have the following requirement and was wondering if htransformation could be used to solve it: Traefik receives requests that contain the following header:

X-Forwarded-For: 62.123.23.32:63417, 172.22.0.4

What I would like to do, is add the following header, using the value of X-Forwarded-For:

X-Real-Ip: 62.123.23.32

I was thinking about an approach like:

  1. clone X-Forwarded-For header as X-Real-Ip
  2. using RewriteValueRule tweak the value of X-Real-Ip to only include the first IP address from the list.

What I can't figure out is 1.: I thought I could use Set with HeaderPrefix to reference the value of another header. However, it seems HeaderPrefix is only supported by Join. When using Join however, X-Real-Ip is not set - maybe because Join requires the header to already be there (assumption based on join.go implementation)?

Do you have any thoughts on whether it's feasible to solve the above using htransformation and if so, how?