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

fix bug in WaitLoad when event has circular reference #1150

Open
wants to merge 3 commits 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 fixtures/wait-load-circular-reference.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<head>
<script>
let a = { b: 1 }
a.c = a
window.addEventListener('load', (e) => {
e.delegateTarget = a
})
</script>
</head>
<body>
<iframe src="http://localhost:8080" />
</body>
</html>
2 changes: 1 addition & 1 deletion lib/js/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var WaitIdle = &Function{
// WaitLoad ...
var WaitLoad = &Function{
Name: "waitLoad",
Definition: `function(){const n=this===window;return new Promise((e,t)=>{if(n){if("complete"===document.readyState)return e();window.addEventListener("load",e)}else void 0===this.complete||this.complete?e():(this.addEventListener("load",e),this.addEventListener("error",t))})}`,
Definition: `function(){const n=this===window;return new Promise((t,e)=>{if(n){if("complete"===document.readyState)return t();window.addEventListener("load",e=>t())}else void 0===this.complete||this.complete?t():(this.addEventListener("load",e=>t()),this.addEventListener("error",e))})}`,
Dependencies: []*Function{},
}

Expand Down
4 changes: 2 additions & 2 deletions lib/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ const functions = {
return new Promise((resolve, reject) => {
if (isWin) {
if (document.readyState === 'complete') return resolve()
window.addEventListener('load', resolve)
window.addEventListener('load', _=>resolve())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
window.addEventListener('load', _=>resolve())
window.addEventListener('load', resolve)

} else {
if (this.complete === undefined || this.complete) {
resolve()
} else {
this.addEventListener('load', resolve)
this.addEventListener('load', _=>resolve())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.addEventListener('load', _=>resolve())
this.addEventListener('load', resolve)

this.addEventListener('error', reject)
}
}
Expand Down
39 changes: 39 additions & 0 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,45 @@ func TestPageWaitLoadErr(t *testing.T) {
})
}

func TestPageWaitLoadCircularReference(t *testing.T) {
g := setup(t)

// We need to simulate a page taking a nontrivial amount of time to load in
// order to exercise the "load" event handler of js.WaitLoad. This temporary
// HTTP server lets us simulate a slow external resource which delays the
// page load.
serveMux := http.NewServeMux()
serveMux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
time.Sleep(100 * time.Millisecond)
_, err := fmt.Fprintf(w, "Hello, World!")
g.Err(err)
})
server := http.Server{
Addr: ":8080",
Handler: serveMux,
}
defer func() { g.Err(server.Close()) }()

go func() {
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}()

// Bootstrap.bundle.min.js sets up a "load" event listener and mutates the
// event by adding the "window" object as the delegateTarget property. If
// we're not careful, we might end up reading that event object from the
// browser as the result of the js.WaitLoad call. If the window object has a
// circular reference, this would cause an "Object reference chain is too
// long" error. Because this crash relies on event handlers firing in a
// specific order we need to try several times in order to have a good shot
// at reproducing it.
file := g.srcFile("fixtures/wait-load-circular-reference.html")
g.page.MustNavigate(file).MustWaitLoad()
g.page.MustNavigate(file).MustWaitLoad()
g.page.MustNavigate(file).MustWaitLoad()
}

func TestPageNavigation(t *testing.T) {
g := setup(t)

Expand Down