Skip to content

Commit 092913a

Browse files
authored
httpcaddyfile: Prevent error handler from overriding sub-handler matchers (#6999)
Fixes: #6957
1 parent 7099892 commit 092913a

File tree

9 files changed

+440
-35
lines changed

9 files changed

+440
-35
lines changed

caddyconfig/httpcaddyfile/builtins.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package httpcaddyfile
1616

1717
import (
18+
"encoding/json"
1819
"fmt"
1920
"html"
2021
"net/http"
@@ -843,13 +844,18 @@ func parseHandleErrors(h Helper) ([]ConfigValue, error) {
843844
return nil, h.Errf("segment was not parsed as a subroute")
844845
}
845846

847+
// wrap the subroutes
848+
wrappingRoute := caddyhttp.Route{
849+
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(subroute, "handler", "subroute", nil)},
850+
}
851+
subroute = &caddyhttp.Subroute{
852+
Routes: []caddyhttp.Route{wrappingRoute},
853+
}
846854
if expression != "" {
847855
statusMatcher := caddy.ModuleMap{
848856
"expression": h.JSON(caddyhttp.MatchExpression{Expr: expression}),
849857
}
850-
for i := range subroute.Routes {
851-
subroute.Routes[i].MatcherSetsRaw = []caddy.ModuleMap{statusMatcher}
852-
}
858+
subroute.Routes[0].MatcherSetsRaw = []caddy.ModuleMap{statusMatcher}
853859
}
854860
return []ConfigValue{
855861
{

caddytest/integration/caddyfile_adapt/error_example.caddyfiletest

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,29 @@ example.com {
106106
"handler": "subroute",
107107
"routes": [
108108
{
109-
"group": "group0",
110109
"handle": [
111110
{
112-
"handler": "rewrite",
113-
"uri": "/{http.error.status_code}.html"
114-
}
115-
]
116-
},
117-
{
118-
"handle": [
119-
{
120-
"handler": "file_server",
121-
"hide": [
122-
"./Caddyfile"
111+
"handler": "subroute",
112+
"routes": [
113+
{
114+
"group": "group0",
115+
"handle": [
116+
{
117+
"handler": "rewrite",
118+
"uri": "/{http.error.status_code}.html"
119+
}
120+
]
121+
},
122+
{
123+
"handle": [
124+
{
125+
"handler": "file_server",
126+
"hide": [
127+
"./Caddyfile"
128+
]
129+
}
130+
]
131+
}
123132
]
124133
}
125134
]

caddytest/integration/caddyfile_adapt/error_multi_site_blocks.caddyfiletest

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,17 @@ bar.localhost {
165165
{
166166
"handle": [
167167
{
168-
"body": "404 or 410 error",
169-
"handler": "static_response"
168+
"handler": "subroute",
169+
"routes": [
170+
{
171+
"handle": [
172+
{
173+
"body": "404 or 410 error",
174+
"handler": "static_response"
175+
}
176+
]
177+
}
178+
]
170179
}
171180
],
172181
"match": [
@@ -178,8 +187,17 @@ bar.localhost {
178187
{
179188
"handle": [
180189
{
181-
"body": "Error In range [500 .. 599]",
182-
"handler": "static_response"
190+
"handler": "subroute",
191+
"routes": [
192+
{
193+
"handle": [
194+
{
195+
"body": "Error In range [500 .. 599]",
196+
"handler": "static_response"
197+
}
198+
]
199+
}
200+
]
183201
}
184202
],
185203
"match": [
@@ -208,8 +226,17 @@ bar.localhost {
208226
{
209227
"handle": [
210228
{
211-
"body": "404 or 410 error from second site",
212-
"handler": "static_response"
229+
"handler": "subroute",
230+
"routes": [
231+
{
232+
"handle": [
233+
{
234+
"body": "404 or 410 error from second site",
235+
"handler": "static_response"
236+
}
237+
]
238+
}
239+
]
213240
}
214241
],
215242
"match": [
@@ -221,8 +248,17 @@ bar.localhost {
221248
{
222249
"handle": [
223250
{
224-
"body": "Error In range [500 .. 599] from second site",
225-
"handler": "static_response"
251+
"handler": "subroute",
252+
"routes": [
253+
{
254+
"handle": [
255+
{
256+
"body": "Error In range [500 .. 599] from second site",
257+
"handler": "static_response"
258+
}
259+
]
260+
}
261+
]
226262
}
227263
],
228264
"match": [

caddytest/integration/caddyfile_adapt/error_range_codes.caddyfiletest

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,17 @@ localhost:3010 {
9696
{
9797
"handle": [
9898
{
99-
"body": "Error in the [400 .. 499] range",
100-
"handler": "static_response"
99+
"handler": "subroute",
100+
"routes": [
101+
{
102+
"handle": [
103+
{
104+
"body": "Error in the [400 .. 499] range",
105+
"handler": "static_response"
106+
}
107+
]
108+
}
109+
]
101110
}
102111
],
103112
"match": [

caddytest/integration/caddyfile_adapt/error_range_simple_codes.caddyfiletest

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,17 @@ localhost:2099 {
116116
{
117117
"handle": [
118118
{
119-
"body": "Error in the [400 .. 499] range",
120-
"handler": "static_response"
119+
"handler": "subroute",
120+
"routes": [
121+
{
122+
"handle": [
123+
{
124+
"body": "Error in the [400 .. 499] range",
125+
"handler": "static_response"
126+
}
127+
]
128+
}
129+
]
121130
}
122131
],
123132
"match": [
@@ -129,8 +138,17 @@ localhost:2099 {
129138
{
130139
"handle": [
131140
{
132-
"body": "Error code is equal to 500 or in the [300..399] range",
133-
"handler": "static_response"
141+
"handler": "subroute",
142+
"routes": [
143+
{
144+
"handle": [
145+
{
146+
"body": "Error code is equal to 500 or in the [300..399] range",
147+
"handler": "static_response"
148+
}
149+
]
150+
}
151+
]
134152
}
135153
],
136154
"match": [

caddytest/integration/caddyfile_adapt/error_simple_codes.caddyfiletest

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,17 @@ localhost:3010 {
9696
{
9797
"handle": [
9898
{
99-
"body": "404 or 410 error",
100-
"handler": "static_response"
99+
"handler": "subroute",
100+
"routes": [
101+
{
102+
"handle": [
103+
{
104+
"body": "404 or 410 error",
105+
"handler": "static_response"
106+
}
107+
]
108+
}
109+
]
101110
}
102111
],
103112
"match": [

caddytest/integration/caddyfile_adapt/error_sort.caddyfiletest

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,17 @@ localhost:2099 {
116116
{
117117
"handle": [
118118
{
119-
"body": "Error in the [400 .. 499] range",
120-
"handler": "static_response"
119+
"handler": "subroute",
120+
"routes": [
121+
{
122+
"handle": [
123+
{
124+
"body": "Error in the [400 .. 499] range",
125+
"handler": "static_response"
126+
}
127+
]
128+
}
129+
]
121130
}
122131
],
123132
"match": [
@@ -129,8 +138,17 @@ localhost:2099 {
129138
{
130139
"handle": [
131140
{
132-
"body": "Fallback route: code outside the [400..499] range",
133-
"handler": "static_response"
141+
"handler": "subroute",
142+
"routes": [
143+
{
144+
"handle": [
145+
{
146+
"body": "Fallback route: code outside the [400..499] range",
147+
"handler": "static_response"
148+
}
149+
]
150+
}
151+
]
134152
}
135153
]
136154
}

0 commit comments

Comments
 (0)