Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spaze/phpinfo",
"description": "Extract phpinfo() into a variable and move CSS to external file.",
"description": "Extract phpinfo() output into a variable, sanitize sensitive values, and move inline styles to external CSS.",
"keywords": ["PHP","phpinfo"],
"license": "MIT",
"authors": [
Expand Down
3 changes: 3 additions & 0 deletions src/PhpInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public function doNotSanitizeSessionId(): self
}


/**
* @param non-empty-string $sanitize
*/
public function addSanitization(string $sanitize, ?string $with = null): self
{
$this->sanitizer->addSanitization($sanitize, $with);
Expand Down
14 changes: 10 additions & 4 deletions src/SensitiveValueSanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ public function __construct(private string $sanitizeWith = '[***]')
public function sanitize(string $info): string
{
$sanitize = [];
if ($this->sanitizeSessionId && $this->getSessionId() !== null) {
$sanitize[$this->getSessionId()] = $this->sanitizeWith;
$sanitize[urlencode($this->getSessionId())] = $this->sanitizeWith;
if ($this->sanitizeSessionId) {
$sessionId = $this->getSessionId();
if ($sessionId !== null) {
$sanitize[$sessionId] = $this->sanitizeWith;
$sanitize[urlencode($sessionId)] = $this->sanitizeWith;
}
Comment thread
spaze marked this conversation as resolved.
}
return strtr($info, $this->sanitize + $sanitize);
}
Comment thread
spaze marked this conversation as resolved.
Expand All @@ -46,7 +49,7 @@ private function getSessionId(): ?string
} else {
$sessionId = $_COOKIE[$sessionName] ?? null;
}
return is_string($sessionId) ? $sessionId : null;
return is_string($sessionId) && $sessionId !== '' ? $sessionId : null;
}


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


/**
* @param non-empty-string $sanitize
*/
public function addSanitization(string $sanitize, ?string $with = null): self
{
$this->sanitize[$sanitize] = $this->sanitize[urlencode($sanitize)] = $with ?? $this->sanitizeWith;
Expand Down
25 changes: 20 additions & 5 deletions tests/PhpInfoTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ class PhpInfoTest extends TestCase
protected function setUp(): void
{
$_SERVER['HTTP_WALDO_FRED'] = self::WALDO_1337;
$_SERVER['HTTP_COOKIE'] = 'PHPSESSID=' . urlencode(self::SESSION_ID);
$_COOKIE['PHPSESSID'] = self::SESSION_ID;

session_set_save_handler(new TestSessionHandler(self::SESSION_ID));
session_start();
$this->sessionStart(self::SESSION_ID);
}


Expand Down Expand Up @@ -135,6 +131,25 @@ class PhpInfoTest extends TestCase
Assert::contains('🍕', $html);
}


public function testGetHtmlEmptySessionCookie(): void
{
session_destroy();
$this->sessionStart('');
Comment thread
spaze marked this conversation as resolved.
Assert::noError(function (): void {
(new PhpInfo())->getHtml();
});
Comment thread
spaze marked this conversation as resolved.
}


private function sessionStart(string $sessionId): void
{
$_SERVER['HTTP_COOKIE'] = 'PHPSESSID=' . urlencode($sessionId);
$_COOKIE['PHPSESSID'] = $sessionId;
session_set_save_handler(new TestSessionHandler($sessionId));
session_start();
}

}

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