Add input validation for HTTP round parameter#17
Open
alienx5499 wants to merge 1 commit intodrand:masterfrom
Open
Add input validation for HTTP round parameter#17alienx5499 wants to merge 1 commit intodrand:masterfrom
alienx5499 wants to merge 1 commit intodrand:masterfrom
Conversation
4 tasks
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.
Problem
The HTTP handlers for
/public/{round},/{chainHash}/public/{round}, and the V2 API routes (/chains/{chainhash}/rounds/{round},/beacons/{beaconID}/rounds/{round}) parse the round number from the URL but don't perform comprehensive input validation. Whilestrconv.ParseUinthandles some validation (overflow, negative numbers), the API doesn't provide early validation feedback with clear error messages or reasonable maximum limit checks.Location:
routes_handler.go:22-30Previous Code:
Solution
Added input validation: Created
readRoundhelper function with:fmt.Errorfwith error wrapping (%w)Improved error handling: Updated error logging to include client information and request path for better debugging
Added comprehensive test coverage: Added
TestReadRoundandTestReadRoundDirecttest functions with 9 test cases covering:Fixed test infrastructure: Updated
main.goto skip flag parsing during tests to prevent test failuresChanges
routes_handler.go:readRoundhelper function with validation and better error messagesGetBeaconto usereadRoundand include client/path in error logsroutes_handler_test.go(new file):TestReadRoundtest function with comprehensive test casesTestReadRoundDirectfor direct function testingmain.go:init()to skip flag parsing during tests (prevents test binary flag conflicts)Testing
go test -v -run TestReadRoundgo build ./...Impact
This enhancement ensures that:
Related
This addresses the same concern raised in drand/drand#1447, but implemented in the http-relay repository where HTTP handling is now maintained (as recommended by @AnomalRoil). The original PR in drand/drand has been closed in favor of this implementation.