Sanitize CORS headers (#85)

Add sanitation step for `Access-Control-Allow-Headers` when echoing back user supplied headers
This commit is contained in:
Benson Wong
2025-04-01 08:43:53 -07:00
committed by GitHub
parent 4c3aa40564
commit a23da6eb57
3 changed files with 122 additions and 1 deletions

43
proxy/sanitize_cors.go Normal file
View File

@@ -0,0 +1,43 @@
package proxy
import (
"strings"
)
func isTokenChar(r rune) bool {
switch {
case r >= 'a' && r <= 'z':
case r >= 'A' && r <= 'Z':
case r >= '0' && r <= '9':
case strings.ContainsRune("!#$%&'*+-.^_`|~", r):
default:
return false
}
return true
}
func SanitizeAccessControlRequestHeaderValues(headerValues string) string {
parts := strings.Split(headerValues, ",")
valid := make([]string, 0, len(parts))
for _, p := range parts {
v := strings.TrimSpace(p)
if v == "" {
continue
}
validPart := true
for _, c := range v {
if !isTokenChar(c) {
validPart = false
break
}
}
if validPart {
valid = append(valid, v)
}
}
return strings.Join(valid, ", ")
}