BxDolAiChatImagesTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Chat image URLs: this site and the storage host only.
  4. */
  5. class BxDolAiChatImagesTest extends \PHPUnit\Framework\TestCase
  6. {
  7. public function testRejectsOtherHosts()
  8. {
  9. $o = new BxDolAiChatImages();
  10. $this->assertSame('', $o->sanitizeUrl('http://169.254.169.254/latest/meta-data/iam/?x=sys_agents_chat_images'));
  11. $this->assertSame('', $o->sanitizeUrl('http://10.0.0.5:8080/admin/storage.php'));
  12. $this->assertSame('', $o->sanitizeUrl('https://evil.test/storage.php?o=sys_agents_chat_images'));
  13. $this->assertSame('', $o->sanitizeUrl('javascript:alert(1)'));
  14. $this->assertSame('', $o->sanitizeUrl('file:///etc/passwd'));
  15. }
  16. public function testAllowsThisSite()
  17. {
  18. $o = new BxDolAiChatImages();
  19. $aRoot = parse_url(BX_DOL_URL_ROOT);
  20. $sHost = $aRoot['host'];
  21. $sPath = '/storage.php?o=sys_agents_chat_images&f=a.jpg';
  22. $this->assertNotSame('', $o->sanitizeUrl(rtrim(BX_DOL_URL_ROOT, '/') . $sPath));
  23. $this->assertStringContainsString($sHost, $o->sanitizeUrl($sPath));
  24. if (!filter_var($sHost, FILTER_VALIDATE_IP)) {
  25. $sWww = strncmp($sHost, 'www.', 4) === 0 ? substr($sHost, 4) : 'www.' . $sHost;
  26. $this->assertNotSame('', $o->sanitizeUrl($aRoot['scheme'] . '://' . $sWww . $sPath));
  27. }
  28. $this->assertSame('', $o->sanitizeUrl('https://' . $sHost . '.evil.test/storage.php'));
  29. $this->assertSame('', $o->sanitizeUrl('http://' . $sHost . '@169.254.169.254/storage.php'));
  30. }
  31. public function testMaxBytesFollowsStorage()
  32. {
  33. $oStorage = BxDolStorage::getObjectInstance('sys_agents_chat_images');
  34. if (!$oStorage)
  35. $this->markTestSkipped('Chat images storage is not installed.');
  36. $iMax = (int)($oStorage->getObjectData()['max_file_size'] ?? 0);
  37. $this->assertSame($iMax, (new BxDolAiChatImages())->maxBytes());
  38. }
  39. public function testAllowsStorageDomain()
  40. {
  41. $sDomain = trim((string)getParam('sys_storage_s3_domain'));
  42. if ($sDomain === '')
  43. $this->markTestSkipped('Storage domain is not configured.');
  44. $aDomain = parse_url(preg_match('#://#', $sDomain) ? $sDomain : 'https://' . $sDomain);
  45. $o = new BxDolAiChatImages();
  46. $this->assertNotSame('', $o->sanitizeUrl('https://' . $aDomain['host'] . '/sys_agents_chat_images/a.jpg'));
  47. $this->assertSame('', $o->sanitizeUrl('https://' . $aDomain['host'] . '.evil.test/sys_agents_chat_images/a.jpg'));
  48. }
  49. }