-
Notifications
You must be signed in to change notification settings - Fork 9k
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(page.evaluate): better function serialization #3480
Changes from 1 commit
53d5fb7
4408c18
3ed0d07
558841a
f58dc68
f0ea50c
81345f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -94,8 +94,23 @@ class ExecutionContext { | |||||
if (typeof pageFunction !== 'function') | ||||||
throw new Error('The following is not a function: ' + pageFunction); | ||||||
|
||||||
let functionText = pageFunction.toString(); | ||||||
try { | ||||||
eval(functionText); | ||||||
} catch (e1) { | ||||||
// This means we have a function shorthand. Try another | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// time prefixing 'function '. | ||||||
functionText = 'function ' + functionText; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about async static generators? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. async done; the rest is too exotic for now! |
||||||
try { | ||||||
eval(functionText); | ||||||
} catch (e2) { | ||||||
// We tried hard to serialize, but there's a weird beast here. | ||||||
throw new Error('Passed function is not well-serializable!'); | ||||||
} | ||||||
} | ||||||
|
||||||
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.callFunctionOn', { | ||||||
functionDeclaration: pageFunction.toString() + '\n' + suffix + '\n', | ||||||
functionDeclaration: functionText + '\n' + suffix + '\n', | ||||||
executionContextId: this._contextId, | ||||||
arguments: args.map(convertArgument.bind(this)), | ||||||
returnByValue: false, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be worried about unintended side effects here. Consider:
function functionText(){return 5}
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch; switched to
new Function
instead