-
Notifications
You must be signed in to change notification settings - Fork 95
Fix path-traversal vulnerability in emergency P2P checkpoint service #3105
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
Closed
+198
−3
Closed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| """Unit tests for P2PNode service.""" | ||
|
|
||
| import functools | ||
| import os | ||
| import threading | ||
| from unittest import mock | ||
|
|
||
|
|
@@ -23,6 +24,151 @@ | |
| from orbax.checkpoint.experimental.emergency.p2p import service | ||
|
|
||
|
|
||
| class SafePathJoinTest(absltest.TestCase): | ||
| """Tests for the ``_safe_path_join`` path-containment helper.""" | ||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| self.base = epath.Path(self.create_tempdir().full_path) | ||
|
|
||
| def test_valid_relative_path(self): | ||
| result = service._safe_path_join(self.base, '1/file1') | ||
| self.assertIsNotNone(result) | ||
| self.assertEqual(str(result), str(self.base / '1/file1')) | ||
|
|
||
| def test_valid_nested_path(self): | ||
| result = service._safe_path_join(self.base, '1/subdir/file2') | ||
| self.assertIsNotNone(result) | ||
|
|
||
| def test_empty_rel_path_rejected(self): | ||
| self.assertIsNone(service._safe_path_join(self.base, '')) | ||
|
|
||
| def test_absolute_posix_path_rejected(self): | ||
| self.assertIsNone(service._safe_path_join(self.base, '/etc/passwd')) | ||
|
|
||
| def test_parent_traversal_rejected(self): | ||
| self.assertIsNone(service._safe_path_join(self.base, '../secret')) | ||
|
|
||
| def test_deep_parent_traversal_rejected(self): | ||
| self.assertIsNone( | ||
| service._safe_path_join(self.base, 'a/b/../../../etc/passwd') | ||
| ) | ||
|
|
||
| def test_traversal_to_siblings_rejected(self): | ||
| # Reference the base directory's sibling by name; must still be rejected. | ||
| sibling = os.path.basename(os.path.dirname(str(self.base))) + '_evil' | ||
| self.assertIsNone(service._safe_path_join(self.base, f'../{sibling}/x')) | ||
|
|
||
| def test_symlink_escape_rejected(self): | ||
| # A symlink inside ``base`` that points outside ``base`` must not be | ||
| # writable through ``_safe_path_join``. | ||
| outside = epath.Path(self.create_tempdir().full_path) | ||
| link_dir = self.base / 'link' | ||
| try: | ||
| os.symlink(str(outside), str(link_dir)) | ||
| except (OSError, NotImplementedError): | ||
| self.skipTest('Symlinks not supported on this platform') | ||
| self.assertIsNone(service._safe_path_join(self.base, 'link/pwn')) | ||
|
|
||
|
|
||
| class PathTraversalRegressionTest(absltest.TestCase): | ||
| """Regression tests for CVE-class path-traversal in the P2P service.""" | ||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| self.temp_dir = epath.Path(self.create_tempdir().full_path) | ||
| mock_server_cls = self.enter_context( | ||
| mock.patch.object(service, '_ThreadingTCPServer', autospec=True) | ||
| ) | ||
| server = mock_server_cls.return_value | ||
| server.server_address = ('localhost', 12345) | ||
| self.enter_context( | ||
| mock.patch.object(service.multihost, 'process_index', return_value=0) | ||
| ) | ||
| self.enter_context( | ||
| mock.patch.object( | ||
| service.socket, | ||
| 'getaddrinfo', | ||
| return_value=[(service.socket.AF_INET, 0, 0, '', ('127.0.0.1', 0))], | ||
| ) | ||
| ) | ||
| self.node = service.P2PNode(directory=self.temp_dir) | ||
|
|
||
| @mock.patch.object(service.protocol.TCPMessage, 'send_file', autospec=True) | ||
| def test_handle_download_blocks_traversal(self, mock_send_file): | ||
| """Server must reject ``..`` components regardless of form.""" | ||
| sock = mock.Mock() | ||
| for bad in [ | ||
| '../unsafe', | ||
| 'a/b/../../../etc/passwd', | ||
| '/etc/passwd', | ||
| '', | ||
| ]: | ||
| mock_send_file.reset_mock() | ||
| self.node.handle_download(sock, {'rel_path': bad}) | ||
| mock_send_file.assert_called_once_with(sock, epath.Path('__INVALID__')) | ||
|
|
||
| @mock.patch.object(service.shutil, 'rmtree', autospec=True) | ||
| @mock.patch.object(service.shutil, 'move', autospec=True) | ||
| @mock.patch.object(service.time, 'time', autospec=True) | ||
| @mock.patch.object(service.protocol.TCPClient, 'request', autospec=True) | ||
| @mock.patch.object(service.protocol.TCPClient, 'download', autospec=True) | ||
| def test_fetch_shard_from_peer_rejects_malicious_manifest( | ||
| self, | ||
| mock_download, | ||
| mock_request, | ||
| unused_mock_time, | ||
| unused_mock_move, | ||
| unused_mock_rmtree, | ||
| ): | ||
| """Client must refuse to download peer-supplied traversal paths. | ||
|
|
||
| A malicious peer returns a manifest whose ``rel_path`` tries to escape the | ||
| staging directory (e.g., writing a ``.pth`` file into site-packages). | ||
| ``fetch_shard_from_peer`` must abort before any ``download`` call. | ||
|
|
||
| Args: | ||
| mock_download: Mock for download. | ||
| mock_request: Mock for request. | ||
| unused_mock_time: Unused mock for time. | ||
| unused_mock_move: Unused mock for move. | ||
| unused_mock_rmtree: Unused mock for rmtree. | ||
| """ | ||
| mock_request.return_value = [ | ||
| {'rel_path': '../../evil.pth', 'size': 10}, | ||
| ] | ||
| self.assertFalse(self.node.fetch_shard_from_peer('peer', 123, 1, 10)) | ||
| mock_download.assert_not_called() | ||
|
|
||
| @mock.patch.object(service.shutil, 'rmtree', autospec=True) | ||
| @mock.patch.object(service.shutil, 'move', autospec=True) | ||
| @mock.patch.object(service.time, 'time', autospec=True) | ||
| @mock.patch.object(service.protocol.TCPClient, 'request', autospec=True) | ||
| @mock.patch.object(service.protocol.TCPClient, 'download', autospec=True) | ||
| def test_fetch_shard_from_peer_rejects_absolute_path( | ||
| self, | ||
| mock_download, | ||
| mock_request, | ||
| unused_mock_time, | ||
| unused_mock_move, | ||
| unused_mock_rmtree, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| ): | ||
| """Client must refuse manifest entries with an absolute ``rel_path``. | ||
|
|
||
| Args: | ||
| mock_download: Mock for download. | ||
| mock_request: Mock for request. | ||
| unused_mock_time: Unused mock for time. | ||
| unused_mock_move: Unused mock for move. | ||
| unused_mock_rmtree: Unused mock for rmtree. | ||
| """ | ||
| mock_request.return_value = [ | ||
| {'rel_path': '/etc/passwd', 'size': 10}, | ||
| ] | ||
| self.assertFalse(self.node.fetch_shard_from_peer('peer', 123, 1, 10)) | ||
| mock_download.assert_not_called() | ||
|
|
||
|
|
||
| class NodeHandlerTest(absltest.TestCase): | ||
|
|
||
| def setUp(self): | ||
|
|
||
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.
to be consistent with code style.