Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Alter Response Headers](./samples/alter-headers/)
- [Auto-Complete API](./samples/auto-complete/e/)
- [Cookie Parsing](./samples/parse-cookie/)
- [Hot-Link Protection](./samples/hot-link-protection/)
- [Traffic Filtering: Block By IP address](./samples/block-by-ip/)
- [Traffic Filtering: Block By user country](./samples/block-by-country/)
- [Traffic mirroring and splitting](./samples/traffic-splitting/)
Expand Down
5 changes: 5 additions & 0 deletions samples/hot-link-protection/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist
target
.spin/
build/
1 change: 1 addition & 0 deletions samples/hot-link-protection/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KNITWIT_SOURCE=./config/knitwit.json
40 changes: 40 additions & 0 deletions samples/hot-link-protection/README.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth spelling out how to deploy to FwF? Then a note to update the test-site index.html with the fwf app address?

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Hot link protection

Hot linking is when one web site includes data served from another, for example
a scraper site incorporating images served by a news site. This is often unwelcome
because it means you pay for the bandwidth but another site gets the benefit.
This sample illustrates how to prevent third party sites hot linking to your site.

The sample acts as a proxy for the origin site, but when it detects an image
being served, it checks the `Referer` header to verify that the request is
coming from the proxy site.

## Try it out

Trying this sample out requires you to run a site that tries to hot link an image
that is protected by the proxy. You can find some HTML for this in the `test-site`
directory. (For illustrative purposes, this HTML hot links both the origin site and the
proxy. In a real world scenario, your origin site would not be accessible from the
public internet - it would be behind the proxy.)

In one terminal, start the proxy by running:

```sh
spin up --build
```

(You can visit `http://127.0.0.1:3000` to verify that images on the site appear correctly.)

In another terminal, start the test site by running:

```sh
cd test-site
python -m http.server 8080 # or python3
```

Then visit `http://127.0.0.1:8080` to see that the link via the proxy is blocked.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if my system/python, but the command to run the test-site shows the following:

$ python -m http.server 8080
Serving HTTP on :: port 8080 (http://[::]:8080/) ...

and while http://localhost:8080 navigates to the test site correctly, http://127.0.0.1:8080 doesn't (rather, eg curl: (56) Recv failure: Connection reset by peer occurs).

Do you see the same thing? If so, should we update this to http://localhost:8080?


> Note: You may see different behaviour according to whether you visit `localhost` or `127.0.0.1`!
> This is because the way the sample checks for 'same site' doesn't cater for multiple sites
> on different ports. In a real deployment this would typically not be a concern; but also,
> it's a sample and we aimed to keep things simple rather than fully featured!)
14 changes: 14 additions & 0 deletions samples/hot-link-protection/config/knitwit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": 1,
"project": {
"worlds": [
"spin-http"
]
},
"packages": {
"@fermyon/spin-sdk": {
"witPath": "../../bin/wit",
"world": "spin-imports"
}
}
}
Loading