caddy-cgi icon indicating copy to clipboard operation
caddy-cgi copied to clipboard

Modifying reverse proxy response?

Open torarnv opened this issue 1 year ago • 5 comments

Can a CGI be fed the response from a reverse proxy, modify it , and spit out the modified response?

I tried this without success:

        reverse_proxy {host}:443 {
                header_up Host {upstream_hostport}
                handle_response {
                    handle /some/path/i/want/to/override {
                        cgi * /scripts/hello.py {
                            pass_all_env
                        }
                    }
                    handle {
                        copy_response
                    }
                }
            }

torarnv avatar Dec 04 '24 20:12 torarnv

That's an interesting problem. I'll try to reproduce that setup in the next days and see how far I can get. No promises yet 🙂

aksdb avatar Dec 04 '24 20:12 aksdb

Thanks! :)

torarnv avatar Dec 04 '24 20:12 torarnv

I looked into it a bit and also searched through documentation and the caddy forum, but from what I can tell, there is no way to access the body of the reverse_proxy response. If you are only interested in the headers, you could basically do, what forward_auth does, but as soon as you need the body, there doesn't seem to be any way. If I missed anything, please point me in the right direction, otherwise I think this cannot be accomplished (here; it would have to be a change request towards caddy).

aksdb avatar Dec 07 '24 16:12 aksdb

Thanks for looking into it! I guess as a workaround I can pass the required cookies as ENV and do the reverse proxying step in the CGI

torarnv avatar Dec 07 '24 16:12 torarnv

I think that would also be cleaner in general. You end up with a "proper" stream: client -> caddy -> cgi -> upstream.

aksdb avatar Dec 07 '24 16:12 aksdb