Bug 1088050 - Add a pref to disable search for single-word hosts. r=smaug

This commit is contained in:
Gijs Kruitbosch
2014-11-16 23:01:59 +00:00
parent b5b90e3e38
commit c02e4c18b7
3 changed files with 81 additions and 17 deletions

View File

@@ -34,6 +34,7 @@ NS_IMPL_ISUPPORTS(nsDefaultURIFixup, nsIURIFixup)
static bool sInitializedPrefCaches = false;
static bool sFixTypos = true;
static bool sDNSFirstForSingleWords = false;
static bool sFixupKeywords = true;
nsDefaultURIFixup::nsDefaultURIFixup()
@@ -244,6 +245,11 @@ nsDefaultURIFixup::GetFixupURIInfo(const nsACString& aStringURI, uint32_t aFixup
MOZ_ASSERT(NS_SUCCEEDED(rv),
"Failed to observe \"browser.fixup.typo.scheme\"");
rv = Preferences::AddBoolVarCache(&sDNSFirstForSingleWords,
"browser.fixup.dns_first_for_single_words",
sDNSFirstForSingleWords);
MOZ_ASSERT(NS_SUCCEEDED(rv), "Failed to observe \"browser.fixup.dns_first_for_single_words\"");
rv = Preferences::AddBoolVarCache(&sFixupKeywords, "keyword.enabled",
sFixupKeywords);
MOZ_ASSERT(NS_SUCCEEDED(rv), "Failed to observe \"keyword.enabled\"");
@@ -1067,16 +1073,18 @@ nsDefaultURIFixup::KeywordURIFixup(const nsACString & aURIString,
nsresult rv = NS_OK;
// We do keyword lookups if a space or quote preceded the dot, colon
// or question mark (or if the latter were not found)
// or when the host is the same as asciiHost and there are no
// characters from [a-z][A-Z]
// or question mark (or if the latter is not found, or if the input starts with a question mark)
if (((firstSpaceLoc < firstDotLoc || firstQuoteLoc < firstDotLoc) &&
(firstSpaceLoc < firstColonLoc || firstQuoteLoc < firstColonLoc) &&
(firstSpaceLoc < firstQMarkLoc || firstQuoteLoc < firstQMarkLoc)) || firstQMarkLoc == 0 ||
(isValidAsciiHost && isValidHost && !hasAsciiAlpha &&
host.EqualsIgnoreCase(asciiHost.get()))) {
(firstSpaceLoc < firstQMarkLoc || firstQuoteLoc < firstQMarkLoc)) || firstQMarkLoc == 0) {
rv = TryKeywordFixupForURIInfo(aFixupInfo->mOriginalInput, aFixupInfo, aPostData);
// ... or when the host is the same as asciiHost and there are no
// characters from [a-z][A-Z]
} else if (isValidAsciiHost && isValidHost && !hasAsciiAlpha &&
host.EqualsIgnoreCase(asciiHost.get())) {
if (!sDNSFirstForSingleWords) {
rv = TryKeywordFixupForURIInfo(aFixupInfo->mOriginalInput, aFixupInfo, aPostData);
}
}
// ... or if there is no question mark or colon, and there is either no
// dot, or exactly 1 and it is the first or last character of the input:
@@ -1098,6 +1106,9 @@ nsDefaultURIFixup::KeywordURIFixup(const nsACString & aURIString,
bool nsDefaultURIFixup::IsDomainWhitelisted(const nsAutoCString aAsciiHost,
const uint32_t aDotLoc)
{
if (sDNSFirstForSingleWords) {
return true;
}
// Check if this domain is whitelisted as an actual
// domain (which will prevent a keyword query)
// NB: any processing of the host here should stay in sync with

View File

@@ -20,6 +20,7 @@ Services.search.defaultEngine = Services.search.getEngineByName(kSearchEngineID)
let selectedName = Services.search.defaultEngine.name;
do_check_eq(selectedName, kSearchEngineID);
const kForceHostLookup = "browser.fixup.dns_first_for_single_words";
do_register_cleanup(function() {
if (oldDefaultEngine) {
Services.search.defaultEngine = oldDefaultEngine;
@@ -30,6 +31,7 @@ do_register_cleanup(function() {
}
Services.prefs.clearUserPref("keyword.enabled");
Services.prefs.clearUserPref("browser.fixup.typo.scheme");
Services.prefs.clearUserPref(kForceHostLookup);
});
let flagInputs = [
@@ -56,6 +58,7 @@ flagInputs.concat([
protocolChange: false, // Whether a protocol change is expected
affectedByWhitelist: false, // Whether the input host is affected by the whitelist
inWhitelist: false, // Whether the input host is in the whitelist
affectedByDNSForSingleHosts: false, // Whether the input host could be a host, but is normally assumed to be a keyword query
}
*/
let testcases = [ {
@@ -116,6 +119,7 @@ let testcases = [ {
fixedURI: "http://1.2.3/",
keywordLookup: true,
protocolChange: true,
affectedByDNSForSingleHosts: true,
}, {
input: "1.2.3/",
fixedURI: "http://1.2.3/",
@@ -129,6 +133,7 @@ let testcases = [ {
fixedURI: "http://1.2.3:8000/",
keywordLookup: true,
protocolChange: true,
affectedByDNSForSingleHosts: true,
}, {
input: "1.2.3:8000/",
fixedURI: "http://1.2.3:8000/",
@@ -198,7 +203,8 @@ let testcases = [ {
alternateURI: "http://[::1][100/",
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
}, {
input: "[::1]]",
keywordLookup: true,
@@ -210,6 +216,7 @@ let testcases = [ {
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
}, {
input: "host/foo.txt",
fixedURI: "http://host/foo.txt",
@@ -223,6 +230,7 @@ let testcases = [ {
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
}, {
input: "test.",
fixedURI: "http://test./",
@@ -230,12 +238,14 @@ let testcases = [ {
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
}, {
input: ".test",
fixedURI: "http://.test/",
alternateURI: "http://www..test/",
keywordLookup: true,
protocolChange: true,
affectedByDNSForSingleHosts: true,
}, {
input: "mozilla is amazing",
keywordLookup: true,
@@ -263,6 +273,7 @@ let testcases = [ {
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
}, {
input: " mozilla ",
fixedURI: "http://mozilla/",
@@ -270,6 +281,7 @@ let testcases = [ {
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
}, {
input: "mozilla \\",
keywordLookup: true,
@@ -290,6 +302,7 @@ let testcases = [ {
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
}, {
input: "mozilla \r\n",
fixedURI: "http://mozilla/",
@@ -297,6 +310,7 @@ let testcases = [ {
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
}, {
input: "moz\r\nfirefox\nos\r",
fixedURI: "http://mozfirefoxos/",
@@ -304,6 +318,7 @@ let testcases = [ {
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
}, {
input: "moz\r\n firefox\n",
keywordLookup: true,
@@ -331,30 +346,35 @@ let testcases = [ {
input: "47.6182,-122.830",
fixedURI: "http://47.6182,-122.830/",
keywordLookup: true,
protocolChange: true
protocolChange: true,
affectedByDNSForSingleHosts: true,
}, {
input: "-47.6182,-23.51",
fixedURI: "http://-47.6182,-23.51/",
keywordLookup: true,
protocolChange: true
protocolChange: true,
affectedByDNSForSingleHosts: true,
}, {
input: "-22.14,23.51-",
fixedURI: "http://-22.14,23.51-/",
keywordLookup: true,
protocolChange: true
protocolChange: true,
affectedByDNSForSingleHosts: true,
}, {
input: "32.7",
fixedURI: "http://32.7/",
alternateURI: "http://www.32.7/",
keywordLookup: true,
protocolChange: true
protocolChange: true,
affectedByDNSForSingleHosts: true,
}, {
input: "5+2",
fixedURI: "http://5+2/",
alternateURI: "http://www.5+2.com/",
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
}, {
input: "moz ?.::%27",
keywordLookup: true,
@@ -450,6 +470,7 @@ if (Services.appinfo.OS.toLowerCase().startsWith("win")) {
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
});
} else {
testcases.push({
@@ -469,6 +490,7 @@ if (Services.appinfo.OS.toLowerCase().startsWith("win")) {
keywordLookup: true,
protocolChange: true,
affectedByWhitelist: true,
affectedByDNSForSingleHosts: true,
});
}
@@ -476,20 +498,47 @@ function sanitize(input) {
return input.replace(/\r|\n/g, "").trim();
}
let gSingleWordHostLookup = false;
function run_test() {
// Only keywordlookup things should be affected by requiring a DNS lookup for single-word hosts:
do_print("Check only keyword lookup testcases should be affected by requiring DNS for single hosts");
let affectedTests = testcases.filter(t => !t.keywordLookup && t.affectedByDNSForSingleHosts);
if (affectedTests.length) {
for (let testcase of affectedTests) {
do_print("Affected: " + testcase.input);
}
}
do_check_eq(affectedTests.length, 0);
do_single_test_run();
gSingleWordHostLookup = true;
do_single_test_run();
}
function do_single_test_run() {
Services.prefs.setBoolPref(kForceHostLookup, gSingleWordHostLookup);
let relevantTests = gSingleWordHostLookup ? testcases.filter(t => t.keywordLookup) :
testcases;
for (let { input: testInput,
fixedURI: expectedFixedURI,
alternateURI: alternativeURI,
keywordLookup: expectKeywordLookup,
protocolChange: expectProtocolChange,
affectedByWhitelist: affectedByWhitelist,
inWhitelist: inWhitelist } of testcases) {
inWhitelist: inWhitelist,
affectedByDNSForSingleHosts: affectedByDNSForSingleHosts,
} of relevantTests) {
// Explicitly force these into a boolean
expectKeywordLookup = !!expectKeywordLookup;
expectProtocolChange = !!expectProtocolChange;
affectedByWhitelist = !!affectedByWhitelist;
inWhitelist = !!inWhitelist;
affectedByDNSForSingleHosts = !!affectedByDNSForSingleHosts;
expectKeywordLookup = expectKeywordLookup && (!affectedByDNSForSingleHosts || !gSingleWordHostLookup);
for (let flags of flagInputs) {
let info;
@@ -511,7 +560,8 @@ function run_test() {
continue;
}
do_print("Checking \"" + testInput + "\" with flags " + flags);
do_print("Checking \"" + testInput + "\" with flags " + flags +
" (host lookup for single words: " + (gSingleWordHostLookup ? "yes" : "no") + ")");
// Both APIs should then also be using the same spec.
do_check_eq(!!fixupURIOnly, !!info.preferredURI);
@@ -555,8 +605,10 @@ function run_test() {
do_check_eq(info.preferredURI.spec, info.fixedURI.spec);
}
} else if (requiresWhitelistedDomain) {
// Not a keyword search, but we want to enforce the host whitelist
if (!affectedByWhitelist || (affectedByWhitelist && inWhitelist))
// Not a keyword search, but we want to enforce the host whitelist.
// If we always do DNS lookups, we should always havea preferred URI here - unless the
// input starts with '?' in which case preferredURI will just be null...
if (!affectedByWhitelist || (affectedByWhitelist && inWhitelist) || (gSingleWordHostLookup && !testInput.startsWith("?")))
do_check_eq(info.preferredURI.spec, info.fixedURI.spec);
else
do_check_eq(info.preferredURI, null);

View File

@@ -844,6 +844,7 @@ pref("application.use_ns_plugin_finder", false);
pref("browser.fixup.alternate.enabled", true);
pref("browser.fixup.alternate.prefix", "www.");
pref("browser.fixup.alternate.suffix", ".com");
pref("browser.fixup.dns_first_for_single_words", false);
pref("browser.fixup.hide_user_pass", true);
// Location Bar AutoComplete