-
Notifications
You must be signed in to change notification settings - Fork 853
node: Validate and sanitize RPC URLs #4822
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
johnsaigle
wants to merge
6
commits into
wormhole-foundation:main
Choose a base branch
from
johnsaigle:url-safety
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ead2223
node: Validate and sanitize RPC URLs
johnsaigle 0307c67
Disable linting for hard-coded creds in *_test.go
johnsaigle 4cb6210
sanitize more URL logging locations
johnsaigle c611125
add more cases; specific file for connectors
johnsaigle 9d1aa99
add test
johnsaigle 1f43285
add IBC watcher timestamp conversion; ignore binary
johnsaigle 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
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
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
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net" | ||
| "net/url" | ||
| "strings" | ||
|
|
@@ -40,3 +41,70 @@ func ValidateURL(urlStr string, validSchemes []string) bool { | |
| } | ||
| return false | ||
| } | ||
|
|
||
| // SafeURLForLogging returns only the host and port for a URL-like string. | ||
| // It intentionally omits userinfo, path, query, and fragment because those may contain credentials. | ||
| func SafeURLForLogging(urlStr string) string { | ||
| if urlStr == "" { | ||
| return "" | ||
| } | ||
|
|
||
| parsedURL, err := url.Parse(urlStr) | ||
| if err != nil || parsedURL.Host == "" { | ||
| // Schemeless host:port strings parse as scheme:opaque. Parse them again | ||
| // as network-path references so URL.Hostname can extract the host. | ||
| parsedURL, err = url.Parse("//" + urlStr) | ||
|
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 we keep the scheme? I don't see the harm and it would make debugging easier. |
||
| } | ||
| if err != nil { | ||
| return "<invalid-url>" | ||
| } | ||
|
|
||
| if host := parsedURL.Host; host != "" { | ||
| return host | ||
| } | ||
|
|
||
| return "<invalid-url>" | ||
| } | ||
|
|
||
| // SafeErrorForLogging returns an error string with a raw URL replaced by its safe logging form. | ||
| func SafeErrorForLogging(err error, urlStr string) string { | ||
| if err == nil { | ||
| return "" | ||
| } | ||
| if urlStr == "" { | ||
| return err.Error() | ||
| } | ||
|
|
||
| safeURL := SafeURLForLogging(urlStr) | ||
| errStr := err.Error() | ||
|
|
||
| var urlErr *url.Error | ||
| if errors.As(err, &urlErr) && urlErr.URL != "" { | ||
| // url.Error carries the URL separately. Sanitize that exact value in the | ||
| // full error string so wrapped errors keep their additional context. | ||
| errStr = strings.ReplaceAll(errStr, urlErr.URL, SafeURLForLogging(urlErr.URL)) | ||
| } | ||
|
|
||
| errStr = strings.ReplaceAll(errStr, urlStr, safeURL) | ||
|
|
||
| parsedURL, parseErr := url.Parse(urlStr) | ||
| if parseErr != nil { | ||
| return errStr | ||
| } | ||
|
|
||
| // Some libraries report URLs through url.URL.String(), which can escape or otherwise | ||
| // canonicalize the original string. | ||
| canonicalURL := parsedURL.String() | ||
| errStr = strings.ReplaceAll(errStr, canonicalURL, safeURL) | ||
|
|
||
| if parsedURL.User != nil { | ||
| if _, hasPassword := parsedURL.User.Password(); hasPassword { | ||
| // net/http redacts userinfo passwords as "***" in *url.Error while leaving | ||
| // path and query values intact, so the raw URL no longer matches exactly. | ||
| redactedURL := strings.Replace(canonicalURL, parsedURL.User.String()+"@", parsedURL.User.Username()+":***@", 1) | ||
| errStr = strings.ReplaceAll(errStr, redactedURL, safeURL) | ||
| } | ||
| } | ||
|
|
||
| return errStr | ||
| } | ||
Oops, something went wrong.
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.
Why also strip the path? I don't think that would realistically contain any secrets plus would be helpful to know the actual path delimited endpoint in some scenarios?
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.
IMO we need it. e.g. for Alchemy, they give you a URL like this:
wss://eth-mainnet.g.alchemy.com/v2/W_...<secret here>..., and that would be part of the path. I don't know why they do it like that, but we have to work around it.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.
Ah good point, thanks
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.
Is there any reason that we would WANT to log the path or query? If it's important enough, I could see a world where we only block/strip known patterns or use a regex to strip out specific portions of the path?
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.
I'm not aware of a reason right now. I think we can go ahead with this change for now and do more advanced parsing later if we need it. It seems like a bit of a headache to try to think of every possible provider and analyze their APIs to satisfy a regex or identify known-bad patterns.