Skip to content

Commit 5411b0e

Browse files
authored
Minor cleanup (#28)
- Call `getSessionId()` just once - Pass only non-empty string to `strtr()`, otherwise it throws a warning - Update description in `composer.json`
2 parents 9b86553 + f9fa7f4 commit 5411b0e

4 files changed

Lines changed: 34 additions & 10 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spaze/phpinfo",
3-
"description": "Extract phpinfo() into a variable and move CSS to external file.",
3+
"description": "Extract phpinfo() output into a variable, sanitize sensitive values, and move inline styles to external CSS.",
44
"keywords": ["PHP","phpinfo"],
55
"license": "MIT",
66
"authors": [

src/PhpInfo.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public function doNotSanitizeSessionId(): self
6666
}
6767

6868

69+
/**
70+
* @param non-empty-string $sanitize
71+
*/
6972
public function addSanitization(string $sanitize, ?string $with = null): self
7073
{
7174
$this->sanitizer->addSanitization($sanitize, $with);

src/SensitiveValueSanitizer.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ public function __construct(private string $sanitizeWith = '[***]')
2626
public function sanitize(string $info): string
2727
{
2828
$sanitize = [];
29-
if ($this->sanitizeSessionId && $this->getSessionId() !== null) {
30-
$sanitize[$this->getSessionId()] = $this->sanitizeWith;
31-
$sanitize[urlencode($this->getSessionId())] = $this->sanitizeWith;
29+
if ($this->sanitizeSessionId) {
30+
$sessionId = $this->getSessionId();
31+
if ($sessionId !== null) {
32+
$sanitize[$sessionId] = $this->sanitizeWith;
33+
$sanitize[urlencode($sessionId)] = $this->sanitizeWith;
34+
}
3235
}
3336
return strtr($info, $this->sanitize + $sanitize);
3437
}
@@ -46,7 +49,7 @@ private function getSessionId(): ?string
4649
} else {
4750
$sessionId = $_COOKIE[$sessionName] ?? null;
4851
}
49-
return is_string($sessionId) ? $sessionId : null;
52+
return is_string($sessionId) && $sessionId !== '' ? $sessionId : null;
5053
}
5154

5255

@@ -61,6 +64,9 @@ public function doNotSanitizeSessionId(): self
6164
}
6265

6366

67+
/**
68+
* @param non-empty-string $sanitize
69+
*/
6470
public function addSanitization(string $sanitize, ?string $with = null): self
6571
{
6672
$this->sanitize[$sanitize] = $this->sanitize[urlencode($sanitize)] = $with ?? $this->sanitizeWith;

tests/PhpInfoTest.phpt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ class PhpInfoTest extends TestCase
2323
protected function setUp(): void
2424
{
2525
$_SERVER['HTTP_WALDO_FRED'] = self::WALDO_1337;
26-
$_SERVER['HTTP_COOKIE'] = 'PHPSESSID=' . urlencode(self::SESSION_ID);
27-
$_COOKIE['PHPSESSID'] = self::SESSION_ID;
28-
29-
session_set_save_handler(new TestSessionHandler(self::SESSION_ID));
30-
session_start();
26+
$this->sessionStart(self::SESSION_ID);
3127
}
3228

3329

@@ -135,6 +131,25 @@ class PhpInfoTest extends TestCase
135131
Assert::contains('🍕', $html);
136132
}
137133

134+
135+
public function testGetHtmlEmptySessionCookie(): void
136+
{
137+
session_destroy();
138+
$this->sessionStart('');
139+
Assert::noError(function (): void {
140+
(new PhpInfo())->getHtml();
141+
});
142+
}
143+
144+
145+
private function sessionStart(string $sessionId): void
146+
{
147+
$_SERVER['HTTP_COOKIE'] = 'PHPSESSID=' . urlencode($sessionId);
148+
$_COOKIE['PHPSESSID'] = $sessionId;
149+
session_set_save_handler(new TestSessionHandler($sessionId));
150+
session_start();
151+
}
152+
138153
}
139154

140155
(new PhpInfoTest())->run();

0 commit comments

Comments
 (0)