Skip to content
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

Use X-Request-ID header as request id if provided by client #28144

Merged
merged 3 commits into from
Jun 22, 2017
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
12 changes: 12 additions & 0 deletions lib/private/AppFramework/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,22 @@ public function passesCSRFCheck() {

/**
* Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging
* If an X-Request-ID header is sent by the client this value will be taken.
* If `mod_unique_id` is installed this value will be taken.
* @return string
*/
public function getId() {
// allow clients to provide a request id
if(isset($this->server['HTTP_X_REQUEST_ID'])) {
$reqId = $this->server['HTTP_X_REQUEST_ID'];
if (strlen($reqId) > 19
&& strlen($reqId) < 200
&& preg_match('%^[a-zA-Z0-9-+/_=.:]+$%', $reqId)) {
return $this->server['HTTP_X_REQUEST_ID'];
} else {
throw new \InvalidArgumentException('X-Request-ID must be 20-200 bytes of chars, numbers and -+/_=.:');
}
}
if(isset($this->server['UNIQUE_ID'])) {
return $this->server['UNIQUE_ID'];
}
Expand Down
72 changes: 72 additions & 0 deletions tests/lib/AppFramework/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,78 @@ public function testGetIdWithModUnique() {
$this->assertSame('GeneratedUniqueIdByModUnique', $request->getId());
}

public function providesGetIdWithValidXRequestID() {
return [
['6241e4ae-ca5d-49e6-948c-6c9e20624361'],
['6241e4ae+ca5d+49e6+948c+6c9e20624361'],
['6241e4ae/ca5d/49e6/948c/6c9e20624361'],
['6241e4ae_ca5d_49e6_948c_6c9e20624361'],
['6241e4ae=ca5d=49e6=948c=6c9e20624361'],
['6241e4ae.ca5d.49e6.948c.6c9e20624361'],
['6241e4ae:ca5d:49e6:948c:6c9e20624361'],
['12-34-56-78-9A-BC_1985-04-12T23:20:50.52Z'],
];
}

/**
* @dataProvider providesGetIdWithValidXRequestID
*/
public function testGetIdWithValidXRequestID($xRequestID) {
$vars = [
'server' => [
'HTTP_X_REQUEST_ID' => $xRequestID
],
];

$request = new Request(
$vars,
$this->secureRandom,
$this->config,
$this->csrfTokenManager,
$this->stream
);

$this->assertSame($xRequestID, $request->getId());
}
public function providesGetIdWithInvalidXRequestID() {
return [
['too-short'],
[
'too-long--too-long--too-long--too-long--too-long--'.
'too-long--too-long--too-long--too-long--too-long--'.
'too-long--too-long--too-long--too-long--too-long--'.
'too-long--too-long--too-long--too-long--too-long--x'
],
['6241e4ae ca5d 49e6 948c 6c9e20624361'],
['6241e4ae#ca5d#49e6#948c#6c9e20624361'],
['6241e4ae%ca5d%49e6%948c%6c9e20624361'],
['6241e4ae"ca5d"49e6"948c"6c9e20624361'],
['6241e4ae\'ca5d\'49e6\'948c\'6c9e20624361'],
];
}

/**
* @dataProvider providesGetIdWithInvalidXRequestID
* @expectedException \InvalidArgumentException
*/
public function testGetIdWithInvalidXRequestID($xRequestID) {
$vars = [
'server' => [
'HTTP_X_REQUEST_ID' => $xRequestID
],
];

$request = new Request(
$vars,
$this->secureRandom,
$this->config,
$this->csrfTokenManager,
$this->stream
);

$request->getId();
}

public function testGetIdWithoutModUnique() {
$this->secureRandom->expects($this->once())
->method('generate')
Expand Down