Skip to content

Instantly share code, notes, and snippets.

@vanthome
Last active December 14, 2015 09:59
Show Gist options
  • Save vanthome/5069041 to your computer and use it in GitHub Desktop.
Save vanthome/5069041 to your computer and use it in GitHub Desktop.
For nodejs: Find out whether a client connection which might be proxied by front-end apache or nginx server OR is directly terminated by node is protected by SSL / TLS
var util = require('utile'),
propUtil = require('./propUtil');
// Find out whether the client connection is encrypted
var isClientOnTls = function(req) {
var isEncrypted = true;
if(req.connection.encrypted === undefined) { // Derive from nodejs internals
isEncrypted = false;
}
var forwardedHeader = propUtil.get(req, 'headers.x-forwarded-proto');
if (forwardedHeader === 'https') { // Derive from front-end infrastructure injected header
isEncrypted = true;
} else {
isEncrypted = false;
}
return isEncrypted;
}
exports.isClientOnTls = isClientOnTls;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment