-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from effrenus/master
Support for Referrer-Policy header
- Loading branch information
Showing
4 changed files
with
103 additions
and
1 deletion.
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,40 @@ | ||
'use strict'; | ||
|
||
|
||
/** | ||
* See https://www.w3.org/TR/referrer-policy/#referrer-policies | ||
* @type {Array} | ||
*/ | ||
var supportedValues = [ | ||
'', | ||
'no-referrer', | ||
'no-referrer-when-downgrade', | ||
'same-origin', | ||
'origin', | ||
'strict-origin', | ||
'origin-when-cross-origin', | ||
'strict-origin-when-cross-origin', | ||
'unsafe-url' | ||
]; | ||
|
||
/** | ||
* Default value. | ||
* @type {String} | ||
*/ | ||
var defaultValue = ''; // Browser should fallback to a Referrer Policy defined via other mechanisms elsewhere | ||
|
||
/** | ||
* Referrer-Policy | ||
* https://scotthelme.co.uk/a-new-security-header-referrer-policy/ | ||
* Specification: https://www.w3.org/TR/referrer-policy/ | ||
* @param {String} value The Referrer-Policy header value, e.g. no-referrer, same-origin, origin. | ||
*/ | ||
module.exports = function (value) { | ||
if (supportedValues.indexOf(value) === -1 && process.env.NODE_ENV !== 'production') { | ||
throw Error('Referrer-Policy header doesn\'t support value: ' + value); | ||
} | ||
return function referrerpolicy(req, res, next) { | ||
res.header('referrer-policy', value || defaultValue); | ||
next(); | ||
}; | ||
}; |
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,52 @@ | ||
/*global describe:false, it:false */ | ||
'use strict'; | ||
|
||
|
||
var lusca = require('../index'), | ||
request = require('supertest'), | ||
assert = require('assert'), | ||
mock = require('./mocks/app'); | ||
|
||
|
||
|
||
describe('referrerPolicy', function () { | ||
|
||
it('method', function () { | ||
assert(typeof lusca.referrerPolicy === 'function'); | ||
}); | ||
|
||
it('header (enabled)', function (done) { | ||
var config = { referrerPolicy: 'no-referrer-when-downgrade' }, | ||
app = mock(config); | ||
|
||
app.get('/', function (req, res) { | ||
res.status(200).end(); | ||
}); | ||
|
||
request(app) | ||
.get('/') | ||
.expect('referrer-policy', 'no-referrer-when-downgrade') | ||
.expect(200, done); | ||
}); | ||
|
||
it('header invalid value', function () { | ||
assert.throws(function () { | ||
lusca.referrerPolicy('value-with-error'); | ||
}, /Referrer-Policy header doesn't support/); | ||
}); | ||
|
||
it('header invalid value in production doesn\'t throw error', function (done) { | ||
process.env.NODE_ENV = 'production'; | ||
var config = { referrerPolicy: 'invalid-value' }, | ||
app = mock(config); | ||
|
||
app.get('/', function (req, res) { | ||
res.status(200).end(); | ||
}); | ||
|
||
request(app) | ||
.get('/') | ||
.expect('referrer-policy', 'invalid-value') | ||
.expect(200, done); | ||
}); | ||
}); |