-
Notifications
You must be signed in to change notification settings - Fork 13.2k
fix(models): escape external inputs before RegExp construction in Liv… #38490
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
SrinjoyeeDey
wants to merge
7
commits into
RocketChat:develop
Choose a base branch
from
SrinjoyeeDey:fix/livechat-regex-escaping
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.
+134
−9
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
53555ad
fix(security): escape external inputs in LivechatRooms regex queries
SrinjoyeeDey dd9088e
security: fix regex vulnerabilities and standardize usage in models
SrinjoyeeDey 694277d
security: strip inline comments from implementation
SrinjoyeeDey f18005e
Merge branch 'develop' into fix/livechat-regex-escaping
SrinjoyeeDey 932803d
Merge branch 'develop' into fix/livechat-regex-escaping
SrinjoyeeDey 548d44f
test: revert double escaping inside models and add verification unit …
SrinjoyeeDey a799b41
Merge branch 'develop' into fix/livechat-regex-escaping
SrinjoyeeDey 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
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
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,66 @@ | ||
| import { ModerationReportsRaw } from './ModerationReports'; | ||
|
|
||
| describe('ModerationReportsRaw', () => { | ||
| it('should pass selector to $regex unchanged in findReportedMessagesByReportedUserId', async () => { | ||
| const mockCollection = { | ||
| find: jest.fn().mockReturnValue({ toArray: jest.fn().mockResolvedValue([]), _bsonType: 'Cursor' }), | ||
| countDocuments: jest.fn().mockResolvedValue(0), | ||
| collectionName: 'moderation_reports', | ||
| createIndexes: jest.fn().mockResolvedValue([]), | ||
| } as any; | ||
| const mockDb = { | ||
| collection: jest.fn().mockReturnValue(mockCollection), | ||
| } as any; | ||
|
|
||
| const moderationReportsRaw = new ModerationReportsRaw(mockDb); | ||
|
|
||
| await moderationReportsRaw.findReportedMessagesByReportedUserId('123', '.*', { offset: 0, count: 10 }); | ||
|
|
||
| expect(mockCollection.find).toHaveBeenCalled(); | ||
| const query = mockCollection.find.mock.calls[0][0]; | ||
|
|
||
| expect(query['message.msg'].$regex).toBe('.*'); | ||
| }); | ||
|
|
||
| it('should pass selector to $regex unchanged in countMessageReportsInRange (getSearchQueryForSelector)', () => { | ||
| const mockCollection = { | ||
| countDocuments: jest.fn().mockResolvedValue(0), | ||
| collectionName: 'moderation_reports', | ||
| createIndexes: jest.fn().mockResolvedValue([]), | ||
| } as any; | ||
| const mockDb = { | ||
| collection: jest.fn().mockReturnValue(mockCollection), | ||
| } as any; | ||
|
|
||
| const moderationReportsRaw = new ModerationReportsRaw(mockDb); | ||
|
|
||
| moderationReportsRaw.countMessageReportsInRange(new Date(), new Date(), '.*'); | ||
|
|
||
| expect(mockCollection.countDocuments).toHaveBeenCalled(); | ||
| const query = mockCollection.countDocuments.mock.calls[0][0]; | ||
|
|
||
| expect(query.$or[0]['message.msg'].$regex).toBe('.*'); | ||
| expect(query.$or[1].description.$regex).toBe('.*'); | ||
| }); | ||
|
|
||
| it('should pass selector to $regex unchanged via getTotalUniqueReportedUsers (getSearchQueryForSelectorUsers)', async () => { | ||
| const mockCollection = { | ||
| aggregate: jest.fn().mockReturnValue({ toArray: jest.fn().mockResolvedValue([]) }), | ||
| collectionName: 'moderation_reports', | ||
| createIndexes: jest.fn().mockResolvedValue([]), | ||
| } as any; | ||
| const mockDb = { | ||
| collection: jest.fn().mockReturnValue(mockCollection), | ||
| } as any; | ||
|
|
||
| const moderationReportsRaw = new ModerationReportsRaw(mockDb); | ||
|
|
||
| await moderationReportsRaw.getTotalUniqueReportedUsers(new Date(), new Date(), '.*', false); | ||
|
|
||
| expect(mockCollection.aggregate).toHaveBeenCalled(); | ||
| const pipeline = mockCollection.aggregate.mock.calls[0][0]; | ||
| const matchStage = pipeline.find((stage: any) => stage.$match); | ||
|
|
||
| expect(matchStage.$match.$or[0]['reportedUser.username'].$regex).toBe('.*'); | ||
| }); | ||
| }); |
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
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,50 @@ | ||
| import { SessionsRaw } from './Sessions'; | ||
|
|
||
| describe('SessionsRaw', () => { | ||
| it('should pass search parameter to $regex unchanged in aggregateSessionsByUserId', async () => { | ||
| const mockCollection = { | ||
| aggregate: jest.fn().mockReturnValue({ toArray: jest.fn().mockResolvedValue([]) }), | ||
| collectionName: 'sessions', | ||
| createIndexes: jest.fn().mockResolvedValue([]), | ||
| } as any; | ||
| const mockDb = { | ||
| collection: jest.fn().mockReturnValue(mockCollection), | ||
| } as any; | ||
|
|
||
| const sessionsRaw = new SessionsRaw(mockDb); | ||
|
|
||
| await sessionsRaw.aggregateSessionsByUserId({ | ||
| uid: '123', | ||
| search: '.*', // raw regex that should not be escaped | ||
| }); | ||
|
|
||
| expect(mockCollection.aggregate).toHaveBeenCalled(); | ||
| const pipeline = mockCollection.aggregate.mock.calls[0][0]; | ||
| const matchStage = pipeline.find((stage: any) => stage.$match); | ||
|
|
||
| expect(matchStage.$match.$and[0]).toEqual({ searchTerm: { $regex: '.*', $options: 'i' } }); | ||
| }); | ||
|
|
||
| it('should pass search parameter to $regex unchanged in aggregateSessionsAndPopulate', async () => { | ||
| const mockCollection = { | ||
| aggregate: jest.fn().mockReturnValue({ toArray: jest.fn().mockResolvedValue([]) }), | ||
| collectionName: 'sessions', | ||
| createIndexes: jest.fn().mockResolvedValue([]), | ||
| } as any; | ||
| const mockDb = { | ||
| collection: jest.fn().mockReturnValue(mockCollection), | ||
| } as any; | ||
|
|
||
| const sessionsRaw = new SessionsRaw(mockDb); | ||
|
|
||
| await sessionsRaw.aggregateSessionsAndPopulate({ | ||
| search: 'a|b', // pipe should not be escaped | ||
| }); | ||
|
|
||
| expect(mockCollection.aggregate).toHaveBeenCalled(); | ||
| const pipeline = mockCollection.aggregate.mock.calls[0][0]; | ||
| const matchStage = pipeline.find((stage: any) => stage.$match); | ||
|
|
||
| expect(matchStage.$match.$and[0]).toEqual({ searchTerm: { $regex: 'a|b', $options: 'i' } }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.