|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { describe, it, beforeEach } from 'mocha'; |
| 3 | +import proxyquire from 'proxyquire'; |
| 4 | +import sinon from 'sinon'; |
| 5 | + |
| 6 | +const findOneByIdAndUserIdAndRoomId = sinon.stub(); |
| 7 | +const updateFileMetadata = sinon.stub().resolves(); |
| 8 | +const getPath = sinon.stub().returns('/path/to/file.txt'); |
| 9 | +const isImagePreviewSupported = sinon.stub().returns(false); |
| 10 | +const getFileExtension = sinon.stub().returns('txt'); |
| 11 | + |
| 12 | +const { parseFileIntoMessageAttachments } = proxyquire.noCallThru().load('./sendFileMessage', { |
| 13 | + '@rocket.chat/models': { |
| 14 | + Uploads: { findOneByIdAndUserIdAndRoomId, updateFileMetadata }, |
| 15 | + Rooms: { findOneById: sinon.stub() }, |
| 16 | + Users: { findOneById: sinon.stub() }, |
| 17 | + }, |
| 18 | + 'meteor/check': { |
| 19 | + check: sinon.stub(), |
| 20 | + Match: { |
| 21 | + Maybe: sinon.stub(), |
| 22 | + Optional: sinon.stub(), |
| 23 | + ObjectIncluding: sinon.stub(), |
| 24 | + }, |
| 25 | + }, |
| 26 | + 'meteor/meteor': { |
| 27 | + Meteor: { |
| 28 | + Error: class Error extends global.Error {}, |
| 29 | + methods: sinon.stub(), |
| 30 | + }, |
| 31 | + }, |
| 32 | + '../lib/FileUpload': { |
| 33 | + FileUpload: { getPath }, |
| 34 | + }, |
| 35 | + './isImagePreviewSupported': { isImagePreviewSupported }, |
| 36 | + '../../../../lib/utils/getFileExtension': { getFileExtension }, |
| 37 | + '../../../../server/lib/callbacks': { callbacks: { runAsync: sinon.stub() } }, |
| 38 | + '../../../../server/lib/logger/system': { SystemLogger: { error: sinon.stub() } }, |
| 39 | + '../../../authorization/server/functions/canAccessRoom': { canAccessRoomAsync: sinon.stub().resolves(true) }, |
| 40 | + '../../../lib/server/methods/sendMessage': { executeSendMessage: sinon.stub().resolves({}) }, |
| 41 | +}); |
| 42 | + |
| 43 | +describe('sendFileMessage - Mass Assignment & Type Pollution Prevention', () => { |
| 44 | + const mockUser = { _id: 'user123' }; |
| 45 | + const roomId = 'room123'; |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + findOneByIdAndUserIdAndRoomId.reset(); |
| 49 | + updateFileMetadata.reset(); |
| 50 | + |
| 51 | + findOneByIdAndUserIdAndRoomId.resolves({ _id: 'file123' }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should filter out invalid types, nulls, and malicious fields before updating the database', async () => { |
| 55 | + const maliciousFilePayload = { |
| 56 | + _id: 'file123', |
| 57 | + name: null, // invalid type, must be ignored |
| 58 | + type: 'text/plain', |
| 59 | + size: 1024, |
| 60 | + description: 12345, // invalid type, must be ignored |
| 61 | + typeGroup: 'image', // only valid field |
| 62 | + content: null, // invalid type, must be ignored |
| 63 | + maliciousRoleAssignment: 'admin', // mass assignment, must be ignored |
| 64 | + $set: { bypassSecurity: true }, // mongo injection, must be ignored |
| 65 | + }; |
| 66 | + |
| 67 | + await parseFileIntoMessageAttachments(maliciousFilePayload as any, roomId, mockUser as any); |
| 68 | + |
| 69 | + expect(updateFileMetadata.calledOnce).to.equal(true); |
| 70 | + |
| 71 | + const [fileId, userId, safeMetadata] = updateFileMetadata.getCall(0).args; |
| 72 | + |
| 73 | + expect(fileId).to.equal('file123'); |
| 74 | + expect(userId).to.equal('user123'); |
| 75 | + |
| 76 | + expect(safeMetadata).to.deep.equal({ |
| 77 | + typeGroup: 'image', |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should pass valid fields correctly to the database', async () => { |
| 82 | + const validFilePayload = { |
| 83 | + _id: 'file123', |
| 84 | + name: 'picture.jpg', |
| 85 | + type: 'image/jpeg', |
| 86 | + size: 2048, |
| 87 | + description: 'Description', |
| 88 | + typeGroup: 'image', |
| 89 | + content: { |
| 90 | + algorithm: 'rc.v1.aes-sha2', |
| 91 | + ciphertext: 'test', |
| 92 | + }, |
| 93 | + }; |
| 94 | + |
| 95 | + await parseFileIntoMessageAttachments(validFilePayload as any, roomId, mockUser as any); |
| 96 | + |
| 97 | + expect(updateFileMetadata.calledOnce).to.equal(true); |
| 98 | + |
| 99 | + const [, , safeMetadata] = updateFileMetadata.getCall(0).args; |
| 100 | + |
| 101 | + expect(safeMetadata).to.deep.equal({ |
| 102 | + name: 'picture.jpg', |
| 103 | + description: 'Description', |
| 104 | + typeGroup: 'image', |
| 105 | + content: { |
| 106 | + algorithm: 'rc.v1.aes-sha2', |
| 107 | + ciphertext: 'test', |
| 108 | + }, |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments