Skip to content

Commit 4caaf35

Browse files
authored
refactor: apply 2FA to any non-exempt routes (#15688)
1 parent 3f9e0e2 commit 4caaf35

File tree

4 files changed

+20
-33
lines changed

4 files changed

+20
-33
lines changed

tests/unit/accounts/test_security_policy.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13-
from datetime import datetime
14-
1513
import pretend
1614
import pytest
1715

@@ -527,8 +525,7 @@ def test_acl(self, monkeypatch, policy_class, principals, expected):
527525
identity=pretend.stub(
528526
__principals__=lambda: principals,
529527
has_primary_verified_email=True,
530-
has_two_factor=False,
531-
date_joined=datetime(2022, 8, 1),
528+
has_two_factor=True,
532529
),
533530
matched_route=pretend.stub(name="random.route"),
534531
)
@@ -561,7 +558,6 @@ def test_permits_manage_projects_with_2fa(self, monkeypatch, policy_class):
561558
__principals__=lambda: ["user:5"],
562559
has_primary_verified_email=True,
563560
has_two_factor=True,
564-
date_joined=datetime(2022, 8, 1),
565561
),
566562
matched_route=pretend.stub(name="manage.projects"),
567563
)
@@ -579,7 +575,6 @@ def test_deny_manage_projects_without_2fa(self, monkeypatch, policy_class):
579575
__principals__=lambda: ["user:5"],
580576
has_primary_verified_email=True,
581577
has_two_factor=False,
582-
date_joined=datetime(2023, 8, 9),
583578
),
584579
matched_route=pretend.stub(name="manage.projects"),
585580
)
@@ -597,7 +592,6 @@ def test_deny_forklift_file_upload_without_2fa(self, monkeypatch, policy_class):
597592
__principals__=lambda: ["user:5"],
598593
has_primary_verified_email=True,
599594
has_two_factor=False,
600-
date_joined=datetime(2023, 8, 9),
601595
),
602596
matched_route=pretend.stub(name="forklift.legacy.file_upload"),
603597
)
@@ -627,7 +621,6 @@ def test_permits_2fa_routes_without_2fa(
627621
__principals__=lambda: ["user:5"],
628622
has_primary_verified_email=True,
629623
has_two_factor=False,
630-
date_joined=datetime.now(),
631624
),
632625
matched_route=pretend.stub(name=matched_route),
633626
)

warehouse/accounts/security_policy.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,16 @@ def _check_for_mfa(request, context) -> WarehouseDenied | None:
205205
# at this point, and we only a User in these policies.
206206
assert isinstance(request.identity, User)
207207

208-
# If we're in the manage namespace or file uploads, we'll check if the user
209-
# has 2FA enabled, and if they don't we'll deny them.
208+
if request.identity.has_two_factor:
209+
# We're good to go!
210+
return None
211+
212+
# Return a different message for upload endpoint first.
213+
if request.matched_route.name == "forklift.legacy.file_upload":
214+
return WarehouseDenied(
215+
"You must enable two factor authentication to upload",
216+
reason="upload_2fa_required",
217+
)
210218

211219
# Management routes that don't require 2FA, mostly to set up 2FA.
212220
_exempt_routes = [
@@ -218,26 +226,13 @@ def _check_for_mfa(request, context) -> WarehouseDenied | None:
218226
"accounts.verify-email",
219227
]
220228

221-
if (
222-
request.matched_route.name.startswith("manage")
223-
and request.matched_route.name != "manage.account"
224-
and not any(
225-
request.matched_route.name.startswith(route) for route in _exempt_routes
226-
)
227-
and not request.identity.has_two_factor
228-
):
229-
return WarehouseDenied(
230-
"You must enable two factor authentication to manage other settings",
231-
reason="manage_2fa_required",
232-
)
233-
234-
if (
235-
request.matched_route.name == "forklift.legacy.file_upload"
236-
and not request.identity.has_two_factor
229+
if request.matched_route.name == "manage.account" or any(
230+
request.matched_route.name.startswith(route) for route in _exempt_routes
237231
):
238-
return WarehouseDenied(
239-
"You must enable two factor authentication to upload",
240-
reason="upload_2fa_required",
241-
)
232+
return None
242233

243-
return None
234+
# No exemptions matched, 2FA is required.
235+
return WarehouseDenied(
236+
"You must enable two factor authentication.",
237+
reason="manage_2fa_required",
238+
)

warehouse/locale/messages.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ msgstr ""
705705
msgid "Provide an Inspector link to specific lines of code."
706706
msgstr ""
707707

708-
#: warehouse/packaging/views.py:213
708+
#: warehouse/packaging/views.py:212
709709
msgid "Your report has been recorded. Thank you for your help."
710710
msgstr ""
711711

warehouse/packaging/views.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ def includes_submit_malware_observation(project, request):
184184
renderer="packaging/submit-malware-observation.html",
185185
require_csrf=True,
186186
require_methods=False,
187-
require_reauth=True,
188187
route_name="packaging.project.submit_malware_observation",
189188
uses_session=True,
190189
)

0 commit comments

Comments
 (0)