Skip to content
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

[show] replace shell=True, replace xml by lxml, replace exit by sys.exit #2666

Merged
merged 15 commits into from
May 31, 2023

Conversation

maipbui
Copy link
Contributor

@maipbui maipbui commented Feb 9, 2023

Signed-off-by: maipbui maibui@microsoft.com

What I did

subprocess() - when using with shell=True is dangerous. Using subprocess function without a static string can lead to command injection.
sys.exit is better than exit, considered good to use in production code.
Ref:
https://stackoverflow.com/questions/6501121/difference-between-exit-and-sys-exit-in-python
https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used

How I did it

subprocess() - use shell=False instead, use list of strings Ref: https://semgrep.dev/docs/cheat-sheets/python-command-injection/#mitigation
Replace exit() by sys.exit()

How to verify it

Pass UT
Manual test

Previous command output (if the output of a command-line utility has changed)

New command output (if the output of a command-line utility has changed)

Signed-off-by: maipbui <maibui@microsoft.com>
show/main.py Outdated
if route_map_name is not None:
cmd += ' {}'.format(route_map_name)
cmd += '"'
cmd += [str(route_map_name)]
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

route_map_name

It should be appended to embedded show route-map command. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make sure this is tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To simplify:
cmd[-1] += ' {}'.format(route_map_name)

show/main.py Outdated
if prefix_list_name is not None:
cmd += ' {}'.format(prefix_list_name)
cmd += '"'
cmd += [str(prefix_list_name)]
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefix_list_name

append to embedded command #Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same.

show/main.py Outdated
if prefix_list_name is not None:
cmd += ' {}'.format(prefix_list_name)
cmd += '"'
cmd += [str(prefix_list_name)]
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefix_list_name

append to embedded command #Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same

show/main.py Outdated

run_command(cmd, display_cmd=verbose)
if cmd1 is None and cmd2 is None:
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmd1

You implemented a 2x2 conditions with 4 branches. This is not scalable if number of cmd increase. For example 10 cmd.

You can construct an array first, and feed the expanded array to function call. ref: https://stackoverflow.com/a/7745986/2514803 #Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or cmdn could be defined as array elements from the beginning.

show/main.py Outdated
cmd0 = ['sudo', 'docker', 'ps']
cmd1 = ['grep', 'bgp']
cmd2 = ['awk', '{print$2}']
cmd3 = ['cut', '-d', '-', '-f3']
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'-d', '-'

originally it is one arg. #Closed

show/main.py Outdated
cmd1 = ['grep', 'bgp']
cmd2 = ['awk', '{print$2}']
cmd3 = ['cut', '-d', '-', '-f3']
cmd4 = ['cut', '-d', ':', "-f1"]
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'-d', ':'

originally it is one arg. #Closed

r'-maxdepth 1 -type d -name install_\*_profile ' + opts + '| sed '
r's%/opt/bfn/install_\\\(.\*\\\)_profile%\\1%', shell=True)
cmd0 = ['docker', 'exec', '-it', 'syncd', 'find', '/opt/bfn', '-mindepth', '1',\
r'-maxdepth', '1', '-type', 'd', '-name', 'install_\*_profile', r'{}' % opts]
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r'{}' % opts

What the diff if using opts directly? #Pending

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r'' is to bypass escaping special characters. Using opts directly will escape special characters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you be more specific?, r is applied to {} string only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for example, r'\n' will treated the string containing 2 characters: backslash \ and letter n. But '\n' will be treated as a newline character.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, raised another PR for barefoot https://github.com/sonic-net/sonic-utilities/pull/2699/files

@@ -4,6 +4,7 @@
import json
import subprocess
from sonic_py_common import device_info
from sonic_py_common.general import getstatusoutput_noshell_pipe, check_output_pipe

@click.group()
def barefoot():
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

barefoot

This file need to reviewed by barefoot. Suggest further split into a smaller PR. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

@qiluo-msft
Copy link
Contributor

qiluo-msft commented Feb 10, 2023

Specific command-line utility for Mellanox platform

This file need to reviewed by Mellanox. Suggest further split into a smaller PR.


In reply to: 1426293827


In reply to: 1426293827


In reply to: 1426293827


In reply to: 1426293827


In reply to: 1426293827


Refers to: show/plugins/mlnx.py:22 in 4d96b6e. [](commit_id = 4d96b6e, deletion_comment = False)

show/main.py Outdated
@@ -130,6 +133,32 @@ def run_command(command, display_cmd=False, return_cmd=False):
if rc != 0:
sys.exit(rc)

def run_command_pipe(*args, display_cmd=False, return_cmd=False):
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run_command_pipe

Treat run_command as a special case, call the other with only one command in args, so most of the code are reused. #Closed

@qiluo-msft
Copy link
Contributor

qiluo-msft commented Feb 10, 2023

clicommon.run_command(cmd + options, display_cmd=verbose)

Use string array instead of string for run_command.


In reply to: 1426311506


In reply to: 1426311506


Refers to: show/platform.py:111 in 4d96b6e. [](commit_id = 4d96b6e, deletion_comment = False)

Signed-off-by: maipbui <maibui@microsoft.com>
@maipbui
Copy link
Contributor Author

maipbui commented Feb 15, 2023

clicommon.run_command(cmd + options, display_cmd=verbose)

Use string array instead of string for run_command.

Refers to: show/platform.py:111 in 4d96b6e. [](commit_id = 4d96b6e, deletion_comment = False)

this will be covered in another PR. I'll draft another PR.

Signed-off-by: maipbui <maibui@microsoft.com>
Signed-off-by: maipbui <maibui@microsoft.com>
@sonic-net sonic-net deleted a comment from azure-pipelines bot Feb 27, 2023
r'-maxdepth 1 -type d -name install_\*_profile ' + opts + '| sed '
r's%/opt/bfn/install_\\\(.\*\\\)_profile%\\1%', shell=True)
cmd0 = ['docker', 'exec', '-it', 'syncd', 'find', '/opt/bfn', '-mindepth', '1',\
r'-maxdepth', '1', '-type', 'd', '-name', 'install_\*_profile', r'{}' % opts]
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r

This r is not needed. #Pending

Signed-off-by: maipbui <maibui@microsoft.com>
@maipbui
Copy link
Contributor Author

maipbui commented Mar 3, 2023

/azp run Azure.sonic-utilities

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

qiluo-msft
qiluo-msft previously approved these changes Mar 7, 2023
Signed-off-by: Mai Bui <maibui@microsoft.com>
Signed-off-by: Mai Bui <maibui@microsoft.com>
Signed-off-by: Mai Bui <maibui@microsoft.com>
Signed-off-by: Mai Bui <maibui@microsoft.com>
Signed-off-by: Mai Bui <maibui@microsoft.com>
show/main.py Outdated
@click.option('-f', '--follow', is_flag=True)
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def logging(process, lines, follow, verbose):
"""Show system log"""
if process and not re.match(r'^[a-zA-Z0-9\s]+$', process):
sys.exit('Process contains only number, alphabet, and whitespace.')
Copy link
Contributor

@qiluo-msft qiluo-msft May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit

What is the reason to add this check? #Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Offline discussed, this check is not accurate. Let's revert back, and please use another future PR to focus on this fix.

Signed-off-by: Mai Bui <maibui@microsoft.com>
Signed-off-by: Mai Bui <maibui@microsoft.com>
qiluo-msft
qiluo-msft previously approved these changes May 31, 2023
Signed-off-by: Mai Bui <maibui@microsoft.com>
Signed-off-by: Mai Bui <maibui@microsoft.com>
Signed-off-by: Mai Bui <maibui@microsoft.com>
@qiluo-msft
Copy link
Contributor

qiluo-msft commented May 31, 2023

class TestShowPlatform(object):

Thanks for cleaning up the codebase! #Closed


Refers to: tests/show_test.py:498 in a20435a. [](commit_id = a20435a, deletion_comment = True)

@maipbui maipbui merged commit a66f41c into sonic-net:master May 31, 2023
4 checks passed
@maipbui maipbui deleted the show_pysec branch May 31, 2023 18:20
dprital added a commit to dprital/sonic-buildimage that referenced this pull request Jun 5, 2023
Update sonic-utilities submodule pointer to include the following:
* 5c9b2177 Fix issue: out of range sflow polling interval is accepted and stored in config_db ([sonic-net#2847](sonic-net/sonic-utilities#2847))
* 72ca4848 Add CLI configuration options for teamd retry count feature ([sonic-net#2642](sonic-net/sonic-utilities#2642))
* 359dfc0c [Clock] Implement clock CLI ([sonic-net#2793](sonic-net/sonic-utilities#2793))
* b316fc27 Add transceiver status CLI to show output from TRANSCEIVER_STATUS table ([sonic-net#2772](sonic-net/sonic-utilities#2772))
* dc59dbd2 Replace pickle by json ([sonic-net#2849](sonic-net/sonic-utilities#2849))
* a66f41c4 [show] replace shell=True, replace xml by lxml, replace exit by sys.exit ([sonic-net#2666](sonic-net/sonic-utilities#2666))
* 57500572 [utilities_common] replace shell=True ([sonic-net#2718](sonic-net/sonic-utilities#2718))
* 6e0ee3e7 [CRM][DASH] Extend CRM utility to support DASH resources. ([sonic-net#2800](sonic-net/sonic-utilities#2800))
* b2c29b0b [config] Generate sysinfo in single asic ([sonic-net#2856](sonic-net/sonic-utilities#2856))

Signed-off-by: dprital <drorp@nvidia.com>
StormLiangMS pushed a commit to sonic-net/sonic-buildimage that referenced this pull request Jun 5, 2023
…nic-utilities submodule on master (#15193)

Dependency:
sonic-net/sonic-utilities#2718

Why I did it
This PR sonic-net/sonic-utilities#2718 reduce shell=True usage in utilities_common.cli.run_command() function.

Work item tracking
Microsoft ADO (number only): 15022050
How I did it
Replace strings commands using utilities_common.cli.run_command() function to list of strings

due to circular dependency, advance sonic-utilities submodule
72ca4848 (HEAD -> master, upstream/master, upstream/HEAD) Add CLI configuration options for teamd retry count feature (sonic-net/sonic-utilities#2642)
359dfc0c [Clock] Implement clock CLI (sonic-net/sonic-utilities#2793)
b316fc27 Add transceiver status CLI to show output from TRANSCEIVER_STATUS table (sonic-net/sonic-utilities#2772)
dc59dbd2 Replace pickle by json (sonic-net/sonic-utilities#2849)
a66f41c4 [show] replace shell=True, replace xml by lxml, replace exit by sys.exit (sonic-net/sonic-utilities#2666)
57500572 [utilities_common] replace shell=True (sonic-net/sonic-utilities#2718)
6e0ee3e7 [CRM][DASH] Extend CRM utility to support DASH resources. (sonic-net/sonic-utilities#2800)
b2c29b0b [config] Generate sysinfo in single asic (sonic-net/sonic-utilities#2856)
pdhruv-marvell pushed a commit to pdhruv-marvell/sonic-utilities that referenced this pull request Aug 23, 2023
…xit (sonic-net#2666)

#### What I did
`subprocess()` - when using with `shell=True` is dangerous. Using subprocess function without a static string can lead to command injection.
`sys.exit` is better than `exit`, considered good to use in production code.
Ref:
https://stackoverflow.com/questions/6501121/difference-between-exit-and-sys-exit-in-python
https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used
#### How I did it
`subprocess()` - use `shell=False` instead, use list of strings Ref: [https://semgrep.dev/docs/cheat-sheets/python-command-injection/#mitigation](https://semgrep.dev/docs/cheat-sheets/python-command-injection/#mitigation)
Replace `exit()` by `sys.exit()`
#### How to verify it
Pass UT
Manual test
Signed-off-by: Mai Bui <maibui@microsoft.com>
sonic-otn pushed a commit to sonic-otn/sonic-buildimage that referenced this pull request Sep 20, 2023
…nic-utilities submodule on master (sonic-net#15193)

Dependency:
sonic-net/sonic-utilities#2718

Why I did it
This PR sonic-net/sonic-utilities#2718 reduce shell=True usage in utilities_common.cli.run_command() function.

Work item tracking
Microsoft ADO (number only): 15022050
How I did it
Replace strings commands using utilities_common.cli.run_command() function to list of strings

due to circular dependency, advance sonic-utilities submodule
72ca4848 (HEAD -> master, upstream/master, upstream/HEAD) Add CLI configuration options for teamd retry count feature (sonic-net/sonic-utilities#2642)
359dfc0c [Clock] Implement clock CLI (sonic-net/sonic-utilities#2793)
b316fc27 Add transceiver status CLI to show output from TRANSCEIVER_STATUS table (sonic-net/sonic-utilities#2772)
dc59dbd2 Replace pickle by json (sonic-net/sonic-utilities#2849)
a66f41c4 [show] replace shell=True, replace xml by lxml, replace exit by sys.exit (sonic-net/sonic-utilities#2666)
57500572 [utilities_common] replace shell=True (sonic-net/sonic-utilities#2718)
6e0ee3e7 [CRM][DASH] Extend CRM utility to support DASH resources. (sonic-net/sonic-utilities#2800)
b2c29b0b [config] Generate sysinfo in single asic (sonic-net/sonic-utilities#2856)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants