From 7ee89ef7630a5a1a939783269167ee357bf2686f Mon Sep 17 00:00:00 2001 From: Denis Santos Date: Fri, 15 May 2026 14:12:38 +1000 Subject: [PATCH] fix(v8engine): handle nil logger and module download errors Two latent issues in V8ScriptContext: 1. DownloadModule called InstantiateModule even when the prior download returned an error, causing a nil-deref. Return the download error. 2. logger() dereferenced host.logger unconditionally. Fall back to the default logger when nil so callers don't need to set one up. --- scripting/v8engine/script_context.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/scripting/v8engine/script_context.go b/scripting/v8engine/script_context.go index e6fc2b5b..5c9798fa 100644 --- a/scripting/v8engine/script_context.go +++ b/scripting/v8engine/script_context.go @@ -11,6 +11,7 @@ import ( "github.com/gost-dom/browser/internal/constants" "github.com/gost-dom/browser/internal/entity" "github.com/gost-dom/browser/internal/gosthttp" + "github.com/gost-dom/browser/internal/log" "github.com/gost-dom/browser/scripting/internal/codec" "github.com/gost-dom/browser/scripting/internal/js" "github.com/gost-dom/browser/url" @@ -183,13 +184,21 @@ func (ctx *V8ScriptContext) DownloadScript(src string) (html.Script, error) { func (ctx *V8ScriptContext) DownloadModule(src string) (html.Script, error) { url := html.WindowResolveHref(ctx.browsingCtx, src) module, err := ctx.resolver.downloadAndCompile(ctx.Context(), ctx.logger(), url) - if err = module.InstantiateModule(ctx.v8ctx, &ctx.resolver); err != nil { + if err != nil { + return nil, fmt.Errorf("gost: v8engine: module download: %w", err) + } + if err := module.InstantiateModule(ctx.v8ctx, &ctx.resolver); err != nil { return nil, fmt.Errorf("gost: v8engine: module instantiation: %w", err) } return V8Module{ctx, module, ctx.logger(), url}, nil } -func (ctx *V8ScriptContext) logger() *slog.Logger { return ctx.host.logger } +func (ctx *V8ScriptContext) logger() *slog.Logger { + if ctx.host.logger == nil { + return log.Default() + } + return ctx.host.logger +} type resolvedModule struct { scriptID int