Skip to content

Security: add ignoreXForwardedHost option to block SSRF attacks #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.8.0 (2023-12-22)
Added:
- Added option `ignoreXForwardedHost` to ignore any `X-Forwarded-Host` headers, which can be used to block SSRF attacks

## 3.7.0 (2023-05-22)
Added:
- Add Google-InspectionTool to the recognized user agents
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ Option to forward headers from request to prerender.
app.use(require('prerender-node').set('forwardHeaders', true));
```

### ignoreXForwardedHost

Option to ignore `X-Forwarded-Host` header. Can be used to block SSRF attacks.

**Important:** this header is used to preserve the original host header for servers that sit behind a load balancer or internal reverse proxy. If your server does not need this header then it's probably safe to enable this option. Check your environment before enabling this option and make sure to test it first.

The url that Prerender will get and return the (possibly cached) contents of is created by taking the first available of the following:
- The `host` option, as set with `app.use(require('prerender-node').set('host', 'example.com'));`
- The `X-Forwarded-Host` header, if `ignoreXForwardedHost` is **NOT** set to `true`
- The Host header
Finally the originally requested url path is added to the end.

The Prerender.io servers will connect to any url you specify at the end of the prerender service URL. This means that if someone visits `https://mysite.com`, and the request is modified with the path `/some/path` and the header `x-forwarded-host` set to `example.com`, Prerender will connect to `https://example.com/some/path`. (You actually don't need to intercept any request, you can just run the whole request directly, e.g. by using Postman.)

If you set `ignoreXForwardedHost` to `true`, the `X-Forwarded-Host` header will be ignored, effectively blocking these SSRF attacks.

```js
app.use(require('prerender-node').set('ignoreXForwardedHost', true));
```

### prerenderServerRequestOptions

Option to add options to the request sent to the prerender server.
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ prerender.buildApiUrl = function(req) {
if (this.protocol) {
protocol = this.protocol;
}
var fullUrl = protocol + "://" + (this.host || req.headers['x-forwarded-host'] || req.headers['host']) + req.url;

// if set, ignore X-Forwarded-Host header to block SSRF attacks
const xforwardedHost = this.ignoreXForwardedHost !== true && req.headers['x-forwarded-host'];
const fullUrl = `${protocol}://${this.host || xforwardedHost || req.headers.host}${req.url}`;
return prerenderUrl + forwardSlash + fullUrl;
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prerender-node",
"version": "3.7.0",
"version": "3.8.0",
"description": "express middleware for serving prerendered javascript-rendered pages for SEO",
"author": "Todd Hooper",
"license": "MIT",
Expand Down