Skip to content

Commit 269482f

Browse files
anonrigtargos
authored andcommitted
esm: avoid accessing lazy getters for urls
PR-URL: #47542 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 889add6 commit 269482f

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

lib/internal/modules/esm/resolve.js

+25-15
Original file line numberDiff line numberDiff line change
@@ -894,16 +894,20 @@ function resolveAsCommonJS(specifier, parentURL) {
894894
// TODO(@JakobJingleheimer): de-dupe `specifier` & `parsed`
895895
function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
896896
if (parsedParentURL) {
897+
// Avoid accessing the `protocol` property due to the lazy getters.
898+
const parentProtocol = parsedParentURL.protocol;
897899
if (
898-
parsedParentURL.protocol === 'http:' ||
899-
parsedParentURL.protocol === 'https:'
900+
parentProtocol === 'http:' ||
901+
parentProtocol === 'https:'
900902
) {
901903
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
904+
// Avoid accessing the `protocol` property due to the lazy getters.
905+
const parsedProtocol = parsed?.protocol;
902906
// data: and blob: disallowed due to allowing file: access via
903907
// indirection
904-
if (parsed &&
905-
parsed.protocol !== 'https:' &&
906-
parsed.protocol !== 'http:'
908+
if (parsedProtocol &&
909+
parsedProtocol !== 'https:' &&
910+
parsedProtocol !== 'http:'
907911
) {
908912
throw new ERR_NETWORK_IMPORT_DISALLOWED(
909913
specifier,
@@ -944,22 +948,26 @@ function throwIfInvalidParentURL(parentURL) {
944948
}
945949

946950
function throwIfUnsupportedURLProtocol(url) {
947-
if (url.protocol !== 'file:' && url.protocol !== 'data:' &&
948-
url.protocol !== 'node:') {
951+
// Avoid accessing the `protocol` property due to the lazy getters.
952+
const protocol = url.protocol;
953+
if (protocol !== 'file:' && protocol !== 'data:' &&
954+
protocol !== 'node:') {
949955
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url);
950956
}
951957
}
952958

953959
function throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports) {
960+
// Avoid accessing the `protocol` property due to the lazy getters.
961+
const protocol = parsed?.protocol;
954962
if (
955-
parsed &&
956-
parsed.protocol !== 'file:' &&
957-
parsed.protocol !== 'data:' &&
963+
protocol &&
964+
protocol !== 'file:' &&
965+
protocol !== 'data:' &&
958966
(
959967
!experimentalNetworkImports ||
960968
(
961-
parsed.protocol !== 'https:' &&
962-
parsed.protocol !== 'http:'
969+
protocol !== 'https:' &&
970+
protocol !== 'http:'
963971
)
964972
)
965973
) {
@@ -1016,11 +1024,13 @@ function defaultResolve(specifier, context = {}) {
10161024
parsed = new URL(specifier);
10171025
}
10181026

1019-
if (parsed.protocol === 'data:' ||
1027+
// Avoid accessing the `protocol` property due to the lazy getters.
1028+
const protocol = parsed.protocol;
1029+
if (protocol === 'data:' ||
10201030
(experimentalNetworkImports &&
10211031
(
1022-
parsed.protocol === 'https:' ||
1023-
parsed.protocol === 'http:'
1032+
protocol === 'https:' ||
1033+
protocol === 'http:'
10241034
)
10251035
)
10261036
) {

0 commit comments

Comments
 (0)