Repository navigation
fix(payment): fix mutable default argument bug in capture and refund - #350
Open
Sreejith088 wants to merge 1 commit into
Open
Sreejith088 wants to merge 1 commit into
Sreejith088 wants to merge 1 commit into
Conversation
Closes razorpay#134 capture() and refund() both used data={} as a default parameter and then mutated it with data['amount'] = amount. Because Python creates the default dict once at function definition time, every call that omits the data argument shares the same dict object. In a threaded server where a single Client is reused across requests, two threads calling capture() concurrently race on that shared dict: one thread's amount can overwrite the other's before the POST is sent. Fix: change the default to data=None and build a fresh dict per call with dict(data or {}), which also avoids mutating a dict the caller passed in.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #134
capture()andrefund()both useddata={}as a default parameter and then mutated it withdata['amount'] = amount. Because Python creates the default dict once at function-definition time, every call that omits thedataargument shares the same dict object.In a threaded server where a single
Clientis reused across requests (the typical Django/Flask pattern), two threads callingcapture()concurrently race on that shared dict — one thread'samountcan overwrite the other's before the POST is sent, causing a payment to be captured for the wrong amount.Single-threaded it stays hidden because the key is simply overwritten each time.
Root cause
Fix
Same fix applied to
refund().Test plan
datadict