File tree Expand file tree Collapse file tree 1 file changed +16
-5
lines changed
src/sentry/sentry_apps/token_exchange Expand file tree Collapse file tree 1 file changed +16
-5
lines changed Original file line number Diff line number Diff line change 6
6
from django .utils .functional import cached_property
7
7
8
8
from sentry import analytics
9
+ from sentry .locks import locks
9
10
from sentry .models .apiapplication import ApiApplication
10
11
from sentry .models .apigrant import ApiGrant
11
12
from sentry .models .apitoken import ApiToken
@@ -35,12 +36,22 @@ class GrantExchanger:
35
36
def run (self ):
36
37
with transaction .atomic (using = router .db_for_write (ApiToken )):
37
38
try :
38
- self ._validate ()
39
- token = self ._create_token ()
39
+ lock = locks .get (
40
+ ApiGrant .get_lock_key (self .grant .id ),
41
+ duration = 10 ,
42
+ name = "api_grant" ,
43
+ )
44
+
45
+ # we use a lock to prevent race conditions when creating the ApiToken
46
+ # an attacker could send two requests to create an access/refresh token pair
47
+ # at the same time, using the same grant, and get two different tokens
48
+ with lock .acquire ():
49
+ self ._validate ()
50
+ token = self ._create_token ()
40
51
41
- # Once it's exchanged it's no longer valid and should not be
42
- # exchangeable, so we delete it.
43
- self ._delete_grant ()
52
+ # Once it's exchanged it's no longer valid and should not be
53
+ # exchangeable, so we delete it.
54
+ self ._delete_grant ()
44
55
except SentryAppIntegratorError :
45
56
logger .info (
46
57
"grant-exchanger.context" ,
You can’t perform that action at this time.
0 commit comments