|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2021 Damien Nguyen |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import astroid |
| 17 | +import pytest |
| 18 | + |
| 19 | +import pylint_secure_coding_standard as pylint_scs |
| 20 | +import pylint.testutils |
| 21 | + |
| 22 | + |
| 23 | +class TestSecureCodingStandardChecker(pylint.testutils.CheckerTestCase): |
| 24 | + CHECKER_CLASS = pylint_scs.SecureCodingStandardChecker |
| 25 | + |
| 26 | + def test_shell_true_ok(self): |
| 27 | + nodes = astroid.extract_node( |
| 28 | + """ |
| 29 | + subprocess.Popen(["cat", "/etc/passwd"], b, e, i, o, e, pre, c) #@ |
| 30 | + subprocess.Popen(["cat", "/etc/passwd"], b, e, i, o, e, pre, c, False) #@ |
| 31 | + subprocess.Popen(["cat", "/etc/passwd"], b, e, i, o, e, pre, c, False, cwd) #@ |
| 32 | + subprocess.Popen(["cat", "/etc/passwd"], shell=False) #@ |
| 33 | + sp.Popen(["cat", "/etc/passwd"], shell=False) #@ |
| 34 | + subprocess.run(["cat", "/etc/passwd"], shell=False) #@ |
| 35 | + sp.run(["cat", "/etc/passwd"], shell=False) #@ |
| 36 | + subprocess.call(["cat", "/etc/passwd"], shell=False) #@ |
| 37 | + sp.call(["cat", "/etc/passwd"], shell=False) #@ |
| 38 | + subprocess.check_call(["cat", "/etc/passwd"], shell=False) #@ |
| 39 | + sp.check_call(["cat", "/etc/passwd"], shell=False) #@ |
| 40 | + subprocess.check_output(["cat", "/etc/passwd"], shell=False) #@ |
| 41 | + sp.check_output(["cat", "/etc/passwd"], shell=False) #@ |
| 42 | + """ |
| 43 | + ) |
| 44 | + |
| 45 | + with self.assertNoMessages(): |
| 46 | + for node in nodes: |
| 47 | + self.checker.visit_call(node) |
| 48 | + |
| 49 | + @pytest.mark.parametrize( |
| 50 | + 's, msg_id', |
| 51 | + ( |
| 52 | + ('from os import system', 'avoid-os-system'), |
| 53 | + ('from os import system as os_system', 'avoid-os-system'), |
| 54 | + ), |
| 55 | + ) |
| 56 | + def test_shell_true_importfrom(self, s, msg_id): |
| 57 | + node = astroid.extract_node(s + ' #@') |
| 58 | + with self.assertAddsMessages(pylint.testutils.Message(msg_id=msg_id, node=node)): |
| 59 | + self.checker.visit_importfrom(node) |
| 60 | + |
| 61 | + @pytest.mark.parametrize( |
| 62 | + 's, msg_id', |
| 63 | + ( |
| 64 | + ('os.system("ls -l")', 'avoid-os-system'), |
| 65 | + ('subprocess.Popen(["cat", "/etc/passwd"], b, e, i, o, e, pre, c, True)', 'avoid-shell-true'), |
| 66 | + ('subprocess.Popen(["cat", "/etc/passwd"], b, e, i, o, e, pre, c, True, cwd)', 'avoid-shell-true'), |
| 67 | + ('subprocess.run(["cat", "/etc/passwd"], shell=True)', 'avoid-shell-true'), |
| 68 | + ('sp.run(["cat", "/etc/passwd"], shell=True)', 'avoid-shell-true'), |
| 69 | + ('subprocess.call(["cat", "/etc/passwd"], shell=True)', 'avoid-shell-true'), |
| 70 | + ('sp.call(["cat", "/etc/passwd"], shell=True)', 'avoid-shell-true'), |
| 71 | + ('subprocess.check_call(["cat", "/etc/passwd"], shell=True)', 'avoid-shell-true'), |
| 72 | + ('sp.check_call(["cat", "/etc/passwd"], shell=True)', 'avoid-shell-true'), |
| 73 | + ('subprocess.check_output(["cat", "/etc/passwd"], shell=True)', 'avoid-shell-true'), |
| 74 | + ('sp.check_output(["cat", "/etc/passwd"], shell=True)', 'avoid-shell-true'), |
| 75 | + ), |
| 76 | + ) |
| 77 | + def test_shell_true_call(self, s, msg_id): |
| 78 | + node = astroid.extract_node(s + ' #@') |
| 79 | + with self.assertAddsMessages(pylint.testutils.Message(msg_id=msg_id, node=node)): |
| 80 | + self.checker.visit_call(node) |
0 commit comments