doucongqian6644 2014-05-03 17:59
浏览 111
已采纳

在基于AJAX的应用程序中使用令牌防止CSRF

I'm using tokens to prevent CSRF attacks in my application. But this application is single page and heavily AJAX based, so I need to find a way to provide a valid token for N actions in a single page:

e.g. A document fragment loads with 3 possible actions, each action needs a token to work properly but server side has just one valid token at time...

Since each token is just a encrypted nonce (the value isn't based in a specific form), I came with the idea of automatize the token assignation for each action with something like this:

  1. The App intercepts an AJAX call, and check if it's a sensitive action (i.e. delete a user)
  2. A token is requested to the server before the action proceed
  3. The token is generated and then added to the action request
  4. The action in executed since the request included a valid token
  5. Do the same for any subsequent actions executed via AJAX

I believe that method isn't effective enough because the attacker can use a script that does exactly the same my App does (retrieve token and append to the request).

How can I improve my method to be effective against CSRF attacks?

Additional info: My backend environment is PHP/Phalcon and the tokens are generated using Phalcon.

  • 写回答

2条回答 默认 最新

  • doushou1298 2014-05-05 11:27
    关注

    A simpler method than using tokens for an AJAX only might be to check headers that can only be present in an AJAX request from your own domain.

    Two options are:

    The Origin header can also be used for normal HTML form submissions, and you would verify that this contains the URL of your own site before the form submission does its processing.

    If you decide to check the X-Requested-With header, you will need to make sure it is added to each AJAX request client side (JQuery will do this by default). As this header cannot be sent cross domain (without your server's consent to the browser first), checking that this is present and set to your own value (e.g. "XMLHttpRequest") will verify that the request is not a CSRF attack.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备