changeset 24205:9340e1db0f46

Prevent DNS-SD TXT record strings from exceeding the maximum length. Fixes #7242
author Daniel Atallah <daniel.atallah@gmail.com>
date Tue, 30 Sep 2008 00:49:53 +0000
parents 23cffa95216d
children f54e7398f733 fb722b8b4c74
files libpurple/protocols/bonjour/mdns_common.c
diffstat 1 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/bonjour/mdns_common.c	Mon Sep 29 17:17:38 2008 +0000
+++ b/libpurple/protocols/bonjour/mdns_common.c	Tue Sep 30 00:49:53 2008 +0000
@@ -46,6 +46,30 @@
 	g_free(data);
 }
 
+#define MAX_TXT_CONSTITUENT_LEN 255
+
+/* Make sure that the value isn't longer than it is supposed to be */
+static const char*
+get_max_txt_record_value(const char *key, const char *value)
+{
+	/* "each constituent string of a DNS TXT record is limited to 255 bytes"
+	 * This includes the key and the '='
+	 */
+	static char buffer[MAX_TXT_CONSTITUENT_LEN + 1];
+	gchar *end_valid = NULL;
+	int len = MIN(strlen(value), MAX_TXT_CONSTITUENT_LEN - (strlen(key) + 2));
+
+	strncpy(buffer, value, len);
+
+	buffer[len] = '\0';
+
+	/* If we've cut part of a utf-8 character, kill it */
+	if (!g_utf8_validate(buffer, -1, (const gchar **)&end_valid))
+		*end_valid = '\0';
+
+	return buffer;
+}
+
 static GSList *generate_presence_txt_records(BonjourDnsSd *data) {
 	GSList *ret = NULL;
 	PurpleKeyValuePair *kvp;
@@ -62,13 +86,20 @@
 #define _M_ADD_R(k, v) \
 	kvp = g_new0(PurpleKeyValuePair, 1); \
 	kvp->key = g_strdup(k); \
-	kvp->value = g_strdup(v); \
+	kvp->value = g_strdup(get_max_txt_record_value(k, v)); \
 	ret = g_slist_prepend(ret, kvp); \
 
 	/* We should try to follow XEP-0174, but some clients have "issues", so we humor them.
 	 * See http://telepathy.freedesktop.org/wiki/SalutInteroperability
 	 */
 
+	/* Large TXT records are problematic.
+	 * While it is technically possible for this to exceed a standard 512-byte
+	 * DNS message, it shouldn't happen unless we get wacky data entered for
+	 * some of the freeform fields.  It is even less likely to exceed the
+	 * recommended maximum of 1300 bytes.
+	 */
+
 	/* Needed by iChat */
 	_M_ADD_R("txtvers", "1")
 	/* Needed by Gaim/Pidgin <= 2.0.1 (remove at some point) */