@adaptic/backend 0.0.1031 → 0.0.1033
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.
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket connection-auth decision for the `/subscriptions` transport.
|
|
3
|
+
*
|
|
4
|
+
* graphql-ws invokes the `context` callback lazily, on Subscribe. Whatever that
|
|
5
|
+
* callback throws terminates the socket: graphql-ws catches it in its message
|
|
6
|
+
* handler and closes with `CloseCode.InternalServerError` (4500), carrying the
|
|
7
|
+
* error message as the close reason. There is deliberately no path that keeps
|
|
8
|
+
* an unverified socket open with a degraded context — a subscription that
|
|
9
|
+
* streams data to a caller whose token did not verify is an authentication
|
|
10
|
+
* bypass, not a degraded mode.
|
|
11
|
+
*
|
|
12
|
+
* The decision is separated from its effects (metrics, logging, Prisma
|
|
13
|
+
* attachment) so the security-relevant predicate is a pure function that can be
|
|
14
|
+
* exercised, and mutation-proven, without booting the server.
|
|
15
|
+
*/
|
|
16
|
+
import { GraphQLError } from 'graphql';
|
|
17
|
+
import { type AuthErrorReason, type BackendPrincipal } from './token-verifier';
|
|
18
|
+
/**
|
|
19
|
+
* Outcome of evaluating a WebSocket connection's `authorization` parameter.
|
|
20
|
+
*
|
|
21
|
+
* - `anonymous`: no bearer token presented. The socket opens with a null
|
|
22
|
+
* principal; the AuthChecker rejects any operation that requires one.
|
|
23
|
+
* - `authenticated`: the token verified to a `BackendPrincipal`.
|
|
24
|
+
* - `rejected`: a token was presented and failed verification. The caller MUST
|
|
25
|
+
* throw so graphql-ws closes the socket.
|
|
26
|
+
*/
|
|
27
|
+
export type WsAuthDecision = {
|
|
28
|
+
kind: 'anonymous';
|
|
29
|
+
} | {
|
|
30
|
+
kind: 'authenticated';
|
|
31
|
+
principal: BackendPrincipal;
|
|
32
|
+
} | {
|
|
33
|
+
kind: 'rejected';
|
|
34
|
+
reason: AuthErrorReason;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Token verification entry point. Injectable so callers can exercise the
|
|
38
|
+
* decision without provisioning real signing keys.
|
|
39
|
+
*/
|
|
40
|
+
export type TokenVerifier = (token: string) => Promise<BackendPrincipal>;
|
|
41
|
+
/**
|
|
42
|
+
* Extract the raw token from an `Authorization` value. Returns the empty string
|
|
43
|
+
* for anything that is not a well-formed `Bearer <token>` header, which the
|
|
44
|
+
* caller treats as "no token presented" rather than as a malformed token.
|
|
45
|
+
*/
|
|
46
|
+
export declare function parseBearerToken(authHeader: string | undefined): string;
|
|
47
|
+
/**
|
|
48
|
+
* Classify a WebSocket connection's credentials.
|
|
49
|
+
*
|
|
50
|
+
* A presented-but-unverifiable token always yields `rejected`, never
|
|
51
|
+
* `anonymous`. Downgrading a failed verification to an anonymous session would
|
|
52
|
+
* convert an authentication failure into a weaker session that still serves
|
|
53
|
+
* data — the bypass this function exists to make unrepresentable.
|
|
54
|
+
*
|
|
55
|
+
* Verification failures that are not `AuthError` are classified
|
|
56
|
+
* `bad_signature` rather than escaping, so an unexpected verifier fault still
|
|
57
|
+
* fails closed (socket closed) instead of surfacing as an unhandled rejection.
|
|
58
|
+
*/
|
|
59
|
+
export declare function decideWsAuth(authHeader: string | undefined, verify?: TokenVerifier): Promise<WsAuthDecision>;
|
|
60
|
+
/**
|
|
61
|
+
* The error a rejected WebSocket connection throws from its `context` callback.
|
|
62
|
+
*
|
|
63
|
+
* `extensions.http.status` is carried for symmetry with the HTTP context: the
|
|
64
|
+
* WebSocket transport closes rather than producing an HTTP response, but any
|
|
65
|
+
* future code that funnels a WS rejection into an HTTP reply gets the correct
|
|
66
|
+
* status without a second definition of it.
|
|
67
|
+
*/
|
|
68
|
+
export declare function unauthenticatedWsError(reason: AuthErrorReason): GraphQLError;
|
|
69
|
+
//# sourceMappingURL=ws-auth-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ws-auth-context.d.ts","sourceRoot":"","sources":["../../../src/auth/ws-auth-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAGL,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACtB,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;GAQG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,GACrB;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,SAAS,EAAE,gBAAgB,CAAA;CAAE,GACtD;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,MAAM,EAAE,eAAe,CAAA;CAAE,CAAC;AAElD;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC;AAEzE;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAGvE;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,YAAY,CAChC,UAAU,EAAE,MAAM,GAAG,SAAS,EAC9B,MAAM,GAAE,aAAkC,GACzC,OAAO,CAAC,cAAc,CAAC,CAYzB;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,eAAe,GAAG,YAAY,CAQ5E"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ws-auth-context.js","sourceRoot":"","sources":["../../../src/auth/ws-auth-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EACL,kBAAkB,EAClB,SAAS,GAGV,MAAM,kBAAkB,CAAC;AAsB1B;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,UAA8B;IAC7D,IAAI,CAAC,UAAU,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,SAAS,CAAC;QAAE,OAAO,EAAE,CAAC;IAChE,OAAO,UAAU,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;AACnD,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,UAA8B,EAC9B,SAAwB,kBAAkB;IAE1C,MAAM,KAAK,GAAG,gBAAgB,CAAC,UAAU,CAAC,CAAC;IAC3C,IAAI,CAAC,KAAK;QAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;IAEzC,IAAI,CAAC;QACH,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,CAAC;QACtC,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,SAAS,EAAE,CAAC;IAC9C,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,MAAM,MAAM,GACV,CAAC,YAAY,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,eAAe,CAAC;QACtD,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC;IACtC,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CAAC,MAAuB;IAC5D,OAAO,IAAI,YAAY,CAAC,iBAAiB,EAAE;QACzC,UAAU,EAAE;YACV,IAAI,EAAE,iBAAiB;YACvB,MAAM;YACN,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE;SACtB;KACF,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket connection-auth decision for the `/subscriptions` transport.
|
|
3
|
+
*
|
|
4
|
+
* graphql-ws invokes the `context` callback lazily, on Subscribe. Whatever that
|
|
5
|
+
* callback throws terminates the socket: graphql-ws catches it in its message
|
|
6
|
+
* handler and closes with `CloseCode.InternalServerError` (4500), carrying the
|
|
7
|
+
* error message as the close reason. There is deliberately no path that keeps
|
|
8
|
+
* an unverified socket open with a degraded context — a subscription that
|
|
9
|
+
* streams data to a caller whose token did not verify is an authentication
|
|
10
|
+
* bypass, not a degraded mode.
|
|
11
|
+
*
|
|
12
|
+
* The decision is separated from its effects (metrics, logging, Prisma
|
|
13
|
+
* attachment) so the security-relevant predicate is a pure function that can be
|
|
14
|
+
* exercised, and mutation-proven, without booting the server.
|
|
15
|
+
*/
|
|
16
|
+
import { GraphQLError } from 'graphql';
|
|
17
|
+
import { verifyBackendToken, AuthError, } from './token-verifier.mjs';
|
|
18
|
+
/**
|
|
19
|
+
* Extract the raw token from an `Authorization` value. Returns the empty string
|
|
20
|
+
* for anything that is not a well-formed `Bearer <token>` header, which the
|
|
21
|
+
* caller treats as "no token presented" rather than as a malformed token.
|
|
22
|
+
*/
|
|
23
|
+
export function parseBearerToken(authHeader) {
|
|
24
|
+
if (!authHeader || !authHeader.startsWith('Bearer '))
|
|
25
|
+
return '';
|
|
26
|
+
return authHeader.slice('Bearer '.length).trim();
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Classify a WebSocket connection's credentials.
|
|
30
|
+
*
|
|
31
|
+
* A presented-but-unverifiable token always yields `rejected`, never
|
|
32
|
+
* `anonymous`. Downgrading a failed verification to an anonymous session would
|
|
33
|
+
* convert an authentication failure into a weaker session that still serves
|
|
34
|
+
* data — the bypass this function exists to make unrepresentable.
|
|
35
|
+
*
|
|
36
|
+
* Verification failures that are not `AuthError` are classified
|
|
37
|
+
* `bad_signature` rather than escaping, so an unexpected verifier fault still
|
|
38
|
+
* fails closed (socket closed) instead of surfacing as an unhandled rejection.
|
|
39
|
+
*/
|
|
40
|
+
export async function decideWsAuth(authHeader, verify = verifyBackendToken) {
|
|
41
|
+
const token = parseBearerToken(authHeader);
|
|
42
|
+
if (!token)
|
|
43
|
+
return { kind: 'anonymous' };
|
|
44
|
+
try {
|
|
45
|
+
const principal = await verify(token);
|
|
46
|
+
return { kind: 'authenticated', principal };
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
const reason = e instanceof AuthError ? e.reason : 'bad_signature';
|
|
50
|
+
return { kind: 'rejected', reason };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The error a rejected WebSocket connection throws from its `context` callback.
|
|
55
|
+
*
|
|
56
|
+
* `extensions.http.status` is carried for symmetry with the HTTP context: the
|
|
57
|
+
* WebSocket transport closes rather than producing an HTTP response, but any
|
|
58
|
+
* future code that funnels a WS rejection into an HTTP reply gets the correct
|
|
59
|
+
* status without a second definition of it.
|
|
60
|
+
*/
|
|
61
|
+
export function unauthenticatedWsError(reason) {
|
|
62
|
+
return new GraphQLError('Unauthenticated', {
|
|
63
|
+
extensions: {
|
|
64
|
+
code: 'UNAUTHENTICATED',
|
|
65
|
+
reason,
|
|
66
|
+
http: { status: 401 },
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=ws-auth-context.js.map
|
package/package.json
CHANGED
package/server.cjs
CHANGED
|
@@ -73,6 +73,7 @@ const prismaClient_1 = __importStar(require("./prismaClient.cjs"));
|
|
|
73
73
|
const health_1 = require("./health.cjs");
|
|
74
74
|
const logger_1 = require("./utils/logger.cjs");
|
|
75
75
|
const token_verifier_1 = require("./auth/token-verifier.cjs");
|
|
76
|
+
const ws_auth_context_1 = require("./auth/ws-auth-context.cjs");
|
|
76
77
|
const graphql_auth_shadow_1 = require("./auth/graphql-auth-shadow.cjs");
|
|
77
78
|
/**
|
|
78
79
|
* Adapt a verified `BackendPrincipal` to the legacy `user` context shape used
|
|
@@ -418,63 +419,54 @@ const startServer = async () => {
|
|
|
418
419
|
}
|
|
419
420
|
const authHeader = ((_a = ctx.connectionParams) === null || _a === void 0 ? void 0 : _a.authorization) ||
|
|
420
421
|
'';
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
//
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
// an HTTP response (e.g. a graceful-degrade fallback) will get
|
|
470
|
-
// the correct status without further changes.
|
|
471
|
-
throw new graphql_1.GraphQLError('Unauthenticated', {
|
|
472
|
-
extensions: {
|
|
473
|
-
code: 'UNAUTHENTICATED',
|
|
474
|
-
reason,
|
|
475
|
-
http: { status: 401 },
|
|
476
|
-
},
|
|
477
|
-
});
|
|
422
|
+
// The classification itself lives in src/auth/ws-auth-context.ts so the
|
|
423
|
+
// security predicate is reachable by tests without booting the server.
|
|
424
|
+
// The effects it drives — shadow metrics, logging, Prisma attachment —
|
|
425
|
+
// stay here at the transport boundary.
|
|
426
|
+
const decision = await (0, ws_auth_context_1.decideWsAuth)(authHeader);
|
|
427
|
+
switch (decision.kind) {
|
|
428
|
+
case 'anonymous': {
|
|
429
|
+
// No token presented -> deliver a null-user context. The AuthChecker
|
|
430
|
+
// landing in CORTEX-P0-001 will reject any subscription that requires
|
|
431
|
+
// a principal. Until then, public subscriptions continue to work.
|
|
432
|
+
//
|
|
433
|
+
// Shadow-observe the would-deny WITHOUT blocking — the WebSocket
|
|
434
|
+
// parallel of the HTTP path. Operation name is best-effort from
|
|
435
|
+
// ExecutionArgs; origin / user-agent / IP come from the upgrade
|
|
436
|
+
// request carried on ctx.extra.
|
|
437
|
+
const wsIdentity = (0, graphql_auth_shadow_1.extractHeaderIdentityFromWsExtra)(ctx.extra);
|
|
438
|
+
(0, graphql_auth_shadow_1.recordShadowAuthMiss)({
|
|
439
|
+
transport: 'ws',
|
|
440
|
+
operationName: (0, graphql_auth_shadow_1.extractOperationNameFromArgs)(args),
|
|
441
|
+
origin: wsIdentity.origin,
|
|
442
|
+
ip: wsIdentity.ip,
|
|
443
|
+
userAgent: wsIdentity.userAgent,
|
|
444
|
+
authHeaderPresent: authHeader.length > 0,
|
|
445
|
+
});
|
|
446
|
+
return { prisma: global.prisma, user: null, principal: null };
|
|
447
|
+
}
|
|
448
|
+
case 'authenticated': {
|
|
449
|
+
(0, graphql_auth_shadow_1.recordAuthContextOutcome)('ws', 'authenticated');
|
|
450
|
+
return {
|
|
451
|
+
prisma: global.prisma,
|
|
452
|
+
user: principalToUser(decision.principal),
|
|
453
|
+
principal: decision.principal,
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
case 'rejected': {
|
|
457
|
+
// Already rejected today (connection closed). Counted only to complete
|
|
458
|
+
// the shadow denominator alongside authenticated / no_principal.
|
|
459
|
+
(0, graphql_auth_shadow_1.recordAuthContextOutcome)('ws', 'invalid_token');
|
|
460
|
+
logger_1.logger.warn('WebSocket auth rejected — closing connection', {
|
|
461
|
+
reason: decision.reason,
|
|
462
|
+
});
|
|
463
|
+
// Throwing IS the close mechanism: graphql-ws propagates the throw
|
|
464
|
+
// out of its message handler and terminates the socket (close code
|
|
465
|
+
// 4500, close reason "Unauthenticated"). There is deliberately no
|
|
466
|
+
// fall-through to a degraded `authError` context that would keep
|
|
467
|
+
// delivering messages to an unauthenticated socket.
|
|
468
|
+
throw (0, ws_auth_context_1.unauthenticatedWsError)(decision.reason);
|
|
469
|
+
}
|
|
478
470
|
}
|
|
479
471
|
},
|
|
480
472
|
}, wsServer);
|