tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 38235ce193f32279a729e4e60048104d0d5b4ebe
parent fd8a661551ed398326127643e54275adb89710fe
Author: Dão Gottwald <dao@mozilla.com>
Date:   Tue,  4 Nov 2025 08:49:23 +0000

Bug 1990522 - Restrict tokens should not be available in the search bar re-implementation. r=mbeier

Differential Revision: https://phabricator.services.mozilla.com/D266142

Diffstat:
Mbrowser/components/urlbar/UrlbarInput.sys.mjs | 15++++++++++++---
Mbrowser/components/urlbar/UrlbarProviderHeuristicFallback.sys.mjs | 2+-
Mbrowser/components/urlbar/UrlbarProvidersManager.sys.mjs | 31+++++++++++++++----------------
Mbrowser/components/urlbar/UrlbarUtils.sys.mjs | 12++++++++++++
Mbrowser/components/urlbar/tests/unit/head.js | 1+
Mbrowser/components/urlbar/tests/unit/test_UrlbarQueryContext.js | 22++++++++++++++++++++--
6 files changed, 61 insertions(+), 22 deletions(-)

diff --git a/browser/components/urlbar/UrlbarInput.sys.mjs b/browser/components/urlbar/UrlbarInput.sys.mjs @@ -326,6 +326,14 @@ export class UrlbarInput { } /** + * The search access point name of the UrlbarInput for use with telemetry or + * logging, e.g. `urlbar`, `searchbar`. + */ + get sapName() { + return this.#sapName; + } + + /** * @type {typeof HTMLDivElement.prototype.getAttribute} */ getAttribute; @@ -1990,9 +1998,9 @@ export class UrlbarInput { }; } - let mode = lazy.UrlbarUtils.LOCAL_SEARCH_MODES.find( - m => m.restrict == token - ); + let mode = + this.isAddressbar && + lazy.UrlbarUtils.LOCAL_SEARCH_MODES.find(m => m.restrict == token); if (mode) { // Return a copy so callers don't modify the object in LOCAL_SEARCH_MODES. return { ...mode }; @@ -4934,6 +4942,7 @@ export class UrlbarInput { let options = { allowAutofill, isPrivate: this.isPrivate, + sapName: this.sapName, maxResults, searchString, userContextId: parseInt( diff --git a/browser/components/urlbar/UrlbarProviderHeuristicFallback.sys.mjs b/browser/components/urlbar/UrlbarProviderHeuristicFallback.sys.mjs @@ -231,7 +231,7 @@ export class UrlbarProviderHeuristicFallback extends UrlbarProvider { } async _searchModeKeywordResult(queryContext) { - if (!queryContext.tokens.length) { + if (!queryContext.tokens.length || queryContext.sapName != "urlbar") { return null; } diff --git a/browser/components/urlbar/UrlbarProvidersManager.sys.mjs b/browser/components/urlbar/UrlbarProvidersManager.sys.mjs @@ -992,18 +992,21 @@ function updateSourcesIfEmpty(context) { } let acceptedSources = []; // There can be only one restrict token per query. - let restrictToken = context.tokens.find(t => - [ - lazy.UrlbarTokenizer.TYPE.RESTRICT_HISTORY, - lazy.UrlbarTokenizer.TYPE.RESTRICT_BOOKMARK, - lazy.UrlbarTokenizer.TYPE.RESTRICT_TAG, - lazy.UrlbarTokenizer.TYPE.RESTRICT_OPENPAGE, - lazy.UrlbarTokenizer.TYPE.RESTRICT_SEARCH, - lazy.UrlbarTokenizer.TYPE.RESTRICT_TITLE, - lazy.UrlbarTokenizer.TYPE.RESTRICT_URL, - lazy.UrlbarTokenizer.TYPE.RESTRICT_ACTION, - ].includes(t.type) - ); + let restrictToken = + context.sapName != "urlbar" + ? undefined + : context.tokens.find(t => + [ + lazy.UrlbarTokenizer.TYPE.RESTRICT_HISTORY, + lazy.UrlbarTokenizer.TYPE.RESTRICT_BOOKMARK, + lazy.UrlbarTokenizer.TYPE.RESTRICT_TAG, + lazy.UrlbarTokenizer.TYPE.RESTRICT_OPENPAGE, + lazy.UrlbarTokenizer.TYPE.RESTRICT_SEARCH, + lazy.UrlbarTokenizer.TYPE.RESTRICT_TITLE, + lazy.UrlbarTokenizer.TYPE.RESTRICT_URL, + lazy.UrlbarTokenizer.TYPE.RESTRICT_ACTION, + ].includes(t.type) + ); // RESTRICT_TITLE and RESTRICT_URL do not affect query sources. let restrictTokenType = @@ -1014,10 +1017,6 @@ function updateSourcesIfEmpty(context) { : undefined; for (let source of Object.values(lazy.UrlbarUtils.RESULT_SOURCE)) { - // Skip sources that the context doesn't care about. - if (context.sources && !context.sources.includes(source)) { - continue; - } // Check prefs and restriction tokens. switch (source) { case lazy.UrlbarUtils.RESULT_SOURCE.BOOKMARKS: diff --git a/browser/components/urlbar/UrlbarUtils.sys.mjs b/browser/components/urlbar/UrlbarUtils.sys.mjs @@ -1059,6 +1059,7 @@ export var UrlbarUtils = { let options = { allowAutofill: false, isPrivate: urlbarInput.isPrivate, + sapName: urlbarInput.sapName, maxResults: 1, searchString, userContextId: parseInt( @@ -2330,6 +2331,9 @@ export class UrlbarQueryContext { * * @param {object} options * The initial options for UrlbarQueryContext. + * @param {string} options.sapName + * The search access point name of the UrlbarInput for use with telemetry or + * logging, e.g. `urlbar`, `searchbar`. * @param {string} options.searchString * The string the user entered in autocomplete. Could be the empty string * in the case of the user opening the popup via the mouse. @@ -2361,6 +2365,7 @@ export class UrlbarQueryContext { "allowAutofill", "isPrivate", "maxResults", + "sapName", "searchString", ]); @@ -2500,6 +2505,13 @@ export class UrlbarQueryContext { results; /** + * @type {string} + * The search access point name of the UrlbarInput for use with telemetry or + * logging, e.g. `urlbar`, `searchbar`. + */ + sapName; + + /** * @type {UrlbarSearchModeData} * Details about the search mode associated with this context. */ diff --git a/browser/components/urlbar/tests/unit/head.js b/browser/components/urlbar/tests/unit/head.js @@ -129,6 +129,7 @@ function createContext(searchString = "foo", properties = {}) { allowAutofill: UrlbarPrefs.get("autoFill"), isPrivate: true, maxResults: UrlbarPrefs.get("maxRichResults"), + sapName: "urlbar", searchString, }, properties diff --git a/browser/components/urlbar/tests/unit/test_UrlbarQueryContext.js b/browser/components/urlbar/tests/unit/test_UrlbarQueryContext.js @@ -43,11 +43,24 @@ add_task(function test_constructor() { "Should throw with a missing allowAutofill parameter" ); + Assert.throws( + () => + new UrlbarQueryContext({ + allowAutofill: true, + isPrivate: false, + maxResults: 1, + searchString: "foo", + }), + /Missing or empty sapName provided to UrlbarQueryContext/, + "Should throw with a missing sapName parameter" + ); + let qc = new UrlbarQueryContext({ allowAutofill: false, isPrivate: true, maxResults: 1, - searchString: "foo", + sapName: "foo", + searchString: "bar", }); Assert.strictEqual( @@ -66,8 +79,13 @@ add_task(function test_constructor() { "Should have saved the correct value for maxResults" ); Assert.equal( - qc.searchString, + qc.sapName, "foo", "Should have saved the correct value for searchString" ); + Assert.equal( + qc.searchString, + "bar", + "Should have saved the correct value for searchString" + ); });