Skip to content
Open
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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ parameters:
path: src/Options.php

-
message: "#^Method Sentry\\\\Options\\:\\:getMaxRequestBodySize\\(\\) should return string but returns mixed\\.$#"
message: "#^Method Sentry\\\\Options\\:\\:getMaxRequestBodySize\\(\\) should return 'always'\\|'medium'\\|'never'\\|'none'\\|'small' but returns mixed\\.$#"
count: 1
path: src/Options.php

Expand Down
40 changes: 37 additions & 3 deletions src/DataCollection/KeyValueDataFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@

namespace Sentry\DataCollection;

use Sentry\Util\Arr;

/**
* @internal
*
* @phpstan-type KeyValueCollectionBehavior array{mode: 'off'|'denyList'|'allowList', terms: string[]}
*/
final class KeyValueDataFilter
{
public const FILTERED_VALUE = '[Filtered]';

private const DEFAULT_BODY_FILTER_BEHAVIOR = [
'mode' => 'denyList',
'terms' => [],
];

private const SENSITIVE_DATA_DENYLIST = [
'auth',
'token',
Expand Down Expand Up @@ -68,7 +77,7 @@ public static function filterHeaders(array $headers, array $behavior): ?array

if (\in_array(strtolower($name), self::SENSITIVE_HEADERS, true) || self::shouldFilterValue($name, $behavior)) {
foreach ($values as $headerLine => $headerValue) {
$values[$headerLine] = '[Filtered]';
$values[$headerLine] = self::FILTERED_VALUE;
}
}

Expand Down Expand Up @@ -98,7 +107,7 @@ public static function filterKeyValueData(array $data, array $behavior): ?array
$key = (string) $key;

if (self::shouldFilterValue($key, $behavior)) {
$filtered[$key] = '[Filtered]';
$filtered[$key] = self::FILTERED_VALUE;
} elseif (\is_array($value)) {
$filtered[$key] = self::filterKeyValueData($value, $behavior);
} else {
Expand All @@ -109,6 +118,31 @@ public static function filterKeyValueData(array $data, array $behavior): ?array
return $filtered;
}

/**
* Filters structured HTTP body data while replacing unkeyed top-level values.
*
* @param array<array-key, mixed> $data
*
* @return array<array-key, mixed>
*/
public static function filterHttpBodyData(array $data): array
{
if (!Arr::isList($data)) {
return self::filterKeyValueData($data, self::DEFAULT_BODY_FILTER_BEHAVIOR) ?? [];
}

$filtered = [];

/** @mago-ignore analysis:mixed-assignment */
foreach ($data as $value) {
$filtered[] = \is_array($value)
? self::filterHttpBodyData($value)
: self::FILTERED_VALUE;
}

return $filtered;
}

/**
* @phpstan-param KeyValueCollectionBehavior $behavior
*/
Expand All @@ -126,7 +160,7 @@ public static function filterQueryString(string $queryString, array $behavior):
$key = urldecode($encodedKey);

if ($separatorPosition !== false && self::shouldFilterValue($key, $behavior)) {
$parts[$index] = $encodedKey . '=[Filtered]';
$parts[$index] = $encodedKey . '=' . self::FILTERED_VALUE;
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/DataCollection/RequestDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,10 @@ public function collectRequestBody($body)
}

if (!\is_array($body)) {
return '[Filtered]';
return KeyValueDataFilter::FILTERED_VALUE;
}

return KeyValueDataFilter::filterKeyValueData($body, [
'mode' => 'denyList',
'terms' => [],
]);
return KeyValueDataFilter::filterHttpBodyData($body);
}

/**
Expand All @@ -165,7 +162,7 @@ private function sanitizeLegacyHeaders(array $headers): array

if (\in_array(strtolower($name), $this->piiSanitizeHeaders, true)) {
foreach ($values as $headerLine => $headerValue) {
$values[$headerLine] = '[Filtered]';
$values[$headerLine] = KeyValueDataFilter::FILTERED_VALUE;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,8 @@ public function setCaptureSilencedErrors(bool $shouldCapture): self
/**
* Gets the limit up to which integrations should capture the HTTP request
* body.
*
* @return 'none'|'never'|'small'|'medium'|'always'
*/
public function getMaxRequestBodySize(): string
{
Expand Down
Loading