Skip to content

Commit afbfec3

Browse files
authored
fix(2fa): require 2FA for auth token requests (dry run 3) (#92250)
Continuation of #92015 Really make sure that 2FA requirement is not visible to users outside of the organization. After this PR, cases logged with `access.not-2fa-compliant.dry-run` should be nearly zero.
1 parent 6717ba0 commit afbfec3

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/sentry/api/permissions.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,19 @@ def determine_access(
167167
organization = org_context.organization
168168
extra = {"organization_id": organization.id, "user_id": user_id}
169169

170-
is_token_access_allowed = False
171-
if request.auth and request.user and request.user.is_authenticated:
172-
request.access = access.from_request_org_and_scopes(
173-
request=request,
174-
rpc_user_org_context=org_context,
175-
scopes=request.auth.get_scopes(),
176-
)
177-
is_token_access_allowed = True
178-
elif request.auth:
179-
request.access = access.from_rpc_auth(
180-
auth=request.auth, rpc_user_org_context=org_context
181-
)
182-
is_token_access_allowed = True
170+
if request.auth:
171+
if request.user and request.user.is_authenticated:
172+
request.access = access.from_request_org_and_scopes(
173+
request=request,
174+
rpc_user_org_context=org_context,
175+
scopes=request.auth.get_scopes(),
176+
)
177+
else:
178+
request.access = access.from_rpc_auth(
179+
auth=request.auth, rpc_user_org_context=org_context
180+
)
183181

184-
if is_token_access_allowed:
185-
if self.is_not_2fa_compliant(request, organization):
182+
if org_context.member and self.is_not_2fa_compliant(request, organization):
186183
logger.info(
187184
"access.not-2fa-compliant.dry-run",
188185
extra=extra,

tests/sentry/api/bases/test_organization.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ def test_org_requires_2fa_with_superuser_not_active(self):
170170
with pytest.raises(SuperuserRequired):
171171
assert self.has_object_perm("GET", self.org, user=user)
172172

173+
def test_org_does_not_require_2fa_for_user_auth_token_request_if_no_membership(self):
174+
# make sure that 2FA requirement is not visible to the outsiders
175+
self.org_require_2fa()
176+
177+
other_org = self.create_organization()
178+
user = self.create_user()
179+
self.create_member(user=user, organization=other_org, role="owner")
180+
token = self.create_user_auth_token(user)
181+
182+
request = drf_request_from_request(self.make_request(user=user, auth=token, method="GET"))
183+
permission = self.permission_cls()
184+
185+
with assume_test_silo_mode(SiloMode.CONTROL):
186+
permission.determine_access(request=request, organization=self.org)
187+
173188
def test_sentryapp_passes_2fa(self):
174189
self.org_require_2fa()
175190
internal_sentry_app = self.create_internal_integration(

0 commit comments

Comments
 (0)