weixin_33749242 2018-10-30 17:17 采纳率: 0%
浏览 16

为什么AJAX不安全?

I'm new to JS and AJAX, and one day, I tried a cross-domain AJAX request. After some researchs, I found out that AJAX could not work over cross domains (natively) because it is unsecure.

From Wikipedia: " This policy prevents a malicious script on one page from obtaining access to sensitive data on another web page through that page's Document Object Model. "

But how could an AJAX request access to "sensitive data", while you can't with default HTTP?

  • 写回答

1条回答 默认 最新

  • weixin_33736048 2018-10-30 18:01
    关注

    An AJAX request is an HTTP request.

    AJAX stands for Asyncronous Javascript And XML. It's kind of named that, after the first browser-based javascript HTTP client API, XMLHttpRequest.

    HTTP requests are not inherently insecure, but certain things might make HTTP requests problematic.

    A big one related to 'Ajax' requests is that, in the past at least, a HTTP request can carry session/cookie information.

    This means that if Ajax requests were not restricted in browser sandboxes (cross-domain), it could mean that the owner of Site A, could make a request to Site B on behalf of a user.

    Example: You're logged into a popular social network. Your browser uses a cookie to identify your logged in session. I send you a link to evil.example.org. If cross-site restrictions didn't exist, I could now make a HTTP for you + your session to the social network and act on your behalf.

    However, this is not the end of this story. It is possible to do cross-site requests. This is called a CORS requests.

    BUT: the way this works is that the owner of the site that you want to make a request to, has to allow in. In our previous example that means that the social network needs to explicitly allow "evil.example.org" to make these kind of requests.

    The way this site gives you permission is via CORS headers.

    Other ways to work around it is via:

    • Frames that are hosted on the site you're trying to access. (with specific code)
    • A proxy you control.
    • If the server you're trying to access delivers its content in a very specific way. (again, you need control of the target server).

    If you control the target server, your best options is to just use CORS though. If you don't your best bet is to setup a proxy you control.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?