Sanitize blockchain API URLs to prevent injection attacks#706
Merged
Conversation
…igins The CodeQL taint tracker follows user input through new URL(url) → parsedUrl.toString() → fetch(), and doesn't recognize Set.has() or regex checks as sanitizers. Break the taint chain by looking up the origin from getTrustedOrigin() which returns hardcoded string literals via an if/else chain, then reconstructing the URL with new URL(path, origin). Also strips credentials and fragment from parsed URLs for defense in depth. https://claude.ai/code/session_011pxQ2tSP9bsZzKdmA8gzz8
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.
Summary
Adds URL sanitization to the
fetchJsonfunction inblockchain-api.tsto prevent potential injection attacks. The changes introduce agetTrustedOriginhelper function that validates hostnames against a whitelist of allowed providers and reconstructs URLs using only safe components (pathname and search), while stripping credentials and fragments.Type of Change
Details
What changed:
getTrustedOrigin()function that maps allowed hostnames to their canonical HTTPS originsfetchJson()to validate the parsed URL's hostname against the trusted origin listsafeUrlinstead of the originalparsedUrlWhy it matters:
This prevents potential URL injection attacks where malicious input could:
The sanitization ensures that only the pathname and query string from user input are preserved, while the origin is always taken from the trusted whitelist.
Test Plan
Existing validation logic remains intact and continues to check paths against
ALLOWED_PATHSregex patterns. The new sanitization layer adds defense-in-depth by ensuring the final URL always uses a whitelisted origin. Manual verification can confirm that:Checklist
npm run typecheckpassesnpm run lintpassesnpm run testpasseshttps://claude.ai/code/session_011pxQ2tSP9bsZzKdmA8gzz8