-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: add referer checker and csp header (#68)
* misc: add referer checker and csp header * chore: add tests
- Loading branch information
Showing
7 changed files
with
147 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function getOrigin(uri) { | ||
if (!uri) { | ||
return null; | ||
} | ||
const url = new URL(uri); | ||
return url.origin; | ||
} | ||
|
||
const KNOWN_REFERER_HOSTS = [ | ||
getOrigin(process.env.APP_SERVER), | ||
]; | ||
|
||
function refererChecker(req, res, next) { | ||
const referrer = req.get('Referer'); | ||
if (!referrer) { | ||
res.status(403).send('No Referer'); | ||
return; | ||
} | ||
const referrerOrigin = getOrigin(referrer); | ||
if ( | ||
KNOWN_REFERER_HOSTS.find(host => { | ||
if (!host) { | ||
return false; | ||
} | ||
return host === referrerOrigin; | ||
}) | ||
) { | ||
next(); | ||
return; | ||
} | ||
res.status(403).send('Invalid Referer'); | ||
}; | ||
|
||
exports.refererChecker = refererChecker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.