Skip to content

Commit fa7cfa2

Browse files
authored
Validate import path (#1211)
1 parent 776dd39 commit fa7cfa2

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

server/build.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,15 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
406406
return esbuild.OnResolveResult{Path: path}, nil
407407
}
408408

409-
// ban file: imports
410-
if strings.HasPrefix(args.Path, "file:") {
409+
// ban `file:` imports
410+
if after, ok := strings.CutPrefix(args.Path, "file:"); ok {
411411
return esbuild.OnResolveResult{
412-
Path: fmt.Sprintf("/error.js?type=unsupported-file-dependency&name=%s&importer=%s", strings.TrimPrefix(args.Path, "file:"), ctx.esmPath.Specifier()),
412+
Path: fmt.Sprintf("/error.js?type=unsupported-file-dependency&name=%s&importer=%s", after, ctx.esmPath.Specifier()),
413413
External: true,
414414
}, nil
415415
}
416416

417-
// skip data: and http: imports
417+
// skip `data:` and `http:` imports
418418
if strings.HasPrefix(args.Path, "data:") || strings.HasPrefix(args.Path, "https:") || strings.HasPrefix(args.Path, "http:") {
419419
return esbuild.OnResolveResult{
420420
Path: args.Path,
@@ -546,6 +546,11 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
546546
filename = path.Join(ctx.wd, "node_modules", specifier)
547547
}
548548

549+
// check if the filename is within the working directory
550+
if !strings.HasPrefix(filename, ctx.wd+string(os.PathSeparator)) {
551+
return esbuild.OnResolveResult{}, fmt.Errorf("could not resolve module %s", specifier)
552+
}
553+
549554
// node native modules do not work via http import
550555
if strings.HasSuffix(filename, ".node") && existsFile(filename) {
551556
return esbuild.OnResolveResult{

0 commit comments

Comments
 (0)