Bug 1924087: Check language spoofing in SVG's switch element. r=tjr,longsonr

Differential Revision: https://phabricator.services.mozilla.com/D225358
This commit is contained in:
Fatih
2024-11-07 12:58:10 +00:00
parent dbe2e1a79b
commit 267c85c60c
5 changed files with 101 additions and 11 deletions

View File

@@ -4364,13 +4364,17 @@ bool nsContentUtils::SpoofLocaleEnglish() {
return StaticPrefs::privacy_spoof_english() == 2;
}
/* static */
bool nsContentUtils::SpoofLocaleEnglish(const Document* aDocument) {
return SpoofLocaleEnglish() && (!aDocument || !aDocument->AllowsL10n());
}
static nsContentUtils::PropertiesFile GetMaybeSpoofedPropertiesFile(
nsContentUtils::PropertiesFile aFile, const char* aKey,
Document* aDocument) {
// When we spoof English, use en-US properties in strings that are accessible
// by content.
bool spoofLocale = nsContentUtils::SpoofLocaleEnglish() &&
(!aDocument || !aDocument->AllowsL10n());
bool spoofLocale = nsContentUtils::SpoofLocaleEnglish(aDocument);
if (spoofLocale) {
switch (aFile) {
case nsContentUtils::eFORMS_PROPERTIES:

View File

@@ -1320,6 +1320,7 @@ class nsContentUtils {
static void LogMessageToConsole(const char* aMsg);
static bool SpoofLocaleEnglish();
static bool SpoofLocaleEnglish(const Document* aDocument);
/**
* Get the localized string named |aKey| in properties file |aFile|.

View File

@@ -61,15 +61,19 @@ bool SVGTests::IsConditionalProcessingAttribute(
// Find the best match from aAvailLangs for the users accept-languages,
// returning the index in the aAvailLangs list, or -1 if no match.
static int32_t FindBestLanguage(const nsTArray<nsCString>& aAvailLangs) {
static int32_t FindBestLanguage(const nsTArray<nsCString>& aAvailLangs,
const Document* aDoc) {
AutoTArray<nsCString, 16> reqLangs;
nsCString acceptLangs;
Preferences::GetLocalizedCString("intl.accept_languages", acceptLangs);
nsCCharSeparatedTokenizer languageTokenizer(acceptLangs, ',');
while (languageTokenizer.hasMoreTokens()) {
reqLangs.AppendElement(languageTokenizer.nextToken());
if (nsContentUtils::SpoofLocaleEnglish(aDoc)) {
reqLangs.AppendElements(Span(std::array{"en-US", "en"}));
} else {
nsCString acceptLangs;
Preferences::GetLocalizedCString("intl.accept_languages", acceptLangs);
nsCCharSeparatedTokenizer languageTokenizer(acceptLangs, ',');
while (languageTokenizer.hasMoreTokens()) {
reqLangs.AppendElement(languageTokenizer.nextToken());
}
}
for (const auto& req : reqLangs) {
for (const auto& avail : aAvailLangs) {
if (avail.Length() > req.Length()) {
@@ -128,7 +132,7 @@ nsIContent* SVGTests::FindActiveSwitchChild(
return defaultChild;
}
int32_t index = FindBestLanguage(availLocales);
int32_t index = FindBestLanguage(availLocales, aSwitch->OwnerDoc());
if (index >= 0) {
return children[index];
}
@@ -186,7 +190,7 @@ bool SVGTests::PassesConditionalProcessingTests() const {
}
mPassesConditionalProcessingTests =
Some(FindBestLanguage(availLocales) >= 0);
Some(FindBestLanguage(availLocales, AsSVGElement()->OwnerDoc()) >= 0);
return mPassesConditionalProcessingTests.value();
}

View File

@@ -184,3 +184,5 @@ scheme = "https"
["test_viewBox.html"]
["test_viewport.html"]
["test_SVG_switch_language_spoof.html"]

View File

@@ -0,0 +1,79 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1924087
-->
<head>
<title>Test for Bug 1924087</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
function clearAndAddSVG() {
document.getElementById("display").innerHTML = "";
const template = document.querySelector("template");
const clone = document.importNode(template.content, true);
document.getElementById("display").appendChild(clone);
}
function getShownTexts() {
const swtch = document.getElementById("switch");
return [...swtch.children].filter(
child => child.getBoundingClientRect().width > 0
);
}
async function runTests() {
await SpecialPowers.pushPrefEnv({
set: [["intl.accept_languages", "tr"]],
});
await SpecialPowers.pushPrefEnv({
set: [["privacy.spoof_english", 2]],
});
clearAndAddSVG();
const spoofedShown = getShownTexts();
is(spoofedShown.length, 1, "Only one child should be selected");
is(
spoofedShown[0].textContent,
"Hello!",
"The selected child should be the one with the 'en' language"
);
await SpecialPowers.popPrefEnv();
clearAndAddSVG();
const shown = getShownTexts();
is(shown.length, 1, "Only one child should be selected");
is(
shown[0].textContent,
"Merhaba!",
"The selected child should be the one with the 'tr' language"
);
await SpecialPowers.popPrefEnv();
SimpleTest.finish();
}
</script>
</head>
<body onload="runTests()">
<a
target="_blank"
href="https://bugzilla.mozilla.org/show_bug.cgi?id=1924087"
>Mozilla Bug 1924087</a
>
<template>
<svg>
<switch id="switch">
<text systemLanguage="en">Hello!</text>
<text systemLanguage="tr">Merhaba!</text>
</switch>
</svg>
</template>
<div id="display"></div>
</body>
</html>