Skip to content

PHPC-1774 Fix truncation of PHP_VERSION constant in handshake metadata #1202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
#define PHONGO_DEBUG_INI_DEFAULT ""
#define PHONGO_METADATA_SEPARATOR " / "
#define PHONGO_METADATA_SEPARATOR_LEN (sizeof(PHONGO_METADATA_SEPARATOR) - 1)
#define PHONGO_METADATA_PHP_VERSION_PREFIX "PHP "
#define PHONGO_METADATA_PHP_VERSION_PREFIX_LEN (sizeof(PHONGO_METADATA_PHP_VERSION_PREFIX) - 1)

ZEND_DECLARE_MODULE_GLOBALS(mongodb)
#if defined(ZTS) && defined(COMPILE_DL_MONGODB)
Expand Down Expand Up @@ -2565,20 +2567,20 @@ static bool php_phongo_extract_handshake_data(zval* driver, const char* key, cha
static char* php_phongo_concat_handshake_data(const char* default_value, const char* custom_value, size_t custom_value_len)
{
char* ret;
/* Length of the returned value needs to include the trailing null byte */
size_t ret_len = strlen(default_value) + 1;
/* Length of the returned value needs to include a trailing space and null byte */
size_t ret_len = strlen(default_value) + 2;

if (custom_value) {
/* Increase the length by that of the custom value as well as one byte for the separator */
/* Increase the length by that of the custom value as well as the separator length */
ret_len += custom_value_len + PHONGO_METADATA_SEPARATOR_LEN;
}

ret = ecalloc(sizeof(char*), ret_len);

if (custom_value) {
snprintf(ret, ret_len, "%s%s%s", default_value, PHONGO_METADATA_SEPARATOR, custom_value);
snprintf(ret, ret_len, "%s%s%s ", default_value, PHONGO_METADATA_SEPARATOR, custom_value);
} else {
snprintf(ret, ret_len, "%s", default_value);
snprintf(ret, ret_len, "%s ", default_value);
}

return ret;
Expand All @@ -2592,16 +2594,16 @@ static void php_phongo_handshake_data_append(const char* name, size_t name_len,
char* driver_version;
char* full_platform;

php_version_string_len = strlen(PHP_VERSION);
php_version_string = ecalloc(sizeof(char*), 4 + php_version_string_len);
snprintf(php_version_string, 4 + php_version_string_len, "PHP %s", PHP_VERSION);
php_version_string_len = strlen(PHP_VERSION) + PHONGO_METADATA_PHP_VERSION_PREFIX_LEN + 1;
php_version_string = ecalloc(sizeof(char*), php_version_string_len);
snprintf(php_version_string, php_version_string_len, "%s%s", PHONGO_METADATA_PHP_VERSION_PREFIX, PHP_VERSION);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're still missing the space that goes after the PHONGO_METADATA_PHP_VERSION_PREFIX + PHP_VERSION combo.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are indeed correct. This is an issue in libmongoc which seems to append platform data without a separating space. I'm working on a fix for that, but for the time being this PR will mitigate the issue.


driver_name = php_phongo_concat_handshake_data("ext-mongodb:PHP", name, name_len);
driver_version = php_phongo_concat_handshake_data(PHP_MONGODB_VERSION, version, version_len);
full_platform = php_phongo_concat_handshake_data(php_version_string, platform, platform_len);

MONGOC_DEBUG(
"Setting driver handshake data: name %s, version %s, platform %s",
"Setting driver handshake data: { name: '%s', version: '%s', platform: '%s' }",
driver_name,
driver_version,
full_platform);
Expand Down
12 changes: 10 additions & 2 deletions tests/manager/manager-ctor-driver-metadata-001.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
--TEST--
MongoDB\Driver\Manager: Pass custom handshake data
--DESCRIPTION--
This test matches spaces at the end of the handshake data it appends. The only
way to see final output is by checking against the binary socket communication:
[2021-02-17T13:57:57.155166+00:00] socket: TRACE > 00100: 66 6f 72 6d 00 76 00 00 00 50 48 50 20 37 2e 34 f o r m . v . . . P H P 7 . 4
[2021-02-17T13:57:57.155182+00:00] socket: TRACE > 00110: 2e 31 35 20 2f 20 6d 69 6e 65 20 63 66 67 3d 30 . 1 5 / m i n e c f g = 0

Since matching this is not trivial, we're happy matching the trailing space at
the end of each handshake data item.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trailing space will ultimately be removed once you address a related ticket in libmongoc, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can be removed once we fix that.

--INI--
mongodb.debug=stderr
--FILE--
Expand All @@ -12,6 +20,6 @@ $manager = new MongoDB\Driver\Manager(null, [], ['driver' => ['name' => 'test']]
===DONE===
<?php exit(0); ?>
--EXPECTF--
%A[%s] PHONGO: DEBUG > Setting driver handshake data: name ext-mongodb:PHP / test, version %s / 0.1, platform PHP %s / mine
%A[%s] PHONGO: DEBUG > Setting driver handshake data: name ext-mongodb:PHP / test, version %s, platform PHP %s
%A[%s] PHONGO: DEBUG > Setting driver handshake data: { name: 'ext-mongodb:PHP / test ', version: '%s / 0.1 ', platform: 'PHP %s / mine ' }
%A[%s] PHONGO: DEBUG > Setting driver handshake data: { name: 'ext-mongodb:PHP / test ', version: '%s ', platform: 'PHP %s ' }
%A===DONE===%A