3 ms·
> CORS only exists because XMLHTTPRequest broke the assumptions of web 1.0 servers. Suddenly any web browser loading any page anywhere could make a request to y
by thaumasiotes 5y ago
> CORS only exists because XMLHTTPRequest broke the assumptions of web 1.0 servers. Suddenly any web browser loading any page anywhere could make a request to your server without the user's explicit permission
This had always been true; making cross-origin requests with e.g. <img> tags is still very much a thing.
- colinclerk 5y agoThis is why we can make GET and POST requests without an OPTIONS preflight... Apparently, since developers should already have been protecting against cross-origin requests on GET and POST via other means (CSRF tokens), there was no need for the additional preflight protection. They only added preflights to requests that browsers couldn't make previously. Source: https://stackoverflow.com/a/39736697/114855 https://stackoverflow.com/a/39736697/114855
- Raqbit 5y agoThis is also the reason you can still POST text/plain to random TCP servers on the user's local network, without pre-flight check.
- dividuum 5y agoThat’ll be gone soon: https://web.dev/cors-rfc1918-feedback/ https://web.dev/cors-rfc1918-feedback/
- matsemann 5y agoI'd rather deal with stateless CORS than having to muck around with CSRF again, though.
- jrochkind1 5y agoTrue. CORS exists because the web browser would send the user's cookies (ie auth) for your site to you, when the user doesn't think they are on your site, but it's really malicious code from another site the user was visiting, acting as the user on your site without the user's knowledge. Thats the threat model CORS is meant to address. Not just in general that a web host might not want a request from "anywhere" happening, but that site B might not want an authenticated request on behalf of User X to site B being made by code from site A.
- thaumasiotes 5y agoThat still doesn't distinguish the XmlHttpRequest from the <img> tag. They both send cookies. You're right that sending cookies is the problem CORS addresses, but XmlHttpRequest didn't break any assumptions that were valid before.
- jrochkind1 5y agoIt is much more difficult (perhaps impossible on a properly-implemented site?) to interact with a site on behalf of a user in harmful ways through an img tag -- Like, say, getting their checking account number, or transfering money from their checking account to yours, or resetting their password or email address. It's difficult/impossible to do that kind of attack only through an <img> tag, because Javascript can't see the body response of the URL in an <img> tag even if you point it at a non-image, and additionally an img tag can only be used for GET requests. I didn't say that XmlHttpRequests "broke any assumptions that were valid before", I don't know exactly what that means. If an XmlHttpRequest were allowed to do arbitrary cross-site requests, Javascrpt could use them do some of the kind of attacks mentioned above. Which is why XmlHttpRequests can't do cross-site requests. CORS exists to let XmlHttpRequests do cross-site requests only in an opt-in way, so a site can opt-in to it in ways that it knows/believes are secure from attacks like above. I'm not saying the overall web security story is perfect (nobody would ever say that!), but that's where CORS comes from, and why it's different than img tag. The common misconception is that CORS exists as some kind of general access control to your website. It only exists to keep the user of a well-behaved browser safe (in certain ways) from malicious Javascript on a site they are visiting. CORS has nothing at all to do with any scenarios involving malicious browsers, which are of course free to ignore CORS.
- thaumasiotes 5y ago> (perhaps impossible on a properly-implemented site?) Well, "proper implementation" would prevent a lot of attacks. But there's nothing stopping you, as a web server, from making state changes in response to GET requests, and it's far from unheard of.
- bzbarsky 5y agoThe thing XHR added was: 1) POST. 2) Of arbitrary content. 3) With an arbitrary Content-Type header. You don't get an OPTIONS for the sort of XHR request that you could do with an <img> tag (always GET). You don't get an OPTIONS for the sort of XHR request that you could do with a <form> tag (GET or POST). You only get OPTIONS if one of the following is true: * Your request method is not GET/HEAD/POST * You set a header value for a header other than Accept, Accept-Language, Content-Language, or Content-Type. * You set Content-Type to a value other than "application/x-www-form-urlencoded", "multipart/form-data", or "text/plain". * You have upload listeners on the XHR upload. * You use ReadableStream in the request.
- thaumasiotes 5y agoTrue, though POST requests claiming to send form data are among the most threatening requests (and still would be if they weren't immune to CORS). It's a weird hole from a security perspective, though as you note it makes perfect sense from a historical-development perspective.
- jrochkind1 5y agoAlso 4) ability to see the text results of your request. No way for JS to do that with an IMG tag, I don't think.
- bzbarsky 5y agoIf the XHR is same-origin then you control that text anyway, so know what it is (and there is no OPTIONS anything going on), and if it's cross-origin then the text results are not exposed by default (unless the server jumps through the other CORS hoops to expose them). The OPTIONS bits are not relevant to this part.