http - 为什么浏览器不遵循呈现的 GitHub README.md 上图像资源的 HTTP 302 重定向?

标签 http github browser http-status-code-302

这个问题可能看起来是关于 GitHub,但实际上是关于 HTTP 和浏览器的。不幸的是,我只能在 GitHub 上重现这个问题,所以我没有比下面提供的例子更简单的例子了。

这个问题可以通过访问 https://github.com/lonelearner/img-load-issue 来观察。图像未加载到呈现的 README 中是问题所在。

这是 README.md 中的 Markdown 代码。

Foo

<img src="https://github.com/lonelearner/img-load-issue/releases/download/v1.0.0/ulam.png" alt="Image of Ulam spiral">

Bar

当我在浏览器 (Firefox) 上单击查看源代码时,我看到上面的 markdown 已呈现为以下 HTML 代码。

<article class="markdown-body entry-content" itemprop="text">
<p>Foo</p>
<p><a href="https://github.com/lonelearner/img-load-issue/releases/download/v1.0.0/ulam.png" target="_blank"><img src="https://github.com/lonelearner/img-load-issue/releases/download/v1.0.0/ulam.png" alt="Image of Ulam spiral" style="max-width:100%;"></a></p>
<p>Bar</p>
</article>

您可以通过转到 https://github.com/lonelearner/img-load-issue/releases 并单击 ulam.png 来验证图像是否确实存在。生成的 HTML 代码看起来也不错。为什么浏览器加载图片失败?

如果我在 jsfiddle 中插入相同的 HTML 代码,图像似乎可以正常加载。示例:https://jsfiddle.net/qxybjwf1/

为什么这张图片在 jsfiddle.net 上加载正常,但在 GitHub 上加载正常?

我在 Firefox 检查器的“网络”选项卡中检查了请求和响应 header 。返回 ulam.png 的 GET 请求

302 Found
Location: https://github-cloud.s3.amazonaws.com/releases/64644360/7e448240-57e5-11e6-8bda-58e154461718.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAISTNZFOVBIJMK3TQ%2F20160801%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20160801T072537Z&X-Amz-Expires=300&X-Amz-Signature=fb0e8423517a09b80624b3512f8ebeffc54cfb976e409379c532b4625b179095&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dulam.png&response-content-type=application%2Foctet-stream

在“网络”选项卡中,我没有看到 Firefox 发出另一个检索 amazonaws.com URL 的请求。为什么?

这是 Firefox 检查器网络选项卡中的完整请求和响应 header 。

请求 header :

Host: github.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: https://github.com/lonelearner/img-load-issue
Cookie: logged_in=no; _gh_sess=eyJzZXNzaW9uX2lkIjoiNzdkNGI0N2UxYjUzNzhkNGE4ODZlMzU1YzA2ODE4NDgiLCJzcHlfcmVwbyI6ImxvbmVsZWFybmVyL2ltZy1sb2FkLWlzc3VlIiwic3B5X3JlcG9fYXQiOjE0NzAwMzYzMzYsIl9jc3JmX3Rva2VuIjoiSnBMK0Yxd3NWb1VOc3NSRnZjWnUxSkswbG1FVkpqS3BEcEF0TkFxbmpqRT0iLCJyZWZlcnJhbF9jb2RlIjoiaHR0cHM6Ly9naXRodWIuY29tL2xvbmVsZWFybmVyL2ltZy1sb2FkLWlzc3VlIiwiZmxhc2giOnsiZGlzY2FyZCI6WyJhbmFseXRpY3NfbG9jYXRpb24iXSwiZmxhc2hlcyI6eyJhbmFseXRpY3NfbG9jYXRpb24iOiIvPHVzZXItbmFtZT4vPHJlcG8tbmFtZT4ifX19--1fe743598d196f7f04c9f1f6d27901b542b6bea3; _ga=GA1.2.594721001.1470036331; _octo=GH1.1.1243010616.1470036331; _gat=1; tz=Asia%2FKolkata
Connection: keep-alive

响应头:

Cache-Control: no-cache
Content-Type: text/html; charset=utf-8
Date: Mon, 01 Aug 2016 07:25:37 GMT
Location: https://github-cloud.s3.amazonaws.com/releases/64644360/7e448240-57e5-11e6-8bda-58e154461718.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAISTNZFOVBIJMK3TQ%2F20160801%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20160801T072537Z&X-Amz-Expires=300&X-Amz-Signature=fb0e8423517a09b80624b3512f8ebeffc54cfb976e409379c532b4625b179095&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dulam.png&response-content-type=application%2Foctet-stream
Public-Key-Pins: max-age=5184000; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho="; pin-sha256="k2v657xBsOVe1PQRwOsHsw3bsGT2VzIqz5K+59sNQws="; pin-sha256="K87oWBWM9UZfyddvDfoxL+8lpNyoUB2ptGtn0fv6G2Q="; pin-sha256="IQBnNBEiFuhj+8x6X8XLgh01V9Ic5/V3IRQLNFFc7v4="; pin-sha256="iie1VXtL7HzAMF+/PVPR9xzT80kQxdZeJ+zduCB3uj0="; pin-sha256="LvRiGEjRqfzurezaWuj8Wie2gyHMrW5Q06LspMnox7A="; includeSubDomains
Server: GitHub.com
Set-Cookie: _gh_sess=eyJzZXNzaW9uX2lkIjoiNzdkNGI0N2UxYjUzNzhkNGE4ODZlMzU1YzA2ODE4NDgiLCJzcHlfcmVwbyI6ImxvbmVsZWFybmVyL2ltZy1sb2FkLWlzc3VlIiwic3B5X3JlcG9fYXQiOjE0NzAwMzYzMzcsIl9jc3JmX3Rva2VuIjoiSnBMK0Yxd3NWb1VOc3NSRnZjWnUxSkswbG1FVkpqS3BEcEF0TkFxbmpqRT0iLCJyZWZlcnJhbF9jb2RlIjoiaHR0cHM6Ly9naXRodWIuY29tL2xvbmVsZWFybmVyL2ltZy1sb2FkLWlzc3VlIn0%3D--b5a70b2ac300eeac4032c88460bd8154b41e9b77; path=/; secure; HttpOnly
Status: 302 Found
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
Transfer-Encoding: chunked
Vary: X-PJAX, Accept-Encoding
X-Frame-Options: deny
X-GitHub-Request-Id: C40F106C:24F2:900C5F6:579EF970
X-Request-Id: 33b9ee97cf3e035d814561d7a4ccc100
X-Runtime: 0.023602
X-Served-By: 50b06cef3698e972f044d7dc2cb41530
X-UA-Compatible: IE=Edge,chrome=1
X-XSS-Protection: 1; mode=block
content-security-policy: default-src 'none'; base-uri 'self'; connect-src 'self' uploads.github.com status.github.com api.github.com www.google-analytics.com github-cloud.s3.amazonaws.com wss://live.github.com; font-src assets-cdn.github.com; form-action 'self' github.com gist.github.com; frame-ancestors 'none'; frame-src render.githubusercontent.com; img-src 'self' data: assets-cdn.github.com identicons.github.com www.google-analytics.com collector.githubapp.com *.gravatar.com *.wp.com *.githubusercontent.com; media-src 'none'; object-src assets-cdn.github.com; script-src assets-cdn.github.com; style-src 'unsafe-inline' assets-cdn.github.com
x-content-type-options: nosniff

最佳答案

图像加载被 Content Security Policy 阻止页面上的 GitHub 问题。

如果您查看页面的响应 header ,您会看到已发布 Content-Security-Policy。我已经删除了 CSP header 值的适当部分:

img-src 'self' data: assets-cdn.github.com identicons.github.com www.google-analytics.com collector.githubapp.com *.gravatar.com *.wp.com *.githubusercontent.com;


这列出了允许浏览器从当前页面加载图像的所有位置。页面上的图像标记正在尝试从以下位置加载:

https://github.com/lonelearner/img-load-issue/releases/download/v1.0.0/ulam.png


如果您查看政策,您会发现使用了 CSP 关键字 'self',这意味着提供页面的来源,在本例中为 github.com。这意味着允许发送初始图像请求。此请求以 302 和以下位置响应:

https://github-cloud.s3.amazonaws.com/releases/64644360/...


不幸的是 github-cloud.s3.amazonaws.com 域不在 img-src 指令中,因此浏览器不允许发送请求。您应该能够在浏览器的开发人员控制台中看到错误。 Chrome 为我提供了以下信息:

Refused to load the image 'https://github-cloud.s3.amazonaws.com/releases/64644360/7e448240-57e5-11e6-…%3B%20filename%3Dulam.png&response-content-type=application%2Foctet-stream' because it violates the following Content Security Policy directive: "img-src 'self' data: assets-cdn.github.com identicons.github.com www.google-analytics.com collector.githubapp.com *.gravatar.com *.wp.com *.githubusercontent.com".


除非 GitHub 添加 github-cloud.s3.amazonaws.com,否则您无法解决此问题。


2016 年 8 月 4 日更新:我向 GitHub Security 提出了这个问题,他们已将适当的条目添加到他们的 CSP header 中。这个问题现在应该已经解决了!

关于http - 为什么浏览器不遵循呈现的 GitHub README.md 上图像资源的 HTTP 302 重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38694187/

相关文章:

json - 抽象 GET、读取、解码逻辑

flash - Adobe 和浏览器都将终止 Adob​​e Flash 后,有什么方法可以运行 Web 应用程序

asp.net-mvc - 开发人员工具中缺少页面表单数据

javascript - 我怎么知道我的 js/CSS 文件是否太大?

java - 使用 Java 从特定 URL 获取整个网页

HTTP 多范围请求 - 响应中的 header

python - python 2.7 中的 HTTP 2 请求

javascript - 如何在 GitHub Pages 上托管?

github - 如何删除github主页中的 'Repositories you contribute to'

GitHub token - 限制访问