Skip to content

Commit 69a9125

Browse files
committed
Fix: Suppress HTMLPurifier cache warning by using temp directory for serializer
- Updated InputSanitizer to configure HTMLPurifier with a safe, writable Cache.SerializerPath pointing to sys_get_temp_dir(). - Disabled Cache.DefinitionImpl to prevent disk writes during testing and avoid PHPUnit warnings. - Eliminates “Base directory .../storage/purifier_cache does not exist” warning triggered across sanitization tests. - Ensures consistent, warning-free execution in CI and read-only environments.
1 parent a04a2e1 commit 69a9125

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

src/Security/InputSanitizer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ public static function sanitizeWithWhitelist(
8383
// ⚙️ Configure HTMLPurifier for strict whitelist filtering
8484
$config = HTMLPurifier_Config::createDefault();
8585
$config->set('HTML.Allowed', implode(',', $allowedTags));
86-
$config->set('Cache.SerializerPath', __DIR__ . '/../../../storage/purifier_cache');
86+
87+
// Use system temp directory instead of project path
88+
$config->set('Cache.SerializerPath', sys_get_temp_dir() . '/htmlpurifier');
89+
// $config->set('Cache.SerializerPath', __DIR__ . '/../../../storage/purifier_cache');
90+
$config->set('Cache.DefinitionImpl', null);
8791

8892
// 🚫 Control external/internal resource URIs
8993
$config->set('URI.DisableExternalResources', $disableExternalResources);

tests/Security/InputSanitizerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,15 @@ public function testSanitizeWithWhitelistDisablesExternalResources(): void
9696
$dirty = '<img src="http://evil.com/x.png"><b>safe</b>';
9797
$clean = InputSanitizer::sanitizeWithWhitelist($dirty, ['b', 'img[src]']);
9898

99-
// ✅ External resource should be stripped, safe content preserved
99+
// Allowed safe content should remain
100100
$this->assertStringContainsString('<b>safe</b>', $clean);
101+
102+
// Malicious external resource should be completely removed
101103
$this->assertStringNotContainsString('evil.com', $clean);
104+
105+
// Ensure sanitizer produced valid output (not empty, not NULL)
106+
$this->assertIsString($clean);
107+
$this->assertGreaterThan(0, strlen($clean));
102108
}
103109

104110
/**

0 commit comments

Comments
 (0)