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

chore: replace __proto__ by getPrototypeOf #17386

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module.exports = {
"valid-typeof": 2,
"no-implicit-globals": [2],
"no-unused-expressions": [2, { "allowShortCircuit": true, "allowTernary": true, "allowTaggedTemplates": true}],
"no-proto": 2,

// es2015 features
"require-yield": 2,
Expand Down
3 changes: 2 additions & 1 deletion packages/playwright-core/src/protocol/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,6 @@ function isURL(obj: any): obj is URL {
}

function isError(obj: any): obj is Error {
return obj instanceof Error || obj?.__proto__?.name === 'Error' || (obj?.__proto__ && isError(obj.__proto__));
const proto = obj ? Object.getPrototypeOf(obj) : null;
Copy link
Member

Choose a reason for hiding this comment

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

This will fail if there is a user code like Object.getPrototypeOf = () => {};

Copy link
Member

Choose a reason for hiding this comment

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

Oh, this is a server side code, we don't run random js here so it should be ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, and obj.__proto__ was an order of magnitude less safe than Object.getPrototypeOf :)

return obj instanceof Error || proto?.name === 'Error' || (proto && isError(proto));
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function source() {

function isError(obj: any): obj is Error {
try {
return obj instanceof Error || (obj && obj.__proto__ && obj.__proto__.name === 'Error');
return obj instanceof Error || (obj && Object.getPrototypeOf(obj)?.name === 'Error');
} catch (error) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function isObject(obj: any): obj is NonNullable<object> {
}

export function isError(obj: any): obj is Error {
return obj instanceof Error || (obj && obj.__proto__ && obj.__proto__.name === 'Error');
return obj instanceof Error || (obj && Object.getPrototypeOf(obj)?.name === 'Error');
}

const debugEnv = getFromENV('PWDEBUG') || '';
Expand Down