-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.go
More file actions
319 lines (282 loc) · 8.97 KB
/
Copy pathrules.go
File metadata and controls
319 lines (282 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package cors
import (
"net/http"
"strings"
)
// PublicRule is a CORS rule that allows any origin.
// It does not have [Rule.AllowCredentials] because allowing credentials
// with any origin is a security vulnerability (CORS spec violation).
// Use [OriginFunc] if you need this escape hatch.
type PublicRule struct {
c config
}
// Rule is a CORS rule for specific origins.
// It supports [Rule.AllowCredentials] because specific origins are safe.
type Rule struct {
c config
}
// OriginMatcher is implemented by origin matching rule types ([PublicRule] and [Rule]).
// It cannot be implemented outside this package.
type OriginMatcher interface {
private()
}
func (PublicRule) private() {}
func (Rule) private() {}
// AnyOrigin creates a rule that matches all origins.
// Returns [PublicRule] which does NOT have [Rule.AllowCredentials].
// This prevents the dangerous AnyOrigin + Credentials combination.
func AnyOrigin() PublicRule {
return PublicRule{c: config{
matchType: matchAny,
allowMethods: defaultAllowMethods,
allowHeaders: defaultAllowHeaders,
}}
}
// Origins creates a [Rule] that matches specific origins exactly.
func Origins(origins ...string) Rule {
set := make(map[string]struct{}, len(origins))
for _, o := range origins {
set[o] = struct{}{}
}
return Rule{c: config{
matchType: matchExact,
origins: set,
allowMethods: defaultAllowMethods,
allowHeaders: defaultAllowHeaders,
}}
}
// OriginSuffix creates a [Rule] that matches origins by domain suffix.
//
// Patterns:
//
// "example.com" → example.com and *.example.com
// ".example.com" → *.example.com only (subdomains)
// "localhost:*" → localhost on any port
// ".example.com:*" → *.example.com on any port
//
// Matching requires a dot boundary, preventing "evilexample.com" from matching "example.com".
func OriginSuffix(suffix string) Rule {
// Check for port wildcard
suffix, portWildcard := strings.CutSuffix(suffix, ":*")
suffix, suffixOnly := strings.CutPrefix(suffix, ".")
return Rule{c: config{
matchType: matchSuffix,
suffix: suffix,
suffixOnly: suffixOnly,
portWildcard: portWildcard,
allowMethods: defaultAllowMethods,
allowHeaders: defaultAllowHeaders,
}}
}
// OriginFunc creates a [Rule] with a custom origin matching function.
// This is the escape hatch for complex scenarios including dynamic origins.
//
// Note: This allocates 1 object per request due to the function call.
// For static origins, prefer [Origins] or [OriginSuffix].
//
// Warning: If you use this with [Rule.AllowCredentials], ensure your function
// validates origins strictly. A function that returns true for all origins
// combined with credentials is a security vulnerability.
func OriginFunc(fn func(origin string) bool) Rule {
return Rule{c: config{
matchType: matchCustom,
customFn: fn,
allowMethods: defaultAllowMethods,
allowHeaders: defaultAllowHeaders,
}}
}
// CombinedRule is a set of rules combined with [Or]. The first matching rule wins.
type CombinedRule struct {
rules []config
}
// Or combines multiple rules. The first matching rule wins.
// Accepts both [Rule] and [PublicRule].
//
// Example:
//
// cors.Or(
// cors.Origins("https://trusted.com").AllowCredentials(),
// cors.AnyOrigin(), // fallback without credentials
// )
func Or(rules ...OriginMatcher) CombinedRule {
configs := make([]config, len(rules))
for i, r := range rules {
switch v := r.(type) {
case PublicRule:
configs[i] = v.c
case Rule:
configs[i] = v.c
}
}
return CombinedRule{rules: configs}
}
// AllowMethods returns a copy with the specified allowed methods.
func (r PublicRule) AllowMethods(methods ...string) PublicRule {
r.c.allowMethods = methods
return r
}
// AllowHeaders returns a copy with the specified allowed headers.
func (r PublicRule) AllowHeaders(headers ...string) PublicRule {
r.c.allowHeaders = headers
return r
}
// ExposeHeaders returns a copy with the specified exposed headers.
func (r PublicRule) ExposeHeaders(headers ...string) PublicRule {
r.c.exposeHeaders = headers
return r
}
// MaxAge returns a copy with the specified preflight cache duration in seconds.
// Use MaxAge(0) to explicitly disable preflight caching (overrides browser default).
func (r PublicRule) MaxAge(seconds int) PublicRule {
r.c.maxAge = &seconds
return r
}
// Handler returns the CORS middleware.
// Panics if configuration is invalid (e.g., forbidden methods/headers, negative MaxAge).
func (r PublicRule) Handler() func(http.Handler) http.Handler {
if err := r.c.validate(); err != nil {
panic("cors: " + err.Error())
}
return buildHandler(&r.c)
}
// Wrap directly wraps a handler with CORS middleware.
func (r PublicRule) Wrap(h http.Handler) http.Handler {
return r.Handler()(h)
}
// AllowCredentials returns a copy with credentials enabled.
// When enabled, Access-Control-Allow-Credentials: true is sent,
// allowing browsers to send cookies and auth headers.
//
// This also adds "Authorization" to allowed headers if not already present,
// since most credentialed APIs use Bearer tokens.
func (r Rule) AllowCredentials() Rule {
r.c.allowCredentials = true
// Add Authorization header for Bearer token support
if !containsHeader(r.c.allowHeaders, "Authorization") {
r.c.allowHeaders = append(r.c.allowHeaders, "Authorization")
}
return r
}
// AllowMethods returns a copy with the specified allowed methods.
func (r Rule) AllowMethods(methods ...string) Rule {
r.c.allowMethods = methods
return r
}
// AllowHeaders returns a copy with the specified allowed headers.
func (r Rule) AllowHeaders(headers ...string) Rule {
r.c.allowHeaders = headers
return r
}
// ExposeHeaders returns a copy with the specified exposed headers.
func (r Rule) ExposeHeaders(headers ...string) Rule {
r.c.exposeHeaders = headers
return r
}
// MaxAge returns a copy with the specified preflight cache duration in seconds.
// Use MaxAge(0) to explicitly disable preflight caching (overrides browser default).
func (r Rule) MaxAge(seconds int) Rule {
r.c.maxAge = &seconds
return r
}
// Handler returns the CORS middleware.
// Returns an error if the configuration is invalid.
//
// For scripts and tests where panics are acceptable, use MustHandler instead.
func (r Rule) Handler() (func(http.Handler) http.Handler, error) {
if err := r.c.validate(); err != nil {
return nil, err
}
return buildHandler(&r.c), nil
}
// MustHandler returns the CORS middleware, panicking if the configuration
// is invalid. Use this for variable initialization and tests:
//
// var corsHandler = cors.Origins("https://app.com").MustHandler()
//
// For production code that cannot tolerate panics, use Handler instead.
func (r Rule) MustHandler() func(http.Handler) http.Handler {
h, err := r.Handler()
if err != nil {
panic("cors: " + err.Error())
}
return h
}
// Wrap directly wraps a handler with CORS middleware.
// Returns an error if the configuration is invalid.
func (r Rule) Wrap(h http.Handler) (http.Handler, error) {
mw, err := r.Handler()
if err != nil {
return nil, err
}
return mw(h), nil
}
// MustWrap directly wraps a handler with CORS middleware, panicking if
// the configuration is invalid.
func (r Rule) MustWrap(h http.Handler) http.Handler {
return r.MustHandler()(h)
}
// Handler returns the CORS middleware.
// Returns an error if any rule has invalid configuration.
func (cr CombinedRule) Handler() (func(http.Handler) http.Handler, error) {
// Validate all rules
for i := range cr.rules {
if err := cr.rules[i].validate(); err != nil {
return nil, err
}
}
// Compile all rules
compiled := make([]config, len(cr.rules))
for i := range cr.rules {
compiled[i] = compileConfig(&cr.rules[i])
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
origin := req.Header.Get("Origin")
if origin == "" {
next.ServeHTTP(w, req)
return
}
// Find first matching rule
var matched *config
for i := range compiled {
if matchesOrigin(&compiled[i], origin) {
matched = &compiled[i]
break
}
}
if matched == nil {
next.ServeHTTP(w, req)
return
}
applyHeaders(w, req, matched, origin)
if req.Method == "OPTIONS" && req.Header.Get("Access-Control-Request-Method") != "" {
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, req)
})
}, nil
}
// MustHandler returns the CORS middleware, panicking if any rule configuration
// is invalid.
func (cr CombinedRule) MustHandler() func(http.Handler) http.Handler {
h, err := cr.Handler()
if err != nil {
panic("cors: " + err.Error())
}
return h
}
// Wrap directly wraps a handler with CORS middleware.
// Returns an error if any rule configuration is invalid.
func (cr CombinedRule) Wrap(h http.Handler) (http.Handler, error) {
mw, err := cr.Handler()
if err != nil {
return nil, err
}
return mw(h), nil
}
// MustWrap directly wraps a handler with CORS middleware, panicking if
// any rule configuration is invalid.
func (cr CombinedRule) MustWrap(h http.Handler) http.Handler {
return cr.MustHandler()(h)
}