changeset 27470:e90fa379fcec

Validate IPv6 identifiers in the domain portion of a JID.
author Paul Aurich <paul@darkrain42.org>
date Sat, 11 Jul 2009 07:47:07 +0000
parents 8c41a23e6b44
children ef48fb87d8d2
files libpurple/protocols/jabber/chat.c libpurple/protocols/jabber/jutil.c libpurple/protocols/jabber/jutil.h libpurple/tests/test_jabber_jutil.c
diffstat 4 files changed, 24 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/jabber/chat.c	Sat Jul 11 07:32:27 2009 +0000
+++ b/libpurple/protocols/jabber/chat.c	Sat Jul 11 07:47:07 2009 +0000
@@ -235,7 +235,7 @@
 		purple_serv_got_join_chat_failed(gc, data);
 		g_free(buf);
 		return;
-	} else if(!jabber_nameprep_validate(server)) {
+	} else if(!jabber_domain_validate(server)) {
 		char *buf = g_strdup_printf(_("%s is not a valid server name"), server);
 		purple_notify_error(gc, _("Invalid Server Name"),
 				_("Invalid Server Name"), buf);
--- a/libpurple/protocols/jabber/jutil.c	Sat Jul 11 07:32:27 2009 +0000
+++ b/libpurple/protocols/jabber/jutil.c	Sat Jul 11 07:47:07 2009 +0000
@@ -54,7 +54,7 @@
 	return TRUE;
 }
 
-gboolean jabber_nameprep_validate(const char *str)
+gboolean jabber_domain_validate(const char *str)
 {
 	const char *c;
 
@@ -64,11 +64,24 @@
 	if(strlen(str) > 1023)
 		return FALSE;
 
-	/*
-	 * TODO: An IPv6 address of the form [2001:470:1f05:d58::2] is also
-	 * a valid XMPP domain portion.
-	 */
 	c = str;
+
+	if (*c == '[') {
+		/* Check if str is a valid IPv6 identifier */
+		const gchar *end_bracket = strstr(c, "]");
+		gboolean valid = FALSE;
+
+		if (!end_bracket || *(end_bracket + 1) != '\0')
+			return FALSE;
+
+		/* Ugly, but in-place */
+		*(gchar *)end_bracket = '\0';
+		valid = purple_ipv6_address_is_valid(c + 1);
+		*(gchar *)end_bracket = ']';
+
+		return valid;
+	}
+
 	while(c && *c) {
 		gunichar ch = g_utf8_get_char(c);
 		/* The list of characters allowed in domain names is pretty small */
@@ -271,7 +284,7 @@
 
 	/* and finally the jabber nodeprep */
 	if(!jabber_nodeprep_validate(jid->node) ||
-			!jabber_nameprep_validate(jid->domain) ||
+			!jabber_domain_validate(jid->domain) ||
 			!jabber_resourceprep_validate(jid->resource)) {
 		jabber_id_free(jid);
 		return NULL;
--- a/libpurple/protocols/jabber/jutil.h	Sat Jul 11 07:32:27 2009 +0000
+++ b/libpurple/protocols/jabber/jutil.h	Sat Jul 11 07:47:07 2009 +0000
@@ -45,8 +45,7 @@
 gboolean jabber_is_own_account(JabberStream *js, const char *jid);
 
 gboolean jabber_nodeprep_validate(const char *);
-/* TODO: This needs to be named jabber_domain_validate and handle IPv6/IDNA. */
-gboolean jabber_nameprep_validate(const char *);
+gboolean jabber_domain_validate(const char *);
 gboolean jabber_resourceprep_validate(const char *);
 
 PurpleConversation *jabber_find_unnormalized_conv(const char *name, PurpleAccount *account);
--- a/libpurple/tests/test_jabber_jutil.c	Sat Jul 11 07:32:27 2009 +0000
+++ b/libpurple/tests/test_jabber_jutil.c	Sat Jul 11 07:47:07 2009 +0000
@@ -100,11 +100,9 @@
 	assert_valid_jid("mark.doliner@わいど.org");
 	assert_valid_jid("nick@まつ.おおかみ.net");
 	assert_valid_jid("paul@10.0.42.230/s");
-#if 0
-/* Uncomment these when jabber_domain_validate supports IPv6 addresses */
 	assert_valid_jid("paul@[::1]"); /* IPv6 */
 	assert_valid_jid("paul@[2001:470:1f05:d58::2]");
-#endif
+	assert_valid_jid("paul@[2001:470:1f05:d58::2]/foo");
 
 	assert_invalid_jid("@gmail.com");
 	assert_invalid_jid("@@gmail.com");
@@ -117,6 +115,8 @@
 	assert_invalid_jid("mark.doliner@gmail_stuff.org");
 	assert_invalid_jid("mark.doliner@gmail[stuff.org");
 	assert_invalid_jid("mark.doliner@gmail\\stuff.org");
+	assert_invalid_jid("paul@[::1]124");
+	assert_invalid_jid("paul@2[::1]124/as");
 }
 END_TEST