为什么我的 XMLHttpRequest 不允许 XSS?

Why does my XMLHttpRequest not allow XSS?

我正在编写一个简单的应用程序来从位于 http://feeds.bbci.co.uk/news/rss.xml 的 bbc rss 提要中提取新闻故事。

它需要完全在客户端 运行 而不是使用 jQuery,因此 JSONP 不是一个可能的解决方案。我一直在本地主机上使用 IE 进行测试,并在检测到跨站点请求时单击弹出的 "Allow Content" 按钮。 Chrome 和 Firefox 并不是那么容易让它们允许这样做,我现在想在这些浏览器上进行测试,看看我的应用程序是否可以在它们上运行。

到目前为止..... 我已经尝试更改我的 Javascript 以使用像这样的 CORS 请求...

function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {

    // Check if the XMLHttpRequest object has a "withCredentials" property.
    // "withCredentials" only exists on XMLHTTPRequest2 objects.
    xhr.open(method, url, true);

  } else if (typeof XDomainRequest != "undefined") {

    // Otherwise, check if XDomainRequest.
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
    xhr = new XDomainRequest();
    xhr.open(method, url);

  } else {

    // Otherwise, CORS is not supported by the browser.
    xhr = null;

  }
  return xhr;
}

var xhr = createCORSRequest('GET', feedURL);
 xhr.withCredentials = true;
 if (!xhr) {
   throw new Error('CORS not supported');
 }
 xhr.onload = function() {
     if (xhr.status === 200) {
      var xmlDoc;
   if (window.DOMParser){
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(xhr.responseText,"text/xml");
   }
   else{ // Internet Explorer
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.loadXML(xhr.responseText); 
   }

   //do some stuff
     }
     else {
         alert('Request failed.  Returned status of ' + xhr.status);
     }
 };
 xhr.send();

我也已将它上传到我的 Web 服务器并使用 IIS 6 托管它。我使用这些设置添加了一个 Web 配置。

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
    <defaultDocument>
      <files>
        <add value="rss.html" />
      </files>
    </defaultDocument>
  </system.webServer>
</configuration>

我找到了一篇关于在 IIS 中设置处理程序映射的文章。建议将 OPTIONSVerbHandler 设置为 ISAPI ...但是我没有那个设置。

任何人都可以阐明这一点。我将不胜感激。

经过更多研究。似乎最简单的解决方案是创建我自己的代理。

  • 将静态站点转换为空白ASP.Net Web 应用程序
  • 在与来自服务器的 bbc 提要联系的项目中创建通用处理程序
  • 从客户端 JS 调用该处理程序

这是我的经纪人,有兴趣的人可以

using System;

使用System.Collections.Generic; 使用 System.IO; 使用 System.Linq; 使用 System.Net; 使用 System.Web; 使用 System.Xml;

命名空间提要 { /// /// rss 的摘要描述 /// public class rss:IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        string locationsRequest = CreateRequest();
        context.Response.Write(locationsRequest);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public static string CreateRequest()
    {
        return XmlHttpRequest("http://feeds.bbci.co.uk/news/rss.xml", "");
    }

    public static string XmlHttpRequest(string urlString, string xmlContent)
    {
        string response = null;
        HttpWebRequest httpWebRequest = null;//Declare an HTTP-specific implementation of the WebRequest class.
        HttpWebResponse httpWebResponse = null;//Declare an HTTP-specific implementation of the WebResponse class

        //Creates an HttpWebRequest for the specified URL.
        httpWebRequest = (HttpWebRequest)WebRequest.Create(urlString);

        try
        {
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(xmlContent);
            //Set HttpWebRequest properties
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentLength = bytes.Length;
            httpWebRequest.ContentType = "text/xml; encoding='utf-8'";

            using (Stream requestStream = httpWebRequest.GetRequestStream())
            {
                //Writes a sequence of bytes to the current stream 
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();//Close stream
            }

            //Sends the HttpWebRequest, and waits for a response.
            httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            if (httpWebResponse.StatusCode == HttpStatusCode.OK)
            {
                //Get response stream into StreamReader
                using (Stream responseStream = httpWebResponse.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                        response = reader.ReadToEnd();
                }
            }
            httpWebResponse.Close();//Close HttpWebResponse
        }
        catch (WebException we)
        {   //TODO: Add custom exception handling
            throw new Exception(we.Message);
        }
        catch (Exception ex) { throw new Exception(ex.Message); }
        finally
        {
            httpWebResponse.Close();
            //Release objects
            httpWebResponse = null;
            httpWebRequest = null;
        }
        return response;
    }
}

}