plugin-block-cross-join 0.1.7 → 0.1.8

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,74 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ import { createMockServer, type MockServer } from '@nocobase/test';
4
+ import { afterAll, beforeAll, expect, it } from 'vitest';
5
+ import Plugin from '../plugin';
6
+
7
+ let app: MockServer;
8
+ let reader: ReturnType<MockServer['agent']>;
9
+ beforeAll(async () => {
10
+ app = await createMockServer({
11
+ acl: true,
12
+ plugins: [
13
+ 'field-sort',
14
+ 'system-settings',
15
+ 'users',
16
+ 'auth',
17
+ 'acl',
18
+ 'data-source-manager',
19
+ 'data-source-main',
20
+ 'error-handler',
21
+ [Plugin, { name: 'plugin-block-cross-join' }],
22
+ ],
23
+ });
24
+ const collection = app.db.collection({
25
+ name: 'joinDocuments',
26
+ fields: [
27
+ { name: 'ownerId', type: 'integer' },
28
+ { name: 'title', type: 'string' },
29
+ { name: 'secret', type: 'string' },
30
+ ],
31
+ });
32
+ await collection.sync();
33
+ await app.db.getRepository('roles').create({ values: { name: 'join-reader', title: 'Reader' } });
34
+ const user = await app.db
35
+ .getRepository('users')
36
+ .create({ values: { username: 'join-reader', roles: ['join-reader'] } });
37
+ await collection.repository.create({ values: { ownerId: user.id, title: 'mine', secret: 'hidden' } });
38
+ await collection.repository.create({ values: { ownerId: 999, title: 'other', secret: 'hidden' } });
39
+ reader = await app.agent().login(user, 'join-reader');
40
+ app.acl.define({
41
+ role: 'join-reader',
42
+ actions: { 'joinDocuments:list': { fields: ['id', 'title'], filter: { ownerId: user.id } } },
43
+ });
44
+ }, 120000);
45
+ afterAll(async () => {
46
+ await app?.destroy();
47
+ });
48
+
49
+ it('returns only authorized rows and fields through the real HTTP action', async () => {
50
+ const response = await reader
51
+ .resource('crossJoin')
52
+ .query({ values: { config: { primarySource: { dataSource: 'main', collection: 'joinDocuments' } } } });
53
+ expect(response.status).toBe(200);
54
+ expect(response.body.data).toHaveLength(1);
55
+ expect(response.body.data[0]).toMatchObject({ 'primary.id': expect.any(Number), 'primary.title': 'mine' });
56
+ expect(Object.keys(response.body.data[0])).not.toContain('primary.secret');
57
+ expect(response.body.meta.count).toBe(1);
58
+ });
59
+
60
+ it('denies ungranted collections and restricted columns over HTTP', async () => {
61
+ const forbidden = await reader
62
+ .resource('crossJoin')
63
+ .query({ values: { config: { primarySource: { dataSource: 'main', collection: 'users' } } } });
64
+ expect(forbidden.status).toBe(403);
65
+ const secret = await reader.resource('crossJoin').query({
66
+ values: {
67
+ config: {
68
+ primarySource: { dataSource: 'main', collection: 'joinDocuments' },
69
+ columns: [{ source: 0, field: 'secret' }],
70
+ },
71
+ },
72
+ });
73
+ expect(secret.status).toBe(403);
74
+ });
@@ -0,0 +1,85 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ import { ACL } from '@nocobase/acl';
4
+ import type { Context } from '@nocobase/actions';
5
+ import { dataWrapping } from '../../../../../core/server/src/middlewares/data-wrapping';
6
+ import { describe, expect, it, vi } from 'vitest';
7
+ import { crossJoinQuery } from '../actions/cross-join-query';
8
+
9
+ function fixture(role = 'reader') {
10
+ const acl = new ACL();
11
+ acl.define({ role: 'reader', actions: { 'orders:list': { fields: ['id', 'ownerId'], filter: { ownerId: 7 } } } });
12
+ acl.define({ role: 'root' });
13
+ const externalAcl = new ACL();
14
+ externalAcl.define({
15
+ role: 'reader',
16
+ actions: { 'details:list': { fields: ['id', 'label'], filter: { visible: true } } },
17
+ });
18
+ const repo = { find: vi.fn(async () => [{ id: 1, ownerId: 7 }]), count: vi.fn(async () => 1) };
19
+ const joinRepo = { find: vi.fn(async () => [{ id: 1, label: 'Visible' }]) };
20
+ const sources = {
21
+ main: { acl, collectionManager: { getRepository: () => repo } },
22
+ external: { acl: externalAcl, collectionManager: { getRepository: () => joinRepo } },
23
+ };
24
+ const config = {
25
+ primarySource: { dataSource: 'main', collection: 'orders', filter: { id: 1 } },
26
+ joins: [{ dataSource: 'external', collection: 'details', joinType: 'left', leftField: 'id', rightField: 'id' }],
27
+ columns: [],
28
+ };
29
+ const ctx = {
30
+ app: { dataSourceManager: { get: (name: string) => sources[name] } },
31
+ state: { currentRoles: [role], currentUser: { id: 7 } },
32
+ action: { params: { config, page: 1, pageSize: 20 } },
33
+ throw: (status: number) => {
34
+ throw Object.assign(new Error('denied'), { status });
35
+ },
36
+ } as unknown as Context;
37
+ return { ctx, config, repo, joinRepo };
38
+ }
39
+
40
+ describe('cross join data permissions and response', () => {
41
+ it('applies filters and fields on every datasource and wraps the rows exactly once', async () => {
42
+ const { ctx, repo, joinRepo } = fixture();
43
+ await dataWrapping()(ctx, () => crossJoinQuery(ctx, async () => undefined));
44
+ expect(repo.find).toHaveBeenCalledWith(
45
+ expect.objectContaining({ fields: ['id', 'ownerId'], filter: { $and: [{ id: 1 }, { ownerId: 7 }] } }),
46
+ );
47
+ expect(repo.count).toHaveBeenCalledWith(expect.objectContaining({ filter: { $and: [{ id: 1 }, { ownerId: 7 }] } }));
48
+ expect(joinRepo.find).toHaveBeenCalledWith(
49
+ expect.objectContaining({
50
+ fields: ['id', 'label'],
51
+ filter: { $and: [{ visible: true }, { id: { $in: ['1'] } }] },
52
+ }),
53
+ );
54
+ expect(ctx.body).toEqual({
55
+ data: [{ 'primary.id': 1, 'primary.ownerId': 7, 'join0.id': 1, 'join0.label': 'Visible' }],
56
+ meta: { page: 1, pageSize: 20, count: 1 },
57
+ });
58
+ });
59
+
60
+ it('denies ungranted roles before reading either repository', async () => {
61
+ const { ctx, repo, joinRepo } = fixture('member');
62
+ await expect(crossJoinQuery(ctx, vi.fn())).rejects.toMatchObject({ status: 403 });
63
+ expect(repo.find).not.toHaveBeenCalled();
64
+ expect(joinRepo.find).not.toHaveBeenCalled();
65
+ });
66
+
67
+ it.each(['column', 'join', 'sort', 'filter'])('denies a restricted field used as %s', async (kind) => {
68
+ const { ctx, repo } = fixture();
69
+ const config = ctx.action.params.config;
70
+ if (kind === 'column') config.columns = [{ source: 0, field: 'secret' }];
71
+ if (kind === 'join') config.joins[0].leftField = 'secret';
72
+ if (kind === 'sort') ctx.action.params.sort = ['-secret'];
73
+ if (kind === 'filter') config.primarySource.filter = { secret: 'hidden' };
74
+ await expect(crossJoinQuery(ctx, vi.fn())).rejects.toMatchObject({ status: 403 });
75
+ expect(repo.find).not.toHaveBeenCalled();
76
+ });
77
+
78
+ it('checks join permission even when the primary source has no rows', async () => {
79
+ const { ctx, config, repo } = fixture();
80
+ config.joins[0].collection = 'private';
81
+ repo.find.mockResolvedValue([]);
82
+ await expect(crossJoinQuery(ctx, vi.fn())).rejects.toMatchObject({ status: 403 });
83
+ expect(repo.find).not.toHaveBeenCalled();
84
+ });
85
+ });