-
Notifications
You must be signed in to change notification settings - Fork 1
feature: SignatureValidation #2
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
Open
esodot
wants to merge
5
commits into
blockfrost:master
Choose a base branch
from
andro-devs:feature/blockfrost_secure_webhooks
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # https://dart.dev/guides/libraries/private-files | ||
|
|
||
| # Don't commit the following files and directories created by pub | ||
| .dart_tool/ | ||
| build/ | ||
|
|
||
| # Don't commit the API documentation directory created by dart doc | ||
| doc/api/ | ||
|
|
||
| # Don't commit files and directories created by other development environments | ||
| ## IntelliJ | ||
| *.iml | ||
| *.ipr | ||
| *.iws | ||
| .idea/ | ||
|
|
||
| ## Mac | ||
| .DS_Store |
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
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
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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:blockfrost_api/src/utils/signature_validation_exception.dart'; | ||
| import 'package:blockfrost_api/src/utils/signature_validator.dart'; | ||
| import 'package:crypto/crypto.dart'; | ||
|
|
||
| /// Adapter class which implements the validator interface to validate the blockfrost webhook signature. | ||
| class BlockfrostSignatureValidator implements SignatureValidator { | ||
| @override | ||
| bool validate({ | ||
| required String requestPayload, | ||
| required String signatureHeader, | ||
| required String secretAuthToken, | ||
| int maxToleranceSeconds = defaultMaxToleranceSeconds, | ||
| }) { | ||
| return _validateSignature( | ||
| requestPayload: requestPayload, | ||
| signatureHeader: signatureHeader, | ||
| secretAuthToken: secretAuthToken, | ||
| maxToleranceSeconds: maxToleranceSeconds, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| bool _validateSignature({ | ||
| required String requestPayload, | ||
| required String signatureHeader, | ||
| required String secretAuthToken, | ||
| required int maxToleranceSeconds, | ||
| int? currentUnixTime, | ||
| }) { | ||
| // Parse the timestamp and signature from the header | ||
| String? timestampString; | ||
| List<String> providedSignatures = []; | ||
|
|
||
| final parts = signatureHeader.split(','); | ||
| for (final part in parts) { | ||
| final pair = part.trim().split('='); | ||
| if (pair.length == 2) { | ||
| final key = pair[0]; | ||
| final value = pair[1]; | ||
| if (key == 't') { | ||
| timestampString = value; | ||
| } else if (key == 'v1') { | ||
| providedSignatures.add(value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (timestampString == null || providedSignatures.isEmpty) { | ||
| throw SignatureValidationException( | ||
| "Invalid signature header format.", | ||
| header: signatureHeader, | ||
| payload: requestPayload, | ||
| ); | ||
| } | ||
|
|
||
| final int timestamp; | ||
| try { | ||
| timestamp = int.parse(timestampString); | ||
| } catch (e) { | ||
| throw SignatureValidationException( | ||
| "Invalid timestamp format.", | ||
| header: signatureHeader, | ||
| payload: requestPayload, | ||
| ); | ||
| } | ||
|
|
||
| // Prepare the signature_payload (timestamp.payload) | ||
| final signaturePayload = '$timestampString.$requestPayload'; | ||
|
|
||
| // Compute the expected signature (HMAC-SHA256) | ||
| final key = utf8.encode(secretAuthToken); | ||
| final messageBytes = utf8.encode(signaturePayload); | ||
|
|
||
| final hmac = Hmac(sha256, key); | ||
| final digest = hmac.convert(messageBytes); | ||
| final expectedSignature = digest.toString(); | ||
|
|
||
| // Check for matching signature | ||
| bool signatureMatch = | ||
| providedSignatures.any((sig) => sig == expectedSignature); | ||
|
|
||
| if (!signatureMatch) { | ||
| throw SignatureValidationException( | ||
| "No signature matches the expected signature for the payload.", | ||
| header: signatureHeader, | ||
| payload: requestPayload, | ||
| ); | ||
| } | ||
|
|
||
| // Check timestamp tolerance (prevent replay attacks) | ||
| // Note: currentUnixTime can be injected for testing purposes | ||
| final currentTimestamp = | ||
| currentUnixTime ?? (DateTime.now().millisecondsSinceEpoch ~/ 1000); | ||
| final timeDifference = (currentTimestamp - timestamp).abs(); | ||
| if (timeDifference > maxToleranceSeconds) { | ||
| throw SignatureValidationException( | ||
| "Signature's timestamp is outside of the time tolerance.", | ||
| header: signatureHeader, | ||
| payload: requestPayload, | ||
| ); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /// TESTING ACCESSOR: used only by unit test to access the private method | ||
| class TestBlockfrostValidatorAccessor { | ||
| bool callValidateSignature({ | ||
| required String requestPayload, | ||
| required String signatureHeader, | ||
| required String secretAuthToken, | ||
| required int currentUnixTime, | ||
| int maxToleranceSeconds = defaultMaxToleranceSeconds, | ||
| }) { | ||
| return _validateSignature( | ||
| requestPayload: requestPayload, | ||
| signatureHeader: signatureHeader, | ||
| secretAuthToken: secretAuthToken, | ||
| currentUnixTime: currentUnixTime, | ||
| maxToleranceSeconds: maxToleranceSeconds, | ||
| ); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class SignatureValidationException implements Exception { | ||
| final String message; | ||
| final String header; | ||
| final String payload; | ||
|
|
||
| SignatureValidationException( | ||
| this.message, { | ||
| required this.header, | ||
| required this.payload, | ||
| }); | ||
|
|
||
| @override | ||
| String toString() { | ||
| return 'SignatureValidationException: $message\n' | ||
| 'Header: $header\n' | ||
| 'Payload: $payload'; | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // The interface for the signature validation | ||
| const int defaultMaxToleranceSeconds = 600; | ||
|
|
||
| abstract class SignatureValidator { | ||
| bool validate({ | ||
| required String requestPayload, | ||
| required String signatureHeader, | ||
| required String secretAuthToken, | ||
| int maxToleranceSeconds, | ||
| }); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // Export validator files | ||
| export 'blockfrost_signature_validator.dart'; | ||
| export 'signature_validation_exception.dart'; | ||
| export 'signature_validator.dart'; |
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 you please change the order of the params to match the node.js, python and ruby SDK implementation
https://github.com/blockfrost/blockfrost-python/blob/master/blockfrost/helpers.py#L18
https://github.com/blockfrost/blockfrost-js/blob/master/src/utils/helpers.ts#L211
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.
fixed
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.
fixed