Safari Web Extensions: Unexpected tab ID change when redirecting to extesnion URL in a tab

Hello!

We have been testing the upcoming Safari 18 on macOS 15 Sequoia betas and noticed one inconsistent detail about Safari Web Extensions support compared to other browser which implement Web Extensions (Chrome, Edge, Firefox).

Background

We have a Safari Web extension which is monitoring navigation events using browser.tabs.onUpdated API.

navigation event subscription code sample

browser.tabs.onUpdated.addListener((tabId, changeInfo, details) => {
   onTabUpdated(tabId, changeInfo, details)
});

navigation event handling code sample

onTabUpdated(tabId, changeInfo, details) {
        console.log(`onTabUpdated: ${tabId}`, changeInfo, details);
        // check URL in the tab for safety
    }
});

If the extension detects that the user navigates to an unsafe URL, it redirects the user to a page hosted by the extension. It's an HTML resource from the extension bundle. The extension is using browser.tabs.update API to redirect a specific tab to an internal page.

const internalPage = browser.runtime.getURL("popup.html");
browser.tabs.update(tabId, { url: internalPage });

Discovered problem

When we use browser.tabs.update API

browser.tabs.update(tabId, { url: internalPage });

to redirect the user from an unsafe page, we notice that the redirected tab changes its identifier. We know that is the case because we see another API firing. It's called browser.tabs.onReplaced. We have a similar subscription for those events.

When the page is redirected, the onTabReplaced handler is firing and informs us about the tab ID change after the redirect.

onTabReplaced(addedTabId, removedTabId) {
        console.log(`onTabReplaced: ${removedTabId} -> ${addedTabId}`);
    }

This is problematic for us in several ways:

  1. The extension keeps track of the tab ID so that when the embedded HTML page is loaded, it can still tell the user about the original URL that was blocked. The behavior observed in Safari 18 breaks current expectations of our code and breaks the functionality of our extension.
  2. This behavior is specific to Safari 18. Safari 17 does not behave this way which means that we will need to deploy an update to our Safari extension to mitigate that bug on the upcoming Safari version.
  3. Moreover, this behavior is not observed in other browsers which implement Web Extensions standard (Chrome, Edge, Firefox). All these browsers preserve the tab ID after redirect. That is a problem for us as we run the same code in all 4 browsers that we support. This will cause increase of code complexity to cover Safari as an exception out of common rule.

Environment

Safari version 18.0 (20619.1.26.31.6) and all prior Safari 18 betas.

  • issue does not happen on Safari 17.

macOS 15 beta 8 (24A5331b) and all prior macOS 15 betas.

  • issue has been successfully reproduced on macOS 14 with Safari 18 betas which points to the fact that the issue is not exclusive to macOS 15. Safari 18 brings the faulty logic.

The issue has been confirmed and reproduced in a sample Xcode prowejt provided by Apple called "Sea Creator". So the issue is not specific to a single extension.

Feedback case

FB14975378. It contains sample code, the full Xcode project, screenshots and sysdiagnose.

Any advice or assistance is highly appreciated!

Answered by Frameworks Engineer in 802635022

You’re correct that tabs.onReplaced is fired in Safari 18 during actions like redirecting to an internal extension page or preloading a Top Hit. This event can also occur in Chrome under certain conditions. It’s important to handle this event and update any internal tab ID state mappings in your extension to ensure consistent behavior across all types of tab navigations.

You’re correct that tabs.onReplaced is fired in Safari 18 during actions like redirecting to an internal extension page or preloading a Top Hit. This event can also occur in Chrome under certain conditions. It’s important to handle this event and update any internal tab ID state mappings in your extension to ensure consistent behavior across all types of tab navigations.

__`
> * - [ ] Unexpected tab ID change when redirecting to extesnion `__
Safari Web Extensions: Unexpected tab ID change when redirecting to extesnion URL in a tab
 
 
Q