Skip to content

Commit

Permalink
Merge pull request #42572 from tienifr/fix/42078
Browse files Browse the repository at this point in the history
fix Password-protected PDF not handled correctly
  • Loading branch information
Julesssss authored Jun 18, 2024
2 parents 9ec57c0 + 61666db commit f0f3b03
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/libs/isPdfFilePasswordProtected/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as pdfjsLib from 'pdfjs-dist';
import type {FileObject} from '@components/AttachmentModal';

const isPdfFilePasswordProtected = (file: FileObject): Promise<boolean> =>
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;
10 changes: 10 additions & 0 deletions src/pages/iou/request/step/IOURequestStepScan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(() => {
Expand Down

0 comments on commit f0f3b03

Please sign in to comment.