Add ENABLE_SYSTEM_COMMANDS setting to control external command execution#1660
Add ENABLE_SYSTEM_COMMANDS setting to control external command execution#1660RinZ27 wants to merge 1 commit intoMathics3:masterfrom
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.
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.
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.
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.
| "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.
Mark this as SandBox test (>> -> S>)
mathics/builtin/system.py
Outdated
69c7360 to
e134a76
Compare
|
I've updated the PR to address the latest feedback:
|
4111647 to
3a11687
Compare
|
Pushed a fix for the CI failures in Pyodide and Ubuntu. The issue was mainly due to inconsistent sandbox detection between the doctest runner and the kernel, especially in restricted environments where environment variables like |
|
Just looked at the revision. There was a misunderstanding: we don't need both Thanks for sticking with it this far. |
|
Thanks for the clarification, @rocky. I see the redundancy now. I'll clean that up and stick to |
Many thanks. So we can remove "B>" then? One other test doc mark that can be used is "X>", which indicates skipping running the test but keeping the example for the documentation. (I am not sure why we don't use that for Breakpoint). See https://mathics-development-guide.readthedocs.io/en/latest/extending/developing-code/extending/documentation-markup.html#guidelines-for-writing-documentation I will be revising this soon, hopefully to make this clearer. |
39e6363 to
e59aa7d
Compare
|
I've updated the PR based on your suggestions, @rocky.
This aligns better with the documentation guidelines you pointed out and keeps the markup system lean. Everything should be ready for another look! |
e59aa7d to
64c1f02
Compare
|
I'm not seeing the top-level Makefile changed. It also refers to SANDOX. Please run |
64c1f02 to
b4b7da9
Compare
|
Performed a project-wide sweep using
Thanks for the catch on the |
|
@RinZ27 Thanks for all of the work. I hope the last request, using black==23.12.1 (or some version of black that is compatible with that) run: You should see both files getting changed. Commit the changes. black 23.12.1 is what Many thanks. |
b4b7da9 to
a7312fc
Compare
|
Reformatted both |
|
@RinZ27 I am seeing all sorts of weird changes and drops of URL links for no reason I can understand. I will pull in your branch and see if can just make the changes to get this over the finish line. |
|
My bad @rocky, I definitely clipped those URL links by accident while adding the sandbox examples to the docstrings. Thanks for picking this up to get it across the finish line. I saw your mention about needing similar changes in |
Revise #1660 Disallow various shell escape and OS/environment queries in a sandboxed environment. Rename `SANDBOX` variable `MATHICS3_SANDBOX`. --------- Co-authored-by: RinZ27 <222222878+RinZ27@users.noreply.github.com>
|
Superceded by #1667 One side thing that came out of this is that we probably should update the version of "black" used in testing. I think everyone is now stumbling over the fact that the version we are using 23.12.1 is outdated. 26.1.0 seems to be current, and if this works on 3.10 I guess we should use it. Thoughts @mmatera and @RinZ27 ? |
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.