|
| 1 | +// Copyright (C) 2026 Yota Hamada |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + |
| 4 | +package frontend |
| 5 | + |
| 6 | +import ( |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/go-chi/cors" |
| 13 | +) |
| 14 | + |
| 15 | +type corsPolicy struct { |
| 16 | + allowedOrigins []string |
| 17 | + publicURL string |
| 18 | + setupPath string |
| 19 | +} |
| 20 | + |
| 21 | +func (p corsPolicy) middleware(next http.Handler) http.Handler { |
| 22 | + corsConfigured := len(p.allowedOrigins) > 0 |
| 23 | + wrapped := next |
| 24 | + if corsConfigured { |
| 25 | + allowAllOrigins := p.allowsAllOrigins() |
| 26 | + options := cors.Options{ |
| 27 | + AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, |
| 28 | + AllowedHeaders: []string{"Content-Type", "Authorization", "Content-Encoding", "Accept", "MCP-Protocol-Version", "Mcp-Session-Id", "Last-Event-ID"}, |
| 29 | + ExposedHeaders: []string{"Mcp-Session-Id"}, |
| 30 | + AllowCredentials: !allowAllOrigins, |
| 31 | + MaxAge: 300, |
| 32 | + } |
| 33 | + if allowAllOrigins { |
| 34 | + options.AllowedOrigins = []string{"*"} |
| 35 | + } else { |
| 36 | + options.AllowOriginFunc = func(_ *http.Request, origin string) bool { |
| 37 | + return p.allowsOrigin(origin) |
| 38 | + } |
| 39 | + } |
| 40 | + wrapped = cors.Handler(options)(next) |
| 41 | + } |
| 42 | + |
| 43 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 44 | + if !corsConfigured { |
| 45 | + w.Header().Add("Vary", "Origin") |
| 46 | + } |
| 47 | + origin := r.Header.Get("Origin") |
| 48 | + if origin != "" && p.isCrossOrigin(r, origin) { |
| 49 | + if p.isSetupPath(r.URL.Path) || !p.allowsOrigin(origin) { |
| 50 | + if corsConfigured { |
| 51 | + w.Header().Add("Vary", "Origin") |
| 52 | + } |
| 53 | + http.Error(w, "cross-origin request denied", http.StatusForbidden) |
| 54 | + return |
| 55 | + } |
| 56 | + } |
| 57 | + wrapped.ServeHTTP(w, r) |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +func (p corsPolicy) isCrossOrigin(r *http.Request, origin string) bool { |
| 62 | + sourceOrigin := canonicalOrigin(origin) |
| 63 | + if sourceOrigin == "" { |
| 64 | + return true |
| 65 | + } |
| 66 | + if sourceOrigin == requestOrigin(r) || sourceOrigin == canonicalOrigin(p.publicURL) { |
| 67 | + return false |
| 68 | + } |
| 69 | + |
| 70 | + // Fetch Metadata preserves same-origin classification through reverse proxies |
| 71 | + // that do not expose the public scheme and host to the application. |
| 72 | + return !strings.EqualFold(strings.TrimSpace(r.Header.Get("Sec-Fetch-Site")), "same-origin") |
| 73 | +} |
| 74 | + |
| 75 | +func requestOrigin(r *http.Request) string { |
| 76 | + scheme := "http" |
| 77 | + if r.TLS != nil { |
| 78 | + scheme = "https" |
| 79 | + } |
| 80 | + return canonicalOrigin(scheme + "://" + r.Host) |
| 81 | +} |
| 82 | + |
| 83 | +func (p corsPolicy) allowsOrigin(origin string) bool { |
| 84 | + origin = strings.ToLower(strings.TrimSpace(origin)) |
| 85 | + canonicalRequestOrigin := canonicalOrigin(origin) |
| 86 | + for _, candidate := range p.allowedOrigins { |
| 87 | + candidate = strings.ToLower(strings.TrimSpace(candidate)) |
| 88 | + if candidate == "*" { |
| 89 | + return true |
| 90 | + } |
| 91 | + if prefix, suffix, ok := strings.Cut(candidate, "*"); ok { |
| 92 | + if len(origin) >= len(prefix)+len(suffix) && |
| 93 | + strings.HasPrefix(origin, prefix) && strings.HasSuffix(origin, suffix) { |
| 94 | + return true |
| 95 | + } |
| 96 | + continue |
| 97 | + } |
| 98 | + if candidate == origin || |
| 99 | + (canonicalRequestOrigin != "" && canonicalOrigin(candidate) == canonicalRequestOrigin) { |
| 100 | + return true |
| 101 | + } |
| 102 | + } |
| 103 | + return false |
| 104 | +} |
| 105 | + |
| 106 | +func (p corsPolicy) allowsAllOrigins() bool { |
| 107 | + for _, origin := range p.allowedOrigins { |
| 108 | + if strings.TrimSpace(origin) == "*" { |
| 109 | + return true |
| 110 | + } |
| 111 | + } |
| 112 | + return false |
| 113 | +} |
| 114 | + |
| 115 | +func (p corsPolicy) isSetupPath(requestPath string) bool { |
| 116 | + return strings.TrimRight(requestPath, "/") == strings.TrimRight(p.setupPath, "/") |
| 117 | +} |
| 118 | + |
| 119 | +func canonicalOrigin(value string) string { |
| 120 | + parsed, err := url.Parse(strings.TrimSpace(value)) |
| 121 | + if err != nil || parsed.Scheme == "" || parsed.Host == "" || parsed.User != nil { |
| 122 | + return "" |
| 123 | + } |
| 124 | + if !strings.EqualFold(parsed.Scheme, "http") && !strings.EqualFold(parsed.Scheme, "https") { |
| 125 | + return "" |
| 126 | + } |
| 127 | + |
| 128 | + scheme := strings.ToLower(parsed.Scheme) |
| 129 | + hostname := strings.ToLower(parsed.Hostname()) |
| 130 | + if hostname == "" { |
| 131 | + return "" |
| 132 | + } |
| 133 | + port := parsed.Port() |
| 134 | + if (scheme == "http" && port == "80") || (scheme == "https" && port == "443") { |
| 135 | + port = "" |
| 136 | + } |
| 137 | + |
| 138 | + host := hostname |
| 139 | + if port != "" { |
| 140 | + host = net.JoinHostPort(hostname, port) |
| 141 | + } else if strings.Contains(hostname, ":") { |
| 142 | + host = "[" + hostname + "]" |
| 143 | + } |
| 144 | + return scheme + "://" + host |
| 145 | +} |
0 commit comments