@miqro/core 5.1.6 → 5.1.7
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/README.md +98 -23
- package/build/app.js +14 -1
- package/build/app.js.map +1 -1
- package/build/common.js.map +1 -1
- package/build/middleware/body-parser.js +47 -10
- package/build/middleware/body-parser.js.map +1 -1
- package/build/middleware/router.d.ts +8 -0
- package/build/middleware/router.js +104 -29
- package/build/middleware/router.js.map +1 -1
- package/build/middleware/session.d.ts +5 -3
- package/build/middleware/session.js +49 -16
- package/build/middleware/session.js.map +1 -1
- package/build/types.d.ts +6 -5
- package/build/types.js +37 -7
- package/build/types.js.map +1 -1
- package/build/websocket.d.ts +55 -0
- package/build/websocket.js +253 -73
- package/build/websocket.js.map +1 -1
- package/package.json +8 -8
- package/src/app.ts +12 -0
- package/src/middleware/body-parser.ts +49 -10
- package/src/middleware/router.ts +114 -30
- package/src/middleware/session.ts +52 -17
- package/src/types.ts +43 -7
- package/src/websocket.ts +274 -69
|
@@ -110,6 +110,26 @@ export const DEFAULT_READ_BUFFER_LIMIT = 1024 * 8 * 100;
|
|
|
110
110
|
|
|
111
111
|
const DEFAULT_READ_BUFFER_TIMEOUT = 1000 * 40;
|
|
112
112
|
|
|
113
|
+
/**
|
|
114
|
+
* parseInt is wrong for a limit in two different ways, so this accepts whole digits
|
|
115
|
+
* only. A human readable "8mb" parses to 8 -- an eight *byte* limit that rejects every
|
|
116
|
+
* request -- and anything with no leading digit parses to NaN, where every comparison
|
|
117
|
+
* against it is false, so ReadBuffer buffers without a bound and the body parsers
|
|
118
|
+
* neither parse a body nor reject it. Both have to fail loudly at startup instead of
|
|
119
|
+
* silently becoming the opposite of what was configured.
|
|
120
|
+
*/
|
|
121
|
+
function parseEnvInt(name: string, value: string): number {
|
|
122
|
+
const trimmed = value.trim();
|
|
123
|
+
if (!/^[0-9]+$/.test(trimmed)) {
|
|
124
|
+
throw new Error(`env variable ${name} is not a positive integer [${value}]`);
|
|
125
|
+
}
|
|
126
|
+
const ret = Number(trimmed);
|
|
127
|
+
if (!Number.isSafeInteger(ret)) {
|
|
128
|
+
throw new Error(`env variable ${name} is too large [${value}]`);
|
|
129
|
+
}
|
|
130
|
+
return ret;
|
|
131
|
+
}
|
|
132
|
+
|
|
113
133
|
export function ReadBuffer(options?: {
|
|
114
134
|
limit?: number;
|
|
115
135
|
timeout?: number;
|
|
@@ -122,8 +142,8 @@ export function ReadBuffer(options?: {
|
|
|
122
142
|
} else {
|
|
123
143
|
const [limitS, timeoutS] =
|
|
124
144
|
checkEnvVariables(["READ_BUFFER_LIMIT", "READ_BUFFER_TIMEOUT"], [String(DEFAULT_READ_BUFFER_LIMIT), String(DEFAULT_READ_BUFFER_TIMEOUT)]);
|
|
125
|
-
limit =
|
|
126
|
-
timeout =
|
|
145
|
+
limit = parseEnvInt("READ_BUFFER_LIMIT", limitS);
|
|
146
|
+
timeout = parseEnvInt("READ_BUFFER_TIMEOUT", timeoutS);
|
|
127
147
|
}
|
|
128
148
|
return async function ReadBuffer(req: Request, res: Response): Promise<void> {
|
|
129
149
|
return new Promise<void>((resolve, reject) => {
|
|
@@ -188,6 +208,22 @@ export function ReadBuffer(options?: {
|
|
|
188
208
|
* URLEncodedParser
|
|
189
209
|
*/
|
|
190
210
|
|
|
211
|
+
/**
|
|
212
|
+
* Compares the essence of the request's content type -- the media type with its
|
|
213
|
+
* parameters and surrounding whitespace stripped, so "application/json; charset=utf-8"
|
|
214
|
+
* matches "application/json". Matching the configured type anywhere in the header
|
|
215
|
+
* instead would accept "text/plain; charset=application/json", and text/plain is CORS
|
|
216
|
+
* safelisted, so it reaches the parser without a preflight.
|
|
217
|
+
*/
|
|
218
|
+
function isContentType(req: Request, type: string): boolean {
|
|
219
|
+
const header = req.headers["content-type"];
|
|
220
|
+
if (!header) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
const essence = (value: string) => value.split(";")[0].trim().toLocaleLowerCase();
|
|
224
|
+
return essence(header) === essence(type);
|
|
225
|
+
}
|
|
226
|
+
|
|
191
227
|
const DEFAULT_FORM_MAX_KEYS: undefined | number = undefined;
|
|
192
228
|
|
|
193
229
|
export function URLEncodedParser(options?: {
|
|
@@ -205,16 +241,16 @@ export function URLEncodedParser(options?: {
|
|
|
205
241
|
} else {
|
|
206
242
|
const [limitS, typeS, maxKeysS] =
|
|
207
243
|
checkEnvVariables(["BODY_PARSER_URL_ENCODED_LIMIT", "BODY_PARSER_URL_ENCODED_TYPE", "BODY_PARSER_URL_ENCODED_MAX_KEYS"], [String(DEFAULT_READ_BUFFER_LIMIT), "application/x-www-form-urlencoded", String(DEFAULT_FORM_MAX_KEYS)]);
|
|
208
|
-
limit =
|
|
244
|
+
limit = parseEnvInt("BODY_PARSER_URL_ENCODED_LIMIT", limitS);
|
|
209
245
|
type = typeS;
|
|
210
|
-
maxKeys = maxKeysS !== "undefined" ?
|
|
246
|
+
maxKeys = maxKeysS !== "undefined" ? parseEnvInt("BODY_PARSER_URL_ENCODED_MAX_KEYS", maxKeysS) : DEFAULT_FORM_MAX_KEYS;
|
|
211
247
|
}
|
|
212
248
|
const readBuffer = ReadBuffer({
|
|
213
249
|
limit
|
|
214
250
|
});
|
|
215
251
|
return async function URLEncodedParser(req: Request, res: Response): Promise<void> {
|
|
216
252
|
try {
|
|
217
|
-
const isType = req.body === undefined && req
|
|
253
|
+
const isType = req.body === undefined && isContentType(req, type);
|
|
218
254
|
if (isType && !req.buffer) {
|
|
219
255
|
await readBuffer(req, res);
|
|
220
256
|
}
|
|
@@ -264,7 +300,7 @@ export function JSONParser(options?: {
|
|
|
264
300
|
const [limitS, strictS, typeS] =
|
|
265
301
|
checkEnvVariables(["BODY_PARSER_LIMIT", "BODY_PARSER_STRICT", "BODY_PARSER_TYPE"], [String(DEFAULT_READ_BUFFER_LIMIT), "true", "application/json"])
|
|
266
302
|
strict = strictS === "true";
|
|
267
|
-
limit =
|
|
303
|
+
limit = parseEnvInt("BODY_PARSER_LIMIT", limitS);
|
|
268
304
|
type = typeS;
|
|
269
305
|
}
|
|
270
306
|
const readBuffer = ReadBuffer({
|
|
@@ -272,7 +308,7 @@ export function JSONParser(options?: {
|
|
|
272
308
|
});
|
|
273
309
|
return async function JSONParser(req, res): Promise<void> {
|
|
274
310
|
try {
|
|
275
|
-
const isType = req.body === undefined && req
|
|
311
|
+
const isType = req.body === undefined && isContentType(req, type);
|
|
276
312
|
if (isType && !req.buffer) {
|
|
277
313
|
await readBuffer(req, res);
|
|
278
314
|
}
|
|
@@ -317,7 +353,7 @@ export function TextParser(options?: {
|
|
|
317
353
|
} else {
|
|
318
354
|
const [limitS, typeS] =
|
|
319
355
|
checkEnvVariables(["BODY_TEXT_PARSER_LIMIT", "BODY_TEXT_PARSER_TYPE"], [String(DEFAULT_READ_BUFFER_LIMIT), "text/plain"]);
|
|
320
|
-
limit =
|
|
356
|
+
limit = parseEnvInt("BODY_TEXT_PARSER_LIMIT", limitS);
|
|
321
357
|
type = typeS;
|
|
322
358
|
}
|
|
323
359
|
const readBuffer = ReadBuffer({
|
|
@@ -325,7 +361,7 @@ export function TextParser(options?: {
|
|
|
325
361
|
});
|
|
326
362
|
return async function TextParser(req, res): Promise<void> {
|
|
327
363
|
try {
|
|
328
|
-
const isType = req.body === undefined && req
|
|
364
|
+
const isType = req.body === undefined && isContentType(req, type);
|
|
329
365
|
if (isType && !req.buffer) {
|
|
330
366
|
await readBuffer(req, res);
|
|
331
367
|
}
|
|
@@ -563,7 +599,10 @@ export function ResponseHandler(options?: ResponseHandlerOptions): Handler {
|
|
|
563
599
|
const body = lastResult.body;
|
|
564
600
|
const status = lastResult.status;
|
|
565
601
|
|
|
566
|
-
|
|
602
|
+
// header names are case insensitive, so a handler returning {'content-type': 'text/csv'}
|
|
603
|
+
// used to get its body JSON.stringify'd and served as text/csv
|
|
604
|
+
const noContentType = !headers ||
|
|
605
|
+
Object.keys(headers).every(name => name.toLocaleLowerCase() !== "content-type");
|
|
567
606
|
|
|
568
607
|
if (options?.etag !== undefined) {
|
|
569
608
|
res.setETag(options?.etag);
|
package/src/middleware/router.ts
CHANGED
|
@@ -37,9 +37,9 @@ import { ServerOptions } from "https";
|
|
|
37
37
|
import { SessionHandler, GroupPolicyHandler } from "./session.js";
|
|
38
38
|
import { DEFAULT_PARSER, assertNotUndefined, ResponseHandler, ResultParser, ParseRequest } from "./body-parser.js";
|
|
39
39
|
import { RequestOptions, request } from "@miqro/request";
|
|
40
|
-
import { existsSync, lstatSync, readFileSync, readdirSync, realpathSync, statSync } from "fs";
|
|
40
|
+
import { createReadStream, existsSync, lstatSync, readFileSync, readdirSync, realpathSync, statSync } from "fs";
|
|
41
|
+
import { readFile } from "fs/promises";
|
|
41
42
|
import { basename, extname, join, parse, resolve, sep } from "path";
|
|
42
|
-
import { parseEncodeHTML } from "@miqro/parser/build/built-in-parsers.js";
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Router Public
|
|
@@ -374,7 +374,14 @@ class RouterHandler {
|
|
|
374
374
|
match: boolean;
|
|
375
375
|
params?: PathParams
|
|
376
376
|
} {
|
|
377
|
-
|
|
377
|
+
// a HEAD is a GET without the response body (RFC 9110), so a GET route has to
|
|
378
|
+
// answer it. both sides are already lowercased -- the route at registration, the
|
|
379
|
+
// request by Request.method -- so exact equality alone would 404 every uptime
|
|
380
|
+
// monitor, curl -I and CDN validation probe. node drops the body for us.
|
|
381
|
+
const methodMatch = this.method === undefined ||
|
|
382
|
+
this.method === method ||
|
|
383
|
+
(method === "head" && this.method === "get");
|
|
384
|
+
if (methodMatch) {
|
|
378
385
|
if (this.path === undefined) {
|
|
379
386
|
//console.log("isMatch(%s, %s, %s)=%s", method, this.method, this.path, true);
|
|
380
387
|
return {
|
|
@@ -410,11 +417,51 @@ class RouterHandler {
|
|
|
410
417
|
export interface ProxyRouterOptions extends RouterOptions {
|
|
411
418
|
url: string;
|
|
412
419
|
rejectUnauthorized?: boolean;
|
|
420
|
+
/**
|
|
421
|
+
* Forward the caller's Cookie and Authorization to the upstream. Off by default: the
|
|
422
|
+
* upstream is a different host than the one the caller authenticated against, so sending
|
|
423
|
+
* its credentials there is an ambient authority the caller never granted. Turn it on only
|
|
424
|
+
* when proxying to something inside the same trust boundary.
|
|
425
|
+
*/
|
|
426
|
+
forwardCredentials?: boolean;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Headers that describe the connection they arrived on, not the message (RFC 9110 7.6.1).
|
|
431
|
+
* Relaying them describes the wrong connection to the next hop, which is where request
|
|
432
|
+
* smuggling and confused length handling start.
|
|
433
|
+
*/
|
|
434
|
+
const HOP_BY_HOP_HEADERS = [
|
|
435
|
+
"connection",
|
|
436
|
+
"keep-alive",
|
|
437
|
+
"proxy-authenticate",
|
|
438
|
+
"proxy-authorization",
|
|
439
|
+
"proxy-connection",
|
|
440
|
+
"te",
|
|
441
|
+
"trailer",
|
|
442
|
+
"transfer-encoding",
|
|
443
|
+
"upgrade"
|
|
444
|
+
];
|
|
445
|
+
|
|
446
|
+
const CREDENTIAL_HEADERS = ["cookie", "authorization"];
|
|
447
|
+
|
|
448
|
+
function stripProxyHeaders(headers: { [name: string]: any }, forwardCredentials: boolean): { [name: string]: any } {
|
|
449
|
+
const ret = { ...headers };
|
|
450
|
+
for (const name of Object.keys(ret)) {
|
|
451
|
+
const lower = name.toLocaleLowerCase();
|
|
452
|
+
if (HOP_BY_HOP_HEADERS.indexOf(lower) !== -1) {
|
|
453
|
+
delete ret[name];
|
|
454
|
+
} else if (!forwardCredentials && CREDENTIAL_HEADERS.indexOf(lower) !== -1) {
|
|
455
|
+
delete ret[name];
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return ret;
|
|
413
459
|
}
|
|
414
460
|
|
|
415
461
|
class ProxyRouter extends Router {
|
|
416
462
|
private readonly proxyURL: URL;
|
|
417
463
|
private readonly rejectUnauthorized?: boolean;
|
|
464
|
+
private readonly forwardCredentials?: boolean;
|
|
418
465
|
|
|
419
466
|
constructor(options: ProxyRouterOptions) {
|
|
420
467
|
super(options);
|
|
@@ -422,6 +469,7 @@ class ProxyRouter extends Router {
|
|
|
422
469
|
proxyURL.pathname = normalizePath(proxyURL.pathname);
|
|
423
470
|
this.proxyURL = proxyURL;
|
|
424
471
|
this.rejectUnauthorized = options.rejectUnauthorized;
|
|
472
|
+
this.forwardCredentials = options.forwardCredentials;
|
|
425
473
|
}
|
|
426
474
|
|
|
427
475
|
async run(req: Request, res: Response, pathTokens?: { value: string; lower: string }[], prePathTokens: PathPartToken[] = []): Promise<boolean> {
|
|
@@ -436,12 +484,16 @@ class ProxyRouter extends Router {
|
|
|
436
484
|
nUrl.pathname = join(nUrl.pathname, relative);
|
|
437
485
|
const url = nUrl.toString();
|
|
438
486
|
|
|
439
|
-
const
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
487
|
+
const forwardCredentials = this.forwardCredentials === true;
|
|
488
|
+
const headers = stripProxyHeaders(req.headers, forwardCredentials);
|
|
489
|
+
// Request defines headers.cookie as a non enumerable accessor, so it never survives a
|
|
490
|
+
// spread. Opting in has to read it explicitly.
|
|
491
|
+
if (forwardCredentials && req.headers.cookie !== undefined) {
|
|
492
|
+
headers["cookie"] = req.headers.cookie;
|
|
493
|
+
}
|
|
494
|
+
// the caller's Host names the proxy, not the upstream. Sending it on makes the upstream
|
|
495
|
+
// generate absolute URLs and vhost decisions for a host it does not serve.
|
|
496
|
+
headers["host"] = nUrl.host;
|
|
445
497
|
|
|
446
498
|
const requestArgs: RequestOptions = {
|
|
447
499
|
url: url.toString(),
|
|
@@ -452,11 +504,8 @@ class ProxyRouter extends Router {
|
|
|
452
504
|
method: req.method,
|
|
453
505
|
headers,
|
|
454
506
|
onChunk: async function (chunk: Uint8Array, chunkReq: IncomingMessage): Promise<void> {
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
};
|
|
458
|
-
delete responseHeaders["connection"];
|
|
459
|
-
delete responseHeaders["keep-alive"];
|
|
507
|
+
// hop-by-hop headers describe the upstream connection, not this one
|
|
508
|
+
const responseHeaders = stripProxyHeaders(chunkReq.headers, true);
|
|
460
509
|
if (!res.headersSent) {
|
|
461
510
|
const headerNames = Object.keys(responseHeaders);
|
|
462
511
|
for (const name of headerNames) {
|
|
@@ -468,7 +517,9 @@ class ProxyRouter extends Router {
|
|
|
468
517
|
}
|
|
469
518
|
await res.asyncWrite(chunk);
|
|
470
519
|
},
|
|
471
|
-
|
|
520
|
+
// Request lowercases the method, so comparing against "GET" was always true and
|
|
521
|
+
// a GET was proxied with whatever body ReadBuffer had collected.
|
|
522
|
+
data: req.method !== "get" ? req.buffer : undefined
|
|
472
523
|
};
|
|
473
524
|
|
|
474
525
|
req.logger.info("proxy to %s", url.toString());
|
|
@@ -477,13 +528,17 @@ class ProxyRouter extends Router {
|
|
|
477
528
|
const response = await request(requestArgs);
|
|
478
529
|
|
|
479
530
|
if (!res.headersSent) {
|
|
480
|
-
const responseHeaders =
|
|
481
|
-
...response.headers
|
|
482
|
-
};
|
|
531
|
+
const responseHeaders = stripProxyHeaders(response.headers, true);
|
|
483
532
|
const headerNames = Object.keys(responseHeaders);
|
|
484
533
|
for (const name of headerNames) {
|
|
485
534
|
res.setHeader(name, responseHeaders[name] as any);
|
|
486
535
|
}
|
|
536
|
+
// a response with no body never reaches onChunk, which is the only other place
|
|
537
|
+
// the upstream status is copied -- so without this an upstream 204, 304 or
|
|
538
|
+
// empty 404 is relayed as a 200 to every caller and cache downstream.
|
|
539
|
+
if (response.status !== undefined) {
|
|
540
|
+
res.statusCode = response.status;
|
|
541
|
+
}
|
|
487
542
|
}
|
|
488
543
|
|
|
489
544
|
await res.end();
|
|
@@ -501,18 +556,44 @@ export function Proxy(options: ProxyRouterOptions) {
|
|
|
501
556
|
*/
|
|
502
557
|
|
|
503
558
|
async function outputFile(path: string, contentType: string, req: Request, res: Response, allowGetJSON: boolean, status = 200) {
|
|
504
|
-
const
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
}
|
|
514
|
-
|
|
559
|
+
const asJSON = allowGetJSON && req.query.format && req.query.format === "json";
|
|
560
|
+
// an ETag has to hash the whole body, and ?format=json wraps it, so both need the file in
|
|
561
|
+
// memory. Reading it asynchronously still keeps the process responsive, which readFileSync
|
|
562
|
+
// did not: it stopped every other connection and timer for the length of the read.
|
|
563
|
+
if (asJSON || res.useETag) {
|
|
564
|
+
const buffer = await readFile(path);
|
|
565
|
+
return asJSON ?
|
|
566
|
+
res.json({
|
|
567
|
+
buffer
|
|
568
|
+
}) :
|
|
569
|
+
res.asyncEnd({
|
|
570
|
+
status,
|
|
571
|
+
headers: {
|
|
572
|
+
["Content-Type"]: contentType
|
|
573
|
+
},
|
|
574
|
+
body: buffer
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
// otherwise stream it: the response starts before the file has been read and the whole
|
|
578
|
+
// file is never resident at once
|
|
579
|
+
return new Promise<void>((resolve, reject) => {
|
|
580
|
+
const stat = statSync(path);
|
|
581
|
+
res.statusCode = status;
|
|
582
|
+
res.setHeader("Content-Type", contentType);
|
|
583
|
+
res.setHeader("Content-Length", stat.size);
|
|
584
|
+
const stream = createReadStream(path);
|
|
585
|
+
stream.on("error", (e) => {
|
|
586
|
+
stream.destroy();
|
|
587
|
+
reject(e);
|
|
588
|
+
});
|
|
589
|
+
res.on("close", () => {
|
|
590
|
+
stream.destroy();
|
|
591
|
+
});
|
|
592
|
+
stream.pipe(res);
|
|
593
|
+
res.on("finish", () => {
|
|
594
|
+
resolve();
|
|
515
595
|
});
|
|
596
|
+
});
|
|
516
597
|
}
|
|
517
598
|
|
|
518
599
|
async function generateList(path: string, req: Request, res: Response, allowListJSON: boolean) {
|
|
@@ -520,7 +601,10 @@ async function generateList(path: string, req: Request, res: Response, allowList
|
|
|
520
601
|
return allowListJSON && req.query.format && req.query.format === "json" ?
|
|
521
602
|
res.json({ files: files.map(f => join(req.path, f)) }) :
|
|
522
603
|
res.html(`<html><body><ul>${files.map(f => {
|
|
523
|
-
|
|
604
|
+
// through @miqro/parser's public entry point, not a deep import into its build/:
|
|
605
|
+
// the deep path only resolved because the package has no exports map, so adding one
|
|
606
|
+
// would have broken core
|
|
607
|
+
const escaped = DEFAULT_PARSER.parse(f, { type: "encodeHTML" }) as string;
|
|
524
608
|
return `<li><a href="${join(req.path, escaped)}">${escaped}</a></li>`;
|
|
525
609
|
}).join("")}</ul></body></html>`);
|
|
526
610
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { assertNotUndefined, DEFAULT_PARSER } from "./body-parser.js";
|
|
2
2
|
import { RequestOptions, RequestResponse, request } from "@miqro/request";
|
|
3
3
|
import { Schema } from "@miqro/parser";
|
|
4
|
-
import {
|
|
4
|
+
import { parseSetCookie, SetCookie } from "cookie";
|
|
5
|
+
import { createHash } from "crypto";
|
|
5
6
|
import { BadRequestError, checkEnvVariables, ForbiddenError, UnAuthorizedError } from "../common.js";
|
|
6
7
|
import { GroupPolicy, GroupPolicySchema, Handler, NoTokenSession, Request, Response, Session, SessionHandlerOptions, SessionHandlerOptionsOptionsSchema, SessionHandlerOptionsSchema } from "../types.js";
|
|
7
8
|
|
|
@@ -10,13 +11,26 @@ const DEFAULT_TOKEN_LOCATION = "free";
|
|
|
10
11
|
const DEFAULT_TOKEN_HEADER = "Authorization";
|
|
11
12
|
const DEFAULT_TOKEN_QUERY = "token";
|
|
12
13
|
const DEFAULT_TOKEN_COOKIE = "Authorization";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A token is a bearer credential, so any line that logs one hands a working credential
|
|
17
|
+
* to whoever can read the log -- and warn passes the default LOG_LEVEL=info filter and
|
|
18
|
+
* reaches disk whenever LOG_FILE is set. The digest is still stable enough to correlate
|
|
19
|
+
* every line about the same token, without being usable as one.
|
|
20
|
+
*/
|
|
21
|
+
function maskToken(token?: string | null): string {
|
|
22
|
+
if (!token) {
|
|
23
|
+
return "<no token>";
|
|
24
|
+
}
|
|
25
|
+
return `sha256:${createHash("sha256").update(String(token)).digest("hex").substring(0, 12)}`;
|
|
26
|
+
}
|
|
13
27
|
export const DEFAULT_TOKEN_SET_COOKIE_HTTP_ONLY = "true";
|
|
14
28
|
export const DEFAULT_TOKEN_SET_COOKIE_SECURE = "true";
|
|
15
29
|
export const DEFAULT_TOKEN_SET_COOKIE_PATH = "/";
|
|
16
30
|
export const DEFAULT_TOKEN_SET_COOKIE_SAME_SITE = "strict";
|
|
17
31
|
|
|
18
32
|
export interface VerifyTokenService {
|
|
19
|
-
verify(args: { token
|
|
33
|
+
verify(args: { token?: string | null; req: Request; res?: Response }): Promise<Session | null>;
|
|
20
34
|
}
|
|
21
35
|
|
|
22
36
|
export interface VerifyEndpointServiceOptions {
|
|
@@ -84,7 +98,16 @@ export class VerifyEndpointService implements VerifyTokenService {
|
|
|
84
98
|
}
|
|
85
99
|
}
|
|
86
100
|
|
|
87
|
-
public async verify({ token, req }: { token
|
|
101
|
+
public async verify({ token, req }: { token?: string | null; req: Request; res?: Response; }): Promise<Session | null> {
|
|
102
|
+
// SessionHandler hands over whatever it found, which is nothing when TOKEN_LOCATION is
|
|
103
|
+
// "free". There is no credential to check then, so this is unauthenticated rather than
|
|
104
|
+
// an endpoint call -- which would otherwise send the literal "null" upstream as a token
|
|
105
|
+
// and treat whatever came back as a session. res is accepted and ignored: this service
|
|
106
|
+
// verifies out of band and has no response of its own to write.
|
|
107
|
+
if (!token) {
|
|
108
|
+
req.logger.warn("no token to verify");
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
88
111
|
try {
|
|
89
112
|
let response = null;
|
|
90
113
|
const tokenVerifyLocation = this.options.tokenLocation;
|
|
@@ -93,7 +116,7 @@ export class VerifyEndpointService implements VerifyTokenService {
|
|
|
93
116
|
switch (tokenVerifyLocation) {
|
|
94
117
|
case "header":
|
|
95
118
|
const tokenHeaderLocation = typeof this.options.tokenLocationName === "string" ? this.options.tokenLocationName : await this.options.tokenLocationName(req);
|
|
96
|
-
req.logger.debug(`verifying [${token}] on TOKEN_VERIFY_ENDPOINT=[${process.env.TOKEN_VERIFY_ENDPOINT}] TOKEN_HEADER=[${tokenHeaderLocation}]`);
|
|
119
|
+
req.logger.debug(`verifying [${maskToken(token)}] on TOKEN_VERIFY_ENDPOINT=[${process.env.TOKEN_VERIFY_ENDPOINT}] TOKEN_HEADER=[${tokenHeaderLocation}]`);
|
|
97
120
|
response = await this.getResponse({
|
|
98
121
|
url,
|
|
99
122
|
headers: {
|
|
@@ -104,7 +127,7 @@ export class VerifyEndpointService implements VerifyTokenService {
|
|
|
104
127
|
break;
|
|
105
128
|
case "query":
|
|
106
129
|
const tokenQueryLocation = typeof this.options.tokenLocationName === "string" ? this.options.tokenLocationName : await this.options.tokenLocationName(req);
|
|
107
|
-
req.logger.debug(`verifying [${token}] on TOKEN_VERIFY_ENDPOINT=[${process.env.TOKEN_VERIFY_ENDPOINT}] TOKEN_QUERY=[${tokenQueryLocation}]`);
|
|
130
|
+
req.logger.debug(`verifying [${maskToken(token)}] on TOKEN_VERIFY_ENDPOINT=[${process.env.TOKEN_VERIFY_ENDPOINT}] TOKEN_QUERY=[${tokenQueryLocation}]`);
|
|
108
131
|
response = await this.getResponse({
|
|
109
132
|
url,
|
|
110
133
|
query: {
|
|
@@ -115,7 +138,7 @@ export class VerifyEndpointService implements VerifyTokenService {
|
|
|
115
138
|
break;
|
|
116
139
|
case "cookie":
|
|
117
140
|
const tokenCookieLocation = typeof this.options.tokenLocationName === "string" ? this.options.tokenLocationName : await this.options.tokenLocationName(req);
|
|
118
|
-
req.logger.debug(`verifying [${token}] on TOKEN_VERIFY_ENDPOINT=[${process.env.TOKEN_VERIFY_ENDPOINT}] TOKEN_COOKIE=[${tokenCookieLocation}]`);
|
|
141
|
+
req.logger.debug(`verifying [${maskToken(token)}] on TOKEN_VERIFY_ENDPOINT=[${process.env.TOKEN_VERIFY_ENDPOINT}] TOKEN_COOKIE=[${tokenCookieLocation}]`);
|
|
119
142
|
response = await this.getResponse({
|
|
120
143
|
url,
|
|
121
144
|
method,
|
|
@@ -130,16 +153,20 @@ export class VerifyEndpointService implements VerifyTokenService {
|
|
|
130
153
|
if (response) {
|
|
131
154
|
const session = await this.decodeSession(response, token, req);
|
|
132
155
|
if (!this.checkSession(session, req)) {
|
|
133
|
-
req.logger.warn(`unauthorized token not valid [${token}]`);
|
|
156
|
+
req.logger.warn(`unauthorized token not valid [${maskToken(token)}]`);
|
|
134
157
|
return null;
|
|
135
158
|
} else {
|
|
136
|
-
req.logger.debug("token [%s]authorized!", token);
|
|
159
|
+
req.logger.debug("token [%s] authorized!", maskToken(token));
|
|
137
160
|
|
|
138
161
|
if (tokenVerifyLocation === "cookie" && response.headers["set-cookie"]) {
|
|
139
162
|
const tokenCookieLocation = typeof this.options.tokenLocationName === "string" ? this.options.tokenLocationName : await this.options.tokenLocationName(req);
|
|
140
|
-
|
|
163
|
+
// parseSetCookie reads the header as what it is, so an attribute that happens to
|
|
164
|
+
// be spelled like the token cookie (Path, Domain, ...) can no longer pass for it
|
|
165
|
+
const cookies = response.headers["set-cookie"]
|
|
166
|
+
.map((c: string) => parseSetCookie(c))
|
|
167
|
+
.filter((c: SetCookie) => c.name === tokenCookieLocation);
|
|
141
168
|
if (cookies.length === 1) {
|
|
142
|
-
const value = cookies[0]
|
|
169
|
+
const value = cookies[0].value;
|
|
143
170
|
if (value) {
|
|
144
171
|
session.token = value; // replace token because token update via set-cookie
|
|
145
172
|
}
|
|
@@ -151,11 +178,14 @@ export class VerifyEndpointService implements VerifyTokenService {
|
|
|
151
178
|
} as Session;
|
|
152
179
|
}
|
|
153
180
|
} else {
|
|
154
|
-
req.logger.warn(`unauthorized token not valid [${token}]`);
|
|
181
|
+
req.logger.warn(`unauthorized token not valid [${maskToken(token)}]`);
|
|
155
182
|
return null;
|
|
156
183
|
}
|
|
157
184
|
} catch (e: any) {
|
|
158
|
-
|
|
185
|
+
// the endpoint throws for a non 2xx, so an UnAuthorizedError is the right outcome
|
|
186
|
+
// here, but the cause was being dropped entirely -- log it before rewriting.
|
|
187
|
+
req.logger.error(e);
|
|
188
|
+
throw new UnAuthorizedError(`Fail to authenticate token! error verifying [${maskToken(token)}] [${e.status}][${e.config ? e.config.url : ""}]`);
|
|
159
189
|
}
|
|
160
190
|
}
|
|
161
191
|
|
|
@@ -250,11 +280,12 @@ export function SessionHandler(config: SessionHandlerOptions): Handler<void> {
|
|
|
250
280
|
}
|
|
251
281
|
if (!token && tokenLocation !== "free") {
|
|
252
282
|
req.logger.warn("No token provided!");
|
|
253
|
-
|
|
283
|
+
// no credential at all is "unauthenticated", not "authenticated but not allowed"
|
|
284
|
+
throw new UnAuthorizedError("NO TOKEN");
|
|
254
285
|
} else {
|
|
255
286
|
const session = await config.authService.verify({ token, req, res });
|
|
256
287
|
if (!session) {
|
|
257
|
-
req.logger.warn("fail to authenticate token [%s]!", token);
|
|
288
|
+
req.logger.warn("fail to authenticate token [%s]!", maskToken(token));
|
|
258
289
|
throw new UnAuthorizedError();
|
|
259
290
|
} else {
|
|
260
291
|
if (tokenLocation === "cookie" && session.token !== token) {
|
|
@@ -278,11 +309,15 @@ export function SessionHandler(config: SessionHandlerOptions): Handler<void> {
|
|
|
278
309
|
}
|
|
279
310
|
}
|
|
280
311
|
} catch (e: any) {
|
|
281
|
-
if (e
|
|
312
|
+
if (e && e.name === "UnAuthorizedError") {
|
|
282
313
|
throw e;
|
|
283
|
-
} else {
|
|
284
|
-
throw new UnAuthorizedError();
|
|
285
314
|
}
|
|
315
|
+
// Everything else is reported as 401 on purpose: letting a 5xx out would tell the
|
|
316
|
+
// caller their input reached something that broke, which is exactly what someone
|
|
317
|
+
// probing the auth path is looking for. Only the operator half was missing -- the
|
|
318
|
+
// cause used to be dropped entirely, so an outage was invisible from inside too.
|
|
319
|
+
req.logger.error(e);
|
|
320
|
+
throw new UnAuthorizedError();
|
|
286
321
|
}
|
|
287
322
|
};
|
|
288
323
|
};
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ParserInterface, Schema, InferSchema } from "@miqro/parser";
|
|
2
|
-
import { SerializeOptions as CookieSerializeOptions,
|
|
2
|
+
import { SerializeOptions as CookieSerializeOptions, parseCookie, stringifySetCookie } from "cookie";
|
|
3
3
|
import { createHash, randomUUID } from "node:crypto";
|
|
4
4
|
import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "node:http";
|
|
5
5
|
import { Socket } from "node:net";
|
|
@@ -576,7 +576,7 @@ export class Request extends IncomingMessage implements Request {
|
|
|
576
576
|
set: (v) => {
|
|
577
577
|
vCookie = v;
|
|
578
578
|
this.cookies = Object.create(null);
|
|
579
|
-
const cookies =
|
|
579
|
+
const cookies = parseCookie(vCookie || '');
|
|
580
580
|
const cookieList = Object.keys(cookies);
|
|
581
581
|
for (const name of cookieList) {
|
|
582
582
|
const value = cookies[name];
|
|
@@ -626,7 +626,8 @@ export class Response<Request extends IncomingMessage = IncomingMessage> extends
|
|
|
626
626
|
constructor(req: Request) {
|
|
627
627
|
super(req);
|
|
628
628
|
//this.logger = null as any;
|
|
629
|
-
|
|
629
|
+
// Date is not removed: HTTP/1.1 requires an origin server to send it, and caches need it
|
|
630
|
+
// to compute freshness. node sets it for us.
|
|
630
631
|
}
|
|
631
632
|
setETag(useETag: boolean | ((lastResult?: { headers?: any; body?: any; status?: any; }) => Promise<string | false | null | undefined> | string | false | null | undefined)) {
|
|
632
633
|
this.useETag = useETag;
|
|
@@ -681,7 +682,14 @@ export class Response<Request extends IncomingMessage = IncomingMessage> extends
|
|
|
681
682
|
});
|
|
682
683
|
}
|
|
683
684
|
public setCookie(name: string, value: string, options?: CookieSerializeOptions): ServerResponse {
|
|
684
|
-
|
|
685
|
+
// cookie 2 moved encode out of the cookie object into the second argument, and spreading
|
|
686
|
+
// it into the first one is not a type error, so it would just stop being called
|
|
687
|
+
const { encode, ...rest } = options ? options : {};
|
|
688
|
+
return this.setHeader('Set-Cookie', stringifySetCookie({
|
|
689
|
+
name,
|
|
690
|
+
value: String(value),
|
|
691
|
+
...rest
|
|
692
|
+
}, encode ? { encode } : undefined));
|
|
685
693
|
}
|
|
686
694
|
addVaryHeader(value: number | string | ReadonlyArray<string>): ServerResponse {
|
|
687
695
|
const c = this.getHeader("Vary");
|
|
@@ -776,7 +784,10 @@ export class Response<Request extends IncomingMessage = IncomingMessage> extends
|
|
|
776
784
|
}
|
|
777
785
|
}
|
|
778
786
|
}
|
|
779
|
-
|
|
787
|
+
// no unconditional Connection: close -- that turned keep-alive off for every
|
|
788
|
+
// response on the server, so each request paid a fresh TCP (and TLS) handshake.
|
|
789
|
+
// App sets the header itself while it is shutting down, which is the case that
|
|
790
|
+
// actually needs it.
|
|
780
791
|
//console.log("body = [%s][%s]", nBody, typeof nBody);
|
|
781
792
|
this.end(nBody, () => {
|
|
782
793
|
resolve();
|
|
@@ -866,7 +877,7 @@ export function matchTokenizePath(checkAsRouter: boolean, pathTokens: PathPartTo
|
|
|
866
877
|
// check pass
|
|
867
878
|
const wild = pathTokens[i].wild;
|
|
868
879
|
if (wild) {
|
|
869
|
-
params[wild] = ctxToken.value;
|
|
880
|
+
params[wild] = decodeParam(ctxToken.value);
|
|
870
881
|
}
|
|
871
882
|
continue;
|
|
872
883
|
}
|
|
@@ -937,6 +948,20 @@ export function getSlashIndex(input: string): number {
|
|
|
937
948
|
* @param path input path
|
|
938
949
|
* @returns the same path if it passed the checks
|
|
939
950
|
*/
|
|
951
|
+
/**
|
|
952
|
+
* Route params arrive raw while req.query is already decoded by URLSearchParams.
|
|
953
|
+
* Decoding here, once, keeps handlers from doing it themselves downstream -- which is
|
|
954
|
+
* the pattern that reintroduces traversal. A malformed escape is left as written
|
|
955
|
+
* rather than throwing a 500 out of routing.
|
|
956
|
+
*/
|
|
957
|
+
function decodeParam(value: string): string {
|
|
958
|
+
try {
|
|
959
|
+
return decodeURIComponent(value);
|
|
960
|
+
} catch (e) {
|
|
961
|
+
return value;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
|
|
940
965
|
export function normalizePath(path: string): string {
|
|
941
966
|
if (typeof path !== "string") {
|
|
942
967
|
throw new Error("path not string");
|
|
@@ -945,7 +970,12 @@ export function normalizePath(path: string): string {
|
|
|
945
970
|
if (path.charAt(0) !== "/") {
|
|
946
971
|
throw new Error("path doesnt start with /");
|
|
947
972
|
}
|
|
948
|
-
|
|
973
|
+
// splitPath already drops empty segments when matching a route, so //admin/secret and
|
|
974
|
+
// /admin/secret reach the same handler while req.path kept both slashes. Any middleware
|
|
975
|
+
// that compares or prefix matches req.path for an ACL was bypassable by adding a slash,
|
|
976
|
+
// so the two spellings have to collapse to one here. A trailing slash is kept, it is the
|
|
977
|
+
// only slash the router does not ignore.
|
|
978
|
+
return path.replace(/\/{2,}/g, "/");
|
|
949
979
|
} else if (path !== "/") {
|
|
950
980
|
throw new Error("path of length 1 not /");
|
|
951
981
|
} else {
|
|
@@ -976,6 +1006,12 @@ export interface WebSocketComplexValidateResult {
|
|
|
976
1006
|
export interface WebSocketServerOptions {
|
|
977
1007
|
maxConnections?: number;
|
|
978
1008
|
maxFrameSize?: number;
|
|
1009
|
+
/**
|
|
1010
|
+
* Origins allowed to open a connection, compared against the Origin header. A WebSocket
|
|
1011
|
+
* upgrade is exempt from the same origin policy and cookies are attached to it, so without
|
|
1012
|
+
* this (or a validate that checks it) any site can open an authenticated socket.
|
|
1013
|
+
*/
|
|
1014
|
+
allowedOrigins?: string[];
|
|
979
1015
|
validate?: (req: Request) => Promise<boolean | string | WebSocketComplexValidateResult> | boolean | string | WebSocketComplexValidateResult;
|
|
980
1016
|
onConnection?: (req: WebSocketClient) => void | Promise<void>;
|
|
981
1017
|
onMessage?: (req: WebSocketClient, data: string | undefined | null) => void | Promise<void>;
|