-
Notifications
You must be signed in to change notification settings - Fork 229
Fix TypeError when using subprocess shell=True under strict Java Secu… #419
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -724,10 +724,46 @@ def test_invalid_args(self): | |
| "-c", 1]) | ||
|
|
||
|
|
||
| class SecurityManagerTestCase(unittest.TestCase): | ||
| def test_subprocess_shell_with_security_manager(self): | ||
| if not jython: | ||
|
Member
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. A nicer way to do this is the decorator Really appreciate you adding a test. |
||
| return | ||
|
|
||
| import java.lang.SecurityManager as SecurityManager | ||
|
Member
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. Just a note: SecurityManager is deprecated for removal. Perhaps, it's premature to address this here, before it's actually removed. Once it's removed and the definitive JDK version of the removal is known, we can add a conditional skip. |
||
| import java.lang.System as System | ||
| import java.security.AccessControlException as AccessControlException | ||
|
|
||
| class MySecurityManager(SecurityManager): | ||
| def checkRead(self, file, context=None): | ||
| if file == "/bin/sh" or file == "cmd.exe" or file.endswith("sh"): | ||
|
Member
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. This needs a little more thought. Ok, it's only for the test and we're not proposing a real security manager. But it fails on my Windows machine because It passes on the Windows CI because we do not enable the Fixed like that, and with |
||
| raise AccessControlException("Access denied: " + file) | ||
| def checkPermission(self, perm, context=None): | ||
| pass | ||
|
|
||
| original_sm = System.getSecurityManager() | ||
| System.setSecurityManager(MySecurityManager()) | ||
| try: | ||
| # We must force re-initialization of subprocess _shell_command | ||
| # because it was already initialized when the module was loaded. | ||
| saved_shell_command = subprocess._shell_command | ||
| subprocess._shell_command = None | ||
| subprocess._setup_platform() | ||
|
|
||
| try: | ||
| subprocess.Popen(["whoami"], shell=True) | ||
| self.fail("Expected OSError") | ||
| except OSError: | ||
| pass | ||
| except TypeError as e: | ||
| self.fail("Caught TypeError instead of OSError: " + str(e)) | ||
| finally: | ||
| System.setSecurityManager(original_sm) | ||
| subprocess._shell_command = saved_shell_command | ||
|
|
||
| def test_main(): | ||
| # Spawning many new jython processes takes a long time | ||
| test_support.requires('subprocess') | ||
| test_support.run_unittest(ProcessTestCase) | ||
| test_support.run_unittest(ProcessTestCase, SecurityManagerTestCase) | ||
| if hasattr(test_support, "reap_children"): | ||
| test_support.reap_children() | ||
|
|
||
|
|
||
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.
Or on second thoughts, is this maybe happier in
test_subprocess_jy.py, where everything is Jython-specific?