Fix http network construction: avoid log.Fatal and validate host #97
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
tlock/networks/http.NewNetworkfrom terminating the process on bad input.Problem
tlock/networks/http.NewNetworkis part of a library API, but it previously calledlog.Fatalwhen URL parsing failed, which would exit the entire process of any downstream application importingtlock(including CLIs, services, or libraries) and is unexpected and makes error handling and testing difficult.Additionally, the previous URL parsing check was too permissive: some malformed host inputs could still parse as a URL path and slip through, causing failures later in the network setup instead of being rejected up front.
Initially I expected url.Parse to reject these inputs, but some malformed hosts still parsed as paths, which is why the stricter validation lives in normalizeHost.
Changes
File:
networks/http/http.golog.Fatalcall inNewNetworkwith a returned error so callerscan handle invalid input without the process exiting.
normalizeHost(host)to:https://scheme when the host doesn’t include one.chain hash mistmatch→chain hash mismatchFile:
networks/http/http_test.goTestNewNetwork_InvalidInputsReturnErrorwith deterministic cases:http://%)https://)not-hex)Notes
This change improves robustness for any downstream code using
tlockas a library: invalid host strings now return errors instead of terminating the entire process.Testing
From the
tlockmodule:This runs:
github.com/drand/tlockgithub.com/drand/tlock/networks/fixedgithub.com/drand/tlock/networks/httpgithub.com/drand/tlock/cmd/tle/commandsand passes with the new tests in place.