fix: block os.system via the nt module on Windows#2535
Open
nolanchic wants to merge 1 commit into
Open
Conversation
check_safer_result matches each dangerous function by 'module.name'. On Windows os.system is implemented in the nt module, so its __module__ is "nt" rather than "os", and the check silently let it through when os was authorized via additional_authorized_imports. DANGEROUS_FUNCTIONS already had posix.system for Linux/macOS but was missing nt.system. Closes huggingface#2232
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Closes #2232.
check_safer_resultblocks dangerous functions by matchingresult.__module__+"."+result.__name__againstDANGEROUS_FUNCTIONS. On Windows,os.systemis implemented in thentmodule, soos.system.__module__is"nt"— not"os". SinceDANGEROUS_FUNCTIONSlistedos.systemandposix.systembut notnt.system, the check silently letos.systemthrough on Windows when a user authorizedosviaadditional_authorized_imports=["os"].The default sandbox blocks
osentirely, so this only affects users who explicitly authorize it — but for them it is a sandbox escape on Windows.How
Add
nt.systemtoDANGEROUS_FUNCTIONS, mirroring the existingposix.systementry that covers Linux/macOS.Test
test_vulnerability_for_all_dangerous_functionsnow coversnt.systemon Windows. It usespytest.importorskip, sont.systemis skipped on Linux/macOS — exactly symmetric to howposix.systemis skipped on Windows.test_check_safer_result_blocks_nt_system, which is platform-independent: it passescheck_safer_resulta function with__module__ == "nt"/__name__ == "system"and asserts it is blocked. This keeps the regression covered on the Linux CI runners wherentis unavailable. Fails onmain, passes with this change.