Joined August 2013
·
Posted to
Javascript-Friendly REST API with CORS
over 1 year
ago
This is a handy snippet, but it might be worth noting what such a "most permissive" policy means since it's not always desirable.
If you use this policy, and if you're providing an API at MyRESTService.com, then a user might browse to EvilDomain.com and receive JS from EvilDomain.com that launches requests to MyRESTService.com and (if I recall correctly) that even uses authentication cookies from MyRESTService.com. So this is not what you want, if you want to prevent foreign domains from triggering cookie-authenticated requests to your API without the user's knowledge.
If you want something more restrictive, I researched this in painful detail on nginx: https://gist.github.com/algal/5480916
Achievements
78 Karma
0 Total ProTip Views
Komodo Dragon
Have at least one original repo where Java is the dominant language
Walrus
The walrus is no stranger to variety. Use at least 4 different languages throughout all your repos
Forked
Have a project valued enough to be forked by someone else
Mongoose
Have at least one original repo where Ruby is the dominant language
Bear 3
Have at least three original repos where Objective-C is the dominant language
Bear
Have at least one original repo where Objective-C is the dominant language
Epidexipteryx
Have at least one original repo where C++ is the dominant language
Charity
Fork and commit to someone's open source project in need
@irakli sorry, you're right. I thought you were setting Access-Control-Allow-Credentials: true, but clearly you're not, and that's the super permissive setting to be a bit more careful about.
However, I think I disagree with your second point. I think this is an issue even if you're creating a RESTful API. For one, allowing credentials will allow foreign JS to launch requests that use HTTP Basic authorization credentials cached in the browser, which is quite orthodox, so this a problem even if you're not using cookies.
Second, I'm not convinced using persistent authentication cookies means you're not being RESTful. If the cookie holds all the user info, encrypted, then there's no additional server state beyond what you'd have in order to maintain a user accounts table, right? I don't see how cookies are worse than any other token you hand a client for later authentication, like a token set in a custom header. But I may be fundamentally misunderstanding how one is "supposed to" do RESTful authentication or what counts as such. I welcome education!