nest-graph-inspector 0.8.0-test.5 → 0.8.1-test.1
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/dist/access-attempt-limiter.d.ts +66 -0
- package/dist/access-attempt-limiter.d.ts.map +1 -0
- package/dist/access-attempt-limiter.js +153 -0
- package/dist/access-attempt-limiter.js.map +1 -0
- package/dist/access-token.service.d.ts +107 -0
- package/dist/access-token.service.d.ts.map +1 -0
- package/dist/access-token.service.js +326 -0
- package/dist/access-token.service.js.map +1 -0
- package/dist/adapters/direct-run-output.adapter.d.ts +1 -0
- package/dist/adapters/direct-run-output.adapter.d.ts.map +1 -0
- package/dist/adapters/direct-run-output.adapter.js +1 -0
- package/dist/adapters/direct-run-output.adapter.js.map +1 -0
- package/dist/adapters/discovery.d.ts +4 -0
- package/dist/adapters/discovery.d.ts.map +1 -0
- package/dist/adapters/discovery.js +9 -2
- package/dist/adapters/discovery.js.map +1 -0
- package/dist/adapters/file-output.adapter.d.ts +1 -0
- package/dist/adapters/file-output.adapter.d.ts.map +1 -0
- package/dist/adapters/file-output.adapter.js +1 -0
- package/dist/adapters/file-output.adapter.js.map +1 -0
- package/dist/adapters/http-output.adapter.d.ts +28 -1
- package/dist/adapters/http-output.adapter.d.ts.map +1 -0
- package/dist/adapters/http-output.adapter.js +29 -3
- package/dist/adapters/http-output.adapter.js.map +1 -0
- package/dist/adapters/http-serve.adapter.d.ts +20 -1
- package/dist/adapters/http-serve.adapter.d.ts.map +1 -0
- package/dist/adapters/http-serve.adapter.js +14 -1
- package/dist/adapters/http-serve.adapter.js.map +1 -0
- package/dist/adapters/json-output.adapter.d.ts +1 -0
- package/dist/adapters/json-output.adapter.d.ts.map +1 -0
- package/dist/adapters/json-output.adapter.js +1 -0
- package/dist/adapters/json-output.adapter.js.map +1 -0
- package/dist/adapters/viewer-output.adapter.d.ts +10 -5
- package/dist/adapters/viewer-output.adapter.d.ts.map +1 -0
- package/dist/adapters/viewer-output.adapter.js +30 -30
- package/dist/adapters/viewer-output.adapter.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/inspector-endpoint-info.d.ts +1 -0
- package/dist/inspector-endpoint-info.d.ts.map +1 -0
- package/dist/inspector-endpoint-info.js +12 -1
- package/dist/inspector-endpoint-info.js.map +1 -0
- package/dist/nest-graph-inspector.config.d.ts +1 -0
- package/dist/nest-graph-inspector.config.d.ts.map +1 -0
- package/dist/nest-graph-inspector.config.js +1 -0
- package/dist/nest-graph-inspector.config.js.map +1 -0
- package/dist/nest-graph-inspector.module.d.ts +1 -0
- package/dist/nest-graph-inspector.module.d.ts.map +1 -0
- package/dist/nest-graph-inspector.module.js +5 -6
- package/dist/nest-graph-inspector.module.js.map +1 -0
- package/dist/nest-graph-inspector.setup.d.ts +1 -0
- package/dist/nest-graph-inspector.setup.d.ts.map +1 -0
- package/dist/nest-graph-inspector.setup.js +12 -5
- package/dist/nest-graph-inspector.setup.js.map +1 -0
- package/dist/nest-graph-inspector.type.d.ts +133 -5
- package/dist/nest-graph-inspector.type.d.ts.map +1 -0
- package/dist/nest-graph-inspector.type.js +1 -0
- package/dist/nest-graph-inspector.type.js.map +1 -0
- package/dist/ports/output.adapter.d.ts +1 -0
- package/dist/ports/output.adapter.d.ts.map +1 -0
- package/dist/ports/output.adapter.js +1 -0
- package/dist/ports/output.adapter.js.map +1 -0
- package/dist/runtime-trace.recorder.d.ts +1 -0
- package/dist/runtime-trace.recorder.d.ts.map +1 -0
- package/dist/runtime-trace.recorder.js +4 -1
- package/dist/runtime-trace.recorder.js.map +1 -0
- package/dist/source-metadata.service.d.ts +6 -0
- package/dist/source-metadata.service.d.ts.map +1 -0
- package/dist/source-metadata.service.js +6 -0
- package/dist/source-metadata.service.js.map +1 -0
- package/dist/types/direct-run.type.d.ts +1 -0
- package/dist/types/direct-run.type.d.ts.map +1 -0
- package/dist/types/direct-run.type.js +1 -0
- package/dist/types/direct-run.type.js.map +1 -0
- package/dist/types/graph-output.schema.d.ts +1 -0
- package/dist/types/graph-output.schema.d.ts.map +1 -0
- package/dist/types/graph-output.schema.js +1 -0
- package/dist/types/graph-output.schema.js.map +1 -0
- package/dist/types/graph-output.type.d.ts +1 -0
- package/dist/types/graph-output.type.d.ts.map +1 -0
- package/dist/types/graph-output.type.js +1 -0
- package/dist/types/graph-output.type.js.map +1 -0
- package/dist/types/module-controller.type.d.ts +1 -0
- package/dist/types/module-controller.type.d.ts.map +1 -0
- package/dist/types/module-controller.type.js +1 -0
- package/dist/types/module-controller.type.js.map +1 -0
- package/dist/types/module-map.type.d.ts +1 -0
- package/dist/types/module-map.type.d.ts.map +1 -0
- package/dist/types/module-map.type.js +1 -0
- package/dist/types/module-map.type.js.map +1 -0
- package/dist/types/module-provider.type.d.ts +1 -0
- package/dist/types/module-provider.type.d.ts.map +1 -0
- package/dist/types/module-provider.type.js +1 -0
- package/dist/types/module-provider.type.js.map +1 -0
- package/dist/types/module.type.d.ts +1 -0
- package/dist/types/module.type.d.ts.map +1 -0
- package/dist/types/module.type.js +1 -0
- package/dist/types/module.type.js.map +1 -0
- package/package.json +23 -6
- package/src/access-attempt-limiter.spec.ts +165 -0
- package/src/access-attempt-limiter.ts +208 -0
- package/src/access-token.service.spec.ts +319 -0
- package/src/access-token.service.ts +410 -0
- package/src/adapters/direct-run-output.adapter.spec.ts +424 -0
- package/src/adapters/direct-run-output.adapter.ts +262 -0
- package/src/adapters/discovery.spec.ts +47 -0
- package/src/adapters/discovery.ts +736 -0
- package/src/adapters/file-output.adapter.spec.ts +572 -0
- package/src/adapters/file-output.adapter.ts +611 -0
- package/src/adapters/http-output.adapter.spec.ts +521 -0
- package/src/adapters/http-output.adapter.ts +172 -0
- package/src/adapters/http-serve.adapter.ts +508 -0
- package/src/adapters/json-output.adapter.spec.ts +135 -0
- package/src/adapters/json-output.adapter.ts +26 -0
- package/src/adapters/viewer-output.adapter.spec.ts +338 -0
- package/src/adapters/viewer-output.adapter.ts +163 -0
- package/src/index.ts +28 -0
- package/src/inspector-endpoint-info.spec.ts +105 -0
- package/src/inspector-endpoint-info.ts +49 -0
- package/src/nest-graph-inspector.config.ts +7 -0
- package/src/nest-graph-inspector.module.spec.ts +71 -0
- package/src/nest-graph-inspector.module.ts +62 -0
- package/src/nest-graph-inspector.setup.spec.ts +846 -0
- package/src/nest-graph-inspector.setup.ts +1162 -0
- package/src/nest-graph-inspector.type.ts +181 -0
- package/src/ports/output.adapter.ts +10 -0
- package/src/runtime-trace.recorder.ts +441 -0
- package/src/source-metadata.service.spec.ts +52 -0
- package/src/source-metadata.service.ts +132 -0
- package/src/types/direct-run.type.ts +102 -0
- package/src/types/graph-output.schema.ts +258 -0
- package/src/types/graph-output.type.ts +62 -0
- package/src/types/module-controller.type.ts +5 -0
- package/src/types/module-map.type.ts +7 -0
- package/src/types/module-provider.type.ts +5 -0
- package/src/types/module.type.ts +10 -0
- package/dist/adapters/proxy.adapter.d.ts +0 -24
- package/dist/adapters/proxy.adapter.js +0 -180
- package/dist/ports/proxy.gateway.d.ts +0 -15
- package/dist/ports/proxy.gateway.js +0 -2
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export type AccessAttemptLimiterOptions = {
|
|
2
|
+
/** Whether failed token guesses are counted at all. Defaults to `true`. */
|
|
3
|
+
enabled?: boolean;
|
|
4
|
+
/** Guesses tolerated inside one window before the client is blocked. */
|
|
5
|
+
maxFailures?: number;
|
|
6
|
+
/** How long failures accumulate before the count resets. */
|
|
7
|
+
windowMs?: number;
|
|
8
|
+
/** How long a blocked client stays blocked. */
|
|
9
|
+
blockMs?: number;
|
|
10
|
+
/** Upper bound on tracked clients, so the tracker cannot be grown forever. */
|
|
11
|
+
maxTrackedClients?: number;
|
|
12
|
+
};
|
|
13
|
+
export declare const DEFAULT_MAX_FAILURES = 10;
|
|
14
|
+
export declare const DEFAULT_FAILURE_WINDOW_MS = 60000;
|
|
15
|
+
export declare const DEFAULT_BLOCK_MS: number;
|
|
16
|
+
export declare const DEFAULT_MAX_TRACKED_CLIENTS = 1000;
|
|
17
|
+
export type AccessAttemptDecision = {
|
|
18
|
+
blocked: false;
|
|
19
|
+
} | {
|
|
20
|
+
blocked: true;
|
|
21
|
+
retryAfterMs: number;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Per-client lockout for failed access token guesses.
|
|
25
|
+
*
|
|
26
|
+
* Tokens are signed, so guessing one is already infeasible; this caps how fast
|
|
27
|
+
* an attacker may try at all, and makes the attempt visible as a block rather
|
|
28
|
+
* than an endless stream of 401s.
|
|
29
|
+
*
|
|
30
|
+
* The tracker is bounded on purpose: an unbounded map keyed by client address
|
|
31
|
+
* would itself be a way to exhaust the host's memory.
|
|
32
|
+
*/
|
|
33
|
+
export declare class AccessAttemptLimiter {
|
|
34
|
+
private readonly enabled;
|
|
35
|
+
readonly maxFailures: number;
|
|
36
|
+
readonly windowMs: number;
|
|
37
|
+
readonly blockMs: number;
|
|
38
|
+
private readonly maxTrackedClients;
|
|
39
|
+
private readonly clients;
|
|
40
|
+
constructor(options?: AccessAttemptLimiterOptions);
|
|
41
|
+
isEnabled(): boolean;
|
|
42
|
+
/** Whether this client is currently locked out. */
|
|
43
|
+
check(clientId: string): AccessAttemptDecision;
|
|
44
|
+
/** Counts one failed guess, blocking the client once it runs out of them. */
|
|
45
|
+
recordFailure(clientId: string): void;
|
|
46
|
+
/** Clears a client's history once it proves it holds a valid token. */
|
|
47
|
+
recordSuccess(clientId: string): void;
|
|
48
|
+
/** Tracked client count, for tests and diagnostics. */
|
|
49
|
+
size(): number;
|
|
50
|
+
private evict;
|
|
51
|
+
/**
|
|
52
|
+
* Picks what to drop when the tracker is full.
|
|
53
|
+
*
|
|
54
|
+
* Blocked clients are given up last, and the one whose block ends soonest
|
|
55
|
+
* goes first. Otherwise a client could flush its own block simply by filling
|
|
56
|
+
* the tracker with traffic from other addresses.
|
|
57
|
+
*
|
|
58
|
+
* If every tracked client is blocked, one block does end early. That is the
|
|
59
|
+
* price of a bounded tracker, and it costs the attacker far more traffic
|
|
60
|
+
* than evicting an idle record would.
|
|
61
|
+
*/
|
|
62
|
+
private evictionCandidate;
|
|
63
|
+
private isBlockedAt;
|
|
64
|
+
private positive;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=access-attempt-limiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access-attempt-limiter.d.ts","sourceRoot":"","sources":["../src/access-attempt-limiter.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,2BAA2B,GAAG;IACxC,2EAA2E;IAC3E,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,wEAAwE;IACxE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,4DAA4D;IAC5D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,+CAA+C;IAC/C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,8EAA8E;IAC9E,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,eAAO,MAAM,oBAAoB,KAAK,CAAC;AACvC,eAAO,MAAM,yBAAyB,QAAS,CAAC;AAChD,eAAO,MAAM,gBAAgB,QAAc,CAAC;AAC5C,eAAO,MAAM,2BAA2B,OAAQ,CAAC;AAEjD,MAAM,MAAM,qBAAqB,GAC7B;IAAE,OAAO,EAAE,KAAK,CAAA;CAAE,GAClB;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAAC;AAS5C;;;;;;;;;GASG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAU;IAClC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAS;IAC3C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAmC;gBAE/C,OAAO,GAAE,2BAAgC;IAWrD,SAAS,IAAI,OAAO;IAIpB,mDAAmD;IACnD,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,qBAAqB;IAsB9C,6EAA6E;IAC7E,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAuCrC,uEAAuE;IACvE,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAIrC,uDAAuD;IACvD,IAAI,IAAI,MAAM;IAId,OAAO,CAAC,KAAK;IAqBb;;;;;;;;;;OAUG;IACH,OAAO,CAAC,iBAAiB;IA6BzB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,QAAQ;CAOjB"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccessAttemptLimiter = exports.DEFAULT_MAX_TRACKED_CLIENTS = exports.DEFAULT_BLOCK_MS = exports.DEFAULT_FAILURE_WINDOW_MS = exports.DEFAULT_MAX_FAILURES = void 0;
|
|
4
|
+
exports.DEFAULT_MAX_FAILURES = 10;
|
|
5
|
+
exports.DEFAULT_FAILURE_WINDOW_MS = 60_000;
|
|
6
|
+
exports.DEFAULT_BLOCK_MS = 15 * 60_000;
|
|
7
|
+
exports.DEFAULT_MAX_TRACKED_CLIENTS = 1_000;
|
|
8
|
+
/**
|
|
9
|
+
* Per-client lockout for failed access token guesses.
|
|
10
|
+
*
|
|
11
|
+
* Tokens are signed, so guessing one is already infeasible; this caps how fast
|
|
12
|
+
* an attacker may try at all, and makes the attempt visible as a block rather
|
|
13
|
+
* than an endless stream of 401s.
|
|
14
|
+
*
|
|
15
|
+
* The tracker is bounded on purpose: an unbounded map keyed by client address
|
|
16
|
+
* would itself be a way to exhaust the host's memory.
|
|
17
|
+
*/
|
|
18
|
+
class AccessAttemptLimiter {
|
|
19
|
+
enabled;
|
|
20
|
+
maxFailures;
|
|
21
|
+
windowMs;
|
|
22
|
+
blockMs;
|
|
23
|
+
maxTrackedClients;
|
|
24
|
+
clients = new Map();
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
this.enabled = options.enabled ?? true;
|
|
27
|
+
this.maxFailures = this.positive(options.maxFailures, exports.DEFAULT_MAX_FAILURES);
|
|
28
|
+
this.windowMs = this.positive(options.windowMs, exports.DEFAULT_FAILURE_WINDOW_MS);
|
|
29
|
+
this.blockMs = this.positive(options.blockMs, exports.DEFAULT_BLOCK_MS);
|
|
30
|
+
this.maxTrackedClients = this.positive(options.maxTrackedClients, exports.DEFAULT_MAX_TRACKED_CLIENTS);
|
|
31
|
+
}
|
|
32
|
+
isEnabled() {
|
|
33
|
+
return this.enabled;
|
|
34
|
+
}
|
|
35
|
+
/** Whether this client is currently locked out. */
|
|
36
|
+
check(clientId) {
|
|
37
|
+
if (!this.enabled) {
|
|
38
|
+
return { blocked: false };
|
|
39
|
+
}
|
|
40
|
+
const record = this.clients.get(clientId);
|
|
41
|
+
if (!record?.blockedUntil) {
|
|
42
|
+
return { blocked: false };
|
|
43
|
+
}
|
|
44
|
+
const now = Date.now();
|
|
45
|
+
if (now < record.blockedUntil) {
|
|
46
|
+
return { blocked: true, retryAfterMs: record.blockedUntil - now };
|
|
47
|
+
}
|
|
48
|
+
// The block has run its course, so the client starts over with a full
|
|
49
|
+
// allowance rather than one guess away from being blocked again.
|
|
50
|
+
this.clients.delete(clientId);
|
|
51
|
+
return { blocked: false };
|
|
52
|
+
}
|
|
53
|
+
/** Counts one failed guess, blocking the client once it runs out of them. */
|
|
54
|
+
recordFailure(clientId) {
|
|
55
|
+
if (!this.enabled) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const now = Date.now();
|
|
59
|
+
const existing = this.clients.get(clientId);
|
|
60
|
+
const withinWindow = existing !== undefined && now - existing.windowStartedAt < this.windowMs;
|
|
61
|
+
// A fresh window starts the count over, but still runs the threshold check
|
|
62
|
+
// below: with a limit of one, the very first guess has to block. An active
|
|
63
|
+
// block carries over, since blockMs outlives windowMs by default and
|
|
64
|
+
// rolling the window must not hand a blocked client a clean slate.
|
|
65
|
+
const record = withinWindow
|
|
66
|
+
? existing
|
|
67
|
+
: {
|
|
68
|
+
failures: 0,
|
|
69
|
+
windowStartedAt: now,
|
|
70
|
+
lastSeenAt: now,
|
|
71
|
+
blockedUntil: existing?.blockedUntil,
|
|
72
|
+
};
|
|
73
|
+
record.failures += 1;
|
|
74
|
+
record.lastSeenAt = now;
|
|
75
|
+
if (record.failures >= this.maxFailures) {
|
|
76
|
+
record.blockedUntil = now + this.blockMs;
|
|
77
|
+
}
|
|
78
|
+
if (!withinWindow) {
|
|
79
|
+
this.clients.set(clientId, record);
|
|
80
|
+
if (this.clients.size > this.maxTrackedClients) {
|
|
81
|
+
this.evict(now);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Clears a client's history once it proves it holds a valid token. */
|
|
86
|
+
recordSuccess(clientId) {
|
|
87
|
+
this.clients.delete(clientId);
|
|
88
|
+
}
|
|
89
|
+
/** Tracked client count, for tests and diagnostics. */
|
|
90
|
+
size() {
|
|
91
|
+
return this.clients.size;
|
|
92
|
+
}
|
|
93
|
+
evict(now) {
|
|
94
|
+
for (const [clientId, record] of this.clients) {
|
|
95
|
+
if (!this.isBlockedAt(record, now) &&
|
|
96
|
+
now - record.windowStartedAt >= this.windowMs) {
|
|
97
|
+
this.clients.delete(clientId);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
while (this.clients.size > this.maxTrackedClients) {
|
|
101
|
+
const victim = this.evictionCandidate(now);
|
|
102
|
+
if (victim === undefined) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
this.clients.delete(victim);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Picks what to drop when the tracker is full.
|
|
110
|
+
*
|
|
111
|
+
* Blocked clients are given up last, and the one whose block ends soonest
|
|
112
|
+
* goes first. Otherwise a client could flush its own block simply by filling
|
|
113
|
+
* the tracker with traffic from other addresses.
|
|
114
|
+
*
|
|
115
|
+
* If every tracked client is blocked, one block does end early. That is the
|
|
116
|
+
* price of a bounded tracker, and it costs the attacker far more traffic
|
|
117
|
+
* than evicting an idle record would.
|
|
118
|
+
*/
|
|
119
|
+
evictionCandidate(now) {
|
|
120
|
+
let candidate;
|
|
121
|
+
let candidateRank = Number.POSITIVE_INFINITY;
|
|
122
|
+
let candidateIsBlocked = true;
|
|
123
|
+
for (const [clientId, record] of this.clients) {
|
|
124
|
+
const blocked = this.isBlockedAt(record, now);
|
|
125
|
+
if (candidateIsBlocked && !blocked) {
|
|
126
|
+
candidate = clientId;
|
|
127
|
+
candidateRank = record.lastSeenAt;
|
|
128
|
+
candidateIsBlocked = false;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (blocked !== candidateIsBlocked) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const rank = blocked ? (record.blockedUntil ?? 0) : record.lastSeenAt;
|
|
135
|
+
if (rank < candidateRank) {
|
|
136
|
+
candidate = clientId;
|
|
137
|
+
candidateRank = rank;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return candidate;
|
|
141
|
+
}
|
|
142
|
+
isBlockedAt(record, now) {
|
|
143
|
+
return record.blockedUntil !== undefined && now < record.blockedUntil;
|
|
144
|
+
}
|
|
145
|
+
positive(value, fallback) {
|
|
146
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
|
|
147
|
+
return fallback;
|
|
148
|
+
}
|
|
149
|
+
return Math.floor(value);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.AccessAttemptLimiter = AccessAttemptLimiter;
|
|
153
|
+
//# sourceMappingURL=access-attempt-limiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access-attempt-limiter.js","sourceRoot":"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@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type http from 'node:http';
|
|
2
|
+
import type { NestGraphInspectorModuleOptions } from './nest-graph-inspector.type';
|
|
3
|
+
import type { HttpServeAuthorize } from './adapters/http-serve.adapter';
|
|
4
|
+
import { AccessAttemptLimiter } from './access-attempt-limiter';
|
|
5
|
+
/**
|
|
6
|
+
* Query parameter carrying the access token.
|
|
7
|
+
*
|
|
8
|
+
* The hosted viewer loads graph data straight from the inspected application,
|
|
9
|
+
* so the token has to survive a plain URL round trip.
|
|
10
|
+
*/
|
|
11
|
+
export declare const ACCESS_TOKEN_QUERY_PARAM = "__inspector_token";
|
|
12
|
+
/** Dedicated header accepted alongside `Authorization: Bearer`. */
|
|
13
|
+
export declare const ACCESS_TOKEN_HEADER = "x-graph-inspector-token";
|
|
14
|
+
/** Three hours: long enough for a working session, short enough to expire. */
|
|
15
|
+
export declare const DEFAULT_ACCESS_TOKEN_TTL_MS: number;
|
|
16
|
+
export type AccessTokenPayload = {
|
|
17
|
+
/** Issued at, epoch milliseconds. */
|
|
18
|
+
iat: number;
|
|
19
|
+
/** Expires at, epoch milliseconds. */
|
|
20
|
+
exp: number;
|
|
21
|
+
/** Token id, so two tokens issued in the same millisecond still differ. */
|
|
22
|
+
jti: string;
|
|
23
|
+
};
|
|
24
|
+
export type AccessTokenRejection = 'missing' | 'malformed' | 'signature' | 'expired';
|
|
25
|
+
export type AccessTokenVerification = {
|
|
26
|
+
ok: true;
|
|
27
|
+
payload: AccessTokenPayload;
|
|
28
|
+
} | {
|
|
29
|
+
ok: false;
|
|
30
|
+
reason: AccessTokenRejection;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Issues and verifies the short-lived tokens that gate every inspector
|
|
34
|
+
* endpoint.
|
|
35
|
+
*
|
|
36
|
+
* Tokens are self-describing: the payload carries its own expiry and an
|
|
37
|
+
* HMAC-SHA256 signature proves the payload was issued by this process. That
|
|
38
|
+
* keeps verification stateless, so an expired token dies on its own clock
|
|
39
|
+
* rather than relying on server-side bookkeeping.
|
|
40
|
+
*/
|
|
41
|
+
export declare class AccessTokenService {
|
|
42
|
+
private readonly logger;
|
|
43
|
+
private readonly enabled;
|
|
44
|
+
private readonly secret;
|
|
45
|
+
private readonly logToken;
|
|
46
|
+
readonly ttlMs: number;
|
|
47
|
+
readonly limiter: AccessAttemptLimiter;
|
|
48
|
+
private issued;
|
|
49
|
+
constructor(options?: NestGraphInspectorModuleOptions);
|
|
50
|
+
isEnabled(): boolean;
|
|
51
|
+
/** Whether the issued token may appear in the startup message. */
|
|
52
|
+
isTokenLoggable(): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* The token to hand out right now, minting a fresh one once the previous
|
|
55
|
+
* token has expired.
|
|
56
|
+
*/
|
|
57
|
+
current(): string;
|
|
58
|
+
/** Expiry of the token {@link current} would return, as a Date. */
|
|
59
|
+
currentExpiresAt(): Date;
|
|
60
|
+
private currentPayload;
|
|
61
|
+
verify(token: string | undefined): AccessTokenVerification;
|
|
62
|
+
/**
|
|
63
|
+
* Verifies the token attached to an inbound request.
|
|
64
|
+
*
|
|
65
|
+
* Returns `{ ok: true }` untouched when token protection is disabled, so
|
|
66
|
+
* callers can wire the guard unconditionally.
|
|
67
|
+
*/
|
|
68
|
+
authorizeRequest(req: http.IncomingMessage): AccessTokenVerification | {
|
|
69
|
+
ok: true;
|
|
70
|
+
payload?: undefined;
|
|
71
|
+
};
|
|
72
|
+
/** Human readable reason, safe to return in an HTTP body. */
|
|
73
|
+
rejectionMessage(reason: AccessTokenRejection): string;
|
|
74
|
+
/**
|
|
75
|
+
* Route guard applied to every endpoint the inspector installs.
|
|
76
|
+
*
|
|
77
|
+
* Rejections answer 401 rather than 404 so a developer holding an expired
|
|
78
|
+
* link can tell "you need a fresh token" from "wrong path".
|
|
79
|
+
*/
|
|
80
|
+
createHttpGuard(): HttpServeAuthorize;
|
|
81
|
+
private blockedResponse;
|
|
82
|
+
/**
|
|
83
|
+
* Identifies the client for lockout purposes.
|
|
84
|
+
*
|
|
85
|
+
* Deliberately the socket address rather than a forwarded header: a header
|
|
86
|
+
* is attacker-controlled, so trusting one would let a brute force rotate
|
|
87
|
+
* past the lockout, and would let anyone lock out an address they choose.
|
|
88
|
+
*/
|
|
89
|
+
private clientId;
|
|
90
|
+
/**
|
|
91
|
+
* Appends the current token to a URL the viewer will load.
|
|
92
|
+
*
|
|
93
|
+
* Skipped when the token must stay out of logs, since the viewer link is
|
|
94
|
+
* printed: the operator supplies the token themselves in that setup.
|
|
95
|
+
*/
|
|
96
|
+
appendToUrl(url: URL): URL;
|
|
97
|
+
private readToken;
|
|
98
|
+
private headerValue;
|
|
99
|
+
private mint;
|
|
100
|
+
private sign;
|
|
101
|
+
private signatureMatches;
|
|
102
|
+
private decodePayload;
|
|
103
|
+
private resolveSecret;
|
|
104
|
+
private normalizeTtlMs;
|
|
105
|
+
private now;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=access-token.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access-token.service.d.ts","sourceRoot":"","sources":["../src/access-token.service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAIlC,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,6BAA6B,CAAC;AACnF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAEhE;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,sBAAsB,CAAC;AAE5D,mEAAmE;AACnE,eAAO,MAAM,mBAAmB,4BAA4B,CAAC;AAE7D,8EAA8E;AAC9E,eAAO,MAAM,2BAA2B,QAAqB,CAAC;AAoB9D,MAAM,MAAM,kBAAkB,GAAG;IAC/B,qCAAqC;IACrC,GAAG,EAAE,MAAM,CAAC;IACZ,sCAAsC;IACtC,GAAG,EAAE,MAAM,CAAC;IACZ,2EAA2E;IA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H,OAAO,CAAC,QAAQ;IAShB;;;;;OAKG;IACH,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG;IAU1B,OAAO,CAAC,SAAS;IAiBjB,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,IAAI;IAkBZ,OAAO,CAAC,IAAI;IAYZ,OAAO,CAAC,gBAAgB;IAUxB,OAAO,CAAC,aAAa;IAqBrB,OAAO,CAAC,aAAa;IAqBrB,OAAO,CAAC,cAAc;IAQtB,OAAO,CAAC,GAAG;CAGZ"}
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var AccessTokenService_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.AccessTokenService = exports.DEFAULT_ACCESS_TOKEN_TTL_MS = exports.ACCESS_TOKEN_HEADER = exports.ACCESS_TOKEN_QUERY_PARAM = void 0;
|
|
17
|
+
const node_crypto_1 = require("node:crypto");
|
|
18
|
+
const common_1 = require("@nestjs/common");
|
|
19
|
+
const nest_graph_inspector_config_1 = require("./nest-graph-inspector.config");
|
|
20
|
+
const access_attempt_limiter_1 = require("./access-attempt-limiter");
|
|
21
|
+
/**
|
|
22
|
+
* Query parameter carrying the access token.
|
|
23
|
+
*
|
|
24
|
+
* The hosted viewer loads graph data straight from the inspected application,
|
|
25
|
+
* so the token has to survive a plain URL round trip.
|
|
26
|
+
*/
|
|
27
|
+
exports.ACCESS_TOKEN_QUERY_PARAM = '__inspector_token';
|
|
28
|
+
/** Dedicated header accepted alongside `Authorization: Bearer`. */
|
|
29
|
+
exports.ACCESS_TOKEN_HEADER = 'x-graph-inspector-token';
|
|
30
|
+
/** Three hours: long enough for a working session, short enough to expire. */
|
|
31
|
+
exports.DEFAULT_ACCESS_TOKEN_TTL_MS = 3 * 60 * 60 * 1000;
|
|
32
|
+
const TOKEN_PREFIX = 'ngi1';
|
|
33
|
+
const SECRET_ENV_KEY = 'NEST_GRAPH_INSPECTOR_TOKEN_SECRET';
|
|
34
|
+
/**
|
|
35
|
+
* Below this, a configured secret is worth brute forcing offline: a token's
|
|
36
|
+
* payload and signature are both readable, so anyone holding one leaked token
|
|
37
|
+
* can test candidate secrets locally, without touching the application.
|
|
38
|
+
*/
|
|
39
|
+
const MIN_SAFE_SECRET_LENGTH = 32;
|
|
40
|
+
/**
|
|
41
|
+
* Rejections that mean a token was actually presented and did not check out.
|
|
42
|
+
*
|
|
43
|
+
* A missing token is not a guess, and an expired one can only be produced by
|
|
44
|
+
* something that already held a real token, so neither counts toward a lockout.
|
|
45
|
+
*/
|
|
46
|
+
const GUESS_REJECTIONS = new Set(['malformed', 'signature']);
|
|
47
|
+
const REJECTION_MESSAGES = {
|
|
48
|
+
missing: 'Graph inspector access token is required. Open the viewer link printed in the application console.',
|
|
49
|
+
malformed: 'Graph inspector access token is malformed.',
|
|
50
|
+
signature: 'Graph inspector access token is not valid for this application.',
|
|
51
|
+
expired: 'Graph inspector access token has expired. Restart the application to print a fresh viewer link.',
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Issues and verifies the short-lived tokens that gate every inspector
|
|
55
|
+
* endpoint.
|
|
56
|
+
*
|
|
57
|
+
* Tokens are self-describing: the payload carries its own expiry and an
|
|
58
|
+
* HMAC-SHA256 signature proves the payload was issued by this process. That
|
|
59
|
+
* keeps verification stateless, so an expired token dies on its own clock
|
|
60
|
+
* rather than relying on server-side bookkeeping.
|
|
61
|
+
*/
|
|
62
|
+
let AccessTokenService = AccessTokenService_1 = class AccessTokenService {
|
|
63
|
+
logger = new common_1.Logger(AccessTokenService_1.name);
|
|
64
|
+
enabled;
|
|
65
|
+
secret;
|
|
66
|
+
logToken;
|
|
67
|
+
ttlMs;
|
|
68
|
+
limiter;
|
|
69
|
+
issued;
|
|
70
|
+
constructor(options) {
|
|
71
|
+
const accessToken = options?.accessToken;
|
|
72
|
+
this.enabled = accessToken?.enabled ?? true;
|
|
73
|
+
this.logToken = accessToken?.logToken ?? true;
|
|
74
|
+
this.ttlMs = this.normalizeTtlMs(accessToken?.ttlMs);
|
|
75
|
+
this.secret = this.resolveSecret(accessToken?.secret);
|
|
76
|
+
this.limiter = new access_attempt_limiter_1.AccessAttemptLimiter(accessToken?.bruteForce);
|
|
77
|
+
}
|
|
78
|
+
isEnabled() {
|
|
79
|
+
return this.enabled;
|
|
80
|
+
}
|
|
81
|
+
/** Whether the issued token may appear in the startup message. */
|
|
82
|
+
isTokenLoggable() {
|
|
83
|
+
return this.enabled && this.logToken;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The token to hand out right now, minting a fresh one once the previous
|
|
87
|
+
* token has expired.
|
|
88
|
+
*/
|
|
89
|
+
current() {
|
|
90
|
+
if (!this.issued || this.issued.payload.exp <= this.now()) {
|
|
91
|
+
this.issued = this.mint();
|
|
92
|
+
}
|
|
93
|
+
return this.issued.token;
|
|
94
|
+
}
|
|
95
|
+
/** Expiry of the token {@link current} would return, as a Date. */
|
|
96
|
+
currentExpiresAt() {
|
|
97
|
+
this.current();
|
|
98
|
+
return new Date(this.currentPayload().exp);
|
|
99
|
+
}
|
|
100
|
+
currentPayload() {
|
|
101
|
+
if (!this.issued) {
|
|
102
|
+
this.issued = this.mint();
|
|
103
|
+
}
|
|
104
|
+
return this.issued.payload;
|
|
105
|
+
}
|
|
106
|
+
verify(token) {
|
|
107
|
+
if (!token) {
|
|
108
|
+
return { ok: false, reason: 'missing' };
|
|
109
|
+
}
|
|
110
|
+
const separatorIndex = token.lastIndexOf('.');
|
|
111
|
+
if (separatorIndex === -1) {
|
|
112
|
+
return { ok: false, reason: 'malformed' };
|
|
113
|
+
}
|
|
114
|
+
const signedPart = token.slice(0, separatorIndex);
|
|
115
|
+
const signature = token.slice(separatorIndex + 1);
|
|
116
|
+
if (!signature || !signedPart.startsWith(`${TOKEN_PREFIX}.`)) {
|
|
117
|
+
return { ok: false, reason: 'malformed' };
|
|
118
|
+
}
|
|
119
|
+
if (!this.signatureMatches(signedPart, signature)) {
|
|
120
|
+
return { ok: false, reason: 'signature' };
|
|
121
|
+
}
|
|
122
|
+
const payload = this.decodePayload(signedPart.slice(TOKEN_PREFIX.length + 1));
|
|
123
|
+
if (!payload) {
|
|
124
|
+
return { ok: false, reason: 'malformed' };
|
|
125
|
+
}
|
|
126
|
+
if (payload.exp <= this.now()) {
|
|
127
|
+
return { ok: false, reason: 'expired' };
|
|
128
|
+
}
|
|
129
|
+
return { ok: true, payload };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Verifies the token attached to an inbound request.
|
|
133
|
+
*
|
|
134
|
+
* Returns `{ ok: true }` untouched when token protection is disabled, so
|
|
135
|
+
* callers can wire the guard unconditionally.
|
|
136
|
+
*/
|
|
137
|
+
authorizeRequest(req) {
|
|
138
|
+
if (!this.enabled) {
|
|
139
|
+
return { ok: true };
|
|
140
|
+
}
|
|
141
|
+
return this.verify(this.readToken(req));
|
|
142
|
+
}
|
|
143
|
+
/** Human readable reason, safe to return in an HTTP body. */
|
|
144
|
+
rejectionMessage(reason) {
|
|
145
|
+
return REJECTION_MESSAGES[reason];
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Route guard applied to every endpoint the inspector installs.
|
|
149
|
+
*
|
|
150
|
+
* Rejections answer 401 rather than 404 so a developer holding an expired
|
|
151
|
+
* link can tell "you need a fresh token" from "wrong path".
|
|
152
|
+
*/
|
|
153
|
+
createHttpGuard() {
|
|
154
|
+
return (req) => {
|
|
155
|
+
if (!this.enabled) {
|
|
156
|
+
return { ok: true };
|
|
157
|
+
}
|
|
158
|
+
const clientId = this.clientId(req);
|
|
159
|
+
const decision = this.limiter.check(clientId);
|
|
160
|
+
if (decision.blocked) {
|
|
161
|
+
return this.blockedResponse(decision.retryAfterMs);
|
|
162
|
+
}
|
|
163
|
+
const result = this.authorizeRequest(req);
|
|
164
|
+
if (result.ok) {
|
|
165
|
+
this.limiter.recordSuccess(clientId);
|
|
166
|
+
return { ok: true };
|
|
167
|
+
}
|
|
168
|
+
if (GUESS_REJECTIONS.has(result.reason)) {
|
|
169
|
+
this.limiter.recordFailure(clientId);
|
|
170
|
+
if (this.limiter.check(clientId).blocked) {
|
|
171
|
+
this.logger.warn(`Blocked ${clientId} from the graph inspector after ${this.limiter.maxFailures} invalid access tokens.`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
ok: false,
|
|
176
|
+
response: {
|
|
177
|
+
statusCode: 401,
|
|
178
|
+
contentType: 'application/json; charset=utf-8',
|
|
179
|
+
headers: {
|
|
180
|
+
'www-authenticate': 'Bearer realm="nest-graph-inspector"',
|
|
181
|
+
},
|
|
182
|
+
body: {
|
|
183
|
+
ok: false,
|
|
184
|
+
reason: result.reason,
|
|
185
|
+
error: this.rejectionMessage(result.reason),
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
blockedResponse(retryAfterMs) {
|
|
192
|
+
const retryAfterSeconds = Math.ceil(retryAfterMs / 1_000);
|
|
193
|
+
return {
|
|
194
|
+
ok: false,
|
|
195
|
+
response: {
|
|
196
|
+
statusCode: 429,
|
|
197
|
+
contentType: 'application/json; charset=utf-8',
|
|
198
|
+
headers: { 'retry-after': String(retryAfterSeconds) },
|
|
199
|
+
body: {
|
|
200
|
+
ok: false,
|
|
201
|
+
reason: 'blocked',
|
|
202
|
+
error: `Too many invalid graph inspector access tokens. Try again in ${retryAfterSeconds} seconds.`,
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Identifies the client for lockout purposes.
|
|
209
|
+
*
|
|
210
|
+
* Deliberately the socket address rather than a forwarded header: a header
|
|
211
|
+
* is attacker-controlled, so trusting one would let a brute force rotate
|
|
212
|
+
* past the lockout, and would let anyone lock out an address they choose.
|
|
213
|
+
*/
|
|
214
|
+
clientId(req) {
|
|
215
|
+
const address = req.socket?.remoteAddress ?? 'unknown';
|
|
216
|
+
// Node reports IPv4 peers on a dual-stack socket as ::ffff:127.0.0.1.
|
|
217
|
+
return address.startsWith('::ffff:')
|
|
218
|
+
? address.slice('::ffff:'.length)
|
|
219
|
+
: address;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Appends the current token to a URL the viewer will load.
|
|
223
|
+
*
|
|
224
|
+
* Skipped when the token must stay out of logs, since the viewer link is
|
|
225
|
+
* printed: the operator supplies the token themselves in that setup.
|
|
226
|
+
*/
|
|
227
|
+
appendToUrl(url) {
|
|
228
|
+
if (!this.isTokenLoggable()) {
|
|
229
|
+
return url;
|
|
230
|
+
}
|
|
231
|
+
url.searchParams.set(exports.ACCESS_TOKEN_QUERY_PARAM, this.current());
|
|
232
|
+
return url;
|
|
233
|
+
}
|
|
234
|
+
readToken(req) {
|
|
235
|
+
const authorization = this.headerValue(req.headers.authorization);
|
|
236
|
+
if (authorization?.toLowerCase().startsWith('bearer ')) {
|
|
237
|
+
return authorization.slice('bearer '.length).trim() || undefined;
|
|
238
|
+
}
|
|
239
|
+
const headerToken = this.headerValue(req.headers[exports.ACCESS_TOKEN_HEADER]);
|
|
240
|
+
if (headerToken) {
|
|
241
|
+
return headerToken;
|
|
242
|
+
}
|
|
243
|
+
// `req.url` is origin-relative, so the base only exists to satisfy URL.
|
|
244
|
+
const requestUrl = new URL(req.url ?? '/', 'http://graph-inspector.invalid');
|
|
245
|
+
return requestUrl.searchParams.get(exports.ACCESS_TOKEN_QUERY_PARAM) ?? undefined;
|
|
246
|
+
}
|
|
247
|
+
headerValue(value) {
|
|
248
|
+
const header = Array.isArray(value) ? value[0] : value;
|
|
249
|
+
return header?.trim() || undefined;
|
|
250
|
+
}
|
|
251
|
+
mint() {
|
|
252
|
+
const issuedAt = this.now();
|
|
253
|
+
const payload = {
|
|
254
|
+
iat: issuedAt,
|
|
255
|
+
exp: issuedAt + this.ttlMs,
|
|
256
|
+
jti: (0, node_crypto_1.randomBytes)(9).toString('base64url'),
|
|
257
|
+
};
|
|
258
|
+
const signedPart = `${TOKEN_PREFIX}.${Buffer.from(JSON.stringify(payload)).toString('base64url')}`;
|
|
259
|
+
return {
|
|
260
|
+
token: `${signedPart}.${this.sign(signedPart)}`,
|
|
261
|
+
payload,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
sign(signedPart) {
|
|
265
|
+
/**
|
|
266
|
+
* The digest is encoded from a Buffer rather than by passing `base64url`
|
|
267
|
+
* to `digest()`. Both produce the same string on Node, but a runtime that
|
|
268
|
+
* ignores the encoding argument hands back raw bytes, and a token carrying
|
|
269
|
+
* raw bytes does not survive the URL it travels in.
|
|
270
|
+
*/
|
|
271
|
+
return Buffer.from((0, node_crypto_1.createHmac)('sha256', this.secret).update(signedPart).digest()).toString('base64url');
|
|
272
|
+
}
|
|
273
|
+
signatureMatches(signedPart, signature) {
|
|
274
|
+
const expected = Buffer.from(this.sign(signedPart));
|
|
275
|
+
const actual = Buffer.from(signature);
|
|
276
|
+
// timingSafeEqual throws on a length mismatch, which is itself a mismatch.
|
|
277
|
+
return (expected.length === actual.length && (0, node_crypto_1.timingSafeEqual)(expected, actual));
|
|
278
|
+
}
|
|
279
|
+
decodePayload(encoded) {
|
|
280
|
+
try {
|
|
281
|
+
const payload = JSON.parse(Buffer.from(encoded, 'base64url').toString('utf8'));
|
|
282
|
+
if (typeof payload !== 'object' ||
|
|
283
|
+
payload === null ||
|
|
284
|
+
typeof payload.iat !== 'number' ||
|
|
285
|
+
typeof payload.exp !== 'number') {
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
return payload;
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
return undefined;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
resolveSecret(configuredSecret) {
|
|
295
|
+
// A stable secret keeps tokens valid across restarts, which matters for
|
|
296
|
+
// containerized apps that reboot more often than a developer reloads the
|
|
297
|
+
// viewer. Without one, every process gets its own throwaway secret.
|
|
298
|
+
const secret = configuredSecret ?? process.env[SECRET_ENV_KEY];
|
|
299
|
+
if (!secret) {
|
|
300
|
+
return (0, node_crypto_1.randomBytes)(32);
|
|
301
|
+
}
|
|
302
|
+
if (this.enabled && secret.length < MIN_SAFE_SECRET_LENGTH) {
|
|
303
|
+
this.logger.warn(`Graph inspector access token secret is ${secret.length} characters. ` +
|
|
304
|
+
`Use at least ${MIN_SAFE_SECRET_LENGTH}, or leave it unset for a random one: ` +
|
|
305
|
+
'a short secret can be recovered offline from a single leaked token.');
|
|
306
|
+
}
|
|
307
|
+
return Buffer.from(secret, 'utf8');
|
|
308
|
+
}
|
|
309
|
+
normalizeTtlMs(ttlMs) {
|
|
310
|
+
if (typeof ttlMs !== 'number' || !Number.isFinite(ttlMs) || ttlMs <= 0) {
|
|
311
|
+
return exports.DEFAULT_ACCESS_TOKEN_TTL_MS;
|
|
312
|
+
}
|
|
313
|
+
return Math.floor(ttlMs);
|
|
314
|
+
}
|
|
315
|
+
now() {
|
|
316
|
+
return Date.now();
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
exports.AccessTokenService = AccessTokenService;
|
|
320
|
+
exports.AccessTokenService = AccessTokenService = AccessTokenService_1 = __decorate([
|
|
321
|
+
(0, common_1.Injectable)(),
|
|
322
|
+
__param(0, (0, common_1.Optional)()),
|
|
323
|
+
__param(0, (0, common_1.Inject)(nest_graph_inspector_config_1.MODULE_OPTIONS_TOKEN)),
|
|
324
|
+
__metadata("design:paramtypes", [Object])
|
|
325
|
+
], AccessTokenService);
|
|
326
|
+
//# sourceMappingURL=access-token.service.js.map
|