Skip to content
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

[FIX] Not valid relative URLs on message attachments #15651

Merged
merged 2 commits into from Nov 20, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 27 additions & 13 deletions app/lib/server/functions/sendMessage.js
Expand Up @@ -6,7 +6,7 @@ import { callbacks } from '../../../callbacks';
import { Messages } from '../../../models';
import { Apps } from '../../../apps/server';
import { Markdown } from '../../../markdown/server';
import { isURL } from '../../../utils/lib/isURL';
import { isURL, isRelativeURL } from '../../../utils/lib/isURL';
import { FileUpload } from '../../../file-upload/server';

/**
Expand All @@ -18,7 +18,7 @@ import { FileUpload } from '../../../file-upload/server';
* is going to be rendered in the href attribute of a
* link.
*/
const ValidLinkParam = Match.Where((value) => {
const ValidFullURLParam = Match.Where((value) => {
check(value, String);

if (!isURL(value) && !value.startsWith(FileUpload.getPath())) {
Expand All @@ -32,6 +32,20 @@ const ValidLinkParam = Match.Where((value) => {
return true;
});

const ValidPartialURLParam = Match.Where((value) => {
check(value, String);

if (!isRelativeURL(value) && !isURL(value) && !value.startsWith(FileUpload.getPath())) {
throw new Error('Invalid href value provided');
}

if (/^javascript:/i.test(value)) {
throw new Error('Invalid href value provided');
}

return true;
});

const objectMaybeIncluding = (types) => Match.Where((value) => {
Object.keys(types).forEach((field) => {
if (value[field] != null) {
Expand Down Expand Up @@ -63,8 +77,8 @@ const validateAttachmentsActions = (attachmentActions) => {
check(attachmentActions, objectMaybeIncluding({
type: String,
text: String,
url: ValidLinkParam,
image_url: ValidLinkParam,
url: ValidFullURLParam,
image_url: ValidFullURLParam,
is_webview: Boolean,
webview_height_ratio: String,
msg: String,
Expand All @@ -77,26 +91,26 @@ const validateAttachment = (attachment) => {
color: String,
text: String,
ts: Match.OneOf(String, Match.Integer),
thumb_url: ValidLinkParam,
thumb_url: ValidFullURLParam,
button_alignment: String,
actions: [Match.Any],
message_link: ValidLinkParam,
message_link: ValidFullURLParam,
collapsed: Boolean,
author_name: String,
author_link: ValidLinkParam,
author_icon: ValidLinkParam,
author_link: ValidFullURLParam,
author_icon: ValidFullURLParam,
title: String,
title_link: ValidLinkParam,
title_link: ValidFullURLParam,
title_link_download: Boolean,
image_dimensions: Object,
image_url: ValidLinkParam,
image_url: ValidFullURLParam,
image_preview: String,
image_type: String,
image_size: Number,
audio_url: ValidLinkParam,
audio_url: ValidFullURLParam,
audio_type: String,
audio_size: Number,
video_url: ValidLinkParam,
video_url: ValidFullURLParam,
video_type: String,
video_size: Number,
fields: [Match.Any],
Expand All @@ -120,7 +134,7 @@ const validateMessage = (message) => {
text: String,
alias: String,
emoji: String,
avatar: ValidLinkParam,
avatar: ValidPartialURLParam,
attachments: [Match.Any],
}));

Expand Down
1 change: 1 addition & 0 deletions app/utils/lib/isURL.js
@@ -1 +1,2 @@
export const isURL = (str) => /^(https?:\/\/|data:)/.test(str);
export const isRelativeURL = (str) => /^[^\/]+\/[^\/].*$|^\/[^\/].*$/gmi.test(str);