Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ring-anti-forgery and web services #2

Closed
mynomoto opened this issue May 20, 2014 · 7 comments
Closed

ring-anti-forgery and web services #2

mynomoto opened this issue May 20, 2014 · 7 comments

Comments

@mynomoto
Copy link

On the readme there is a caveat about using ring-anti-forgery with web services. But it can work with those just fine and would even work out of the box if the default access-denied response included the token on the header - if the client just send the token back with its request.

Could this header be in the access-denied response by default? Or can you think about reasons not to do it?

Thanks.

@ghost
Copy link

ghost commented Aug 21, 2014

I'd also like to know, if returning the anti-forgery-token in the access-denied response could be harmful. One scenario I could think of, is that your site isn't XSS-secure, makes a faulty request on purpose and just sends the token from the response off to some third party for CSRF attacks.

@weavejester
Copy link
Member

CSRF is a form of confused deputy attack that targets websites secured via a session cookie. Your web service shouldn't be authenticating with session cookies, so anti-forgery protection does not apply. There's literally no point to including ring-anti-forgery on any part of your site not sitting behind a secured session.

@jayp
Copy link

jayp commented Dec 3, 2014

Hi @weavejester - I believe @mynomoto may be requesting this somewhat abstruse feature because webservices and user-facing webapp can't coexist in the same ring-app when using ring-anti-forgery. From the README:

Caveats

This middleware will prevent all HTTP methods except for GET and HEAD from accessing your
handler without an anti-forgery token that matches the one in the current session.

You should therefore only apply this middleware to the parts of your application designed to be
accessed through a web browser. This middleware should not be applied to handlers that define
web services.

As a clojure noob, I would like to make an attempt at a patch that adds an optional parameter (let's say :protect-urls) to wrap-anti-forgery. Or is there a better way to address the caveat? (If so, we should update the README for all noobs alike).

@weavejester
Copy link
Member

You don't need to apply wrap-anti-forgery at the top level of your app. For instance, in Compojure you could write:

(routes
 api-routes
 (wrap-anti-forgery site-routes))

@jayp
Copy link

jayp commented Dec 3, 2014

Ha! Thanks @weavejester.

To help out fellow noobs, I will submit a small patch noting this in the README.

@Parth-Brahmbhatt
Copy link

I still don't get this. In apache storm we have same post route being used by our web page and the rest api. In order to use CSRF protection I will now have to create duplicate routes?

@weavejester
Copy link
Member

CSRF attacks come about because form POSTs can be triggered across domains. If the same URI is intended to receive authenticated POST requests from both a browser and an API client, then you need some way of determining whether or not the request comes from the browser or API client.

For example, perhaps you use cookie-based sessions to authenticate browsers, and HTTP Basic Auth to authenticate API clients. You could have some custom middleware that checks to see how the user is authenticating; if it's via a session, then the anti-forgery middleware is added, otherwise it leaves it out.

mshenfield added a commit to mshenfield/tryclojure that referenced this issue Jan 20, 2016
Close Raynes#53. Bump Clojure to the latest stable release, 1.7.0.

The only major switch was avoiding the ring-clojure/ring-anti-forgery
403 permissions error that was being thrown when POST to /tutorial. The library is included
in noir.util.middleware from ring-clojure/ring-defaults. For more information
about ring-anti-forgery and POST see ring-clojure/ring-anti-forgery#2.
For some reason, upgrading to 1.7 triggered this middleware on this endpoint only.

To remedy, updated /tutorial to a GET, forgoing the nice $.load convenience
function and useing $.get. This has the nicety of being more semantically correct anyways.
$.load defaults to a GET when no data is given, as in the setupLink function.

Upgraded lib-noir to v0.9.9. In versions up to v0.9.8, it fails to start,
raising an exception due to an undeclared variable "pretty".
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants