Skip to content

Commit 8364767

Browse files
committed
Apply code suggestions
1 parent 47f6e26 commit 8364767

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

system/Filters/RequestId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private function isValidRequestId(string $requestId): bool
5757
return false;
5858
}
5959

60-
if (strlen($requestId) > 255) {
60+
if (strlen($requestId) > 64) {
6161
return false;
6262
}
6363

tests/system/Filters/RequestIdTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public function testBefore(): void
3838

3939
$requestId = context()->get('request_id');
4040

41+
$requestIdFromHeader = $request->getHeaderLine('X-Request-ID');
42+
4143
$this->assertNotEmpty($requestId);
44+
$this->assertSame($requestId, $requestIdFromHeader);
4245
$this->assertSame(32, strlen($requestId));
4346
$this->assertMatchesRegularExpression('/^[A-Za-z0-9._:-]+$/', $requestId);
4447
}
@@ -55,7 +58,10 @@ public function testBeforeWithExistingRequestId(): void
5558

5659
$requestId = context()->get('request_id');
5760

61+
$requestIdFromHeader = $request->getHeaderLine('X-Request-ID');
62+
5863
$this->assertSame($existingRequestId, $requestId);
64+
$this->assertSame($existingRequestId, $requestIdFromHeader);
5965
$this->assertMatchesRegularExpression('/^[A-Za-z0-9._:-]+$/', $requestId);
6066
}
6167

@@ -71,7 +77,30 @@ public function testBeforeWithExistingInvalidRequestId(): void
7177

7278
$requestId = context()->get('request_id');
7379

80+
$requestIdFromHeader = $request->getHeaderLine('X-Request-ID');
81+
82+
$this->assertNotSame($existingRequestId, $requestId);
83+
$this->assertSame($requestId, $requestIdFromHeader);
84+
$this->assertSame(32, strlen($requestId));
85+
$this->assertMatchesRegularExpression('/^[A-Za-z0-9._:-]+$/', $requestId);
86+
}
87+
88+
public function testBeforeWithExistingLongRequestId(): void
89+
{
90+
$filter = new RequestId();
91+
$request = service('request', null, false);
92+
93+
$existingRequestId = str_repeat('a', 65);
94+
$request->setHeader('X-Request-ID', $existingRequestId);
95+
96+
$filter->before($request);
97+
98+
$requestId = context()->get('request_id');
99+
100+
$requestIdFromHeader = $request->getHeaderLine('X-Request-ID');
101+
74102
$this->assertNotSame($existingRequestId, $requestId);
103+
$this->assertSame($requestId, $requestIdFromHeader);
75104
$this->assertSame(32, strlen($requestId));
76105
$this->assertMatchesRegularExpression('/^[A-Za-z0-9._:-]+$/', $requestId);
77106
}

user_guide_src/source/incoming/filters.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,20 +386,18 @@ Request ID
386386

387387
.. versionadded:: 4.8.0
388388

389-
This filter adds a unique request ID to each request in context of the application. This can be useful for
389+
This filter adds a request ID to each request in context of the application. This can be useful for
390390
debugging and logging purposes, as it allows you to trace a specific request through the application.
391391

392-
Framework-generated IDs are unique for practical purposes, however valid incoming IDs are reused.
392+
Framework-generated IDs are 32-character hexadecimal strings and are unique for practical purposes, however valid incoming IDs are reused.
393393
It is added to the request's context and can be accessed via the ``request_id`` key.
394394

395-
The ID is generated via ``bin2hex(random_bytes(16))``, so it is a 32-character hexadecimal string.
396-
397395
To enable this filter, simply add/uncomment the ``requestid`` alias in the ``$required['before']`` and ``$required['after']`` array in **app/Config/Filters.php**:
398396

399397
.. literalinclude:: filters/014.php
400398

401399
.. note:: If the incoming request has a header named ``X-Request-ID``, the value of that header
402400
will be used as the request ID instead of generating a new one or checking for uniqueness.
403401
The framework does basic validation to ensure that the incoming request ID is a non-empty string,
404-
has at most 255 characters, and contains only valid characters (alphanumeric, dot, underscore, colon, and hyphen).
402+
has at most 64 characters, and contains only valid characters (alphanumeric, dot, underscore, colon, and hyphen).
405403
If the validation fails, a new request ID will be generated as normal. This allows you to pass in your own request ID if you have one available, such as from a client or a load balancer.

0 commit comments

Comments
 (0)