Skip to content

Commit 4ad7265

Browse files
authored
CDRIVER-4437 Improve quality of SRV URI parse error messages (#1076)
1 parent e05c317 commit 4ad7265

File tree

2 files changed

+135
-86
lines changed

2 files changed

+135
-86
lines changed

src/libmongoc/src/mongoc/mongoc-uri.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -442,32 +442,39 @@ mongoc_uri_parse_host (mongoc_uri_t *uri, const char *host_and_port_in)
442442
}
443443

444444

445-
bool
446-
mongoc_uri_parse_srv (mongoc_uri_t *uri, const char *str)
445+
static bool
446+
mongoc_uri_parse_srv (mongoc_uri_t *uri, const char *str, bson_error_t *error)
447447
{
448-
char *service;
449-
450448
if (*str == '\0') {
449+
MONGOC_URI_ERROR (error, "%s", "Missing service name in SRV URI");
451450
return false;
452451
}
453452

454-
service = bson_strdup (str);
455-
mongoc_uri_do_unescape (&service);
456-
if (!service) {
457-
/* invalid */
458-
return false;
459-
}
453+
{
454+
char *service = bson_strdup (str);
455+
456+
mongoc_uri_do_unescape (&service);
457+
458+
if (!service || !valid_hostname (service) || count_dots (service) < 2) {
459+
MONGOC_URI_ERROR (error, "%s", "Invalid service name in URI");
460+
bson_free (service);
461+
return false;
462+
}
463+
464+
bson_strncpy (uri->srv, service, sizeof uri->srv);
460465

461-
if (!valid_hostname (service) || count_dots (service) < 2) {
462466
bson_free (service);
463-
return false;
464467
}
465468

466-
bson_strncpy (uri->srv, service, sizeof uri->srv);
467-
bson_free (service);
469+
if (strchr (uri->srv, ',')) {
470+
MONGOC_URI_ERROR (
471+
error, "%s", "Multiple service names are prohibited in an SRV URI");
472+
return false;
473+
}
468474

469-
if (strchr (uri->srv, ',') || strchr (uri->srv, ':')) {
470-
/* prohibit port number or multiple service names */
475+
if (strchr (uri->srv, ':')) {
476+
MONGOC_URI_ERROR (
477+
error, "%s", "Port numbers are prohibited in an SRV URI");
471478
return false;
472479
}
473480

@@ -1543,8 +1550,7 @@ mongoc_uri_parse_before_slash (mongoc_uri_t *uri,
15431550
}
15441551

15451552
if (uri->is_srv) {
1546-
if (!mongoc_uri_parse_srv (uri, hosts)) {
1547-
MONGOC_URI_ERROR (error, "%s", "Invalid service name in URI");
1553+
if (!mongoc_uri_parse_srv (uri, hosts, error)) {
15481554
goto error;
15491555
}
15501556
} else {

src/libmongoc/tests/test-mongoc-uri.c

Lines changed: 111 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,49 @@ test_mongoc_uri_new_with_error (void)
807807
MONGOC_ERROR_COMMAND,
808808
MONGOC_ERROR_COMMAND_INVALID_ARG,
809809
"Invalid \"zlibcompressionlevel\" of 10: must be between -1 and 9");
810+
811+
memset (&error, 0, sizeof (bson_error_t));
812+
ASSERT (!mongoc_uri_new_with_error ("mongodb+srv://", &error));
813+
ASSERT_ERROR_CONTAINS (error,
814+
MONGOC_ERROR_COMMAND,
815+
MONGOC_ERROR_COMMAND_INVALID_ARG,
816+
"Missing service name in SRV URI");
817+
818+
memset (&error, 0, sizeof (bson_error_t));
819+
ASSERT (!mongoc_uri_new_with_error ("mongodb+srv://%", &error));
820+
ASSERT_ERROR_CONTAINS (error,
821+
MONGOC_ERROR_COMMAND,
822+
MONGOC_ERROR_COMMAND_INVALID_ARG,
823+
"Invalid service name in URI");
824+
825+
memset (&error, 0, sizeof (bson_error_t));
826+
ASSERT (!mongoc_uri_new_with_error ("mongodb+srv://x", &error));
827+
ASSERT_ERROR_CONTAINS (error,
828+
MONGOC_ERROR_COMMAND,
829+
MONGOC_ERROR_COMMAND_INVALID_ARG,
830+
"Invalid service name in URI");
831+
832+
memset (&error, 0, sizeof (bson_error_t));
833+
ASSERT (!mongoc_uri_new_with_error ("mongodb+srv://x.y", &error));
834+
ASSERT_ERROR_CONTAINS (error,
835+
MONGOC_ERROR_COMMAND,
836+
MONGOC_ERROR_COMMAND_INVALID_ARG,
837+
"Invalid service name in URI");
838+
839+
memset (&error, 0, sizeof (bson_error_t));
840+
ASSERT (!mongoc_uri_new_with_error ("mongodb+srv://a.b.c,d.e.f", &error));
841+
ASSERT_ERROR_CONTAINS (
842+
error,
843+
MONGOC_ERROR_COMMAND,
844+
MONGOC_ERROR_COMMAND_INVALID_ARG,
845+
"Multiple service names are prohibited in an SRV URI");
846+
847+
memset (&error, 0, sizeof (bson_error_t));
848+
ASSERT (!mongoc_uri_new_with_error ("mongodb+srv://a.b.c:8000", &error));
849+
ASSERT_ERROR_CONTAINS (error,
850+
MONGOC_ERROR_COMMAND,
851+
MONGOC_ERROR_COMMAND_INVALID_ARG,
852+
"Port numbers are prohibited in an SRV URI");
810853
}
811854

812855

@@ -1576,17 +1619,17 @@ test_mongoc_uri_tls_ssl (const char *tls,
15761619
bson_error_t err;
15771620

15781621
bson_snprintf (url_buffer,
1579-
sizeof (url_buffer),
1580-
"mongodb://CN=client,OU=kerneluser,O=10Gen,L=New York City,"
1581-
"ST=New York,[email protected]/?"
1582-
"%s=true&authMechanism=MONGODB-X509&"
1583-
"%s=tests/x509gen/legacy-x509.pem&"
1584-
"%s=tests/x509gen/legacy-ca.crt&"
1585-
"%s=true",
1586-
tls,
1587-
tlsCertificateKeyFile,
1588-
tlsCAFile,
1589-
tlsAllowInvalidHostnames);
1622+
sizeof (url_buffer),
1623+
"mongodb://CN=client,OU=kerneluser,O=10Gen,L=New York City,"
1624+
"ST=New York,[email protected]/?"
1625+
"%s=true&authMechanism=MONGODB-X509&"
1626+
"%s=tests/x509gen/legacy-x509.pem&"
1627+
"%s=tests/x509gen/legacy-ca.crt&"
1628+
"%s=true",
1629+
tls,
1630+
tlsCertificateKeyFile,
1631+
tlsCAFile,
1632+
tlsAllowInvalidHostnames);
15901633
uri = mongoc_uri_new (url_buffer);
15911634

15921635
ASSERT_CMPSTR (
@@ -1614,11 +1657,11 @@ test_mongoc_uri_tls_ssl (const char *tls,
16141657

16151658

16161659
bson_snprintf (url_buffer,
1617-
sizeof (url_buffer),
1618-
"mongodb://localhost/?%s=true&%s=key.pem&%s=ca.pem",
1619-
tls,
1620-
tlsCertificateKeyFile,
1621-
tlsCAFile);
1660+
sizeof (url_buffer),
1661+
"mongodb://localhost/?%s=true&%s=key.pem&%s=ca.pem",
1662+
tls,
1663+
tlsCertificateKeyFile,
1664+
tlsCAFile);
16221665
uri = mongoc_uri_new (url_buffer);
16231666

16241667
ASSERT_CMPSTR (mongoc_uri_get_option_as_utf8 (
@@ -1667,11 +1710,11 @@ test_mongoc_uri_tls_ssl (const char *tls,
16671710

16681711

16691712
bson_snprintf (url_buffer,
1670-
sizeof (url_buffer),
1671-
"mongodb://localhost/?%s=true&%s=pa$$word!&%s=encrypted.pem",
1672-
tls,
1673-
tlsCertificateKeyPassword,
1674-
tlsCertificateKeyFile);
1713+
sizeof (url_buffer),
1714+
"mongodb://localhost/?%s=true&%s=pa$$word!&%s=encrypted.pem",
1715+
tls,
1716+
tlsCertificateKeyPassword,
1717+
tlsCertificateKeyFile);
16751718
uri = mongoc_uri_new (url_buffer);
16761719

16771720
ASSERT_CMPSTR (mongoc_uri_get_option_as_utf8 (
@@ -1700,10 +1743,10 @@ test_mongoc_uri_tls_ssl (const char *tls,
17001743

17011744

17021745
bson_snprintf (url_buffer,
1703-
sizeof (url_buffer),
1704-
"mongodb://localhost/?%s=true&%s=true",
1705-
tls,
1706-
tlsAllowInvalidCertificates);
1746+
sizeof (url_buffer),
1747+
"mongodb://localhost/?%s=true&%s=true",
1748+
tls,
1749+
tlsAllowInvalidCertificates);
17071750
uri = mongoc_uri_new (url_buffer);
17081751

17091752
ASSERT_CMPSTR (mongoc_uri_get_option_as_utf8 (
@@ -1727,29 +1770,29 @@ test_mongoc_uri_tls_ssl (const char *tls,
17271770

17281771

17291772
bson_snprintf (url_buffer,
1730-
sizeof (url_buffer),
1731-
"mongodb://localhost/?%s=foo.pem",
1732-
tlsCertificateKeyFile);
1773+
sizeof (url_buffer),
1774+
"mongodb://localhost/?%s=foo.pem",
1775+
tlsCertificateKeyFile);
17331776
uri = mongoc_uri_new (url_buffer);
17341777
ASSERT (mongoc_uri_get_ssl (uri));
17351778
ASSERT (mongoc_uri_get_tls (uri));
17361779
mongoc_uri_destroy (uri);
17371780

17381781

17391782
bson_snprintf (url_buffer,
1740-
sizeof (url_buffer),
1741-
"mongodb://localhost/?%s=foo.pem",
1742-
tlsCAFile);
1783+
sizeof (url_buffer),
1784+
"mongodb://localhost/?%s=foo.pem",
1785+
tlsCAFile);
17431786
uri = mongoc_uri_new (url_buffer);
17441787
ASSERT (mongoc_uri_get_ssl (uri));
17451788
ASSERT (mongoc_uri_get_tls (uri));
17461789
mongoc_uri_destroy (uri);
17471790

17481791

17491792
bson_snprintf (url_buffer,
1750-
sizeof (url_buffer),
1751-
"mongodb://localhost/?%s=true",
1752-
tlsAllowInvalidCertificates);
1793+
sizeof (url_buffer),
1794+
"mongodb://localhost/?%s=true",
1795+
tlsAllowInvalidCertificates);
17531796
uri = mongoc_uri_new (url_buffer);
17541797
ASSERT (mongoc_uri_get_ssl (uri));
17551798
ASSERT (mongoc_uri_get_tls (uri));
@@ -1761,9 +1804,9 @@ test_mongoc_uri_tls_ssl (const char *tls,
17611804

17621805

17631806
bson_snprintf (url_buffer,
1764-
sizeof (url_buffer),
1765-
"mongodb://localhost/?%s=true",
1766-
tlsAllowInvalidHostnames);
1807+
sizeof (url_buffer),
1808+
"mongodb://localhost/?%s=true",
1809+
tlsAllowInvalidHostnames);
17671810
uri = mongoc_uri_new (url_buffer);
17681811
ASSERT (mongoc_uri_get_ssl (uri));
17691812
ASSERT (mongoc_uri_get_tls (uri));
@@ -1775,32 +1818,32 @@ test_mongoc_uri_tls_ssl (const char *tls,
17751818

17761819

17771820
bson_snprintf (url_buffer,
1778-
sizeof (url_buffer),
1779-
"mongodb://localhost/?%s=false&%s=foo.pem",
1780-
tls,
1781-
tlsCertificateKeyFile);
1821+
sizeof (url_buffer),
1822+
"mongodb://localhost/?%s=false&%s=foo.pem",
1823+
tls,
1824+
tlsCertificateKeyFile);
17821825
uri = mongoc_uri_new (url_buffer);
17831826
ASSERT (!mongoc_uri_get_ssl (uri));
17841827
ASSERT (!mongoc_uri_get_tls (uri));
17851828
mongoc_uri_destroy (uri);
17861829

17871830

17881831
bson_snprintf (url_buffer,
1789-
sizeof (url_buffer),
1790-
"mongodb://localhost/?%s=false&%s=foo.pem",
1791-
tls,
1792-
tlsCertificateKeyFile);
1832+
sizeof (url_buffer),
1833+
"mongodb://localhost/?%s=false&%s=foo.pem",
1834+
tls,
1835+
tlsCertificateKeyFile);
17931836
uri = mongoc_uri_new (url_buffer);
17941837
ASSERT (!mongoc_uri_get_ssl (uri));
17951838
ASSERT (!mongoc_uri_get_tls (uri));
17961839
mongoc_uri_destroy (uri);
17971840

17981841

17991842
bson_snprintf (url_buffer,
1800-
sizeof (url_buffer),
1801-
"mongodb://localhost/?%s=false&%s=true",
1802-
tls,
1803-
tlsAllowInvalidCertificates);
1843+
sizeof (url_buffer),
1844+
"mongodb://localhost/?%s=false&%s=true",
1845+
tls,
1846+
tlsAllowInvalidCertificates);
18041847
uri = mongoc_uri_new (url_buffer);
18051848
ASSERT (!mongoc_uri_get_ssl (uri));
18061849
ASSERT (!mongoc_uri_get_tls (uri));
@@ -1812,10 +1855,10 @@ test_mongoc_uri_tls_ssl (const char *tls,
18121855

18131856

18141857
bson_snprintf (url_buffer,
1815-
sizeof (url_buffer),
1816-
"mongodb://localhost/?%s=false&%s=false",
1817-
tls,
1818-
tlsAllowInvalidHostnames);
1858+
sizeof (url_buffer),
1859+
"mongodb://localhost/?%s=false&%s=false",
1860+
tls,
1861+
tlsAllowInvalidHostnames);
18191862
uri = mongoc_uri_new (url_buffer);
18201863
ASSERT (!mongoc_uri_get_ssl (uri));
18211864
ASSERT (!mongoc_uri_get_tls (uri));
@@ -1834,10 +1877,10 @@ test_mongoc_uri_tls_ssl (const char *tls,
18341877
/* Mixing options okay so long as they match */
18351878
capture_logs (true);
18361879
bson_snprintf (url_buffer,
1837-
sizeof (url_buffer),
1838-
"mongodb://localhost/?%s=true&%s=true",
1839-
tls,
1840-
tlsalt);
1880+
sizeof (url_buffer),
1881+
"mongodb://localhost/?%s=true&%s=true",
1882+
tls,
1883+
tlsalt);
18411884
uri = mongoc_uri_new (url_buffer);
18421885
ASSERT (mongoc_uri_get_option_as_bool (uri, tls, false));
18431886
ASSERT_NO_CAPTURED_LOGS (url_buffer);
@@ -1846,10 +1889,10 @@ test_mongoc_uri_tls_ssl (const char *tls,
18461889
/* Same option with different values okay, latter overrides */
18471890
capture_logs (true);
18481891
bson_snprintf (url_buffer,
1849-
sizeof (url_buffer),
1850-
"mongodb://localhost/?%s=true&%s=false",
1851-
tls,
1852-
tls);
1892+
sizeof (url_buffer),
1893+
"mongodb://localhost/?%s=true&%s=false",
1894+
tls,
1895+
tls);
18531896
uri = mongoc_uri_new (url_buffer);
18541897
ASSERT (!mongoc_uri_get_option_as_bool (uri, tls, true));
18551898
if (strcmp (tls, "tls")) {
@@ -1866,10 +1909,10 @@ test_mongoc_uri_tls_ssl (const char *tls,
18661909
/* Mixing options not okay if values differ */
18671910
capture_logs (false);
18681911
bson_snprintf (url_buffer,
1869-
sizeof (url_buffer),
1870-
"mongodb://localhost/?%s=true&%s=false",
1871-
tls,
1872-
tlsalt);
1912+
sizeof (url_buffer),
1913+
"mongodb://localhost/?%s=true&%s=false",
1914+
tls,
1915+
tlsalt);
18731916
uri = mongoc_uri_new_with_error (url_buffer, &err);
18741917
if (strcmp (tls, "tls")) {
18751918
ASSERT_ERROR_CONTAINS (err,
@@ -1889,9 +1932,9 @@ test_mongoc_uri_tls_ssl (const char *tls,
18891932
/* No conflict appears with implicit tls=true via SRV */
18901933
capture_logs (false);
18911934
bson_snprintf (url_buffer,
1892-
sizeof (url_buffer),
1893-
"mongodb+srv://a.b.c/?%s=foo.pem",
1894-
tlsCAFile);
1935+
sizeof (url_buffer),
1936+
"mongodb+srv://a.b.c/?%s=foo.pem",
1937+
tlsCAFile);
18951938
uri = mongoc_uri_new (url_buffer);
18961939
ASSERT (mongoc_uri_get_option_as_bool (uri, tls, false));
18971940
mongoc_uri_destroy (uri);

0 commit comments

Comments
 (0)