Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/meteor/app/ui-master/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Inject } from 'meteor/meteorhacks:inject-initial';
import { Tracker } from 'meteor/tracker';

import { applyHeadInjections, headInjections, injectIntoBody, injectIntoHead } from './inject';
import { getMessageMaxParseLength } from '../../../lib/getMessageMaxParseLength';
import { withDebouncing } from '../../../lib/utils/highOrderFunctions';
import { settings } from '../../settings/server';
import { getURL } from '../../utils/server/getURL';
Expand Down Expand Up @@ -126,6 +127,9 @@ Meteor.startup(() => {
})(__meteor_runtime_config__.ROOT_URL_PATH_PREFIX);

injectIntoHead('base', `<base href="${baseUrl}">`);

const escapedMessageMaxParseLength = escapeHTML(String(getMessageMaxParseLength()));
injectIntoHead('MESSAGE_MAX_PARSE_LENGTH', `<meta name="rc-message-parser-max-length" content="${escapedMessageMaxParseLength}" />`);
});

const renderDynamicCssList = withDebouncing({ wait: 500 })(async () => {
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const NOTIFICATION_ATTACHMENT_COLOR = '#FD745E';
export const MAX_MULTIPLE_UPLOADED_FILES = 10;
export const MAX_CUSTOM_SOUND_SIZE_BYTES = 5242880;
export const CUSTOM_SOUND_ALLOWED_MIME_TYPES = ['audio/mpeg', 'audio/mp3', 'audio/wav', 'audio/x-wav'];
export const MESSAGE_MAX_PARSE_LENGTH_DEFAULT = 0;
6 changes: 6 additions & 0 deletions apps/meteor/lib/getMessageMaxParseLength.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MESSAGE_MAX_PARSE_LENGTH_DEFAULT } from './constants';

export function getMessageMaxParseLength(): number {
const parsed = Number.parseInt(process.env.MESSAGE_MAX_PARSE_LENGTH ?? '', 10);
Comment thread
nazabucciarelli marked this conversation as resolved.
return Number.isFinite(parsed) ? parsed : MESSAGE_MAX_PARSE_LENGTH_DEFAULT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { isE2EEMessage } from '@rocket.chat/core-typings';
import type { IMessage } from '@rocket.chat/core-typings';
import { parse } from '@rocket.chat/message-parser';

import { getMessageMaxParseLength } from '../../../../lib/getMessageMaxParseLength';

type ParserConfig = {
colors?: boolean;
emoticons?: boolean;
Expand All @@ -26,6 +28,12 @@ export class BeforeSaveMarkdownParser {
return message;
}

const messageMaxParseLength = getMessageMaxParseLength();
if (messageMaxParseLength > 0 && message.msg && message.msg.length > messageMaxParseLength) {
delete message.md;
return message;
}

try {
if (message.msg) {
message.md = parse(message.msg, config);
Expand Down
39 changes: 39 additions & 0 deletions apps/meteor/tests/unit/lib/getMessageMaxParseLength.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect } from 'chai';

import { getMessageMaxParseLength } from '../../../lib/getMessageMaxParseLength';

describe('getMessageMaxParseLength', () => {
afterEach(() => {
delete process.env.MESSAGE_MAX_PARSE_LENGTH;
});

it('should return 0 (default) when env var is not set', () => {
delete process.env.MESSAGE_MAX_PARSE_LENGTH;
expect(getMessageMaxParseLength()).to.equal(0);
});

it('should return 0 (default) when env var is empty string', () => {
process.env.MESSAGE_MAX_PARSE_LENGTH = '';
expect(getMessageMaxParseLength()).to.equal(0);
});

it('should return 0 (default) when env var is not a number', () => {
process.env.MESSAGE_MAX_PARSE_LENGTH = 'abc';
expect(getMessageMaxParseLength()).to.equal(0);
});

it('should return the parsed number when env var is a valid integer', () => {
process.env.MESSAGE_MAX_PARSE_LENGTH = '5000';
expect(getMessageMaxParseLength()).to.equal(5000);
});

it('should return 0 when env var is "0"', () => {
process.env.MESSAGE_MAX_PARSE_LENGTH = '0';
expect(getMessageMaxParseLength()).to.equal(0);
});

it('should return 0 (default) when env var is Infinity', () => {
process.env.MESSAGE_MAX_PARSE_LENGTH = 'Infinity';
expect(getMessageMaxParseLength()).to.equal(0);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { beforeEach } from 'mocha';

import { BeforeSaveMarkdownParser } from '../../../../../../server/services/messages/hooks/BeforeSaveMarkdownParser';

Expand All @@ -16,6 +17,14 @@ const createMessage = (msg?: string, extra: any = {}) => ({
});

describe('Markdown parser', () => {
beforeEach(() => {
delete process.env.MESSAGE_MAX_PARSE_LENGTH;
});

afterEach(() => {
delete process.env.MESSAGE_MAX_PARSE_LENGTH;
});

it('should do nothing if markdown parser is disabled', async () => {
const markdownParser = new BeforeSaveMarkdownParser(false);

Expand All @@ -38,11 +47,36 @@ describe('Markdown parser', () => {
expect(message).to.not.have.property('md');
});

it('should parse markdown', async () => {
it('should skip parsing when msg exceeds MESSAGE_MAX_PARSE_LENGTH', async () => {
process.env.MESSAGE_MAX_PARSE_LENGTH = '10';
const markdownParser = new BeforeSaveMarkdownParser(true);

const message = await markdownParser.parseMarkdown({
message: createMessage('hey'),
message: createMessage('a'.repeat(11)),
config: {},
});

expect(message).to.not.have.property('md');
});

it('should parse normally when msg is within MESSAGE_MAX_PARSE_LENGTH', async () => {
process.env.MESSAGE_MAX_PARSE_LENGTH = '100';
const markdownParser = new BeforeSaveMarkdownParser(true);

const message = await markdownParser.parseMarkdown({
message: createMessage('short msg'),
config: {},
});

expect(message).to.have.property('md');
});

it('should parse normally when MESSAGE_MAX_PARSE_LENGTH is 0', async () => {
process.env.MESSAGE_MAX_PARSE_LENGTH = '0';
const markdownParser = new BeforeSaveMarkdownParser(true);

const message = await markdownParser.parseMarkdown({
message: createMessage('short msg'),
config: {},
});

Expand Down
Loading