fix(security): use URL hostname check for isNewRelicDomain + exclude vendor minified files from CodeQL - #2306
Open
pranav-new-relic wants to merge 1 commit into
Open
Conversation
…minified files from CodeQL Fixes CodeQL js/incomplete-url-substring-sanitization alerts #1, #2, #3 (NR-560205). - Replaced substring-based URL check (endsWith/includes) in isNewRelicDomain with URL constructor hostname parsing. The old check allowed crafted URLs like 'evil.com/newrelic.com/page' or 'evil.newrelic.com' to bypass domain detection. The new implementation correctly extracts the hostname and checks against exact domain / subdomain match. - Added .github/codeql/codeql-config.yml to exclude static minified vendor files (static/*.min*.js) from CodeQL analysis, closing alert #3 in tessen.min-1.14.0.js.
| const isNewRelicDomain = (to) => { | ||
| try { | ||
| const { hostname } = new URL(to); | ||
| return hostname === 'newrelic.com' || hostname.endsWith('.newrelic.com'); |
Member
Author
There was a problem hiding this comment.
Why this check works when the old one didn't
The original code used substring checks:
to.endsWith('newrelic.com')→ passes forhttps://evilnewrelic.com❌to.includes('newrelic.com/')→ passes forhttps://evil.com/newrelic.com/path❌
A URL has distinct parts: scheme (https://), hostname (evil.com), and path (/newrelic.com/path). By parsing the URL with the built-in URL constructor we get the hostname field — the actual server the browser connects to — which an attacker cannot forge while still reaching evil.com:
new URL('https://evilnewrelic.com').hostname → 'evilnewrelic.com' → no match ✅
new URL('https://evil.com/newrelic.com/p').hostname → 'evil.com' → no match ✅
new URL('https://docs.newrelic.com/guide').hostname → 'docs.newrelic.com' → .endsWith('.newrelic.com') ✅
The try/catch is there because new URL(x) throws a TypeError if x is not a valid URL (e.g. a bare path like /docs). This function is only called for external links (those starting with http) so in practice it will never throw — but the guard keeps things safe for future callers.
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.
What and Why
This PR fixes three CodeQL
js/incomplete-url-substring-sanitizationalerts in NR-560205.Background (for those new to this): When you check whether a URL belongs to a certain website by looking for substrings like
url.includes("newrelic.com/"), an attacker can craft a URL likehttps://evil.com/newrelic.com/docsthat passes the check even though it clearly belongs toevil.com. The safest way to check a URL's true domain is to actually parse it using the browser's built-inURLclass.Changes
1.
src/@newrelic/gatsby-theme-newrelic/components/Link.js— fixes alerts #1 and #2The
isNewRelicDomainhelper previously used:to.endsWith("newrelic.com")— matchedhttps://evilnewrelic.com❌to.includes("newrelic.com/")— matchedhttps://evil.com/newrelic.com/page❌It now parses the URL with
new URL(to)and checks the actual hostname field, so those crafted URLs are correctly rejected. Atry/catchhandles non-URL inputs gracefully.2.
.github/codeql/codeql-config.yml(new file) — closes alert #3Alert #3 fires in
static/tessen.min-1.14.0.js, a pre-built vendored analytics library. Editing minified files directly is impractical; this config tells CodeQL to skipstatic/**/*.min*.jsfiles, which are build artifacts rather than first-party source.Testing
isNewRelicDomainis used on line 102 to decide whether to includenoreferrerin therelattribute for external links. The fix is a strict improvement: real New Relic domains still work, and crafted URLs are correctly blocked. The try/catch ensures no breakage on unusual inputs.🤖 Generated with Claude Code