Bug 1680139 - Add the ability for unEscapeURIForUI to disable re-escaping IDN blocklisted characters. r=m_kato

Differential Revision: https://phabricator.services.mozilla.com/D134401
This commit is contained in:
Masatoshi Kimura 2021-12-23 03:34:13 +00:00
parent 28706cdf1a
commit a41588c2f6
4 changed files with 38 additions and 1 deletions

View File

@ -32,9 +32,17 @@ interface nsITextToSubURI : nsISupports
* </ul> * </ul>
* *
* @param aURIFragment the URI (or URI fragment) to unescape * @param aURIFragment the URI (or URI fragment) to unescape
* @param aDontEscape whether to escape IDN blocklisted characters
* @return Unescaped aURIFragment converted to unicode * @return Unescaped aURIFragment converted to unicode
*/ */
AString unEscapeURIForUI(in AUTF8String aURIFragment); AString unEscapeURIForUI(in AUTF8String aURIFragment,
[optional] in boolean aDontEscape);
%{C++
nsresult UnEscapeURIForUI(const nsACString& aURIFragment,
nsAString& _retval) {
return UnEscapeURIForUI(aURIFragment, false, _retval);
}
%}
/** /**
* Unescapes only non ASCII characters in the given URI fragment * Unescapes only non ASCII characters in the given URI fragment

View File

@ -100,6 +100,7 @@ nsresult nsTextToSubURI::convertURItoUnicode(const nsCString& aCharset,
} }
NS_IMETHODIMP nsTextToSubURI::UnEscapeURIForUI(const nsACString& aURIFragment, NS_IMETHODIMP nsTextToSubURI::UnEscapeURIForUI(const nsACString& aURIFragment,
bool aDontEscape,
nsAString& _retval) { nsAString& _retval) {
nsAutoCString unescapedSpec; nsAutoCString unescapedSpec;
// skip control octets (0x00 - 0x1f and 0x7f) when unescaping // skip control octets (0x00 - 0x1f and 0x7f) when unescaping
@ -114,6 +115,10 @@ NS_IMETHODIMP nsTextToSubURI::UnEscapeURIForUI(const nsACString& aURIFragment,
CopyUTF8toUTF16(aURIFragment, _retval); CopyUTF8toUTF16(aURIFragment, _retval);
} }
if (aDontEscape) {
return NS_OK;
}
// If there are any characters that are unsafe for URIs, reescape those. // If there are any characters that are unsafe for URIs, reescape those.
if (mIDNBlocklist.IsEmpty()) { if (mIDNBlocklist.IsEmpty()) {
mozilla::net::InitializeBlocklist(mIDNBlocklist); mozilla::net::InitializeBlocklist(mIDNBlocklist);

View File

@ -0,0 +1,23 @@
// Tests for nsITextToSubURI.unEscapeURIForUI
function run_test() {
// Tests whether incomplete multibyte sequences throw.
const tests = [
{
input: "http://example.com/?p=%E3%80%82",
//TODO: should be the same as input, bug 1248812
expected: "http://example.com/?p=%u3002",
},
{
input: "http://example.com/?name=%E3%80%82",
dontEscape: true,
expected: "http://example.com/?name=\u3002",
},
];
for (const t of tests) {
Assert.equal(
Services.textToSubURI.unEscapeURIForUI(t.input, t.dontEscape),
t.expected
);
}
}

View File

@ -86,5 +86,6 @@ support-files =
[test_encode_macintosh.js] [test_encode_macintosh.js]
[test_input_stream.js] [test_input_stream.js]
[test_unEscapeNonAsciiURI.js] [test_unEscapeNonAsciiURI.js]
[test_unEscapeURIForUI.js]
[test_unmapped.js] [test_unmapped.js]
[test_utf8_illegals.js] [test_utf8_illegals.js]