Skip to content

Commit 937b087

Browse files
committed
Add extra check
1 parent cc0ff55 commit 937b087

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

tests/unit/macaroons/test_caveats.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,25 @@ def test_verify_no_identity(self):
271271

272272
assert result == Failure("token with user restriction without a user")
273273

274-
def test_verify_invalid_identity(self):
274+
def test_verify_invalid_identity_no_user(self):
275275
caveat = RequestUser(user_id="invalid")
276276
result = caveat.verify(
277277
pretend.stub(identity=pretend.stub()), pretend.stub(), pretend.stub()
278278
)
279279

280280
assert result == Failure("token with user restriction without a user")
281281

282+
def test_verify_invalid_identity_no_macaroon(self, db_request):
283+
user = UserFactory.create()
284+
user_context = UserContext(user, None)
285+
286+
caveat = RequestUser(user_id=str(user.id))
287+
result = caveat.verify(
288+
pretend.stub(identity=user_context), pretend.stub(), pretend.stub()
289+
)
290+
291+
assert result == Failure("token with user restriction without a macaroon")
292+
282293
def test_verify_invalid_user_id(self, db_request):
283294
user = UserFactory.create()
284295
user_context = UserContext(user, pretend.stub())

warehouse/macaroons/caveats/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def verify(self, request: Request, context: Any, permission: str) -> Result:
107107
if not isinstance(request.identity, UserContext):
108108
return Failure("token with user restriction without a user")
109109

110+
if request.identity.macaroon is None:
111+
return Failure("token with user restriction without a macaroon")
112+
110113
if str(request.identity.user.id) != self.user_id:
111114
return Failure("current user does not match user restriction in token")
112115

0 commit comments

Comments
 (0)