Skip to content

Commit a496a25

Browse files
yash-rajpalgaolin1
authored andcommitted
fix!: title and value properties should be required on attachments.fields (RocketChat#37233)
1 parent d1547e3 commit a496a25

File tree

3 files changed

+127
-2
lines changed

3 files changed

+127
-2
lines changed

.changeset/hungry-fans-wait.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rocket.chat/meteor': minor
3+
---
4+
5+
Validates attachment fields to require `title` and `value` properties on APIs `chat.postMessage` and `chat.sendMessage`.

apps/meteor/app/lib/server/functions/sendMessage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ const validateAttachmentsFields = (attachmentField: any) => {
7979
}),
8080
);
8181

82-
if (typeof attachmentField.value !== 'undefined') {
83-
attachmentField.value = String(attachmentField.value);
82+
if (!attachmentField.value || !attachmentField.title) {
83+
throw new Error('Invalid attachment field, title and value is required');
8484
}
8585
};
8686

apps/meteor/tests/end-to-end/api/chat.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,126 @@ describe('[Chat]', () => {
464464
.end(done);
465465
});
466466

467+
it('should throw an error when the properties (attachments.fields.title) is missing', (done) => {
468+
void request
469+
.post(api('chat.postMessage'))
470+
.set(credentials)
471+
.send({
472+
channel: testChannel.name,
473+
text: 'Sample message',
474+
emoji: ':smirk:',
475+
alias: 'Gruggy',
476+
avatar: 'http://res.guggy.com/logo_128.png',
477+
attachments: [
478+
{
479+
color: '#ff0000',
480+
text: 'Yay for gruggy!',
481+
ts: '2016-12-09T16:53:06.761Z',
482+
thumb_url: 'http://res.guggy.com/logo_128.png',
483+
message_link: 'https://google.com',
484+
collapsed: false,
485+
author_name: 'Bradley Hilton',
486+
author_link: 'https://rocket.chat/',
487+
author_icon: 'https://avatars.githubusercontent.com/u/850391?v=3',
488+
title: 'Attachment Example',
489+
title_link: 'https://youtube.com',
490+
title_link_download: true,
491+
image_url: 'http://res.guggy.com/logo_128.png',
492+
audio_url: 'http://www.w3schools.com/tags/horse.mp3',
493+
video_url: 'http://www.w3schools.com/tags/movie.mp4',
494+
fields: [
495+
{
496+
short: true,
497+
value: 'This is attachment field value',
498+
},
499+
],
500+
},
501+
],
502+
})
503+
.expect('Content-Type', 'application/json')
504+
.expect(400)
505+
.expect((res) => {
506+
expect(res.body).to.have.property('success', false);
507+
expect(res.body).to.have.property('error');
508+
})
509+
.end(done);
510+
});
511+
512+
it('should throw an error when the properties (attachments.fields.value) is missing', (done) => {
513+
void request
514+
.post(api('chat.postMessage'))
515+
.set(credentials)
516+
.send({
517+
channel: testChannel.name,
518+
text: 'Sample message',
519+
emoji: ':smirk:',
520+
alias: 'Gruggy',
521+
avatar: 'http://res.guggy.com/logo_128.png',
522+
attachments: [
523+
{
524+
color: '#ff0000',
525+
text: 'Yay for gruggy!',
526+
ts: '2016-12-09T16:53:06.761Z',
527+
thumb_url: 'http://res.guggy.com/logo_128.png',
528+
message_link: 'https://google.com',
529+
collapsed: false,
530+
author_name: 'Bradley Hilton',
531+
author_link: 'https://rocket.chat/',
532+
author_icon: 'https://avatars.githubusercontent.com/u/850391?v=3',
533+
title: 'Attachment Example',
534+
title_link: 'https://youtube.com',
535+
title_link_download: true,
536+
image_url: 'http://res.guggy.com/logo_128.png',
537+
audio_url: 'http://www.w3schools.com/tags/horse.mp3',
538+
video_url: 'http://www.w3schools.com/tags/movie.mp4',
539+
fields: [
540+
{
541+
short: true,
542+
title: 'This is attachment field title',
543+
},
544+
],
545+
},
546+
],
547+
})
548+
.expect('Content-Type', 'application/json')
549+
.expect(400)
550+
.expect((res) => {
551+
expect(res.body).to.have.property('success', false);
552+
expect(res.body).to.have.property('error');
553+
})
554+
.end(done);
555+
});
556+
557+
it('attachment.fields should work fine when value and title are provided', (done) => {
558+
void request
559+
.post(api('chat.postMessage'))
560+
.set(credentials)
561+
.send({
562+
channel: testChannel.name,
563+
text: 'Sample message',
564+
attachments: [
565+
{
566+
text: 'This is attachment field',
567+
color: '#764FA5',
568+
fields: [{ short: true, value: 'This is value', title: 'This is title' }],
569+
},
570+
],
571+
})
572+
.expect('Content-Type', 'application/json')
573+
.expect(200)
574+
.expect((res) => {
575+
expect(res.body).to.have.property('success', true);
576+
expect(res.body).to.not.have.property('error');
577+
expect(res.body).to.have.nested.property('message.msg', 'Sample message');
578+
expect(res.body).to.have.nested.property('message.attachments').to.be.an('array');
579+
expect(res.body).to.have.nested.property('message.attachments[0].fields').to.be.an('array');
580+
expect(res.body).to.have.nested.property('message.attachments[0].fields[0].short', true);
581+
expect(res.body).to.have.nested.property('message.attachments[0].fields[0].value', 'This is value');
582+
expect(res.body).to.have.nested.property('message.attachments[0].fields[0].title', 'This is title');
583+
})
584+
.end(done);
585+
});
586+
467587
it('should return statusCode 200 when postMessage successfully', (done) => {
468588
void request
469589
.post(api('chat.postMessage'))

0 commit comments

Comments
 (0)