| 1 |
NAME |
| 2 |
Plack::Middleware::CrossOrigin - Adds headers to allow Cross-Origin |
| 3 |
Resource Sharing |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
# Allow any WebDAV or standard HTTP request from any location. |
| 7 |
builder { |
| 8 |
enable 'CrossOrigin', origins => '*'; |
| 9 |
$app; |
| 10 |
}; |
| 11 |
|
| 12 |
# Allow GET and POST requests from any location, cache results for 30 days. |
| 13 |
builder { |
| 14 |
enable 'CrossOrigin', |
| 15 |
origins => '*', methods => ['GET', 'POST'], max_age => 60*60*24*30; |
| 16 |
$app; |
| 17 |
}; |
| 18 |
|
| 19 |
DESCRIPTION |
| 20 |
Adds Cross Origin Request Sharing headers used by modern browsers to |
| 21 |
allow "XMLHttpRequest" to work across domains. This module will also |
| 22 |
help protect against CSRF attacks in some browsers. |
| 23 |
|
| 24 |
This module attempts to fully conform to the CORS spec, while allowing |
| 25 |
additional flexibility in the values specified for the of the headers. |
| 26 |
|
| 27 |
The module also ensures that the response contains a "Vary: Origin" |
| 28 |
header to avoid potential issues with caches. |
| 29 |
|
| 30 |
CORS REQUESTS IN BRIEF |
| 31 |
There are two types of CORS requests. Simple requests, and preflighted |
| 32 |
requests. |
| 33 |
|
| 34 |
Simple Requests |
| 35 |
A simple request is one that could be generated by a standard HTML form. |
| 36 |
Either a "GET" or "POST" request, with no additional headers. For these |
| 37 |
requests, the server processes the request as normal, and attaches the |
| 38 |
correct CORS headers in the response. The browser then decides based on |
| 39 |
those headers whether to allow the client script access to the response. |
| 40 |
|
| 41 |
Preflighted Requests |
| 42 |
If additional headers are specified, or a method other than "GET" or |
| 43 |
"POST" is used, the request must be preflighted. This means that the |
| 44 |
browser will first send a special request to the server to check if |
| 45 |
access is allowed. If the server allows it by responding with the |
| 46 |
correct headers, the actual request is then performed. |
| 47 |
|
| 48 |
CSRF Protection |
| 49 |
Some browsers will also provide same headers with cross domain "POST" |
| 50 |
requests from HTML forms. These requests will also be checked against |
| 51 |
the allowed origins and rejected before they reach the rest of your |
| 52 |
Plack application. |
| 53 |
|
| 54 |
CONFIGURATION |
| 55 |
origins A list of allowed origins. Origins should be formatted as a URL |
| 56 |
scheme and host, with no path information. |
| 57 |
("http://www.example.com") '"*"' can be specified to allow |
| 58 |
access from any location. Wildcards ("*") can also be included |
| 59 |
in in the host to match any part of a host name (e.g. |
| 60 |
"https://*.example.com"). At least one origin must bust be |
| 61 |
specified for this middleware to have any effect. This will be |
| 62 |
matched against the "Origin" request header, and will control |
| 63 |
the "Access-Control-Allow-Origin" response header. If the origin |
| 64 |
does not match, the request is aborted. |
| 65 |
|
| 66 |
headers A list of allowed request headers. '"*"' can be specified to |
| 67 |
allow any headers. Controls the "Access-Control-Allow-Headers" |
| 68 |
response header. Includes a set of headers by default to |
| 69 |
simplify working with WebDAV and AJAX frameworks: |
| 70 |
|
| 71 |
* "Cache-Control" |
| 72 |
|
| 73 |
* "Depth" |
| 74 |
|
| 75 |
* "If-Modified-Since" |
| 76 |
|
| 77 |
* "User-Agent" |
| 78 |
|
| 79 |
* "X-File-Name" |
| 80 |
|
| 81 |
* "X-File-Size" |
| 82 |
|
| 83 |
* "X-Prototype-Version" |
| 84 |
|
| 85 |
* "X-Requested-With" |
| 86 |
|
| 87 |
methods A list of allowed methods. '"*"' can be specified to allow any |
| 88 |
methods. Controls the "Access-Control-Allow-Methods" response |
| 89 |
header. Defaults to all of the standard HTTP and WebDAV methods. |
| 90 |
|
| 91 |
max_age The max length in seconds to cache the response data for. |
| 92 |
Controls the "Access-Control-Max-Age" response header. If not |
| 93 |
specified, the web browser will decide how long to use. |
| 94 |
|
| 95 |
expose_headers |
| 96 |
A list of allowed headers to expose to the client. '"*"' can be |
| 97 |
specified to allow the browser to see all of the response |
| 98 |
headers. Controls the "Access-Control-Expose-Headers" response |
| 99 |
header. |
| 100 |
|
| 101 |
credentials |
| 102 |
Whether the resource will be allowed with user credentials |
| 103 |
(cookies, HTTP authentication, and client-side SSL certificates) |
| 104 |
supplied. Controls the "Access-Control-Allow-Credentials" |
| 105 |
response header. |
| 106 |
|
| 107 |
continue_on_failure |
| 108 |
Normally, simple requests with an Origin that hasn't been |
| 109 |
allowed will be stopped before they continue to the main app. If |
| 110 |
this option is set, the request will be allowed to continue, but |
| 111 |
no CORS headers will be added to the response. This matches how |
| 112 |
non-allowed requests would be handled if this module was not |
| 113 |
used at all. |
| 114 |
|
| 115 |
This disables the CSRF protection and is not recommended. It |
| 116 |
could be needed for applications that need to allow cross-origin |
| 117 |
HTML form "POST"s without whitelisting domains. |
| 118 |
|
| 119 |
BROWSER SUPPORT |
| 120 |
Different browsers have different levels of support for CORS headers. |
| 121 |
|
| 122 |
Gecko (Firefox, Seamonkey) |
| 123 |
Initially supported in Gecko 1.9.1 (Firefox 3.5). Supports the |
| 124 |
complete CORS spec for "XMLHttpRequest"s. |
| 125 |
|
| 126 |
Does not yet provide the "Origin" header for CSRF protection |
| 127 |
(Bugzilla #446344 |
| 128 |
<https://bugzilla.mozilla.org/show_bug.cgi?id=446344>). |
| 129 |
|
| 130 |
WebKit (Safari, Google Chrome) |
| 131 |
Initially supported in Safari 4 and Chrome 3. Supports the |
| 132 |
complete CORS spec. |
| 133 |
|
| 134 |
The "expose_headers" feature has been supported since WebKit |
| 135 |
v535.18 (Safari 6, Chrome 18). Preflighted requests were buggy |
| 136 |
prior to WebKit v534.19 (Safari 5.1, Chrome 11), but this module |
| 137 |
uses a workaround where possible (using the "Referer" header). |
| 138 |
|
| 139 |
Also provides the "Origin" header for CSRF protection starting |
| 140 |
with WebKit v528.5 (Chrome 2, Safari 4). |
| 141 |
|
| 142 |
Internet Explorer |
| 143 |
Initially supported in IE8. Not supported with the standard |
| 144 |
"XMLHttpRequest" object. A separate object, "XDomainRequest", |
| 145 |
must be used. Only "GET" and "POST" methods are allowed. No |
| 146 |
extra headers can be added to the request. Neither the status |
| 147 |
code or any headers aside from "Content-Type" can be retrieved |
| 148 |
from the response. |
| 149 |
|
| 150 |
IE10 supports CORS via the standard "XMLHttpRequest" object. |
| 151 |
|
| 152 |
Opera Opera and Opera Mobile support CORS since version 12. |
| 153 |
|
| 154 |
SEE ALSO |
| 155 |
CORS Resources |
| 156 |
* W3C Spec for Cross-Origin Resource Sharing |
| 157 |
<http://www.w3.org/TR/cors/> |
| 158 |
|
| 159 |
* W3C Spec for Cross-Origin Resource Sharing - Implementation |
| 160 |
Considerations <http://www.w3.org/TR/cors/#resource-implementation> |
| 161 |
|
| 162 |
* Mozilla Developer Center - HTTP Access Control |
| 163 |
<https://developer.mozilla.org/En/HTTP_access_control> |
| 164 |
|
| 165 |
* Mozilla Developer Center - Server-Side Access Control |
| 166 |
<https://developer.mozilla.org/En/Server-Side_Access_Control> |
| 167 |
|
| 168 |
* Cross browser examples of using CORS requests |
| 169 |
<http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cro |
| 170 |
ss-origin-resource-sharing/> |
| 171 |
|
| 172 |
* MSDN - XDomainRequest Object |
| 173 |
<http://msdn.microsoft.com/en-us/library/cc288060%28v=vs.85%29.aspx> |
| 174 |
|
| 175 |
* XDomainRequest - Restrictions, Limitations and Workarounds |
| 176 |
<http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainreque |
| 177 |
st-restrictions-limitations-and-workarounds.aspx> |
| 178 |
|
| 179 |
* Wikipedia - Cross-Origin Resource Sharing |
| 180 |
<http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing> |
| 181 |
|
| 182 |
* CORS advocacy <http://enable-cors.org/> |
| 183 |
|
| 184 |
CSRF Resources |
| 185 |
* Wikipedia - Cross-site request forgery |
| 186 |
<http://en.wikipedia.org/wiki/Cross-site_request_forgery> |
| 187 |
|
| 188 |
* Stanford Web Security Research - Cross-Site Request Forgery |
| 189 |
<http://seclab.stanford.edu/websec/csrf/> |
| 190 |
|
| 191 |
* WebKit Bugzilla - Add origin header to POST requests |
| 192 |
<https://bugs.webkit.org/show_bug.cgi?id=20792> |
| 193 |
|
| 194 |
* Mozilla Bugzilla - Implement Origin header CSRF mitigation |
| 195 |
<https://bugzilla.mozilla.org/show_bug.cgi?id=446344> |
| 196 |
|
| 197 |
Related Technologies |
| 198 |
* Cross-domain policy file for Flash |
| 199 |
<http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.h |
| 200 |
tml> |
| 201 |
|
| 202 |
* Wikipedia - JSONP <http://en.wikipedia.org/wiki/JSONP> |
| 203 |
|
| 204 |
AUTHOR |
| 205 |
Graham Knop <[email protected]> |
| 206 |
|
| 207 |
COPYRIGHT AND LICENSE |
| 208 |
This software is copyright (c) 2011 by Graham Knop. |
| 209 |
|
| 210 |
This is free software; you can redistribute it and/or modify it under |
| 211 |
the same terms as the Perl 5 programming language system itself. |
| 212 |
|
| 213 |
AUTHOR |
| 214 |
haarg - Graham Knop (cpan:HAARG) <[email protected]> |
| 215 |
|
| 216 |
CONTRIBUTORS |
| 217 |
None so far. |
| 218 |
|
| 219 |
COPYRIGHT |
| 220 |
Copyright (c) 2011 the Plack::Middleware::CrossOrigin "AUTHOR" and |
| 221 |
"CONTRIBUTORS" as listed above. |
| 222 |
|
| 223 |
LICENSE |
| 224 |
This library is free software and may be distributed under the same |
| 225 |
terms as perl itself. |
| 226 |
|