-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[BREAK] Validate incoming message schema #9922
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c7a169e
Added validation in chat.postMessage endpoint
MarcosSpessatto b42d221
fix required parameters and add more tests cases.
MarcosSpessatto c8d047a
Added the same validation to chat.sendMessage method too.
MarcosSpessatto ad512fb
changed validation level, set in sendMessage method
MarcosSpessatto 2c2f4e7
Merge branch 'develop' into fix-rest-api-chat-postmessage-validations
MarcosSpessatto 392729f
Merge commit 'a9fb4da5c847a456990a5d60369f0f52ff4a8bd8' into fix-rest…
MarcosSpessatto be4a6ef
Improvements in the sendMessage function, removed underscore uses, an…
MarcosSpessatto e957d88
Merge branch 'develop' into fix-rest-api-chat-postmessage-validations
MarcosSpessatto c770bf4
Fix test in chat.sendMessage endpoint, and fix error in sendMessage f…
MarcosSpessatto d948c97
fix review
ggazzo 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 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 |
---|---|---|
|
@@ -7,7 +7,7 @@ RocketChat.API.v1.addRoute('chat.delete', { authRequired: true }, { | |
asUser: Match.Maybe(Boolean) | ||
})); | ||
|
||
const msg = RocketChat.models.Messages.findOneById(this.bodyParams.msgId, { fields: { u: 1, rid: 1 }}); | ||
const msg = RocketChat.models.Messages.findOneById(this.bodyParams.msgId, { fields: { u: 1, rid: 1 } }); | ||
|
||
if (!msg) { | ||
return RocketChat.API.v1.failure(`No message found with the id of "${ this.bodyParams.msgId }".`); | ||
|
@@ -106,6 +106,67 @@ RocketChat.API.v1.addRoute('chat.pinMessage', { authRequired: true }, { | |
|
||
RocketChat.API.v1.addRoute('chat.postMessage', { authRequired: true }, { | ||
post() { | ||
const validateBodyAttachments = (attachments) => { | ||
|
||
const validateAttachmentsFields = (attachmentFields) => { | ||
check(attachmentFields, Match.ObjectIncluding({ | ||
short: Match.Maybe(Boolean), | ||
title: String, | ||
value: String | ||
})); | ||
}; | ||
|
||
const validateAttachment = (attachment) => { | ||
check(attachment, Match.ObjectIncluding({ | ||
color: Match.Maybe(String), | ||
text: Match.Maybe(String), | ||
ts: Match.Maybe(String), | ||
thumb_url: Match.Maybe(String), | ||
message_link: Match.Maybe(String), | ||
collapsed: Match.Maybe(Boolean), | ||
author_name: Match.Maybe(String), | ||
author_link: Match.Maybe(String), | ||
author_icon: Match.Maybe(String), | ||
title: Match.Maybe(String), | ||
title_link: Match.Maybe(String), | ||
title_link_download: Match.Maybe(Boolean), | ||
image_url: Match.Maybe(String), | ||
audio_url: Match.Maybe(String), | ||
video_url: Match.Maybe(String) | ||
})); | ||
|
||
if (attachment.fields.length) { | ||
attachment.fields.map(validateAttachmentsFields); | ||
} | ||
}; | ||
|
||
attachments.map(validateAttachment); | ||
}; | ||
|
||
const validateBodyParams = (bodyParams) => { | ||
check(bodyParams, Match.ObjectIncluding({ | ||
roomId: Match.Maybe(String), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this one REQUIRED? |
||
channel: Match.Maybe(String), | ||
text: Match.Maybe(String), | ||
alias: Match.Maybe(String), | ||
emoji: Match.Maybe(String), | ||
avatar: Match.Maybe(String), | ||
attachments: Match.Maybe(Array) | ||
})); | ||
|
||
if (Array.isArray(bodyParams.attachments) && bodyParams.attachments.length) { | ||
validateBodyAttachments(bodyParams.attachments); | ||
} | ||
}; | ||
|
||
try { | ||
validateBodyParams(this.bodyParams); | ||
} catch (error) { | ||
return RocketChat.API.v1.failure({ | ||
error: error.message | ||
}); | ||
} | ||
|
||
const messageReturn = processWebhookMessage(this.bodyParams, this.user, undefined, true)[0]; | ||
|
||
if (!messageReturn) { | ||
|
This file contains 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 |
---|---|---|
|
@@ -32,7 +32,7 @@ describe('[Chat]', function() { | |
author_icon: 'https://avatars.githubusercontent.com/u/850391?v=3', | ||
title: 'Attachment Example', | ||
title_link: 'https://youtube.com', | ||
title_link_download: 'https://rocket.chat/download', | ||
title_link_download: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can have more tests here, with lots of different variations and some fail case. Can you do that, please? |
||
image_url: 'http://res.guggy.com/logo_128.png', | ||
audio_url: 'http://www.w3schools.com/tags/horse.mp3', | ||
video_url: 'http://www.w3schools.com/tags/movie.mp4', | ||
|
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.
ts
is a String? 🤔