From c2ca21b7d703bc5e70a7e348776d126f1e52d8bb Mon Sep 17 00:00:00 2001 From: Julien Ballestracci Date: Tue, 30 Jan 2024 10:32:54 +0100 Subject: [PATCH] feat(Common): sanitize headers before adding them to span attributes --- src/Http/TracedResponse.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Http/TracedResponse.php b/src/Http/TracedResponse.php index 99c23bb..889745a 100644 --- a/src/Http/TracedResponse.php +++ b/src/Http/TracedResponse.php @@ -48,7 +48,37 @@ public function getStatusCode(): int public function getHeaders(bool $throw = true): array { - return $this->response->getHeaders($throw); + $headers = []; + $raw = $this->response->getHeaders($throw); + foreach ($raw as $header => $value) { + $headers[$header] = $this->toReadableHeaderValue($value); + } + + /** @phpstan-ignore-next-line */ + return $headers; + } + + 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