You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/mixed-content.md
+86-16Lines changed: 86 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,34 +4,104 @@ title: Mixed Content
4
4
permalink: /mixed-content/
5
5
---
6
6
7
-
### What is mixed content?
7
+
When an HTTPS website references insecure (HTTP) resources, this is called **[mixed content](http://www.w3.org/TR/mixed-content/)**.
8
8
9
-
When you view a page in your browser, it is almost never just a single request. Almost every web page contains resources, such as Javascript, CSS, and images, which cause additional requests. Sometimes these requests are to the same servers as the rest of the site, other times they are external resources (such as Google Analytics snippets of JavaScript).
9
+
Browsers prevent an HTTPS website from loading most insecure resources, like fonts, scripts, etc. Migrating an existing website from HTTP to HTTPS means identifying and fixing or replacing mixed content.
10
10
11
-
When the main page is loaded over `HTTPS` and these sub-resources are loaded over `HTTP` this is called **mixed content**.
11
+
[Mixed content](http://www.w3.org/TR/mixed-content/) comes in two varieties:
12
12
13
-
Mixed content comes in two varieties:
13
+
**Active** mixed content includes resources that can greatly change the behavior of a website, such as JavaScript, CSS, fonts, and iframes. Browsers refuse to load active mixed content, which often results in affected pages being completely unstyled or broken. Browsers treat these very aggressively because of the consequences if they were compromised. For example, a single compromised Javascript file compromises the entire website, regardless of how other resources are loaded.
14
14
15
-
**Active** mixed content includes resources that can greatly change the behavior of a website, such as JavaScript, CSS, fonts, and iframes. Browsers refuse to load active mixed content, which often results in affected pages being completely unstyled or broken. Browsers treat these very aggressively because of the consequences if they were compromised, for example a single compromised Javascript file compromises the entire website, regardless of how other resources are loaded.
15
+
**Passive** mixed content includes resources whose impact on the page's overall behavior is more minimal, such as images, audio, and video. Browsers will load passive mixed content, but will typically change the HTTPS indicator.
16
16
17
-
**Passive** mixed content includes resources whose impact on the page's overall behavior is more minimal, such as images, audio, and video. Browsers will load passive mixed content, but will typically change the HTTPS indicator. For example, in Chrome this means that the "green lock" icon becomes a gray lock with a yellow triangle over it.
17
+
In Chrome, a website indicator for passive mixed content looks like this:
18
18
19
-
### Why is mixedcontent a problem?
19
+

20
20
21
-
Mixed content is a problem because, were browsers to allow it, an attacker would be able to conduct a MITM attack against it the same way they could the main page. Even where browsers do allow it, as with images, attackers can manipulate what the page looks like, and so the yellow-lock icon is intended to communicate that security has been weakened and user confidence should be reduced. In addition, an attacker will be able to read any cookies for that domain which do have the `Secure` flag, and set cookies.
21
+
### Migration strategy
22
22
23
-
When a website is `HTTP`-only, loading other `HTTP` sub-resources does not generate any sort of warning, and so websites operating over `HTTP` often accumulate many of these sub-resources.
23
+
Every website's mixed content situation will be different, but the general approach is:
24
24
25
-
When an `HTTP` website tries to migrate to `HTTPS`, these can often become a source of difficulty.
25
+
* Enable `https://` for your website, but don't force a redirect. Continue to present the `http://` version as the canonical URL to search engines.
26
+
* Identify the most obvious and widespread pieces of mixed content by loading your website in a browser over `https://` and observing breakages. Chrome and Firefox will log any mixed content warnings to the console, which should point out necessary site-wide changes. Use these to [secure your resource links](#linking-to-resources-securely).
27
+
* After fixing them, tackle the long tail by [scanning your code](#scanning-your-code), [crawling your website](#crawling-your-website), and potentially adding [automatic mixed content reporting](#automatic-mixed-content-reporting).
28
+
* Finally, force the redirect to HTTPS, and tell search engines that your new URL starts with `https://`.
26
29
27
-
### Strategies for dealing with mixed content
30
+
Note: the below instructions use tools **optimized for an OS X or Linux environment**. Documentation for Windows-based tools would be a welcome contribution to this guide.
28
31
29
-
The easiest approach is to use relative URLS (e.g. `<img src="https://github.com/media/my-picture.png" />`) for links which are on your domain. These will work with both `HTTP` and `HTTPS` websites. For external resources you can simply always use `https://`. Ocasionally you will find that some media you wish to hotlink is on a third-party which doesn't support HTTPS. This is a great opportunity to improve your website's privacy and to lessen your dependency on third parties by hosting that media on your own server instead (you can also encourage the host for that media to begin offering `https://` support on their website).
32
+
### Linking to resources securely
30
33
31
-
In order to locate mixed content, the easiest way to start is to load your website in a browser over `https://` and see if it looks ok. Both Chrome and Firefox will log any mixed content to the console. This gives a quick way to get a sense of how large the problem is.
34
+
Most commonly used third party services, such as Google Analytics or AddThis, **will automatically adapt** when migrating to HTTPS.
32
35
33
-
Another approach is to grep your codebase for any explicit `http://`URLs, since these will always cause a problem.
36
+
Other services may require manual updates, but have an `https://`version ready:
*[`mixed-content-scan`](https://github.com/bramus/mixed-content-scan) can crawl a website to see if it contains any references to insecure resources. It can work on either `HTTP` or `HTTPS` websites.
42
+
Generally speaking, for content on your own domain, stick to site-relative URLs wherever possible:
43
+
44
+
```html
45
+
<imgsrc="/media/my-picture.png" />
46
+
```
47
+
48
+
When migrating a site with a lot of user- or staff-submitted content (e.g. a blog), you may find media hotlinked from a third-party domain which doesn't support HTTPS.
49
+
50
+
This is a great opportunity to improve your website's privacy and lessen your dependency on third parties, by copying those media files to your own server instead and hosting them yourself.
51
+
52
+
### Scanning your code
53
+
54
+
After identifying and fixing the obvious issues, you can scan your website's files for leads. On a Mac or Linux-based system, `grep` is very handy:
55
+
56
+
Images and scripts:
57
+
58
+
grep -r "src=\"http:" *
59
+
60
+
Stylesheets and fonts:
61
+
62
+
grep -r "href=\"http:" * | grep "<link"
63
+
64
+
CSS imports and references:
65
+
66
+
grep -r "url(\"http:"
67
+
68
+
Finding links in JavaScript is more challenging, but you can look for all `http:` references and try to exclude hyperlinks in HTML or Markdown:
69
+
70
+
grep -r "http:" | grep -v "href=\"http:"
71
+
grep -r "http:" | grep -v "](http:"
72
+
73
+
74
+
### Crawling your website
75
+
76
+
[`mixed-content-scan`](https://github.com/bramus/mixed-content-scan) is a very handy command line tool that can crawl an `http://` or `https://` website to see if it contains any references to insecure resources. This is especially helpful if your content is primarily managed in a CMS.
77
+
78
+
`mixed-content-scan` requires PHP, then [installing Composer](https://getcomposer.org/doc/00-intro.md). [Install with Composer](https://github.com/bramus/mixed-content-scan#installation), then use it with your domain:
If mixed content were not blocked, an attacker could control the main website by conducting a MITM attack against any of its active resources.
104
+
105
+
Even with passive content like images, attackers can manipulate what the page looks like, and so the yellow-lock icon is intended to communicate that security has been weakened and user confidence should be reduced. In addition, an attacker will be able to read any cookies for that domain which do have the `Secure` flag, and set cookies.
106
+
107
+
When a website is accessible over `http://`, loading other insecure resources does not generate any sort of warning, and so websites operating over plain HTTP often accumulate many of these sub-resources.
0 commit comments