Skip to content

Commit 2f812f7

Browse files
committed
Hot link protection
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
1 parent 5723151 commit 2f812f7

13 files changed

+3444
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
- [A/B Testing using HTTP Headers (e.g User Agent)](./samples/ab-testing-user-agent/)
1616
- [Alter Response Headers](./samples/alter-headers/)
1717
- [Auto-Complete API](./samples/auto-complete/e/)
18+
- [Auto-Complete API](./samples/hot-link-protection/)
19+
- [Hot-Link Protection](./samples/hot-link-protection/)
1820
- [Traffic Filtering: Block By IP address](./samples/block-by-ip/)
1921
- [Traffic Filtering: Block By user country](./samples/block-by-country/)
2022
- [Reading the Body of a POST Request](./samples/read-post/)
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
dist
3+
target
4+
.spin/
5+
build/

samples/hot-link-protection/.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KNITWIT_SOURCE=./config/knitwit.json

samples/hot-link-protection/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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!)
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": 1,
3+
"project": {
4+
"worlds": [
5+
"spin-http"
6+
]
7+
},
8+
"packages": {
9+
"@fermyon/spin-sdk": {
10+
"witPath": "../../bin/wit",
11+
"world": "spin-imports"
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)