-
-
Notifications
You must be signed in to change notification settings - Fork 65
Add ENABLE_SYSTEM_COMMANDS setting to control external command execution #1660
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?
Conversation
db89e25 to
28f1bb1
Compare
|
Probably you also want to avoid that 'SetEnvironment' can change environment variables too, as well as writing files. |
mathics/__main__.py
Outdated
| if len(source_code) and source_code[0] == "!": | ||
| subprocess.run(source_code[1:], shell=True) | ||
| if not settings.ENABLE_SYSTEM_COMMANDS: | ||
| print("Execution of external commands is disabled.") |
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.
define a message and use evaluation.message instead of print
|
|
mathics/settings.py
Outdated
|
|
||
| # Leave this True unless you have specific reason for not permitting | ||
| # users to execute system commands. | ||
| ENABLE_SYSTEM_COMMANDS = os.environ.get("MATHICS_ENABLE_SYSTEM_COMMANDS", "True").lower() == "true" |
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.
We already use in testing the environment variable "SANDBOX" to indicate restricted access.
I guess we should use that here as well. Thoughts?
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.
We use SANDBOX=False to avoid tests that need access to the local filesystem. Probably it is also a good idea to avoid the evaluation of these expressions.
a8c1196 to
d177790
Compare
|
Feedback from the team has been incorporated into the latest push.
|
d177790 to
7b30f89
Compare
|
LGTM |
mathics/settings.py
Outdated
| # If SANDBOX environment variable is set, this defaults to False. | ||
| ENABLE_SYSTEM_COMMANDS = ( | ||
| os.environ.get( | ||
| "MATHICS_ENABLE_SYSTEM_COMMANDS", str(not os.environ.get("SANDBOX")) |
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.
| "MATHICS_ENABLE_SYSTEM_COMMANDS", str(not os.environ.get("SANDBOX")) | |
| "MATHICS3_ENABLE_SYSTEM_COMMANDS", str(not os.environ.get("SANDBOX")) |
We've been moving to the name Mathics3.
2f24c15 to
7132beb
Compare
|
Incorporated the latest suggestions by renaming the setting to Default behavior now correctly respects the |
Looking at the code, I see that this sandbox thing, from a security standpoint, is lacking. We should remove the builtin functions ProcessId, ParentProcess, and stuff like that. And then the doctest marks More broadly, are we interested in One other thing, we should be using a less generic name than @RinZ27 Are you up for making those changes? |
7132beb to
74b68dd
Compare
|
I completely agree that a robust sandbox should restrict more than just shell execution. I've updated the logic to use the more specific |
|
It looks like |
|
It seems the docstring test in '$CommandLine' should be adjusted (remove the curly brackets in the expected result), or just change the behavior when SANDBOX is false, and return an empty list. |
|
I've incorporated the latest feedback.
|
7cb5f0d to
69c7360
Compare
mathics/builtin/system.py
Outdated
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.
Mark this as SandBox test (>> -> S>)
mathics/builtin/system.py
Outdated
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.
Also here.
69c7360 to
e134a76
Compare
|
I've updated the PR to address the latest feedback:
|
e134a76 to
0fc2c86
Compare
946ab2a to
4111647
Compare
4111647 to
3a11687
Compare
The current implementation of the
Run[]builtin and the shell escape (!) in the CLI allows arbitrary command execution throughsubprocesswithshell=True. While this is a core feature for local use, it poses a significant security risk when Mathics is deployed in a sandboxed or remote environment (e.g., a web-based notebook server).I've introduced a new configuration setting,
ENABLE_SYSTEM_COMMANDS, which allows administrators to disable external command execution when necessary. This follows the pattern already established byENABLE_FILES_MODULE.Changes:
ENABLE_SYSTEM_COMMANDStomathics/settings.py(defaults toTrue, can be overridden viaMATHICS3_ENABLE_SYSTEM_COMMANDSenvironment variable, and defaults toFalseifSANDBOXis set).Run[]builtin inmathics/builtin/system.pyto check this setting before execution.mathics/main.pyto respect this setting for the!escape command.inspecteval loop inmathics/interrupt.pyto respect this setting.Run[]will issue a message and return$Failed, and the CLI will display an informative message.This change provides a much-needed security control for shared or public Mathics instances without impacting the default local experience.