-
Notifications
You must be signed in to change notification settings - Fork 13.5k
feat: restrict @all and @here autocomplete by user permissions #40262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ishanmitra
wants to merge
1
commit into
RocketChat:develop
Choose a base branch
from
ishanmitra:feat/show-all-here-user-permission
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+107
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
apps/meteor/client/views/room/providers/ComposerPopupProvider.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { mockAppRoot } from '@rocket.chat/mock-providers'; | ||
| import { render, waitFor } from '@testing-library/react'; | ||
| import { useEffect } from 'react'; | ||
|
|
||
| import ComposerPopupProvider from './ComposerPopupProvider'; | ||
| import { useComposerPopupOptions } from '../contexts/ComposerPopupContext'; | ||
| import FakeRoomProvider from '../../../../tests/mocks/client/FakeRoomProvider'; | ||
| import { createFakeRoom } from '../../../../tests/mocks/data'; | ||
|
|
||
| const mockGrantedPermissions = new Set<string>(); | ||
|
|
||
| jest.mock('../../../../app/authorization/client', () => ({ | ||
| hasAtLeastOnePermission: (permissions: string[] | string) => { | ||
| const permissionList = Array.isArray(permissions) ? permissions : [permissions]; | ||
|
|
||
| return permissionList.some((permission) => mockGrantedPermissions.has(permission)); | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('../../../../app/utils/client', () => ({ | ||
| slashCommands: { | ||
| commands: {}, | ||
| }, | ||
| })); | ||
|
|
||
| type PopupOptionsConsumerProps = { | ||
| onReady: (options: ReturnType<typeof useComposerPopupOptions>) => void; | ||
| }; | ||
|
|
||
| const PopupOptionsConsumer = ({ onReady }: PopupOptionsConsumerProps) => { | ||
| const options = useComposerPopupOptions(); | ||
|
|
||
| useEffect(() => { | ||
| onReady(options); | ||
| }, [onReady, options]); | ||
|
|
||
| return null; | ||
| }; | ||
|
|
||
| const renderProvider = async (permissions: string[] = []) => { | ||
| mockGrantedPermissions.clear(); | ||
| permissions.forEach((permission) => mockGrantedPermissions.add(permission)); | ||
|
|
||
| const room = createFakeRoom({ t: 'c' }); | ||
| const appRoot = permissions.reduce((wrapper, permission) => wrapper.withPermission(permission), mockAppRoot().withJohnDoe()).build(); | ||
|
|
||
| let popupOptions: ReturnType<typeof useComposerPopupOptions> | undefined; | ||
|
|
||
| render( | ||
| <FakeRoomProvider roomOverrides={room}> | ||
| <ComposerPopupProvider room={room}> | ||
| <PopupOptionsConsumer | ||
| onReady={(options) => { | ||
| popupOptions = options; | ||
| }} | ||
| /> | ||
| </ComposerPopupProvider> | ||
| </FakeRoomProvider>, | ||
| { | ||
| wrapper: appRoot, | ||
| }, | ||
| ); | ||
|
|
||
| await waitFor(() => expect(popupOptions).toBeDefined()); | ||
|
|
||
| const mentionPopup = popupOptions?.find(({ trigger }) => trigger === '@'); | ||
| expect(mentionPopup).toBeDefined(); | ||
|
|
||
| const items = await mentionPopup?.getItemsFromLocal?.(''); | ||
|
|
||
| return items?.map(({ _id }) => _id) ?? []; | ||
| }; | ||
|
|
||
| describe('ComposerPopupProvider', () => { | ||
| it('does not show @all or @here in autocomplete when user does not have permissions', async () => { | ||
| const itemIds = await renderProvider(); | ||
|
|
||
| expect(itemIds).not.toContain('all'); | ||
| expect(itemIds).not.toContain('here'); | ||
| }); | ||
|
|
||
| it('shows only @all when user has mention-all permission', async () => { | ||
| const itemIds = await renderProvider(['mention-all']); | ||
|
|
||
| expect(itemIds).toContain('all'); | ||
| expect(itemIds).not.toContain('here'); | ||
| }); | ||
|
|
||
| it('shows only @here when user has mention-here permission', async () => { | ||
| const itemIds = await renderProvider(['mention-here']); | ||
|
|
||
| expect(itemIds).toContain('here'); | ||
| expect(itemIds).not.toContain('all'); | ||
| }); | ||
|
|
||
| it('shows both @all and @here when user has both permissions', async () => { | ||
| const itemIds = await renderProvider(['mention-all', 'mention-here']); | ||
|
|
||
| expect(itemIds).toContain('all'); | ||
| expect(itemIds).toContain('here'); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make the permission mock enforce the room scope.
The mock ignores
scope, so these tests would still pass ifComposerPopupProvideraccidentally calledhasAtLeastOnePermission('mention-all')withoutrid. Encode the room id into the mocked grant, or assert the second argument, so the test protects the scoped permission behavior.🧪 Proposed test hardening
const mockGrantedPermissions = new Set<string>(); jest.mock('../../../../app/authorization/client', () => ({ - hasAtLeastOnePermission: (permissions: string[] | string) => { + hasAtLeastOnePermission: (permissions: string[] | string, scope?: string) => { const permissionList = Array.isArray(permissions) ? permissions : [permissions]; - return permissionList.some((permission) => mockGrantedPermissions.has(permission)); + return permissionList.some((permission) => mockGrantedPermissions.has(`${scope}:${permission}`)); }, }));const renderProvider = async (permissions: string[] = []) => { - mockGrantedPermissions.clear(); - permissions.forEach((permission) => mockGrantedPermissions.add(permission)); - - const room = createFakeRoom({ t: 'c' }); - const appRoot = permissions.reduce((wrapper, permission) => wrapper.withPermission(permission), mockAppRoot().withJohnDoe()).build(); + const room = createFakeRoom({ _id: 'permission-scoped-room', t: 'c' }); + + mockGrantedPermissions.clear(); + permissions.forEach((permission) => mockGrantedPermissions.add(`${room._id}:${permission}`)); + + const appRoot = mockAppRoot().withJohnDoe().build();apps/meteor/app/authorization/client/hasPermission.ts:72-73exposesscopeas the second argument, and lines 41-64 pass it into validation.Also applies to: 40-45
🤖 Prompt for AI Agents