Skip to content

Commit f5307e2

Browse files
committed
Handle invalid URLs
1 parent 7b5657b commit f5307e2

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

packages/react-devtools-extensions/src/main/fetchFileWithCaching.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* global chrome */
22

3+
import {normalizeUrlIfValid} from 'react-devtools-shared/src/utils';
34
import {__DEBUG__} from 'react-devtools-shared/src/constants';
45

56
let debugIDCounter = 0;
@@ -116,7 +117,7 @@ async function fetchFileWithCaching(url: string): Promise<string> {
116117
chrome.devtools.inspectedWindow.getResources(r => resolve(r)),
117118
);
118119

119-
const normalizedReferenceURL = new URL(url).toString();
120+
const normalizedReferenceURL = normalizeUrlIfValid(url);
120121
const resource = resources.find(r => r.url === normalizedReferenceURL);
121122

122123
if (resource != null) {

packages/react-devtools-extensions/src/main/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
LOCAL_STORAGE_TRACE_UPDATES_ENABLED_KEY,
1717
} from 'react-devtools-shared/src/constants';
1818
import {logEvent} from 'react-devtools-shared/src/Logger';
19+
import {normalizeUrlIfValid} from 'react-devtools-shared/src/utils';
1920

2021
import {
2122
setBrowserSelectionFromReact,
@@ -129,9 +130,7 @@ function createBridgeAndStore() {
129130

130131
// We use 1-based line and column, Chrome expects them 0-based.
131132
chrome.devtools.panels.openResource(
132-
// openResource needs normalized URLs.
133-
// Using `URL` triggers path normalization.
134-
new URL(sourceURL).toString(),
133+
normalizeUrlIfValid(sourceURL),
135134
line - 1,
136135
column - 1,
137136
);

packages/react-devtools-shared/src/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,19 @@ export function backendToFrontendSerializedElementMapper(
996996
};
997997
}
998998

999+
/**
1000+
* Should be used when treating url as a Chrome Resource URL.
1001+
*/
1002+
export function normalizeUrlIfValid(url: string): string {
1003+
try {
1004+
return new URL(url).toString();
1005+
} catch {
1006+
// Giving up if it's not a valid URL without basepath
1007+
// TODO: Chrome will use the basepath to create a Resource URL.
1008+
return url;
1009+
}
1010+
}
1011+
9991012
export function getIsReloadAndProfileSupported(): boolean {
10001013
// Notify the frontend if the backend supports the Storage API (e.g. localStorage).
10011014
// If not, features like reload-and-profile will not work correctly and must be disabled.

0 commit comments

Comments
 (0)