tomMoulard / htransformation

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

SetOnResponse doesn't seem to work #42

Closed ericmorand closed 1 year ago

ericmorand commented 1 year ago

Hi

I have the feeling that SetOnResponse setting doesn't work.

Here is the docker-composer.yml file that spawns Traefik and their whoami service:

version: "3.3"

services:
  traefik:
    image: "traefik"
    container_name: "traefik"
    command:
      - "--providers.docker=true"
      - "--experimental.plugins.htransformation.modulename=github.com/tomMoulard/htransformation"
      - "--experimental.plugins.htransformation.version=v0.2.7"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  whoami:
    image: "traefik/whoami"
    container_name: "whoami"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.tls=true"
      - "traefik.http.routers.whoami.tls.domains[0].main=[HIDDEN]"
      - "traefik.http.routers.whoami.rule=Host(`whoami.[HIDDEN]`)"
      - "traefik.http.routers.whoami.entrypoints=websecure"
      - "traefik.http.routers.whoami.middlewares=htransformation"
      - "traefik.http.middlewares.htransformation.plugin.htransformation.Rules[0].Name=Set Foo"
      - "traefik.http.middlewares.htransformation.plugin.htransformation.Rules[0].Type=Set"
      - "traefik.http.middlewares.htransformation.plugin.htransformation.Rules[0].Header=Foo"
      - "traefik.http.middlewares.htransformation.plugin.htransformation.Rules[0].Value=bar"
      - "traefik.http.middlewares.htransformation.plugin.htransformation.Rules[0].SetOnResponse=true"

When I curl the whoami service, I can see the Foo header in the request...but not in the response:

curl -v https://whoami.[HIDDEN]
< HTTP/2 200
< content-type: text/plain; charset=utf-8
< date: Sun, 16 Apr 2023 11:22:03 GMT
< content-length: 404
<
RemoteAddr: [HIDDEN]
GET / HTTP/1.1
Host: whoami.[HIDDEN]
User-Agent: curl/7.58.0
Accept: */*
Accept-Encoding: gzip
Foo: bar
X-Forwarded-Host: whoami.[HIDDEN]
X-Forwarded-Port: 443
X-Forwarded-Proto: https

Am I doing something wrong here or is there an issue with the SetOnResponse field?

ericmorand commented 1 year ago

By the way, traefik version is 2.9.10, in case it matters.

tomMoulard commented 1 year ago

Hello @ericmorand,

Thanks for your interest in this Traefik plugin !

Indeed, I can see an issue with the following line: https://github.com/tomMoulard/htransformation/blob/6236d9c13c900fca50a70225881ea4cd4a0e6d79/pkg/handler/set/set.go#L19

It should be:

rw.Header().Set(rule.Header, rule.Value)