Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that relative imports from module import are module-relative #2033

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Jint.Tests/Runtime/Modules/DefaultModuleLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ public void ShouldResolveRelativePaths(string specifier, string expectedUri)
Assert.Equal(SpecifierType.RelativeOrAbsolute, resolved.Type);
}


[Fact]
public void ShouldResolveRelativeToReferencingModuleSpecifier()
{
var resolver = new DefaultModuleLoader("file:///project");

var resolved = resolver.Resolve("referencing-module", new ModuleRequest("./local-module-file.js", []));

Assert.Equal("./local-module-file.js", resolved.ModuleRequest.Specifier);
Assert.Equal("file:///project/referencing-module/local-module-file.js", resolved.Key);
Assert.Equal("file:///project/referencing-module/local-module-file.js", resolved.Uri?.AbsoluteUri);
Assert.Equal(SpecifierType.RelativeOrAbsolute, resolved.Type);
}

[Theory]
[InlineData("./../../other.js")]
[InlineData("../../model/other.js")]
Expand Down
9 changes: 9 additions & 0 deletions Jint/Runtime/Modules/DefaultModuleLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,16 @@ Path traversal prevention is not a concern here because it is checked later
*/
if (Uri.TryCreate(referencingModuleLocation, UriKind.Absolute, out var referencingLocation) ||
Uri.TryCreate(_basePath, referencingModuleLocation, out referencingLocation))
{
if (!Path.HasExtension(referencingLocation.LocalPath) && referencingLocation.LocalPath[^1] != '/')
{
var uriBuilder = new UriBuilder(referencingLocation);
uriBuilder.Path += '/';
return uriBuilder.Uri;
}

return referencingLocation;
}
}
return _basePath;
}
Expand Down
Loading