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

Only allow certain filetypes for receipt uploads #29963

Merged
merged 7 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const CONST = {

// An arbitrary size, but the same minimum as in the PHP layer
MIN_SIZE: 240,

// Allowed extensions for receipts
ALLOWED_RECEIPT_EXTENSIONS: ['jpg', 'jpeg', 'gif', 'png', 'pdf', 'htm', 'html', 'text', 'rtf', 'doc', 'tif', 'tiff', 'msword', 'zip', 'xml', 'message'],
},

AUTO_AUTH_STATE: {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/iou/ReceiptSelector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function ReceiptSelector({route, transactionID, iou, report}) {

function validateReceipt(file) {
const {fileExtension} = FileUtils.splitExtensionFromFileName(lodashGet(file, 'name', ''));
if (_.contains(CONST.API_ATTACHMENT_VALIDATIONS.UNALLOWED_EXTENSIONS, fileExtension.toLowerCase())) {
if (!_.contains(CONST.API_ATTACHMENT_VALIDATIONS.ALLOWED_RECEIPT_EXTENSIONS, fileExtension.toLowerCase())) {
Gonals marked this conversation as resolved.
Show resolved Hide resolved
setUploadReceiptError(true, 'attachmentPicker.wrongFileType', 'attachmentPicker.notAllowedExtension');
return false;
}
Expand Down
23 changes: 23 additions & 0 deletions src/pages/iou/ReceiptSelector/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {ActivityIndicator, Alert, AppState, Text, View} from 'react-native';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {useCameraDevices} from 'react-native-vision-camera';
import lodashGet from 'lodash/get';
import _ from 'underscore';
Gonals marked this conversation as resolved.
Show resolved Hide resolved
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import {RESULTS} from 'react-native-permissions';
Expand Down Expand Up @@ -99,6 +100,25 @@ function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator})
};
}, []);

const validateReceipt = (file) => {
const {fileExtension} = FileUtils.splitExtensionFromFileName(lodashGet(file, 'name', ''));
if (!_.contains(CONST.API_ATTACHMENT_VALIDATIONS.ALLOWED_RECEIPT_EXTENSIONS, fileExtension.toLowerCase())) {
Gonals marked this conversation as resolved.
Show resolved Hide resolved
Alert.alert(translate('attachmentPicker.wrongFileType'), translate('attachmentPicker.notAllowedExtension'));
return false;
}

if (lodashGet(file, 'size', 0) > CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE) {
Alert.alert(translate('attachmentPicker.attachmentTooLarge'), translate('attachmentPicker.sizeExceeded'));
return false;
}

if (lodashGet(file, 'size', 0) < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) {
Alert.alert(translate('attachmentPicker.attachmentTooSmall'), translate('attachmentPicker.sizeNotMet'));
return false;
}
return true;
};

const askForPermissions = () => {
// There's no way we can check for the BLOCKED status without requesting the permission first
// https://github.com/zoontek/react-native-permissions/blob/a836e114ce3a180b2b23916292c79841a267d828/README.md?plain=1#L670
Expand Down Expand Up @@ -207,6 +227,9 @@ function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator})
onPress={() => {
openPicker({
onPicked: (file) => {
if (!validateReceipt(file)) {
return;
}
const filePath = file.uri;
IOU.setMoneyRequestReceipt(filePath, file.name);

Expand Down
Loading