Plack:...Origin(3pm)
π Plack::Middleware::CrossOrigin π
aka the page that makes cross-origin requests stop being a whole entire problem, bestie π
β¨ NAME β¨
Plack::Middleware::CrossOrigin β it adds headers so Cross-Origin Resource Sharing can actually happen. slay.
π SYNOPSIS π
# Allow any WebDAV or standard HTTP request from any location.
builder {
enable 'CrossOrigin', origins => '*';
$app;
};
# Allow GET and POST requests from any location, cache results for 30 days.
builder {
enable 'CrossOrigin',
origins => '*', methods => ['GET', 'POST'], max_age => 60*60*24*30;
$app;
};
π DESCRIPTION π
okay so basically this adds the Cross Origin Request Sharing headers that modern browsers are obsessed with, so that XMLHttpRequest can vibe across domains. β¨ Bonus: in some browsers it also helps protect you against CSRF attacks. two for one, we love that.
It lowkey tries to fully conform to the CORS spec, while still letting you have extra flexibility in the values you specify for the headers. flexible but responsible, the dream.
It also makes sure the response has a Vary: Origin header, so caches don't cause you mysterious problems later. π¦ no cap, that one matters.
π₯ CORS REQUESTS IN BRIEF π₯
There are literally two types of CORS requests: simple requests and preflighted requests.
πΈ Simple Requests
A simple request is one that a standard HTML form could've generated. Either a GET or POST request, with no extra headers. For these, the server just processes the request like normal and attaches the correct CORS headers in the response. Then the browser looks at those headers and decides whether to let the client script see the response. server does its thing, browser does the vibe check π
π Preflighted Requests
If extra headers are specified, or a method other than GET or POST is used, the request has to be preflighted. Meaning: the browser first sends a special request to the server to check if access is even allowed. If the server says yes by responding with the correct headers, then the actual request happens. ask permission first, iconic behaviour β¨
π«Ά CSRF Protection π«Ά
Some browsers also send the same headers along with cross domain POST requests coming from HTML forms. Those requests get checked against the allowed origins and rejected before they ever reach the rest of your Plack application. π goodbye, sketchy form.
π CONFIGURATION π
originsβ a list of allowed origins. Origins should be formatted as a URL scheme and host, with no path info (http://www.example.com). You can specify'*'to allow access from literally any location. Wildcards (*) can also be included in the host to match any part of a host name (e.g.https://*.example.com). At least one origin must be specified for this middleware to do anything at all. This gets matched against theOriginrequest header, and it controls theAccess-Control-Allow-Originresponse header. If the origin doesn't match, the request is aborted. no match, no entry.headersβ a list of allowed request headers.'*'can be specified to allow any headers. Controls theAccess-Control-Allow-Headersresponse header. It includes a set of headers by default to simplify working with WebDAV and AJAX frameworks:Cache-ControlDepthIf-Modified-SinceUser-AgentX-File-NameX-File-SizeX-Prototype-VersionX-Requested-With
methodsβ a list of allowed methods.'*'can be specified to allow any methods. Controls theAccess-Control-Allow-Methodsresponse header. Defaults to all of the standard HTTP and WebDAV methods.max_ageβ the max length in seconds to cache the response data for. Controls theAccess-Control-Max-Ageresponse header. If you don't specify it, the web browser decides how long to use. π°οΈexpose_headersβ a list of allowed headers to expose to the client.'*'can be specified to allow the browser to see all of the response headers. Controls theAccess-Control-Expose-Headersresponse header.credentialsβ whether the resource will be allowed with user credentials (cookies, HTTP authentication, and client-side SSL certificates) supplied. Controls theAccess-Control-Allow-Credentialsresponse header.continue_on_failureβ normally, simple requests with an Origin that hasn't been allowed get stopped before they continue to the main app. If this option is set, the request is allowed to continue, but no CORS headers will be added to the response. This matches how non-allowed requests would be handled if this module wasn't used at all.
π¨ This disables the CSRF protection and is not recommended. It could be needed for applications that need to allow cross-origin HTML form POSTs without whitelisting domains. you've been warned, bestie.
π BROWSER SUPPORT π
Different browsers have different levels of support for CORS headers. the chaos of it all π
Gecko (Firefox, Seamonkey)
Initially supported in Gecko 1.9.1 (Firefox 3.5). Supports the complete CORS spec for XMLHttpRequests.
Does not yet provide the Origin header for CSRF protection (Bugzilla #446344 <https://bugzilla.mozilla.org/show_bug.cgi?id=446344>).
WebKit (Safari, Google Chrome)
Initially supported in Safari 4 and Chrome 3. Supports the complete CORS spec.
The expose_headers feature has been supported since WebKit v535.18 (Safari 6, Chrome 18). Preflighted requests were buggy prior to WebKit v534.19 (Safari 5.1, Chrome 11), but this module uses a workaround where possible (using the Referer header).
Also provides the Origin header for CSRF protection starting with WebKit v528.5 (Chrome 2, Safari 4).
Internet Explorer
Initially supported in IE8. Not supported with the standard XMLHttpRequest object. A separate object, XDomainRequest, must be used. Only GET and POST methods are allowed. No extra headers can be added to the request. Neither the status code or any headers aside from Content-Type can be retrieved from the response. π
IE10 supports CORS via the standard XMLHttpRequest object.
Opera
Opera and Opera Mobile support CORS since version 12.
π SEE ALSO π
π CORS Resources
- W3C Spec for Cross-Origin Resource Sharing <http://www.w3.org/TR/cors/>
- W3C Spec for Cross-Origin Resource Sharing - Implementation Considerations <http://www.w3.org/TR/cors/#resource-implementation>
- Mozilla Developer Center - HTTP Access Control <https://developer.mozilla.org/En/HTTP_access_control>
- Mozilla Developer Center - Server-Side Access Control <https://developer.mozilla.org/En/Server-Side_Access_Control>
- Cross browser examples of using CORS requests <http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/>
- MSDN - XDomainRequest Object <http://msdn.microsoft.com/en-us/library/cc288060%28v=vs.85%29.aspx>
- XDomainRequest - Restrictions, Limitations and Workarounds <http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx>
- Wikipedia - Cross-Origin Resource Sharing <http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing>
- CORS advocacy <http://enable-cors.org/>
π§ CSRF Resources
- Wikipedia - Cross-site request forgery <http://en.wikipedia.org/wiki/Cross-site_request_forgery>
- Stanford Web Security Research - Cross-Site Request Forgery <http://seclab.stanford.edu/websec/csrf/>
- WebKit Bugzilla - Add origin header to POST requests <https://bugs.webkit.org/show_bug.cgi?id=20792>
- Mozilla Bugzilla - Implement Origin header CSRF mitigation <https://bugzilla.mozilla.org/show_bug.cgi?id=446344>
π« Related Technologies
- Cross-domain policy file for Flash <http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html>
- Wikipedia - JSONP <http://en.wikipedia.org/wiki/JSONP>
π AUTHOR π
Graham Knop <haarg@haarg.org>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2011 by Graham Knop.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. π«Ά
π AUTHOR π
haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org>
πΈ CONTRIBUTORS πΈ
None so far. (bestieβ¦ that could be you π)
π COPYRIGHT π
Copyright (c) 2011 the Plack::Middleware::CrossOrigin AUTHOR and CONTRIBUTORS as listed above.
π LICENSE π
This library is free software and may be distributed under the same terms as perl itself.
and that's the whole page, fr fr π
this doc is from perl v5.36.0, dated 2022-11-19 β now go forth and let your XMLHttpRequests cross those origins, no cap πβ¨
π¦ where this page comes from
- debian bookworm: libplack-middleware-crossorigin-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libplack-middleware-crossorigin-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libplack-middleware-crossorigin-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libplack-middleware-crossorigin-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libplack-middleware-crossorigin-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms