@@ -15,46 +15,85 @@ type (
15
15
// Skipper defines a function to skip middleware.
16
16
Skipper Skipper
17
17
18
- // AllowOrigin defines a list of origins that may access the resource.
18
+ // AllowOrigins determines the value of the Access-Control-Allow-Origin
19
+ // response header. This header defines a list of origins that may access the
20
+ // resource. The wildcard characters '*' and '?' are supported and are
21
+ // converted to regex fragments '.*' and '.' accordingly.
22
+ //
23
+ // Security: use extreme caution when handling the origin, and carefully
24
+ // validate any logic. Remember that attackers may register hostile domain names.
25
+ // See https://blog.portswigger.net/2016/10/exploiting-cors-misconfigurations-for.html
26
+ //
19
27
// Optional. Default value []string{"*"}.
28
+ //
29
+ // See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
20
30
AllowOrigins []string `yaml:"allow_origins"`
21
31
22
32
// AllowOriginFunc is a custom function to validate the origin. It takes the
23
33
// origin as an argument and returns true if allowed or false otherwise. If
24
34
// an error is returned, it is returned by the handler. If this option is
25
35
// set, AllowOrigins is ignored.
36
+ //
37
+ // Security: use extreme caution when handling the origin, and carefully
38
+ // validate any logic. Remember that attackers may register hostile domain names.
39
+ // See https://blog.portswigger.net/2016/10/exploiting-cors-misconfigurations-for.html
40
+ //
26
41
// Optional.
27
42
AllowOriginFunc func (origin string ) (bool , error ) `yaml:"allow_origin_func"`
28
43
29
- // AllowMethods defines a list methods allowed when accessing the resource.
30
- // This is used in response to a preflight request.
44
+ // AllowMethods determines the value of the Access-Control-Allow-Methods
45
+ // response header. This header specified the list of methods allowed when
46
+ // accessing the resource. This is used in response to a preflight request.
47
+ //
31
48
// Optional. Default value DefaultCORSConfig.AllowMethods.
32
- // If `allowMethods` is left empty will fill for preflight request `Access-Control-Allow-Methods` header value
49
+ // If `allowMethods` is left empty, this middleware will fill for preflight
50
+ // request `Access-Control-Allow-Methods` header value
33
51
// from `Allow` header that echo.Router set into context.
52
+ //
53
+ // See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
34
54
AllowMethods []string `yaml:"allow_methods"`
35
55
36
- // AllowHeaders defines a list of request headers that can be used when
37
- // making the actual request. This is in response to a preflight request.
56
+ // AllowHeaders determines the value of the Access-Control-Allow-Headers
57
+ // response header. This header is used in response to a preflight request to
58
+ // indicate which HTTP headers can be used when making the actual request.
59
+ //
38
60
// Optional. Default value []string{}.
61
+ //
62
+ // See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
39
63
AllowHeaders []string `yaml:"allow_headers"`
40
64
41
- // AllowCredentials indicates whether or not the response to the request
42
- // can be exposed when the credentials flag is true. When used as part of
43
- // a response to a preflight request, this indicates whether or not the
44
- // actual request can be made using credentials.
45
- // Optional. Default value false.
65
+ // AllowCredentials determines the value of the
66
+ // Access-Control-Allow-Credentials response header. This header indicates
67
+ // whether or not the response to the request can be exposed when the
68
+ // credentials mode (Request.credentials) is true. When used as part of a
69
+ // response to a preflight request, this indicates whether or not the actual
70
+ // request can be made using credentials. See also
71
+ // [MDN: Access-Control-Allow-Credentials].
72
+ //
73
+ // Optional. Default value false, in which case the header is not set.
74
+ //
46
75
// Security: avoid using `AllowCredentials = true` with `AllowOrigins = *`.
47
- // See http://blog.portswigger.net/2016/10/exploiting-cors-misconfigurations-for.html
76
+ // See "Exploiting CORS misconfigurations for Bitcoins and bounties",
77
+ // https://blog.portswigger.net/2016/10/exploiting-cors-misconfigurations-for.html
78
+ //
79
+ // See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
48
80
AllowCredentials bool `yaml:"allow_credentials"`
49
81
50
- // ExposeHeaders defines a whitelist headers that clients are allowed to
51
- // access.
52
- // Optional. Default value []string{}.
82
+ // ExposeHeaders determines the value of Access-Control-Expose-Headers, which
83
+ // defines a list of headers that clients are allowed to access.
84
+ //
85
+ // Optional. Default value []string{}, in which case the header is not set.
86
+ //
87
+ // See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Header
53
88
ExposeHeaders []string `yaml:"expose_headers"`
54
89
55
- // MaxAge indicates how long (in seconds) the results of a preflight request
56
- // can be cached.
57
- // Optional. Default value 0.
90
+ // MaxAge determines the value of the Access-Control-Max-Age response header.
91
+ // This header indicates how long (in seconds) the results of a preflight
92
+ // request can be cached.
93
+ //
94
+ // Optional. Default value 0. The header is set only if MaxAge > 0.
95
+ //
96
+ // See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age
58
97
MaxAge int `yaml:"max_age"`
59
98
}
60
99
)
@@ -69,13 +108,22 @@ var (
69
108
)
70
109
71
110
// CORS returns a Cross-Origin Resource Sharing (CORS) middleware.
72
- // See: https://developer.mozilla.org/en/docs/Web/HTTP/Access_control_CORS
111
+ // See also [MDN: Cross-Origin Resource Sharing (CORS)].
112
+ //
113
+ // Security: Poorly configured CORS can compromise security because it allows
114
+ // relaxation of the browser's Same-Origin policy. See [Exploiting CORS
115
+ // misconfigurations for Bitcoins and bounties] and [Portswigger: Cross-origin
116
+ // resource sharing (CORS)] for more details.
117
+ //
118
+ // [MDN: Cross-Origin Resource Sharing (CORS)]: https://developer.mozilla.org/en/docs/Web/HTTP/Access_control_CORS
119
+ // [Exploiting CORS misconfigurations for Bitcoins and bounties]: https://blog.portswigger.net/2016/10/exploiting-cors-misconfigurations-for.html
120
+ // [Portswigger: Cross-origin resource sharing (CORS)]: https://portswigger.net/web-security/cors
73
121
func CORS () echo.MiddlewareFunc {
74
122
return CORSWithConfig (DefaultCORSConfig )
75
123
}
76
124
77
125
// CORSWithConfig returns a CORS middleware with config.
78
- // See: ` CORS()` .
126
+ // See: [ CORS] .
79
127
func CORSWithConfig (config CORSConfig ) echo.MiddlewareFunc {
80
128
// Defaults
81
129
if config .Skipper == nil {
0 commit comments