|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | +import pretend |
| 14 | + |
| 15 | +from warehouse.malware.checks.package_turnover import check as c |
| 16 | +from warehouse.malware.models import ( |
| 17 | + MalwareCheckState, |
| 18 | + VerdictClassification, |
| 19 | + VerdictConfidence, |
| 20 | +) |
| 21 | + |
| 22 | +from .....common.db.accounts import UserFactory |
| 23 | +from .....common.db.malware import MalwareCheckFactory |
| 24 | +from .....common.db.packaging import ProjectFactory, ReleaseFactory |
| 25 | + |
| 26 | + |
| 27 | +def test_initializes(db_session): |
| 28 | + check_model = MalwareCheckFactory.create( |
| 29 | + name="PackageTurnoverCheck", state=MalwareCheckState.Enabled, |
| 30 | + ) |
| 31 | + check = c.PackageTurnoverCheck(db_session) |
| 32 | + |
| 33 | + assert check.id == check_model.id |
| 34 | + |
| 35 | + |
| 36 | +def test_user_posture_verdicts(db_session): |
| 37 | + user = UserFactory.create() |
| 38 | + project = pretend.stub(users=[user], id=pretend.stub()) |
| 39 | + |
| 40 | + MalwareCheckFactory.create( |
| 41 | + name="PackageTurnoverCheck", state=MalwareCheckState.Enabled, |
| 42 | + ) |
| 43 | + check = c.PackageTurnoverCheck(db_session) |
| 44 | + |
| 45 | + user.record_event( |
| 46 | + tag="account:two_factor:method_removed", ip_address="0.0.0.0", additional={} |
| 47 | + ) |
| 48 | + |
| 49 | + check.user_posture_verdicts(project) |
| 50 | + assert len(check._verdicts) == 1 |
| 51 | + assert check._verdicts[0].check_id == check.id |
| 52 | + assert check._verdicts[0].project_id == project.id |
| 53 | + assert check._verdicts[0].classification == VerdictClassification.Threat |
| 54 | + assert check._verdicts[0].confidence == VerdictConfidence.High |
| 55 | + assert ( |
| 56 | + check._verdicts[0].message |
| 57 | + == "User with control over this package has disabled 2FA" |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def test_user_posture_verdicts_hasnt_removed_2fa(db_session): |
| 62 | + user = UserFactory.create() |
| 63 | + project = pretend.stub(users=[user], id=pretend.stub()) |
| 64 | + |
| 65 | + MalwareCheckFactory.create( |
| 66 | + name="PackageTurnoverCheck", state=MalwareCheckState.Enabled, |
| 67 | + ) |
| 68 | + check = c.PackageTurnoverCheck(db_session) |
| 69 | + |
| 70 | + check.user_posture_verdicts(project) |
| 71 | + assert len(check._verdicts) == 0 |
| 72 | + |
| 73 | + |
| 74 | +def test_user_posture_verdicts_has_2fa(db_session): |
| 75 | + user = UserFactory.create(totp_secret=b"fake secret") |
| 76 | + project = pretend.stub(users=[user], id=pretend.stub()) |
| 77 | + |
| 78 | + MalwareCheckFactory.create( |
| 79 | + name="PackageTurnoverCheck", state=MalwareCheckState.Enabled, |
| 80 | + ) |
| 81 | + check = c.PackageTurnoverCheck(db_session) |
| 82 | + |
| 83 | + user.record_event( |
| 84 | + tag="account:two_factor:method_removed", ip_address="0.0.0.0", additional={} |
| 85 | + ) |
| 86 | + |
| 87 | + check.user_posture_verdicts(project) |
| 88 | + assert len(check._verdicts) == 0 |
| 89 | + |
| 90 | + |
| 91 | +def test_user_turnover_verdicts(db_session): |
| 92 | + user = UserFactory.create() |
| 93 | + project = ProjectFactory.create(users=[user]) |
| 94 | + |
| 95 | + project.record_event( |
| 96 | + tag="project:role:add", |
| 97 | + ip_address="0.0.0.0", |
| 98 | + additional={"target_user": user.username}, |
| 99 | + ) |
| 100 | + |
| 101 | + MalwareCheckFactory.create( |
| 102 | + name="PackageTurnoverCheck", state=MalwareCheckState.Enabled, |
| 103 | + ) |
| 104 | + check = c.PackageTurnoverCheck(db_session) |
| 105 | + |
| 106 | + check.user_turnover_verdicts(project) |
| 107 | + assert len(check._verdicts) == 1 |
| 108 | + assert check._verdicts[0].check_id == check.id |
| 109 | + assert check._verdicts[0].project_id == project.id |
| 110 | + assert check._verdicts[0].classification == VerdictClassification.Threat |
| 111 | + assert check._verdicts[0].confidence == VerdictConfidence.High |
| 112 | + assert ( |
| 113 | + check._verdicts[0].message |
| 114 | + == "Suspicious user turnover; all current maintainers are new" |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def test_user_turnover_verdicts_no_turnover(db_session): |
| 119 | + user = UserFactory.create() |
| 120 | + project = ProjectFactory.create(users=[user]) |
| 121 | + |
| 122 | + MalwareCheckFactory.create( |
| 123 | + name="PackageTurnoverCheck", state=MalwareCheckState.Enabled, |
| 124 | + ) |
| 125 | + check = c.PackageTurnoverCheck(db_session) |
| 126 | + |
| 127 | + check.user_turnover_verdicts(project) |
| 128 | + assert len(check._verdicts) == 0 |
| 129 | + |
| 130 | + |
| 131 | +def test_scan(db_session, monkeypatch): |
| 132 | + user = UserFactory.create() |
| 133 | + project = ProjectFactory.create(users=[user]) |
| 134 | + |
| 135 | + for _ in range(3): |
| 136 | + ReleaseFactory.create(project=project) |
| 137 | + |
| 138 | + MalwareCheckFactory.create( |
| 139 | + name="PackageTurnoverCheck", state=MalwareCheckState.Enabled, |
| 140 | + ) |
| 141 | + check = c.PackageTurnoverCheck(db_session) |
| 142 | + |
| 143 | + monkeypatch.setattr( |
| 144 | + check, "user_posture_verdicts", pretend.call_recorder(lambda project: None) |
| 145 | + ) |
| 146 | + monkeypatch.setattr( |
| 147 | + check, "user_turnover_verdicts", pretend.call_recorder(lambda project: None) |
| 148 | + ) |
| 149 | + |
| 150 | + check.scan() |
| 151 | + |
| 152 | + # Each verdict rendering method is only called once per project, |
| 153 | + # thanks to deduplication. |
| 154 | + assert check.user_posture_verdicts.calls == [pretend.call(project)] |
| 155 | + assert check.user_turnover_verdicts.calls == [pretend.call(project)] |
| 156 | + |
| 157 | + |
| 158 | +def test_scan_too_few_releases(db_session, monkeypatch): |
| 159 | + user = UserFactory.create() |
| 160 | + project = ProjectFactory.create(users=[user]) |
| 161 | + ReleaseFactory.create(project=project) |
| 162 | + |
| 163 | + MalwareCheckFactory.create( |
| 164 | + name="PackageTurnoverCheck", state=MalwareCheckState.Enabled, |
| 165 | + ) |
| 166 | + check = c.PackageTurnoverCheck(db_session) |
| 167 | + |
| 168 | + monkeypatch.setattr( |
| 169 | + check, "user_posture_verdicts", pretend.call_recorder(lambda project: None) |
| 170 | + ) |
| 171 | + monkeypatch.setattr( |
| 172 | + check, "user_turnover_verdicts", pretend.call_recorder(lambda project: None) |
| 173 | + ) |
| 174 | + |
| 175 | + check.scan() |
| 176 | + assert check.user_posture_verdicts.calls == [] |
| 177 | + assert check.user_turnover_verdicts.calls == [] |
0 commit comments