-
Notifications
You must be signed in to change notification settings - Fork 302
feat: add verification flow for v4 #8163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MohammedRyaan786
wants to merge
1
commit into
master
Choose a base branch
from
CAAS-829
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,313
−54
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,46 +230,99 @@ export function verifyResponse( | |
| return response; | ||
| } | ||
|
|
||
| const verificationResponse = bitgo.verifyResponse({ | ||
| url: req.url, | ||
| hmac: response.header.hmac, | ||
| statusCode: response.status, | ||
| text: response.text, | ||
| timestamp: response.header.timestamp, | ||
| token: req.authenticationToken, | ||
| method, | ||
| authVersion, | ||
| }); | ||
| // --- Build version-specific params, call bitgo.verifyResponse(), collect error context --- | ||
| let verificationResult: { | ||
| isValid: boolean; | ||
| expectedHmac: string; | ||
| isInResponseValidityWindow: boolean; | ||
| verificationTime: number; | ||
| }; | ||
| let hmacErrorDetails: Record<string, unknown>; | ||
| let responseTimestamp: string | number; | ||
|
|
||
| if (authVersion === 4) { | ||
| const hmac = response.header['x-signature']; | ||
| const timestamp = response.header['x-request-timestamp']; | ||
| const authRequestId = response.header['x-auth-request-id']; | ||
|
|
||
| if (!hmac || !timestamp) { | ||
| // Server didn't sign the response. This can happen legitimately when the | ||
MohammedRyaan786 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| debug( | ||
| 'v4 response verification skipped: server response (status %d) missing HMAC headers (x-signature: %s, x-request-timestamp: %s)', | ||
| response.status, | ||
| hmac ? 'present' : 'missing', | ||
| timestamp ? 'present' : 'missing' | ||
| ); | ||
| return response; | ||
| } | ||
|
|
||
| // Hash the raw response body bytes. | ||
| // Convert response.text to a Buffer (UTF-8) so we're hashing the actual bytes, | ||
| // not relying on Node's implicit string encoding in crypto.update(). | ||
| const rawResponseBuffer = Buffer.from(response.text || ''); | ||
| const bodyHashHex = bitgo.calculateBodyHash(rawResponseBuffer); | ||
|
|
||
| // req.v4PathWithQuery is always set by requestPatch; fallback parses req.url as a safety net. | ||
| let pathWithQuery = req.v4PathWithQuery; | ||
| if (!pathWithQuery) { | ||
| const parsedUrl = new URL(req.url); | ||
| pathWithQuery = parsedUrl.pathname + parsedUrl.search; | ||
| } | ||
|
|
||
| if (!verificationResponse.isValid) { | ||
| // calculate the HMAC | ||
| const receivedHmac = response.header.hmac; | ||
| const expectedHmac = verificationResponse.expectedHmac; | ||
| const signatureSubject = verificationResponse.signatureSubject; | ||
| // Log only the first 10 characters of the token to ensure the full token isn't logged. | ||
| const result = bitgo.verifyResponse({ | ||
| hmac, | ||
| timestampSec: Number(timestamp), | ||
| method: req.v4Method || method, | ||
| pathWithQuery, | ||
| bodyHashHex, | ||
| authRequestId: authRequestId || req.v4AuthRequestId || '', | ||
| statusCode: response.status, | ||
| rawToken: req.authenticationToken, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the authVersion be passed here? |
||
| }); | ||
MohammedRyaan786 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| verificationResult = result; | ||
| responseTimestamp = timestamp; | ||
| hmacErrorDetails = { expectedHmac: result.expectedHmac, receivedHmac: hmac, preimage: result.preimage }; | ||
| } else { | ||
| const result = bitgo.verifyResponse({ | ||
| url: req.url, | ||
| hmac: response.header.hmac, | ||
| statusCode: response.status, | ||
| text: response.text, | ||
| timestamp: response.header.timestamp, | ||
| token: req.authenticationToken, | ||
| method, | ||
| authVersion, | ||
| }); | ||
|
|
||
| verificationResult = result; | ||
| responseTimestamp = response.header.timestamp; | ||
| const partialBitgoToken = token ? token.substring(0, 10) : ''; | ||
| const errorDetails = { | ||
| expectedHmac, | ||
| receivedHmac, | ||
| hmacInput: signatureSubject, | ||
| hmacErrorDetails = { | ||
| expectedHmac: result.expectedHmac, | ||
| receivedHmac: response.header.hmac, | ||
| hmacInput: result.signatureSubject, | ||
| requestToken: req.authenticationToken, | ||
| bitgoToken: partialBitgoToken, | ||
| }; | ||
| debug('Invalid response HMAC: %O', errorDetails); | ||
| throw new ApiResponseError('invalid response HMAC, possible man-in-the-middle-attack', 511, errorDetails); | ||
| } | ||
|
|
||
| if (bitgo.getAuthVersion() === 3 && !verificationResponse.isInResponseValidityWindow) { | ||
| const errorDetails = { | ||
| timestamp: response.header.timestamp, | ||
| verificationTime: verificationResponse.verificationTime, | ||
| }; | ||
| // --- Common validation for all auth versions --- | ||
| if (!verificationResult.isValid) { | ||
| debug('Invalid response HMAC: %O', hmacErrorDetails); | ||
| throw new ApiResponseError('invalid response HMAC, possible man-in-the-middle-attack', 511, hmacErrorDetails); | ||
| } | ||
|
|
||
| // v3 and v4 enforce the response validity window; v2 does not | ||
| if (authVersion >= 3 && !verificationResult.isInResponseValidityWindow) { | ||
| const errorDetails = { timestamp: responseTimestamp, verificationTime: verificationResult.verificationTime }; | ||
| debug('Server response outside response validity time window: %O', errorDetails); | ||
| throw new ApiResponseError( | ||
| 'server response outside response validity time window, possible man-in-the-middle-attack', | ||
| 511, | ||
| errorDetails | ||
| ); | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we extract out v4 to a separate handler function?