Skip to content

bpo-35278: Sanitize tempfile prefix to prevent directory treversal #10627

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Thorleon
Copy link

@Thorleon Thorleon commented Nov 21, 2018

  • Fixed _sanitize_params function in order to detect malicious path
  • Added tests

https://bugs.python.org/issue35278

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

@@ -121,6 +127,12 @@ def _sanitize_params(prefix, suffix, dir):
prefix = template
else:
prefix = _os.fsencode(template)
if output_type is str:
if any(sep in prefix for sep in _path_separators):
raise ValueError("Prefix contains system separator character")
Copy link
Member

Choose a reason for hiding this comment

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

It is called a pathname components separator.

Comment on lines +130 to +131
if output_type is str:
if any(sep in prefix for sep in _path_separators):
Copy link
Member

Choose a reason for hiding this comment

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

You can simply test that os.path.dirname(prefix+suffix) is empty.

Copy link
Member

Choose a reason for hiding this comment

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

I would also prefer to reuse dirname() function.


def test_throw_exception_on_encoded_path_separator_detection(self):
with self.assertRaises(ValueError):
tempfile.mkstemp(prefix=f"{os.fsencode(os.sep)}home")
Copy link
Member

Choose a reason for hiding this comment

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

It does not work as you expected. On Posix it generates prefix "b'/'home".

Use os.fsencode(f"{os.sep}home").

@unittest.skipIf(os.altsep is None, "os.altsep is not present on this platform")
def test_throw_exception_on_alternative_path_separator_detection(self):
with self.assertRaises(ValueError):
tempfile.mkstemp(prefix=f"{os.altsep}home")
Copy link
Member

Choose a reason for hiding this comment

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

You need also a test for bytes prefix with altsep.

Add also tests for suffix containing a pathname components separator.

Add also tests for other functions that use _sanitize_params().

It will be too wasteful to add 24 separate test methods, so you should test several cases per method method.

@bedevere-app
Copy link

bedevere-app bot commented Mar 2, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Apr 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes stale Stale PR or inactive for long period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants