-
Notifications
You must be signed in to change notification settings - Fork 1
fix: validate input is a real PDF before encryption (closes #24) #53
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
Open
ambicuity
wants to merge
1
commit into
SUPAIDEAS:main
Choose a base branch
from
ambicuity:fix/issue-24-validate-real-pdf-input
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,61 +1,89 @@ | ||
| from unittest import TestCase | ||
| from unittest.mock import patch, mock_open | ||
| from unittest.mock import MagicMock, mock_open, patch | ||
|
|
||
| from pypdf.errors import PdfReadError | ||
|
|
||
| from passifypdf.encryptpdf import encrypt_pdf | ||
|
|
||
|
|
||
| class TestPdfUnitTests(TestCase): | ||
|
|
||
| @patch('passifypdf.encryptpdf.PdfReader') | ||
| @patch('passifypdf.encryptpdf.PdfWriter') | ||
| @patch('passifypdf.encryptpdf.Path') # Mock Path | ||
| @patch('builtins.open', new_callable=mock_open) | ||
| @patch("passifypdf.encryptpdf.PdfReader") | ||
| @patch("passifypdf.encryptpdf.PdfWriter") | ||
| @patch("passifypdf.encryptpdf.Path") | ||
| @patch("builtins.open", new_callable=mock_open) | ||
| def test_encrypt_pdf(self, mock_file, mock_path_cls, mock_writer_cls, mock_reader_cls): | ||
| # Setup mocks | ||
| mock_path_instance = mock_path_cls.return_value | ||
| mock_path_instance.exists.return_value = True | ||
| mock_path_instance.is_file.return_value = True | ||
|
|
||
| mock_source_file = MagicMock() | ||
| mock_source_file.read.return_value = b"%PDF-" | ||
| mock_path_instance.open.return_value.__enter__.return_value = mock_source_file | ||
|
|
||
| mock_reader_instance = mock_reader_cls.return_value | ||
| mock_reader_instance.pages = ['page1', 'page2'] | ||
| mock_reader_instance.pages = ["page1", "page2"] | ||
|
|
||
| mock_writer_instance = mock_writer_cls.return_value | ||
|
|
||
| # Call the function | ||
|
|
||
| encrypt_pdf("input.pdf", "output.pdf", "secret") | ||
|
|
||
| # Verify Path existence check | ||
|
|
||
| mock_path_cls.assert_called_with("input.pdf") | ||
| mock_path_instance.exists.assert_called() | ||
| mock_path_instance.is_file.assert_called() | ||
| mock_path_instance.exists.assert_called_once() | ||
| mock_path_instance.is_file.assert_called_once() | ||
| mock_path_instance.open.assert_called_once_with("rb") | ||
|
|
||
| # Verify PdfReader was called with the path object | ||
| mock_reader_cls.assert_called_with(mock_path_instance) | ||
|
|
||
| # Verify pages were added | ||
|
|
||
| self.assertEqual(mock_writer_instance.add_page.call_count, 2) | ||
| mock_writer_instance.add_page.assert_any_call('page1') | ||
| mock_writer_instance.add_page.assert_any_call('page2') | ||
|
|
||
| # Verify encryption | ||
| mock_writer_instance.add_page.assert_any_call("page1") | ||
| mock_writer_instance.add_page.assert_any_call("page2") | ||
|
|
||
| mock_writer_instance.encrypt.assert_called_with("secret") | ||
|
|
||
| # Verify file write | ||
|
|
||
| mock_file.assert_called_with("output.pdf", "wb") | ||
| mock_writer_instance.write.assert_called_with(mock_file()) | ||
|
|
||
| @patch('passifypdf.encryptpdf.Path') | ||
| @patch("passifypdf.encryptpdf.Path") | ||
| def test_encrypt_pdf_file_not_found(self, mock_path_cls): | ||
| mock_path_instance = mock_path_cls.return_value | ||
| mock_path_instance.exists.return_value = False | ||
|
|
||
| with self.assertRaises(FileNotFoundError): | ||
| encrypt_pdf("nonexistent.pdf", "output.pdf", "secret") | ||
|
|
||
| @patch('passifypdf.encryptpdf.Path') | ||
| @patch("passifypdf.encryptpdf.Path") | ||
| def test_encrypt_pdf_is_directory(self, mock_path_cls): | ||
| mock_path_instance = mock_path_cls.return_value | ||
| mock_path_instance.exists.return_value = True | ||
| mock_path_instance.is_file.return_value = False | ||
|
|
||
| with self.assertRaises(IsADirectoryError): | ||
| encrypt_pdf("directory", "output.pdf", "secret") | ||
|
|
||
| @patch("passifypdf.encryptpdf.Path") | ||
| def test_encrypt_pdf_rejects_non_pdf_magic_bytes(self, mock_path_cls): | ||
| mock_path_instance = mock_path_cls.return_value | ||
| mock_path_instance.exists.return_value = True | ||
| mock_path_instance.is_file.return_value = True | ||
|
|
||
| mock_source_file = MagicMock() | ||
| mock_source_file.read.return_value = b"NOTPD" | ||
| mock_path_instance.open.return_value.__enter__.return_value = mock_source_file | ||
|
|
||
| with self.assertRaisesRegex(ValueError, "Input file 'input.pdf' is not a valid PDF file\\."): | ||
| encrypt_pdf("input.pdf", "output.pdf", "secret") | ||
|
|
||
| @patch("passifypdf.encryptpdf.PdfReader", side_effect=PdfReadError("broken pdf")) | ||
| @patch("passifypdf.encryptpdf.Path") | ||
| def test_encrypt_pdf_rejects_pdfreader_parse_failure(self, mock_path_cls, _mock_reader): | ||
| mock_path_instance = mock_path_cls.return_value | ||
| mock_path_instance.exists.return_value = True | ||
| mock_path_instance.is_file.return_value = True | ||
|
|
||
| mock_source_file = MagicMock() | ||
| mock_source_file.read.return_value = b"%PDF-" | ||
| mock_path_instance.open.return_value.__enter__.return_value = mock_source_file | ||
|
|
||
| with self.assertRaisesRegex(ValueError, "Input file 'input.pdf' is not a valid PDF file\\."): | ||
| encrypt_pdf("input.pdf", "output.pdf", "secret") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic-byte check is overly strict: some valid PDFs can include leading whitespace/bytes before the "%PDF-" header (the header is typically expected near the start, not necessarily at byte 0). Reading and comparing only the first 5 bytes may incorrectly reject such files. Consider scanning the first ~1KB for the "%PDF-" marker (or skipping leading whitespace) before failing, and keep the error message the same.