forked from MeiK2333/pyppeteer_stealth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite navigator.permissions evasion
- Loading branch information
1 parent
c4fc746
commit 4948cb5
Showing
1 changed file
with
37 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,43 @@ | ||
// https://github.com/berstend/puppeteer-extra/blob/c44c8bb0224c6bba2554017bfb9d7a1d0119f92f/packages/puppeteer-extra-plugin-stealth/evasions/navigator.permissions/index.js | ||
|
||
() => { | ||
const handler = { | ||
apply: function (target, ctx, args) { | ||
const param = (args || [])[0] | ||
const isSecure = document.location.protocol.startsWith("https"); | ||
|
||
if (param && param.name && param.name === 'notifications') { | ||
const result = { state: Notification.permission } | ||
Object.setPrototypeOf(result, PermissionStatus.prototype) | ||
return Promise.resolve(result) | ||
} | ||
|
||
return utils.cache.Reflect.apply(...arguments) | ||
} | ||
// In headful on secure origins the permission should be "default", not "denied" | ||
if (isSecure) { | ||
utils.replaceGetterWithProxy(Notification, "permission", { | ||
apply() { | ||
return "default"; | ||
}, | ||
}); | ||
} | ||
|
||
utils.replaceWithProxy( | ||
window.navigator.permissions.__proto__, // eslint-disable-line no-proto | ||
'query', | ||
handler | ||
) | ||
} | ||
// Another weird behavior: | ||
// On insecure origins in headful the state is "denied", | ||
// whereas in headless it's "prompt" | ||
if (!isSecure) { | ||
const handler = { | ||
apply(target, ctx, args) { | ||
const param = (args || [])[0]; | ||
|
||
const isNotifications = | ||
param && param.name && param.name === "notifications"; | ||
if (!isNotifications) { | ||
return utils.cache.Reflect.apply(...arguments); | ||
} | ||
|
||
return Promise.resolve( | ||
Object.setPrototypeOf( | ||
{ | ||
state: "denied", | ||
onchange: null, | ||
}, | ||
PermissionStatus.prototype | ||
) | ||
); | ||
}, | ||
}; | ||
// Note: Don't use `Object.getPrototypeOf` here | ||
utils.replaceWithProxy(Permissions.prototype, "query", handler); | ||
} | ||
} |