Skip to content

Commit aef9e6f

Browse files
jakubboucekdg
authored andcommitted
Request: Move Basic Auth credential from Url to Request due to prevent leak it (#211)
1 parent 59e76dc commit aef9e6f

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

src/Http/Request.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Request implements IRequest
4444

4545
/** @var ?callable */
4646
private $rawBodyCallback;
47+
private ?string $user;
48+
private ?string $password;
4749

4850

4951
public function __construct(
@@ -56,6 +58,8 @@ public function __construct(
5658
?string $remoteAddress = null,
5759
?string $remoteHost = null,
5860
?callable $rawBodyCallback = null,
61+
?string $user = null,
62+
?string $password = null,
5963
) {
6064
$this->url = $url;
6165
$this->post = (array) $post;
@@ -66,6 +70,8 @@ public function __construct(
6670
$this->remoteAddress = $remoteAddress;
6771
$this->remoteHost = $remoteHost;
6872
$this->rawBodyCallback = $rawBodyCallback;
73+
$this->user = $user;
74+
$this->password = $password;
6975
}
7076

7177

@@ -284,6 +290,18 @@ public function getRawBody(): ?string
284290
}
285291

286292

293+
public function getUser(): ?string
294+
{
295+
return $this->user;
296+
}
297+
298+
299+
public function getPassword(): ?string
300+
{
301+
return $this->password;
302+
}
303+
304+
287305
/**
288306
* Returns the most preferred language by browser. Uses the `Accept-Language` header. If no match is reached, it returns `null`.
289307
* @param string[] $langs supported languages

src/Http/RequestFactory.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public function fromGlobals(): Request
5959
$url = new Url;
6060
$this->getServer($url);
6161
$this->getPathAndQuery($url);
62-
$this->getUserAndPassword($url);
6362
[$post, $cookies] = $this->getGetPostCookie($url);
6463
[$remoteAddr, $remoteHost] = $this->getClient($url);
64+
[$user, $password] = $this->getUserAndPassword();
6565

6666
return new Request(
6767
new UrlScript($url, $this->getScriptPath($url)),
@@ -73,6 +73,8 @@ public function fromGlobals(): Request
7373
$remoteAddr,
7474
$remoteHost,
7575
fn(): string => file_get_contents('php://input'),
76+
$user,
77+
$password,
7678
);
7779
}
7880

@@ -109,13 +111,6 @@ private function getPathAndQuery(Url $url): void
109111
}
110112

111113

112-
private function getUserAndPassword(Url $url): void
113-
{
114-
$url->setUser($_SERVER['PHP_AUTH_USER'] ?? '');
115-
$url->setPassword($_SERVER['PHP_AUTH_PW'] ?? '');
116-
}
117-
118-
119114
private function getScriptPath(Url $url): string
120115
{
121116
if (PHP_SAPI === 'cli-server') {
@@ -290,6 +285,15 @@ private function getClient(Url $url): array
290285
}
291286

292287

288+
private function getUserAndPassword(): array
289+
{
290+
$user = $_SERVER['PHP_AUTH_USER'] ?? null;
291+
$password = $_SERVER['PHP_AUTH_PW'] ?? null;
292+
293+
return [$user, $password];
294+
}
295+
296+
293297
private function useForwardedProxy(Url $url, &$remoteAddr, &$remoteHost): void
294298
{
295299
$forwardParams = preg_split('/[,;]/', $_SERVER['HTTP_FORWARDED']);

tests/Http/RequestFactory.userAndPassword.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ $_SERVER = [
1717
'PHP_AUTH_PW' => 'password',
1818
];
1919
$factory = new RequestFactory;
20-
Assert::same('user', $factory->fromGlobals()->getUrl()->getUser());
21-
Assert::same('password', $factory->fromGlobals()->getUrl()->getPassword());
20+
Assert::same('user', $factory->fromGlobals()->getUser());
21+
Assert::same('password', $factory->fromGlobals()->getPassword());
2222

2323

2424
$_SERVER = [];
2525
$factory = new RequestFactory;
26-
Assert::same('', $factory->fromGlobals()->getUrl()->getUser());
27-
Assert::same('', $factory->fromGlobals()->getUrl()->getPassword());
26+
Assert::same(null, $factory->fromGlobals()->getUser());
27+
Assert::same(null, $factory->fromGlobals()->getPassword());

0 commit comments

Comments
 (0)