Skip to content

fix Guzzle psr7 invalid header error #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2024
Merged
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
7 changes: 6 additions & 1 deletion class_map/TextCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jaeger\Codec;

use Hyperf\Tracer\Support\GuzzleHeaderValidate;
use Hyperf\Tracer\Support\TextCodecOtel;
use Exception;
use Jaeger\SpanContext;
Expand Down Expand Up @@ -76,8 +77,12 @@ public function inject(SpanContext $spanContext, &$carrier)
if ($this->urlEncoding) {
$encodedValue = urlencode($value);
}
$headerName = $this->baggagePrefix . $key;

$carrier[$this->baggagePrefix . $key] = $encodedValue;
if (!GuzzleHeaderValidate::isValidHeader($headerName, $encodedValue)) {
continue;
}
$carrier[$headerName] = $encodedValue;
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/Support/GuzzleHeaderValidate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf + OpenCodeCo
*
* @link https://opencodeco.dev
* @document https://hyperf.wiki
* @contact leo@opencodeco.dev
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE
*/

namespace Hyperf\Tracer\Support;

final class GuzzleHeaderValidate
{
public static function isValidHeader(string $headerName, string $headerValue): bool
{
return self::isValidHeaderName($headerName) && self::isValidHeaderValue($headerValue);
}

/**
* caused by https://github.com/guzzle/psr7/blob/a70f5c95fb43bc83f07c9c948baa0dc1829bf201/src/MessageTrait.php#L229
*/
public static function isValidHeaderName(string $headerName): bool
{
return (bool)preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $headerName);
}

/**
* caused by https://github.com/guzzle/psr7/blob/a70f5c95fb43bc83f07c9c948baa0dc1829bf201/src/MessageTrait.php#L259
*/
public static function isValidHeaderValue(string $value): bool
{
return (bool)preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D', $value);
}
}
6 changes: 5 additions & 1 deletion src/Support/TextCodecOtel.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public function inject(SpanContext $spanContext, &$carrier)
$baggageHeader = [];

foreach ($baggage as $key => $value) {
$baggageHeader[] = $key . '=' . $value;
$value = $key . '=' . $value;
if (!GuzzleHeaderValidate::isValidHeaderValue($value)) {
continue;
}
$baggageHeader[] = $value;
}
$carrier[$this->traceStateHeader] = implode(',', $baggageHeader);
}
Expand Down
35 changes: 35 additions & 0 deletions tests/GuzzleHeaderValidateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf + OpenCodeCo
*
* @link https://opencodeco.dev
* @document https://hyperf.wiki
* @contact leo@opencodeco.dev
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE
*/

namespace HyperfTest\Tracer\Support;

use Hyperf\Tracer\Support\GuzzleHeaderValidate;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
final class JaegerDecoderTest extends TestCase
{
public function testTraceIdDecoder(): void
{
self::assertFalse(GuzzleHeaderValidate::isValidHeader('uberctx-40247e74-4e8ccd0c@dt', '1234'));
self::assertTrue(GuzzleHeaderValidate::isValidHeader('uberctx-40247e74-4e8ccd0c', '1234'));
}

public function testSpanIdDecoder(): void
{
self::assertSame('255d1b261e0fcbc3', JaegerDecoder::spanIdDecoder('2692338002764483523'));
self::assertSame('21c2405de0e90c56', JaegerDecoder::spanIdDecoder('21c2405de0e90c56'));
}
}