Why does additionalRequestHeaders in my SFSafariExtensionHandler subclass never get called?

I have an app extension and would like to add additional headers to certain requests and it seems like overriding the "additionalRequestHeaders" function would be the way to do that, except it never seems to get called.


The online docs have no details about this function, but inside Xcode it says:


/// This is called when Safari is about to load a page that the extension has stated it wants to add additional headers for.


My extension is set to "All" for the Safari Website Access key in my extensions plist. I can see other things happening such as my Javascript being injected and messaging seems to work, so I'm confident my extensions is working, but I am not sure what I need to do to get this function to be called.


Thanks in advance.

You will need to add a new entry to your SFSafariWebsiteAccess dictionary. The key name is:


"Allowed Domains for Header Injection" (without quotes), and then the value is an array of domains that you want your extension to be able to add headers to.


Currently we do not support a value of "All" for header injection - if this is something your extension needs - please file a radar with details about what your extension is trying to do.

Hello @bweinstein,


As there is zero documentation for this (why...?) can you enlighten me as to how it works?
Is this synchronous? i.e. If I put a sleep in the method will the browser hang forever?
Is it guaranteed to be called for every request and before it goes out...?
Thanks in advance

Can we get more clarification here? The documentation is non-existent.

It also seems that this API is not even listed in any of the safari release notes: https://developer.apple.com/documentation/safari_release_notes. Which version did this ship with?

This only works for the selected domain and only on the https protocol.

Info.plist

		<key>SFSafariWebsiteAccess</key>
		<dict>
			<key>Level</key>
			<string>All</string>
			<key>Allowed Domains for Header Injection</key>
			<array>
				<string>js-blocker.com</string>
			</array>
		</dict>

SafariExtensionHandler.swift

class SafariExtensionHandler: SFSafariExtensionHandler {

    override func additionalRequestHeaders(for url: URL, completionHandler: @escaping ([String : String]?) -> Void) {
        print("FIRE: additionalRequestHeaders")
    }

}
Why does additionalRequestHeaders in my SFSafariExtensionHandler subclass never get called?
 
 
Q