feat(19.3-02): per-group CORS policies through cors:<profile> with router-registered preflights

- surf: LoadCORSProfiles reads http.cors_profiles.<name> (http.cors keys
  without paths, validated the same way); factory cors:<profile> sets the
  profile's headers; an unknown profile fails boot naming plugin and route
- BuildRouter registers OPTIONS <path> (owner surf, recovery only) for every
  path whose routes name cors:<profile>; two profiles on one path, an existing
  OPTIONS route or overlap with http.cors.paths fail boot
- docs: CORS profiles section, middleware table and README rows
This commit is contained in:
Jakub Zych
2026-10-15 17:51:58 +02:00
parent e7659f2f4b
commit 5055e8bb61
5 changed files with 274 additions and 1 deletions

View File

@@ -41,6 +41,9 @@ type route struct {
middleware []string
constraints []Constraint
raw bool
// preflight marks an OPTIONS route addCORSPreflights registered for a
// cors:<profile> path: wrapped only in recovery and recordPattern.
preflight bool
}
// Router compiles group declarations onto net/http ServeMux.
@@ -56,6 +59,7 @@ type Router struct {
compileErr error
limiter *FixedWindowLimiter
corsCfg CORSConfig
corsProfiles map[string]*corsPolicy
defaultBytes int64
uploadBytes int64
built map[string]pact.Middleware
@@ -397,6 +401,11 @@ func handleRoute(mux *http.ServeMux, rt route, h http.Handler) (err error) {
}
func (r *Router) wrap(rt route) (http.Handler, error) {
if rt.preflight {
// Preflights carry no credentials: no auth, throttle, body or locale.
h := recoverBare(constrain(rt.handler, rt.constraints))
return recordPattern(rt.method+" "+rt.path, h), nil
}
h := constrain(rt.handler, rt.constraints)
limit, err := routeBodyLimit(rt, r.defaultBytes)
if err != nil {
@@ -423,6 +432,11 @@ func (r *Router) wrap(rt route) (http.Handler, error) {
return nil, fmt.Errorf("surf: plugin %q: %w", rt.pluginID, err)
}
}
if base == "cors" {
if _, ok := r.corsProfiles[param]; !ok {
return nil, fmt.Errorf("surf: plugin %q route %s %s references unknown CORS profile %q (define http.cors_profiles.%s)", rt.pluginID, rt.method, rt.path, param, param)
}
}
if base == "body.limit" {
if _, err := parseBodyLimit(param); err != nil {
return nil, fmt.Errorf("surf: plugin %q: %w", rt.pluginID, err)
@@ -500,12 +514,26 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) {
if err := r.RegisterMiddleware("surf", "locale.from-principal", LocaleFromPrincipal); err != nil {
return nil, err
}
r.corsProfiles = map[string]*corsPolicy{}
if err := r.RegisterMiddlewareFactory("surf", "cors", func(param string) pact.Middleware {
// wrap refuses an unknown profile before the factory runs.
return r.corsProfiles[param].profileMiddleware
}); err != nil {
return nil, err
}
if app != nil {
corsCfg, err := LoadCORSConfig(app.Config)
if err != nil {
return nil, err
}
r.corsCfg = corsCfg
profiles, err := LoadCORSProfiles(app.Config)
if err != nil {
return nil, err
}
for name, pc := range profiles {
r.corsProfiles[name] = newCORSPolicy(pc)
}
if app.Config != nil {
d, err := requiredBytes(app, "http.body_limits.default_bytes")
if err != nil {
@@ -586,6 +614,9 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) {
return nil, err
}
}
if err := r.addCORSPreflights(); err != nil {
return nil, err
}
return r, nil
}