summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/JID')
-rw-r--r--Swiften/JID/JID.cpp138
-rw-r--r--Swiften/JID/JID.h6
-rw-r--r--Swiften/JID/UnitTest/JIDTest.cpp120
3 files changed, 201 insertions, 63 deletions
diff --git a/Swiften/JID/JID.cpp b/Swiften/JID/JID.cpp
index c82674d..eb72014 100644
--- a/Swiften/JID/JID.cpp
+++ b/Swiften/JID/JID.cpp
@@ -1,25 +1,19 @@
/*
- * Copyright (c) 2010-2016 Isode Limited.
+ * Copyright (c) 2010-2018 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
-#define SWIFTEN_CACHE_JID_PREP
-
#include <sstream>
#include <string>
#include <vector>
-#ifdef SWIFTEN_CACHE_JID_PREP
-#include <mutex>
-#include <unordered_map>
-#endif
-
#include <boost/optional.hpp>
#include <Swiften/Base/String.h>
#include <Swiften/IDN/IDNConverter.h>
#include <Swiften/JID/JID.h>
+#include <Swiften/Network/HostAddress.h>
#ifndef SWIFTEN_JID_NO_DEFAULT_IDN_CONVERTER
#include <memory>
@@ -28,15 +22,6 @@
using namespace Swift;
-#ifdef SWIFTEN_CACHE_JID_PREP
-typedef std::unordered_map<std::string, std::string> PrepCache;
-
-static std::mutex namePrepCacheMutex;
-static PrepCache nodePrepCache;
-static PrepCache domainPrepCache;
-static PrepCache resourcePrepCache;
-#endif
-
static const std::vector<char> escapedChars = {' ', '"', '&', '\'', '/', '<', '>', '@', ':'};
static IDNConverter* idnConverter = nullptr;
@@ -87,6 +72,33 @@ JID::JID(const std::string& node, const std::string& domain, const std::string&
nameprepAndSetComponents(node, domain, resource);
}
+JID::JID(const JID& other) {
+ this->operator=(other);
+}
+
+JID::JID(JID&& other) {
+ this->operator=(std::move(other));
+}
+
+JID& JID::operator=(const JID& other) {
+ valid_ = other.valid_;
+ node_ = other.node_;
+ domain_ = other.domain_;
+ hasResource_ = other.hasResource_;
+ resource_ = other.resource_;
+ return *this;
+}
+
+JID& JID::operator=(JID&& other) {
+ valid_ = other.valid_;
+ other.valid_ = false;
+ node_ = std::move(other.node_);
+ domain_ = std::move(other.domain_);
+ hasResource_ = other.hasResource_;
+ resource_ = std::move(other.resource_);
+ return *this;
+}
+
void JID::initializeFromString(const std::string& jid) {
if (String::beginsWith(jid, '@')) {
valid_ = false;
@@ -104,74 +116,76 @@ void JID::initializeFromString(const std::string& jid) {
hasResource_ = false;
bare = jid;
}
- std::pair<std::string,std::string> nodeAndDomain = String::getSplittedAtFirst(bare, '@');
- if (nodeAndDomain.second.empty()) {
- nameprepAndSetComponents("", nodeAndDomain.first, resource);
+ auto firstMatch = bare.find('@');
+ if (firstMatch != bare.npos) {
+ nameprepAndSetComponents(bare.substr(0, firstMatch), bare.substr(firstMatch + 1), resource);
}
else {
- nameprepAndSetComponents(nodeAndDomain.first, nodeAndDomain.second, resource);
+ nameprepAndSetComponents("", bare, resource);
}
}
-
-void JID::nameprepAndSetComponents(const std::string& node, const std::string& domain, const std::string& resource) {
- if (domain.empty() || !idnConverter->getIDNAEncoded(domain)) {
+void JID::setComponents(const std::string& node, const std::string& domain, const std::string& resource) {
+ domain_ = domain;
+ try {
+ node_ = idnConverter->getStringPrepared(node, IDNConverter::XMPPNodePrep);
+ resource_ = idnConverter->getStringPrepared(resource, IDNConverter::XMPPResourcePrep);
+ }
+ catch (...) {
valid_ = false;
return;
}
+}
- if (hasResource_ && resource.empty()) {
+void JID::nameprepAndSetComponents(const std::string& node, const std::string& domain, const std::string& resource) {
+ if (domain.empty() || (hasResource_ && resource.empty())) {
valid_ = false;
return;
}
-#ifndef SWIFTEN_CACHE_JID_PREP
- node_ = idnConverter->getStringPrepared(node, IDNConverter::XMPPNodePrep);
- domain_ = idnConverter->getStringPrepared(domain, IDNConverter::NamePrep);
- resource_ = idnConverter->getStringPrepared(resource, IDNConverter::XMPPResourcePrep);
-#else
- std::unique_lock<std::mutex> lock(namePrepCacheMutex);
-
- std::pair<PrepCache::iterator, bool> r;
-
- r = nodePrepCache.insert(std::make_pair(node, std::string()));
- if (r.second) {
- try {
- r.first->second = idnConverter->getStringPrepared(node, IDNConverter::XMPPNodePrep);
- }
- catch (...) {
- nodePrepCache.erase(r.first);
- valid_ = false;
+
+ // Handling IPv6 addresses according to RFC 3986 rules
+ // saying that they are enclosed in square brackets
+ // which we have to remove when passing to HostAddress
+ if (domain.size() > 2 && domain.front() == '[' && domain.back() == ']') {
+ auto inner = std::string(domain.begin() + 1, domain.end() - 1);
+ auto hostAddress = HostAddress::fromString(inner);
+ if (hostAddress && hostAddress->isValid()) {
+ setComponents(node, domain, resource);
return;
}
}
- node_ = r.first->second;
- r = domainPrepCache.insert(std::make_pair(domain, std::string()));
- if (r.second) {
- try {
- r.first->second = idnConverter->getStringPrepared(domain, IDNConverter::NamePrep);
- }
- catch (...) {
- domainPrepCache.erase(r.first);
- valid_ = false;
+ const auto isAnyOfNonNumericAndNotDot = std::any_of(std::begin(domain), std::end(domain), [](char c) {return !::isdigit(c) && c != '.'; });
+ const auto isDomainAllNumeric = std::all_of(std::begin(domain), std::end(domain), [](char c) {return ::isdigit(c) ; });
+
+ //Prevent Windows validating non-dotted integers as OK if it can unpack them
+ if (!isAnyOfNonNumericAndNotDot && !isDomainAllNumeric) {
+ auto hostAddress = HostAddress::fromString(domain);
+ if (hostAddress && hostAddress->isValid()) {
+ setComponents(node, domain, resource);
return;
}
}
- domain_ = r.first->second;
- r = resourcePrepCache.insert(std::make_pair(resource, std::string()));
- if (r.second) {
- try {
- r.first->second = idnConverter->getStringPrepared(resource, IDNConverter::XMPPResourcePrep);
+ if (!isAnyOfNonNumericAndNotDot || !idnConverter->getIDNAEncoded(domain)) {
+ valid_ = false;
+ return;
+ }
+
+ try {
+ node_ = idnConverter->getStringPrepared(node, IDNConverter::XMPPNodePrep);
+ if (domain.back() == '.') {
+ domain_ = idnConverter->getStringPrepared(domain.substr(0, domain.size() - 1), IDNConverter::NamePrep);
}
- catch (...) {
- resourcePrepCache.erase(r.first);
- valid_ = false;
- return;
+ else {
+ domain_ = idnConverter->getStringPrepared(domain, IDNConverter::NamePrep);
}
+ resource_ = idnConverter->getStringPrepared(resource, IDNConverter::XMPPResourcePrep);
+ }
+ catch (...) {
+ valid_ = false;
+ return;
}
- resource_ = r.first->second;
-#endif
if (domain_.empty()) {
valid_ = false;
diff --git a/Swiften/JID/JID.h b/Swiften/JID/JID.h
index dc92f53..aecc7cb 100644
--- a/Swiften/JID/JID.h
+++ b/Swiften/JID/JID.h
@@ -75,6 +75,11 @@ namespace Swift {
*/
JID(const std::string& node, const std::string& domain, const std::string& resource);
+ JID(const JID& other);
+ JID(JID&& other);
+ JID& operator=(const JID& other);
+ JID& operator=(JID&& other);
+
/**
* @return Is a correctly-formatted JID.
*/
@@ -184,6 +189,7 @@ namespace Swift {
private:
void nameprepAndSetComponents(const std::string& node, const std::string& domain, const std::string& resource);
+ void setComponents(const std::string& node, const std::string& domain, const std::string& resource);
void initializeFromString(const std::string&);
private:
diff --git a/Swiften/JID/UnitTest/JIDTest.cpp b/Swiften/JID/UnitTest/JIDTest.cpp
index ca3e5ae..fc7583f 100644
--- a/Swiften/JID/UnitTest/JIDTest.cpp
+++ b/Swiften/JID/UnitTest/JIDTest.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2016 Isode Limited.
+ * Copyright (c) 2010-2018 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -20,11 +20,20 @@ class JIDTest : public CppUnit::TestFixture
CPPUNIT_TEST(testConstructorWithString_NoNode);
CPPUNIT_TEST(testConstructorWithString_EmptyResource);
CPPUNIT_TEST(testConstructorWithString_OnlyDomain);
+ CPPUNIT_TEST(testConstructorWithString_OnlyDomainWithDot);
+ CPPUNIT_TEST(testConstructorWithString_OnlyDomainDotStrippedOff);
+ CPPUNIT_TEST(testConstructorWithString_InvalidOnlyDomainSingleDot);
CPPUNIT_TEST(testConstructorWithString_InvalidDomain);
+ CPPUNIT_TEST(testConstructorWithString_InvalidDomainOnlyDigits);
+ CPPUNIT_TEST(testConstructorWithString_InvalidDomainEmptyLabel);
CPPUNIT_TEST(testConstructorWithString_UpperCaseNode);
CPPUNIT_TEST(testConstructorWithString_UpperCaseDomain);
CPPUNIT_TEST(testConstructorWithString_UpperCaseResource);
CPPUNIT_TEST(testConstructorWithString_EmptyNode);
+ CPPUNIT_TEST(testConstructorWithString_EmptyDomain);
+ CPPUNIT_TEST(testConstructorWithString_EmptyDomainWithResource);
+ CPPUNIT_TEST(testConstructorWithString_DotDomain);
+ CPPUNIT_TEST(testConstructorWithString_DotDomainWithResource);
CPPUNIT_TEST(testConstructorWithString_IllegalResource);
CPPUNIT_TEST(testConstructorWithString_SpacesInNode);
CPPUNIT_TEST(testConstructorWithStrings);
@@ -62,6 +71,13 @@ class JIDTest : public CppUnit::TestFixture
CPPUNIT_TEST(testGetEscapedNode_BackslashAtEnd);
CPPUNIT_TEST(testGetUnescapedNode);
CPPUNIT_TEST(testGetUnescapedNode_XEP106Examples);
+ CPPUNIT_TEST(testStringPrepFailures);
+ CPPUNIT_TEST(testConstructorWithString_DomainIPv4);
+ CPPUNIT_TEST(testConstructorWithString_DomainNOTIPv4);
+ CPPUNIT_TEST(testConstructorWithString_ValidDomainNOTIPv4);
+ CPPUNIT_TEST(testConstructorWithString_DomainIPv6);
+ CPPUNIT_TEST(testConstructorWithString_DomainInvalidIPv6);
+ CPPUNIT_TEST(testConstructorWithString_DomainIPv6NoBrackets);
CPPUNIT_TEST_SUITE_END();
public:
@@ -119,10 +135,42 @@ class JIDTest : public CppUnit::TestFixture
CPPUNIT_ASSERT(testling.isValid());
}
+ void testConstructorWithString_OnlyDomainWithDot() {
+ JID testling("bar.");
+
+ CPPUNIT_ASSERT_EQUAL(std::string(""), testling.getNode());
+ CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.getDomain());
+ CPPUNIT_ASSERT_EQUAL(std::string(""), testling.getResource());
+ CPPUNIT_ASSERT(testling.isBare());
+ CPPUNIT_ASSERT(testling.isValid());
+ }
+
+ void testConstructorWithString_OnlyDomainDotStrippedOff() {
+ JID testling("foo.@bar./resource.");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("foo."), testling.getNode());
+ CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.getDomain());
+ CPPUNIT_ASSERT_EQUAL(std::string("resource."), testling.getResource());
+ CPPUNIT_ASSERT(!testling.isBare());
+ CPPUNIT_ASSERT(testling.isValid());
+ }
+
+ void testConstructorWithString_InvalidOnlyDomainSingleDot() {
+ CPPUNIT_ASSERT(!JID(".").isValid());
+ }
+
void testConstructorWithString_InvalidDomain() {
CPPUNIT_ASSERT(!JID("foo@bar,baz").isValid());
}
+ void testConstructorWithString_InvalidDomainOnlyDigits() {
+ CPPUNIT_ASSERT(!JID("1234").isValid());
+ }
+
+ void testConstructorWithString_InvalidDomainEmptyLabel() {
+ CPPUNIT_ASSERT(!JID("foo@bar..").isValid());
+ }
+
void testConstructorWithString_UpperCaseNode() {
JID testling("Fo\xCE\xA9@bar");
@@ -151,6 +199,36 @@ class JIDTest : public CppUnit::TestFixture
CPPUNIT_ASSERT(!testling.isValid());
}
+ void testConstructorWithString_EmptyDomain() {
+ JID testling("bar@");
+
+ CPPUNIT_ASSERT(!testling.isValid());
+ }
+
+ void testStringPrepFailures() {
+ CPPUNIT_ASSERT_EQUAL(false, JID("foo@bar", "example.com").isValid());
+ CPPUNIT_ASSERT_EQUAL(false, JID("foo^", "example*com").isValid());
+ CPPUNIT_ASSERT_EQUAL(false, JID("foobar", "example^com").isValid());
+ }
+
+ void testConstructorWithString_EmptyDomainWithResource() {
+ JID testling("bar@/resource");
+
+ CPPUNIT_ASSERT(!testling.isValid());
+ }
+
+ void testConstructorWithString_DotDomain() {
+ JID testling("bar@.");
+
+ CPPUNIT_ASSERT(!testling.isValid());
+ }
+
+ void testConstructorWithString_DotDomainWithResource() {
+ JID testling("bar@./resource");
+
+ CPPUNIT_ASSERT(!testling.isValid());
+ }
+
void testConstructorWithString_IllegalResource() {
JID testling("foo@bar.com/\xd8\xb1\xd9\x85\xd9\x82\xd9\x87\x20\xd8\xaa\xd8\xb1\xd9\x86\xd8\xb3\x20");
@@ -425,6 +503,46 @@ class JIDTest : public CppUnit::TestFixture
CPPUNIT_ASSERT_EQUAL(std::string("c:\\cool stuff"), JID("c\\3a\\cool\\20stuff@example.com").getUnescapedNode());
CPPUNIT_ASSERT_EQUAL(std::string("c:\\5commas"), JID("c\\3a\\5c5commas@example.com").getUnescapedNode());
}
+
+ void testConstructorWithString_DomainIPv4() {
+ JID testling("foo@192.34.12.1/resource");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("foo"), testling.getNode());
+ CPPUNIT_ASSERT_EQUAL(std::string("192.34.12.1"), testling.getDomain());
+ CPPUNIT_ASSERT_EQUAL(std::string("resource"), testling.getResource());
+ CPPUNIT_ASSERT(!testling.isBare());
+ CPPUNIT_ASSERT(testling.isValid());
+ }
+
+ void testConstructorWithString_DomainNOTIPv4() {
+ JID testling("foo@500.34.12.1/resource");
+ CPPUNIT_ASSERT(!testling.isValid());
+ }
+
+ void testConstructorWithString_ValidDomainNOTIPv4() {
+ JID testling("foo@500.34.12.1a/resource");
+ CPPUNIT_ASSERT(testling.isValid());
+ }
+
+ void testConstructorWithString_DomainIPv6() {
+ JID testling("foo@[fe80::a857:33ff:febd:3580]/resource");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("foo"), testling.getNode());
+ CPPUNIT_ASSERT_EQUAL(std::string("[fe80::a857:33ff:febd:3580]"), testling.getDomain());
+ CPPUNIT_ASSERT_EQUAL(std::string("resource"), testling.getResource());
+ CPPUNIT_ASSERT(!testling.isBare());
+ CPPUNIT_ASSERT(testling.isValid());
+ }
+
+ void testConstructorWithString_DomainInvalidIPv6() {
+ JID testling("foo@[1111::a1111:1111:111!:!!!!]/resource");
+ CPPUNIT_ASSERT(!testling.isValid());
+ }
+
+ void testConstructorWithString_DomainIPv6NoBrackets() {
+ JID testling("foo@fe80::a857:33ff:febd:3580/resource");
+ CPPUNIT_ASSERT(!testling.isValid());
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(JIDTest);