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 Password-protected PDF not handled correctly #42572

Merged
merged 25 commits into from
Jun 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resolve(false);
resolve(false);
return;

then remove the else block

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
Loading