tomMoulard / htransformation

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

Unable to replace all instances of a given string in request header #72

Open jvanheesch opened 5 months ago

jvanheesch commented 5 months ago

Reproducer can be found here.

I am unable to replace all instances of a given string input with replacement value output. I checked the source code, but could not come up with a different config that would achieve the desired result. As a workaround, we currently resort to adding multiple instances of the exact same RewriteValueRule.

tomMoulard commented 5 months ago

Hello @jvanheesch,

Thanks for your interest in this Traefik Plugin !

What do you mean when you say:

I am unable to replace all instances of a given string input with replacement value output.

By all instances, do you mean that you have multiple headers where you want to replace the value.

like Replace: ``` $ > curl -I https://example.com HTTP/2 200 accept-ranges: bytes alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 content-type: text/html date: Mon, 03 Jun 2024 15:31:59 GMT etag: "5fd8e6de-690f" X-Forwarded-For: 10.0.0.0 X-Real-IP: 10.0.0.0 ``` To: ``` $ > curl -I https://google.com HTTP/2 200 accept-ranges: bytes alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 content-type: text/html date: Mon, 03 Jun 2024 15:31:59 GMT etag: "5fd8e6de-690f" X-Forwarded-For: 11.0.0.0 X-Real-IP: 11.0.0.0 ``` This is possible by using a plugin configuration that could look like this: ```yaml - Rule: Name: 'Header rewriteValue' Header: 'X-(Real-IP|Forwarded-For)' Value: '10.(.*)' ValueReplace: '11.$1' Type: 'RewriteValueRule' ```

Or do you mean that you have multiple instance of the value in a single header value ?

like Replace: ``` $ > curl -I https://example.com HTTP/2 200 accept-ranges: bytes alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 content-type: text/html date: Mon, 03 Jun 2024 15:31:59 GMT etag: "5fd8e6de-690f" X-Forwarded-For: 10.0.0.0 ``` To: ``` $ > curl -I https://google.com HTTP/2 200 accept-ranges: bytes alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 content-type: text/html date: Mon, 03 Jun 2024 15:31:59 GMT etag: "5fd8e6de-690f" X-Forwarded-For: 10.42.42.42 ```
jvanheesch commented 5 months ago

@tomMoulard Thank you for the quick response!

I am trying the second, i.e. replacing multiple (all) instances of a string in a single header value. I've created a reproducer, which can be found here, to share my config + actual/expected behavior. Please let me know if you have any further questions!

tomMoulard commented 5 months ago

Indeed the could be a good fix to add to the RewriteValueRule handler!