@terreno/api 57.16.1 → 57.17.0
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/actions.d.ts +26 -3
- package/dist/actions.js +102 -16
- package/dist/api.d.ts +7 -0
- package/dist/api.js +59 -3
- package/dist/auth.d.ts +28 -4
- package/dist/auth.js +140 -15
- package/dist/authRecovery.d.ts +31 -0
- package/dist/authRecovery.js +86 -6
- package/dist/betterAuth.d.ts +86 -0
- package/dist/betterAuthApp.d.ts +17 -0
- package/dist/betterAuthApp.js +12 -1
- package/dist/betterAuthOAuthHandoff.d.ts +24 -0
- package/dist/betterAuthOAuthHandoff.js +305 -0
- package/dist/betterAuthSetup.d.ts +37 -2
- package/dist/betterAuthSetup.js +304 -24
- package/dist/expressServer.d.ts +16 -0
- package/dist/expressServer.js +61 -14
- package/dist/githubAuth.d.ts +9 -0
- package/dist/githubAuth.js +230 -31
- package/dist/index.d.ts +9 -1
- package/dist/index.js +27 -3
- package/dist/mcp/actionTools.d.ts +4 -0
- package/dist/mcp/actionTools.js +158 -0
- package/dist/mcp/auth.d.ts +30 -0
- package/dist/mcp/auth.js +83 -28
- package/dist/mcp/createMCPRequest.d.ts +5 -1
- package/dist/mcp/createMCPRequest.js +3 -3
- package/dist/mcp/handlers.d.ts +3 -0
- package/dist/mcp/handlers.js +43 -27
- package/dist/mcp/oauth.d.ts +14 -0
- package/dist/mcp/oauth.js +618 -0
- package/dist/mcp/oauthMetadata.d.ts +89 -0
- package/dist/mcp/oauthMetadata.js +137 -0
- package/dist/mcp/registry.d.ts +5 -0
- package/dist/mcp/registry.js +9 -1
- package/dist/mcp/server.d.ts +6 -0
- package/dist/mcp/server.js +44 -7
- package/dist/mcp/serviceTokens.js +12 -2
- package/dist/mcp/toolExecution.d.ts +12 -0
- package/dist/mcp/toolExecution.js +60 -0
- package/dist/mcp/toolGenerator.d.ts +4 -1
- package/dist/mcp/toolGenerator.js +26 -60
- package/dist/mcp/types.d.ts +4 -4
- package/dist/models/mcpOAuthAuthorization.d.ts +2 -0
- package/dist/models/mcpOAuthAuthorization.js +86 -0
- package/dist/models/mcpOAuthClient.d.ts +2 -0
- package/dist/models/mcpOAuthClient.js +42 -0
- package/dist/models/mcpOAuthRevokedFamily.d.ts +2 -0
- package/dist/models/mcpOAuthRevokedFamily.js +36 -0
- package/dist/models/mcpOAuthToken.d.ts +12 -0
- package/dist/models/mcpOAuthToken.js +159 -0
- package/dist/models/mcpServiceToken.d.ts +1 -0
- package/dist/models/mcpServiceToken.js +21 -4
- package/dist/permissions.d.ts +7 -0
- package/dist/permissions.js +18 -1
- package/dist/rateLimit/policies.d.ts +4 -0
- package/dist/rateLimit/policies.js +46 -7
- package/dist/rbac/access.js +1 -0
- package/dist/rbac/membershipOrgAdminSource.d.ts +4 -0
- package/dist/rbac/membershipOrgAdminSource.js +5 -0
- package/dist/rbac/modelRouterAccess.d.ts +7 -1
- package/dist/rbac/modelRouterAccess.js +37 -24
- package/dist/rbac/realtimeAccess.js +17 -3
- package/dist/rbac/resolve.js +66 -9
- package/dist/rbac/roleManager.js +60 -5
- package/dist/rbac/roleModel.js +1 -1
- package/dist/rbac/statements.d.ts +1 -1
- package/dist/rbac/statements.js +13 -1
- package/dist/rbac/types.d.ts +12 -0
- package/dist/rbac/userWriteGuards.d.ts +50 -0
- package/dist/rbac/userWriteGuards.js +156 -0
- package/dist/realtime/changeStreamWatcher.d.ts +4 -1
- package/dist/realtime/changeStreamWatcher.js +157 -73
- package/dist/realtime/queryMatcher.d.ts +3 -0
- package/dist/realtime/queryMatcher.js +25 -10
- package/dist/realtime/realtimeApp.d.ts +16 -1
- package/dist/realtime/realtimeApp.js +76 -8
- package/dist/realtime/socketAuth.d.ts +17 -6
- package/dist/realtime/socketAuth.js +77 -16
- package/dist/realtime/types.d.ts +8 -5
- package/dist/requestContext.d.ts +22 -0
- package/dist/requestContext.js +22 -1
- package/dist/seedRunner.js +3 -1
- package/dist/sortFields.d.ts +33 -0
- package/dist/sortFields.js +55 -0
- package/dist/sync/executors.js +140 -2
- package/dist/sync/models.d.ts +2 -0
- package/dist/sync/models.js +5 -0
- package/dist/sync/mutationHandler.js +70 -7
- package/dist/sync/routes.js +4 -2
- package/dist/sync/syncSeqPlugin.js +6 -5
- package/dist/terrenoApp.d.ts +14 -0
- package/dist/terrenoApp.js +21 -6
- package/dist/types/mcpOAuth.d.ts +101 -0
- package/dist/types/mcpOAuth.js +2 -0
- package/dist/types/mcpServiceToken.d.ts +8 -0
- package/package.json +3 -3
- package/src/actions.test.ts +118 -0
- package/src/actions.ts +139 -17
- package/src/api.arrayOperations.test.ts +78 -0
- package/src/api.test.ts +36 -0
- package/src/api.ts +76 -4
- package/src/auth.test.ts +521 -3
- package/src/auth.ts +171 -17
- package/src/authRecovery.test.ts +99 -0
- package/src/authRecovery.ts +117 -5
- package/src/authVerification.test.ts +3 -3
- package/src/betterAuth.ts +104 -1
- package/src/betterAuthApp.test.ts +634 -1
- package/src/betterAuthApp.ts +28 -0
- package/src/betterAuthOAuthHandoff.test.ts +946 -0
- package/src/betterAuthOAuthHandoff.ts +375 -0
- package/src/betterAuthSetup.test.ts +393 -4
- package/src/betterAuthSetup.ts +402 -29
- package/src/expressServer.test.ts +93 -0
- package/src/expressServer.ts +81 -14
- package/src/githubAuth.test.ts +448 -6
- package/src/githubAuth.ts +276 -34
- package/src/index.ts +35 -1
- package/src/mcp/actionTools.test.ts +498 -0
- package/src/mcp/actionTools.ts +220 -0
- package/src/mcp/auth.test.ts +114 -1
- package/src/mcp/auth.ts +122 -35
- package/src/mcp/createMCPRequest.ts +8 -2
- package/src/mcp/handlers.test.ts +27 -0
- package/src/mcp/handlers.ts +21 -6
- package/src/mcp/oauth.test.ts +1026 -0
- package/src/mcp/oauth.ts +798 -0
- package/src/mcp/oauthMetadata.ts +234 -0
- package/src/mcp/registry.ts +11 -0
- package/src/mcp/server.ts +54 -14
- package/src/mcp/serviceTokens.ts +16 -3
- package/src/mcp/toolExecution.ts +67 -0
- package/src/mcp/toolGenerator.ts +27 -83
- package/src/mcp/types.ts +4 -4
- package/src/models/mcpOAuthAuthorization.ts +97 -0
- package/src/models/mcpOAuthClient.ts +49 -0
- package/src/models/mcpOAuthRevokedFamily.ts +47 -0
- package/src/models/mcpOAuthToken.test.ts +82 -0
- package/src/models/mcpOAuthToken.ts +195 -0
- package/src/models/mcpServiceToken.ts +26 -2
- package/src/permissions.ts +17 -0
- package/src/rateLimit/policies.ts +54 -10
- package/src/rateLimit/rateLimit.test.ts +41 -0
- package/src/rbac/access.ts +1 -0
- package/src/rbac/membershipOrgAdminSource.ts +5 -0
- package/src/rbac/modelRouterAccess.ts +40 -23
- package/src/rbac/realtimeAccess.test.ts +43 -0
- package/src/rbac/realtimeAccess.ts +23 -3
- package/src/rbac/resolve.test.ts +179 -1
- package/src/rbac/resolve.ts +78 -9
- package/src/rbac/roleManager.test.ts +132 -0
- package/src/rbac/roleManager.ts +69 -5
- package/src/rbac/roleModel.ts +1 -1
- package/src/rbac/statements.ts +14 -1
- package/src/rbac/types.ts +12 -0
- package/src/rbac/userWriteGuards.test.ts +407 -0
- package/src/rbac/userWriteGuards.ts +185 -0
- package/src/realtime/changeStreamWatcher.ts +203 -75
- package/src/realtime/queryMatcher.ts +30 -13
- package/src/realtime/realtime.test.ts +132 -0
- package/src/realtime/realtimeApp.ts +94 -10
- package/src/realtime/realtimeAppSetup.test.ts +282 -1
- package/src/realtime/socketAuth.test.ts +140 -8
- package/src/realtime/socketAuth.ts +90 -19
- package/src/realtime/types.ts +8 -5
- package/src/requestContext.ts +52 -1
- package/src/requestCredential.test.ts +89 -0
- package/src/seedRunner.test.ts +21 -0
- package/src/seedRunner.ts +3 -1
- package/src/sortFields.ts +69 -0
- package/src/sync/executors.test.ts +382 -3
- package/src/sync/executors.ts +179 -4
- package/src/sync/models.ts +8 -0
- package/src/sync/mutationHandler.test.ts +166 -2
- package/src/sync/mutationHandler.ts +97 -5
- package/src/sync/routes.ts +4 -2
- package/src/sync/syncFrontier.test.ts +11 -0
- package/src/sync/syncPhaseC.test.ts +18 -0
- package/src/sync/syncSeqPlugin.ts +6 -5
- package/src/sync/syncSocket.test.ts +92 -0
- package/src/terrenoApp.ts +36 -21
- package/src/types/mcpOAuth.ts +129 -0
- package/src/types/mcpServiceToken.ts +6 -1
|
@@ -43,6 +43,17 @@ mutStuffSchema.plugin(createdUpdatedPlugin);
|
|
|
43
43
|
mutStuffSchema.plugin(syncPlugin);
|
|
44
44
|
const MutStuffModel = model<MutStuff>("MutStuff", mutStuffSchema);
|
|
45
45
|
|
|
46
|
+
// A second synced collection, for ledger replays that name a different collection.
|
|
47
|
+
const mutOtherSchema = new Schema<MutStuff>({
|
|
48
|
+
_id: {description: "Client-minted string id", type: String},
|
|
49
|
+
name: {description: "The name of the item", required: true, type: String},
|
|
50
|
+
ownerId: {description: "The user who owns this item", type: String},
|
|
51
|
+
});
|
|
52
|
+
mutOtherSchema.plugin(isDeletedPlugin);
|
|
53
|
+
mutOtherSchema.plugin(createdUpdatedPlugin);
|
|
54
|
+
mutOtherSchema.plugin(syncPlugin);
|
|
55
|
+
const MutOtherModel = model<MutStuff>("MutOther", mutOtherSchema);
|
|
56
|
+
|
|
46
57
|
const owner = {_id: "mut-owner", admin: false, id: "mut-owner"} as unknown as User;
|
|
47
58
|
const stranger = {_id: "mut-stranger", admin: false, id: "mut-stranger"} as unknown as User;
|
|
48
59
|
|
|
@@ -56,6 +67,17 @@ const ownerOptions: ModelRouterOptions<MutStuff> = {
|
|
|
56
67
|
},
|
|
57
68
|
};
|
|
58
69
|
|
|
70
|
+
// For broadcast and tenant collections, where `ownerId` is not an owner binding.
|
|
71
|
+
const sharedOptions: ModelRouterOptions<MutStuff> = {
|
|
72
|
+
permissions: {
|
|
73
|
+
create: [Permissions.IsAuthenticated],
|
|
74
|
+
delete: [Permissions.IsAuthenticated],
|
|
75
|
+
list: [Permissions.IsAny],
|
|
76
|
+
read: [Permissions.IsAny],
|
|
77
|
+
update: [Permissions.IsAuthenticated],
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
|
|
59
81
|
const registerMutStuff = (
|
|
60
82
|
overrides: {config?: Partial<SyncConfig>; options?: ModelRouterOptions<MutStuff>} = {}
|
|
61
83
|
): void => {
|
|
@@ -181,6 +203,45 @@ describe("applySyncMutation", () => {
|
|
|
181
203
|
const tombstones = await MutStuffModel.find({_id: doc._id, deleted: true});
|
|
182
204
|
expect(tombstones).toHaveLength(1);
|
|
183
205
|
});
|
|
206
|
+
|
|
207
|
+
it("ignores client-sent sync bookkeeping fields on update", async () => {
|
|
208
|
+
const doc = await MutStuffModel.create({name: "original", ownerId: owner.id});
|
|
209
|
+
const outcome = await applySyncMutation({
|
|
210
|
+
mutation: {
|
|
211
|
+
baseVersion: 1,
|
|
212
|
+
collection: "mutStuff",
|
|
213
|
+
data: {_syncPrevStream: "mutStuff|owner:elsewhere", _syncSeq: 777},
|
|
214
|
+
id: doc._id.toString(),
|
|
215
|
+
mutationId: "m-update-pinned-seq",
|
|
216
|
+
operation: "update",
|
|
217
|
+
},
|
|
218
|
+
user: owner,
|
|
219
|
+
});
|
|
220
|
+
const ack = expectAck(outcome);
|
|
221
|
+
expect(ack.seq).toBe(1);
|
|
222
|
+
const saved = await MutStuffModel.findById(doc._id).lean();
|
|
223
|
+
expect(saved?._syncSeq).toBe(1);
|
|
224
|
+
expect((saved as {_syncPrevStream?: string | null})?._syncPrevStream ?? null).toBeNull();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("stamps a fresh seq when a client-sent seq rides along with a real change", async () => {
|
|
228
|
+
const doc = await MutStuffModel.create({name: "original", ownerId: owner.id});
|
|
229
|
+
const outcome = await applySyncMutation({
|
|
230
|
+
mutation: {
|
|
231
|
+
baseVersion: 1,
|
|
232
|
+
collection: "mutStuff",
|
|
233
|
+
data: {_syncSeq: 999, name: "changed"},
|
|
234
|
+
id: doc._id.toString(),
|
|
235
|
+
mutationId: "m-update-seq-and-name",
|
|
236
|
+
operation: "update",
|
|
237
|
+
},
|
|
238
|
+
user: owner,
|
|
239
|
+
});
|
|
240
|
+
expect(expectAck(outcome).seq).toBe(2);
|
|
241
|
+
const saved = await MutStuffModel.findById(doc._id).lean();
|
|
242
|
+
expect(saved?._syncSeq).toBe(2);
|
|
243
|
+
expect(saved?.name).toBe("changed");
|
|
244
|
+
});
|
|
184
245
|
});
|
|
185
246
|
|
|
186
247
|
describe("idempotency", () => {
|
|
@@ -1084,7 +1145,7 @@ describe("applySyncMutation", () => {
|
|
|
1084
1145
|
|
|
1085
1146
|
describe("write scope enforcement", () => {
|
|
1086
1147
|
it("skips the write-scope check for broadcast collections", async () => {
|
|
1087
|
-
registerMutStuff({config: {scope: {type: "broadcast"}}});
|
|
1148
|
+
registerMutStuff({config: {scope: {type: "broadcast"}}, options: sharedOptions});
|
|
1088
1149
|
const ack = expectAck(
|
|
1089
1150
|
await applySyncMutation({
|
|
1090
1151
|
mutation: {
|
|
@@ -1120,7 +1181,10 @@ describe("applySyncMutation", () => {
|
|
|
1120
1181
|
});
|
|
1121
1182
|
|
|
1122
1183
|
it("acks a tenant-scoped create when the resolver confirms the membership", async () => {
|
|
1123
|
-
registerMutStuff({
|
|
1184
|
+
registerMutStuff({
|
|
1185
|
+
config: {scope: {field: "ownerId", type: "tenant"}},
|
|
1186
|
+
options: sharedOptions,
|
|
1187
|
+
});
|
|
1124
1188
|
const ack = expectAck(
|
|
1125
1189
|
await applySyncMutation({
|
|
1126
1190
|
mutation: {
|
|
@@ -1190,6 +1254,106 @@ describe("applySyncMutation", () => {
|
|
|
1190
1254
|
});
|
|
1191
1255
|
});
|
|
1192
1256
|
|
|
1257
|
+
describe("ledger replay authorization", () => {
|
|
1258
|
+
const attacker = {_id: "mut-attacker", admin: false, id: "mut-attacker"} as unknown as User;
|
|
1259
|
+
const victim = {_id: "mut-victim", admin: false, id: "mut-victim"} as unknown as User;
|
|
1260
|
+
|
|
1261
|
+
beforeEach(async () => {
|
|
1262
|
+
registerSync({
|
|
1263
|
+
config: {scope: {type: "owner"}},
|
|
1264
|
+
model: MutOtherModel,
|
|
1265
|
+
options: ownerOptions,
|
|
1266
|
+
routePath: "/mutOther",
|
|
1267
|
+
});
|
|
1268
|
+
await MutOtherModel.collection.deleteMany({});
|
|
1269
|
+
});
|
|
1270
|
+
|
|
1271
|
+
it("does not replay a recorded outcome for another collection", async () => {
|
|
1272
|
+
const sharedId = new mongoose.Types.ObjectId().toHexString();
|
|
1273
|
+
await MutOtherModel.create({_id: sharedId, name: "DUMMY-victim-secret", ownerId: victim.id});
|
|
1274
|
+
expectAck(
|
|
1275
|
+
await applySyncMutation({
|
|
1276
|
+
mutation: {
|
|
1277
|
+
collection: "mutStuff",
|
|
1278
|
+
data: {name: "attacker own", ownerId: attacker.id},
|
|
1279
|
+
id: sharedId,
|
|
1280
|
+
mutationId: "m-cross-own",
|
|
1281
|
+
operation: "create",
|
|
1282
|
+
},
|
|
1283
|
+
user: attacker,
|
|
1284
|
+
})
|
|
1285
|
+
);
|
|
1286
|
+
const conflict = expectNack(
|
|
1287
|
+
await applySyncMutation({
|
|
1288
|
+
mutation: {
|
|
1289
|
+
baseVersion: 999,
|
|
1290
|
+
collection: "mutStuff",
|
|
1291
|
+
data: {name: "stale"},
|
|
1292
|
+
id: sharedId,
|
|
1293
|
+
mutationId: "m-cross-conflict",
|
|
1294
|
+
operation: "update",
|
|
1295
|
+
},
|
|
1296
|
+
user: attacker,
|
|
1297
|
+
})
|
|
1298
|
+
);
|
|
1299
|
+
expect(conflict.code).toBe("conflict");
|
|
1300
|
+
|
|
1301
|
+
const replayed = expectNack(
|
|
1302
|
+
await applySyncMutation({
|
|
1303
|
+
mutation: {
|
|
1304
|
+
baseVersion: 0,
|
|
1305
|
+
collection: "mutOther",
|
|
1306
|
+
data: {name: "z"},
|
|
1307
|
+
id: "unrelated",
|
|
1308
|
+
mutationId: "m-cross-conflict",
|
|
1309
|
+
operation: "update",
|
|
1310
|
+
},
|
|
1311
|
+
user: attacker,
|
|
1312
|
+
})
|
|
1313
|
+
);
|
|
1314
|
+
expect(replayed.code).toBe("unauthorized");
|
|
1315
|
+
expect(replayed.serverDoc).toBeUndefined();
|
|
1316
|
+
expect(JSON.stringify(replayed)).not.toContain("DUMMY-victim-secret");
|
|
1317
|
+
});
|
|
1318
|
+
|
|
1319
|
+
it("omits the server doc on replay after the caller loses access to the document", async () => {
|
|
1320
|
+
const created = expectAck(
|
|
1321
|
+
await applySyncMutation({
|
|
1322
|
+
mutation: {
|
|
1323
|
+
collection: "mutStuff",
|
|
1324
|
+
data: {name: "attacker own", ownerId: attacker.id},
|
|
1325
|
+
mutationId: "m-stale-own",
|
|
1326
|
+
operation: "create",
|
|
1327
|
+
},
|
|
1328
|
+
user: attacker,
|
|
1329
|
+
})
|
|
1330
|
+
);
|
|
1331
|
+
const mutation: SyncMutateRequest = {
|
|
1332
|
+
baseVersion: 999,
|
|
1333
|
+
collection: "mutStuff",
|
|
1334
|
+
data: {name: "stale"},
|
|
1335
|
+
id: created.id,
|
|
1336
|
+
mutationId: "m-stale-conflict",
|
|
1337
|
+
operation: "update",
|
|
1338
|
+
};
|
|
1339
|
+
const first = expectNack(await applySyncMutation({mutation, user: attacker}));
|
|
1340
|
+
expect((first.serverDoc as {name: string}).name).toBe("attacker own");
|
|
1341
|
+
|
|
1342
|
+
const doc = await MutStuffModel.findById(created.id);
|
|
1343
|
+
if (!doc) {
|
|
1344
|
+
throw new Error("expected doc");
|
|
1345
|
+
}
|
|
1346
|
+
doc.ownerId = victim.id;
|
|
1347
|
+
doc.name = "DUMMY-after-transfer";
|
|
1348
|
+
await doc.save();
|
|
1349
|
+
|
|
1350
|
+
const replayed = expectNack(await applySyncMutation({mutation, user: attacker}));
|
|
1351
|
+
expect(replayed.code).toBe("conflict");
|
|
1352
|
+
expect(replayed.serverSeq).toBe(first.serverSeq);
|
|
1353
|
+
expect(replayed.serverDoc).toBeUndefined();
|
|
1354
|
+
});
|
|
1355
|
+
});
|
|
1356
|
+
|
|
1193
1357
|
describe("admin window mutations", () => {
|
|
1194
1358
|
const admin = {_id: "mut-admin", admin: true, id: "mut-admin"} as unknown as User;
|
|
1195
1359
|
|
|
@@ -5,7 +5,9 @@ import type {User} from "../auth";
|
|
|
5
5
|
import {APIError, isAPIError} from "../errors";
|
|
6
6
|
import {createFeatureFlaggedLogger, logger} from "../logger";
|
|
7
7
|
import {resolveOrgContext, runWithOrgContext} from "../orgs/orgContext";
|
|
8
|
+
import {checkPermissions} from "../permissions";
|
|
8
9
|
import {findOneOrNoneFor} from "../plugins";
|
|
10
|
+
import {authorizeAdminBroadcastDocument} from "./adminBroadcastScope";
|
|
9
11
|
import {
|
|
10
12
|
type AdminWindowMutationScope,
|
|
11
13
|
buildAdminWindowExecutorOptions,
|
|
@@ -26,7 +28,7 @@ import {SYNC_MUTATION_LEASE_MS, SyncMutation, type SyncMutationDocument} from ".
|
|
|
26
28
|
import {findSyncEntryByCollectionTag, type SyncRegistryEntry} from "./registry";
|
|
27
29
|
import type {SyncAppOptions} from "./routes";
|
|
28
30
|
import {serializeSyncDoc} from "./routes";
|
|
29
|
-
import {getScopeField} from "./streams";
|
|
31
|
+
import {getScopeField, resolveStreamForDoc, resolveUserStreamsForEntry} from "./streams";
|
|
30
32
|
import type {
|
|
31
33
|
SyncAck,
|
|
32
34
|
SyncMutateBatchResponse,
|
|
@@ -172,20 +174,71 @@ const makeNack = (nack: SyncNack): SyncMutationOutcome => {
|
|
|
172
174
|
return {nack, type: "nack"};
|
|
173
175
|
};
|
|
174
176
|
|
|
177
|
+
/**
|
|
178
|
+
* A replayed conflict hands back the LIVE document, so the caller must still be allowed to
|
|
179
|
+
* read it now: the admin broadcast scope for an admin-window replay, otherwise the
|
|
180
|
+
* collection's read permission plus membership in the document's sync stream (the checks a
|
|
181
|
+
* snapshot applies).
|
|
182
|
+
*/
|
|
183
|
+
const canReadReplayedDoc = async ({
|
|
184
|
+
doc,
|
|
185
|
+
entry,
|
|
186
|
+
mutation,
|
|
187
|
+
scopeResolver,
|
|
188
|
+
user,
|
|
189
|
+
}: {
|
|
190
|
+
doc: mongoose.Document;
|
|
191
|
+
entry: SyncRegistryEntry;
|
|
192
|
+
mutation: SyncMutateRequest;
|
|
193
|
+
scopeResolver?: SyncMutationScopeResolver;
|
|
194
|
+
user: User;
|
|
195
|
+
}): Promise<boolean> => {
|
|
196
|
+
const plain = doc.toObject() as Record<string, unknown>;
|
|
197
|
+
if (mutation.mutationMode === "adminWindow") {
|
|
198
|
+
const decision = await authorizeAdminBroadcastDocument({
|
|
199
|
+
doc: plain,
|
|
200
|
+
modelName: entry.modelName,
|
|
201
|
+
user,
|
|
202
|
+
});
|
|
203
|
+
return decision === "allow";
|
|
204
|
+
}
|
|
205
|
+
if (!(await checkPermissions("read", entry.options.permissions.read, user, doc))) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
const stream = resolveStreamForDoc({
|
|
209
|
+
collectionTag: entry.collectionTag,
|
|
210
|
+
doc: plain,
|
|
211
|
+
scope: entry.config.scope,
|
|
212
|
+
});
|
|
213
|
+
const userStreams = await resolveUserStreamsForEntry({
|
|
214
|
+
entry,
|
|
215
|
+
getUserScopes: scopeResolver,
|
|
216
|
+
user,
|
|
217
|
+
});
|
|
218
|
+
return userStreams.includes(stream);
|
|
219
|
+
};
|
|
220
|
+
|
|
175
221
|
/**
|
|
176
222
|
* Load a document by id INCLUDING tombstones (`isDeletedPlugin` would otherwise filter a
|
|
177
223
|
* soft-deleted doc out) and serialize it for a sync payload. Returns undefined when the
|
|
178
|
-
* document is gone, the id does not cast,
|
|
179
|
-
* treat a missing `serverDoc` as "seq only", which
|
|
224
|
+
* document is gone, the id does not cast, the caller may no longer read it, or the
|
|
225
|
+
* consumer's serializer throws — callers treat a missing `serverDoc` as "seq only", which
|
|
226
|
+
* is what the client needs to re-base.
|
|
180
227
|
*/
|
|
181
228
|
const serializeLiveDoc = async ({
|
|
182
229
|
entry,
|
|
183
230
|
id,
|
|
231
|
+
mutation,
|
|
184
232
|
request,
|
|
233
|
+
scopeResolver,
|
|
234
|
+
user,
|
|
185
235
|
}: {
|
|
186
236
|
entry: SyncRegistryEntry;
|
|
187
237
|
id: string | undefined;
|
|
238
|
+
mutation: SyncMutateRequest;
|
|
188
239
|
request: express.Request;
|
|
240
|
+
scopeResolver?: SyncMutationScopeResolver;
|
|
241
|
+
user: User;
|
|
189
242
|
}): Promise<unknown> => {
|
|
190
243
|
if (!id) {
|
|
191
244
|
return undefined;
|
|
@@ -197,6 +250,9 @@ const serializeLiveDoc = async ({
|
|
|
197
250
|
if (!doc) {
|
|
198
251
|
return undefined;
|
|
199
252
|
}
|
|
253
|
+
if (!(await canReadReplayedDoc({doc, entry, mutation, scopeResolver, user}))) {
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
200
256
|
return await serializeSyncDoc({doc, entry, req: request});
|
|
201
257
|
} catch (error: unknown) {
|
|
202
258
|
logger.warn("[sync] Failed to serialize the server doc for a recorded conflict", {
|
|
@@ -218,12 +274,18 @@ const serializeLiveDoc = async ({
|
|
|
218
274
|
*/
|
|
219
275
|
const outcomeFromLedgerRow = async ({
|
|
220
276
|
entry,
|
|
277
|
+
mutation,
|
|
221
278
|
request,
|
|
222
279
|
row,
|
|
280
|
+
scopeResolver,
|
|
281
|
+
user,
|
|
223
282
|
}: {
|
|
224
283
|
entry: SyncRegistryEntry;
|
|
284
|
+
mutation: SyncMutateRequest;
|
|
225
285
|
request: express.Request;
|
|
226
286
|
row: SyncMutationDocument;
|
|
287
|
+
scopeResolver?: SyncMutationScopeResolver;
|
|
288
|
+
user: User;
|
|
227
289
|
}): Promise<SyncMutationOutcome | undefined> => {
|
|
228
290
|
if (row.status === "applied") {
|
|
229
291
|
return {
|
|
@@ -236,7 +298,14 @@ const outcomeFromLedgerRow = async ({
|
|
|
236
298
|
code: "conflict",
|
|
237
299
|
message: row.error,
|
|
238
300
|
mutationId: row.mutationId,
|
|
239
|
-
serverDoc: await serializeLiveDoc({
|
|
301
|
+
serverDoc: await serializeLiveDoc({
|
|
302
|
+
entry,
|
|
303
|
+
id: row.resultId,
|
|
304
|
+
mutation,
|
|
305
|
+
request,
|
|
306
|
+
scopeResolver,
|
|
307
|
+
user,
|
|
308
|
+
}),
|
|
240
309
|
serverSeq: row.resultSeq,
|
|
241
310
|
});
|
|
242
311
|
}
|
|
@@ -281,14 +350,18 @@ const takeoverStaleLease = async (
|
|
|
281
350
|
*/
|
|
282
351
|
const waitForRecordedOutcome = async ({
|
|
283
352
|
entry,
|
|
353
|
+
mutation,
|
|
284
354
|
mutationId,
|
|
285
355
|
request,
|
|
356
|
+
scopeResolver,
|
|
286
357
|
user,
|
|
287
358
|
onTakeover,
|
|
288
359
|
}: {
|
|
289
360
|
entry: SyncRegistryEntry;
|
|
361
|
+
mutation: SyncMutateRequest;
|
|
290
362
|
mutationId: string;
|
|
291
363
|
request: express.Request;
|
|
364
|
+
scopeResolver?: SyncMutationScopeResolver;
|
|
292
365
|
user: User;
|
|
293
366
|
/** Invoked with the re-claimed row when a stale lease is taken over. */
|
|
294
367
|
onTakeover: (row: SyncMutationDocument) => Promise<SyncMutationOutcome>;
|
|
@@ -303,7 +376,23 @@ const waitForRecordedOutcome = async ({
|
|
|
303
376
|
mutationId,
|
|
304
377
|
});
|
|
305
378
|
}
|
|
306
|
-
|
|
379
|
+
// Rows recorded before collectionTag existed carry none; their replays still pass
|
|
380
|
+
// the live read check in serializeLiveDoc.
|
|
381
|
+
if (row.collectionTag !== undefined && row.collectionTag !== entry.collectionTag) {
|
|
382
|
+
return makeNack({
|
|
383
|
+
code: "unauthorized",
|
|
384
|
+
message: `Mutation ${mutationId} was submitted for another collection`,
|
|
385
|
+
mutationId,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
const outcome = await outcomeFromLedgerRow({
|
|
389
|
+
entry,
|
|
390
|
+
mutation,
|
|
391
|
+
request,
|
|
392
|
+
row,
|
|
393
|
+
scopeResolver,
|
|
394
|
+
user,
|
|
395
|
+
});
|
|
307
396
|
if (outcome) {
|
|
308
397
|
return outcome;
|
|
309
398
|
}
|
|
@@ -865,6 +954,7 @@ export const applySyncMutation = async ({
|
|
|
865
954
|
try {
|
|
866
955
|
claimed = await SyncMutation.create({
|
|
867
956
|
claimedAt: DateTime.now().toJSDate(),
|
|
957
|
+
collectionTag: entry.collectionTag,
|
|
868
958
|
mutationId,
|
|
869
959
|
status: "pending",
|
|
870
960
|
userId: String(user.id),
|
|
@@ -875,6 +965,7 @@ export const applySyncMutation = async ({
|
|
|
875
965
|
}
|
|
876
966
|
return waitForRecordedOutcome({
|
|
877
967
|
entry,
|
|
968
|
+
mutation,
|
|
878
969
|
mutationId,
|
|
879
970
|
onTakeover: (takenOver) =>
|
|
880
971
|
applyClaimedMutation({
|
|
@@ -888,6 +979,7 @@ export const applySyncMutation = async ({
|
|
|
888
979
|
user,
|
|
889
980
|
}),
|
|
890
981
|
request,
|
|
982
|
+
scopeResolver,
|
|
891
983
|
user,
|
|
892
984
|
});
|
|
893
985
|
}
|
package/src/sync/routes.ts
CHANGED
|
@@ -327,9 +327,11 @@ const pageLegacyStratum = async ({
|
|
|
327
327
|
if (!(await checkPermissions("read", entry.options.permissions.read, user, doc))) {
|
|
328
328
|
continue;
|
|
329
329
|
}
|
|
330
|
+
const isTombstone = Boolean(doc.deleted);
|
|
330
331
|
entities.push({
|
|
331
|
-
|
|
332
|
-
|
|
332
|
+
// C7: tombstones carry no data, the same as the seq stratum and /sync/entities.
|
|
333
|
+
data: isTombstone ? null : await serializeSyncDoc({doc, entry, req}),
|
|
334
|
+
deleted: isTombstone,
|
|
333
335
|
id: String(doc._id),
|
|
334
336
|
seq: 0,
|
|
335
337
|
});
|
|
@@ -245,6 +245,17 @@ describe("C1 write-path guards (m9 / m10) + C4 scope-move markers", () => {
|
|
|
245
245
|
expect(counter?.seq).toBe(firstSeq as number);
|
|
246
246
|
});
|
|
247
247
|
|
|
248
|
+
it("m10: a save that only changes the seq stamp restamps it instead of keeping it", async () => {
|
|
249
|
+
const doc = await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
250
|
+
const firstSeq = doc._syncSeq as number;
|
|
251
|
+
doc._syncSeq = 123456789;
|
|
252
|
+
await doc.save();
|
|
253
|
+
const stored = await FrontierModel.findById(doc._id).lean();
|
|
254
|
+
expect(stored?._syncSeq).toBe(firstSeq + 1);
|
|
255
|
+
const counter = await SyncCounter.findOne({stream: "frontierStuff|tenant:org1"});
|
|
256
|
+
expect(counter?.seq).toBe(firstSeq + 1);
|
|
257
|
+
});
|
|
258
|
+
|
|
248
259
|
it("m10: a meaningful save DOES claim a new seq", async () => {
|
|
249
260
|
const doc = await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
250
261
|
const firstSeq = doc._syncSeq as number;
|
|
@@ -315,6 +315,24 @@ describe("Phase C sync routes", () => {
|
|
|
315
315
|
expect(stamped?.seq).toBe(1);
|
|
316
316
|
});
|
|
317
317
|
|
|
318
|
+
it("serves soft-deleted legacy documents as data-less tombstones", async () => {
|
|
319
|
+
await PhaseCTodoModel.collection.insertMany([
|
|
320
|
+
{deleted: true, ownerId: notAdminId, title: "DUMMY-deleted-legacy"},
|
|
321
|
+
{deleted: false, ownerId: notAdminId, title: "DUMMY-live-legacy"},
|
|
322
|
+
] as unknown as PhaseCTodo[]);
|
|
323
|
+
|
|
324
|
+
const page = await agent
|
|
325
|
+
.get(`/sync/snapshot?stream=${enc(ownerStream())}&limit=10`)
|
|
326
|
+
.expect(200);
|
|
327
|
+
const entities = page.body.entities as SnapshotEntity[];
|
|
328
|
+
expect(entities).toHaveLength(2);
|
|
329
|
+
const tombstone = entities.find((e) => e.deleted);
|
|
330
|
+
expect(tombstone).toMatchObject({data: null, deleted: true, seq: 0});
|
|
331
|
+
const live = entities.find((e) => !e.deleted);
|
|
332
|
+
expect(live?.data?.title).toBe("DUMMY-live-legacy");
|
|
333
|
+
expect(JSON.stringify(page.body)).not.toContain("DUMMY-deleted-legacy");
|
|
334
|
+
});
|
|
335
|
+
|
|
318
336
|
it("Task 9.14: a string-_id model with more legacy docs than the limit bootstraps completely", async () => {
|
|
319
337
|
// Non-hex, client-minted ids: casting the legacy cursor to ObjectId would throw
|
|
320
338
|
// (500), and a hex-shaped string id would compare across BSON types and match
|
|
@@ -359,12 +359,13 @@ export const syncPlugin = (schema: Schema): void => {
|
|
|
359
359
|
// behind; drop it so this save's post hook never acts on the earlier attempt's claim.
|
|
360
360
|
this.$locals[PENDING_COMMIT_KEY] = undefined;
|
|
361
361
|
// m10: a save that changes nothing meaningful must not burn a seq or emit a delta.
|
|
362
|
-
// Excluded from the "meaningful" set:
|
|
363
|
-
//
|
|
364
|
-
//
|
|
365
|
-
//
|
|
362
|
+
// Excluded from the "meaningful" set: auto-managed timestamp metadata (`updated`, which
|
|
363
|
+
// createdUpdatedPlugin bumps on EVERY save — otherwise no save would ever be a no-op).
|
|
364
|
+
// A save whose only modified path is that is a no-op: skip the claim. The sync stamps
|
|
365
|
+
// (`_syncSeq`, `_syncPrevStream`) still count: this hook has not written them yet, so a
|
|
366
|
+
// change to them came from the caller, and it must be restamped rather than persisted.
|
|
366
367
|
if (!this.isNew) {
|
|
367
|
-
const ignored = new Set(["
|
|
368
|
+
const ignored = new Set(["updated"]);
|
|
368
369
|
const meaningful = this.modifiedPaths().filter((p) => !ignored.has(p));
|
|
369
370
|
if (meaningful.length === 0) {
|
|
370
371
|
return;
|
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
startChangeStreamWatcher,
|
|
21
21
|
stopChangeStreamWatcher,
|
|
22
22
|
} from "../realtime/changeStreamWatcher";
|
|
23
|
+
import {addQuerySubscription, clearQueryStore, computeQueryId} from "../realtime/queryStore";
|
|
23
24
|
import {clearRealtimeRegistry, registerRealtime} from "../realtime/registry";
|
|
24
25
|
import {setupDb} from "../tests";
|
|
25
26
|
import {defaultResponseHandler} from "../transformers";
|
|
@@ -1726,3 +1727,94 @@ describe("sync:delta — change stream integration", () => {
|
|
|
1726
1727
|
expect(io.emissions.filter((e) => e.event === "sync")).toHaveLength(0);
|
|
1727
1728
|
});
|
|
1728
1729
|
});
|
|
1730
|
+
|
|
1731
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1732
|
+
// change handler — realtime query fan-out failures stay isolated
|
|
1733
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1734
|
+
|
|
1735
|
+
describe("change handler isolation", () => {
|
|
1736
|
+
let originalDb: typeof mongoose.connection.db;
|
|
1737
|
+
|
|
1738
|
+
beforeAll(async () => {
|
|
1739
|
+
await setupDb();
|
|
1740
|
+
});
|
|
1741
|
+
|
|
1742
|
+
beforeEach(() => {
|
|
1743
|
+
originalDb = mongoose.connection.db;
|
|
1744
|
+
clearRealtimeRegistry();
|
|
1745
|
+
clearQueryStore();
|
|
1746
|
+
clearSyncRegistry();
|
|
1747
|
+
});
|
|
1748
|
+
|
|
1749
|
+
afterEach(async () => {
|
|
1750
|
+
await stopChangeStreamWatcher();
|
|
1751
|
+
(mongoose.connection as unknown as {db: unknown}).db = originalDb;
|
|
1752
|
+
clearRealtimeRegistry();
|
|
1753
|
+
clearQueryStore();
|
|
1754
|
+
clearSyncRegistry();
|
|
1755
|
+
});
|
|
1756
|
+
|
|
1757
|
+
it("an unevaluable query predicate does not block sync:delta or later query rooms", async () => {
|
|
1758
|
+
const listeners = new Map<string, (...args: unknown[]) => unknown>();
|
|
1759
|
+
const mockStream = {
|
|
1760
|
+
close: async (): Promise<void> => {},
|
|
1761
|
+
on(event: string, handler: (...args: unknown[]) => unknown) {
|
|
1762
|
+
listeners.set(event, handler);
|
|
1763
|
+
return this;
|
|
1764
|
+
},
|
|
1765
|
+
};
|
|
1766
|
+
(mongoose.connection as unknown as {db: unknown}).db = {watch: () => mockStream};
|
|
1767
|
+
// One registration carries both surfaces, as modelRouter does for realtime + sync.
|
|
1768
|
+
registerSync({
|
|
1769
|
+
config: {scope: {type: "owner"}},
|
|
1770
|
+
model: SockStuffModel as unknown as Model<unknown>,
|
|
1771
|
+
options: {
|
|
1772
|
+
...permissiveOptions,
|
|
1773
|
+
realtime: {methods: ["create", "update", "delete"], roomStrategy: "model"},
|
|
1774
|
+
},
|
|
1775
|
+
routePath: "/sockStuff",
|
|
1776
|
+
});
|
|
1777
|
+
const poisoned = [{$and: [null]}, {name: {$eq: {toString: 1}}}] as Record<string, unknown>[];
|
|
1778
|
+
for (const [index, query] of poisoned.entries()) {
|
|
1779
|
+
addQuerySubscription(`attacker-${index}`, "sockStuff", query, `poisoned-${index}`);
|
|
1780
|
+
}
|
|
1781
|
+
const healthyQueryId = computeQueryId("sockStuff", {name: "poison-check"});
|
|
1782
|
+
addQuerySubscription("victim", "sockStuff", {name: "poison-check"}, healthyQueryId);
|
|
1783
|
+
|
|
1784
|
+
const io = makeTrackedIo();
|
|
1785
|
+
io.addSocketToRoom(`query:${healthyQueryId}`, {admin: true, id: "admin"});
|
|
1786
|
+
io.addSocketToRoom(syncRoomForStream("sockStuff|owner:victim-user"), {
|
|
1787
|
+
admin: false,
|
|
1788
|
+
id: "victim-user",
|
|
1789
|
+
});
|
|
1790
|
+
startChangeStreamWatcher(io, {}, true);
|
|
1791
|
+
const changeHandler = listeners.get("change");
|
|
1792
|
+
if (!changeHandler) {
|
|
1793
|
+
throw new Error("expected change handler");
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
await changeHandler(
|
|
1797
|
+
makeChange({
|
|
1798
|
+
documentKey: {_id: "poison-doc"},
|
|
1799
|
+
fullDocument: {
|
|
1800
|
+
_id: "poison-doc",
|
|
1801
|
+
_syncSeq: 3,
|
|
1802
|
+
name: "poison-check",
|
|
1803
|
+
ownerId: "victim-user",
|
|
1804
|
+
},
|
|
1805
|
+
ns: {coll: SockStuffModel.collection.collectionName},
|
|
1806
|
+
operationType: "insert",
|
|
1807
|
+
})
|
|
1808
|
+
);
|
|
1809
|
+
await waitForDelta(io, (d) => d.id === "poison-doc", 2000);
|
|
1810
|
+
|
|
1811
|
+
const deltas = io.emissions.filter(
|
|
1812
|
+
(e) => e.event === "sync:delta" && (e.payload as SyncDelta).id === "poison-doc"
|
|
1813
|
+
);
|
|
1814
|
+
expect(deltas).toHaveLength(1);
|
|
1815
|
+
const healthy = io.emissions.filter(
|
|
1816
|
+
(e) => e.event === "sync" && e.room === `query:${healthyQueryId}`
|
|
1817
|
+
);
|
|
1818
|
+
expect(healthy).toHaveLength(1);
|
|
1819
|
+
});
|
|
1820
|
+
});
|