forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sonic-py-common] Add getstatusoutput_noshell() functions to general …
…module (sonic-net#12065) Signed-off-by: maipbui <maibui@microsoft.com> #### Why I did it `getstatusoutput()` function from `subprocess` module has shell injection issue because it includes `shell=True` in the implementation Eliminate duplicate code #### How I did it Reimplement `getstatusoutput_noshell()` and `getstatusoutput_noshell_pipe()` functions with `shell=False` Add `check_output_pipe()` function #### How to verify it Pass UT
- Loading branch information
1 parent
fb9f5ce
commit cc922ba
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,31 @@ | ||
import sys | ||
import pytest | ||
import subprocess | ||
from sonic_py_common.general import getstatusoutput_noshell, getstatusoutput_noshell_pipe, check_output_pipe | ||
|
||
|
||
def test_getstatusoutput_noshell(tmp_path): | ||
exitcode, output = getstatusoutput_noshell(['echo', 'sonic']) | ||
assert (exitcode, output) == (0, 'sonic') | ||
|
||
exitcode, output = getstatusoutput_noshell([sys.executable, "-c", "import sys; sys.exit(6)"]) | ||
assert exitcode != 0 | ||
|
||
def test_getstatusoutput_noshell_pipe(): | ||
exitcode, output = getstatusoutput_noshell_pipe(['echo', 'sonic'], ['awk', '{print $1}']) | ||
assert (exitcode, output) == ([0, 0], 'sonic') | ||
|
||
exitcode, output = getstatusoutput_noshell_pipe([sys.executable, "-c", "import sys; sys.exit(6)"], [sys.executable, "-c", "import sys; sys.exit(8)"]) | ||
assert exitcode == [6, 8] | ||
|
||
def test_check_output_pipe(): | ||
output = check_output_pipe(['echo', 'sonic'], ['awk', '{print $1}']) | ||
assert output == 'sonic\n' | ||
|
||
with pytest.raises(subprocess.CalledProcessError) as e: | ||
check_output_pipe([sys.executable, "-c", "import sys; sys.exit(6)"], [sys.executable, "-c", "import sys; sys.exit(0)"]) | ||
assert e.returncode == [6, 0] | ||
|
||
with pytest.raises(subprocess.CalledProcessError) as e: | ||
check_output_pipe([sys.executable, "-c", "import sys; sys.exit(0)"], [sys.executable, "-c", "import sys; sys.exit(6)"]) | ||
assert e.returncode == [0, 6] |