-
Notifications
You must be signed in to change notification settings - Fork 25
[Security] Sanitize user inputs to prevent OS command injection via SSM #469
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import logging | ||
| import shlex | ||
| import pytest | ||
| from unittest import mock | ||
|
|
||
| from api.PclusterApiHandler import ssm_command | ||
| from api.pcm_globals import _logger_ctxvar | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def bind_logger(): | ||
| """ssm_command writes to a request-scoped logger proxy; bind a stdlib | ||
| logger to the contextvar so calls don't fail outside a Flask request.""" | ||
| token = _logger_ctxvar.set(logging.getLogger("test")) | ||
| yield | ||
| _logger_ctxvar.reset(token) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_ssm_send(mocker): | ||
| mock_client = mock.MagicMock() | ||
| mock_client.send_command.return_value = {"Command": {"CommandId": "cmd-id"}} | ||
| mock_client.get_command_invocation.return_value = {"Status": "Success"} | ||
|
|
||
| mocker.patch("api.PclusterApiHandler.boto3.client", return_value=mock_client) | ||
| mocker.patch("api.PclusterApiHandler.time.sleep") | ||
| mocker.patch( | ||
| "api.PclusterApiHandler.read_and_delete_ssm_output_from_cloudwatch", | ||
| return_value="", | ||
| ) | ||
| return mock_client | ||
|
|
||
|
|
||
| def _sent_command(mock_client): | ||
| return mock_client.send_command.call_args.kwargs["Parameters"]["commands"][0] | ||
|
|
||
|
|
||
| def test_malicious_user_stays_a_single_argument(mock_ssm_send): | ||
| """A user string with a single quote must not break out of the -l argument.""" | ||
| malicious = "ec2-user';touch /tmp/pwned;'" | ||
| ssm_command("us-east-1", "i-1234", malicious, "sacct") | ||
|
|
||
| tokens = shlex.split(_sent_command(mock_ssm_send)) | ||
| # Expected shell parse: ['runuser', '-l', <malicious literal>, '-c', 'sacct'] | ||
| # If shlex.quote were missing, the shell would see extra tokens / commands | ||
| # from the injected ';touch /tmp/pwned;' portion. | ||
| assert tokens[0:2] == ["runuser", "-l"] | ||
| assert tokens[2] == malicious | ||
| assert tokens[3:] == ["-c", "sacct"] | ||
|
|
||
|
|
||
| def test_malicious_run_command_stays_a_single_argument(mock_ssm_send): | ||
| """A run_command with shell metacharacters must be passed as one -c argument.""" | ||
| malicious = "sacct';rm -rf /;'" | ||
| ssm_command("us-east-1", "i-1234", "ec2-user", malicious) | ||
|
|
||
| tokens = shlex.split(_sent_command(mock_ssm_send)) | ||
| assert tokens == ["runuser", "-l", "ec2-user", "-c", malicious] |
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
Oops, something went wrong.
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.
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.
why not shlexing the dcv_command as well?
Uh oh!
There was an error while loading. Please reload this page.
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.
See
aws-parallelcluster-ui/api/PclusterApiHandler.py
Line 450 in b925613
It's hardcoded, not user input.