Skip to content

Commit

Permalink
Removed old attempt, added new and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
link04 committed Oct 22, 2024
1 parent 9bf828c commit f6f1fd6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
4 changes: 1 addition & 3 deletions ext/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,7 @@ static void dd_add_post_fields_to_meta_recursive(zend_array *meta, const char *t
zend_string *postvalconcat = zend_strpprintf(0, "%s=%s", ZSTR_VAL(postkey), ZSTR_VAL(postvalstr));
zend_string_release(postvalstr);

// Match it with the regex to redact if needed and value is not an empty string
zend_string *regex_pattern = get_DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP();
if (strlen(regex_pattern) > 0 && zai_match_regex(regex_pattern, postvalconcat)) {
if (zai_match_regex(get_DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP(), postvalconcat)) {
zend_string *replacement = zend_string_init(ZEND_STRL("<redacted>"), 0);
dd_add_post_fields_to_meta(meta, type, postkey, replacement);
zend_string_release(replacement);
Expand Down
12 changes: 7 additions & 5 deletions src/DDTrace/Util/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,13 @@ private static function generateFilteredPostFields($postKey, $postVal, array $wh

// Match it with the regex to redact if needed
$obfuscationRegex = \ini_get('datadog.trace.obfuscation_query_string_regexp');
$obfuscationRegex = '(' . $obfuscationRegex . ')';
if (preg_match($obfuscationRegex, $postField)) {
return [$postKey => '<redacted>'];
} else {
return [$postKey => $postVal];
if ($obfuscationRegex !== "") {
$obfuscationRegex = '(' . $obfuscationRegex . ')';
if (preg_match($obfuscationRegex, $postField)) {
return [$postKey => '<redacted>'];
} else {
return [$postKey => $postVal];
}
}
} else {
// The postkey is not in the whitelist, and no wildcard set, then always use <redacted>
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Util/Normalizer/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protected function ddSetUp()
'DD_TRACE_RESOURCE_URI_MAPPING_INCOMING',
'DD_TRACE_RESOURCE_URI_MAPPING_OUTGOING',
'DD_TRACE_RESOURCE_URI_QUERY_PARAM_ALLOWED',
'DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP',
]);
parent::ddSetUp();
}
Expand All @@ -25,6 +26,7 @@ protected function ddTearDown()
'DD_TRACE_RESOURCE_URI_MAPPING_INCOMING',
'DD_TRACE_RESOURCE_URI_MAPPING_OUTGOING',
'DD_TRACE_RESOURCE_URI_QUERY_PARAM_ALLOWED',
'DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP',
]);
}

Expand Down Expand Up @@ -486,6 +488,40 @@ public function testQueryParamPreserveWildcard()
);
}

public function testObfuscationQueryStringConfigured()
{
$this->putEnvAndReloadConfig([
'DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP=\d{3}-\d{2}-\d{4}',
]);

$this->assertSame(
'/?<redacted>',
\DDTrace\Util\Normalizer::uriNormalizeIncomingPath('/?ssn=123-45-6789')
);

$this->assertSame(
'/?<redacted>',
\DDTrace\Util\Normalizer::uriNormalizeOutgoingPath('/?ssn=123-45-6789')
);
}

public function testObfuscationQueryStringWithEmptyRegex()
{
$this->putEnvAndReloadConfig([
'DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP=""',
]);

$this->assertSame(
'/?application_key=123',
\DDTrace\Util\Normalizer::uriNormalizeIncomingPath('/?application_key=123')
);

$this->assertSame(
'/?application_key=123',
\DDTrace\Util\Normalizer::uriNormalizeOutgoingPath('/?application_key=123')
);
}

/**
* @dataProvider dataProviderSanitizeNoDropUserinfo
* @param string $url
Expand Down

0 comments on commit f6f1fd6

Please sign in to comment.