@memberjunction/server 5.51.2 → 5.51.4
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 +34 -1
- package/dist/agentSessions/SessionManager.d.ts +5 -2
- package/dist/agentSessions/SessionManager.d.ts.map +1 -1
- package/dist/agentSessions/SessionManager.js +17 -8
- package/dist/agentSessions/SessionManager.js.map +1 -1
- package/dist/agentSessions/SessionRunIDVerifier.d.ts +63 -0
- package/dist/agentSessions/SessionRunIDVerifier.d.ts.map +1 -0
- package/dist/agentSessions/SessionRunIDVerifier.js +136 -0
- package/dist/agentSessions/SessionRunIDVerifier.js.map +1 -0
- package/dist/agentSessions/index.d.ts +1 -0
- package/dist/agentSessions/index.d.ts.map +1 -1
- package/dist/agentSessions/index.js +1 -0
- package/dist/agentSessions/index.js.map +1 -1
- package/dist/auth/actionAuthorization.d.ts +40 -0
- package/dist/auth/actionAuthorization.d.ts.map +1 -0
- package/dist/auth/actionAuthorization.js +94 -0
- package/dist/auth/actionAuthorization.js.map +1 -0
- package/dist/auth/index.d.ts +1 -0
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +1 -0
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/scopeLimitedPrincipal.d.ts +39 -0
- package/dist/auth/scopeLimitedPrincipal.d.ts.map +1 -0
- package/dist/auth/scopeLimitedPrincipal.js +49 -0
- package/dist/auth/scopeLimitedPrincipal.js.map +1 -0
- package/dist/config.d.ts +53 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +34 -1
- package/dist/config.js.map +1 -1
- package/dist/context.d.ts +6 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +24 -34
- package/dist/context.js.map +1 -1
- package/dist/generated/generated.d.ts.map +1 -1
- package/dist/generated/generated.js +3262 -3273
- package/dist/generated/generated.js.map +1 -1
- package/dist/generic/CacheInvalidationResolver.d.ts +124 -4
- package/dist/generic/CacheInvalidationResolver.d.ts.map +1 -1
- package/dist/generic/CacheInvalidationResolver.js +170 -15
- package/dist/generic/CacheInvalidationResolver.js.map +1 -1
- package/dist/generic/FireAndForgetHeartbeat.d.ts +5 -0
- package/dist/generic/FireAndForgetHeartbeat.d.ts.map +1 -1
- package/dist/generic/FireAndForgetHeartbeat.js +5 -4
- package/dist/generic/FireAndForgetHeartbeat.js.map +1 -1
- package/dist/generic/PushStatusResolver.d.ts +58 -0
- package/dist/generic/PushStatusResolver.d.ts.map +1 -1
- package/dist/generic/PushStatusResolver.js +42 -3
- package/dist/generic/PushStatusResolver.js.map +1 -1
- package/dist/generic/ResolverBase.d.ts +148 -3
- package/dist/generic/ResolverBase.d.ts.map +1 -1
- package/dist/generic/ResolverBase.js +287 -30
- package/dist/generic/ResolverBase.js.map +1 -1
- package/dist/generic/RunViewResolver.d.ts.map +1 -1
- package/dist/generic/RunViewResolver.js +12 -0
- package/dist/generic/RunViewResolver.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +58 -50
- package/dist/index.js.map +1 -1
- package/dist/multiTenancy/index.d.ts.map +1 -1
- package/dist/multiTenancy/index.js +22 -2
- package/dist/multiTenancy/index.js.map +1 -1
- package/dist/postgresqlCompat.d.ts +8 -0
- package/dist/postgresqlCompat.d.ts.map +1 -0
- package/dist/postgresqlCompat.js +19 -0
- package/dist/postgresqlCompat.js.map +1 -0
- package/dist/postgresqlPoolSettings.d.ts +108 -0
- package/dist/postgresqlPoolSettings.d.ts.map +1 -0
- package/dist/postgresqlPoolSettings.js +134 -0
- package/dist/postgresqlPoolSettings.js.map +1 -0
- package/dist/realtimeWidget/widgetGuestElevation.d.ts +2 -2
- package/dist/realtimeWidget/widgetGuestElevation.js +2 -2
- package/dist/resolvers/ActionResolver.d.ts +31 -5
- package/dist/resolvers/ActionResolver.d.ts.map +1 -1
- package/dist/resolvers/ActionResolver.js +75 -14
- package/dist/resolvers/ActionResolver.js.map +1 -1
- package/dist/resolvers/AdhocQueryResolver.d.ts +39 -17
- package/dist/resolvers/AdhocQueryResolver.d.ts.map +1 -1
- package/dist/resolvers/AdhocQueryResolver.js +97 -157
- package/dist/resolvers/AdhocQueryResolver.js.map +1 -1
- package/dist/resolvers/DatasetResolver.d.ts +24 -3
- package/dist/resolvers/DatasetResolver.d.ts.map +1 -1
- package/dist/resolvers/DatasetResolver.js +57 -8
- package/dist/resolvers/DatasetResolver.js.map +1 -1
- package/dist/resolvers/EntityRecordNameResolver.d.ts +14 -4
- package/dist/resolvers/EntityRecordNameResolver.d.ts.map +1 -1
- package/dist/resolvers/EntityRecordNameResolver.js +18 -6
- package/dist/resolvers/EntityRecordNameResolver.js.map +1 -1
- package/dist/resolvers/GetDataContextDataResolver.d.ts +33 -7
- package/dist/resolvers/GetDataContextDataResolver.d.ts.map +1 -1
- package/dist/resolvers/GetDataContextDataResolver.js +117 -38
- package/dist/resolvers/GetDataContextDataResolver.js.map +1 -1
- package/dist/resolvers/GetDataResolver.d.ts.map +1 -1
- package/dist/resolvers/GetDataResolver.js +13 -1
- package/dist/resolvers/GetDataResolver.js.map +1 -1
- package/dist/resolvers/MCPResolver.d.ts.map +1 -1
- package/dist/resolvers/MCPResolver.js +13 -16
- package/dist/resolvers/MCPResolver.js.map +1 -1
- package/dist/resolvers/RealtimeClientSessionResolver.d.ts +26 -14
- package/dist/resolvers/RealtimeClientSessionResolver.d.ts.map +1 -1
- package/dist/resolvers/RealtimeClientSessionResolver.js +63 -46
- package/dist/resolvers/RealtimeClientSessionResolver.js.map +1 -1
- package/dist/resolvers/RemoteBrowserActionResolver.d.ts +2 -0
- package/dist/resolvers/RemoteBrowserActionResolver.d.ts.map +1 -1
- package/dist/resolvers/RemoteBrowserActionResolver.js +24 -27
- package/dist/resolvers/RemoteBrowserActionResolver.js.map +1 -1
- package/dist/resolvers/RunAIAgentResolver.d.ts.map +1 -1
- package/dist/resolvers/RunAIAgentResolver.js +30 -40
- package/dist/resolvers/RunAIAgentResolver.js.map +1 -1
- package/dist/resolvers/RunTemplateResolver.d.ts.map +1 -1
- package/dist/resolvers/RunTemplateResolver.js +12 -11
- package/dist/resolvers/RunTemplateResolver.js.map +1 -1
- package/dist/resolvers/RunTestResolver.d.ts.map +1 -1
- package/dist/resolvers/RunTestResolver.js +90 -112
- package/dist/resolvers/RunTestResolver.js.map +1 -1
- package/dist/resolvers/SyncRolesUsersResolver.d.ts.map +1 -1
- package/dist/resolvers/SyncRolesUsersResolver.js +11 -2
- package/dist/resolvers/SyncRolesUsersResolver.js.map +1 -1
- package/dist/resolvers/VersionHistoryResolver.d.ts.map +1 -1
- package/dist/resolvers/VersionHistoryResolver.js +8 -12
- package/dist/resolvers/VersionHistoryResolver.js.map +1 -1
- package/dist/resolvers/adhocTimeout.d.ts +7 -0
- package/dist/resolvers/adhocTimeout.d.ts.map +1 -0
- package/dist/resolvers/adhocTimeout.js +12 -0
- package/dist/resolvers/adhocTimeout.js.map +1 -0
- package/dist/rest/ViewOperationsHandler.d.ts +3 -1
- package/dist/rest/ViewOperationsHandler.d.ts.map +1 -1
- package/dist/rest/ViewOperationsHandler.js +29 -17
- package/dist/rest/ViewOperationsHandler.js.map +1 -1
- package/dist/services/TaskOrchestrator.d.ts.map +1 -1
- package/dist/services/TaskOrchestrator.js +17 -15
- package/dist/services/TaskOrchestrator.js.map +1 -1
- package/package.json +90 -89
- package/src/__tests__/ActionResolver.authorization.test.ts +353 -0
- package/src/__tests__/AdhocQueryResolver.bugs.test.ts +14 -35
- package/src/__tests__/AdhocQueryResolver.pagination.test.ts +6 -2
- package/src/__tests__/AdhocQueryResolver.scopeGuard.test.ts +84 -0
- package/src/__tests__/AdhocQueryResolver.sqlAuthorizer.test.ts +104 -0
- package/src/__tests__/DatasetResolver.itemFilters.test.ts +201 -0
- package/src/__tests__/EntityRecordNameResolver.rowSecurity.test.ts +152 -0
- package/src/__tests__/GetDataContextDataResolver.sqlItems.test.ts +416 -0
- package/src/__tests__/ListOperationsResolver.adhocScreen.test.ts +136 -0
- package/src/__tests__/RealtimeClientSessionResolver.configTamper.test.ts +318 -0
- package/src/__tests__/RealtimeClientSessionResolver.test.ts +66 -27
- package/src/__tests__/RemoteBrowserActionResolver.goalRunLink.test.ts +134 -0
- package/src/__tests__/ResolverBase.adhocReadScope.test.ts +221 -0
- package/src/__tests__/ResolverBase.aggregateScreen.test.ts +173 -0
- package/src/__tests__/ResolverBase.balancedClause.test.ts +155 -0
- package/src/__tests__/ResolverBase.clauseUnscopedRead.test.ts +116 -0
- package/src/__tests__/ResolverBase.filterEscaping.test.ts +351 -0
- package/src/__tests__/RunTemplateResolver.test.ts +132 -0
- package/src/__tests__/RunViewResolver.cacheCheckScreen.test.ts +164 -0
- package/src/__tests__/SessionManager.test.ts +88 -13
- package/src/__tests__/SessionRunIDVerifier.test.ts +255 -0
- package/src/__tests__/UpdateMJDataContextItem.oldValues.test.ts +220 -0
- package/src/__tests__/ViewOperationsHandler.clauseScreen.test.ts +155 -0
- package/src/__tests__/actionAuthorization.test.ts +130 -0
- package/src/__tests__/adhocTimeoutClamp.test.ts +26 -0
- package/src/__tests__/cacheInvalidationRecordData.test.ts +241 -0
- package/src/__tests__/multiTenancy.postgresRewrite.test.ts +180 -0
- package/src/__tests__/multiTenancy.security.test.ts +51 -0
- package/src/__tests__/multiTenancy.test.ts +1 -1
- package/src/__tests__/newUserRolesDefault.test.ts +81 -0
- package/src/__tests__/postgresqlPoolSettings.test.ts +131 -0
- package/src/__tests__/pushStatusResolver.filter.test.ts +56 -0
- package/src/__tests__/resolverBase.refusalMessage.test.ts +88 -0
- package/src/__tests__/resolverBase.rls.test.ts +2 -0
- package/src/__tests__/translateBracketsToPG.test.ts +20 -0
- package/src/agentSessions/SessionManager.ts +24 -25
- package/src/agentSessions/SessionRunIDVerifier.ts +179 -0
- package/src/agentSessions/index.ts +1 -0
- package/src/auth/actionAuthorization.ts +119 -0
- package/src/auth/index.ts +1 -0
- package/src/auth/scopeLimitedPrincipal.ts +50 -0
- package/src/config.ts +34 -1
- package/src/context.ts +25 -34
- package/src/generated/generated.ts +3262 -3273
- package/src/generic/CacheInvalidationResolver.ts +206 -15
- package/src/generic/FireAndForgetHeartbeat.ts +10 -4
- package/src/generic/PushStatusResolver.ts +79 -4
- package/src/generic/ResolverBase.ts +339 -31
- package/src/generic/RunViewResolver.ts +17 -0
- package/src/index.ts +65 -52
- package/src/multiTenancy/index.ts +24 -2
- package/src/postgresqlCompat.ts +21 -0
- package/src/postgresqlPoolSettings.ts +213 -0
- package/src/realtimeWidget/widgetGuestElevation.ts +2 -2
- package/src/resolvers/ActionResolver.ts +85 -22
- package/src/resolvers/AdhocQueryResolver.ts +110 -177
- package/src/resolvers/DatasetResolver.ts +70 -10
- package/src/resolvers/EntityRecordNameResolver.ts +24 -7
- package/src/resolvers/GetDataContextDataResolver.ts +139 -41
- package/src/resolvers/GetDataResolver.ts +14 -1
- package/src/resolvers/MCPResolver.ts +13 -15
- package/src/resolvers/RealtimeClientSessionResolver.ts +82 -50
- package/src/resolvers/RemoteBrowserActionResolver.ts +30 -26
- package/src/resolvers/RunAIAgentResolver.ts +30 -40
- package/src/resolvers/RunTemplateResolver.ts +13 -12
- package/src/resolvers/RunTestResolver.ts +90 -112
- package/src/resolvers/SyncRolesUsersResolver.ts +11 -2
- package/src/resolvers/VersionHistoryResolver.ts +10 -13
- package/src/resolvers/__tests__/GetDataResolver.validation.test.ts +95 -0
- package/src/resolvers/__tests__/ReportResolver.test.ts +2 -0
- package/src/resolvers/adhocTimeout.ts +12 -0
- package/src/rest/ViewOperationsHandler.ts +37 -18
- package/src/services/TaskOrchestrator.ts +17 -17
- package/dist/resolvers/adhoc-query-helpers.d.ts +0 -27
- package/dist/resolvers/adhoc-query-helpers.d.ts.map +0 -1
- package/dist/resolvers/adhoc-query-helpers.js +0 -42
- package/dist/resolvers/adhoc-query-helpers.js.map +0 -1
- package/src/__tests__/adhoc-query-helpers.test.ts +0 -50
- package/src/resolvers/adhoc-query-helpers.ts +0 -50
|
@@ -1,7 +1,57 @@
|
|
|
1
|
-
import { Field, ObjectType, Resolver, Root, Subscription } from 'type-graphql';
|
|
1
|
+
import { Ctx, Field, ObjectType, Resolver, Root, Subscription } from 'type-graphql';
|
|
2
|
+
import { Metadata, UserInfo } from '@memberjunction/core';
|
|
2
3
|
|
|
3
4
|
export const CACHE_INVALIDATION_TOPIC = 'CACHE_INVALIDATION';
|
|
4
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Entities whose rows may ride along with an invalidation event. Empty until the server entry point
|
|
8
|
+
* supplies the configured list, so the default — including in any process that never configures it
|
|
9
|
+
* — is to broadcast nothing.
|
|
10
|
+
*/
|
|
11
|
+
let allowedRecordDataEntities: readonly string[] = [];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Supply the configured allowlist. Called once from the server entry point during bootstrap.
|
|
15
|
+
*
|
|
16
|
+
* INJECTED rather than read from `configInfo` here, deliberately. `config.ts` runs `loadConfig()` at
|
|
17
|
+
* module scope, so importing it from this module would drag full config validation into every
|
|
18
|
+
* import chain that touches the resolver — including unit tests, which have no server config and
|
|
19
|
+
* would fail at import with "Configuration validation failed". This module deliberately imports
|
|
20
|
+
* nothing from MJServer itself — only type-graphql and `@memberjunction/core` (for the entity
|
|
21
|
+
* permission check in the subscription filter), neither of which loads config.
|
|
22
|
+
*/
|
|
23
|
+
export function ConfigureRecordDataBroadcast(entities: readonly string[] | undefined | null): void {
|
|
24
|
+
// Normalised HERE, once, rather than at each comparison. The wildcard used to be matched raw
|
|
25
|
+
// (`includes('*')`) while entity names were trimmed and lowercased two lines below, so a
|
|
26
|
+
// hand-edited `[' * ']` matched neither branch and silently opted nothing in — a config that
|
|
27
|
+
// looks like it disables the gate while leaving it fully on. Empty entries are dropped so a
|
|
28
|
+
// stray `''` cannot match an entity whose name is somehow blank.
|
|
29
|
+
allowedRecordDataEntities = (entities ?? [])
|
|
30
|
+
.map((e) => e.trim().toLowerCase())
|
|
31
|
+
.filter((e) => e.length > 0);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* May the full row of `entityName` be broadcast alongside its cache-invalidation event?
|
|
36
|
+
*
|
|
37
|
+
* The subscription below is filtered only by ENTITY-level read permission, so anything included
|
|
38
|
+
* here is disclosed to every signed-in session that may read the entity at all — regardless of
|
|
39
|
+
* row-level security or any tenant scoping a consumer applies on the read path, neither of which
|
|
40
|
+
* this push traverses. The answer is therefore NO unless a deployment has explicitly opted the
|
|
41
|
+
* entity in via `cacheSettings.recordDataBroadcastEntities`.
|
|
42
|
+
*
|
|
43
|
+
* Callers that get `false` simply omit `recordData`; the event still carries the entity name and
|
|
44
|
+
* primary key, so clients evict and re-fetch through the normal access-controlled read path.
|
|
45
|
+
*
|
|
46
|
+
* Matching is exact (trimmed, case-insensitive) rather than substring: a loose match would let an
|
|
47
|
+
* allowlisted name drag in every entity whose name contains it.
|
|
48
|
+
*/
|
|
49
|
+
export function MayBroadcastRecordData(entityName: string): boolean {
|
|
50
|
+
if (allowedRecordDataEntities.length === 0) return false;
|
|
51
|
+
if (allowedRecordDataEntities.includes('*')) return true;
|
|
52
|
+
return allowedRecordDataEntities.includes(entityName.trim().toLowerCase());
|
|
53
|
+
}
|
|
54
|
+
|
|
5
55
|
@ObjectType()
|
|
6
56
|
export class CacheInvalidationNotification {
|
|
7
57
|
@Field(() => String)
|
|
@@ -40,27 +90,168 @@ export interface CacheInvalidationPayload {
|
|
|
40
90
|
recordData?: string;
|
|
41
91
|
}
|
|
42
92
|
|
|
93
|
+
/**
|
|
94
|
+
* What the subscription filter needs off the WS connection.
|
|
95
|
+
*
|
|
96
|
+
* Declared structurally rather than importing MJServer's `UserPayload`, which reaches `types.ts`
|
|
97
|
+
* and through it `auth/index.js` and config. Keeping this module's imports minimal is not fussiness
|
|
98
|
+
* — importing `configInfo` here is what previously broke every unit test that touched the resolver,
|
|
99
|
+
* and the import-surface test in this package exists to keep it that way.
|
|
100
|
+
*/
|
|
101
|
+
export interface CacheInvalidationFilterContext {
|
|
102
|
+
userPayload?: { userRecord?: unknown };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Should this subscriber hear about this event at all?
|
|
107
|
+
*
|
|
108
|
+
* Entity-level narrowing, raised in review: an event for an entity a session has no read
|
|
109
|
+
* permission on tells that session the record exists, its stable key, and when it changed — and
|
|
110
|
+
* the session could never have read it anyway, so nothing is lost by withholding it. This is a
|
|
111
|
+
* monotonic reduction in disclosure that needs no tenancy model, which is why it is here and the
|
|
112
|
+
* row-level question is not.
|
|
113
|
+
*
|
|
114
|
+
* What it does NOT close: two sessions that both hold read permission on an entity still see each
|
|
115
|
+
* other's keys, because what separates them is row-level. That predicate is tracked separately —
|
|
116
|
+
* see the class docblock below.
|
|
117
|
+
*
|
|
118
|
+
* `GetUserPermisions` is an in-memory match of the entity's permission rows against the user's
|
|
119
|
+
* roles, so this stays cheap enough to run per subscriber per event. It is not free: it runs for
|
|
120
|
+
* every connected socket on every save, so keep it free of I/O.
|
|
121
|
+
*/
|
|
122
|
+
export function cacheInvalidationFilter(data: {
|
|
123
|
+
payload: CacheInvalidationPayload;
|
|
124
|
+
context: CacheInvalidationFilterContext | undefined;
|
|
125
|
+
}): boolean {
|
|
126
|
+
const user = data.context?.userPayload?.userRecord as UserInfo | undefined;
|
|
127
|
+
// FAIL CLOSED on identity, matching `statusUpdatesFilter`. `onConnect` rejects a socket whose
|
|
128
|
+
// token does not validate, so a subscriber with no user record should not exist; if one does,
|
|
129
|
+
// it is the case least deserving of an unfiltered firehose.
|
|
130
|
+
if (!user) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const entityName = data.payload?.entityName;
|
|
135
|
+
if (!entityName) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// A pub/sub subscription filter has no per-request provider to thread: it runs on the server's
|
|
140
|
+
// own metadata, which is the one place the entity's permission rows can be answered from.
|
|
141
|
+
const entity = new Metadata().EntityByName(entityName); // global-provider-ok: subscription filter — server-wide metadata, no request-scoped provider exists here
|
|
142
|
+
// DELIVER when the entity is unknown here, deliberately — the opposite of the choice above.
|
|
143
|
+
// A name absent from metadata cannot be permission-checked, and withholding would silently
|
|
144
|
+
// stop invalidating a legitimately cacheable entity, which is a correctness bug rather than a
|
|
145
|
+
// disclosure one. All it discloses is the name and key of something this server cannot
|
|
146
|
+
// describe.
|
|
147
|
+
if (!entity) {
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return entity.GetUserPermisions(user).CanRead;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The part of a permission row and of a user this module reads, so the rule can be tested on plain objects. */
|
|
155
|
+
export interface ReadPermissionRow {
|
|
156
|
+
RoleID: string;
|
|
157
|
+
CanRead: boolean | null;
|
|
158
|
+
/** `Deny` rows subtract; they never carry the read the rule looks at. */
|
|
159
|
+
IsDeny?: boolean;
|
|
160
|
+
Type?: string | null;
|
|
161
|
+
ReadRLSFilterID: string | null;
|
|
162
|
+
}
|
|
163
|
+
export interface RoleHolder {
|
|
164
|
+
UserRoles?: ReadonlyArray<{ RoleID: string }> | null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Is this user's read of the entity row-filtered? True when every read the user holds on the entity comes through a permission
|
|
169
|
+
* row that carries a `ReadRLSFilterID`: there is a row-level security filter between them and the rows, so a record's key, its
|
|
170
|
+
* existence and when it changed are not theirs to learn unless the row passes the filter — which a subscription filter cannot
|
|
171
|
+
* evaluate per event. False when any of their read rows is unfiltered (they could read the row anyway), and false when they hold
|
|
172
|
+
* no read at all (the entity-level filter has already withheld the event).
|
|
173
|
+
*
|
|
174
|
+
* The same in-memory match `GetUserPermisions` does, with no I/O, so it can run per subscriber per event.
|
|
175
|
+
*/
|
|
176
|
+
export function ReadIsRowFiltered(entity: { Permissions: ReadonlyArray<ReadPermissionRow> }, user: RoleHolder): boolean {
|
|
177
|
+
const roles = new Set((user.UserRoles ?? []).map((role) => (role.RoleID ?? '').trim().toLowerCase()));
|
|
178
|
+
let filteredReads = 0;
|
|
179
|
+
for (const row of entity.Permissions) {
|
|
180
|
+
if (!row.CanRead) continue;
|
|
181
|
+
const isDeny = row.IsDeny ?? (row.Type ?? 'Allow').trim().toLowerCase() === 'deny';
|
|
182
|
+
if (isDeny) continue;
|
|
183
|
+
if (!roles.has((row.RoleID ?? '').trim().toLowerCase())) continue;
|
|
184
|
+
if (!row.ReadRLSFilterID) return false;
|
|
185
|
+
filteredReads += 1;
|
|
186
|
+
}
|
|
187
|
+
return filteredReads > 0;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The event as one subscriber may see it. A subscriber whose read of the entity is row-filtered gets the entity, the action and
|
|
192
|
+
* the time, and neither the key nor the row: "something in this entity changed", which the client already takes as a
|
|
193
|
+
* whole-entity invalidation (a null key on `remote-invalidate`). Everyone else gets the event as published.
|
|
194
|
+
*/
|
|
195
|
+
export function ShapeCacheInvalidationEvent(
|
|
196
|
+
payload: CacheInvalidationPayload,
|
|
197
|
+
user: RoleHolder | undefined,
|
|
198
|
+
entity: { Permissions: ReadonlyArray<ReadPermissionRow> } | null | undefined,
|
|
199
|
+
): CacheInvalidationNotification {
|
|
200
|
+
const withholdKey = !!user && !!entity && ReadIsRowFiltered(entity, user);
|
|
201
|
+
return {
|
|
202
|
+
EntityName: payload.entityName,
|
|
203
|
+
PrimaryKeyValues: withholdKey ? undefined : (payload.primaryKeyValues ?? undefined),
|
|
204
|
+
Action: payload.action,
|
|
205
|
+
SourceServerID: payload.sourceServerId,
|
|
206
|
+
Timestamp: payload.timestamp,
|
|
207
|
+
OriginSessionID: payload.originSessionId ?? undefined,
|
|
208
|
+
RecordData: withholdKey ? undefined : (payload.recordData ?? undefined),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
43
212
|
@Resolver()
|
|
44
213
|
export class CacheInvalidationResolver {
|
|
45
214
|
/**
|
|
46
|
-
* Subscription that broadcasts cache invalidation events to
|
|
47
|
-
*
|
|
48
|
-
*
|
|
215
|
+
* Subscription that broadcasts cache invalidation events to connected clients, so cross-server
|
|
216
|
+
* cache invalidation reaches browsers.
|
|
217
|
+
*
|
|
218
|
+
* Delivery is filtered only by ENTITY-level read permission (see {@link cacheInvalidationFilter}).
|
|
219
|
+
* Every session that may read an entity at all still receives every event for it, from every
|
|
220
|
+
* other session and tenant.
|
|
221
|
+
*
|
|
222
|
+
* Because that narrowing stops well short of row level, `RecordData` is only populated for entities a
|
|
223
|
+
* deployment has opted in through `cacheSettings.recordDataBroadcastEntities` — see
|
|
224
|
+
* {@link MayBroadcastRecordData}, applied at both publish sites.
|
|
225
|
+
*
|
|
226
|
+
* `EntityName`, `PrimaryKeyValues`, `Action` and `Timestamp` ARE still broadcast to every
|
|
227
|
+
* session, and that is a disclosure, not a safe residue. An earlier version of this comment
|
|
228
|
+
* called them "safe to broadcast" on the grounds that reading the record still goes through
|
|
229
|
+
* the access-controlled path — true of the row's CONTENTS, and beside the point for the
|
|
230
|
+
* metadata itself. A session with no relationship to a record still learns that it exists,
|
|
231
|
+
* its stable key, and every time it changes; across entities it learns the cadence and volume
|
|
232
|
+
* of another tenant's activity.
|
|
233
|
+
*
|
|
234
|
+
* The key is kept for a subscriber who could read the row anyway, because consumers need it to
|
|
235
|
+
* re-read the record they were told about (see `ResolveEntityEventKey` in @memberjunction/core),
|
|
236
|
+
* and the coarse "something in this entity changed" forces whole-entity invalidation on the
|
|
237
|
+
* hottest write paths. It is withheld, with the row, from a subscriber whose every read of the
|
|
238
|
+
* entity goes through a row-level security filter ({@link ReadIsRowFiltered}): a filter the
|
|
239
|
+
* subscription cannot evaluate per event stands between them and the row, so they learn only
|
|
240
|
+
* that a row of the entity changed, and their caches refresh the entity as a whole. That is the
|
|
241
|
+
* same in-memory role match as the entity-level filter, once more per subscriber per event.
|
|
49
242
|
*/
|
|
50
243
|
@Subscription(() => CacheInvalidationNotification, {
|
|
51
244
|
topics: CACHE_INVALIDATION_TOPIC,
|
|
245
|
+
filter: (data: { payload: CacheInvalidationPayload; context: CacheInvalidationFilterContext | undefined }) =>
|
|
246
|
+
cacheInvalidationFilter(data),
|
|
52
247
|
})
|
|
53
|
-
cacheInvalidation(
|
|
54
|
-
@Root() payload: CacheInvalidationPayload
|
|
248
|
+
cacheInvalidation( // case-violation-ok-legacy-back-compat: the property name is the GraphQL schema field name — renaming it breaks every client query
|
|
249
|
+
@Root() payload: CacheInvalidationPayload,
|
|
250
|
+
@Ctx() context: CacheInvalidationFilterContext | undefined
|
|
55
251
|
): CacheInvalidationNotification {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
SourceServerID: payload.sourceServerId,
|
|
61
|
-
Timestamp: payload.timestamp,
|
|
62
|
-
OriginSessionID: payload.originSessionId ?? undefined,
|
|
63
|
-
RecordData: payload.recordData ?? undefined,
|
|
64
|
-
};
|
|
252
|
+
const user = context?.userPayload?.userRecord as UserInfo | undefined;
|
|
253
|
+
// The same server-wide metadata the filter read; an entity unknown here was delivered whole by the filter's own choice
|
|
254
|
+
const entity = payload?.entityName ? new Metadata().EntityByName(payload.entityName) : null; // global-provider-ok: subscription resolver — server-wide metadata, no request-scoped provider exists here
|
|
255
|
+
return ShapeCacheInvalidationEvent(payload, user, entity);
|
|
65
256
|
}
|
|
66
257
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PubSubEngine } from 'type-graphql';
|
|
2
2
|
import { LogError } from '@memberjunction/core';
|
|
3
|
-
import {
|
|
3
|
+
import { publishStatusUpdate } from './PushStatusResolver.js';
|
|
4
4
|
|
|
5
5
|
/** Default cadence for fire-and-forget liveness pulses (5 minutes). */
|
|
6
6
|
export const DEFAULT_PULSE_INTERVAL_MS = 5 * 60 * 1000;
|
|
@@ -32,6 +32,11 @@ export interface LivenessPulseOptions {
|
|
|
32
32
|
pubSub: PubSubEngine;
|
|
33
33
|
/** Session the client is subscribed on (used by the subscription filter). */
|
|
34
34
|
sessionId: string;
|
|
35
|
+
/**
|
|
36
|
+
* Authenticated user the operation belongs to (B49). Stamped on every pulse so the
|
|
37
|
+
* subscription filter binds delivery to identity, not just the client-chosen sessionId.
|
|
38
|
+
*/
|
|
39
|
+
ownerUserId: string;
|
|
35
40
|
/** Resolver label echoed in the message envelope (e.g. 'RunAIAgentResolver'). */
|
|
36
41
|
resolver: string;
|
|
37
42
|
/** Pulse cadence in ms. Defaults to {@link DEFAULT_PULSE_INTERVAL_MS}. */
|
|
@@ -56,7 +61,7 @@ export interface LivenessPulseOptions {
|
|
|
56
61
|
* so the client receives it through the same subscription with no special parsing.
|
|
57
62
|
*/
|
|
58
63
|
export function startLivenessPulse(options: LivenessPulseOptions): LivenessPulseHandle {
|
|
59
|
-
const { pubSub, sessionId, resolver, readStatus } = options;
|
|
64
|
+
const { pubSub, sessionId, ownerUserId, resolver, readStatus } = options;
|
|
60
65
|
const intervalMs = options.intervalMs ?? DEFAULT_PULSE_INTERVAL_MS;
|
|
61
66
|
|
|
62
67
|
const timer = setInterval(() => {
|
|
@@ -68,14 +73,15 @@ export function startLivenessPulse(options: LivenessPulseOptions): LivenessPulse
|
|
|
68
73
|
LogError(`[LivenessPulse:${resolver}] readStatus failed: ${(e as Error).message}`);
|
|
69
74
|
}
|
|
70
75
|
|
|
71
|
-
pubSub
|
|
76
|
+
publishStatusUpdate(pubSub, {
|
|
77
|
+
sessionId,
|
|
78
|
+
ownerUserId,
|
|
72
79
|
message: JSON.stringify({
|
|
73
80
|
resolver,
|
|
74
81
|
type: HEARTBEAT_MESSAGE_TYPE,
|
|
75
82
|
status: 'ok',
|
|
76
83
|
data: data ?? {},
|
|
77
84
|
}),
|
|
78
|
-
sessionId,
|
|
79
85
|
});
|
|
80
86
|
}, intervalMs);
|
|
81
87
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { Arg, Field, ID, ObjectType, Resolver, ResolverFilterData, Root, Subscription } from 'type-graphql';
|
|
1
|
+
import { Arg, Field, ID, ObjectType, PubSubEngine, Resolver, ResolverFilterData, Root, Subscription } from 'type-graphql';
|
|
2
|
+
import { UUIDsEqual } from '@memberjunction/global';
|
|
3
|
+
import { UserPayload } from '../types.js';
|
|
2
4
|
|
|
3
5
|
export const PUSH_STATUS_UPDATES_TOPIC = 'PUSH_STATUS_UPDATES';
|
|
4
6
|
|
|
@@ -14,27 +16,100 @@ export class PushStatusNotification {
|
|
|
14
16
|
sessionId!: string;
|
|
15
17
|
}
|
|
16
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Internal wire payload for a push-status update. Carries `ownerUserId` — the authenticated user
|
|
21
|
+
* the update belongs to — used ONLY server-side by the subscription filter and deliberately NOT
|
|
22
|
+
* exposed on the client-facing {@link PushStatusNotification} (we never leak user IDs to subscribers).
|
|
23
|
+
*
|
|
24
|
+
* `ownerUserId` is REQUIRED: it is the load-bearing security property (see
|
|
25
|
+
* {@link statusUpdatesFilter}), so making it required means the compiler rejects any publish that
|
|
26
|
+
* omits identity. Always publish through {@link publishStatusUpdate} (or the
|
|
27
|
+
* `ResolverBase.PublishStatusUpdate` ergonomic wrapper) rather than calling `pubSub.publish` on
|
|
28
|
+
* this topic directly, so a new publisher can never forget it.
|
|
29
|
+
*/
|
|
17
30
|
export interface PushStatusNotificationPayload {
|
|
18
31
|
message?: string;
|
|
19
32
|
sessionId: string;
|
|
33
|
+
/** Authenticated user the update belongs to. Server-side filter key; never sent to the client. */
|
|
34
|
+
ownerUserId: string;
|
|
20
35
|
}
|
|
21
36
|
|
|
22
37
|
interface PushStatusNotificationArgs {
|
|
23
38
|
sessionId: string;
|
|
24
39
|
}
|
|
25
40
|
|
|
41
|
+
/** Parameters for {@link publishStatusUpdate} — identity (`ownerUserId`) is required by design. */
|
|
42
|
+
export interface StatusUpdateParams {
|
|
43
|
+
/** Session the target client subscribed on (routes the push to the right browser tab). */
|
|
44
|
+
sessionId: string;
|
|
45
|
+
/** Authenticated user the operation belongs to (the trust anchor the filter enforces). */
|
|
46
|
+
ownerUserId: string;
|
|
47
|
+
/** Message envelope (plain string, or the resolvers' JSON `{resolver,type,status,data}` shape). */
|
|
48
|
+
message?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The single, centralized vehicle for publishing on {@link PUSH_STATUS_UPDATES_TOPIC}. Every
|
|
53
|
+
* publisher — resolver or service — routes through here (resolvers via the
|
|
54
|
+
* `ResolverBase.PublishStatusUpdate` wrapper; non-resolvers by calling this directly), so the
|
|
55
|
+
* payload shape and the required-identity guarantee live in exactly one place. Adding a field to
|
|
56
|
+
* the push is a one-line change here; a new publisher physically cannot omit `ownerUserId`.
|
|
57
|
+
*/
|
|
58
|
+
export function publishStatusUpdate(pubSub: PubSubEngine, params: StatusUpdateParams): void {
|
|
59
|
+
const payload: PushStatusNotificationPayload = {
|
|
60
|
+
sessionId: params.sessionId,
|
|
61
|
+
ownerUserId: params.ownerUserId,
|
|
62
|
+
message: params.message,
|
|
63
|
+
};
|
|
64
|
+
pubSub.publish(PUSH_STATUS_UPDATES_TOPIC, payload);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Minimal shape of the subscription's connection context needed by the filter. */
|
|
68
|
+
export interface StatusUpdatesFilterContext {
|
|
69
|
+
userPayload?: UserPayload;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Filter predicate for the `statusUpdates` subscription. Exported so it can be unit-tested in
|
|
74
|
+
* isolation (it is the security-critical gate). A push reaches a subscriber only when BOTH hold:
|
|
75
|
+
* 1. the push's `sessionId` matches the subscriber's requested `sessionId` (tab routing), AND
|
|
76
|
+
* 2. the push's `ownerUserId` matches the subscriber CONNECTION's authenticated identity
|
|
77
|
+
* (`context.userPayload.userRecord.ID`, established once at WS connect).
|
|
78
|
+
*
|
|
79
|
+
* Condition (2) is the fix for the session-hijack class (B49): `sessionId` is a client-chosen
|
|
80
|
+
* correlation value, so a subscriber who lifts another user's `sessionId` would previously receive
|
|
81
|
+
* their pushes. Binding delivery to the connection's server-authenticated identity means knowing a
|
|
82
|
+
* `sessionId` is no longer sufficient. Fails CLOSED — a missing owner or connection identity never
|
|
83
|
+
* matches.
|
|
84
|
+
*/
|
|
85
|
+
export function statusUpdatesFilter(data: {
|
|
86
|
+
payload: PushStatusNotificationPayload;
|
|
87
|
+
args: PushStatusNotificationArgs;
|
|
88
|
+
context: StatusUpdatesFilterContext | undefined;
|
|
89
|
+
}): boolean {
|
|
90
|
+
const { payload, args, context } = data;
|
|
91
|
+
if (payload.sessionId !== args.sessionId) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
const connectionUserId: string | undefined = context?.userPayload?.userRecord?.ID;
|
|
95
|
+
if (!connectionUserId || !payload.ownerUserId) {
|
|
96
|
+
return false; // fail closed — no identity on either side means no delivery
|
|
97
|
+
}
|
|
98
|
+
return UUIDsEqual(payload.ownerUserId, connectionUserId);
|
|
99
|
+
}
|
|
100
|
+
|
|
26
101
|
@Resolver()
|
|
27
102
|
export class PushStatusResolver {
|
|
28
103
|
@Subscription(() => PushStatusNotification, {
|
|
29
104
|
topics: PUSH_STATUS_UPDATES_TOPIC,
|
|
30
|
-
filter: (
|
|
31
|
-
|
|
32
|
-
},
|
|
105
|
+
filter: (data: ResolverFilterData<PushStatusNotificationPayload, PushStatusNotificationArgs, StatusUpdatesFilterContext>) =>
|
|
106
|
+
statusUpdatesFilter(data),
|
|
33
107
|
})
|
|
34
108
|
statusUpdates(
|
|
35
109
|
@Root() { message }: PushStatusNotificationPayload,
|
|
36
110
|
@Arg('sessionId', () => String) sessionId: string
|
|
37
111
|
): PushStatusNotification {
|
|
112
|
+
// NOTE: ownerUserId is intentionally NOT returned — it is a server-side filter key only.
|
|
38
113
|
return { message, date: new Date(), sessionId };
|
|
39
114
|
}
|
|
40
115
|
}
|