From bf9e9490f2a1c5b90a5d3dae32006dda4c8d832d Mon Sep 17 00:00:00 2001 From: ankur22 Date: Thu, 29 Feb 2024 18:03:10 +0000 Subject: [PATCH 1/4] Remove args argument This argument isn't being used so it's better to remove it for now and add it back in when requested for. --- common/browser_context.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/common/browser_context.go b/common/browser_context.go index dc8ae216a..2afa65e55 100644 --- a/common/browser_context.go +++ b/common/browser_context.go @@ -114,13 +114,13 @@ func NewBrowserContext( wv := rt.ToValue(js.WebVitalIIFEScript) wvi := rt.ToValue(js.WebVitalInitScript) - if err := b.AddInitScript(k6Obj, nil); err != nil { + if err := b.AddInitScript(k6Obj); err != nil { return nil, fmt.Errorf("adding k6 object to new browser context: %w", err) } - if err := b.AddInitScript(wv, nil); err != nil { + if err := b.AddInitScript(wv); err != nil { return nil, fmt.Errorf("adding web vital script to new browser context: %w", err) } - if err := b.AddInitScript(wvi, nil); err != nil { + if err := b.AddInitScript(wvi); err != nil { return nil, fmt.Errorf("adding web vital init script to new browser context: %w", err) } @@ -128,7 +128,7 @@ func NewBrowserContext( } // AddInitScript adds a script that will be initialized on all new pages. -func (b *BrowserContext) AddInitScript(script goja.Value, arg goja.Value) error { +func (b *BrowserContext) AddInitScript(script goja.Value) error { b.logger.Debugf("BrowserContext:AddInitScript", "bctxid:%v", b.id) rt := b.vu.Runtime() From e2a0ed0210928bf819da10c43a602acf71397533 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Thu, 29 Feb 2024 18:18:53 +0000 Subject: [PATCH 2/4] Remove goja from browserContext.AddInitScript --- browser/mapping.go | 30 +++++++++++++++++++++++++-- common/browser_context.go | 43 ++++++--------------------------------- 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/browser/mapping.go b/browser/mapping.go index a53beb76b..5a3fc67d3 100644 --- a/browser/mapping.go +++ b/browser/mapping.go @@ -3,6 +3,7 @@ package browser import ( "context" "fmt" + "reflect" "time" "github.com/dop251/goja" @@ -811,8 +812,33 @@ func mapWorker(vu moduleVU, w *common.Worker) mapping { func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolint:funlen rt := vu.Runtime() return mapping{ - "addCookies": bc.AddCookies, - "addInitScript": bc.AddInitScript, + "addCookies": bc.AddCookies, + "addInitScript": func(script goja.Value) error { + source := "" + if gojaValueExists(script) { + switch script.ExportType() { + case reflect.TypeOf(string("")): + source = script.String() + case reflect.TypeOf(goja.Object{}): + opts := script.ToObject(rt) + for _, k := range opts.Keys() { + switch k { + case "content": + source = opts.Get(k).String() + } + } + default: + _, isCallable := goja.AssertFunction(script) + if !isCallable { + source = fmt.Sprintf("(%s);", script.ToString().String()) + } else { + source = fmt.Sprintf("(%s)(...args);", script.ToString().String()) + } + } + } + + return bc.AddInitScript(source) //nolint:wrapcheck + }, "browser": bc.Browser, "clearCookies": bc.ClearCookies, "clearPermissions": bc.ClearPermissions, diff --git a/common/browser_context.go b/common/browser_context.go index 2afa65e55..f736df525 100644 --- a/common/browser_context.go +++ b/common/browser_context.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/url" - "reflect" "strings" "time" @@ -109,18 +108,13 @@ func NewBrowserContext( } } - rt := b.vu.Runtime() - k6Obj := rt.ToValue(js.K6ObjectScript) - wv := rt.ToValue(js.WebVitalIIFEScript) - wvi := rt.ToValue(js.WebVitalInitScript) - - if err := b.AddInitScript(k6Obj); err != nil { + if err := b.AddInitScript(js.K6ObjectScript); err != nil { return nil, fmt.Errorf("adding k6 object to new browser context: %w", err) } - if err := b.AddInitScript(wv); err != nil { + if err := b.AddInitScript(js.WebVitalIIFEScript); err != nil { return nil, fmt.Errorf("adding web vital script to new browser context: %w", err) } - if err := b.AddInitScript(wvi); err != nil { + if err := b.AddInitScript(js.WebVitalInitScript); err != nil { return nil, fmt.Errorf("adding web vital init script to new browser context: %w", err) } @@ -128,38 +122,13 @@ func NewBrowserContext( } // AddInitScript adds a script that will be initialized on all new pages. -func (b *BrowserContext) AddInitScript(script goja.Value) error { +func (b *BrowserContext) AddInitScript(script string) error { b.logger.Debugf("BrowserContext:AddInitScript", "bctxid:%v", b.id) - rt := b.vu.Runtime() - - source := "" - if gojaValueExists(script) { - switch script.ExportType() { - case reflect.TypeOf(string("")): - source = script.String() - case reflect.TypeOf(goja.Object{}): - opts := script.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "content": - source = opts.Get(k).String() - } - } - default: - _, isCallable := goja.AssertFunction(script) - if !isCallable { - source = fmt.Sprintf("(%s);", script.ToString().String()) - } else { - source = fmt.Sprintf("(%s)(...args);", script.ToString().String()) - } - } - } - - b.evaluateOnNewDocumentSources = append(b.evaluateOnNewDocumentSources, source) + b.evaluateOnNewDocumentSources = append(b.evaluateOnNewDocumentSources, script) for _, p := range b.browser.getPages() { - if err := p.evaluateOnNewDocument(source); err != nil { + if err := p.evaluateOnNewDocument(script); err != nil { return fmt.Errorf("adding init script to browser context: %w", err) } } From d3879822f6b1a317cbf87b592baeeedcfad5b75c Mon Sep 17 00:00:00 2001 From: ankur22 Date: Thu, 29 Feb 2024 18:30:03 +0000 Subject: [PATCH 3/4] Use if instead of switch This was to avoid linter issues. --- browser/mapping.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/browser/mapping.go b/browser/mapping.go index 5a3fc67d3..1024137d4 100644 --- a/browser/mapping.go +++ b/browser/mapping.go @@ -822,8 +822,7 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin case reflect.TypeOf(goja.Object{}): opts := script.ToObject(rt) for _, k := range opts.Keys() { - switch k { - case "content": + if k == "content" { source = opts.Get(k).String() } } From 2c0a7784413b0676d850a4c4dedb9cfa7962ebde Mon Sep 17 00:00:00 2001 From: ankur22 Date: Mon, 4 Mar 2024 09:41:29 +0000 Subject: [PATCH 4/4] Refactor addInitScript for early escape --- browser/mapping.go | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/browser/mapping.go b/browser/mapping.go index 1024137d4..e5239eeb9 100644 --- a/browser/mapping.go +++ b/browser/mapping.go @@ -814,26 +814,28 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin return mapping{ "addCookies": bc.AddCookies, "addInitScript": func(script goja.Value) error { + if !gojaValueExists(script) { + return nil + } + source := "" - if gojaValueExists(script) { - switch script.ExportType() { - case reflect.TypeOf(string("")): - source = script.String() - case reflect.TypeOf(goja.Object{}): - opts := script.ToObject(rt) - for _, k := range opts.Keys() { - if k == "content" { - source = opts.Get(k).String() - } - } - default: - _, isCallable := goja.AssertFunction(script) - if !isCallable { - source = fmt.Sprintf("(%s);", script.ToString().String()) - } else { - source = fmt.Sprintf("(%s)(...args);", script.ToString().String()) + switch script.ExportType() { + case reflect.TypeOf(string("")): + source = script.String() + case reflect.TypeOf(goja.Object{}): + opts := script.ToObject(rt) + for _, k := range opts.Keys() { + if k == "content" { + source = opts.Get(k).String() } } + default: + _, isCallable := goja.AssertFunction(script) + if !isCallable { + source = fmt.Sprintf("(%s);", script.ToString().String()) + } else { + source = fmt.Sprintf("(%s)(...args);", script.ToString().String()) + } } return bc.AddInitScript(source) //nolint:wrapcheck