|
1 | 1 | import sentry_sdk |
2 | 2 | from django.db import router, transaction |
3 | | -from jwt import DecodeError, ExpiredSignatureError, InvalidAlgorithmError, InvalidSignatureError |
| 3 | +from jwt import ( |
| 4 | + InvalidKeyError, |
| 5 | +) |
4 | 6 | from rest_framework import status |
5 | 7 | from rest_framework.request import Request |
6 | 8 | from rest_framework.response import Response |
|
21 | 23 | ) |
22 | 24 | from sentry.utils import jwt |
23 | 25 |
|
24 | | -# Atlassian sends scanner bots to "test" Atlassian apps and they often hit this endpoint with a bad kid causing errors |
25 | | -INVALID_KEY_IDS = ["fake-kid"] |
26 | | - |
27 | 26 |
|
28 | 27 | @control_silo_endpoint |
29 | 28 | class JiraSentryInstalledWebhook(JiraWebhookBase): |
@@ -57,36 +56,17 @@ def post(self, request: Request, *args, **kwargs) -> Response: |
57 | 56 | } |
58 | 57 | ) |
59 | 58 |
|
60 | | - if key_id: |
61 | | - if key_id in INVALID_KEY_IDS: |
62 | | - lifecycle.record_halt(halt_reason="JWT contained invalid key_id (kid)") |
63 | | - return self.respond( |
64 | | - {"detail": "Invalid key id"}, status=status.HTTP_400_BAD_REQUEST |
65 | | - ) |
| 59 | + if not key_id: |
| 60 | + lifecycle.record_halt(halt_reason="Missing key_id (kid)") |
| 61 | + return self.respond( |
| 62 | + {"detail": "Missing key id"}, status=status.HTTP_400_BAD_REQUEST |
| 63 | + ) |
| 64 | + try: |
66 | 65 | decoded_claims = authenticate_asymmetric_jwt(token, key_id) |
67 | | - else: |
68 | | - shared_secret = state.get("sharedSecret") |
69 | | - if not shared_secret: |
70 | | - return self.respond( |
71 | | - {"detail": "Missing shared secret"}, status=status.HTTP_400_BAD_REQUEST |
72 | | - ) |
73 | | - try: |
74 | | - decoded_claims = jwt.decode(token, shared_secret, audience=False) |
75 | | - except ( |
76 | | - InvalidSignatureError, |
77 | | - ExpiredSignatureError, |
78 | | - DecodeError, |
79 | | - InvalidAlgorithmError, |
80 | | - ): |
81 | | - return self.respond( |
82 | | - {"detail": "Invalid JWT"}, status=status.HTTP_400_BAD_REQUEST |
83 | | - ) |
84 | | - |
85 | | - if decoded_claims.get("iss") != state.get("clientKey"): |
86 | | - lifecycle.record_halt(halt_reason="JWT issuer does not match client key") |
| 66 | + except InvalidKeyError: |
| 67 | + lifecycle.record_halt(halt_reason="JWT contained invalid key_id (kid)") |
87 | 68 | return self.respond( |
88 | | - {"detail": "JWT issuer does not match client key"}, |
89 | | - status=status.HTTP_400_BAD_REQUEST, |
| 69 | + {"detail": "Invalid key id"}, status=status.HTTP_400_BAD_REQUEST |
90 | 70 | ) |
91 | 71 |
|
92 | 72 | verify_claims(decoded_claims, request.path, request.GET, method="POST") |
|
0 commit comments