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

Mitigate risk of panic on parse of remote object during console.log #1129

Merged
merged 14 commits into from
Dec 15, 2023

Conversation

ankur22
Copy link
Collaborator

@ankur22 ankur22 commented Dec 11, 2023

What?

This change reduces the risk of a panic occurring when the website under test uses console.log to print logs.

Why?

Currently there is an unknown case where a panic can occur when an object from console.log is unmarshaled and fails with an invalid JSON error. So this isn't fixing the issue but mitigating the risk. This is done by not needing to parse the console.log text at all. The reason behind this choice is that when a user prints something to the console, they don't tend to need the object to be parsed.

Other areas of the module still rely on the existing parse logic which does still attempt to unmarshal the object which is returned when working with APIs such as JSONValue and Eval.

Checklist

  • I have performed a self-review of my code
  • I have added tests for my changes
  • I have commented on my code, particularly in hard-to-understand areas

Related PR(s)/Issue(s)

Updates: #987

@ankur22 ankur22 force-pushed the fix/panic-parse-remote-obj branch 5 times, most recently from 0073ba8 to 2b03672 Compare December 11, 2023 15:30
@ankur22 ankur22 changed the title Fix panic on parse of remote object Mitigate risk of panic on parse of remote object during console.log Dec 11, 2023
tests/console_log_test.go Outdated Show resolved Hide resolved
These new functions are to be used when parsing the console message
which is being printed by the website under test when it used
console.log. Since these are string values that will be parsed and
reprinted to stdout, we don't necessarily need to parse a json string
into a go object (which is the main motivation for this change). This
will avoid situations where k6 browser panics and causes the test run
to end due to not being able to parse a json string.

Other areas of the implementation (e.g. toJSON or eval) still require
a go representation of the object and they will work with the existing
parse functions.
Since a string is already a string, we don't need to marshal it back to
a string. This will be used when working with console messages which
are printed by the website under test.
Work with parseConsoleRemoteObject so that the browser module avoids
trying to unmarshal objects from json to a go object. This doesn't feel
like a worthwhile operation since the string will be printed to stdout.
Work with parseConsoleRemoteObject and strings rather than any types.
The page.on API works with strings so there's no need to try to parse
a json object to a go object and then back to a js object.
tests/console_log_test.go Outdated Show resolved Hide resolved
inancgumus
inancgumus previously approved these changes Dec 13, 2023
Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

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

👍 Nice work. Just some suggestions.

common/frame_session.go Outdated Show resolved Hide resolved
common/page.go Outdated Show resolved Hide resolved
common/remote_object.go Outdated Show resolved Hide resolved
common/remote_object.go Outdated Show resolved Hide resolved
common/remote_object.go Show resolved Hide resolved
tests/remote_obj_test.go Outdated Show resolved Hide resolved
common/remote_object.go Outdated Show resolved Hide resolved
common/frame_session.go Show resolved Hide resolved
ankur22 and others added 3 commits December 13, 2023 10:56
Co-authored-by: İnanç Gümüş <inanc.gumus@grafana.com>
This log is notifying the user of something that can't be changed or
actioned on, so it's better represented as info rather than a warn.
log/logger.go Show resolved Hide resolved
This is just to clarify why these empty cases are there.
ka3de
ka3de previously approved these changes Dec 14, 2023
Copy link
Collaborator

@ka3de ka3de left a comment

Choose a reason for hiding this comment

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

LGTM.

It's no longer being used.
@ankur22 ankur22 force-pushed the fix/panic-parse-remote-obj branch 3 times, most recently from df89ca4 to dbb0981 Compare December 14, 2023 11:32
This particular test may run slower on other machines, so accounting
for that scenario -- specifically in the CI.
Copy link
Collaborator

@ka3de ka3de left a comment

Choose a reason for hiding this comment

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

LGTM.

@ankur22 ankur22 merged commit 373aca8 into main Dec 15, 2023
17 checks passed
@ankur22 ankur22 deleted the fix/panic-parse-remote-obj branch December 15, 2023 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants