2009-03-29 11:49:31 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-09-26 03:49:08 +04:00
|
|
|
|
|
|
|
#include "nsIDNService.h"
|
2003-01-18 02:56:33 +03:00
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
2003-04-11 21:05:56 +04:00
|
|
|
#include "nsIObserverService.h"
|
2005-07-22 19:07:33 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2003-01-18 02:56:33 +03:00
|
|
|
#include "punycode.h"
|
|
|
|
|
2010-05-20 03:22:19 +04:00
|
|
|
#include "mozilla/FunctionTimer.h"
|
|
|
|
|
2003-01-18 02:56:33 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
2003-03-25 23:48:03 +03:00
|
|
|
// RFC 1034 - 3.1. Name space specifications and terminology
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t kMaxDNSNodeLen = 63;
|
2003-01-18 02:56:33 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2005-02-17 23:02:37 +03:00
|
|
|
#define NS_NET_PREF_IDNTESTBED "network.IDN_testbed"
|
|
|
|
#define NS_NET_PREF_IDNPREFIX "network.IDN_prefix"
|
2005-07-22 19:07:33 +04:00
|
|
|
#define NS_NET_PREF_IDNBLACKLIST "network.IDN.blacklist_chars"
|
2007-12-04 09:30:47 +03:00
|
|
|
#define NS_NET_PREF_SHOWPUNYCODE "network.IDN_show_punycode"
|
|
|
|
#define NS_NET_PREF_IDNWHITELIST "network.IDN.whitelist."
|
2005-07-22 19:07:33 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
inline bool isOnlySafeChars(const nsAFlatString& in,
|
2005-07-22 19:07:33 +04:00
|
|
|
const nsAFlatString& blacklist)
|
|
|
|
{
|
|
|
|
return (blacklist.IsEmpty() ||
|
|
|
|
in.FindCharInSet(blacklist) == kNotFound);
|
|
|
|
}
|
2002-09-26 03:49:08 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIDNService
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/* Implementation file */
|
2003-04-11 21:05:56 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsIDNService,
|
|
|
|
nsIIDNService,
|
|
|
|
nsIObserver,
|
2003-09-08 03:12:15 +04:00
|
|
|
nsISupportsWeakReference)
|
2003-04-11 21:05:56 +04:00
|
|
|
|
|
|
|
nsresult nsIDNService::Init()
|
|
|
|
{
|
2010-05-20 03:22:19 +04:00
|
|
|
NS_TIME_FUNCTION;
|
|
|
|
|
2007-12-04 09:30:47 +03:00
|
|
|
nsCOMPtr<nsIPrefService> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
|
|
|
if (prefs)
|
|
|
|
prefs->GetBranch(NS_NET_PREF_IDNWHITELIST, getter_AddRefs(mIDNWhitelistPrefBranch));
|
|
|
|
|
2012-01-17 05:48:29 +04:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefInternal(do_QueryInterface(prefs));
|
2003-04-11 21:05:56 +04:00
|
|
|
if (prefInternal) {
|
2011-10-17 18:59:28 +04:00
|
|
|
prefInternal->AddObserver(NS_NET_PREF_IDNTESTBED, this, true);
|
|
|
|
prefInternal->AddObserver(NS_NET_PREF_IDNPREFIX, this, true);
|
|
|
|
prefInternal->AddObserver(NS_NET_PREF_IDNBLACKLIST, this, true);
|
|
|
|
prefInternal->AddObserver(NS_NET_PREF_SHOWPUNYCODE, this, true);
|
2012-07-30 18:20:58 +04:00
|
|
|
prefsChanged(prefInternal, nullptr);
|
2003-04-11 21:05:56 +04:00
|
|
|
}
|
2007-12-04 09:30:47 +03:00
|
|
|
|
2003-04-11 21:05:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsIDNService::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch( do_QueryInterface(aSubject) );
|
2005-02-17 23:02:37 +03:00
|
|
|
if (prefBranch)
|
|
|
|
prefsChanged(prefBranch, aData);
|
2003-04-11 21:05:56 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-09-26 03:49:08 +04:00
|
|
|
|
2005-02-17 23:02:37 +03:00
|
|
|
void nsIDNService::prefsChanged(nsIPrefBranch *prefBranch, const PRUnichar *pref)
|
|
|
|
{
|
|
|
|
if (!pref || NS_LITERAL_STRING(NS_NET_PREF_IDNTESTBED).Equals(pref)) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool val;
|
2005-02-17 23:02:37 +03:00
|
|
|
if (NS_SUCCEEDED(prefBranch->GetBoolPref(NS_NET_PREF_IDNTESTBED, &val)))
|
|
|
|
mMultilingualTestBed = val;
|
|
|
|
}
|
|
|
|
if (!pref || NS_LITERAL_STRING(NS_NET_PREF_IDNPREFIX).Equals(pref)) {
|
|
|
|
nsXPIDLCString prefix;
|
|
|
|
nsresult rv = prefBranch->GetCharPref(NS_NET_PREF_IDNPREFIX, getter_Copies(prefix));
|
|
|
|
if (NS_SUCCEEDED(rv) && prefix.Length() <= kACEPrefixLen)
|
|
|
|
PL_strncpyz(nsIDNService::mACEPrefix, prefix.get(), kACEPrefixLen + 1);
|
|
|
|
}
|
2005-07-22 19:07:33 +04:00
|
|
|
if (!pref || NS_LITERAL_STRING(NS_NET_PREF_IDNBLACKLIST).Equals(pref)) {
|
|
|
|
nsCOMPtr<nsISupportsString> blacklist;
|
|
|
|
nsresult rv = prefBranch->GetComplexValue(NS_NET_PREF_IDNBLACKLIST,
|
|
|
|
NS_GET_IID(nsISupportsString),
|
|
|
|
getter_AddRefs(blacklist));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
blacklist->ToString(getter_Copies(mIDNBlacklist));
|
|
|
|
else
|
|
|
|
mIDNBlacklist.Truncate();
|
|
|
|
}
|
2007-12-04 09:30:47 +03:00
|
|
|
if (!pref || NS_LITERAL_STRING(NS_NET_PREF_SHOWPUNYCODE).Equals(pref)) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool val;
|
2007-12-04 09:30:47 +03:00
|
|
|
if (NS_SUCCEEDED(prefBranch->GetBoolPref(NS_NET_PREF_SHOWPUNYCODE, &val)))
|
|
|
|
mShowPunycode = val;
|
|
|
|
}
|
2005-02-17 23:02:37 +03:00
|
|
|
}
|
|
|
|
|
2002-09-26 03:49:08 +04:00
|
|
|
nsIDNService::nsIDNService()
|
|
|
|
{
|
2003-03-13 00:31:39 +03:00
|
|
|
// initialize to the official prefix (RFC 3490 "5. ACE prefix")
|
|
|
|
const char kIDNSPrefix[] = "xn--";
|
2003-01-18 02:56:33 +03:00
|
|
|
strcpy(mACEPrefix, kIDNSPrefix);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mMultilingualTestBed = false;
|
2003-01-18 02:56:33 +03:00
|
|
|
|
|
|
|
if (idn_success != idn_nameprep_create(NULL, &mNamePrepHandle))
|
2012-07-30 18:20:58 +04:00
|
|
|
mNamePrepHandle = nullptr;
|
2003-01-18 02:56:33 +03:00
|
|
|
|
2008-08-19 04:19:27 +04:00
|
|
|
mNormalizer = do_GetService(NS_UNICODE_NORMALIZER_CONTRACTID);
|
2002-09-26 03:49:08 +04:00
|
|
|
/* member initializers and constructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDNService::~nsIDNService()
|
|
|
|
{
|
2003-01-18 02:56:33 +03:00
|
|
|
idn_nameprep_destroy(mNamePrepHandle);
|
2002-09-26 03:49:08 +04:00
|
|
|
}
|
|
|
|
|
2003-03-14 02:06:14 +03:00
|
|
|
/* ACString ConvertUTF8toACE (in AUTF8String input); */
|
|
|
|
NS_IMETHODIMP nsIDNService::ConvertUTF8toACE(const nsACString & input, nsACString & ace)
|
2009-03-29 11:49:31 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return UTF8toACE(input, ace, true);
|
2009-03-29 11:49:31 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsIDNService::UTF8toACE(const nsACString & input, nsACString & ace, bool allowUnassigned)
|
2002-09-26 03:49:08 +04:00
|
|
|
{
|
2003-01-18 02:56:33 +03:00
|
|
|
nsresult rv;
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_ConvertUTF8toUTF16 ustr(input);
|
2003-01-18 02:56:33 +03:00
|
|
|
|
|
|
|
// map ideographic period to ASCII period etc.
|
|
|
|
normalizeFullStops(ustr);
|
|
|
|
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len, offset;
|
2003-01-18 02:56:33 +03:00
|
|
|
len = 0;
|
|
|
|
offset = 0;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString encodedBuf;
|
2003-01-18 02:56:33 +03:00
|
|
|
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
ustr.BeginReading(start);
|
|
|
|
ustr.EndReading(end);
|
2003-04-03 01:33:23 +04:00
|
|
|
ace.Truncate();
|
2003-01-18 02:56:33 +03:00
|
|
|
|
|
|
|
// encode nodes if non ASCII
|
|
|
|
while (start != end) {
|
|
|
|
len++;
|
|
|
|
if (*start++ == (PRUnichar)'.') {
|
2009-03-29 11:49:31 +04:00
|
|
|
rv = stringPrepAndACE(Substring(ustr, offset, len - 1), encodedBuf,
|
|
|
|
allowUnassigned);
|
2003-01-18 02:56:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2004-12-04 13:19:29 +03:00
|
|
|
ace.Append(encodedBuf);
|
|
|
|
ace.Append('.');
|
2003-01-18 02:56:33 +03:00
|
|
|
offset += len;
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add extra node for multilingual test bed
|
|
|
|
if (mMultilingualTestBed)
|
2004-12-04 13:19:29 +03:00
|
|
|
ace.AppendLiteral("mltbd.");
|
2003-01-18 02:56:33 +03:00
|
|
|
// encode the last node if non ASCII
|
|
|
|
if (len) {
|
2009-03-29 11:49:31 +04:00
|
|
|
rv = stringPrepAndACE(Substring(ustr, offset, len), encodedBuf,
|
|
|
|
allowUnassigned);
|
2003-01-18 02:56:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
ace.Append(encodedBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2002-09-26 03:49:08 +04:00
|
|
|
}
|
|
|
|
|
2007-05-04 10:00:33 +04:00
|
|
|
/* AUTF8String convertACEtoUTF8(in ACString input); */
|
2003-03-14 02:06:14 +03:00
|
|
|
NS_IMETHODIMP nsIDNService::ConvertACEtoUTF8(const nsACString & input, nsACString & _retval)
|
2009-03-29 11:49:31 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return ACEtoUTF8(input, _retval, true);
|
2009-03-29 11:49:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsIDNService::ACEtoUTF8(const nsACString & input, nsACString & _retval,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool allowUnassigned)
|
2002-09-26 03:49:08 +04:00
|
|
|
{
|
2003-03-26 02:18:41 +03:00
|
|
|
// RFC 3490 - 4.2 ToUnicode
|
|
|
|
// ToUnicode never fails. If any step fails, then the original input
|
|
|
|
// sequence is returned immediately in that step.
|
|
|
|
|
2005-04-27 05:32:36 +04:00
|
|
|
if (!IsASCII(input)) {
|
2003-03-26 02:18:41 +03:00
|
|
|
_retval.Assign(input);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = 0, offset = 0;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString decodedBuf;
|
2003-03-26 02:18:41 +03:00
|
|
|
|
|
|
|
nsACString::const_iterator start, end;
|
|
|
|
input.BeginReading(start);
|
|
|
|
input.EndReading(end);
|
|
|
|
_retval.Truncate();
|
|
|
|
|
|
|
|
// loop and decode nodes
|
|
|
|
while (start != end) {
|
|
|
|
len++;
|
|
|
|
if (*start++ == '.') {
|
2009-03-29 11:49:31 +04:00
|
|
|
if (NS_FAILED(decodeACE(Substring(input, offset, len - 1), decodedBuf,
|
|
|
|
allowUnassigned))) {
|
2003-03-26 02:18:41 +03:00
|
|
|
_retval.Assign(input);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-12-04 13:19:29 +03:00
|
|
|
_retval.Append(decodedBuf);
|
|
|
|
_retval.Append('.');
|
2003-03-26 02:18:41 +03:00
|
|
|
offset += len;
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// decode the last node
|
|
|
|
if (len) {
|
2009-03-29 11:49:31 +04:00
|
|
|
if (NS_FAILED(decodeACE(Substring(input, offset, len), decodedBuf,
|
|
|
|
allowUnassigned)))
|
2003-03-26 02:18:41 +03:00
|
|
|
_retval.Assign(input);
|
|
|
|
else
|
|
|
|
_retval.Append(decodedBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2002-09-26 03:49:08 +04:00
|
|
|
}
|
|
|
|
|
2007-05-04 10:00:33 +04:00
|
|
|
/* boolean isACE(in ACString input); */
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsIDNService::IsACE(const nsACString & input, bool *_retval)
|
2003-03-14 02:06:14 +03:00
|
|
|
{
|
2004-05-14 02:17:17 +04:00
|
|
|
nsACString::const_iterator begin;
|
|
|
|
input.BeginReading(begin);
|
|
|
|
|
|
|
|
const char *data = begin.get();
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dataLen = begin.size_forward();
|
2004-05-14 02:17:17 +04:00
|
|
|
|
|
|
|
// look for the ACE prefix in the input string. it may occur
|
|
|
|
// at the beginning of any segment in the domain name. for
|
|
|
|
// example: "www.xn--ENCODED.com"
|
|
|
|
|
|
|
|
const char *p = PL_strncasestr(data, mACEPrefix, dataLen);
|
2003-03-14 02:06:14 +03:00
|
|
|
|
2004-05-14 02:17:17 +04:00
|
|
|
*_retval = p && (p == data || *(p - 1) == '.');
|
2003-03-14 02:06:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-04 10:00:33 +04:00
|
|
|
/* AUTF8String normalize(in AUTF8String input); */
|
2004-04-03 11:32:18 +04:00
|
|
|
NS_IMETHODIMP nsIDNService::Normalize(const nsACString & input, nsACString & output)
|
|
|
|
{
|
2004-08-25 08:41:48 +04:00
|
|
|
// protect against bogus input
|
2004-08-25 08:43:14 +04:00
|
|
|
NS_ENSURE_TRUE(IsUTF8(input), NS_ERROR_UNEXPECTED);
|
2004-08-25 08:41:48 +04:00
|
|
|
|
2005-02-17 23:02:37 +03:00
|
|
|
NS_ConvertUTF8toUTF16 inUTF16(input);
|
|
|
|
normalizeFullStops(inUTF16);
|
|
|
|
|
2008-04-14 12:26:55 +04:00
|
|
|
// pass the domain name to stringprep label by label
|
|
|
|
nsAutoString outUTF16, outLabel;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = 0, offset = 0;
|
2008-04-14 12:26:55 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
inUTF16.BeginReading(start);
|
|
|
|
inUTF16.EndReading(end);
|
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
len++;
|
|
|
|
if (*start++ == PRUnichar('.')) {
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = stringPrep(Substring(inUTF16, offset, len - 1), outLabel, true);
|
2008-04-14 12:26:55 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
outUTF16.Append(outLabel);
|
|
|
|
outUTF16.Append(PRUnichar('.'));
|
|
|
|
offset += len;
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len) {
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = stringPrep(Substring(inUTF16, offset, len), outLabel, true);
|
2008-04-14 12:26:55 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
outUTF16.Append(outLabel);
|
|
|
|
}
|
2005-07-22 19:07:33 +04:00
|
|
|
|
|
|
|
CopyUTF16toUTF8(outUTF16, output);
|
|
|
|
if (!isOnlySafeChars(outUTF16, mIDNBlacklist))
|
|
|
|
return ConvertUTF8toACE(output, output);
|
|
|
|
|
|
|
|
return NS_OK;
|
2004-04-03 11:32:18 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsIDNService::ConvertToDisplayIDN(const nsACString & input, bool * _isASCII, nsACString & _retval)
|
2007-12-04 09:30:47 +03:00
|
|
|
{
|
|
|
|
// If host is ACE, then convert to UTF-8 if the host is in the IDN whitelist.
|
|
|
|
// Else, if host is already UTF-8, then make sure it is normalized per IDN.
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (IsASCII(input)) {
|
|
|
|
// first, canonicalize the host to lowercase, for whitelist lookup
|
|
|
|
_retval = input;
|
|
|
|
ToLowerCase(_retval);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isACE;
|
2007-12-04 09:30:47 +03:00
|
|
|
IsACE(_retval, &isACE);
|
|
|
|
|
|
|
|
if (isACE && !mShowPunycode && isInWhitelist(_retval)) {
|
2009-03-29 11:49:31 +04:00
|
|
|
// ACEtoUTF8() can't fail, but might return the original ACE string
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString temp(_retval);
|
2011-10-17 18:59:28 +04:00
|
|
|
ACEtoUTF8(temp, _retval, false);
|
2007-12-04 09:30:47 +03:00
|
|
|
*_isASCII = IsASCII(_retval);
|
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isASCII = true;
|
2007-12-04 09:30:47 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We have to normalize the hostname before testing against the domain
|
2008-01-29 09:06:01 +03:00
|
|
|
// whitelist (see bug 315411), and to ensure the entire string gets
|
|
|
|
// normalized.
|
2007-12-04 09:30:47 +03:00
|
|
|
rv = Normalize(input, _retval);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2008-01-29 09:06:01 +03:00
|
|
|
if (mShowPunycode && NS_SUCCEEDED(ConvertUTF8toACE(_retval, _retval))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isASCII = true;
|
2008-01-29 09:06:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-12-04 09:30:47 +03:00
|
|
|
// normalization could result in an ASCII-only hostname. alternatively, if
|
|
|
|
// the host is converted to ACE by the normalizer, then the host may contain
|
|
|
|
// unsafe characters, so leave it ACE encoded. see bug 283016, bug 301694, and bug 309311.
|
|
|
|
*_isASCII = IsASCII(_retval);
|
|
|
|
if (!*_isASCII && !isInWhitelist(_retval)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isASCII = true;
|
2007-12-04 09:30:47 +03:00
|
|
|
return ConvertUTF8toACE(_retval, _retval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-18 02:56:33 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static void utf16ToUcs4(const nsAString& in, uint32_t *out, uint32_t outBufLen, uint32_t *outLen)
|
2003-01-18 02:56:33 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2003-01-18 02:56:33 +03:00
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
in.BeginReading(start);
|
|
|
|
in.EndReading(end);
|
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
PRUnichar curChar;
|
|
|
|
|
|
|
|
curChar= *start++;
|
|
|
|
|
|
|
|
if (start != end &&
|
2006-12-21 10:03:23 +03:00
|
|
|
NS_IS_HIGH_SURROGATE(curChar) &&
|
|
|
|
NS_IS_LOW_SURROGATE(*start)) {
|
2003-01-18 02:56:33 +03:00
|
|
|
out[i] = SURROGATE_TO_UCS4(curChar, *start);
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
out[i] = curChar;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
if (i >= outBufLen) {
|
|
|
|
NS_ERROR("input too big, the result truncated");
|
2012-08-22 19:56:38 +04:00
|
|
|
out[outBufLen-1] = (uint32_t)'\0';
|
2007-01-04 21:37:00 +03:00
|
|
|
*outLen = outBufLen-1;
|
2003-01-18 02:56:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
out[i] = (uint32_t)'\0';
|
2003-01-18 02:56:33 +03:00
|
|
|
*outLen = i;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static void ucs4toUtf16(const uint32_t *in, nsAString& out)
|
2003-01-18 02:56:33 +03:00
|
|
|
{
|
|
|
|
while (*in) {
|
|
|
|
if (!IS_IN_BMP(*in)) {
|
|
|
|
out.Append((PRUnichar) H_SURROGATE(*in));
|
|
|
|
out.Append((PRUnichar) L_SURROGATE(*in));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
out.Append((PRUnichar) *in);
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult punycode(const char* prefix, const nsAString& in, nsACString& out)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t ucs4Buf[kMaxDNSNodeLen + 1];
|
|
|
|
uint32_t ucs4Len;
|
2003-01-18 02:56:33 +03:00
|
|
|
utf16ToUcs4(in, ucs4Buf, kMaxDNSNodeLen, &ucs4Len);
|
|
|
|
|
|
|
|
// need maximum 20 bits to encode 16 bit Unicode character
|
|
|
|
// (include null terminator)
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t kEncodedBufSize = kMaxDNSNodeLen * 20 / 8 + 1 + 1;
|
2003-01-18 02:56:33 +03:00
|
|
|
char encodedBuf[kEncodedBufSize];
|
2003-03-21 03:23:12 +03:00
|
|
|
punycode_uint encodedLength = kEncodedBufSize;
|
2003-01-18 02:56:33 +03:00
|
|
|
|
|
|
|
enum punycode_status status = punycode_encode(ucs4Len,
|
|
|
|
ucs4Buf,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
2003-01-18 02:56:33 +03:00
|
|
|
&encodedLength,
|
|
|
|
encodedBuf);
|
|
|
|
|
2003-03-21 03:23:12 +03:00
|
|
|
if (punycode_success != status ||
|
|
|
|
encodedLength >= kEncodedBufSize)
|
2003-01-18 02:56:33 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2003-03-21 03:23:12 +03:00
|
|
|
encodedBuf[encodedLength] = '\0';
|
2003-01-18 02:56:33 +03:00
|
|
|
out.Assign(nsDependentCString(prefix) + nsDependentCString(encodedBuf));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult encodeToRACE(const char* prefix, const nsAString& in, nsACString& out)
|
|
|
|
{
|
|
|
|
// need maximum 20 bits to encode 16 bit Unicode character
|
|
|
|
// (include null terminator)
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t kEncodedBufSize = kMaxDNSNodeLen * 20 / 8 + 1 + 1;
|
2003-01-18 02:56:33 +03:00
|
|
|
|
|
|
|
// set up a work buffer for RACE encoder
|
|
|
|
PRUnichar temp[kMaxDNSNodeLen + 2];
|
|
|
|
temp[0] = 0xFFFF; // set a place holder (to be filled by get_compress_mode)
|
|
|
|
temp[in.Length() + 1] = (PRUnichar)'\0';
|
|
|
|
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
in.BeginReading(start);
|
|
|
|
in.EndReading(end);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 1; start != end; i++)
|
2003-01-18 02:56:33 +03:00
|
|
|
temp[i] = *start++;
|
|
|
|
|
|
|
|
// encode nodes if non ASCII
|
|
|
|
|
|
|
|
char encodedBuf[kEncodedBufSize];
|
2003-01-18 03:50:37 +03:00
|
|
|
idn_result_t result = race_compress_encode((const unsigned short *) temp,
|
|
|
|
get_compress_mode((unsigned short *) temp + 1),
|
2003-01-18 02:56:33 +03:00
|
|
|
encodedBuf, kEncodedBufSize);
|
|
|
|
if (idn_success != result)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2004-12-04 13:19:29 +03:00
|
|
|
out.Assign(prefix);
|
|
|
|
out.Append(encodedBuf);
|
2003-01-18 02:56:33 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-13 00:31:39 +03:00
|
|
|
// RFC 3454
|
2003-01-18 02:56:33 +03:00
|
|
|
//
|
|
|
|
// 1) Map -- For each character in the input, check if it has a mapping
|
|
|
|
// and, if so, replace it with its mapping. This is described in section 3.
|
|
|
|
//
|
|
|
|
// 2) Normalize -- Possibly normalize the result of step 1 using Unicode
|
|
|
|
// normalization. This is described in section 4.
|
|
|
|
//
|
|
|
|
// 3) Prohibit -- Check for any characters that are not allowed in the
|
|
|
|
// output. If any are found, return an error. This is described in section
|
|
|
|
// 5.
|
|
|
|
//
|
|
|
|
// 4) Check bidi -- Possibly check for right-to-left characters, and if any
|
|
|
|
// are found, make sure that the whole string satisfies the requirements
|
|
|
|
// for bidirectional strings. If the string does not satisfy the requirements
|
|
|
|
// for bidirectional strings, return an error. This is described in section 6.
|
|
|
|
//
|
2009-03-29 11:49:31 +04:00
|
|
|
// 5) Check unassigned code points -- If allowUnassigned is false, check for
|
|
|
|
// any unassigned Unicode points and if any are found return an error.
|
|
|
|
// This is described in section 7.
|
|
|
|
//
|
|
|
|
nsresult nsIDNService::stringPrep(const nsAString& in, nsAString& out,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool allowUnassigned)
|
2003-01-18 02:56:33 +03:00
|
|
|
{
|
|
|
|
if (!mNamePrepHandle || !mNormalizer)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t ucs4Buf[kMaxDNSNodeLen + 1];
|
|
|
|
uint32_t ucs4Len;
|
2003-01-18 02:56:33 +03:00
|
|
|
utf16ToUcs4(in, ucs4Buf, kMaxDNSNodeLen, &ucs4Len);
|
|
|
|
|
|
|
|
// map
|
|
|
|
idn_result_t idn_err;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t namePrepBuf[kMaxDNSNodeLen * 3]; // map up to three characters
|
|
|
|
idn_err = idn_nameprep_map(mNamePrepHandle, (const uint32_t *) ucs4Buf,
|
|
|
|
(uint32_t *) namePrepBuf, kMaxDNSNodeLen * 3);
|
2003-01-18 02:56:33 +03:00
|
|
|
NS_ENSURE_TRUE(idn_err == idn_success, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsAutoString namePrepStr;
|
|
|
|
ucs4toUtf16(namePrepBuf, namePrepStr);
|
|
|
|
if (namePrepStr.Length() >= kMaxDNSNodeLen)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// normalize
|
|
|
|
nsAutoString normlizedStr;
|
|
|
|
rv = mNormalizer->NormalizeUnicodeNFKC(namePrepStr, normlizedStr);
|
|
|
|
if (normlizedStr.Length() >= kMaxDNSNodeLen)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// prohibit
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t *found = nullptr;
|
2003-01-18 02:56:33 +03:00
|
|
|
idn_err = idn_nameprep_isprohibited(mNamePrepHandle,
|
2012-08-22 19:56:38 +04:00
|
|
|
(const uint32_t *) ucs4Buf, &found);
|
2003-01-18 02:56:33 +03:00
|
|
|
if (idn_err != idn_success || found)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// check bidi
|
|
|
|
idn_err = idn_nameprep_isvalidbidi(mNamePrepHandle,
|
2012-08-22 19:56:38 +04:00
|
|
|
(const uint32_t *) ucs4Buf, &found);
|
2003-01-18 02:56:33 +03:00
|
|
|
if (idn_err != idn_success || found)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2009-03-29 11:49:31 +04:00
|
|
|
if (!allowUnassigned) {
|
|
|
|
// check unassigned code points
|
|
|
|
idn_err = idn_nameprep_isunassigned(mNamePrepHandle,
|
2012-08-22 19:56:38 +04:00
|
|
|
(const uint32_t *) ucs4Buf, &found);
|
2009-03-29 11:49:31 +04:00
|
|
|
if (idn_err != idn_success || found)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-01-18 02:56:33 +03:00
|
|
|
// set the result string
|
|
|
|
out.Assign(normlizedStr);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsIDNService::encodeToACE(const nsAString& in, nsACString& out)
|
|
|
|
{
|
|
|
|
// RACE encode is supported for existing testing environment
|
|
|
|
if (!strcmp("bq--", mACEPrefix))
|
|
|
|
return encodeToRACE(mACEPrefix, in, out);
|
|
|
|
|
|
|
|
// use punycoce
|
|
|
|
return punycode(mACEPrefix, in, out);
|
|
|
|
}
|
|
|
|
|
2009-03-29 11:49:31 +04:00
|
|
|
nsresult nsIDNService::stringPrepAndACE(const nsAString& in, nsACString& out,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool allowUnassigned)
|
2003-01-18 02:56:33 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
out.Truncate();
|
|
|
|
|
2003-03-25 23:48:03 +03:00
|
|
|
if (in.Length() > kMaxDNSNodeLen) {
|
2011-06-26 20:28:46 +04:00
|
|
|
NS_WARNING("IDN node too large");
|
2003-01-18 02:56:33 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsASCII(in))
|
2006-02-03 17:18:39 +03:00
|
|
|
LossyCopyUTF16toASCII(in, out);
|
2003-01-18 02:56:33 +03:00
|
|
|
else {
|
|
|
|
nsAutoString strPrep;
|
2009-03-29 11:49:31 +04:00
|
|
|
rv = stringPrep(in, strPrep, allowUnassigned);
|
2003-07-08 05:51:32 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (IsASCII(strPrep))
|
2006-02-03 17:18:39 +03:00
|
|
|
LossyCopyUTF16toASCII(strPrep, out);
|
2003-07-08 05:51:32 +04:00
|
|
|
else
|
|
|
|
rv = encodeToACE(strPrep, out);
|
|
|
|
}
|
2003-01-18 02:56:33 +03:00
|
|
|
}
|
|
|
|
|
2003-03-25 23:48:03 +03:00
|
|
|
if (out.Length() > kMaxDNSNodeLen) {
|
2011-06-26 20:28:46 +04:00
|
|
|
NS_WARNING("IDN node too large");
|
2003-03-25 23:48:03 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-01-18 02:56:33 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-03-13 00:31:39 +03:00
|
|
|
// RFC 3490
|
2003-01-18 02:56:33 +03:00
|
|
|
// 1) Whenever dots are used as label separators, the following characters
|
|
|
|
// MUST be recognized as dots: U+002E (full stop), U+3002 (ideographic full
|
|
|
|
// stop), U+FF0E (fullwidth full stop), U+FF61 (halfwidth ideographic full
|
|
|
|
// stop).
|
|
|
|
|
|
|
|
void nsIDNService::normalizeFullStops(nsAString& s)
|
|
|
|
{
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
s.BeginReading(start);
|
|
|
|
s.EndReading(end);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index = 0;
|
2003-01-18 02:56:33 +03:00
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
switch (*start) {
|
|
|
|
case 0x3002:
|
|
|
|
case 0xFF0E:
|
|
|
|
case 0xFF61:
|
|
|
|
s.Replace(index, 1, NS_LITERAL_STRING("."));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
start++;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-29 11:49:31 +04:00
|
|
|
nsresult nsIDNService::decodeACE(const nsACString& in, nsACString& out,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool allowUnassigned)
|
2003-03-26 02:18:41 +03:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isAce;
|
2003-03-26 02:18:41 +03:00
|
|
|
IsACE(in, &isAce);
|
|
|
|
if (!isAce) {
|
|
|
|
out.Assign(in);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 3490 - 4.2 ToUnicode
|
|
|
|
// The ToUnicode output never contains more code points than its input.
|
|
|
|
punycode_uint output_length = in.Length() - kACEPrefixLen + 1;
|
|
|
|
punycode_uint *output = new punycode_uint[output_length];
|
|
|
|
NS_ENSURE_TRUE(output, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
enum punycode_status status = punycode_decode(in.Length() - kACEPrefixLen,
|
|
|
|
PromiseFlatCString(in).get() + kACEPrefixLen,
|
|
|
|
&output_length,
|
|
|
|
output,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr);
|
2003-03-26 02:18:41 +03:00
|
|
|
if (status != punycode_success) {
|
|
|
|
delete [] output;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UCS4 -> UTF8
|
|
|
|
output[output_length] = 0;
|
|
|
|
nsAutoString utf16;
|
|
|
|
ucs4toUtf16(output, utf16);
|
|
|
|
delete [] output;
|
2005-07-22 19:07:33 +04:00
|
|
|
if (!isOnlySafeChars(utf16, mIDNBlacklist))
|
|
|
|
return NS_ERROR_FAILURE;
|
2003-12-23 19:48:40 +03:00
|
|
|
CopyUTF16toUTF8(utf16, out);
|
2003-03-26 02:18:41 +03:00
|
|
|
|
|
|
|
// Validation: encode back to ACE and compare the strings
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString ace;
|
2009-03-29 11:49:31 +04:00
|
|
|
nsresult rv = UTF8toACE(out, ace, allowUnassigned);
|
2003-03-26 02:18:41 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!ace.Equals(in, nsCaseInsensitiveCStringComparator()))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-12-04 09:30:47 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsIDNService::isInWhitelist(const nsACString &host)
|
2007-12-04 09:30:47 +03:00
|
|
|
{
|
|
|
|
if (mIDNWhitelistPrefBranch) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString tld(host);
|
2009-03-29 11:49:31 +04:00
|
|
|
// make sure the host is ACE for lookup and check that there are no
|
|
|
|
// unassigned codepoints
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!IsASCII(tld) && NS_FAILED(UTF8toACE(tld, tld, false))) {
|
|
|
|
return false;
|
2009-03-29 11:49:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// truncate trailing dots first
|
2007-12-04 09:30:47 +03:00
|
|
|
tld.Trim(".");
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t pos = tld.RFind(".");
|
2008-01-29 09:06:01 +03:00
|
|
|
if (pos == kNotFound)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-01-29 09:06:01 +03:00
|
|
|
|
|
|
|
tld.Cut(0, pos + 1);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool safe;
|
2008-01-29 09:06:01 +03:00
|
|
|
if (NS_SUCCEEDED(mIDNWhitelistPrefBranch->GetBoolPref(tld.get(), &safe)))
|
2007-12-04 09:30:47 +03:00
|
|
|
return safe;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-12-04 09:30:47 +03:00
|
|
|
}
|
|
|
|
|