[asterisk-commits] mmichelson: branch group/dns_naptr r433340 - in /team/group/dns_naptr: main/ ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Mar 24 15:02:47 CDT 2015


Author: mmichelson
Date: Tue Mar 24 15:02:46 2015
New Revision: 433340

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=433340
Log:
Add off-nominal services test.

This tests that invalid services in NAPTR records are
rejected.


Modified:
    team/group/dns_naptr/main/dns_naptr.c
    team/group/dns_naptr/tests/test_dns_naptr.c

Modified: team/group/dns_naptr/main/dns_naptr.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_naptr/main/dns_naptr.c?view=diff&rev=433340&r1=433339&r2=433340
==============================================================================
--- team/group/dns_naptr/main/dns_naptr.c (original)
+++ team/group/dns_naptr/main/dns_naptr.c Tue Mar 24 15:02:46 2015
@@ -112,6 +112,50 @@
 	}
 
 	return (!known_flag_found || known_flag_found == 'p') ? FLAGS_UNKNOWN : FLAGS_TERMINAL;
+}
+
+/*!
+ * \brief Analyze NAPTR services for validity as defined by RFC 3404
+ *
+ * \note The services string passed to this function is NOT NULL-terminated
+ * \param services The services string parsed from a NAPTR record
+ * \param services_size The size of the services string
+ * \retval 0 Services are valid
+ * \retval -1 Services are invalid
+ */
+static int services_invalid(const char *services, uint8_t services_size)
+{
+	const char *current_pos = services;
+	uint8_t remaining_size = services_size;
+
+	while (1) {
+		char *plus_pos = memchr(current_pos, '+', remaining_size);
+		uint8_t current_size = plus_pos ? plus_pos - current_pos : remaining_size;
+		int i;
+
+		if (!isalpha(current_pos[0])) {
+			return -1;
+		}
+
+		if (current_size > 32) {
+			return -1;
+		}
+
+		for (i = 1; i < current_size; ++i) {
+			if (!isalnum(current_pos[i])) {
+				return -1;
+			}
+		}
+
+		remaining_size -= current_size;
+		if (!remaining_size) {
+			break;
+		}
+
+		current_pos = plus_pos + 1;
+	}
+
+	return 0;
 }
 
 struct ast_dns_record *ast_dns_naptr_alloc(struct ast_dns_query *query, const char *data, const size_t size)
@@ -246,6 +290,11 @@
 		return NULL;
 	}
 
+	if (services_invalid(services, services_size)) {
+		ast_log(LOG_ERROR, "NAPTR record contained invalid services %.*s\n", services_size, services);
+		return NULL;
+	}
+
 	naptr = ast_calloc(1, sizeof(*naptr) + size + flags_size + 1 + services_size + 1 + regexp_size + 1 + replacement_size + 1);
 	if (!naptr) {
 		return NULL;

Modified: team/group/dns_naptr/tests/test_dns_naptr.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_naptr/tests/test_dns_naptr.c?view=diff&rev=433340&r1=433339&r2=433340
==============================================================================
--- team/group/dns_naptr/tests/test_dns_naptr.c (original)
+++ team/group/dns_naptr/tests/test_dns_naptr.c Tue Mar 24 15:02:46 2015
@@ -407,9 +407,10 @@
 {
 	struct naptr_record records[] = {
 		/* Non-alphanumeric flag */
-		{ 100, 100, {1, "\x0a"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {1, "!"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
 		/* Mix of valid and non-alphanumeric */
-		{ 100, 100, {2, "A\x0a"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {2, "A!"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {2, "!A"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
 		/* Invalid combinations of flags */
 		{ 100, 100, {2, "sa"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
 		{ 100, 100, {2, "su"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
@@ -442,11 +443,43 @@
 	return off_nominal_test(test, records, ARRAY_LEN(records));
 }
 
+AST_TEST_DEFINE(naptr_resolve_off_nominal_services)
+{
+	struct naptr_record records[] = {
+		{ 100, 100, {1, "A"}, {5, "BLAH!"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {1, "A"}, {5, "BL!AH"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {1, "A"}, {8, "1SIP+D2U"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {1, "A"}, {8, "SIP+1D2U"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {1, "A"}, {4, "+D2U"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {1, "A"}, {4, "SIP+"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {1, "A"}, {8, "SIP++D2U"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {1, "A"}, {37, "SIPSIPSIPSIPSIPSIPSIPSIPSIPSIPSIP+D2U"}, {15, "!.*!horse.mane!"}, ""},
+		{ 100, 100, {1, "A"}, {37, "SIP+D2UD2UD2UD2UD2UD2UD2UD2UD2UD2UD2U"}, {15, "!.*!horse.mane!"}, ""},
+	};
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "naptr_resolve_off_nominal_services";
+		info->category = "/main/dns/naptr/";
+		info->summary = "Ensure that NAPTR records with invalid services are not presented in results";
+		info->description = "This test defines a set of records where the services provided are\n"
+			"invalid in some way. This may be due to providing non-alphanumeric characters, providing\n"
+			"protocols or resolution services that start with a non-alphabetic character, or\n"
+			"providing fields that are too long.\n";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	return off_nominal_test(test, records, ARRAY_LEN(records));
+}
+
 static int unload_module(void)
 {
 	AST_TEST_UNREGISTER(naptr_resolve_nominal);
 	AST_TEST_UNREGISTER(naptr_resolve_off_nominal_length);
 	AST_TEST_UNREGISTER(naptr_resolve_off_nominal_flags);
+	AST_TEST_UNREGISTER(naptr_resolve_off_nominal_services);
 
 	return 0;
 }
@@ -456,6 +489,7 @@
 	AST_TEST_REGISTER(naptr_resolve_nominal);
 	AST_TEST_REGISTER(naptr_resolve_off_nominal_length);
 	AST_TEST_REGISTER(naptr_resolve_off_nominal_flags);
+	AST_TEST_REGISTER(naptr_resolve_off_nominal_services);
 
 	return AST_MODULE_LOAD_SUCCESS;
 }




More information about the asterisk-commits mailing list