Skip to content

Commit de74ee4

Browse files
authored
fix Guzzle psr7 invalid header error (#20)
1 parent 3286019 commit de74ee4

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

class_map/TextCodec.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Jaeger\Codec;
44

5+
use Hyperf\Tracer\Support\GuzzleHeaderValidate;
56
use Hyperf\Tracer\Support\TextCodecOtel;
67
use Exception;
78
use Jaeger\SpanContext;
@@ -76,8 +77,12 @@ public function inject(SpanContext $spanContext, &$carrier)
7677
if ($this->urlEncoding) {
7778
$encodedValue = urlencode($value);
7879
}
80+
$headerName = $this->baggagePrefix . $key;
7981

80-
$carrier[$this->baggagePrefix . $key] = $encodedValue;
82+
if (!GuzzleHeaderValidate::isValidHeader($headerName, $encodedValue)) {
83+
continue;
84+
}
85+
$carrier[$headerName] = $encodedValue;
8186
}
8287
}
8388

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf + OpenCodeCo
6+
*
7+
* @link https://opencodeco.dev
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE
11+
*/
12+
13+
namespace Hyperf\Tracer\Support;
14+
15+
final class GuzzleHeaderValidate
16+
{
17+
public static function isValidHeader(string $headerName, string $headerValue): bool
18+
{
19+
return self::isValidHeaderName($headerName) && self::isValidHeaderValue($headerValue);
20+
}
21+
22+
/**
23+
* caused by https://github.com/guzzle/psr7/blob/a70f5c95fb43bc83f07c9c948baa0dc1829bf201/src/MessageTrait.php#L229
24+
*/
25+
public static function isValidHeaderName(string $headerName): bool
26+
{
27+
return (bool)preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $headerName);
28+
}
29+
30+
/**
31+
* caused by https://github.com/guzzle/psr7/blob/a70f5c95fb43bc83f07c9c948baa0dc1829bf201/src/MessageTrait.php#L259
32+
*/
33+
public static function isValidHeaderValue(string $value): bool
34+
{
35+
return (bool)preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D', $value);
36+
}
37+
}

src/Support/TextCodecOtel.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ public function inject(SpanContext $spanContext, &$carrier)
5050
$baggageHeader = [];
5151

5252
foreach ($baggage as $key => $value) {
53-
$baggageHeader[] = $key . '=' . $value;
53+
$value = $key . '=' . $value;
54+
if (!GuzzleHeaderValidate::isValidHeaderValue($value)) {
55+
continue;
56+
}
57+
$baggageHeader[] = $value;
5458
}
5559
$carrier[$this->traceStateHeader] = implode(',', $baggageHeader);
5660
}

tests/GuzzleHeaderValidateTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf + OpenCodeCo
6+
*
7+
* @link https://opencodeco.dev
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE
11+
*/
12+
13+
namespace HyperfTest\Tracer\Support;
14+
15+
use Hyperf\Tracer\Support\GuzzleHeaderValidate;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @internal
20+
* @coversNothing
21+
*/
22+
final class JaegerDecoderTest extends TestCase
23+
{
24+
public function testTraceIdDecoder(): void
25+
{
26+
self::assertFalse(GuzzleHeaderValidate::isValidHeader('uberctx-40247e74-4e8ccd0c@dt', '1234'));
27+
self::assertTrue(GuzzleHeaderValidate::isValidHeader('uberctx-40247e74-4e8ccd0c', '1234'));
28+
}
29+
30+
public function testSpanIdDecoder(): void
31+
{
32+
self::assertSame('255d1b261e0fcbc3', JaegerDecoder::spanIdDecoder('2692338002764483523'));
33+
self::assertSame('21c2405de0e90c56', JaegerDecoder::spanIdDecoder('21c2405de0e90c56'));
34+
}
35+
}

0 commit comments

Comments
 (0)