Skip to content

Commit e1f4d61

Browse files
robhoganmeta-codesync[bot]
authored andcommitted
Resolver: Gracefully reject illegal browser-spec redirects of bare specifier -> absolute path
Summary: While doing some refactoring here I noticed some nonsensical handling of the case where a browser-spec `package.json` tries to redirect a bare specifier to an absolute path, eg: ``` // /project/package.json { "browser": { "foo-pkg": "/var/foo" } } ``` This currently causes the resolver to redirect to `/project/var/foo` - *joining* the absolute path onto the end of the origin-relative path. This is surely never the desired behaviour, and is outside the [`browser` field spec](https://github.com/defunctzombie/package-browser-field-spec#replace-specific-files---advanced). Rather than "fix" this and support something new in an increasingly outdated spec (`browser` redirects have been effectively replaced by `exports`/`imports` maps), here we just replace what was broken with a graceful, actionable rejection. Although this changes observable resolver behaviour, I think it's safe to treat this as non-breaking because the previous behaviour was so unexpected. ``` - **[Fix]**: Resolver: Gracefully reject bare->absolute redirects rather than failing on a spurious path ``` Reviewed By: huntie Differential Revision: D102568988 fbshipit-source-id: 5bf8d6c3f5a26e0b3ea2fd3f49a3dd9fa4113663
1 parent 9341463 commit e1f4d61

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

‎packages/metro-resolver/src/__tests__/index-test.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,31 @@ describe('browser spec redirection', () => {
483483
`);
484484
});
485485

486+
test('rejects redirection of a bare specifier to an absolute path', () => {
487+
const testFileMap = {
488+
...fileMap,
489+
'/root/project/package.json': JSON.stringify({
490+
name: 'project',
491+
browser: {
492+
'foo-pkg': '/otherroot/foo',
493+
},
494+
}),
495+
'/otherroot/foo.js': '',
496+
};
497+
const context = {
498+
...createResolutionContext(testFileMap),
499+
originModulePath: '/root/project/bar.js',
500+
};
501+
expect(() => Resolver.resolve(context, 'foo-pkg', null))
502+
.toThrowErrorMatchingInlineSnapshot(`
503+
"The package /root/project contains an invalid package.json configuration. Consider raising this issue with the package maintainer(s).
504+
Reason: Attempted to redirect import to an absolute path. This is not allowed by the \\"browser\\" spec.
505+
From: /root/project/bar.js
506+
Import: foo-pkg
507+
Attempted redirect: /otherroot/foo"
508+
`);
509+
});
510+
486511
test('resolves source extension candidates to relative paths', () => {
487512
const testFileMap = {
488513
...fileMap,

‎packages/metro-resolver/src/resolve.js‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,10 @@ export default function resolve(
132132
return {type: 'empty'};
133133
}
134134

135-
// If the specifier was redirected to a relative or absolute path
135+
// If the specifier was redirected to a relative path
136136
if (
137137
maybeRedirectedSpecifier != null &&
138-
(isRelativeImport(maybeRedirectedSpecifier) ||
139-
path.isAbsolute(maybeRedirectedSpecifier))
138+
isRelativeImport(maybeRedirectedSpecifier)
140139
) {
141140
// TODO: (robhogan) This isn't right - per browser spec: "All paths for
142141
// browser fields are relative to the package.json file location". The
@@ -151,15 +150,28 @@ export default function resolve(
151150
originModulePath.indexOf(path.sep, fromModuleParentIdx),
152151
);
153152

154-
// TODO: (robhogan) If maybeRedirectedSpecifier is absolute, this join is
155-
// wrong. We should disallow attempts to redirect to an absolute path, as
156-
// that's not part of the "browser" field spec anyway, and is broken here.
157153
const absPath = path.join(originModuleDir, maybeRedirectedSpecifier);
158154
const result = resolveModulePath(context, absPath, platform);
159155
if (result.type === 'failed') {
160156
throw new FailedToResolvePathError(result.candidates);
161157
}
162158
return result.resolution;
159+
} else if (
160+
maybeRedirectedSpecifier != null &&
161+
closestPackageToOrigin != null && // Implied by maybeRedirectedSpecifier != null
162+
// An absolute path inside the file may not match the system's path separator
163+
(path.posix.isAbsolute(maybeRedirectedSpecifier) ||
164+
path.win32.isAbsolute(maybeRedirectedSpecifier))
165+
) {
166+
throw new InvalidPackageConfigurationError({
167+
packagePath: closestPackageToOrigin.rootPath,
168+
reason:
169+
'Attempted to redirect import to an absolute path. ' +
170+
'This is not allowed by the "browser" spec.' +
171+
`\n From: ${originModulePath}` +
172+
`\n Import: ${specifier}` +
173+
`\n Attempted redirect: ${maybeRedirectedSpecifier}`,
174+
});
163175
}
164176

165177
// At this point, maybeRedirectedSpecifier is either null (not redirected),

0 commit comments

Comments
 (0)