fix(resolver): make sure package.json path is inside the resolved path (#1481)

This commit is contained in:
Boshen 2023-11-21 17:24:06 +08:00 committed by GitHub
parent 79c2bac6b1
commit 4dbe17c33a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View file

@ -282,6 +282,11 @@ impl CachedPathImpl {
let Ok(package_json_string) = fs.read_to_string(&package_json_path) else {
return Ok(None);
};
let package_json_path = if options.symlinks {
self.realpath(fs)?.join("package.json")
} else {
package_json_path
};
PackageJson::parse(package_json_path.clone(), &package_json_string, options)
.map(Arc::new)
.map(Some)

View file

@ -202,11 +202,16 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
let path = self.load_realpath(&cached_path)?;
// enhanced-resolve: restrictions
self.check_restrictions(&path)?;
let package_json = cached_path.find_package_json(&self.cache.fs, &self.options)?;
if let Some(package_json) = &package_json {
// path must be inside the package.
debug_assert!(path.starts_with(package_json.directory()));
}
Ok(Resolution {
path,
query: ctx.query.take(),
fragment: ctx.fragment.take(),
package_json: cached_path.find_package_json(&self.cache.fs, &self.options)?,
package_json,
})
}