redirect-ssl
Advanced tools
Comparing version 0.0.2 to 0.1.0
24
index.js
@@ -10,2 +10,6 @@ | ||
function isEmpty(v) { | ||
return v === undefined || v === null | ||
} | ||
// Creates new middleware using provided options | ||
@@ -18,9 +22,17 @@ function create(options) { | ||
return function redirectSSL(req, res, next) { | ||
const _xForwardedProto = xForwardedProto && req.headers['x-forwarded-proto'] | ||
const _isEncrypted = req.connection.encrypted | ||
const _protocol = req.protocol | ||
// We check against 'http' as some checks may be unavailable | ||
if (_isEncrypted && _xForwardedProto !== 'http' && _protocol !== 'http') { | ||
return 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() | ||
} | ||
@@ -27,0 +39,0 @@ |
{ | ||
"name": "redirect-ssl", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Connect middleware to enforce https", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,2 +7,10 @@ # 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 | ||
@@ -9,0 +17,0 @@ Install package |
8647
49
60