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

[iOS] - Fix securing message handlers on iOS-18 (uplift to 1.71.x) #26066

Merged
merged 1 commit into from
Oct 18, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,9 @@ if (!window.__firefox__) {
let $Function = secureCopy(Function);
let $Reflect = secureCopy(Reflect);
let $Array = secureCopy(Array);
let $webkit = window.webkit;
let $MessageHandlers = $webkit.messageHandlers;

secureCopy = undefined;
let secureObjects = [$Object, $Function, $Reflect, $Array, $MessageHandlers];
let secureObjects = [$Object, $Function, $Reflect, $Array];

/*
* Prevent recursive calls if a page overrides these.
Expand Down Expand Up @@ -412,14 +410,27 @@ if (!window.__firefox__) {
return Promise.reject(new TypeError("undefined is not an object (evaluating 'webkit.messageHandlers')"));
}

let webkit = window.webkit;
delete window.webkit.messageHandlers[messageHandlerName].postMessage;
delete window.webkit.messageHandlers[messageHandlerName];
delete window.webkit.messageHandlers;
delete window.webkit;
let result = $MessageHandlers[messageHandlerName].postMessage(message);
window.webkit = webkit;
return result;
return new Promise((resolve, reject) => {
var oldWebkit = window.webkit;
delete window['webkit'];

// WebKit no longer restores the handler immediately! So we poll for when that happens and resolve the promise accordingly.
const timeout = 5000;
let startTime = Date.now();

// While loop blocks synchronously. SetTimeout or SetInterval can cause a race condition.
while(true) {
if (window.webkit.messageHandlers && window.webkit.messageHandlers[messageHandlerName]) {
let result = window.webkit.messageHandlers[messageHandlerName].postMessage(message);
window.webkit = oldWebkit;
result.then(resolve).catch(reject);
break;
} else if (Date.now() - startTime >= timeout) {
reject(new TypeError("undefined is not an object (evaluating 'webkit.messageHandlers')"));
break;
}
}
});
};

$.dispatchEvent = function(event) {
Expand Down
Loading