@marcel91200 / caddy Mirror · read-only / commits / 0b6f7643
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 16:45 UTC

forwardauth: Support renaming copied headers, block support (#4783)

Francis Lavoie committed Jun 16, 2022 at 16:28 UTC 0b6f7643569690b9a3defe989fb2e0c2607215ec
2 files changed +142 -7
caddytest/integration/caddyfile_adapt/forward_auth_rename_headers.txt new
+116
@@ -0,0 +1,116 @@
1 +:8881
2 +
3 +forward_auth localhost:9000 {
4 + uri /auth
5 + copy_headers A>1 B C>3 {
6 + D
7 + E>5
8 + }
9 +}
10 +----------
11 +{
12 + "apps": {
13 + "http": {
14 + "servers": {
15 + "srv0": {
16 + "listen": [
17 + ":8881"
18 + ],
19 + "routes": [
20 + {
21 + "handle": [
22 + {
23 + "handle_response": [
24 + {
25 + "match": {
26 + "status_code": [
27 + 2
28 + ]
29 + },
30 + "routes": [
31 + {
32 + "handle": [
33 + {
34 + "handler": "headers",
35 + "request": {
36 + "set": {
37 + "1": [
38 + "{http.reverse_proxy.header.A}"
39 + ],
40 + "3": [
41 + "{http.reverse_proxy.header.C}"
42 + ],
43 + "5": [
44 + "{http.reverse_proxy.header.E}"
45 + ],
46 + "B": [
47 + "{http.reverse_proxy.header.B}"
48 + ],
49 + "D": [
50 + "{http.reverse_proxy.header.D}"
51 + ]
52 + }
53 + }
54 + }
55 + ]
56 + }
57 + ]
58 + },
59 + {
60 + "routes": [
61 + {
62 + "handle": [
63 + {
64 + "exclude": [
65 + "Connection",
66 + "Keep-Alive",
67 + "Te",
68 + "Trailers",
69 + "Transfer-Encoding",
70 + "Upgrade"
71 + ],
72 + "handler": "copy_response_headers"
73 + }
74 + ]
75 + },
76 + {
77 + "handle": [
78 + {
79 + "handler": "copy_response"
80 + }
81 + ]
82 + }
83 + ]
84 + }
85 + ],
86 + "handler": "reverse_proxy",
87 + "headers": {
88 + "request": {
89 + "set": {
90 + "X-Forwarded-Method": [
91 + "{http.request.method}"
92 + ],
93 + "X-Forwarded-Uri": [
94 + "{http.request.uri}"
95 + ]
96 + }
97 + }
98 + },
99 + "rewrite": {
100 + "method": "GET",
101 + "uri": "/auth"
102 + },
103 + "upstreams": [
104 + {
105 + "dial": "localhost:9000"
106 + }
107 + ]
108 + }
109 + ]
110 + }
111 + ]
112 + }
113 + }
114 + }
115 + }
116 +}
\ No newline at end of file
modules/caddyhttp/reverseproxy/forwardauth/caddyfile.go
+26 -7
@@ -17,6 +17,7 @@ package forwardauth
17 import (
18 "encoding/json"
19 "net/http"
20 + "strings"
21
22 "github.com/caddyserver/caddy/v2"
23 "github.com/caddyserver/caddy/v2/caddyconfig"
@@ -115,7 +116,7 @@ func parseCaddyfile(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
116 // collect the headers to copy from the auth response
117 // onto the original request, so they can get passed
118 // through to a backend app
118 - headersToCopy := []string{}
119 + headersToCopy := make(map[string]string)
120
121 // read the subdirectives for configuring the forward_auth shortcut
122 // NOTE: we delete the tokens as we go so that the reverse_proxy
@@ -141,10 +142,28 @@ func parseCaddyfile(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
142
143 case "copy_headers":
144 args := dispenser.RemainingArgs()
144 - dispenser.Delete()
145 - for _, headerField := range args {
145 + hadBlock := false
146 + for nesting := dispenser.Nesting(); dispenser.NextBlock(nesting); {
147 + hadBlock = true
148 + args = append(args, dispenser.Val())
149 + }
150 +
151 + dispenser.Delete() // directive name
152 + if hadBlock {
153 + dispenser.Delete() // opening brace
154 + dispenser.Delete() // closing brace
155 + }
156 + for range args {
157 dispenser.Delete()
147 - headersToCopy = append(headersToCopy, headerField)
158 + }
159 +
160 + for _, headerField := range args {
161 + if strings.Contains(headerField, ">") {
162 + parts := strings.Split(headerField, ">")
163 + headersToCopy[parts[0]] = parts[1]
164 + } else {
165 + headersToCopy[headerField] = headerField
166 + }
167 }
168 if len(headersToCopy) == 0 {
169 return nil, dispenser.ArgErr()
@@ -180,9 +199,9 @@ func parseCaddyfile(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
199 },
200 }
201
183 - for _, headerField := range headersToCopy {
184 - handler.Request.Set[headerField] = []string{
185 - "{http.reverse_proxy.header." + headerField + "}",
202 + for from, to := range headersToCopy {
203 + handler.Request.Set[to] = []string{
204 + "{http.reverse_proxy.header." + from + "}",
205 }
206 }
207