Skip to content

Commit

Permalink
Merge pull request #52 from worldia/feat/filter-headers
Browse files Browse the repository at this point in the history
feat(Common): Sanitize headers before adding them to span attributes
  • Loading branch information
cyve authored Feb 1, 2024
2 parents 50c380a + d81d87e commit a9444b0
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Http/TracedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ public function getHeaders(bool $throw = true): array
return $this->response->getHeaders($throw);
}

private function toReadableHeaderValue(mixed $value): string
{
if (null === $value) {
return 'null';
} elseif (\is_array($value)) {
return implode(', ', array_map([$this, __FUNCTION__], $value));
} elseif (\is_scalar($value)) {
if (\is_bool($value)) {
return true === $value ? 'true' : 'false';
}

return (string) $value;
} elseif (\is_object($value)) {
if (method_exists($value, '__toString')) {
return (string) $value;
}

return '(object)#'.$value::class;
}

return \gettype($value);
}

public function getContent(bool $throw = true): string
{
try {
Expand Down Expand Up @@ -150,8 +173,15 @@ protected function endTracing(): void

try {
if (\in_array('response.headers', $info['user_data']['span_attributes'] ?? [])) {
$this->span->setAttribute('response.headers', $this->getHeaders(false));
$headers = [];
$raw = $this->getHeaders(false);
foreach ($raw as $header => $value) {
$headers[$header] = $this->toReadableHeaderValue($value);
}

$this->span->setAttribute('response.headers', $headers);
}

if (\in_array('response.body', $info['user_data']['span_attributes'] ?? [])) {
if (empty($this->content) && \is_resource($this->stream)) {
$this->content = stream_get_contents($this->stream) ?: null;
Expand Down

0 comments on commit a9444b0

Please sign in to comment.