Skip to content

Commit

Permalink
[FIX] Empty white list enables all media types upload (#1080)
Browse files Browse the repository at this point in the history
* Create utils to media (canUpload)

* Fix variable name
  • Loading branch information
djorkaeffalexandre authored and diegolmello committed Jul 29, 2019
1 parent d250f77 commit 8cfdf86
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
7 changes: 5 additions & 2 deletions app/containers/MessageBox/UploadModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Button from '../Button';
import I18n from '../../i18n';
import sharedStyles from '../../views/Styles';
import { isIOS } from '../../utils/deviceInfo';
import { canUploadFile } from '../../utils/media';
import {
COLOR_PRIMARY, COLOR_BACKGROUND_CONTAINER, COLOR_WHITE, COLOR_DANGER
} from '../../constants/colors';
Expand Down Expand Up @@ -291,9 +292,11 @@ export default class UploadModal extends Component {
}

render() {
const { window: { width }, isVisible, close } = this.props;
const {
window: { width }, isVisible, close, file, FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize
} = this.props;
const { name, description } = this.state;
const showError = !this.canUploadFile();
const showError = !canUploadFile(file, { FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize });
return (
<Modal
isVisible={isVisible}
Expand Down
23 changes: 23 additions & 0 deletions app/utils/media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const canUploadFile = (file, serverInfo) => {
const { FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize } = serverInfo;
if (!(file && file.path)) {
return true;
}
if (file.size > FileUpload_MaxFileSize) {
return false;
}
// if white list is empty, all media types are enabled
if (!FileUpload_MediaTypeWhiteList) {
return true;
}
const allowedMime = FileUpload_MediaTypeWhiteList.split(',');
if (allowedMime.includes(file.mime)) {
return true;
}
const wildCardGlob = '/*';
const wildCards = allowedMime.filter(item => item.indexOf(wildCardGlob) > 0);
if (wildCards.includes(file.mime.replace(/(\/.*)$/, wildCardGlob))) {
return true;
}
return false;
};
34 changes: 5 additions & 29 deletions app/views/ShareListView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { isIOS, isAndroid } from '../../utils/deviceInfo';
import I18n from '../../i18n';
import { CustomIcon } from '../../lib/Icons';
import log from '../../utils/log';
import { canUploadFile } from '../../utils/media';
import DirectoryItem, { ROW_HEIGHT } from '../../presentation/DirectoryItem';
import ServerItem from '../../presentation/ServerItem';
import { CloseShareExtensionButton, CustomHeaderButtons, Item } from '../../containers/HeaderButton';
Expand Down Expand Up @@ -130,7 +131,7 @@ export default class ShareListView extends React.Component {
name: data.filename,
description: '',
size: data.size,
type: mime.lookup(data.path),
mime: mime.lookup(data.path),
store: 'Uploads',
path: isIOS ? data.path : `file://${ data.path }`
};
Expand Down Expand Up @@ -187,6 +188,7 @@ export default class ShareListView extends React.Component {
}

getSubscriptions = (server, fileInfo) => {
const { fileInfo: fileData } = this.state;
const { serversDB } = database.databases;

if (server) {
Expand All @@ -199,7 +201,7 @@ export default class ShareListView extends React.Component {
chats: this.chats ? this.chats.slice() : [],
servers: this.servers ? this.servers.slice() : [],
loading: false,
showError: !this.canUploadFile(serverInfo, fileInfo),
showError: !canUploadFile(fileInfo || fileData, serverInfo),
serverInfo
});
this.forceUpdate();
Expand Down Expand Up @@ -227,32 +229,6 @@ export default class ShareListView extends React.Component {
});
}

canUploadFile = (serverInfo, fileInfo) => {
const { fileInfo: fileData } = this.state;
const file = fileInfo || fileData;
const { FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize } = serverInfo;

if (!(file && file.path)) {
return true;
}
if (file.size > FileUpload_MaxFileSize) {
return false;
}
if (!FileUpload_MediaTypeWhiteList) {
return false;
}
const allowedMime = FileUpload_MediaTypeWhiteList.split(',');
if (allowedMime.includes(file.type)) {
return true;
}
const wildCardGlob = '/*';
const wildCards = allowedMime.filter(item => item.indexOf(wildCardGlob) > 0);
if (wildCards.includes(file.type.replace(/(\/.*)$/, wildCardGlob))) {
return true;
}
return false;
}

search = (text) => {
const result = database.objects('subscriptions').filtered('name CONTAINS[c] $0', text);
this.internalSetState({
Expand Down Expand Up @@ -423,7 +399,7 @@ export default class ShareListView extends React.Component {
<View style={[styles.container, styles.centered]}>
<Text style={styles.title}>{I18n.t(errorMessage)}</Text>
<CustomIcon name='circle-cross' size={120} style={styles.errorIcon} />
<Text style={styles.fileMime}>{ file.type }</Text>
<Text style={styles.fileMime}>{ file.mime }</Text>
</View>
</View>
);
Expand Down
2 changes: 1 addition & 1 deletion app/views/ShareView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default class ShareView extends React.Component {
renderPreview = () => {
const { fileInfo } = this.state;

const icon = fileInfo.type.match(/image/)
const icon = fileInfo.mime.match(/image/)
? <Image source={{ isStatic: true, uri: fileInfo.path }} style={styles.mediaImage} />
: (
<View style={styles.mediaIconContainer}>
Expand Down

0 comments on commit 8cfdf86

Please sign in to comment.