From ba28c2eb60f7e8da98333d774319b174df9926f2 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 13:20:16 +0000 Subject: [PATCH 1/3] Filter open streams by the current auth data Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs --- orga/changelog/fix-stream-fresh-auth.md | 1 + src/plugins/server/endpoint-replication.ts | 6 +++- src/plugins/server/endpoint-rest.ts | 11 ++++-- test/unit/endpoint-replication.test.ts | 41 ++++++++++++++++++++++ test/unit/endpoint-rest.test.ts | 36 +++++++++++++++++++ 5 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 orga/changelog/fix-stream-fresh-auth.md diff --git a/orga/changelog/fix-stream-fresh-auth.md b/orga/changelog/fix-stream-fresh-auth.md new file mode 100644 index 00000000..6316fa85 --- /dev/null +++ b/orga/changelog/fix-stream-fresh-auth.md @@ -0,0 +1 @@ +- FIX `/pullStream` of the replication endpoint and `/query/observe` of the REST endpoint loaded the current auth data before each event but still filtered the documents by the auth data of the connect, so a client kept getting documents it had lost access to diff --git a/src/plugins/server/endpoint-replication.ts b/src/plugins/server/endpoint-replication.ts index 3fffb78b..6456a1e4 100644 --- a/src/plugins/server/endpoint-replication.ts +++ b/src/plugins/server/endpoint-replication.ts @@ -233,7 +233,6 @@ export class RxServerReplicationEndpoint imp } adapter.setSSEHeaders(res); - const docDataMatcherStream = getDocAllowedMatcher(this, ensureNotFalsy(authData)); const subscription = replicationHandler.masterChangeStream$.pipe( concatMap(async (changes) => { /** @@ -256,6 +255,11 @@ export class RxServerReplicationEndpoint imp if (changes === 'RESYNC') { return changes; } else { + /** + * The auth data can be different from the one of the connect, + * so the documents must be filtered by the current auth data. + */ + const docDataMatcherStream = getDocAllowedMatcher(this, authData); const useDocs = changes.documents.filter(d => docDataMatcherStream(d as any)); return { documents: useDocs, diff --git a/src/plugins/server/endpoint-rest.ts b/src/plugins/server/endpoint-rest.ts index abc4022b..5ed352bc 100644 --- a/src/plugins/server/endpoint-rest.ts +++ b/src/plugins/server/endpoint-rest.ts @@ -168,7 +168,6 @@ export class RxServerRestEndpoint implements const rxQuery = this.collection.find(useQuery as any); const subscription = rxQuery.$.pipe( concatMap(async (result) => { - const resultData = result.map(doc => removeServerOnlyFields(doc.toJSON())); /** * The auth-data might be expired @@ -186,7 +185,15 @@ export class RxServerRestEndpoint implements return null; } - return resultData; + /** + * The auth data can be different from the one of the connect, + * so the results must be filtered by the current auth data. + */ + const docMatcher = getDocAllowedMatcher(this, ensureNotFalsy(authData)); + return result + .map(doc => doc.toJSON()) + .filter(docData => docMatcher(docData as any)) + .map(docData => removeServerOnlyFields(docData)); }), takeWhile(f => f !== null), tap({ complete: () => adapter.endResponse(res) }), diff --git a/test/unit/endpoint-replication.test.ts b/test/unit/endpoint-replication.test.ts index 75ab9998..987fe2ab 100644 --- a/test/unit/endpoint-replication.test.ts +++ b/test/unit/endpoint-replication.test.ts @@ -909,6 +909,47 @@ describe('endpoint-replication.test.ts', () => { serverCol.database.close(); clientCol.database.close(); }); + it('should use the current auth data to filter the events of an open pullStream', async () => { + const serverCol = await humansCollection.create(0); + let currentUserId = headers.userid; + const port = await nextPort(); + const server = await createRxServer({ + adapter: TEST_SERVER_ADAPTER, + database: serverCol.database, + authHandler: () => ({ + validUntil: Date.now() + 100000, + data: { userid: currentUserId } + }), + port + }); + const endpoint = await server.addReplicationEndpoint({ + name: randomToken(10), + collection: serverCol, + queryModifier + }); + await server.start(); + + const events: any[] = []; + const eventSource = new EventSource('http://localhost:' + port + '/' + endpoint.urlPath + '/pullStream', { + fetch: customFetchWithFixedHeaders(headers) + }); + eventSource.onmessage = event => events.push(JSON.parse(event.data)); + const hasDoc = (id: string) => events.some(e => e.documents && e.documents.some((d: any) => d.passportId === id)); + await waitUntil(() => eventSource.readyState === EventSource.OPEN); + + await serverCol.insert(schemaObjects.humanData('before-change', 1, headers.userid)); + await waitUntil(() => hasDoc('before-change')); + + // the user has no longer access to the documents of the old userid + currentUserId = 'bob'; + await serverCol.insert(schemaObjects.humanData('after-change', 1, headers.userid)); + await serverCol.insert(schemaObjects.humanData('bob-doc', 1, 'bob')); + await waitUntil(() => hasDoc('bob-doc')); + assert.strictEqual(hasDoc('after-change'), false); + + eventSource.close(); + await serverCol.database.close(); + }); it('should only accept the matching documents on the server', async () => { const serverCol = await humansCollection.create(0); const port = await nextPort(); diff --git a/test/unit/endpoint-rest.test.ts b/test/unit/endpoint-rest.test.ts index a3d6e5d6..07a1e4c1 100644 --- a/test/unit/endpoint-rest.test.ts +++ b/test/unit/endpoint-rest.test.ts @@ -647,6 +647,42 @@ describe('endpoint-rest.test.ts', () => { await col.database.close(); }); + it('should use the current auth data to filter the results of an open stream', async () => { + const col = await humansCollection.create(0); + await col.insert(schemaObjects.humanData('alice-doc', 1, headers.userid)); + let currentUserId = headers.userid; + const port = await nextPort(); + const server = await createRxServer({ + adapter: TEST_SERVER_ADAPTER, + database: col.database, + authHandler: () => ({ + validUntil: Date.now() + 100000, + data: { userid: currentUserId } + }), + port + }); + const endpoint = await server.addRestEndpoint({ + name: randomToken(10), + collection: col, + queryModifier + }); + await server.start(); + const client = createRestClient('http://localhost:' + port + '/' + endpoint.urlPath, headers); + + const emitted: HumanDocumentType[][] = []; + const sub = client.observeQuery({ selector: {} }).subscribe(result => emitted.push(result)); + await waitUntil(() => emitted.length === 1); + assert.deepStrictEqual(ensureNotFalsy(emitted[0]).map(d => d.passportId), ['alice-doc']); + + // the user has no longer access to the documents of the old userid + currentUserId = 'bob'; + await col.insert(schemaObjects.humanData('alice-doc-2', 1, headers.userid)); + await waitUntil(() => emitted.length === 2); + assert.deepStrictEqual(ensureNotFalsy(lastOfArray(emitted)), []); + + sub.unsubscribe(); + await col.database.close(); + }); it('should stop observing on the server when the client unsubscribes', async () => { const col = await humansCollection.create(1); const port = await nextPort(); From 073b5c748aa97a9980a824fa39f50af09d2c8b17 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 13:22:40 +0000 Subject: [PATCH 2/3] Add PR link to changelog entry Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs --- orga/changelog/fix-stream-fresh-auth.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orga/changelog/fix-stream-fresh-auth.md b/orga/changelog/fix-stream-fresh-auth.md index 6316fa85..68616c9c 100644 --- a/orga/changelog/fix-stream-fresh-auth.md +++ b/orga/changelog/fix-stream-fresh-auth.md @@ -1 +1 @@ -- FIX `/pullStream` of the replication endpoint and `/query/observe` of the REST endpoint loaded the current auth data before each event but still filtered the documents by the auth data of the connect, so a client kept getting documents it had lost access to +- FIX `/pullStream` of the replication endpoint and `/query/observe` of the REST endpoint loaded the current auth data before each event but still filtered the documents by the auth data of the connect, so a client kept getting documents it had lost access to [#482](https://github.com/pubkey/rxdb-server/pull/482) From d79ee0c6b17b857488e12d198c1a0d5d08ed5ddc Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 13:30:36 +0000 Subject: [PATCH 3/3] End the stream instead of crashing when the queryModifier throws Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs --- src/plugins/server/endpoint-replication.ts | 8 ++++- src/plugins/server/endpoint-rest.ts | 8 ++++- test/unit/endpoint-replication.test.ts | 40 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/plugins/server/endpoint-replication.ts b/src/plugins/server/endpoint-replication.ts index 6456a1e4..a72fb2cf 100644 --- a/src/plugins/server/endpoint-replication.ts +++ b/src/plugins/server/endpoint-replication.ts @@ -259,7 +259,13 @@ export class RxServerReplicationEndpoint imp * The auth data can be different from the one of the connect, * so the documents must be filtered by the current auth data. */ - const docDataMatcherStream = getDocAllowedMatcher(this, authData); + let docDataMatcherStream: (docData: any) => boolean; + try { + docDataMatcherStream = getDocAllowedMatcher(this, authData); + } catch (err) { + // the queryModifier does not allow this auth data, so end the stream + return null; + } const useDocs = changes.documents.filter(d => docDataMatcherStream(d as any)); return { documents: useDocs, diff --git a/src/plugins/server/endpoint-rest.ts b/src/plugins/server/endpoint-rest.ts index 5ed352bc..56bd422a 100644 --- a/src/plugins/server/endpoint-rest.ts +++ b/src/plugins/server/endpoint-rest.ts @@ -189,7 +189,13 @@ export class RxServerRestEndpoint implements * The auth data can be different from the one of the connect, * so the results must be filtered by the current auth data. */ - const docMatcher = getDocAllowedMatcher(this, ensureNotFalsy(authData)); + let docMatcher: (docData: any) => boolean; + try { + docMatcher = getDocAllowedMatcher(this, ensureNotFalsy(authData)); + } catch (err) { + // the queryModifier does not allow this auth data, so end the stream + return null; + } return result .map(doc => doc.toJSON()) .filter(docData => docMatcher(docData as any)) diff --git a/test/unit/endpoint-replication.test.ts b/test/unit/endpoint-replication.test.ts index 987fe2ab..c07cfc4b 100644 --- a/test/unit/endpoint-replication.test.ts +++ b/test/unit/endpoint-replication.test.ts @@ -950,6 +950,46 @@ describe('endpoint-replication.test.ts', () => { eventSource.close(); await serverCol.database.close(); }); + it('should end the pullStream when the queryModifier throws for the current auth data', async () => { + const serverCol = await humansCollection.create(0); + let currentUserId = headers.userid; + const port = await nextPort(); + const server = await createRxServer({ + adapter: TEST_SERVER_ADAPTER, + database: serverCol.database, + authHandler: () => ({ + validUntil: Date.now() + 100000, + data: { userid: currentUserId } + }), + port + }); + const endpoint = await server.addReplicationEndpoint({ + name: randomToken(10), + collection: serverCol, + queryModifier: (authData, query) => { + if (authData.data.userid === 'blocked-user') { + throw new Error('user is blocked'); + } + return queryModifier(authData, query); + } + }); + await server.start(); + + let errored = false; + const eventSource = new EventSource('http://localhost:' + port + '/' + endpoint.urlPath + '/pullStream', { + fetch: customFetchWithFixedHeaders(headers) + }); + eventSource.onerror = () => errored = true; + await waitUntil(() => eventSource.readyState === EventSource.OPEN); + + currentUserId = 'blocked-user'; + await serverCol.insert(schemaObjects.humanData('after-block', 1, headers.userid)); + // the server must end the stream instead of crashing + await waitUntil(() => errored); + + eventSource.close(); + await serverCol.database.close(); + }); it('should only accept the matching documents on the server', async () => { const serverCol = await humansCollection.create(0); const port = await nextPort();