我正在与一位同事讨论我们的网站是否容易受到 CSRF 攻击。他说,由于我们对 CSRF 不可能的所有事情都使用 RESTful AJAX 请求。假设更新您的帐户信息的请求如下所示:
POST /ajax/account/savebasic HTTP/1.1
Host: www.mysite.com
Connection: keep-alive
Content-Length: 72
Accept: application/json, text/javascript, */*; q=0.01
Origin: https://www.mysite.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.65 Safari/537.36
Content-Type: application/json; charset=UTF-8
Referer: https://www.mysite.com/portal
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: X-Mapping-fjhppofk=B8BFE26CD0B3A37348ECC6FFE3940306; sdemail_5228=tester%40test.com; sd_5228=%7B%7D; connect.sid=s%3AAQIPx7hyddXEYTOuyP857jEG.D002GI6%2FSEF0m5WTBUxRmbjpE48%2BMkPxEe9o2T9DH1Y; __utma=160595647.1524201255.1358783236.1379476052.1379533347.150; __utmb=160595647.1.10.1379533347; __utmc=160595647; __utmz=160595647.1376327581.102.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=http://mysite.com/
{"fname":"abe","lname":"m","company":"comp","email":"abe@test.com"}
我创建了一个测试 HTML 页面,testdomain.com
如下所示:
<html>
<head>
<title>Bad site</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div id="main">Hey!</div>
<script type="text/javascript">
$.ajax({
type: "POST",
url: "http://www.mysite.com/ajax/account/savebasic",
data: {
"fname": "CSRF",
"lname":"CSRF",
"company":"CSRF",
"email":"abe@test.com"
},
dataType: "jsonp"
});
</script>
</body>
</html>
认为它会覆盖所有内容,但它重定向了几次并最终引发 404 错误。我的同事说因为我们使用 AJAX/REST 而对 CSRF 是安全的,这是否正确?