Skip to content

gh-150633: properly handle imports with null bytes in names#150634

Merged
serhiy-storchaka merged 5 commits into
python:mainfrom
KowalskiThomas:kowalski/fix-properly-handle-imports-with-null-bytes-in-names
Jun 8, 2026
Merged

gh-150633: properly handle imports with null bytes in names#150634
serhiy-storchaka merged 5 commits into
python:mainfrom
KowalskiThomas:kowalski/fix-properly-handle-imports-with-null-bytes-in-names

Conversation

@KowalskiThomas
Copy link
Copy Markdown
Contributor

@KowalskiThomas KowalskiThomas commented May 30, 2026

@KowalskiThomas KowalskiThomas changed the title fix: properly handle imports with null bytes in names gh-150633: properly handle imports with null bytes in names May 30, 2026
@KowalskiThomas KowalskiThomas marked this pull request as ready for review June 1, 2026 14:15
@@ -0,0 +1,3 @@
Fix :func:`__import__` accepting module names with embedded null bytes, which
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is for frozen modules only, right? I cannot reproduce this for other modules.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, indeed. Initially I had added a check rejecting null bytes in every __import__ call (which I think matches what is discussed in the issue) but I eventually went for a more targeted fix. I'll change the news entry for now so it matches what the code does.

Comment thread Lib/test/test_import/__init__.py Outdated
# lead to duplicates in sys.modules
before = set(sys.modules.keys())
with self.assertRaises(ModuleNotFoundError):
__import__('codecs\x00junk')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I get ModuleNotFoundError on non-installed Python. The bug is only reproducible om the system Python. Can we make the test more reproducible?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I changed it to zipimport instead of codecs, I have it failing on my non-installed build as well now.

% ./python.exe -m unittest Lib.test.test_import.ImportTests.test_import_null_byte_in_name_raises_ModuleNotFoundError -v
test_import_null_byte_in_name_raises_ModuleNotFoundError (Lib.test.test_import.ImportTests.test_import_null_byte_in_name_raises_ModuleNotFoundError) ... FAIL

======================================================================
FAIL: test_import_null_byte_in_name_raises_ModuleNotFoundError (Lib.test.test_import.ImportTests.test_import_null_byte_in_name_raises_ModuleNotFoundError)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/thomas.kowalski/Documents/cpython/Lib/test/test_import/__init__.py", line 371, in test_import_null_byte_in_name_raises_ModuleNotFoundError
    with self.assertRaises(ModuleNotFoundError):
         ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
AssertionError: ModuleNotFoundError not raised

----------------------------------------------------------------------
Ran 1 test in 0.005s

FAILED (failures=1)

Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 8, 2026
@serhiy-storchaka serhiy-storchaka merged commit 54de547 into python:main Jun 8, 2026
65 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @KowalskiThomas for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Jun 8, 2026

GH-151100 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 8, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Jun 8, 2026

GH-151101 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 8, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Jun 8, 2026

GH-151102 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jun 8, 2026
def test_import_null_byte_in_name_raises_ModuleNotFoundError(self):
# gh-150633: module names containing null bytes should not
# lead to duplicates in sys.modules
before = set(sys.modules.keys())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You do not need the .keys()

@warsaw
Copy link
Copy Markdown
Member

warsaw commented Jun 8, 2026

Thanks for the contribution! I have a small PR to improve a style thing.

serhiy-storchaka pushed a commit that referenced this pull request Jun 8, 2026
…porting frozen modules (GH-150634) (GH-151102)

(cherry picked from commit 54de547)

Co-authored-by: Thomas Kowalski <thom.kowa@gmail.com>
serhiy-storchaka pushed a commit that referenced this pull request Jun 8, 2026
…porting frozen modules (GH-150634) (GH-151101)

(cherry picked from commit 54de547)

Co-authored-by: Thomas Kowalski <thom.kowa@gmail.com>
serhiy-storchaka pushed a commit that referenced this pull request Jun 8, 2026
…porting frozen modules (GH-150634) (GH-151100)

(cherry picked from commit 54de547)

Co-authored-by: Thomas Kowalski <thom.kowa@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Module names containing null bytes bypass the sys.modules cache

3 participants