summaryrefslogtreecommitdiff
path: root/caddyconfig/httpcaddyfile/builtins.go
diff options
context:
space:
mode:
Diffstat (limited to 'caddyconfig/httpcaddyfile/builtins.go')
-rw-r--r--caddyconfig/httpcaddyfile/builtins.go16
1 files changed, 14 insertions, 2 deletions
diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go
index 1e7c701..5c539e2 100644
--- a/caddyconfig/httpcaddyfile/builtins.go
+++ b/caddyconfig/httpcaddyfile/builtins.go
@@ -580,12 +580,24 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
body = fmt.Sprintf(metaRedir, safeTo, safeTo, safeTo, safeTo)
code = "302"
default:
+ // Allow placeholders for the code
+ if strings.HasPrefix(code, "{") {
+ break
+ }
+ // Try to validate as an integer otherwise
codeInt, err := strconv.Atoi(code)
if err != nil {
return nil, h.Errf("Not a supported redir code type or not valid integer: '%s'", code)
}
- if codeInt < 300 || codeInt > 399 {
- return nil, h.Errf("Redir code not in the 3xx range: '%v'", codeInt)
+ // Sometimes, a 401 with Location header is desirable because
+ // requests made with XHR will "eat" the 3xx redirect; so if
+ // the intent was to redirect to an auth page, a 3xx won't
+ // work. Responding with 401 allows JS code to read the
+ // Location header and do a window.location redirect manually.
+ // see https://stackoverflow.com/a/2573589/846934
+ // see https://github.com/oauth2-proxy/oauth2-proxy/issues/1522
+ if codeInt < 300 || (codeInt > 399 && codeInt != 401) {
+ return nil, h.Errf("Redir code not in the 3xx range or 401: '%v'", codeInt)
}
}