From ff2ba6de8a312813140b5db24f14a407e98f4509 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Thu, 28 Jul 2022 17:19:48 -0400 Subject: caddyhttp: Clear out matcher error immediately after grabbing it (#4916) Co-authored-by: Matthew Holt --- modules/caddyhttp/routes.go | 4 ++++ modules/caddyhttp/vars.go | 10 ++++++++++ 2 files changed, 14 insertions(+) (limited to 'modules') diff --git a/modules/caddyhttp/routes.go b/modules/caddyhttp/routes.go index 7b2871f..b8771db 100644 --- a/modules/caddyhttp/routes.go +++ b/modules/caddyhttp/routes.go @@ -204,6 +204,10 @@ func wrapRoute(route Route) Middleware { // the request and trigger the error handling chain err, ok := GetVar(req.Context(), MatcherErrorVarKey).(error) if ok { + // clear out the error from context, otherwise + // it will cascade to the error routes (#4916) + SetVar(req.Context(), MatcherErrorVarKey, nil) + // return the matcher's error return err } diff --git a/modules/caddyhttp/vars.go b/modules/caddyhttp/vars.go index 28d0ddf..e7a7dbb 100644 --- a/modules/caddyhttp/vars.go +++ b/modules/caddyhttp/vars.go @@ -301,11 +301,21 @@ func GetVar(ctx context.Context, key string) interface{} { // SetVar sets a value in the context's variable table with // the given key. It overwrites any previous value with the // same key. +// +// If the value is nil (note: non-nil interface with nil +// underlying value does not count) and the key exists in +// the table, the key+value will be deleted from the table. func SetVar(ctx context.Context, key string, value interface{}) { varMap, ok := ctx.Value(VarsCtxKey).(map[string]interface{}) if !ok { return } + if value == nil { + if _, ok := varMap[key]; ok { + delete(varMap, key) + return + } + } varMap[key] = value } -- cgit v1.2.3