commit 6e0856d0cf2bb8f3f9a82376f0728947bba3527c
parent c8b9c7dbb669ef40ad6c7edb1564958c90a0e4be
Author: Pier Angelo Vendrame <pierov@torproject.org>
Date: Mon, 13 Oct 2025 15:18:00 +0000
Bug 1993166 - Improve origin attributes on opensearch. r=Standard8,urlbar-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D268183
Diffstat:
5 files changed, 68 insertions(+), 21 deletions(-)
diff --git a/browser/components/urlbar/ActionsProviderContextualSearch.sys.mjs b/browser/components/urlbar/ActionsProviderContextualSearch.sys.mjs
@@ -282,10 +282,22 @@ class ProviderContextualSearch extends ActionsProvider {
let { type, engine } = this.#resultEngine;
if (type == OPEN_SEARCH_ENGINE) {
+ let originAttributes;
+ try {
+ let currentURI = Services.io.newURI(queryContext.currentPage);
+ originAttributes = {
+ firstPartyDomain: Services.eTLD.getSchemelessSite(currentURI),
+ };
+ } catch {}
let openSearchEngineData = await lazy.loadAndParseOpenSearchEngine(
- Services.io.newURI(engine.uri)
+ Services.io.newURI(engine.uri),
+ null,
+ originAttributes
);
- engine = new lazy.OpenSearchEngine({ engineData: openSearchEngineData });
+ engine = new lazy.OpenSearchEngine({
+ engineData: openSearchEngineData,
+ originAttributes,
+ });
}
this.#performSearch(
diff --git a/toolkit/components/search/OpenSearchEngine.sys.mjs b/toolkit/components/search/OpenSearchEngine.sys.mjs
@@ -53,6 +53,8 @@ export class OpenSearchEngine extends SearchEngine {
* @param {string} [options.faviconURL]
* The website favicon, to be used if the engine data hasn't specified an
* icon.
+ * @param {object} [options.originAttributes]
+ * The origin attributes to use to download additional resources.
*/
constructor(options = {}) {
super({
@@ -65,7 +67,10 @@ export class OpenSearchEngine extends SearchEngine {
});
if (options.faviconURL) {
- this._setIcon(options.faviconURL, undefined, false).catch(e =>
+ this._setIcon(options.faviconURL, {
+ override: false,
+ originAttributes: options.originAttributes,
+ }).catch(e =>
lazy.logConsole.error(
`Error while setting icon for search engine ${options.engineData.name}:`,
e.message
@@ -74,7 +79,7 @@ export class OpenSearchEngine extends SearchEngine {
}
if (options.engineData) {
- this.#setEngineData(options.engineData);
+ this.#setEngineData(options.engineData, options.originAttributes);
// As this is a new engine, we must set the verification hash for the load
// path set in the constructor.
@@ -186,8 +191,10 @@ export class OpenSearchEngine extends SearchEngine {
*
* @param {OpenSearchProperties} data
* The OpenSearch data.
+ * @param {object} originAttributes
+ * The origin attributes for any additional downloads
*/
- #setEngineData(data) {
+ #setEngineData(data, originAttributes) {
let name = data.name.trim();
if (Services.search.getEngineByName(name)) {
throw Components.Exception(
@@ -253,11 +260,12 @@ export class OpenSearchEngine extends SearchEngine {
}
for (let image of data.images) {
- this._setIcon(image.url, image.size).catch(e =>
- lazy.logConsole.error(
- `Error while setting icon for search engine ${data.name}:`,
- e.message
- )
+ this._setIcon(image.url, { size: image.size, originAttributes }).catch(
+ e =>
+ lazy.logConsole.error(
+ `Error while setting icon for search engine ${data.name}:`,
+ e.message
+ )
);
}
}
diff --git a/toolkit/components/search/SearchEngine.sys.mjs b/toolkit/components/search/SearchEngine.sys.mjs
@@ -668,15 +668,19 @@ export class SearchEngine {
* @param {string} iconURL
* A URI string pointing to the engine's icon.
* Must have http[s], data, or moz-extension protocol.
- * @param {number} [size]
+ * @param {object} options
+ * The options object
+ * @param {number} [options.size]
* Width and height of the icon (determined automatically if not provided).
- * @param {boolean} [override]
+ * @param {boolean} [options.override]
* Whether the new URI should override an existing one.
+ * @param {object} [options.originAttributes]
+ * The origin attributes to use to load the icon.
* @returns {Promise<void>}
* Resolves when the icon was set.
* Rejects with an Error if there was an error.
*/
- async _setIcon(iconURL, size, override = true) {
+ async _setIcon(iconURL, options = { override: true }) {
lazy.logConsole.debug(
"_setIcon: Setting icon url for",
this.name,
@@ -684,8 +688,12 @@ export class SearchEngine {
limitURILength(iconURL)
);
- [iconURL, size] = await this._downloadAndRescaleIcon(iconURL, size);
- this._addIconToMap(iconURL, size, override);
+ let size;
+ [iconURL, size] = await this._downloadAndRescaleIcon(iconURL, {
+ size: options.size,
+ originAttributes: options.originAttributes,
+ });
+ this._addIconToMap(iconURL, size, options.override);
if (this._engineAddedToStore) {
lazy.SearchUtils.notifyAction(
@@ -703,18 +711,24 @@ export class SearchEngine {
* @param {string} iconURL
* A URI string pointing to the engine's icon.
* Must have http[s], data, or moz-extension protocol.
- * @param {number} [size]
+ * @param {object} options
+ * The options object
+ * @param {number} [options.size]
* Width and height of the icon (determined automatically if not provided).
+ * @param {object} [options.originAttributes]
+ * The origin attributes to use to load the icon.
* @returns {Promise<[string, number]>}
* Resolves to [dataURL, size] if successful and rejects if there was an error.
*/
- async _downloadAndRescaleIcon(iconURL, size) {
+ async _downloadAndRescaleIcon(iconURL, options = {}) {
let uri = lazy.SearchUtils.makeURI(iconURL);
if (!uri) {
throw new Error(`Invalid URI`);
}
+ let size = options.size;
+
switch (uri.scheme) {
case "moz-extension": {
if (!size) {
@@ -727,7 +741,10 @@ export class SearchEngine {
case "data":
case "http":
case "https": {
- let [byteArray, contentType] = await lazy.SearchUtils.fetchIcon(uri);
+ let [byteArray, contentType] = await lazy.SearchUtils.fetchIcon(
+ uri,
+ options.originAttributes
+ );
if (byteArray.length > lazy.SearchUtils.MAX_ICON_SIZE) {
lazy.logConsole.debug(
`Rescaling icon for search engine ${this.name}.`
diff --git a/toolkit/components/search/SearchService.sys.mjs b/toolkit/components/search/SearchService.sys.mjs
@@ -761,7 +761,11 @@ export class SearchService {
null,
originAttributes
);
- engine = new lazy.OpenSearchEngine({ engineData, faviconURL: iconURL });
+ engine = new lazy.OpenSearchEngine({
+ engineData,
+ faviconURL: iconURL,
+ originAttributes,
+ });
} catch (ex) {
throw Components.Exception(
"addEngine: Error adding engine:\n" + ex,
diff --git a/toolkit/components/search/SearchUtils.sys.mjs b/toolkit/components/search/SearchUtils.sys.mjs
@@ -511,13 +511,19 @@ export var SearchUtils = {
*
* @param {string|nsIURI} uri
* The URI to the icon.
+ * @param {object} [originAttributes]
+ * The origin attributes to download the icon.
* @returns {Promise<[Uint8Array, string]>}
* Resolves to an array containing the data and the mime type.
* Rejects if the icon cannot be fetched.
*/
- async fetchIcon(uri) {
+ async fetchIcon(uri, originAttributes = null) {
return new Promise((resolve, reject) => {
- let chan = SearchUtils.makeChannel(uri, Ci.nsIContentPolicy.TYPE_IMAGE);
+ let chan = SearchUtils.makeChannel(
+ uri,
+ Ci.nsIContentPolicy.TYPE_IMAGE,
+ originAttributes
+ );
let listener = new SearchUtils.LoadListener(
chan,
/^image\//,