This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop support for Node <8; change maxAge: null behavior; remove deprec…
…ated options Closes #21. Closes #22. Closes #26.
- Loading branch information
Showing
6 changed files
with
213 additions
and
269 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 |
---|---|---|
@@ -1,69 +1,41 @@ | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
import depd from 'depd'; | ||
|
||
const deprecate = depd('hsts'); | ||
|
||
const DEFAULT_MAX_AGE = 180 * 24 * 60 * 60; | ||
|
||
interface HstsOptions { | ||
includeSubDomains?: boolean; | ||
maxAge?: number | null; | ||
preload?: boolean; | ||
setIf?: (req: IncomingMessage, res: ServerResponse) => boolean; | ||
} | ||
|
||
function alwaysTrue () { | ||
return true; | ||
} | ||
function getHeaderValueFromOptions(options: HstsOptions): string { | ||
const DEFAULT_MAX_AGE = 180 * 24 * 60 * 60; | ||
|
||
export = function hsts (options: HstsOptions = {}) { | ||
if ('includeSubdomains' in options) { | ||
deprecate('The "includeSubdomains" parameter is deprecated. Use "includeSubDomains" (with a capital D) instead.'); | ||
} | ||
|
||
if ('setIf' in options) { | ||
deprecate('The "setIf" parameter is deprecated. Refer to the documentation to see how to set the header conditionally.'); | ||
} | ||
|
||
if (Object.prototype.hasOwnProperty.call(options, 'maxage')) { | ||
if ('maxage' in options) { | ||
throw new Error('maxage is not a supported property. Did you mean to pass "maxAge" instead of "maxage"?'); | ||
} | ||
|
||
const maxAge = options.maxAge !== null && options.maxAge !== undefined ? options.maxAge : DEFAULT_MAX_AGE; | ||
const maxAge = 'maxAge' in options ? options.maxAge : DEFAULT_MAX_AGE; | ||
if (typeof maxAge !== 'number') { | ||
throw new TypeError('HSTS must be passed a numeric maxAge parameter.'); | ||
} | ||
if (maxAge < 0) { | ||
} else if (maxAge < 0) { | ||
throw new RangeError('HSTS maxAge must be nonnegative.'); | ||
} | ||
|
||
const { setIf = alwaysTrue } = options; | ||
if (typeof setIf !== 'function') { | ||
throw new TypeError('setIf must be a function.'); | ||
} | ||
|
||
if ( | ||
Object.prototype.hasOwnProperty.call(options, 'includeSubDomains') && | ||
Object.prototype.hasOwnProperty.call(options, 'includeSubdomains') | ||
) { | ||
throw new Error('includeSubDomains and includeSubdomains cannot both be specified.'); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const includeSubDomains = options.includeSubDomains !== false && (options as any).includeSubdomains !== false; | ||
|
||
let header = `max-age=${Math.round(maxAge)}`; | ||
if (includeSubDomains) { | ||
if (options.includeSubDomains !== false) { | ||
header += '; includeSubDomains'; | ||
} | ||
if (options.preload) { | ||
header += '; preload'; | ||
} | ||
|
||
return function hsts (req: IncomingMessage, res: ServerResponse, next: () => void) { | ||
if (setIf(req, res)) { | ||
res.setHeader('Strict-Transport-Security', header); | ||
} | ||
return header; | ||
} | ||
|
||
export = function hsts (options: HstsOptions = {}) { | ||
const headerValue = getHeaderValueFromOptions(options); | ||
|
||
return function hsts (_req: IncomingMessage, res: ServerResponse, next: () => void) { | ||
res.setHeader('Strict-Transport-Security', headerValue); | ||
next(); | ||
}; | ||
} |
Oops, something went wrong.