|
| 1 | +# Hot link protection |
| 2 | + |
| 3 | +Hot linking is when one web site includes data served from another, for example |
| 4 | +a scraper site incorporating images served by a news site. This is often unwelcome |
| 5 | +because it means you pay for the bandwidth but another site gets the benefit. |
| 6 | +This sample illustrates how to prevent third party sites hot linking to your site. |
| 7 | + |
| 8 | +The sample acts as a proxy for the origin site, but when it detects an image |
| 9 | +being served, it checks the `Referer` header to verify that the request is |
| 10 | +coming from the proxy site. |
| 11 | + |
| 12 | +## Try it out |
| 13 | + |
| 14 | +Trying this sample out requires you to run a site that tries to hot link an image |
| 15 | +that is protected by the proxy. You can find some HTML for this in the `test-site` |
| 16 | +directory. (For illustrative purposes, this HTML hot links both the origin site and the |
| 17 | +proxy. In a real world scenario, your origin site would not be accessible from the |
| 18 | +public internet - it would be behind the proxy.) |
| 19 | + |
| 20 | +In one terminal, start the proxy by running: |
| 21 | + |
| 22 | +```sh |
| 23 | +spin up --build |
| 24 | +``` |
| 25 | + |
| 26 | +(You can visit `http://127.0.0.1:3000` to verify that images on the site appear correctly.) |
| 27 | + |
| 28 | +In another terminal, start the test site by running: |
| 29 | + |
| 30 | +```sh |
| 31 | +cd test-site |
| 32 | +python -m http.server 8080 # or python3 |
| 33 | +``` |
| 34 | + |
| 35 | +Then visit `http://127.0.0.1:8080` to see that the link via the proxy is blocked. |
| 36 | + |
| 37 | +> Note: You may see different behaviour according to whether you visit `localhost` or `127.0.0.1`! |
| 38 | +> This is because the way the sample checks for 'same site' doesn't cater for multiple sites |
| 39 | +> on different ports. In a real deployment this would typically not be a concern; but also, |
| 40 | +> it's a sample and we aimed to keep things simple rather than fully featured!) |
0 commit comments