0

也许我只是不理解这一点,但这对我来说似乎没有意义。我有一个暴露 ASP.NET WebApi 的 MVC4 项目。在该项目中调用 API 效果很好,但显然从另一个正在运行的项目(在另一个端口上)调用它需要跨站点脚本。

但这是我的问题:这不会破坏 API 的目的吗?如果我想从我的站点调用 reddit API,这被认为是跨站点脚本这一事实使得它不仅是一种糟糕的安全实践,而且在某些情况下是不可能的。

如果需要 XSS 来执行此操作,那是否会使 AJAX 作为一个整体变得毫无用处?

4

2 回答 2

1

传统上,大多数应用程序都有服务器和客户端组件。服务器组件将完成所有繁重的工作,包括向其他 API 发出请求。由于 API 请求是在服务器端完成的,因此请求可以发送到任何远程 API 服务器。从来没有考虑过从客户端访问 API,因为人们希望服务器这样做。

近年来,我们看到越来越多的功能从服务器推送到客户端,特别是通过 JavaScript。但是由于浏览器的同源策略,发出远程请求是无法转移到客户端的事情之一。所以并不是说 API 的目的被打败了,而是我们现在正在以我们以前没有想到的方式使用 API。

浏览器突然忽略同源策略是不负责任的。这将破坏成千上万个依赖同源策略来确保安全的站点。因此,W3C 提出了跨域资源共享(CORS) 规范。CORS 规范允许跨域发出请求,但通过让服务器对谁可以访问 API 拥有最终决定权来安全地做到这一点。这使得跨域请求成为可能,而不会破坏现有的 API。

于 2013-01-19T04:36:02.177 回答
1

Simple answer: Of course not!! Pretty much the whole of the modern web is built on AJAX, if it was so pointless it would never have gone from a MS proprietary API to being the backbone of web 2.0 and all that has come since.

Complex answer: Firstly, XSS is a form of attack/vulnerability, not a form of request. What you're referring to is the same-origin policy, which limits AJAX requests to the same domain, for security reasons.

JSONP is typically used to make async requests to third party APIs. Your own API will typically sit on the same domain as your website so you will not have problems. If your API must be on another domain, you can either look at CORS or setup of a transparent reverse proxy to forward your requests to another server.

Hopefully this all makes sense, it'll at least give you a good foundation of knowledge to build from.

于 2013-01-18T14:24:36.527 回答