-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'amstel-gold-race-2.1.x' into feature/content-security-p…
…olicy
- Loading branch information
Showing
30 changed files
with
945 additions
and
305 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
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
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,35 @@ | ||
'use strict'; | ||
|
||
const oneDay = 86400 | ||
, isDefined = require('../utils').isDefined; | ||
|
||
module.exports = (config, res) => { | ||
const hstsOptions = (isDefined(config.security) && isDefined(config.security.hsts)) ? config.security.hsts : {} | ||
, maxAge = (isDefined(hstsOptions.maxAge)) ? hstsOptions.maxAge : oneDay; | ||
|
||
let header = ''; | ||
|
||
if(hstsOptions !== false) { | ||
if(maxAge < 0) { | ||
throw new Error('maxAge must be a positive number of seconds for the HSTS header'); | ||
} | ||
|
||
if(typeof maxAge !== 'number'){ | ||
throw new Error('maxAge must be a number for the HSTS header'); | ||
} | ||
|
||
header = 'max-age=' + maxAge; | ||
|
||
if(hstsOptions.includeSubDomains !== false) { | ||
header += '; includeSubdomains'; | ||
} | ||
|
||
if(hstsOptions.preload !== false) { | ||
header += '; preload'; | ||
} | ||
|
||
res.setHeader('Strict-Transport-Security', header); | ||
} | ||
|
||
return res; | ||
}; |
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,128 @@ | ||
'use strict'; | ||
|
||
const assert = require('chai').assert | ||
, hsts = require('./hsts'); | ||
|
||
let res = {} | ||
, config = {}; | ||
|
||
describe('Security Headers: Strict-Transport-Security Tests', () => { | ||
beforeEach(() => { | ||
res.headers = {}; | ||
res.setHeader = function (key, value) { | ||
this.headers[key] = value; | ||
}; | ||
|
||
config.security = {}; | ||
}); | ||
|
||
it('should return a function', () => { | ||
assert.isFunction(hsts); | ||
}); | ||
|
||
it('should set a header if there is no option in config', () => { | ||
hsts(config, res); | ||
|
||
assert.strictEqual(res.headers['Strict-Transport-Security'], 'max-age=86400; includeSubdomains; preload'); | ||
}); | ||
|
||
it('should set a header if the config is true', () => { | ||
config.security.hsts = true; | ||
hsts(config, res); | ||
|
||
assert.strictEqual(res.headers['Strict-Transport-Security'], 'max-age=86400; includeSubdomains; preload'); | ||
}); | ||
|
||
it('should set a header with a different max age if passed in', () => { | ||
config.security.hsts = { | ||
maxAge: 100 | ||
}; | ||
hsts(config, res); | ||
|
||
assert.strictEqual(res.headers['Strict-Transport-Security'], 'max-age=100; includeSubdomains; preload'); | ||
}); | ||
|
||
it('should set a header with a different max age and no includeSubdomains if passed in', () => { | ||
config.security.hsts = { | ||
maxAge: 100 | ||
, includeSubDomains: false | ||
}; | ||
hsts(config, res); | ||
|
||
assert.strictEqual(res.headers['Strict-Transport-Security'], 'max-age=100; preload'); | ||
}); | ||
|
||
it('should set a header with no includeSubdomains if passed in', () => { | ||
config.security.hsts = { | ||
includeSubDomains: false | ||
}; | ||
hsts(config, res); | ||
|
||
assert.strictEqual(res.headers['Strict-Transport-Security'], 'max-age=86400; preload'); | ||
}); | ||
|
||
it('should set a header with no preload if passed in', () => { | ||
config.security.hsts = { | ||
preload: false | ||
}; | ||
hsts(config, res); | ||
|
||
assert.strictEqual(res.headers['Strict-Transport-Security'], 'max-age=86400; includeSubdomains'); | ||
}); | ||
|
||
it('should set a header with a different max age and no preloads if passed in', () => { | ||
config.security.hsts = { | ||
maxAge: 100 | ||
, preload: false | ||
}; | ||
hsts(config, res); | ||
|
||
assert.strictEqual(res.headers['Strict-Transport-Security'], 'max-age=100; includeSubdomains'); | ||
}); | ||
|
||
it('should set a header with a different max age and no includeSubdomains or preload if passed in', () => { | ||
config.security.hsts = { | ||
maxAge: 100 | ||
, includeSubDomains: false | ||
, preload: false | ||
}; | ||
hsts(config, res); | ||
|
||
assert.strictEqual(res.headers['Strict-Transport-Security'], 'max-age=100'); | ||
}); | ||
|
||
it('should set a header with a default max age and no includeSubdomains or preload if passed in', () => { | ||
config.security.hsts = { | ||
includeSubDomains: false | ||
, preload: false | ||
}; | ||
hsts(config, res); | ||
|
||
assert.strictEqual(res.headers['Strict-Transport-Security'], 'max-age=86400'); | ||
}); | ||
|
||
it('should not set a header if the config is false', () => { | ||
config.security.hsts = false; | ||
hsts(config, res); | ||
|
||
assert.isUndefined(res.headers['Strict-Transport-Security']); | ||
}); | ||
|
||
it('should throw an error if a bad value for maxAge is passed in', () => { | ||
config.security.hsts = { | ||
maxAge: -1000 | ||
}; | ||
|
||
assert.throws(() => {hsts(config, res);}); | ||
|
||
config.security.hsts = { | ||
maxAge: 'Sam I am' | ||
}; | ||
|
||
assert.throws(() => {hsts(config, res);}); | ||
}); | ||
|
||
it('should return res when executed', () => { | ||
assert.strictEqual(res, hsts(config, res)); | ||
}); | ||
}); |
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.