diff options
Diffstat (limited to 'Swiften/IDN')
-rw-r--r-- | Swiften/IDN/ICUConverter.cpp | 247 | ||||
-rw-r--r-- | Swiften/IDN/ICUConverter.h | 20 | ||||
-rw-r--r-- | Swiften/IDN/IDNConverter.cpp | 6 | ||||
-rw-r--r-- | Swiften/IDN/IDNConverter.h | 43 | ||||
-rw-r--r-- | Swiften/IDN/LibIDNConverter.cpp | 109 | ||||
-rw-r--r-- | Swiften/IDN/LibIDNConverter.h | 20 | ||||
-rw-r--r-- | Swiften/IDN/PlatformIDNConverter.cpp | 17 | ||||
-rw-r--r-- | Swiften/IDN/PlatformIDNConverter.h | 16 | ||||
-rw-r--r-- | Swiften/IDN/SConscript | 30 | ||||
-rw-r--r-- | Swiften/IDN/UTF8Validator.h | 67 | ||||
-rw-r--r-- | Swiften/IDN/UnitTest/IDNConverterTest.cpp | 118 | ||||
-rw-r--r-- | Swiften/IDN/UnitTest/UTF8ValidatorTest.cpp | 54 |
12 files changed, 465 insertions, 282 deletions
diff --git a/Swiften/IDN/ICUConverter.cpp b/Swiften/IDN/ICUConverter.cpp index 18ff231..37ce708 100644 --- a/Swiften/IDN/ICUConverter.cpp +++ b/Swiften/IDN/ICUConverter.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2012-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swiften/IDN/ICUConverter.h> @@ -19,139 +19,134 @@ using namespace Swift; using boost::numeric_cast; namespace { - typedef std::vector<UChar, SafeAllocator<UChar> > ICUString; - - const char* toConstCharArray(const std::string& input) { - return input.c_str(); - } - - const char* toConstCharArray(const std::vector<unsigned char, SafeAllocator<unsigned char> >& input) { - return reinterpret_cast<const char*>(vecptr(input)); - } - - template<typename T> - ICUString convertToICUString(const T& s) { - ICUString result; - result.resize(s.size()); - UErrorCode status = U_ZERO_ERROR; - int32_t icuResultLength = numeric_cast<int32_t>(result.size()); - u_strFromUTF8Lenient(vecptr(result), numeric_cast<int32_t>(result.size()), &icuResultLength, toConstCharArray(s), numeric_cast<int32_t>(s.size()), &status); - if (status == U_BUFFER_OVERFLOW_ERROR) { - status = U_ZERO_ERROR; - result.resize(numeric_cast<size_t>(icuResultLength)); - u_strFromUTF8Lenient(vecptr(result), numeric_cast<int32_t>(result.size()), &icuResultLength, toConstCharArray(s), numeric_cast<int32_t>(s.size()), &status); - } - if (U_FAILURE(status)) { - return ICUString(); - } - result.resize(numeric_cast<size_t>(icuResultLength)); - return result; - } - - std::vector<char, SafeAllocator<char> > convertToArray(const ICUString& input) { - std::vector<char, SafeAllocator<char> > result; - result.resize(input.size()); - UErrorCode status = U_ZERO_ERROR; - int32_t inputLength = numeric_cast<int32_t>(result.size()); - u_strToUTF8(vecptr(result), numeric_cast<int32_t>(result.size()), &inputLength, vecptr(input), numeric_cast<int32_t>(input.size()), &status); - if (status == U_BUFFER_OVERFLOW_ERROR) { - status = U_ZERO_ERROR; - result.resize(numeric_cast<size_t>(inputLength)); - u_strToUTF8(vecptr(result), numeric_cast<int32_t>(result.size()), &inputLength, vecptr(input), numeric_cast<int32_t>(input.size()), &status); - } - if (U_FAILURE(status)) { - return std::vector<char, SafeAllocator<char> >(); - } - - result.resize(numeric_cast<size_t>(inputLength) + 1); - result[result.size() - 1] = '\0'; - return result; - } - - std::string convertToString(const ICUString& input) { - return std::string(vecptr(convertToArray(input))); - } - - UStringPrepProfileType getICUProfileType(IDNConverter::StringPrepProfile profile) { - switch(profile) { - case IDNConverter::NamePrep: return USPREP_RFC3491_NAMEPREP; - case IDNConverter::XMPPNodePrep: return USPREP_RFC3920_NODEPREP; - case IDNConverter::XMPPResourcePrep: return USPREP_RFC3920_RESOURCEPREP; - case IDNConverter::SASLPrep: return USPREP_RFC4013_SASLPREP; - } - assert(false); - return USPREP_RFC3491_NAMEPREP; - } - - template<typename StringType> - std::vector<char, SafeAllocator<char> > getStringPreparedDetail(const StringType& s, IDNConverter::StringPrepProfile profile) { - UErrorCode status = U_ZERO_ERROR; - - boost::shared_ptr<UStringPrepProfile> icuProfile(usprep_openByType(getICUProfileType(profile), &status), usprep_close); - assert(U_SUCCESS(status)); - - ICUString icuInput = convertToICUString(s); - ICUString icuResult; - UParseError parseError; - icuResult.resize(icuInput.size()); - int32_t icuResultLength = usprep_prepare(icuProfile.get(), vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), USPREP_ALLOW_UNASSIGNED, &parseError, &status); - icuResult.resize(numeric_cast<size_t>(icuResultLength)); - if (status == U_BUFFER_OVERFLOW_ERROR) { - status = U_ZERO_ERROR; - icuResult.resize(numeric_cast<size_t>(icuResultLength)); - icuResultLength = usprep_prepare(icuProfile.get(), vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), USPREP_ALLOW_UNASSIGNED, &parseError, &status); - icuResult.resize(numeric_cast<size_t>(icuResultLength)); - } - if (U_FAILURE(status)) { - return std::vector<char, SafeAllocator<char> >(); - } - icuResult.resize(numeric_cast<size_t>(icuResultLength)); - - return convertToArray(icuResult); - } + static constexpr auto maxStringPrepLength = 1023; + + typedef std::vector<UChar, SafeAllocator<UChar> > ICUString; + + const char* toConstCharArray(const std::string& input) { + return input.c_str(); + } + + const char* toConstCharArray(const std::vector<unsigned char, SafeAllocator<unsigned char> >& input) { + return reinterpret_cast<const char*>(vecptr(input)); + } + + template<typename T> + ICUString convertToICUString(const T& s) { + ICUString result; + result.resize(s.size()); + UErrorCode status = U_ZERO_ERROR; + int32_t icuResultLength = numeric_cast<int32_t>(result.size()); + u_strFromUTF8Lenient(vecptr(result), numeric_cast<int32_t>(result.size()), &icuResultLength, toConstCharArray(s), numeric_cast<int32_t>(s.size()), &status); + if (status == U_BUFFER_OVERFLOW_ERROR) { + status = U_ZERO_ERROR; + result.resize(numeric_cast<size_t>(icuResultLength)); + u_strFromUTF8Lenient(vecptr(result), numeric_cast<int32_t>(result.size()), &icuResultLength, toConstCharArray(s), numeric_cast<int32_t>(s.size()), &status); + } + if (U_FAILURE(status)) { + return ICUString(); + } + result.resize(numeric_cast<size_t>(icuResultLength)); + return result; + } + + std::vector<char, SafeAllocator<char> > convertToArray(const ICUString& input) { + std::vector<char, SafeAllocator<char> > result; + result.resize(input.size()); + UErrorCode status = U_ZERO_ERROR; + int32_t inputLength = numeric_cast<int32_t>(result.size()); + u_strToUTF8(vecptr(result), numeric_cast<int32_t>(result.size()), &inputLength, vecptr(input), numeric_cast<int32_t>(input.size()), &status); + if (status == U_BUFFER_OVERFLOW_ERROR) { + status = U_ZERO_ERROR; + result.resize(numeric_cast<size_t>(inputLength)); + u_strToUTF8(vecptr(result), numeric_cast<int32_t>(result.size()), &inputLength, vecptr(input), numeric_cast<int32_t>(input.size()), &status); + } + if (U_FAILURE(status)) { + return std::vector<char, SafeAllocator<char> >(); + } + + result.resize(numeric_cast<size_t>(inputLength) + 1); + result[result.size() - 1] = '\0'; + return result; + } + + std::string convertToString(const ICUString& input) { + return std::string(vecptr(convertToArray(input))); + } + + UStringPrepProfileType getICUProfileType(IDNConverter::StringPrepProfile profile) { + switch(profile) { + case IDNConverter::NamePrep: return USPREP_RFC3491_NAMEPREP; + case IDNConverter::XMPPNodePrep: return USPREP_RFC3920_NODEPREP; + case IDNConverter::XMPPResourcePrep: return USPREP_RFC3920_RESOURCEPREP; + case IDNConverter::SASLPrep: return USPREP_RFC4013_SASLPREP; + } + assert(false); + return USPREP_RFC3491_NAMEPREP; + } + + template<typename StringType> + std::vector<char, SafeAllocator<char> > getStringPreparedDetail(const StringType& s, IDNConverter::StringPrepProfile profile) { + UErrorCode status = U_ZERO_ERROR; + + std::shared_ptr<UStringPrepProfile> icuProfile(usprep_openByType(getICUProfileType(profile), &status), usprep_close); + assert(U_SUCCESS(status)); + + ICUString icuInput = convertToICUString(s); + ICUString icuResult; + UParseError parseError; + icuResult.resize(maxStringPrepLength); + int32_t icuResultLength = usprep_prepare(icuProfile.get(), vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), USPREP_ALLOW_UNASSIGNED, &parseError, &status); + if (U_FAILURE(status)) { + return std::vector<char, SafeAllocator<char> >(); + } + icuResult.resize(numeric_cast<size_t>(icuResultLength)); + + return convertToArray(icuResult); + } } namespace Swift { std::string ICUConverter::getStringPrepared(const std::string& s, StringPrepProfile profile) { - if (s.empty()) { - return ""; - } - std::vector<char, SafeAllocator<char> > preparedData = getStringPreparedDetail(s, profile); - if (preparedData.empty()) { - throw std::exception(); - } - return std::string(vecptr(preparedData)); + if (s.empty()) { + return ""; + } + std::vector<char, SafeAllocator<char> > preparedData = getStringPreparedDetail(s, profile); + if (preparedData.empty()) { + throw std::exception(); + } + return std::string(vecptr(preparedData)); } SafeByteArray ICUConverter::getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) { - if (s.empty()) { - return SafeByteArray(); - } - std::vector<char, SafeAllocator<char> > preparedData = getStringPreparedDetail(s, profile); - if (preparedData.empty()) { - throw std::exception(); - } - return createSafeByteArray(reinterpret_cast<const char*>(vecptr(preparedData))); + if (s.empty()) { + return SafeByteArray(); + } + std::vector<char, SafeAllocator<char> > preparedData = getStringPreparedDetail(s, profile); + if (preparedData.empty()) { + throw std::exception(); + } + return createSafeByteArray(reinterpret_cast<const char*>(vecptr(preparedData))); } -std::string ICUConverter::getIDNAEncoded(const std::string& domain) { - UErrorCode status = U_ZERO_ERROR; - ICUString icuInput = convertToICUString(domain); - ICUString icuResult; - icuResult.resize(icuInput.size()); - UParseError parseError; - int32_t icuResultLength = uidna_IDNToASCII(vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), UIDNA_DEFAULT, &parseError, &status); - if (status == U_BUFFER_OVERFLOW_ERROR) { - status = U_ZERO_ERROR; - icuResult.resize(numeric_cast<size_t>(icuResultLength)); - icuResultLength = uidna_IDNToASCII(vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), UIDNA_DEFAULT, &parseError, &status); - } - if (U_FAILURE(status)) { - return domain; - } - icuResult.resize(numeric_cast<size_t>(icuResultLength)); - return convertToString(icuResult); +boost::optional<std::string> ICUConverter::getIDNAEncoded(const std::string& domain) { + UErrorCode status = U_ZERO_ERROR; + ICUString icuInput = convertToICUString(domain); + ICUString icuResult; + icuResult.resize(icuInput.size()); + UParseError parseError; + int32_t icuResultLength = uidna_IDNToASCII(vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), UIDNA_USE_STD3_RULES, &parseError, &status); + if (status == U_BUFFER_OVERFLOW_ERROR) { + status = U_ZERO_ERROR; + icuResult.resize(numeric_cast<size_t>(icuResultLength)); + icuResultLength = uidna_IDNToASCII(vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), UIDNA_USE_STD3_RULES, &parseError, &status); + } + if (U_FAILURE(status)) { + return boost::optional<std::string>(); + } + icuResult.resize(numeric_cast<size_t>(icuResultLength)); + return convertToString(icuResult); } } diff --git a/Swiften/IDN/ICUConverter.h b/Swiften/IDN/ICUConverter.h index 8ba9bb5..b0f5d85 100644 --- a/Swiften/IDN/ICUConverter.h +++ b/Swiften/IDN/ICUConverter.h @@ -1,22 +1,22 @@ /* - * Copyright (c) 2012-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <string> + #include <Swiften/Base/API.h> -#include <Swiften/Base/Override.h> #include <Swiften/IDN/IDNConverter.h> namespace Swift { - class SWIFTEN_API ICUConverter : public IDNConverter { - public: - virtual std::string getStringPrepared(const std::string& s, StringPrepProfile profile) SWIFTEN_OVERRIDE; - virtual SafeByteArray getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) SWIFTEN_OVERRIDE; + class SWIFTEN_API ICUConverter : public IDNConverter { + public: + virtual std::string getStringPrepared(const std::string& s, StringPrepProfile profile) override; + virtual SafeByteArray getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) override; - virtual std::string getIDNAEncoded(const std::string& s) SWIFTEN_OVERRIDE; - }; + virtual boost::optional<std::string> getIDNAEncoded(const std::string& s) override; + }; } diff --git a/Swiften/IDN/IDNConverter.cpp b/Swiften/IDN/IDNConverter.cpp index 7705812..b2f575f 100644 --- a/Swiften/IDN/IDNConverter.cpp +++ b/Swiften/IDN/IDNConverter.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2013 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swiften/IDN/IDNConverter.h> diff --git a/Swiften/IDN/IDNConverter.h b/Swiften/IDN/IDNConverter.h index c55d969..27ddd78 100644 --- a/Swiften/IDN/IDNConverter.h +++ b/Swiften/IDN/IDNConverter.h @@ -1,31 +1,34 @@ /* - * Copyright (c) 2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2013-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <string> + +#include <boost/optional.hpp> + #include <Swiften/Base/API.h> #include <Swiften/Base/SafeByteArray.h> namespace Swift { - class SWIFTEN_API IDNConverter { - public: - virtual ~IDNConverter(); - - enum StringPrepProfile { - NamePrep, - XMPPNodePrep, - XMPPResourcePrep, - SASLPrep - }; - - virtual std::string getStringPrepared(const std::string& s, StringPrepProfile profile) = 0; - virtual SafeByteArray getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) = 0; - - // Thread-safe - virtual std::string getIDNAEncoded(const std::string& s) = 0; - }; + class SWIFTEN_API IDNConverter { + public: + virtual ~IDNConverter(); + + enum StringPrepProfile { + NamePrep, + XMPPNodePrep, + XMPPResourcePrep, + SASLPrep + }; + + virtual std::string getStringPrepared(const std::string& s, StringPrepProfile profile) = 0; + virtual SafeByteArray getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) = 0; + + // Thread-safe + virtual boost::optional<std::string> getIDNAEncoded(const std::string& s) = 0; + }; } diff --git a/Swiften/IDN/LibIDNConverter.cpp b/Swiften/IDN/LibIDNConverter.cpp index c4a1c18..e2a87be 100644 --- a/Swiften/IDN/LibIDNConverter.cpp +++ b/Swiften/IDN/LibIDNConverter.cpp @@ -1,80 +1,91 @@ /* - * Copyright (c) 2012-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swiften/IDN/LibIDNConverter.h> extern "C" { - #include <stringprep.h> - #include <idna.h> + #include <stringprep.h> + #include <idna.h> } -#include <vector> #include <cassert> #include <cstdlib> +#include <vector> + +#include <memory> + #include <Swiften/Base/ByteArray.h> #include <Swiften/Base/SafeAllocator.h> -#include <boost/shared_ptr.hpp> +#include <Swiften/IDN/UTF8Validator.h> using namespace Swift; namespace { - static const int MAX_STRINGPREP_SIZE = 1024; + static const size_t MAX_STRINGPREP_SIZE = 1024; + + const Stringprep_profile* getLibIDNProfile(IDNConverter::StringPrepProfile profile) { + switch(profile) { + case IDNConverter::NamePrep: return stringprep_nameprep; + case IDNConverter::XMPPNodePrep: return stringprep_xmpp_nodeprep; + case IDNConverter::XMPPResourcePrep: return stringprep_xmpp_resourceprep; + case IDNConverter::SASLPrep: return stringprep_saslprep; + } + assert(false); + return nullptr; + } - const Stringprep_profile* getLibIDNProfile(IDNConverter::StringPrepProfile profile) { - switch(profile) { - case IDNConverter::NamePrep: return stringprep_nameprep; - case IDNConverter::XMPPNodePrep: return stringprep_xmpp_nodeprep; - case IDNConverter::XMPPResourcePrep: return stringprep_xmpp_resourceprep; - case IDNConverter::SASLPrep: return stringprep_saslprep; - } - assert(false); - return 0; - } + template<typename StringType, typename ContainerType> + ContainerType getStringPreparedInternal(const StringType& s, IDNConverter::StringPrepProfile profile) { + ContainerType input(s.begin(), s.end()); + if (!UTF8IsValid(s.data(), s.size())) { + return ContainerType(); + } - template<typename StringType, typename ContainerType> - ContainerType getStringPreparedInternal(const StringType& s, IDNConverter::StringPrepProfile profile) { - ContainerType input(s.begin(), s.end()); - input.resize(MAX_STRINGPREP_SIZE); - if (stringprep(&input[0], MAX_STRINGPREP_SIZE, static_cast<Stringprep_profile_flags>(0), getLibIDNProfile(profile)) == 0) { - return input; - } - else { - return ContainerType(); - } - } + // Ensure we have enough space for stringprepping, and that input is always NUL terminated + input.resize(std::max(MAX_STRINGPREP_SIZE, input.size() + 1)); + if (stringprep(&input[0], MAX_STRINGPREP_SIZE, static_cast<Stringprep_profile_flags>(0), getLibIDNProfile(profile)) == 0) { + return input; + } + else { + return ContainerType(); + } + } } namespace Swift { std::string LibIDNConverter::getStringPrepared(const std::string& s, StringPrepProfile profile) { - std::vector<char> preparedData = getStringPreparedInternal< std::string, std::vector<char> >(s, profile); - if (preparedData.empty()) { - throw std::exception(); - } - return std::string(vecptr(preparedData)); + std::vector<char> preparedData = getStringPreparedInternal< std::string, std::vector<char> >(s, profile); + if (preparedData.empty()) { + throw std::exception(); + } + return std::string(vecptr(preparedData)); } SafeByteArray LibIDNConverter::getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) { - std::vector<char, SafeAllocator<char> > preparedData = getStringPreparedInternal<SafeByteArray, std::vector<char, SafeAllocator<char> > >(s, profile); - if (preparedData.empty()) { - throw std::exception(); - } - return createSafeByteArray(reinterpret_cast<const char*>(vecptr(preparedData))); + std::vector<char, SafeAllocator<char> > preparedData = getStringPreparedInternal<SafeByteArray, std::vector<char, SafeAllocator<char> > >(s, profile); + if (preparedData.empty()) { + throw std::exception(); + } + return createSafeByteArray(reinterpret_cast<const char*>(vecptr(preparedData))); } -std::string LibIDNConverter::getIDNAEncoded(const std::string& domain) { - char* output; - if (idna_to_ascii_8z(domain.c_str(), &output, 0) == IDNA_SUCCESS) { - std::string result(output); - free(output); - return result; - } - else { - return domain; - } +boost::optional<std::string> LibIDNConverter::getIDNAEncoded(const std::string& domain) { + char* output; + if (idna_to_ascii_8z(domain.c_str(), &output, IDNA_USE_STD3_ASCII_RULES) == IDNA_SUCCESS) { + std::string result(output); + free(output); + if (result.size() > 255) { + return boost::optional<std::string>(); + } + return result; + } + else { + return boost::optional<std::string>(); + } } } diff --git a/Swiften/IDN/LibIDNConverter.h b/Swiften/IDN/LibIDNConverter.h index 23f6bbd..5553ab3 100644 --- a/Swiften/IDN/LibIDNConverter.h +++ b/Swiften/IDN/LibIDNConverter.h @@ -1,23 +1,23 @@ /* - * Copyright (c) 2012-2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <string> + #include <Swiften/Base/API.h> -#include <Swiften/Base/Override.h> #include <Swiften/IDN/IDNConverter.h> namespace Swift { - class SWIFTEN_API LibIDNConverter : public IDNConverter { - public: - virtual std::string getStringPrepared(const std::string& s, StringPrepProfile profile) SWIFTEN_OVERRIDE; - virtual SafeByteArray getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) SWIFTEN_OVERRIDE; + class SWIFTEN_API LibIDNConverter : public IDNConverter { + public: + virtual std::string getStringPrepared(const std::string& s, StringPrepProfile profile) override; + virtual SafeByteArray getStringPrepared(const SafeByteArray& s, StringPrepProfile profile) override; - virtual std::string getIDNAEncoded(const std::string& s) SWIFTEN_OVERRIDE; - }; + virtual boost::optional<std::string> getIDNAEncoded(const std::string& s) override; + }; } diff --git a/Swiften/IDN/PlatformIDNConverter.cpp b/Swiften/IDN/PlatformIDNConverter.cpp index 6d9cff7..3564bb9 100644 --- a/Swiften/IDN/PlatformIDNConverter.cpp +++ b/Swiften/IDN/PlatformIDNConverter.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2012 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swiften/IDN/PlatformIDNConverter.h> @@ -13,14 +13,17 @@ namespace Swift { -IDNConverter* PlatformIDNConverter::create() { +std::unique_ptr<IDNConverter> PlatformIDNConverter::create() { #if defined(HAVE_LIBIDN) - return new LibIDNConverter(); + return std::make_unique<LibIDNConverter>(); #elif defined(HAVE_ICU) - return new ICUConverter(); + return std::make_unique<ICUConverter>(); #else +#if defined(NEED_IDN) #error "No IDN implementation" - return 0; +#else + return {}; +#endif #endif } diff --git a/Swiften/IDN/PlatformIDNConverter.h b/Swiften/IDN/PlatformIDNConverter.h index 4b1025b..3b9a275 100644 --- a/Swiften/IDN/PlatformIDNConverter.h +++ b/Swiften/IDN/PlatformIDNConverter.h @@ -1,17 +1,19 @@ /* - * Copyright (c) 2012 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <Swiften/Base/API.h> +#include <memory> + namespace Swift { - class IDNConverter; + class IDNConverter; - namespace PlatformIDNConverter { - SWIFTEN_API IDNConverter* create(); - } + namespace PlatformIDNConverter { + SWIFTEN_API std::unique_ptr<IDNConverter> create(); + } } diff --git a/Swiften/IDN/SConscript b/Swiften/IDN/SConscript index 9d3b8f9..0afad0e 100644 --- a/Swiften/IDN/SConscript +++ b/Swiften/IDN/SConscript @@ -4,23 +4,29 @@ Import("swiften_env", "env") objects = swiften_env.SwiftenObject(["IDNConverter.cpp"]) myenv = swiften_env.Clone() +if myenv.get("NEED_IDN"): + myenv.Append(CPPDEFINES = ["NEED_IDN"]) if myenv.get("HAVE_ICU") : - myenv.MergeFlags(swiften_env["ICU_FLAGS"]) - myenv.Append(CPPDEFINES = ["HAVE_ICU"]) - objects += myenv.SwiftenObject(["ICUConverter.cpp"]) + myenv.MergeFlags(swiften_env["ICU_FLAGS"]) + myenv.Append(CPPDEFINES = ["HAVE_ICU"]) + objects += myenv.SwiftenObject(["ICUConverter.cpp"]) if myenv.get("HAVE_LIBIDN") : - myenv.MergeFlags(swiften_env["LIBIDN_FLAGS"]) - myenv.Append(CPPDEFINES = ["HAVE_LIBIDN"]) - objects += myenv.SwiftenObject(["LibIDNConverter.cpp"]) -objects += myenv.SwiftenObject(["PlatformIDNConverter.cpp"]) + myenv.MergeFlags(swiften_env["LIBIDN_FLAGS"]) + myenv.Append(CPPDEFINES = ["HAVE_LIBIDN"]) + objects += myenv.SwiftenObject(["LibIDNConverter.cpp"]) +objects += myenv.SwiftenObject([ + "PlatformIDNConverter.cpp" + ]) swiften_env.Append(SWIFTEN_OBJECTS = [objects]) if env["TEST"] : - test_env = myenv.Clone() - test_env.UseFlags(swiften_env["CPPUNIT_FLAGS"]) - env.Append(UNITTEST_OBJECTS = test_env.SwiftenObject([ - File("UnitTest/IDNConverterTest.cpp"), - ])) + test_env = myenv.Clone() + test_env.UseFlags(swiften_env["CPPUNIT_FLAGS"]) + test_env.UseFlags(myenv.get("GOOGLETEST_FLAGS", "")) + env.Append(UNITTEST_OBJECTS = test_env.SwiftenObject([ + File("UnitTest/IDNConverterTest.cpp"), + File("UnitTest/UTF8ValidatorTest.cpp") + ])) diff --git a/Swiften/IDN/UTF8Validator.h b/Swiften/IDN/UTF8Validator.h new file mode 100644 index 0000000..d912ef9 --- /dev/null +++ b/Swiften/IDN/UTF8Validator.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include <cstddef> + +namespace Swift { + +// UTF-8 validation based on the description in https://tools.ietf.org/html/rfc3629#section-3 . +template <typename CharType> +bool UTF8IsValid(const CharType* data, size_t length) { + bool isValid = true; + const CharType* current = data; + const CharType* end = data + length; + while (isValid && (current < end)) { + // one byte sequences + if ((*current & 0x80) == 0x0) { + current++; + continue; + } + // longer byte sequences + else { + // two byte sequences + if ((*current & 0xE0) == 0xC0) { + current++; + if ( (current < end) && ((*current & 0xC0) == 0x80) ) { + current++; + continue; + } + } + // three byte sequences + else if ((*current & 0xF0) == 0xE0) { + current++; + if ( ((current + 1) < end) && ((*current & 0xC0) == 0x80) ) { + current++; + if ((*current & 0xC0) == 0x80) { + current++; + continue; + } + } + } + // four byte sequences + else if ((*current & 0xF8) == 0xF0) { + current++; + if ( ((current + 2) < end) && ((*current & 0xC0) == 0x80) ) { + current++; + if ((*current & 0xC0) == 0x80) { + current++; + if ((*current & 0xC0) == 0x80) { + current++; + continue; + } + } + } + } + // invalid sequences + isValid = false; + } + } + return isValid; +} + +} diff --git a/Swiften/IDN/UnitTest/IDNConverterTest.cpp b/Swiften/IDN/UnitTest/IDNConverterTest.cpp index 285cf4b..77a1ece 100644 --- a/Swiften/IDN/UnitTest/IDNConverterTest.cpp +++ b/Swiften/IDN/UnitTest/IDNConverterTest.cpp @@ -1,56 +1,98 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include <cppunit/extensions/HelperMacros.h> -#include <cppunit/extensions/TestFactoryRegistry.h> +#include <memory> + +#include <gtest/gtest.h> -#include <boost/shared_ptr.hpp> #include <Swiften/IDN/IDNConverter.h> #include <Swiften/IDN/PlatformIDNConverter.h> using namespace Swift; -class IDNConverterTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(IDNConverterTest); - CPPUNIT_TEST(testStringPrep); - CPPUNIT_TEST(testStringPrep_Empty); - CPPUNIT_TEST(testGetEncoded); - CPPUNIT_TEST(testGetEncoded_International); - CPPUNIT_TEST_SUITE_END(); +class IDNConverterTest : public ::testing::Test { + +protected: + virtual void SetUp() { + testling_ = std::shared_ptr<IDNConverter>(PlatformIDNConverter::create()); + } - public: - void setUp() { - testling = boost::shared_ptr<IDNConverter>(PlatformIDNConverter::create()); - } + std::shared_ptr<IDNConverter> testling_; +}; - void testStringPrep() { - std::string result = testling->getStringPrepared("tron\xc3\x87on", IDNConverter::NamePrep); +TEST_F(IDNConverterTest, testStringPrep) { + std::string result = testling_->getStringPrepared("tron\xc3\x87on", IDNConverter::NamePrep); - CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), result); - } + ASSERT_EQ(std::string("tron\xc3\xa7on"), result); +} - void testStringPrep_Empty() { - CPPUNIT_ASSERT_EQUAL(std::string(""), testling->getStringPrepared("", IDNConverter::NamePrep)); - CPPUNIT_ASSERT_EQUAL(std::string(""), testling->getStringPrepared("", IDNConverter::XMPPNodePrep)); - CPPUNIT_ASSERT_EQUAL(std::string(""), testling->getStringPrepared("", IDNConverter::XMPPResourcePrep)); - } +TEST_F(IDNConverterTest, testStringPrep_Empty) { + ASSERT_EQ(std::string(""), testling_->getStringPrepared("", IDNConverter::NamePrep)); + ASSERT_EQ(std::string(""), testling_->getStringPrepared("", IDNConverter::XMPPNodePrep)); + ASSERT_EQ(std::string(""), testling_->getStringPrepared("", IDNConverter::XMPPResourcePrep)); +} - void testGetEncoded() { - std::string result = testling->getIDNAEncoded("www.swift.im"); - CPPUNIT_ASSERT_EQUAL(std::string("www.swift.im"), result); - } +TEST_F(IDNConverterTest, testStringPrep_MaximumOutputSize) { + const std::string input(1023, 'x'); + ASSERT_EQ(input, testling_->getStringPrepared(input, IDNConverter::NamePrep)); + ASSERT_EQ(input, testling_->getStringPrepared(input, IDNConverter::XMPPNodePrep)); + ASSERT_EQ(input, testling_->getStringPrepared(input, IDNConverter::XMPPResourcePrep)); +} - void testGetEncoded_International() { - std::string result = testling->getIDNAEncoded("www.tron\xc3\x87on.com"); - CPPUNIT_ASSERT_EQUAL(std::string("www.xn--tronon-zua.com"), result); - } +TEST_F(IDNConverterTest, testStringPrep_TooLong) { + const std::string input(1024, 'x'); + ASSERT_THROW(testling_->getStringPrepared(input, IDNConverter::NamePrep), std::exception); + ASSERT_THROW(testling_->getStringPrepared(input, IDNConverter::XMPPNodePrep), std::exception); + ASSERT_THROW(testling_->getStringPrepared(input, IDNConverter::XMPPResourcePrep), std::exception); +} +TEST_F(IDNConverterTest, testStringPrep_ShrinkingBelow1023) { + std::string input; + std::string expected; + // The four byte \u03b1\u0313 UTF-8 string will shrink to the three byte \u1f00 + for (auto i = 0; i < 300; ++i) { + input +="\xce\xb1\xcc\x93"; // UTF-8 repesentation of U+03B1 U+0313 + expected += "\xe1\xbc\x80"; // UTF-8 representation of U+1F00 + } + ASSERT_EQ(expected, testling_->getStringPrepared(input, IDNConverter::NamePrep)); + ASSERT_EQ(expected, testling_->getStringPrepared(input, IDNConverter::XMPPNodePrep)); + ASSERT_EQ(expected, testling_->getStringPrepared(input, IDNConverter::XMPPResourcePrep)); +} - private: - boost::shared_ptr<IDNConverter> testling; -}; +TEST_F(IDNConverterTest, testGetEncoded) { + boost::optional<std::string> result = testling_->getIDNAEncoded("www.swift.im"); + ASSERT_TRUE(!!result); + ASSERT_EQ(std::string("www.swift.im"), *result); +} + +TEST_F(IDNConverterTest, testGetEncoded_International) { + boost::optional<std::string> result = testling_->getIDNAEncoded("www.tron\xc3\x87on.com"); + ASSERT_TRUE(result); + ASSERT_EQ(std::string("www.xn--tronon-zua.com"), *result); +} + +TEST_F(IDNConverterTest, testGetEncoded_Invalid) { + boost::optional<std::string> result = testling_->getIDNAEncoded("www.foo,bar.com"); + ASSERT_FALSE(result); +} + +TEST_F(IDNConverterTest, testRFC1035LengthRestrictions) { + // label size check, 63 octets or less + ASSERT_TRUE(testling_->getIDNAEncoded(std::string(63, 'a') + ".example")); + ASSERT_TRUE(testling_->getIDNAEncoded(std::string(63, 'a') + "." + std::string(63, 'a') + ".example")); + ASSERT_FALSE(testling_->getIDNAEncoded(std::string(64, 'a') + "." + std::string(63, 'a') + ".example")); + ASSERT_FALSE(testling_->getIDNAEncoded(std::string(63, 'a') + "." + std::string(64, 'a') + ".example")); + ASSERT_FALSE(testling_->getIDNAEncoded(std::string(0, 'a') + "." + std::string(63, 'a') + ".example")); + ASSERT_FALSE(testling_->getIDNAEncoded(std::string(63, 'a') + "." + std::string(0, 'a') + ".example")); -CPPUNIT_TEST_SUITE_REGISTRATION(IDNConverterTest); + // domain name 255 octets or less + ASSERT_TRUE(testling_->getIDNAEncoded(std::string(63, 'a') + ".example")); + ASSERT_TRUE(testling_->getIDNAEncoded(std::string(63, 'a') + "." + std::string(63, 'a') + ".example")); + ASSERT_TRUE(testling_->getIDNAEncoded(std::string(63, 'a') + "." + std::string(63, 'a') + "." + std::string(63, 'a') + ".example")); + ASSERT_TRUE(testling_->getIDNAEncoded(std::string(63, 'a') + "." + std::string(63, 'a') + "." + std::string(63, 'a') + "." + std::string(55, 'a') + ".example")); + ASSERT_FALSE(testling_->getIDNAEncoded(std::string(63, 'a') + "." + std::string(63, 'a') + "." + std::string(63, 'a') + "." + std::string(56, 'a') + ".example")); + ASSERT_FALSE(testling_->getIDNAEncoded(std::string(63, 'a') + "." + std::string(56, 'a') + "." + std::string(63, 'a') + "." + std::string(63, 'a') + ".example")); +} diff --git a/Swiften/IDN/UnitTest/UTF8ValidatorTest.cpp b/Swiften/IDN/UnitTest/UTF8ValidatorTest.cpp new file mode 100644 index 0000000..6db7770 --- /dev/null +++ b/Swiften/IDN/UnitTest/UTF8ValidatorTest.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include <Swiften/IDN/UTF8Validator.h> + +using namespace Swift; + +class UTF8ValidatorTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(UTF8ValidatorTest); + + CPPUNIT_TEST(testValidUTF8Sequences); + CPPUNIT_TEST(testInvalidUTF8Sequences); + + CPPUNIT_TEST_SUITE_END(); + +public: + void testValidUTF8Sequences() { + { + unsigned char test[] = {0x74, 0x65, 0x73, 0x74}; + CPPUNIT_ASSERT(UTF8IsValid(test, sizeof(test))); + } + + { + unsigned char test[] = {0xf4, 0x8f, 0x80, 0xbf}; + CPPUNIT_ASSERT(UTF8IsValid(test, sizeof(test))); + } + } + + void testInvalidUTF8Sequences() { + { + unsigned char test[] = {0x41, 0xC2, 0x3E, 0x42}; + CPPUNIT_ASSERT(!UTF8IsValid(test, sizeof(test))); + } + + { + unsigned char test[] = {0xf4}; + CPPUNIT_ASSERT(!UTF8IsValid(test, sizeof(test))); + } + + { + unsigned char test[] = {0xf4, 0x8f, 0x65, 0x73, 0x80, 0xbf}; + CPPUNIT_ASSERT(!UTF8IsValid(test, sizeof(test))); + } + } + +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(UTF8ValidatorTest); |