diff --git a/src/libs/isPdfFilePasswordProtected/index.tsx b/src/libs/isPdfFilePasswordProtected/index.tsx new file mode 100644 index 000000000000..19ce44b9b8e3 --- /dev/null +++ b/src/libs/isPdfFilePasswordProtected/index.tsx @@ -0,0 +1,39 @@ +import * as pdfjsLib from 'pdfjs-dist'; +import type {FileObject} from '@components/AttachmentModal'; + +const isPdfFilePasswordProtected = (file: FileObject): Promise => + new Promise((resolve) => { + const reader = new FileReader(); + + reader.onload = (event) => { + const arrayBuffer = event.target?.result; + if (!arrayBuffer) { + resolve(false); + return; + } + try { + const loadingTask = pdfjsLib.getDocument({data: arrayBuffer}); + loadingTask.promise.then( + () => { + resolve(false); + }, + (error) => { + if (error.name === 'PasswordException') { + resolve(true); + return; + } + resolve(false); + }, + ); + } catch (error) { + resolve(false); + } + }; + + reader.onerror = () => { + resolve(false); + }; + + reader.readAsArrayBuffer(file as Blob); + }); +export default isPdfFilePasswordProtected; diff --git a/src/pages/iou/request/step/IOURequestStepScan/index.tsx b/src/pages/iou/request/step/IOURequestStepScan/index.tsx index 7efaa0882591..00e0e14ee27c 100644 --- a/src/pages/iou/request/step/IOURequestStepScan/index.tsx +++ b/src/pages/iou/request/step/IOURequestStepScan/index.tsx @@ -25,6 +25,7 @@ import useWindowDimensions from '@hooks/useWindowDimensions'; import * as Browser from '@libs/Browser'; import * as FileUtils from '@libs/fileDownload/FileUtils'; import getCurrentPosition from '@libs/getCurrentPosition'; +import isPdfFilePasswordProtected from '@libs/isPdfFilePasswordProtected'; import Log from '@libs/Log'; import Navigation from '@libs/Navigation/Navigation'; import * as OptionsListUtils from '@libs/OptionsListUtils'; @@ -212,6 +213,15 @@ function IOURequestStepScan({ return false; } + if (fileExtension === 'pdf') { + return isPdfFilePasswordProtected(file).then((isProtected: boolean) => { + if (isProtected) { + setUploadReceiptError(true, 'attachmentPicker.wrongFileType', 'attachmentPicker.protectedPDFNotSupported'); + return false; + } + return true; + }); + } return true; }) .catch(() => {