【问题标题】:AngularJS JSON vulnerability protection not strippedAngularJS JSON漏洞保护未剥离
【发布时间】:2017-05-22 16:34:04
【问题描述】:

我有一个与 Api 通信的 Angular 应用程序,我在其中添加 JSON 响应前缀,该响应仅由 )]}', 的数组组成,正如 angular's official documentation 建议的那样。

问题是,我的浏览器似乎试图在 angular 之前解码响应。

我在调试器中一步一步走,在xhr.onload回调被触发后,响应已经null

我说的代码是这个:

// From angular.js(1.5.9):12122
xhr.onload = function requestLoaded() {
        var statusText = xhr.statusText || '';

        // responseText is the old-school way of retrieving response (supported by IE9)
        // response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
        var response = ('response' in xhr) ? xhr.response : xhr.responseText;

        // normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
        var status = xhr.status === 1223 ? 204 : xhr.status;

        // fix status code when it is 0 (0 status is undocumented).
        // Occurs when accessing file resources or on Android 4.1 stock browser
        // while retrieving files from application cache.
        if (status === 0) {
          status = response ? 200 : urlResolve(url).protocol === 'file' ? 404 : 0;
        }

        completeRequest(callback,
            status,
            response,
            xhr.getAllResponseHeaders(),
            statusText);
      };
}

xhr.response 在 Angular 做任何事情之前为空(我认为)。

Angular 尝试使用此代码去除前缀:

function defaultHttpResponseTransform(data, headers) {
  if (isString(data)) {
    // Strip json vulnerability protection prefix and trim whitespace
    var tempData = data.replace(JSON_PROTECTION_PREFIX, '').trim();

    if (tempData) {
      var contentType = headers('Content-Type');
      if ((contentType && (contentType.indexOf(APPLICATION_JSON) === 0)) || isJsonLike(tempData)) {
        data = fromJson(tempData);
      }
    }
  }

  return data;
}

但此时数据绝不是字符串,浏览器已经尝试对其进行解码(但失败了)。

Api 的输出如下所示:

)]}',
[{"name": "A"},{"name": "B"}]

如果我删除前缀,它可以正常工作。我试过了: - 歌剧 45.0 - 火狐 46 - 铬 58

它们的行为都相似,所以我可能遗漏了一些东西。

响应头包含以下内容:

HTTP/1.1 200 OK
Server: nginx/1.11.5
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Cache-Control: no-cache, private
[...]
Content-Length: 1793

有什么想法吗?

非常感谢。

【问题讨论】:

  • 为什么你的 xhr.response 为空?您的 api 是否返回空响应?
  • 是的,它正在工作..我在搜索时未能找到这个问题..无论如何,您能发布一个答案以便我接受吗?这将增加其他有相同问题的人找到答案的机会。顺便说一句,感谢您的帮助。
  • 当然。很高兴它奏效了。只是希望我们能感谢原作者。

标签: javascript angularjs json security xss


【解决方案1】:

正如 Jeff Sheets 在this answer 中所指出的,您的请求配置对象可能包含responseType: 'json'。这可能会导致对象被浏览器的底层 XMLHttpRequest 实现丢弃,该实现在将其传递回应用程序之前指定此属性。

删除responseType 属性,或将其设置为文本应该可以解决问题。

responseType 记录在 MDN 上,其中声明

如果在 Worker 环境中完成,将忽略将 responseType 的值设置为“document”。将 responseType 设置为特定值时,作者应确保服务器实际上正在发送与该格式兼容的响应。 如果服务器返回的数据与设置的responseType不兼容,则response的值为null。另外,为同步请求设置responseType会抛出InvalidAccessError异常。

(强调我的)

【讨论】:

  • 我想知道使用 $http 代替手卷 xhr 是否会为您做到这一点。 Angular 包装某些浏览器功能是有原因的。
  • 我相信 OP 正在使用$http
  • 啊,我明白了。他的 xhr.onload 是 $http 的 onload,而不是 OP 自己的 onload。我的错。
  • 是的,一开始我也是这么想的,但是在AngularJS源代码的相关行中指出了他。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多