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 #26014

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Changes from 1 commit
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,24 @@ 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;
Copy link
Contributor

@stoletheminerals stoletheminerals Oct 16, 2024

Choose a reason for hiding this comment

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

A few concerns I have.

  1. Does it wait 5 seconds before trying to postMessage? Isn't it a lot for some use cases?
  2. Also a web page can race during this time out and modify window.webkit once it is restored (or before it is restored, I'm not sure what happens in this case)

Copy link
Contributor Author

@Brandon-T Brandon-T Oct 16, 2024

Choose a reason for hiding this comment

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

  1. Fixed.
  2. Fixed.

I tested with:

let interval = setInterval(function() {
  console.log("Replace window.webkit with hacked version");
}, 0);

setTimeout(function() {
   let startTime = Date.now();
    while(true) {
        console.log("Polling for window.webkit legit version");
        if (Date.now() - startTime >= 5000) {
          clearInterval(interval);
          break;
        }
    }
}, 0);

To see if the loop will block the setInterval. So even if a page tries to replace the window.webkit inside of setInterval or something similar, the while loop is synchronous and it blocks ALL other operations (async included).

Only once the while loop completes, everything else executes :) so I changed the code to use a while loop with a timeout. I checked performance and there's no issue as webkit replaces it internally asynchronously, in the next execution frame, while the loop blocks the page from executing anything else.

let startTime = Date.now();
var timerId = setTimeout(() => {
if (window.webkit.messageHandlers && window.webkit.messageHandlers[messageHandlerName]) {
let result = window.webkit.messageHandlers[messageHandlerName].postMessage(message);
window.webkit = oldWebkit;
clearTimeout(timerId);
result.then(resolve).catch(reject);
} else if (Date.now() - startTime >= timeout) {
reject(new TypeError("undefined is not an object (evaluating 'webkit.messageHandlers')"));
}
}, timeout);
});
};

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