Skip to content

Commit

Permalink
throw recursion error when resolving cursed browser fields (#17)
Browse files Browse the repository at this point in the history
feat: throw recursion error when resolving recursed browser fields
  • Loading branch information
Boshen authored Dec 8, 2023
1 parent 2baf418 commit 1d69609
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 14 deletions.
Empty file.
Empty file.
Empty file.
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,13 +790,16 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
package_json: &PackageJson,
ctx: &mut ResolveContext,
) -> ResolveState {
let Some(specifier) = package_json.resolve_browser_field(path, specifier)? else {
let Some(new_specifier) = package_json.resolve_browser_field(path, specifier)? else {
return Ok(None);
};
if ctx.resolving_alias.as_ref().is_some_and(|s| s == specifier) {
if specifier.is_some_and(|s| s == new_specifier) {
return Ok(None);
}
let specifier = Specifier::parse(specifier).map_err(ResolveError::Specifier)?;
if ctx.resolving_alias.as_ref().is_some_and(|s| s == new_specifier) {
return Err(ResolveError::Recursion);
}
let specifier = Specifier::parse(new_specifier).map_err(ResolveError::Specifier)?;
ctx.with_query_fragment(specifier.query, specifier.fragment);
ctx.with_resolving_alias(specifier.path().to_string());
ctx.with_fully_specified(false);
Expand Down Expand Up @@ -845,7 +848,7 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
if let Some(path) = self.load_alias_value(
cached_path,
alias_key,
new_specifier.path(), // pass in passed alias value
new_specifier.path(), // pass in parsed alias value
specifier,
ctx,
)? {
Expand Down
25 changes: 15 additions & 10 deletions src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,17 @@ impl PackageJson {
let mut browser_field = BrowserField::deserialize(browser_field)?;
// Normalize all relative paths to make browser_field a constant value lookup
if let BrowserField::Map(map) = &mut browser_field {
let relative_paths = map
.keys()
.filter(|path| path.starts_with("."))
.cloned()
.collect::<Vec<_>>();
for relative_path in relative_paths {
if let Some(value) = map.remove(&relative_path) {
let normalized_path = dir.normalize_with(relative_path);
map.insert(normalized_path, value);
let keys = map.keys().cloned().collect::<Vec<_>>();
for key in keys {
// Normalize the key if it looks like a file "foo.js"
if key.extension().is_some() {
map.insert(dir.normalize_with(&key), map[&key].clone());
}
// Normalize the key if it is relative path "./relative"
if key.starts_with(".") {
if let Some(value) = map.remove(&key) {
map.insert(dir.normalize_with(&key), value);
}
}
}
}
Expand Down Expand Up @@ -224,7 +226,10 @@ impl PackageJson {
path: &Path,
request: Option<&str>,
) -> Result<Option<&str>, ResolveError> {
let request = request.map_or(path, |r| Path::new(r));
if self.browser_fields.is_empty() {
return Ok(None);
}
let request = request.map_or(path, Path::new);
for browser in &self.browser_fields {
// Only object is valid, all other types are invalid
// https://github.com/webpack/enhanced-resolve/blob/3a28f47788de794d9da4d1702a3a583d8422cd48/lib/AliasFieldPlugin.js#L44-L52
Expand Down
23 changes: 23 additions & 0 deletions src/tests/browser_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,26 @@ fn crypto_js() {
let resolved_path = resolver.resolve(f.join("crypto-js"), "crypto").map(|r| r.full_path());
assert_eq!(resolved_path, Err(ResolveError::Ignored(f.join("crypto-js"))));
}

// https://github.com/webpack/webpack/blob/87660921808566ef3b8796f8df61bd79fc026108/test/cases/resolving/browser-field/index.js#L40-L43
#[test]
fn recursive() {
let f = super::fixture().join("browser-module");

let resolver = Resolver::new(ResolveOptions {
alias_fields: vec![vec!["browser".into()]],
..ResolveOptions::default()
});

let data = [
("should handle recursive file 1", f.clone(), "recursive-file/a"),
("should handle recursive file 2", f.clone(), "recursive-file/b"),
("should handle recursive file 3", f.clone(), "recursive-file/c"),
("should handle recursive file 4", f.clone(), "recursive-file/d"),
];

for (comment, path, request) in data {
let resolved_path = resolver.resolve(&path, request);
assert_eq!(resolved_path, Err(ResolveError::Recursion), "{comment} {path:?} {request}");
}
}

0 comments on commit 1d69609

Please sign in to comment.