Skip to content

Commit f1449d8

Browse files
author
Kyle Mattimore
committed
Merge branch 'http-validation'
2 parents 83536bb + 129458a commit f1449d8

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
var anonymousAccessBlocker = require('./middleware/anonymousAccessBlocker');
44
var healthcheck = require('./middleware/healthcheck');
5+
var httpValidation = require('./middleware/httpValidation');
56
var log41n = require('./middleware/log41n');
67

78
var Log1n = function() {
89
var self = this;
910

1011
self.register = function(keystone){
12+
console.log('keystone-hosting: Adding Keystone routes');
13+
1114
keystone.pre('routes', healthcheck(keystone));
15+
keystone.pre('routes', httpValidation(keystone));
1216
keystone.pre('routes', log41n(keystone));
1317
keystone.pre('routes', anonymousAccessBlocker());
1418
};
@@ -17,4 +21,5 @@ var Log1n = function() {
1721
module.exports = new Log1n();
1822
module.exports.anonymousAccessBlocker = anonymousAccessBlocker;
1923
module.exports.healthcheck = healthcheck;
24+
module.exports.httpValidation = httpValidation;
2025
module.exports.log41n = log41n;

middleware/anonymousAccessBlocker.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ module.exports = function() {
2121
// The set of allowed ranges has to be separated by space characters, a comma, or newline.
2222
var allowedRanges = ipRanges.split(/\s+|,|\n/);
2323

24-
// Using req.ips requires that express 'trust proxy' setting is
25-
// true. When it *is* set the value for ips is extracted from the
26-
// X-Forwarded-For request header. The originating IP address is
27-
// the last one in the array.
24+
//if CLIENT_IP_ADDRESS_HEADER is set and a request coming in does not contain it, it will be denied regardless of IP
2825
var requestIP = (process.env.CLIENT_IP_ADDRESS_HEADER) ? req.header(process.env.CLIENT_IP_ADDRESS_HEADER) : req.ip;
2926
requestIP = rangeCheck.searchIP(requestIP);
3027

@@ -34,13 +31,13 @@ module.exports = function() {
3431

3532
if (requestAllowed) {
3633
// Allow the request to process
37-
console.log('Allowed IP: ' + requestIP);
34+
console.log('keystone-hosting: Allowed IP ' + requestIP);
3835
return next();
3936
}
4037
}
4138

4239
// Request is not allowed. Send the contents of the unauthorized.html file.
43-
console.log('Blocked IP: ' + requestIP);
40+
console.log('keystone-hosting: Blocked IP ' + requestIP);
4441

4542
//set 'unauthorized' response code
4643
res.status(401)

middleware/httpValidation.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = function(keystone) {
2+
3+
//register a route to a static (e.g. /.well-known/etc) response, if configured
4+
return function(req, res, next) {
5+
6+
//example from cloudflare:
7+
//"http_url": "http://http-preval.example.com/.well-known/pki-validation/ca3-0052344e54074d9693e89e27486692d6.txt",
8+
//(include leading slash) /.well-known/pki-validation/ca3-0052344e54074d9693e89e27486692d6.txt
9+
//"http_body": "ca3-be794c5f757b468eba805d1a705e44f6"
10+
//ca3-be794c5f757b468eba805d1a705e44f6
11+
12+
var enabled = process.env.HTTP_VALIDATION_LISTEN;
13+
var httpPath = process.env.HTTP_VALIDATION_PATH;
14+
var httpBody = process.env.HTTP_VALIDATION_BODY;
15+
16+
//ignore all requests except the exact match for /.well-known/etc...
17+
if(enabled !== 'true' || !httpPath || !httpBody || req.path !== httpPath) {
18+
// console.log('keystone-hosting: ignoring request' + req.path)
19+
return next();
20+
}
21+
22+
//hijack this request and simply return the desired body (a guid)
23+
console.log('keystone-hosting: HTTP Validation responding to ' + req.path)
24+
res.send(httpBody);
25+
}
26+
}

0 commit comments

Comments
 (0)