lemonldap-ng-handler 0.10.0 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/lib/canonicalUri.d.mts +42 -0
- package/lib/canonicalUri.d.ts +42 -0
- package/lib/handlerMain.d.mts +1 -1
- package/lib/handlerMain.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +270 -55
- package/lib/index.mjs.map +1 -1
- package/package.json +3 -4
package/lib/index.mjs
CHANGED
|
@@ -6,7 +6,6 @@ import vm from 'vm';
|
|
|
6
6
|
import crypto from 'crypto';
|
|
7
7
|
import RE2 from 're2';
|
|
8
8
|
import http from 'http';
|
|
9
|
-
import normalizeUrl from 'normalize-url';
|
|
10
9
|
import { getAccessTokenSessionId } from '@lemonldap-ng/jwt';
|
|
11
10
|
|
|
12
11
|
/******************************************************************************
|
|
@@ -176,7 +175,11 @@ class HandlerInit {
|
|
|
176
175
|
if (this.localConf.nodeVhosts === undefined)
|
|
177
176
|
// istanbul ignore next
|
|
178
177
|
throw new Error("No Virtualhosts configured for Node.js");
|
|
179
|
-
|
|
178
|
+
// Virtual host names are case-insensitive: keep them lower case, as the
|
|
179
|
+
// Host header is normalized the same way in resolveAlias()
|
|
180
|
+
this.vhostList = this.localConf.nodeVhosts
|
|
181
|
+
.split(/[,\s]+/)
|
|
182
|
+
.map((vhost) => vhost.toLowerCase());
|
|
180
183
|
}
|
|
181
184
|
reload() {
|
|
182
185
|
return new Promise((resolve, reject) => {
|
|
@@ -212,7 +215,7 @@ class HandlerInit {
|
|
|
212
215
|
const val = conf.vhostOptions[vhost][name];
|
|
213
216
|
// @ts-ignore; k is in TSV type
|
|
214
217
|
if (val > 0)
|
|
215
|
-
this.tsv[k][vhost] = val;
|
|
218
|
+
this.tsv[k][vhost.toLowerCase()] = val;
|
|
216
219
|
});
|
|
217
220
|
}
|
|
218
221
|
}
|
|
@@ -271,40 +274,49 @@ class HandlerInit {
|
|
|
271
274
|
* Location rules initialization
|
|
272
275
|
*/
|
|
273
276
|
Object.keys(conf.locationRules).forEach((vhost) => {
|
|
274
|
-
|
|
277
|
+
// Configuration keys are matched case-insensitively too
|
|
278
|
+
const name = vhost.toLowerCase();
|
|
279
|
+
if (this.vhostList.indexOf(name) == -1)
|
|
275
280
|
return;
|
|
276
|
-
this.tsv.locationCount[
|
|
281
|
+
this.tsv.locationCount[name] = 0;
|
|
277
282
|
[
|
|
278
283
|
"locationRegexp",
|
|
279
284
|
"locationProtection",
|
|
280
285
|
"locationCondition",
|
|
281
286
|
].forEach((k) => {
|
|
282
287
|
// @ts-ignore: fields declared in TSV type
|
|
283
|
-
if (this.tsv[k][
|
|
284
|
-
this.tsv[k][
|
|
288
|
+
if (this.tsv[k][name] === undefined)
|
|
289
|
+
this.tsv[k][name] = [];
|
|
285
290
|
});
|
|
286
|
-
if (!this.safe[
|
|
287
|
-
this.safe[
|
|
291
|
+
if (!this.safe[name])
|
|
292
|
+
this.safe[name] = new SafeLib(conf);
|
|
288
293
|
// @ts-ignore conf.locationRules[vhost] exists
|
|
289
294
|
const rules = conf.locationRules[vhost];
|
|
290
295
|
Object.keys(rules).forEach((url) => {
|
|
291
|
-
const [cond, prot] = this.conditionSub(rules[url], this.safe[
|
|
296
|
+
const [cond, prot] = this.conditionSub(rules[url], this.safe[name]);
|
|
292
297
|
if (url === "default") {
|
|
293
|
-
this.tsv.defaultCondition[
|
|
294
|
-
this.tsv.defaultProtection[
|
|
298
|
+
this.tsv.defaultCondition[name] = cond;
|
|
299
|
+
this.tsv.defaultProtection[name] = prot;
|
|
295
300
|
}
|
|
296
301
|
else {
|
|
297
|
-
this.tsv.locationCondition[
|
|
298
|
-
this.tsv.locationProtection[
|
|
299
|
-
this.tsv.locationRegexp[
|
|
300
|
-
|
|
302
|
+
this.tsv.locationCondition[name].push(cond);
|
|
303
|
+
this.tsv.locationProtection[name].push(prot);
|
|
304
|
+
this.tsv.locationRegexp[name].push(
|
|
305
|
+
// Rules are matched case-insensitively: a Node.js
|
|
306
|
+
// application often runs on a case-insensitive file
|
|
307
|
+
// system (macOS, Windows) or router, so "/ADMIN/SECRET"
|
|
308
|
+
// can serve the resource a "^/admin/secret" rule denies.
|
|
309
|
+
// A rule can opt out with "(?-i:...)", which RE2 honours
|
|
310
|
+
// inside a case-insensitive expression
|
|
311
|
+
new RE2(url.replace(/\(\?#.*?\)/g, ""), "i"));
|
|
312
|
+
this.tsv.locationCount[name]++;
|
|
301
313
|
}
|
|
302
314
|
});
|
|
303
|
-
if (!this.tsv.defaultCondition[
|
|
315
|
+
if (!this.tsv.defaultCondition[name]) {
|
|
304
316
|
// istanbul ignore next
|
|
305
|
-
this.tsv.defaultCondition[
|
|
317
|
+
this.tsv.defaultCondition[name] = () => true;
|
|
306
318
|
// istanbul ignore next
|
|
307
|
-
this.tsv.defaultProtection[
|
|
319
|
+
this.tsv.defaultProtection[name] = 0;
|
|
308
320
|
}
|
|
309
321
|
});
|
|
310
322
|
/**
|
|
@@ -312,23 +324,24 @@ class HandlerInit {
|
|
|
312
324
|
*/
|
|
313
325
|
if (conf.exportedHeaders !== undefined) {
|
|
314
326
|
Object.keys(conf.exportedHeaders).forEach((vhost) => {
|
|
327
|
+
const name = vhost.toLowerCase();
|
|
315
328
|
// @ts-ignore
|
|
316
329
|
const headers = (
|
|
317
330
|
// @ts-ignore: conf.exportedHeaders[vhost] is defined
|
|
318
331
|
conf.exportedHeaders[vhost]);
|
|
319
|
-
if (this.vhostList.indexOf(
|
|
332
|
+
if (this.vhostList.indexOf(name) == -1)
|
|
320
333
|
return;
|
|
321
|
-
if (!this.tsv.headerList[
|
|
322
|
-
this.tsv.headerList[
|
|
334
|
+
if (!this.tsv.headerList[name])
|
|
335
|
+
this.tsv.headerList[name] = [];
|
|
323
336
|
let sub = "";
|
|
324
|
-
Object.keys(headers).forEach((
|
|
325
|
-
this.tsv.headerList[
|
|
326
|
-
sub += `'${
|
|
337
|
+
Object.keys(headers).forEach((header) => {
|
|
338
|
+
this.tsv.headerList[name].push(header);
|
|
339
|
+
sub += `'${header}': ${this.substitute(headers[header])},`;
|
|
327
340
|
});
|
|
328
341
|
sub = sub.replace(/,$/, "");
|
|
329
|
-
vm.runInContext(`fg = function(session) {return {${sub}};}`, this.safe[
|
|
342
|
+
vm.runInContext(`fg = function(session) {return {${sub}};}`, this.safe[name]);
|
|
330
343
|
// @ts-ignore: fg is now defined
|
|
331
|
-
this.tsv.forgeHeaders[
|
|
344
|
+
this.tsv.forgeHeaders[name] = this.safe[name].fg;
|
|
332
345
|
});
|
|
333
346
|
}
|
|
334
347
|
// TODO: post url initialization
|
|
@@ -341,7 +354,8 @@ class HandlerInit {
|
|
|
341
354
|
if (conf.vhostOptions[vhost].aliases) {
|
|
342
355
|
// @ts-ignore: conf.vhostOptions[vhost] is defined
|
|
343
356
|
conf.vhostOptions[vhost].aliases.forEach((alias) => {
|
|
344
|
-
this.tsv.vhostAlias[alias] =
|
|
357
|
+
this.tsv.vhostAlias[alias.toLowerCase()] =
|
|
358
|
+
vhost.toLowerCase();
|
|
345
359
|
});
|
|
346
360
|
}
|
|
347
361
|
});
|
|
@@ -588,10 +602,175 @@ class HandlerInit {
|
|
|
588
602
|
}
|
|
589
603
|
}
|
|
590
604
|
|
|
605
|
+
/**
|
|
606
|
+
* Canonical form of an URI, as web servers do before routing requests to
|
|
607
|
+
* applications: the path is percent-decoded and normalized (duplicate slashes
|
|
608
|
+
* removed). The query string is left untouched since some rules contain GET
|
|
609
|
+
* parameters.
|
|
610
|
+
*
|
|
611
|
+
* This is a port of Lemonldap::NG::Handler::Main::canonicalUri(), with
|
|
612
|
+
* intentional differences for Node.js:
|
|
613
|
+
*
|
|
614
|
+
* - the path is decoded until it is stable, because an application or its
|
|
615
|
+
* framework may decode it more than once ("%252f");
|
|
616
|
+
* - a path containing "." or ".." segments, in its raw or decoded form, is
|
|
617
|
+
* rejected: Node.js routers (Express, Fastify, ...) do not normalize dot
|
|
618
|
+
* segments, so removing them here would make the handler decide on a path
|
|
619
|
+
* the application does not serve ("/admin/..%2Fpublic" would be seen as
|
|
620
|
+
* "/public" by the handler but routed under "/admin" by Express).
|
|
621
|
+
*
|
|
622
|
+
* Location rules must be matched against the path the protected application
|
|
623
|
+
* will serve, otherwise they can be bypassed with an encoded URL: the
|
|
624
|
+
* canonicalization used for CVE-2020-24660 did not decode "%2f", so a deny
|
|
625
|
+
* rule on "/admin/secret" missed "/admin%2fsecret".
|
|
626
|
+
*/
|
|
627
|
+
const PERCENT_ENCODED = /(?:%[0-9A-Fa-f]{2})+/g;
|
|
628
|
+
/**
|
|
629
|
+
* Number of decoding passes: unlike a web server, an application (or the
|
|
630
|
+
* framework it runs on) may decode the path again, so "%252f" can be seen as
|
|
631
|
+
* "/" by the protected resource. Decoding stops as soon as nothing changes, so
|
|
632
|
+
* this bound only applies to deeply nested encodings. Each pass shortens the
|
|
633
|
+
* path, so the work stays linear in the length of the request URI.
|
|
634
|
+
*/
|
|
635
|
+
const MAX_DECODE_PASSES = 5;
|
|
636
|
+
/**
|
|
637
|
+
* A "." or ".." path segment, whatever surrounds it.
|
|
638
|
+
*/
|
|
639
|
+
const DOT_SEGMENT = /(?:^|\/)\.{1,2}(?:\/|$)/;
|
|
640
|
+
/**
|
|
641
|
+
* Raised when the request URI can not be matched safely against location rules.
|
|
642
|
+
*/
|
|
643
|
+
class InvalidUriError extends Error {
|
|
644
|
+
constructor(uri) {
|
|
645
|
+
super("Invalid request URI");
|
|
646
|
+
this.uri = uri;
|
|
647
|
+
this.name = "InvalidUriError";
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function assertNoDotSegment(path) {
|
|
651
|
+
if (DOT_SEGMENT.test(path))
|
|
652
|
+
throw new InvalidUriError(path);
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Percent-decode a path like Perl's uri_unescape(): valid escape sequences are
|
|
656
|
+
* decoded, invalid ones are kept as is and "+" is left untouched.
|
|
657
|
+
*/
|
|
658
|
+
function unescapeOnce(path) {
|
|
659
|
+
return path.replace(PERCENT_ENCODED, (encoded) => {
|
|
660
|
+
try {
|
|
661
|
+
return decodeURIComponent(encoded);
|
|
662
|
+
}
|
|
663
|
+
catch (_e) {
|
|
664
|
+
// Not valid UTF-8: decode bytes one by one (uri_unescape() returns raw
|
|
665
|
+
// bytes) so that the escapes do not survive decoding
|
|
666
|
+
return encoded.replace(/%([0-9A-Fa-f]{2})/g, (_match, hex) => String.fromCharCode(parseInt(hex, 16)));
|
|
667
|
+
}
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Decode the path until it is stable. A dot segment produced by decoding is
|
|
672
|
+
* rejected: the application may see it even if the raw URI had none.
|
|
673
|
+
*/
|
|
674
|
+
function unescapePath(path) {
|
|
675
|
+
for (let i = 0; i < MAX_DECODE_PASSES; i++) {
|
|
676
|
+
const decoded = unescapeOnce(path);
|
|
677
|
+
if (decoded === path)
|
|
678
|
+
break;
|
|
679
|
+
assertNoDotSegment(decoded);
|
|
680
|
+
path = decoded;
|
|
681
|
+
}
|
|
682
|
+
return path;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Request URI as sent by the client, restricted to its path and query: it
|
|
686
|
+
* identifies the resource for redirections, unlike the canonical URI that is
|
|
687
|
+
* only meant to be matched against rules.
|
|
688
|
+
*/
|
|
689
|
+
function rawRequestUri(uri) {
|
|
690
|
+
if (!uri)
|
|
691
|
+
return "/";
|
|
692
|
+
// Absolute-URI form (RFC 7230 5.3.2): keep only the path and the query
|
|
693
|
+
const path = uri.replace(/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^/?#]*/, "");
|
|
694
|
+
return path.startsWith("/") ? path : "/";
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* @param uri URI to canonicalize
|
|
698
|
+
* @returns canonical URI
|
|
699
|
+
* @throws InvalidUriError if the URI can not be matched safely
|
|
700
|
+
*/
|
|
701
|
+
function canonicalUri(uri) {
|
|
702
|
+
if (uri === undefined || uri === null)
|
|
703
|
+
return "/";
|
|
704
|
+
// Absolute-URI form (RFC 7230 5.3.2): applications only see the path
|
|
705
|
+
let path = uri.replace(/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^/?]*\/?/, "/");
|
|
706
|
+
// An empty path is "/" (RFC 3986 6.2.3)
|
|
707
|
+
if (/^(?:\?|$)/.test(path))
|
|
708
|
+
path = "/" + path;
|
|
709
|
+
let query;
|
|
710
|
+
const questionMark = path.indexOf("?");
|
|
711
|
+
if (questionMark >= 0) {
|
|
712
|
+
query = path.slice(questionMark + 1);
|
|
713
|
+
path = path.slice(0, questionMark);
|
|
714
|
+
}
|
|
715
|
+
// Only absolute paths are normalized, others (OPTIONS *, ...) are kept as is
|
|
716
|
+
if (path.startsWith("/")) {
|
|
717
|
+
assertNoDotSegment(path);
|
|
718
|
+
path = unescapePath(path);
|
|
719
|
+
// A "?" here can only come from a decoded "%3f" (the query string was
|
|
720
|
+
// split off the raw URI): keep it encoded, so that rule matching can not
|
|
721
|
+
// mistake it for the query separator
|
|
722
|
+
path = path.replace(/\?/g, "%3F");
|
|
723
|
+
// A path ending with "/" keeps its trailing slash
|
|
724
|
+
const trailingSlash = /\/$/.test(path);
|
|
725
|
+
const segments = path.split(/\/+/).filter((segment) => segment !== "");
|
|
726
|
+
path = "/" + segments.join("/");
|
|
727
|
+
if (trailingSlash && path !== "/")
|
|
728
|
+
path += "/";
|
|
729
|
+
}
|
|
730
|
+
return query === undefined ? path : `${path}?${query}`;
|
|
731
|
+
}
|
|
732
|
+
|
|
591
733
|
/**
|
|
592
734
|
* Default internal cache timeout in seconds
|
|
593
735
|
*/
|
|
594
736
|
const DEFAULT_INTERNAL_CACHE_TIMEOUT = 15;
|
|
737
|
+
/**
|
|
738
|
+
* A Host header is a hostname (or an IP address) with an optional port. Any
|
|
739
|
+
* other value is refused: a Host header containing a path or a query would
|
|
740
|
+
* end up in the URLs built for redirections. Underscores are accepted because
|
|
741
|
+
* Docker Compose and some internal DNS use them.
|
|
742
|
+
*/
|
|
743
|
+
const VALID_HOST = /^(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9_](?:[A-Za-z0-9_-]*[A-Za-z0-9_])?(?:\.[A-Za-z0-9_](?:[A-Za-z0-9_-]*[A-Za-z0-9_])?)*\.?)(?::[0-9]{1,5})?$/;
|
|
744
|
+
function isValidHost(host) {
|
|
745
|
+
return VALID_HOST.test(host);
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* A malformed request must not be redirected to the portal: answer with a
|
|
749
|
+
* plain 400.
|
|
750
|
+
*/
|
|
751
|
+
function sendBadRequest(res) {
|
|
752
|
+
res.writeHead(400, "Bad request");
|
|
753
|
+
res.end();
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Location rules are matched against the canonical URI. Rules can contain GET
|
|
757
|
+
* parameters, so the whole URI is tested first, but the path alone is tested
|
|
758
|
+
* too: an anchored rule such as "^/admin$" must still apply to "/admin?x=1",
|
|
759
|
+
* as it did when only the pathname was compared.
|
|
760
|
+
*/
|
|
761
|
+
function ruleMatches(regex, uri) {
|
|
762
|
+
if (regex.test(uri))
|
|
763
|
+
return true;
|
|
764
|
+
const questionMark = uri.indexOf("?");
|
|
765
|
+
return questionMark > 0 && regex.test(uri.slice(0, questionMark));
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* Escape a user-controlled value before writing it in a log line: a decoded
|
|
769
|
+
* URI can contain CR/LF and forge fake log entries.
|
|
770
|
+
*/
|
|
771
|
+
function logValue(value) {
|
|
772
|
+
return JSON.stringify(value);
|
|
773
|
+
}
|
|
595
774
|
class LemonldapNGHandler extends HandlerInit {
|
|
596
775
|
constructor() {
|
|
597
776
|
super(...arguments);
|
|
@@ -607,16 +786,33 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
607
786
|
* run(), the access control function
|
|
608
787
|
*/
|
|
609
788
|
run(req, res, next) {
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
789
|
+
var _a;
|
|
790
|
+
/* 1 - get data */
|
|
791
|
+
const host = req.headers.host;
|
|
792
|
+
if (host === undefined || !isValidHost(host)) {
|
|
793
|
+
this.userLogger.error("Missing or invalid Host header");
|
|
794
|
+
sendBadRequest(res);
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
const vhost = this.resolveAlias(req);
|
|
798
|
+
const rawUri = rawRequestUri(req.url);
|
|
799
|
+
let uri;
|
|
800
|
+
try {
|
|
801
|
+
uri = canonicalUri(req.url);
|
|
802
|
+
}
|
|
803
|
+
catch (e) {
|
|
804
|
+
if (e instanceof InvalidUriError) {
|
|
805
|
+
this.userLogger.warn(`Refusing malformed URI ${logValue(e.uri)}`);
|
|
806
|
+
}
|
|
807
|
+
else {
|
|
808
|
+
// istanbul ignore next
|
|
809
|
+
this.userLogger.error(`Unable to canonicalize URI: ${logValue(String(e))}`);
|
|
810
|
+
}
|
|
811
|
+
sendBadRequest(res);
|
|
614
812
|
return;
|
|
615
813
|
}
|
|
616
|
-
/* 1 - get data */
|
|
617
|
-
const uri = new URL(normalizeUrl(vhost + req.url)).pathname || "/";
|
|
618
814
|
/* 2 - check for maintenance mode */
|
|
619
|
-
if (this.tsv.maintenance[vhost])
|
|
815
|
+
if ((_a = this.tsv.maintenance[vhost]) !== null && _a !== void 0 ? _a : this.tsv.maintenance._)
|
|
620
816
|
return this.setError(res, "/", 503, "Service Temporarily Unavailable");
|
|
621
817
|
/* 3 - check if current URI is protected */
|
|
622
818
|
const protection = this.isUnprotected(req, uri);
|
|
@@ -632,7 +828,7 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
632
828
|
Promise.resolve(this.fetchId(req))
|
|
633
829
|
.then((id) => {
|
|
634
830
|
if (id === "") {
|
|
635
|
-
return this.goToPortal(res, this.selfUri(vhost,
|
|
831
|
+
return this.goToPortal(res, this.selfUri(vhost, rawUri, host));
|
|
636
832
|
}
|
|
637
833
|
this.retrieveSession(id, req)
|
|
638
834
|
.then((session) => {
|
|
@@ -644,11 +840,11 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
644
840
|
this.sendHeaders(req, session);
|
|
645
841
|
this.hideCookie(req);
|
|
646
842
|
next();
|
|
647
|
-
this.userLogger.info(`${vhost}: user ${user} was granted access to ${uri}`);
|
|
843
|
+
this.userLogger.info(`${logValue(vhost)}: user ${user} was granted access to ${logValue(uri)}`);
|
|
648
844
|
}
|
|
649
845
|
else {
|
|
650
846
|
this.forbidden(req, res, session);
|
|
651
|
-
this.userLogger.notice(`${vhost}: user ${user} was denied access to ${uri}`);
|
|
847
|
+
this.userLogger.notice(`${logValue(vhost)}: user ${user} was denied access to ${logValue(uri)}`);
|
|
652
848
|
}
|
|
653
849
|
})
|
|
654
850
|
.catch((e) => {
|
|
@@ -658,19 +854,19 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
658
854
|
})
|
|
659
855
|
.catch((_e) => {
|
|
660
856
|
/* Expired session */
|
|
661
|
-
this.goToPortal(res, this.selfUri(vhost,
|
|
857
|
+
this.goToPortal(res, this.selfUri(vhost, rawUri, host));
|
|
662
858
|
});
|
|
663
859
|
})
|
|
664
860
|
// istanbul ignore next
|
|
665
861
|
.catch((e) => {
|
|
666
862
|
this.userLogger.error(`Unable to fetch session id: ${e}`);
|
|
667
|
-
this.goToPortal(res, this.selfUri(vhost,
|
|
863
|
+
this.goToPortal(res, this.selfUri(vhost, rawUri, host));
|
|
668
864
|
});
|
|
669
865
|
}
|
|
670
|
-
selfUri(vhost, uri) {
|
|
866
|
+
selfUri(vhost, uri, host = vhost) {
|
|
671
867
|
return ((this.tsv.https && this.tsv.https[vhost] ? "https" : "http") +
|
|
672
868
|
"://" +
|
|
673
|
-
|
|
869
|
+
host +
|
|
674
870
|
uri);
|
|
675
871
|
}
|
|
676
872
|
nginxServer(fcgiOpt) {
|
|
@@ -716,16 +912,24 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
716
912
|
if (!this.tsv.defaultCondition[vhost])
|
|
717
913
|
return false;
|
|
718
914
|
for (let i = 0; i < this.tsv.locationRegexp[vhost].length; i++) {
|
|
719
|
-
if (this.tsv.locationRegexp[vhost][i]
|
|
915
|
+
if (ruleMatches(this.tsv.locationRegexp[vhost][i], uri))
|
|
720
916
|
return this.tsv.locationProtection[vhost][i];
|
|
721
917
|
}
|
|
722
918
|
return this.tsv.defaultProtection[vhost];
|
|
723
919
|
}
|
|
724
920
|
resolveAlias(req) {
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
921
|
+
// @ts-ignore: req.headers.host has already been validated
|
|
922
|
+
const host = req.headers.host;
|
|
923
|
+
if (!host)
|
|
924
|
+
return "undef";
|
|
925
|
+
// Host names are case-insensitive and may carry a port, IPv6 brackets and
|
|
926
|
+
// a trailing dot: "TEST1.EXAMPLE.COM.:443" designates "test1.example.com"
|
|
927
|
+
const vhost = host
|
|
928
|
+
.replace(/:\d+$/, "")
|
|
929
|
+
.replace(/^\[|\]$/g, "")
|
|
930
|
+
.replace(/\.$/, "")
|
|
931
|
+
.toLowerCase();
|
|
932
|
+
return this.tsv.vhostAlias[vhost] || vhost;
|
|
729
933
|
}
|
|
730
934
|
fetchId(req) {
|
|
731
935
|
if (req.headers.cookie) {
|
|
@@ -803,20 +1007,25 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
803
1007
|
grant(req, uri, session) {
|
|
804
1008
|
return new Promise((resolve, reject) => {
|
|
805
1009
|
const vhost = this.resolveAlias(req);
|
|
1010
|
+
const unknown = `No configuration found for ${vhost} (or not listed in Node.js virtualHosts)`;
|
|
806
1011
|
if (!Object.prototype.hasOwnProperty.call(this.tsv.defaultCondition, vhost)) {
|
|
807
|
-
return reject(
|
|
1012
|
+
return reject(unknown);
|
|
1013
|
+
}
|
|
1014
|
+
const defaultCondition = this.tsv.defaultCondition[vhost];
|
|
1015
|
+
if (typeof defaultCondition !== "function") {
|
|
1016
|
+
return reject(unknown);
|
|
808
1017
|
}
|
|
809
1018
|
if (this.tsv.locationRegexp[vhost]) {
|
|
810
1019
|
for (let i = 0; i < this.tsv.locationRegexp[vhost].length; i++) {
|
|
811
1020
|
const regex = this.tsv.locationRegexp[vhost][i];
|
|
812
|
-
if (regex
|
|
1021
|
+
if (ruleMatches(regex, uri)) {
|
|
813
1022
|
return resolve(this.tsv.locationCondition[vhost][i](req, session));
|
|
814
1023
|
}
|
|
815
1024
|
}
|
|
816
1025
|
}
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
1026
|
+
// A refusal is not an error: like a location rule, resolve(false) lets
|
|
1027
|
+
// run() answer 403 instead of 503
|
|
1028
|
+
resolve(!!defaultCondition(req, session));
|
|
820
1029
|
});
|
|
821
1030
|
}
|
|
822
1031
|
sendHeaders(req, session) {
|
|
@@ -869,8 +1078,10 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
869
1078
|
}
|
|
870
1079
|
const urlc = this.tsv.portal() +
|
|
871
1080
|
(uri
|
|
872
|
-
? //
|
|
873
|
-
|
|
1081
|
+
? // As the Perl handler does, the URL is base64-encoded and then
|
|
1082
|
+
// percent-escaped (+ / = in the base64 alphabet); encoding the URL
|
|
1083
|
+
// itself would alter the resource the user is sent back to
|
|
1084
|
+
"?url=" + encodeURIComponent(Buffer.from(uri).toString("base64"))
|
|
874
1085
|
: "") +
|
|
875
1086
|
(args ? (uri ? "&" : "?") + args : "");
|
|
876
1087
|
// req.redirect is defined when running under express. If not
|
|
@@ -940,6 +1151,8 @@ class LemonldapNGHandlerDevOps extends LemonldapNGHandler {
|
|
|
940
1151
|
return new Promise((resolve, reject) => {
|
|
941
1152
|
if (!lvOpts.lb)
|
|
942
1153
|
vhost = lvOpts.host;
|
|
1154
|
+
// Virtual host names are case-insensitive, like the Host header
|
|
1155
|
+
vhost = vhost.toLowerCase();
|
|
943
1156
|
delete lvOpts.lb;
|
|
944
1157
|
lvOpts.headers = { Host: vhost };
|
|
945
1158
|
const http = require(lvOpts.prot);
|
|
@@ -980,7 +1193,9 @@ class LemonldapNGHandlerDevOps extends LemonldapNGHandler {
|
|
|
980
1193
|
else {
|
|
981
1194
|
this.tsv.locationCondition[vhost].push(cond);
|
|
982
1195
|
this.tsv.locationProtection[vhost].push(prot);
|
|
983
|
-
this.tsv.locationRegexp[vhost].push(
|
|
1196
|
+
this.tsv.locationRegexp[vhost].push(
|
|
1197
|
+
// See init.ts: rules are matched case-insensitively
|
|
1198
|
+
new RE2(url.replace(/\(\?#.*?\)/g, ""), "i"));
|
|
984
1199
|
this.tsv.locationCount[vhost]++;
|
|
985
1200
|
}
|
|
986
1201
|
});
|