|
| 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 stat |
| 17 | + |
| 18 | +import astroid |
| 19 | +import pylint.testutils |
| 20 | +import pytest |
| 21 | + |
| 22 | +import pylint_secure_coding_standard as pylint_scs |
| 23 | + |
| 24 | + |
| 25 | +class TestSecureCodingStandardChecker(pylint.testutils.CheckerTestCase): |
| 26 | + CHECKER_CLASS = pylint_scs.SecureCodingStandardChecker |
| 27 | + |
| 28 | + @pytest.mark.parametrize( |
| 29 | + 's, expected', |
| 30 | + ( |
| 31 | + ('S_IREAD', stat.S_IREAD), |
| 32 | + ('stat.S_IREAD', stat.S_IREAD), |
| 33 | + ('S_IREAD | S_IWRITE', stat.S_IREAD | stat.S_IWRITE), |
| 34 | + ('stat.S_IREAD | stat.S_IWRITE', stat.S_IREAD | stat.S_IWRITE), |
| 35 | + ('stat.S_IREAD | stat.S_IWRITE | S_IXUSR', stat.S_IREAD | stat.S_IWRITE | stat.S_IXUSR), |
| 36 | + ), |
| 37 | + ) |
| 38 | + def test_chmod_get_mode(self, s, expected): |
| 39 | + node = astroid.extract_node(s + ' #@') |
| 40 | + assert pylint_scs._chmod_get_mode(node) == expected |
| 41 | + |
| 42 | + @pytest.mark.parametrize( |
| 43 | + 's', |
| 44 | + ( |
| 45 | + 'stat.ST_MODE', |
| 46 | + 'bla.S_IREAD', |
| 47 | + ), |
| 48 | + ) |
| 49 | + def test_chmod_get_mode_invalid(self, s): |
| 50 | + node = astroid.extract_node(s + ' #@') |
| 51 | + with pytest.raises(ValueError): |
| 52 | + pylint_scs._chmod_get_mode(node) |
| 53 | + |
| 54 | + @pytest.mark.parametrize( |
| 55 | + 's, expected', |
| 56 | + ( |
| 57 | + ('-stat.S_IREAD', -stat.S_IREAD), |
| 58 | + ('~stat.S_IREAD', ~stat.S_IREAD), |
| 59 | + ('not stat.S_IREAD', not stat.S_IREAD), |
| 60 | + ), |
| 61 | + ) |
| 62 | + def test_chmod_get_mode_unop(self, s, expected): |
| 63 | + node = astroid.extract_node(s + ' #@') |
| 64 | + assert pylint_scs._chmod_get_mode(node) == expected |
| 65 | + |
| 66 | + @pytest.mark.parametrize( |
| 67 | + 's, expected', |
| 68 | + ( |
| 69 | + ('stat.S_IREAD + stat.S_IWRITE', stat.S_IREAD + stat.S_IWRITE), |
| 70 | + ('stat.S_IREAD - stat.S_IWRITE', stat.S_IREAD - stat.S_IWRITE), |
| 71 | + ('stat.S_IREAD * stat.S_IWRITE', stat.S_IREAD * stat.S_IWRITE), |
| 72 | + ('stat.S_IREAD / stat.S_IWRITE', stat.S_IREAD / stat.S_IWRITE), |
| 73 | + ('stat.S_IREAD // stat.S_IWRITE', stat.S_IREAD // stat.S_IWRITE), |
| 74 | + ('stat.S_IREAD % stat.S_IWRITE', stat.S_IREAD % stat.S_IWRITE), |
| 75 | + ('stat.S_IREAD ^ stat.S_IWRITE', stat.S_IREAD ^ stat.S_IWRITE), |
| 76 | + ('stat.S_IREAD | stat.S_IWRITE', stat.S_IREAD | stat.S_IWRITE), |
| 77 | + ('stat.S_IREAD & stat.S_IWRITE', stat.S_IREAD & stat.S_IWRITE), |
| 78 | + ), |
| 79 | + ) |
| 80 | + def test_chmod_get_mode_binop(self, s, expected): |
| 81 | + node = astroid.extract_node(s + ' #@') |
| 82 | + assert pylint_scs._chmod_get_mode(node) == expected |
| 83 | + |
| 84 | + @pytest.mark.parametrize( |
| 85 | + 'platform, enabled_platform', |
| 86 | + ( |
| 87 | + ('Linux', True), |
| 88 | + ('Darwin', True), |
| 89 | + ('Java', True), |
| 90 | + ('Windows', False), |
| 91 | + ), |
| 92 | + ) |
| 93 | + @pytest.mark.parametrize('fname', ('"file.txt"', 'fname')) |
| 94 | + @pytest.mark.parametrize('arg_type', ('', 'mode='), ids=('arg', 'keyword')) |
| 95 | + @pytest.mark.parametrize( |
| 96 | + 'forbidden', |
| 97 | + ( |
| 98 | + 'S_IRGRP', # NB: not actually a forbidden value, only for testing... |
| 99 | + 'S_IRWXG', |
| 100 | + 'S_IWGRP', |
| 101 | + 'S_IXGRP', |
| 102 | + 'S_IRWXO', |
| 103 | + 'S_IWOTH', |
| 104 | + 'S_IXOTH', |
| 105 | + ), |
| 106 | + ) |
| 107 | + @pytest.mark.parametrize( |
| 108 | + 's', |
| 109 | + ( |
| 110 | + '', |
| 111 | + 'S_IREAD', |
| 112 | + 'S_IREAD | S_IWRITE', |
| 113 | + 'S_IRUSR | S_IWUSR | S_IXUSR', |
| 114 | + ), |
| 115 | + ids=lambda s: s if s else '<empty>', |
| 116 | + ) |
| 117 | + def test_chmod(self, mocker, platform, enabled_platform, fname, arg_type, forbidden, s): |
| 118 | + mocker.patch('platform.system', lambda: platform) |
| 119 | + |
| 120 | + if s: |
| 121 | + code = f'os.chmod({fname}, {arg_type}{s} | {forbidden}) #@' |
| 122 | + else: |
| 123 | + code = f'os.chmod({fname}, {arg_type} {forbidden}) #@' |
| 124 | + |
| 125 | + print(code) |
| 126 | + node = astroid.extract_node(code) |
| 127 | + if enabled_platform and forbidden != 'S_IRGRP': |
| 128 | + with self.assertAddsMessages(pylint.testutils.Message(msg_id='os-chmod-unsafe-permissions', node=node)): |
| 129 | + self.checker.visit_call(node) |
| 130 | + else: |
| 131 | + with self.assertNoMessages(): |
| 132 | + self.checker.visit_call(node) |
| 133 | + |
| 134 | + @pytest.mark.parametrize('platform', ('Linux', 'Darwin', 'Java', 'Windows')) |
| 135 | + @pytest.mark.parametrize( |
| 136 | + 's', |
| 137 | + ( |
| 138 | + 'os.chmod("file.txt", stat.ST_MODE)', |
| 139 | + 'os.chmod("file.txt", other.S_IRWXO)', |
| 140 | + 'os.chmod("file.txt", mode)', |
| 141 | + 'os.chmod("file.txt", mode=mode)', |
| 142 | + ), |
| 143 | + ) |
| 144 | + def test_chmod_no_warning(self, mocker, platform, s): |
| 145 | + mocker.patch('platform.system', lambda: platform) |
| 146 | + |
| 147 | + node = astroid.extract_node(s) |
| 148 | + with self.assertNoMessages(): |
| 149 | + self.checker.visit_call(node) |
| 150 | + |
| 151 | + @pytest.mark.parametrize( |
| 152 | + 'platform, enabled_platform', |
| 153 | + ( |
| 154 | + ('Linux', True), |
| 155 | + ('Darwin', True), |
| 156 | + ('Java', True), |
| 157 | + ('Windows', False), |
| 158 | + ), |
| 159 | + ) |
| 160 | + @pytest.mark.parametrize('s', ('os.chmod("file")',)) |
| 161 | + def test_chmod_invalid_raise(self, mocker, platform, enabled_platform, s): |
| 162 | + mocker.patch('platform.system', lambda: platform) |
| 163 | + |
| 164 | + node = astroid.extract_node(s) |
| 165 | + if enabled_platform: |
| 166 | + with pytest.raises(RuntimeError): |
| 167 | + self.checker.visit_call(node) |
| 168 | + else: |
| 169 | + with self.assertNoMessages(): |
| 170 | + self.checker.visit_call(node) |
0 commit comments