Bug 1689420 - Fix origin header for null principal content dialogs. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D103810
This commit is contained in:
Micah Tigley 2021-02-05 17:39:10 +00:00
Родитель f780a008d6
Коммит 4de0841b18
5 изменённых файлов: 91 добавлений и 23 удалений

Просмотреть файл

@ -3,6 +3,8 @@ support-files =
subdialog.xhtml
[browser_tabdialogbox_content_prompts.js]
support-files =
test_page.html
[browser_tabdialogbox_navigation.js]
[browser_tabdialogbox_focus.js]
[browser_subdialog_esc.js]

Просмотреть файл

@ -6,7 +6,16 @@
const CONTENT_PROMPT_PREF = "prompts.contentPromptSubDialog";
const TEST_ROOT_CHROME = getRootDirectory(gTestPath);
const TEST_DIALOG_PATH = TEST_ROOT_CHROME + "subdialog.xhtml";
const TEST_URL = "data:text/html,<body onload='alert(1)'>";
const TEST_DATA_URI = "data:text/html,<body onload='alert(1)'>";
const TEST_ORIGIN = "http://example.com";
const TEST_PAGE =
TEST_ROOT_CHROME.replace("chrome://mochitests/content", TEST_ORIGIN) +
"test_page.html";
var commonDialogsBundle = Services.strings.createBundle(
"chrome://global/locale/commonDialogs.properties"
);
// Setup.
add_task(async function setup() {
@ -48,29 +57,68 @@ add_task(async function test_tabdialog_content_prompts() {
});
/**
* Test that title text is shown in tabmodal alert/confirm/prompt dialogs.
* Test origin text for a null principal.
*/
add_task(async function test_tabdialog_show_title() {
add_task(async function test_tabdialog_null_principal_title() {
let dialogShown = BrowserTestUtils.waitForEvent(
gBrowser,
"DOMWillOpenModalDialog"
);
await BrowserTestUtils.withNewTab(TEST_URL, async function(browser) {
await BrowserTestUtils.withNewTab(TEST_DATA_URI, async function(browser) {
info("Waiting for dialog to open.");
await dialogShown;
info("Check the title is visible.");
let dialogBox = gBrowser.getTabDialogBox(browser);
let contentPromptManager = dialogBox.getContentDialogManager();
let dialog = contentPromptManager._dialogs[0];
info("Waiting for dialog frame to be ready.");
await dialog._dialogReady;
let dialogDoc = dialog._frame.contentWindow.document;
let infoTitle = dialogDoc.querySelector("#infoTitle");
ok(BrowserTestUtils.is_visible(infoTitle), "Title text is visible");
await checkOriginText(browser, "ScriptDlgNullPrincipalHeading");
});
});
/**
* Test origin text for a regular page.
*/
add_task(async function test_tabdialog_page_title() {
let dialogShown = BrowserTestUtils.waitForEvent(
gBrowser,
"DOMWillOpenModalDialog"
);
await BrowserTestUtils.withNewTab(TEST_PAGE, async function(browser) {
info("Waiting for dialog to open.");
await dialogShown;
await checkOriginText(browser, "ScriptDlgHeading", TEST_ORIGIN);
});
});
/**
* Test helper for checking the origin header of a dialog.
*
* @param {Object} browser
* The browser the dialog was opened from.
* @param {String} stringKey
* The bundle key for the text that should be displayed.
* @param {String|null} origin
* The page origin that should be displayed in the header, if any.
*/
async function checkOriginText(browser, stringKey, origin = null) {
info("Check the title is visible.");
let dialogBox = gBrowser.getTabDialogBox(browser);
let contentPromptManager = dialogBox.getContentDialogManager();
let dialog = contentPromptManager._dialogs[0];
info("Waiting for dialog frame to be ready.");
await dialog._dialogReady;
let dialogDoc = dialog._frame.contentWindow.document;
let infoTitle = dialogDoc.querySelector("#infoTitle");
ok(BrowserTestUtils.is_visible(infoTitle), "Title text is visible");
info("Check the displayed origin text is correct.");
let titleText;
if (origin) {
titleText = commonDialogsBundle.formatStringFromName(stringKey, [origin]);
} else {
titleText = commonDialogsBundle.GetStringFromName(stringKey);
}
is(infoTitle.textContent, titleText, "Origin header is correct.");
}

Просмотреть файл

@ -0,0 +1,10 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>TabDialogBox Content Modal Test page</title>
</head>
<body onload='alert("Hi");'>
<h1>TabDialogBox Content Modal</h1>
</body>
</html>

Просмотреть файл

@ -4783,12 +4783,19 @@ void nsGlobalWindowOuter::MakeScriptDialogTitle(
// right thing for javascript: and data: documents.
nsAutoCString prepath;
nsresult rv = aSubjectPrincipal->GetExposablePrePath(prepath);
if (NS_SUCCEEDED(rv) && !prepath.IsEmpty()) {
NS_ConvertUTF8toUTF16 ucsPrePath(prepath);
nsContentUtils::FormatLocalizedString(
aOutTitle, nsContentUtils::eCOMMON_DIALOG_PROPERTIES,
"ScriptDlgHeading", ucsPrePath);
if (aSubjectPrincipal->GetIsNullPrincipal()) {
nsContentUtils::GetLocalizedString(
nsContentUtils::eCOMMON_DIALOG_PROPERTIES,
"ScriptDlgNullPrincipalHeading", aOutTitle);
} else {
nsresult rv = aSubjectPrincipal->GetExposablePrePath(prepath);
if (NS_SUCCEEDED(rv) && !prepath.IsEmpty()) {
NS_ConvertUTF8toUTF16 ucsPrePath(prepath);
nsContentUtils::FormatLocalizedString(
aOutTitle, nsContentUtils::eCOMMON_DIALOG_PROPERTIES,
"ScriptDlgHeading", ucsPrePath);
}
}
if (aOutTitle.IsEmpty()) {

Просмотреть файл

@ -20,6 +20,7 @@ Revert=&Revert
DontSave=Do&nt Save
ScriptDlgGenericHeading=[JavaScript Application]
ScriptDlgHeading=The page at %S says:
ScriptDlgNullPrincipalHeading=This page says:
ScriptDialogLabel=Prevent this page from creating additional dialogs
ScriptDialogPreventTitle=Confirm Dialog Preference
# LOCALIZATION NOTE (EnterLoginForRealm3, EnterLoginForProxy3):