Skip to content

Commit 42c59ff

Browse files
author
Brian Vaughn
committed
DevTooks: Don't dehydrate hook source fileNames
This can cause long file names (URLs) to be truncated which in turn will break named hooks code (since it needs to load those file names). Also fix an issue where an absolute URL was being converted into an incorrect URL.
1 parent 9c7f29e commit 42c59ff

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

packages/react-devtools-extensions/src/parseHookNames.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import type {Thenable} from 'shared/ReactTypes';
2727
import type {SourceConsumer} from './astUtils';
2828

2929
const SOURCE_MAP_REGEX = / ?sourceMappingURL=([^\s'"]+)/gm;
30-
const ABSOLUTE_URL_REGEX = /^https?:\/\//i;
3130
const MAX_SOURCE_LENGTH = 100_000_000;
3231

3332
type AST = mixed;
@@ -282,14 +281,7 @@ function extractAndLoadSourceMaps(
282281
}
283282

284283
let url = sourceMappingURLs[i].split('=')[1];
285-
if (ABSOLUTE_URL_REGEX.test(url)) {
286-
const baseURL = url.slice(0, url.lastIndexOf('/'));
287-
url = `${baseURL}/${url}`;
288-
289-
if (!isValidUrl(url)) {
290-
throw new Error(`Invalid source map URL "${url}"`);
291-
}
292-
} else if (!url.startsWith('/')) {
284+
if (!url.startsWith('http') && !url.startsWith('/')) {
293285
// Resolve paths relative to the location of the file name
294286
const lastSlashIdx = runtimeSourceURL.lastIndexOf('/');
295287
if (lastSlashIdx !== -1) {
@@ -440,16 +432,6 @@ function findHookNames(
440432
return map;
441433
}
442434

443-
function isValidUrl(possibleURL: string): boolean {
444-
try {
445-
// eslint-disable-next-line no-new
446-
new URL(possibleURL);
447-
} catch (_) {
448-
return false;
449-
}
450-
return true;
451-
}
452-
453435
function loadSourceFiles(
454436
locationKeyToHookSourceData: Map<string, HookSourceData>,
455437
): Promise<*> {

packages/react-devtools-shared/src/backend/renderer.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3240,6 +3240,17 @@ export function attach(
32403240
// Never dehydrate the "hooks" object at the top levels.
32413241
return true;
32423242
}
3243+
3244+
if (
3245+
path[path.length - 2] === 'hookSource' &&
3246+
path[path.length - 1] === 'fileName'
3247+
) {
3248+
// It's important to preserve the full file name (URL) for hook sources
3249+
// in case the user has enabled the named hooks feature.
3250+
// Otherwise the frontend may end up with a partial URL which it can't load.
3251+
return true;
3252+
}
3253+
32433254
if (
32443255
path[path.length - 1] === 'subHooks' ||
32453256
path[path.length - 2] === 'subHooks'

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,12 @@ export function dehydrate(
160160
};
161161

162162
case 'string':
163-
return data.length <= 500 ? data : data.slice(0, 500) + '...';
163+
isPathAllowedCheck = isPathAllowed(path);
164+
if (isPathAllowedCheck) {
165+
return data;
166+
} else {
167+
return data.length <= 500 ? data : data.slice(0, 500) + '...';
168+
}
164169

165170
case 'bigint':
166171
cleaned.push(path);

0 commit comments

Comments
 (0)