简体   繁体   English

CORS服务器端与客户端端? 为什么一个有效,但另一个有效?

[英]CORS server-side vs client-side? Why one works, but another - doesn't?

I am not sure I understand this. 我不确定我是否理解这一点。 I can write a simple server-side code to grab any website HTML content. 我可以编写一个简单的服务器端代码来获取任何网站HTML内容。 Run it on my local PC or on any hosting and retrieve any page from any website. 在我的本地PC或任何主机上运行它,并从任何网站检索任何页面。 But this couldn't be done via javaScript? 但这不能通过javaScript完成吗?

How is that possible for remote host to know what sort of application is making a request? 远程主机如何知道正在发出哪种应用程序的请求? Cross origin requests are not allowed for security purpose? 出于安全考虑,不允许跨源请求吗? How come I can make the exact same request using the server-side code? 我怎么能使用服务器端代码发出完全相同的请求? And run this code locally or remotely anywhere? 并在本地或任何地方远程运行此代码? Below is a simple example hot to grab HTML page content from the Weather site, which works fine. 下面是一个简单的示例,可以从Weather网站获取HTML页面内容,效果很好。 But I cannot do this from within JavaScript code? 但是我不能在JavaScript代码中执行此操作吗? Doesn't make sense. 没道理

public static class WeatherManager
{
    private static HtmlDocument document = new HtmlDocument();

    public static MyWeather GetWeather()
    {
        try
        {
            var web = new HtmlWeb();
            document = web.Load("http://www.weatheroffice.gc.ca/city/pages/on-143_metric_e.html");
        }
        catch (Exception ex)
        {
            throw new Exception("Weather is not loaded");
        }

        var mainContent = document.DocumentNode.SelectSingleNode("//*[@id='mainContent']");
        var nownode = mainContent.SelectSingleNode("//section[1]/details/div/div");
        var forecastnodes = mainContent.SelectNodes("//section[2]/details/table[1]/tr[2]/td");

        // Do some processing....
    }
}

But when I try to make similar request from Angular (or any I think JS lib) 但是当我尝试从Angular(或任何我认为JS lib)发出类似请求时

getWeatherForecast() {
    const url = 'https://weather.gc.ca/city/pages/on-143_metric_e.html';
    return this.$http.get(url);
}

I get something like this 我得到这样的东西

在此处输入图片说明

I know... CORS ok, but if its done for security purpose, how come I can make these CORS requests anyway I like, for example as server-side code above? 我知道... CORS可以,但是如果出于安全目的而完成,我怎么仍可以发出这些CORS请求,例如上面的服务器端代码?

But this couldn't be done via javaScript? 但这不能通过javaScript完成吗?

Yes, it can — just not on a browser. 是的,它可以-只是不能在浏览器上。 You could make the request with JavaScript in Node, or in a JVM (since the JVM supports JavaScript via javax.script ), or in a Metro app on Windows, etc. 您可以在Node或JVM中使用JavaScript发出请求(因为JVM通过javax.script支持JavaScript),或者在Windows上的Metro应用程序中发出请求。

How is that possible for remote host to know what sort of application is making a request? 远程主机如何知道正在发出哪种应用程序的请求?

It doesn't. 没有。 The browser enforces the Same Origin Policy, not the server. 浏览器强制实施相同起源策略,而不是服务器。

How come I can make the exact same request using the server-side code? 我怎么能使用服务器端代码发出完全相同的请求?

... ...

...but if its done for security purpose, how come I can make these CORS requests anyway I like, for example as server-side code above? ...但是,如果出于安全目的而这样做,我怎么会以自己喜欢的方式发出这些CORS请求,例如上面的服务器端代码?

Because your server-side code doesn't have access to potentially-confidential client-side information. 因为您的服务器端代码无权访问潜在的机密客户端信息。 From the Wikipedia article on the SOP : SOP上的Wikipedia文章

The same-origin policy helps protect sites that use authenticated sessions. 同源策略有助于保护使用经过身份验证的会话的站点。 The following example illustrates a potential security risk that could arise without the same-origin policy. 以下示例说明了在没有同源策略的情况下可能产生的潜在安全风险。 Assume that a user is visiting a banking website and doesn't log out. 假设用户正在访问银行网站且未注销。 Then, the user goes to another site that has some malicious JavaScript code running in the background that requests data from the banking site. 然后,用户转到另一个站点,该站点的某些恶意JavaScript代码在后台运行,并从银行站点请求数据。 Because the user is still logged in on the banking site, the malicious code could do anything the user could do on the banking site. 由于用户仍在银行站点上登录,因此恶意代码可以执行用户在银行站点上可以执行的任何操作。 For example, it could get a list of the user's last transactions, create a new transaction, etc. This is because the browser can send and receive session cookies to the banking site based on the domain of the banking site. 例如,它可以获取用户最近一次交易的列表,创建新交易等。这是因为浏览器可以基于银行站点的域向银行站点发送和接收会话cookie。

The user visiting the malicious site would expect that the site he or she is visiting has no access to the banking session cookie. 访问恶意站点的用户可能希望他或她访问的站点无法访问银行会话cookie。 While it is true that the JavaScript has no direct access to the banking session cookie, it could still send and receive requests to the banking site with the banking site's session cookie. 虽然JavaScript确实不能直接访问银行会话cookie,但它仍可以使用银行站点的会话cookie向银行站点发送和接收请求。 Because the script can essentially do the same as the user would do, even CSRF protections by the banking site would not be effective. 因为脚本实际上可以执行与用户相同的操作,所以即使由银行站点进行的CSRF保护也将无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM