redirect-ssl
Advanced tools
Comparing version 0.1.0 to 1.0.0
31
index.js
@@ -0,36 +1,19 @@ | ||
const isHTTPS = require('is-https') | ||
// Default options | ||
const defaults = { | ||
xForwardedProto: true, | ||
redirectPort: 443, | ||
redirectHost: undefined, | ||
statusCode: 307 | ||
xForwardedProto: true, | ||
redirectPort: 443, | ||
redirectHost: undefined, | ||
statusCode: 307 | ||
} | ||
function isEmpty(v) { | ||
return v === undefined || v === null | ||
} | ||
// Creates new middleware using provided options | ||
function create(options) { | ||
const { xForwardedProto, redirectPort, redirectHost, statusCode } = Object.assign({}, defaults, options) | ||
const _port = redirectPort === 443 ? '' : (': ' + redirectPort) | ||
return function redirectSSL(req, res, next) { | ||
// Test using req.connection.encrypted | ||
const _encrypted = isEmpty(req.connection.encrypted) ? null : req.connection.encrypted === true | ||
// Test using req.protocol | ||
const _httpsProtocol = isEmpty(req.protocol) ? null : req.protocol === 'https' | ||
// Test using x-forwarded-proto header | ||
const _httpsXforwarded = (!xForwardedProto || isEmpty(req.headers['x-forwarded-proto'])) ? null | ||
: req.headers['x-forwarded-proto'].indexOf('https') !== -1 | ||
const _noDetectionMethod = _encrypted === null && _httpsProtocol === null && _httpsXforwarded === null | ||
if (_encrypted || _httpsProtocol || _httpsXforwarded || _noDetectionMethod) { | ||
return next() | ||
if (isHTTPS(req, xForwardedProto) !== false) { | ||
return next() | ||
} | ||
@@ -37,0 +20,0 @@ |
{ | ||
"name": "redirect-ssl", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Connect middleware to enforce https", | ||
@@ -12,3 +12,6 @@ "main": "index.js", | ||
"pem": "^1.9.7" | ||
}, | ||
"dependencies": { | ||
"is-https": "^1.0.0" | ||
} | ||
} |
# redirect-ssl | ||
> Connect/Express middleware to enforce https. | ||
> Connect/Express middleware to enforce https using [is-https](https://www.npmjs.com/package/is-https). | ||
@@ -7,10 +7,2 @@ [![npm](https://img.shields.io/npm/dt/redirect-ssl.svg?style=flat-square)](https://npmjs.com/package/redirect-ssl) | ||
## Behaviour | ||
This middleware tries to use 3 standard checks for HTTPS detection: | ||
- Test if `req.connection.encrypted` is `true` | ||
- Test if `req.protocol` is `https` | ||
- Test if `x-forwarded-proto` header contains `https` | ||
If all tests are unavailable, middleware just continues to avoid redirect loops. | ||
If any test fails, middleware ends reponse with a [307](#status-code) redirect to `https://[host][:port?][url]`. | ||
## Usage | ||
@@ -17,0 +9,0 @@ Install package |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12137
8
1
1
39
52
+ Addedis-https@^1.0.0
+ Addedis-https@1.0.0(transitive)