Fork me on GitHub
#ring
<
2016-04-10
>
sd18:04:16

is there anything i need to add to my ajax request for anti-forgery to work? I'm getting invalid token errors every time 😞

weavejester19:04:10

sd: Either an __anti-forgery-token parameter, or a x-csrf-token header or a x-xsrf-token header.

weavejester19:04:46

The README explains it.

sd19:04:48

is it not possible to use the value in the session cookie?

weavejester19:04:14

The token is compared to the token in the session.

weavejester19:04:35

The problem with POSTs is that they don’t adhere to the same-origin policy

weavejester19:04:40

Because they predate it.

weavejester19:04:48

So someone can create a form on their own website

weavejester19:04:56

Use Javascript to automatically POST it

weavejester19:04:14

And the form will be POSTed to your website with your website's session

weavejester19:04:37

The way to stop it is to also pass along a secret token that is compared to the session.

weavejester19:04:54

So the user needs to be logged in, and POSTing from a page under your control.

weavejester19:04:06

ring-anti-forgery stores this token in *anti-forgery-token*

sd19:04:23

I saw that token, and it matched the :ring.middleware.anti-forgery/anti-forgery-token in :session

sd19:04:43

since the ajax requests are being submitted with the cookie, i figured the token could be extracted that way

weavejester19:04:58

No, because the cookie isn’t safe.

sd19:04:00

but thanks for explaining, i'll try adding it as a parameter

weavejester19:04:15

The anti-forgery-token needs to be in the session and passed through a parameter.

sd19:04:55

and it's alright to just send the token to the client at some point, to include in your ajax requests?

weavejester19:04:31

Yeah. The point of the token is to make sure that the request comes from a page under your control.