fix(19.3-06): credentialed CORS refuses wildcard-host and unanchored origins (WR-04)

- with supports_credentials, a * origin must be the whole leftmost host
  label, patterns must be anchored with ^ and $, and no origin rule may
  match the probe origins outside any configured domain
- globally and per profile; non-credentialed configs load as before
This commit is contained in:
Jakub Zych
2026-10-15 22:25:43 +02:00
parent 072353663e
commit e32940d90f
4 changed files with 131 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"regexp"
"regexp/syntax"
"strconv"
"strings"
@@ -25,7 +26,10 @@ type CORSConfig struct {
// LoadCORSConfig reads http.cors. Missing section yields a zero config (no
// path matches, so no CORS headers). The config is validated: credentials
// with a "*" origin, an origin pattern that does not compile and a pattern
// still wrapped in PHP delimiters (#^...$#) are errors naming the key.
// still wrapped in PHP delimiters (#^...$#) are errors naming the key. With
// credentials, a wildcard origin must be the whole leftmost host label
// (https://*.example.com), a pattern must be anchored with ^ and $, and no
// origin rule may match an origin outside the configured domains.
func LoadCORSConfig(cfg *compass.Config) (CORSConfig, error) {
var out CORSConfig
if cfg == nil || !cfg.Has("http.cors") {
@@ -50,13 +54,111 @@ func validateCORSConfig(prefix string, cfg CORSConfig) error {
if phpDelimited(p) {
return fmt.Errorf("surf: %s.allowed_origins_patterns[%d] %q is wrapped in PHP delimiters; drop the leading and trailing %q (and any flags): Go patterns take none", prefix, i, p, p[:1])
}
if _, err := regexp.Compile(p); err != nil {
re, err := regexp.Compile(p)
if err != nil {
return fmt.Errorf("surf: %s.allowed_origins_patterns[%d] %q does not compile: %v", prefix, i, p, err)
}
if !cfg.SupportsCredentials {
continue
}
if !anchoredOriginPattern(p) {
return fmt.Errorf("surf: %s.allowed_origins_patterns[%d] %q is not anchored: with %s.supports_credentials true a pattern must start with ^ and end with $, as in ^https://[a-z]+\\.example\\.com$, or it matches inside any origin", prefix, i, p, prefix)
}
if probe, ok := matchesCORSProbe(re); ok {
return fmt.Errorf("surf: %s.allowed_origins_patterns[%d] %q matches %s, an origin outside any configured domain: with %s.supports_credentials true a pattern must name your own domains", prefix, i, p, probe, prefix)
}
}
if !cfg.SupportsCredentials {
return nil
}
for i, o := range cfg.AllowedOrigins {
if o == "*" || !strings.Contains(o, "*") {
continue
}
if !wildcardHostLabel(o) {
return fmt.Errorf("surf: %s.allowed_origins[%d] %q: with %s.supports_credentials true a wildcard must be the whole leftmost host label after a literal scheme, as in https://*.example.com", prefix, i, o, prefix)
}
if probe, ok := matchesCORSProbe(regexp.MustCompile(wildcardOriginPattern(o))); ok {
return fmt.Errorf("surf: %s.allowed_origins[%d] %q matches %s, an origin outside any configured domain: with %s.supports_credentials true a wildcard must sit in front of your own domain, as in https://*.example.com", prefix, i, o, probe, prefix)
}
}
return nil
}
// corsProbeOrigins are origins no credentialed origin rule may match: a
// rule that matches them trusts origins outside the configured domains
// (https://*, https://*.com, ^https?://.*$).
var corsProbeOrigins = []string{
"https://cors-probe.invalid",
"http://cors-probe.invalid",
"https://cors-probe.com",
"null",
}
// matchesCORSProbe reports the first probe origin re matches.
func matchesCORSProbe(re *regexp.Regexp) (string, bool) {
for _, probe := range corsProbeOrigins {
if re.MatchString(probe) {
return probe, true
}
}
return "", false
}
// wildcardHostLabel reports an allowed_origins entry whose only "*" is the
// whole leftmost host label after a literal scheme: https://*.example.com.
func wildcardHostLabel(origin string) bool {
scheme, host, ok := strings.Cut(origin, "://")
if !ok || scheme == "" || strings.Contains(scheme, "*") {
return false
}
rest, ok := strings.CutPrefix(host, "*.")
return ok && rest != "" && !strings.Contains(rest, "*")
}
// anchoredOriginPattern reports a pattern anchored at both ends: it starts
// with ^ and ends with $, and every alternative of the parsed expression
// stays anchored, so ^a|b$ is refused.
func anchoredOriginPattern(p string) bool {
if !strings.HasPrefix(p, "^") || !strings.HasSuffix(p, "$") {
return false
}
re, err := syntax.Parse(p, syntax.Perl)
if err != nil {
return false
}
return anchoredAt(re, true) && anchoredAt(re, false)
}
// anchoredAt reports whether every match of re begins (start true) or ends
// at the text boundary.
func anchoredAt(re *syntax.Regexp, start bool) bool {
switch re.Op {
case syntax.OpBeginText:
return start
case syntax.OpEndText:
return !start
case syntax.OpCapture:
return anchoredAt(re.Sub[0], start)
case syntax.OpConcat:
if len(re.Sub) == 0 {
return false
}
if start {
return anchoredAt(re.Sub[0], start)
}
return anchoredAt(re.Sub[len(re.Sub)-1], start)
case syntax.OpAlternate:
for _, sub := range re.Sub {
if !anchoredAt(sub, start) {
return false
}
}
return len(re.Sub) > 0
}
return false
}
// phpDelimited reports a preg pattern such as #^https://x$# or /^x$/i: the
// first byte is # or /, the same byte closes the pattern and only flag
// letters follow it. An origin pattern never starts with either byte.