caddy: X-Accel-Redirect setup doesn't work due to strange behaior of rewrite

Hey ! I’m trying to configure a pretty simple X-Accel-Redirect kind of setup under Caddy 2.6.2, but think I encountered an issue that prevents me to do so.

My stripped down Caddyfile looks like this:

localhost {
    reverse_proxy service_a:5000 {
        @accel header X-Accel-Redirect *
        handle_response @accel {
            rewrite * {rp.header.X-Accel-Redirect}
            reverse_proxy service_b:5000
        }
    }
}

The service_a returns a response with a header X-Accel-Redirect: /hello?some=param&some_other=param. This is be picked by Caddy and rewritten so that this query is handled by service_b. The rewrite of the path works, but the GET params get swallowed and are not visible to service_b where I only see /hello.

It works as expected when I hardcode the same value in the caddy file like this (which IMO should be 100% equivalent).

localhost {
    reverse_proxy service_a:5000 {
        @accel header X-Accel-Redirect *
        handle_response @accel {
            rewrite * /hello?some=param&some_other=param
            reverse_proxy service_b:5000
        }
    }
}

Just to understand what’s happening, I tried with some variants such as rewrite * /hello?{rp.header.X-Accel-Redirect} with just the paramters in the header. In this case, service_b sees http://localhost/hello?some%3Dparam%26some_other%3Dparam, so not good either.

Seems some rogue urlencoding happens, but I couldn’t find anything in the doc about that.

Any pointer about how to solve this ? I searched through the issues but could not find anything about this exactly.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 2
  • Comments: 21 (15 by maintainers)

Most upvoted comments

@mvaled That:

header X-S3-URI {uri} {defer}

replaces all instances of the {uri} with the contents of {defer}, which would be nothing unless you’ve defined a {defer} placeholder somewhere.

You probably meant:

header X-S3-URI {uri} {
   defer
}

A little trick helps:

   rewrite * {rp.header.X-Accel-Redirect}?

Hmm, good question. I think the rewrite module decides whether it should rewrite the query based on the original string being input, before doing placeholder replacement, by checking if it contains a ?.

This is definitely tricky. There are security considerations here, because not all placeholders contain trusted values, and in many configs there are implicit rewrite * {path} that happen (e.g. with try_files and via php_fastcgi) so we need to be very careful here about not allowing arbitrary clients to inject a ? to affect the behaviour of the rewrite.