1
0

BxDolServiceCallCodecTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Service-call codec: JSON writes, safe decode of old PHP serialize, no object injection.
  4. * Loads the codec file directly so it can run without a full UNA install.
  5. */
  6. class BxDolServiceCallCodecTest extends \PHPUnit\Framework\TestCase
  7. {
  8. public static function setUpBeforeClass(): void
  9. {
  10. if (!defined('BX_DOL'))
  11. define('BX_DOL', 1);
  12. if (!class_exists('BxDolServiceCallCodec', false)) {
  13. require_once dirname(__DIR__, 4) . '/inc/classes/BxDolServiceCallCodec.php';
  14. }
  15. }
  16. public function testEncodeIsJsonWithModuleAndMethod()
  17. {
  18. $s = BxDolServiceCallCodec::encode('system', 'test', array('a' => 1), 'Module');
  19. $this->assertSame('{', $s[0]);
  20. $a = json_decode($s, true);
  21. $this->assertSame('system', $a['module']);
  22. $this->assertSame('test', $a['method']);
  23. $this->assertSame(array('a' => 1), $a['params']);
  24. $this->assertTrue(BxDolServiceCallCodec::isEncoded($s));
  25. $this->assertSame($a, BxDolServiceCallCodec::decode($s));
  26. }
  27. public function testDecodeLegacySerializedArray()
  28. {
  29. $s = serialize(array(
  30. 'module' => 'system',
  31. 'method' => 'test',
  32. 'params' => array(1, 2),
  33. ));
  34. $this->assertTrue(BxDolServiceCallCodec::isEncoded($s));
  35. $a = BxDolServiceCallCodec::decode($s);
  36. $this->assertSame('system', $a['module']);
  37. $this->assertSame('test', $a['method']);
  38. $this->assertSame(array(1, 2), $a['params']);
  39. }
  40. public function testRejectSerializedObject()
  41. {
  42. $s = serialize(new stdClass());
  43. $this->assertFalse(BxDolServiceCallCodec::isEncoded($s));
  44. $this->assertFalse(BxDolServiceCallCodec::decode($s));
  45. }
  46. public function testRejectSerializedArrayContainingObject()
  47. {
  48. $s = serialize(array(
  49. 'module' => 'system',
  50. 'method' => 'test',
  51. 'params' => array(new stdClass()),
  52. ));
  53. $this->assertFalse(BxDolServiceCallCodec::decode($s));
  54. }
  55. public function testRejectGarbageAndEmpty()
  56. {
  57. $this->assertFalse(BxDolServiceCallCodec::decode(''));
  58. $this->assertFalse(BxDolServiceCallCodec::decode('not a blob'));
  59. $this->assertFalse(BxDolServiceCallCodec::decode('{"foo":1}'));
  60. $this->assertFalse(BxDolServiceCallCodec::decode(null));
  61. }
  62. /**
  63. * Lock CVE-2025-32101 / CVE-2025-66571: posted profile_id is json_decode, never unserialize.
  64. */
  65. public function testAclPostedProfileIdDoesNotUnserialize()
  66. {
  67. $this->assertSame('12', BxDolServiceCallCodec::decodeAclProfileId('12'));
  68. $this->assertSame(array(1, 2), BxDolServiceCallCodec::decodeAclProfileId(urlencode(json_encode(array(1, 2)))));
  69. $serializedObject = serialize(new stdClass());
  70. $this->assertNull(BxDolServiceCallCodec::decodeAclProfileId($serializedObject));
  71. $this->assertNull(BxDolServiceCallCodec::decodeAclProfileId(urlencode($serializedObject)));
  72. }
  73. }