Is checking the referrer enough to protect against a CSRF attack?

后端 未结 5 1245
孤街浪徒
孤街浪徒 2020-12-10 00:10

Is checking the referrer enough to protect against a cross site request forgery attack? I know the referrer can be spoofed, but is there any way for the attacker to do that

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 00:51

    Follow the norm: use tokens.

    Checking the referrer actually does nothing, because the request is coming from that page anyway! The problem you are trying to prevent is the page being requested without the user doing anything; not the page being hit itself.

    Tokens are the way to protect against this. You generate one, store it in the session, and write it to the HTML, then, upon posting, you check the one you receive, and see if it matches the one you expect. If it doesn't, you fail. Either way, you generate a new token afterwards.

    It may also be relevant to consider that this will mess people up if the have multiple pages; so you may like to make a different token per page.

提交回复
热议问题