Skip to content

Commit bc0c102

Browse files
committed
feat: fix CORS issue (#157)
1 parent 723f247 commit bc0c102

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

service/proxy.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,28 @@ func forwardHandler(targetUrl string, writer http.ResponseWriter, request *http.
8181
}
8282
}
8383
}
84+
85+
// Fix CORS issue: Remove CORS header combinations that allow credential theft from any origin
86+
allowOrigin := resp.Header.Get("Access-Control-Allow-Origin")
87+
allowCredentials := resp.Header.Get("Access-Control-Allow-Credentials")
88+
89+
// Remove CORS headers when the combination is present:
90+
// 1. Access-Control-Allow-Credentials: true with Access-Control-Allow-Origin: *
91+
// This is actually blocked by browsers but we sanitize it anyway
92+
// 2. Access-Control-Allow-Credentials: true with any origin
93+
// Without a configured allowlist, we cannot safely validate if the origin
94+
// is trusted or if it's being reflected from the request, so we remove all
95+
// CORS headers for credential-bearing responses to prevent theft
96+
if strings.EqualFold(allowCredentials, "true") && allowOrigin != "" {
97+
// Remove CORS headers to prevent credential theft
98+
resp.Header.Del("Access-Control-Allow-Origin")
99+
resp.Header.Del("Access-Control-Allow-Credentials")
100+
resp.Header.Del("Access-Control-Allow-Methods")
101+
resp.Header.Del("Access-Control-Allow-Headers")
102+
resp.Header.Del("Access-Control-Expose-Headers")
103+
resp.Header.Del("Access-Control-Max-Age")
104+
}
105+
84106
return nil
85107
}
86108

0 commit comments

Comments
 (0)