CheckerTest.php 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\IntegrityCheck;
  8. use OC\Core\Command\Maintenance\Mimetype\GenerateMimetypeFileBuilder;
  9. use OC\IntegrityCheck\Checker;
  10. use OC\IntegrityCheck\Helpers\AppLocator;
  11. use OC\IntegrityCheck\Helpers\EnvironmentHelper;
  12. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  13. use OC\Memcache\NullCache;
  14. use OCP\App\IAppManager;
  15. use OCP\IAppConfig;
  16. use OCP\ICacheFactory;
  17. use OCP\IConfig;
  18. use OCP\ServerVersion;
  19. use phpseclib\Crypt\RSA;
  20. use phpseclib\File\X509;
  21. use Test\TestCase;
  22. class CheckerTest extends TestCase {
  23. /** @var ServerVersion|\PHPUnit\Framework\MockObject\MockObject */
  24. private $serverVersion;
  25. /** @var EnvironmentHelper|\PHPUnit\Framework\MockObject\MockObject */
  26. private $environmentHelper;
  27. /** @var AppLocator|\PHPUnit\Framework\MockObject\MockObject */
  28. private $appLocator;
  29. /** @var Checker */
  30. private $checker;
  31. /** @var FileAccessHelper|\PHPUnit\Framework\MockObject\MockObject */
  32. private $fileAccessHelper;
  33. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  34. private $config;
  35. /** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
  36. private $appConfig;
  37. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  38. private $cacheFactory;
  39. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  40. private $appManager;
  41. /** @var \OC\Files\Type\Detection|\PHPUnit\Framework\MockObject\MockObject */
  42. private $mimeTypeDetector;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->serverVersion = $this->createMock(ServerVersion::class);
  46. $this->environmentHelper = $this->createMock(EnvironmentHelper::class);
  47. $this->fileAccessHelper = $this->createMock(FileAccessHelper::class);
  48. $this->appLocator = $this->createMock(AppLocator::class);
  49. $this->config = $this->createMock(IConfig::class);
  50. $this->appConfig = $this->createMock(IAppConfig::class);
  51. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  52. $this->appManager = $this->createMock(IAppManager::class);
  53. $this->mimeTypeDetector = $this->createMock(\OC\Files\Type\Detection::class);
  54. $this->config->method('getAppValue')
  55. ->willReturnArgument(2);
  56. $this->cacheFactory
  57. ->expects($this->any())
  58. ->method('createDistributed')
  59. ->with('oc.integritycheck.checker')
  60. ->willReturn(new NullCache());
  61. $this->checker = new Checker(
  62. $this->serverVersion,
  63. $this->environmentHelper,
  64. $this->fileAccessHelper,
  65. $this->appLocator,
  66. $this->config,
  67. $this->appConfig,
  68. $this->cacheFactory,
  69. $this->appManager,
  70. $this->mimeTypeDetector
  71. );
  72. }
  73. public function testWriteAppSignatureOfNotExistingApp(): void {
  74. $this->expectException(\Exception::class);
  75. $this->expectExceptionMessage('Exception message');
  76. $this->fileAccessHelper
  77. ->expects($this->once())
  78. ->method('assertDirectoryExists')
  79. ->with('NotExistingApp/appinfo')
  80. ->willThrowException(new \Exception('Exception message'));
  81. $this->fileAccessHelper
  82. ->expects($this->once())
  83. ->method('is_writable')
  84. ->with('NotExistingApp/appinfo')
  85. ->willReturn(true);
  86. $keyBundle = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.crt');
  87. $rsaPrivateKey = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.key');
  88. $rsa = new RSA();
  89. $rsa->loadKey($rsaPrivateKey);
  90. $x509 = new X509();
  91. $x509->loadX509($keyBundle);
  92. $this->checker->writeAppSignature('NotExistingApp', $x509, $rsa);
  93. }
  94. public function testWriteAppSignatureWrongPermissions(): void {
  95. $this->expectException(\Exception::class);
  96. $this->expectExceptionMessageMatches('/[a-zA-Z\\/_-]+ is not writable/');
  97. $this->fileAccessHelper
  98. ->expects($this->once())
  99. ->method('file_put_contents')
  100. ->will($this->throwException(new \Exception('Exception message')));
  101. $keyBundle = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.crt');
  102. $rsaPrivateKey = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.key');
  103. $rsa = new RSA();
  104. $rsa->loadKey($rsaPrivateKey);
  105. $x509 = new X509();
  106. $x509->loadX509($keyBundle);
  107. $this->checker->writeAppSignature(\OC::$SERVERROOT . '/tests/data/integritycheck/app/', $x509, $rsa);
  108. }
  109. public function testWriteAppSignature(): void {
  110. $expectedSignatureFileData = '{
  111. "hashes": {
  112. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  113. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  114. },
  115. "signature": "Y5yvXvcGHVPuRRatKVDUONWq1FpLXugZd6Km\/+aEHsQj7coVl9FeMj9OsWamBf7yRIw3dtNLguTLlAA9QAv\/b0uHN3JnbNZN+dwFOve4NMtqXfSDlWftqKN00VS+RJXpG1S2IIx9Poyp2NoghL\/5AuTv4GHiNb7zU\/DT\/kt71pUGPgPR6IIFaE+zHOD96vjYkrH+GfWZzKR0FCdLib9yyNvk+EGrcjKM6qjs2GKfS\/XFjj\/\/neDnh\/0kcPuKE3ZbofnI4TIDTv0CGqvOp7PtqVNc3Vy\/UKa7uF1PT0MAUKMww6EiMUSFZdUVP4WWF0Y72W53Qdtf1hrAZa2kfKyoK5kd7s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
  117. }';
  118. $this->fileAccessHelper
  119. ->expects($this->once())
  120. ->method('file_put_contents')
  121. ->with(
  122. $this->equalTo(\OC::$SERVERROOT . '/tests/data/integritycheck/app//appinfo/signature.json'),
  123. $this->callback(function ($signature) use ($expectedSignatureFileData) {
  124. $expectedArray = json_decode($expectedSignatureFileData, true);
  125. $actualArray = json_decode($signature, true);
  126. $this->assertEquals($expectedArray, $actualArray);
  127. return true;
  128. })
  129. );
  130. $keyBundle = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.crt');
  131. $rsaPrivateKey = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.key');
  132. $rsa = new RSA();
  133. $rsa->loadKey($rsaPrivateKey);
  134. $x509 = new X509();
  135. $x509->loadX509($keyBundle);
  136. $this->checker->writeAppSignature(\OC::$SERVERROOT . '/tests/data/integritycheck/app/', $x509, $rsa);
  137. }
  138. public function testVerifyAppSignatureWithoutSignatureData(): void {
  139. $this->serverVersion
  140. ->expects($this->once())
  141. ->method('getChannel')
  142. ->willReturn('stable');
  143. $this->config
  144. ->expects($this->any())
  145. ->method('getSystemValueBool')
  146. ->with('integrity.check.disabled', false)
  147. ->willReturn(false);
  148. $expected = [
  149. 'EXCEPTION' => [
  150. 'class' => 'OC\IntegrityCheck\Exceptions\InvalidSignatureException',
  151. 'message' => 'Signature data not found.',
  152. ],
  153. ];
  154. $this->assertSame($expected, $this->checker->verifyAppSignature('SomeApp'));
  155. }
  156. public function testVerifyAppSignatureWithValidSignatureData(): void {
  157. $this->serverVersion
  158. ->expects($this->once())
  159. ->method('getChannel')
  160. ->willReturn('stable');
  161. $this->config
  162. ->expects($this->any())
  163. ->method('getSystemValueBool')
  164. ->with('integrity.check.disabled', false)
  165. ->willReturn(false);
  166. $this->appLocator
  167. ->expects($this->once())
  168. ->method('getAppPath')
  169. ->with('SomeApp')
  170. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/app/');
  171. $signatureDataFile = '{
  172. "hashes": {
  173. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  174. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  175. },
  176. "signature": "dYoohBaWIFR\/To1FXEbMQB5apUhVYlEauBGSPo12nq84wxWkBx2EM3KDRgkB5Sub2tr0CgmAc2EVjPhKIEzAam26cyUb48bJziz1V6wvW7z4GZAfaJpzLkyHdSfV5117VSf5w1rDcAeZDXfGUaaNEJPWytaF4ZIxVge7f3NGshHy4odFVPADy\/u6c43BWvaOtJ4m3aJQbP6sxCO9dxwcm5yJJJR3n36jfh229sdWBxyl8BhwhH1e1DEv78\/aiL6ckKFPVNzx01R6yDFt3TgEMR97YZ\/R6lWiXG+dsJ305jNFlusLu518zBUvl7g5yjzGN778H29b2C8VLZKmi\/h1CH9jGdD72fCqCYdenD2uZKzb6dsUtXtvBmVcVT6BUGz41W1pkkEEB+YJpMrHILIxAiHRGv1+aZa9\/Oz8LWFd+BEUQjC2LJgojPnpzaG\/msw1nBkX16NNVDWWtJ25Bc\/r\/mG46rwjWB\/cmV6Lwt6KODiqlxgrC4lm9ALOCEWw+23OcYhLwNfQTYevXqHqsFfXOkhUnM8z5vDUb\/HBraB1DjFXN8iLK+1YewD4P495e+SRzrR79Oi3F8SEqRIzRLfN2rnW1BTms\/wYsz0p67cup1Slk1XlNmHwbWX25NVd2PPlLOvZRGoqcKFpIjC5few8THiZfyjiNFwt3RM0AFdZcXY=",
  177. "certificate": "-----BEGIN CERTIFICATE-----\r\nMIIEvjCCAqagAwIBAgIUc\/0FxYrsgSs9rDxp03EJmbjN0NwwDQYJKoZIhvcNAQEF\r\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTEw\r\nMzIxMDMzM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LQUxGTRZyAcXvj7obqRAEZwFAKQgFpfpqTb\r\nH+kjcbZSaAlLVSF7vBc1syyI8RGYbqpwvtREqJtl5IEIwe6huEqJ3zPnlP2th\/55\r\ncf3Fovj6JJgbb9XFxrdnsOsDOu\/tpnaRWlvv5ib4+SzG5wWFT5UUEo4Wg2STQiiX\r\nuVSRQxK1LE1yg84bs3NZk9FSQh4B8vZVuRr5FaJsZZkwlFlhRO\/\/+TJtXRbyNgsf\r\noMRZGi8DLGU2SGEAHcRH\/QZHq\/XDUWVzdxrSBYcy7GSpT7UDVzGv1rEJUrn5veP1\r\n0KmauAqtiIaYRm4f6YBsn0INcZxzIPZ0p8qFtVZBPeHhvQtvOt0iXI\/XUxEWOa2F\r\nK2EqhErgMK\/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  178. }';
  179. $this->fileAccessHelper
  180. ->expects($this->exactly(2))
  181. ->method('file_get_contents')
  182. ->withConsecutive(
  183. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//appinfo/signature.json'],
  184. ['/resources/codesigning/root.crt'],
  185. )
  186. ->willReturnOnConsecutiveCalls(
  187. $signatureDataFile,
  188. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  189. );
  190. $this->assertSame([], $this->checker->verifyAppSignature('SomeApp'));
  191. }
  192. public function testVerifyAppSignatureWithTamperedSignatureData(): void {
  193. $this->serverVersion
  194. ->expects($this->once())
  195. ->method('getChannel')
  196. ->willReturn('stable');
  197. $this->config
  198. ->expects($this->any())
  199. ->method('getSystemValueBool')
  200. ->with('integrity.check.disabled', false)
  201. ->willReturn(false);
  202. $this->appLocator
  203. ->expects($this->once())
  204. ->method('getAppPath')
  205. ->with('SomeApp')
  206. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/app/');
  207. $signatureDataFile = '{
  208. "hashes": {
  209. "AnotherFile.txt": "tampered",
  210. "subfolder\/file.txt": "tampered"
  211. },
  212. "signature": "EL49UaSeyMAqyMtqId+tgOhhwgOevPZsRLX4j2blnybAB6fN07z0936JqZV7+eMPiE30Idx+UCY6rCFN531Kqe9vAOCdgtHUSOjjKyKc+lvULESlMb6YQcrZrvDlEMMjzjH49ewG7Ai8sNN6HrRUd9U8ws+ewSkW2DOOBItj\/21RBnkrSt+2AtGXGigEvuTm57HrCYDj8\/lSkumC2GVkjLUHeLOKYo4PRNOr6yP5mED5v7zo66AWvXl2fKv54InZcdxsAk35lyK9DGZbk\/027ZRd0AOHT3LImRLvQ+8EAg3XLlRUy0hOFGgPC+jYonMzgYvsAXAXi2j8LnLJlsLwpFwu1k1B+kZVPMumKZvP9OvJb70EirecXmz62V+Jiyuaq7ne4y7Kp5gKZT\/T8SeZ0lFtCmPfYyzBB0y8s5ldmTTmdVYHs54t\/OCCW82HzQZxnFNPzDTRa8HglsaMKrqPtW59+R4UvRKSWhB8M\/Ah57qgzycvPV4KMz\/FbD4l\/\/9chRKSlCfc2k3b8ZSHNmi+EzCKgJjWIoKdgN1yax94puU8jfn8UW+G7H9Y1Jsf\/jox6QLyYEgtV1vOHY2xLT7fVs2vhyvkN2MNjJnmQ70gFG5Qz2lBz5wi6ZpB+tOfCcpbLxWAkoWoIrmC\/Ilqh7mfmRZ43g5upjkepHNd93ONuY8=",
  213. "certificate": "-----BEGIN CERTIFICATE-----\r\nMIIEwTCCAqmgAwIBAgIUWv0iujufs5lUr0svCf\/qTQvoyKAwDQYJKoZIhvcNAQEF\r\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTEw\r\nMzIyNDk1M1oXDTE2MTEwMzIyNDk1M1owEjEQMA4GA1UEAwwHU29tZUFwcDCCAiIw\r\nDQYJKoZIhvcNAQEBBQADggIPADCCAgoCgg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piXaF7WGIV1X6DI\/ipWvfrF7CEy6I\/kP1InY\/vMDjQjeDnJ\/VrXIWXO\r\nyZvHXVaN\/m+1RlETsH7YO\/QmxRue9ZHN3gvvWtmpCeA95sfpepOk7UcHxHZYyQbF\r\n49\/au8j+5tsr4A83xzsT1JbcKRxkAaQ7WDJpOnE5O1+H0fB+BaLakTg6XX9d4Fo7\r\n7Gin7hVWX7pL+JIyxMzME3LhfI61+CRcqZQIrpyaafUziPQbWIPfEs7h8tCOWyvW\r\nUO8ZLervYCB3j44ivkrxPlcBklDCqqKKBzDP9dYOtS\/P4RB1NkHA9+NTvmBpTonS\r\nSFXdg9fFMD7VfjDE3Vnk+8DWkVH5wBYowTAD7w9Wuzr7DumiAULexnP\/Y7xwxLv7\r\n4B+pXTAcRK0zECDEaX3npS8xWzrB\r\n-----END CERTIFICATE-----"
  214. }';
  215. $this->fileAccessHelper
  216. ->expects($this->exactly(2))
  217. ->method('file_get_contents')
  218. ->withConsecutive(
  219. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//appinfo/signature.json'],
  220. ['/resources/codesigning/root.crt'],
  221. )
  222. ->willReturnOnConsecutiveCalls(
  223. $signatureDataFile,
  224. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  225. );
  226. $expected = [
  227. 'EXCEPTION' => [
  228. 'class' => 'OC\\IntegrityCheck\\Exceptions\\InvalidSignatureException',
  229. 'message' => 'Signature could not get verified.',
  230. ],
  231. ];
  232. $this->assertEquals($expected, $this->checker->verifyAppSignature('SomeApp'));
  233. }
  234. public function testVerifyAppSignatureWithTamperedFiles(): void {
  235. $this->serverVersion
  236. ->expects($this->once())
  237. ->method('getChannel')
  238. ->willReturn('stable');
  239. $this->config
  240. ->expects($this->any())
  241. ->method('getSystemValueBool')
  242. ->with('integrity.check.disabled', false)
  243. ->willReturn(false);
  244. $this->appLocator
  245. ->expects($this->once())
  246. ->method('getAppPath')
  247. ->with('SomeApp')
  248. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData/');
  249. $signatureDataFile = '{
  250. "hashes": {
  251. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  252. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  253. },
  254. "signature": "dYoohBaWIFR\/To1FXEbMQB5apUhVYlEauBGSPo12nq84wxWkBx2EM3KDRgkB5Sub2tr0CgmAc2EVjPhKIEzAam26cyUb48bJziz1V6wvW7z4GZAfaJpzLkyHdSfV5117VSf5w1rDcAeZDXfGUaaNEJPWytaF4ZIxVge7f3NGshHy4odFVPADy\/u6c43BWvaOtJ4m3aJQbP6sxCO9dxwcm5yJJJR3n36jfh229sdWBxyl8BhwhH1e1DEv78\/aiL6ckKFPVNzx01R6yDFt3TgEMR97YZ\/R6lWiXG+dsJ305jNFlusLu518zBUvl7g5yjzGN778H29b2C8VLZKmi\/h1CH9jGdD72fCqCYdenD2uZKzb6dsUtXtvBmVcVT6BUGz41W1pkkEEB+YJpMrHILIxAiHRGv1+aZa9\/Oz8LWFd+BEUQjC2LJgojPnpzaG\/msw1nBkX16NNVDWWtJ25Bc\/r\/mG46rwjWB\/cmV6Lwt6KODiqlxgrC4lm9ALOCEWw+23OcYhLwNfQTYevXqHqsFfXOkhUnM8z5vDUb\/HBraB1DjFXN8iLK+1YewD4P495e+SRzrR79Oi3F8SEqRIzRLfN2rnW1BTms\/wYsz0p67cup1Slk1XlNmHwbWX25NVd2PPlLOvZRGoqcKFpIjC5few8THiZfyjiNFwt3RM0AFdZcXY=",
  255. "certificate": "-----BEGIN CERTIFICATE-----\r\nMIIEvjCCAqagAwIBAgIUc\/0FxYrsgSs9rDxp03EJmbjN0NwwDQYJKoZIhvcNAQEF\r\nBQAwIzEhMB8GA1UECgwYb3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EUX2ZrKFWmcgZFDUSRd\r\n9Uj26vtUhCC4bU4jgu6hIrR9IuxOBLQUxGTRZyAcXvj7obqRAEZwFAKQgFpfpqTb\r\nH+kjcbZSaAlLVSF7vBc1syyI8RGYbqpwvtREqJtl5IEIwe6huEqJ3zPnlP2th\/55\r\ncf3Fovj6JJgbb9XFxrdnsOsDOu\/tpnaRWlvv5ib4+SzG5wWFT5UUEo4Wg2STQiiX\r\nuVSRQxK1LE1yg84bs3NZk9FSQh4B8vZVuRr5FaJsZZkwlFlhRO\/\/+TJtXRbyNgsf\r\noMRZGi8DLGU2SGEAHcRH\/QZHq\/XDUWVzdxrSBYcy7GSpT7UDVzGv1rEJUrn5veP1\r\n0KmauAqtiIaYRm4f6YBsn0INcZxzIPZ0p8qFtVZBPeHhvQtvOt0iXI\/XUxEWOa2F\r\nK2EqhErgMK\/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  256. }';
  257. $this->fileAccessHelper
  258. ->expects($this->exactly(2))
  259. ->method('file_get_contents')
  260. ->withConsecutive(
  261. [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//appinfo/signature.json'],
  262. ['/resources/codesigning/root.crt'],
  263. )
  264. ->willReturnOnConsecutiveCalls(
  265. $signatureDataFile,
  266. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  267. );
  268. $expected = [
  269. 'INVALID_HASH' => [
  270. 'AnotherFile.txt' => [
  271. 'expected' => '1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112',
  272. 'current' => '7322348ba269c6d5522efe02f424fa3a0da319a7cd9c33142a5afe32a2d9af2da3a411f086fcfc96ff4301ea566f481dba0960c2abeef3594c4d930462f6584c',
  273. ],
  274. ],
  275. 'FILE_MISSING' => [
  276. 'subfolder/file.txt' => [
  277. 'expected' => '410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b',
  278. 'current' => '',
  279. ],
  280. ],
  281. 'EXTRA_FILE' => [
  282. 'UnecessaryFile' => [
  283. 'expected' => '',
  284. 'current' => 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e',
  285. ],
  286. ],
  287. ];
  288. $this->assertSame($expected, $this->checker->verifyAppSignature('SomeApp'));
  289. }
  290. public function testVerifyAppSignatureWithTamperedFilesAndAlternatePath(): void {
  291. $this->serverVersion
  292. ->expects($this->once())
  293. ->method('getChannel')
  294. ->willReturn('stable');
  295. $this->config
  296. ->expects($this->any())
  297. ->method('getSystemValueBool')
  298. ->with('integrity.check.disabled', false)
  299. ->willReturn(false);
  300. $this->appLocator
  301. ->expects($this->never())
  302. ->method('getAppPath')
  303. ->with('SomeApp');
  304. $signatureDataFile = '{
  305. "hashes": {
  306. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  307. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  308. },
  309. "signature": "dYoohBaWIFR\/To1FXEbMQB5apUhVYlEauBGSPo12nq84wxWkBx2EM3KDRgkB5Sub2tr0CgmAc2EVjPhKIEzAam26cyUb48bJziz1V6wvW7z4GZAfaJpzLkyHdSfV5117VSf5w1rDcAeZDXfGUaaNEJPWytaF4ZIxVge7f3NGshHy4odFVPADy\/u6c43BWvaOtJ4m3aJ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/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  311. }';
  312. $this->fileAccessHelper
  313. ->expects($this->exactly(2))
  314. ->method('file_get_contents')
  315. ->withConsecutive(
  316. [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//appinfo/signature.json'],
  317. ['/resources/codesigning/root.crt'],
  318. )
  319. ->willReturnOnConsecutiveCalls(
  320. $signatureDataFile,
  321. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  322. );
  323. $expected = [
  324. 'INVALID_HASH' => [
  325. 'AnotherFile.txt' => [
  326. 'expected' => '1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112',
  327. 'current' => '7322348ba269c6d5522efe02f424fa3a0da319a7cd9c33142a5afe32a2d9af2da3a411f086fcfc96ff4301ea566f481dba0960c2abeef3594c4d930462f6584c',
  328. ],
  329. ],
  330. 'FILE_MISSING' => [
  331. 'subfolder/file.txt' => [
  332. 'expected' => '410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b',
  333. 'current' => '',
  334. ],
  335. ],
  336. 'EXTRA_FILE' => [
  337. 'UnecessaryFile' => [
  338. 'expected' => '',
  339. 'current' => 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e',
  340. ],
  341. ],
  342. ];
  343. $this->assertSame($expected, $this->checker->verifyAppSignature('SomeApp', \OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData/'));
  344. }
  345. public function testVerifyAppWithDifferentScope(): void {
  346. $this->serverVersion
  347. ->expects($this->once())
  348. ->method('getChannel')
  349. ->willReturn('stable');
  350. $this->config
  351. ->expects($this->any())
  352. ->method('getSystemValueBool')
  353. ->with('integrity.check.disabled', false)
  354. ->willReturn(false);
  355. $this->appLocator
  356. ->expects($this->once())
  357. ->method('getAppPath')
  358. ->with('SomeApp')
  359. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData/');
  360. $signatureDataFile = '{
  361. "hashes": {
  362. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  363. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  364. },
  365. "signature": "eXesvDm3pkek12xSwMG10y9suRES79Nye3jYNe5KYq1tTUPqRRNgxmMGAfcUro0zpLeAr2YgHeSMWtglblGOW7pmwGVPZ0O1Y4r1fE6jnep0kW+35PLIaqCorIOnCAtSzDNKBhwd1ow3zW2wC0DFouuEkIO8u5Fw28g8E8dp8zEk1xMblNPy+xtWkmYHrVJ\/dQgun1bYOF2ZFtAzatwndTI\/bGsy1i3Wsl+x6HyWKQdq8y8VObtOqKDH7uERBEpB9DHVyKflj1v1gQuEH6BhaRdATc7ee0MiQdGblraIySwYRdfo2d8i82OVKrenMB3SLwyCvDPyQ9iKpTOnSF52ZBqaqSXKM2N\/RAkweeBFQQCwcHhqxvB0cfbyHcbkOLeCZe\/tsh68IxwTiYgzvLfl7sOZ5arnZbzrPpZmB+hfV2omkoJ1tDwOWz9hEmLLNtfo2OxyUH1m0+XFaC+Gbn4WkVDgf7YZkwUcG+Qoa3oKDNMss8MEyZxewl2iDGZcf402dlidHRprlfmXbAYuVQ08\/a0HxIKYPGh\/nsMGmwnO15CWtFpAbhUA\/D5oRjsIxnvX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0cZhZ600z+znh3jLJ0TYLvwN6\/qBQTUDaM2o\r\ng1+icMqXIXIeKuoPCVVsG7cCAwEAATANBgkqhkiG9w0BAQUFAAOCAgEAHc4F\/kOV\r\nHc8In5MmGg2YtjwZzjdeoC5TIPZczRqz0B+wRbJzN6aYryKZKLmP+wKpgRnJWDzp\r\nrgKGyyEQIAfK63DEv4B9p4N1B+B3aeMKsSpVcw7wbFTD57V5A7pURGoo31d0mw5L\r\nUIXZ2u+TUfGbzucMxLdFhTwjGpz9M6Kkm\/POxmV0tvLija5LdbdKnYR9BFmyu4IX\r\nqyoIAtComATNLl+3URu3SZxhE3NxhzMz+eAeNfh1KuIf2gWIIeDCXalVSJLym+OQ\r\nHFDpqRhJqfTMprrRlmmU7Zntgbj8\/RRZuXnBvH9cQ2KykLOb4UoCPlGUqOqKyP9m\r\nDJSFRiMJfpgMQUaJk1TLhKF+IR6FnmwURLEtkONJumtDQju9KaWPlhueONdyGi0p\r\nqxLVUo1Vb52XnPhk2GEEduxpDc9V5ePJ+pdcEdMifY\/uPNBRuBj2c87yq1DLH+U4\r\n3XzP1MlwjnBWZYuoFo0j6Jq0r\/MG6HjGdmkGIsRoheRi8Z8Scz5AW5QRkNz8pKop\r\nTELFqQy9g6TyQzzC8t6HZcpNe842ZUk4raEAbCZe\/XqxWMw5svPgNceBqM3fh7sZ\r\nBSykOHLaL8kiRO\/IS3y1yZEAuiWBvtxcTNLzBb+hdRpm2y8\/qH\/pKo+CMj1VzjNT\r\nD8YRQg0cjmDytJzHDrtV\/aTc9W1aPHun0vw=\r\n-----END CERTIFICATE-----"
  367. }';
  368. $this->fileAccessHelper
  369. ->expects($this->exactly(2))
  370. ->method('file_get_contents')
  371. ->withConsecutive(
  372. [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//appinfo/signature.json'],
  373. ['/resources/codesigning/root.crt'],
  374. )->willReturnOnConsecutiveCalls(
  375. $signatureDataFile,
  376. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  377. );
  378. $expected = [
  379. 'EXCEPTION' => [
  380. 'class' => 'OC\\IntegrityCheck\\Exceptions\\InvalidSignatureException',
  381. 'message' => 'Certificate is not valid for required scope. (Requested: SomeApp, current: CN=AnotherScope)',
  382. ],
  383. ];
  384. $this->assertSame($expected, $this->checker->verifyAppSignature('SomeApp'));
  385. }
  386. public function testVerifyAppWithDifferentScopeAndAlwaysTrustedCore(): void {
  387. $this->serverVersion
  388. ->expects($this->once())
  389. ->method('getChannel')
  390. ->willReturn('stable');
  391. $this->config
  392. ->expects($this->any())
  393. ->method('getSystemValueBool')
  394. ->with('integrity.check.disabled', false)
  395. ->willReturn(false);
  396. $this->appLocator
  397. ->expects($this->once())
  398. ->method('getAppPath')
  399. ->with('SomeApp')
  400. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/app/');
  401. $signatureDataFile = '{
  402. "hashes": {
  403. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  404. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  405. },
  406. "signature": "dYoohBaWIFR\/To1FXEbMQB5apUhVYlEauBGSPo12nq84wxWkBx2EM3KDRgkB5Sub2tr0CgmAc2EVjPhKIEzAam26cyUb48bJziz1V6wvW7z4GZAfaJpzLkyHdSfV5117VSf5w1rDcAeZDXfGUaaNEJPWytaF4ZIxVge7f3NGshHy4odFVPADy\/u6c43BWvaOtJ4m3aJQbP6sxCO9dxwcm5yJJJR3n36jfh229sdWBxyl8BhwhH1e1DEv78\/aiL6ckKFPVNzx01R6yDFt3TgEMR97YZ\/R6lWiXG+dsJ305jNFlusLu518zBUvl7g5yjzGN778H29b2C8VLZKmi\/h1CH9jGdD72fCqCYdenD2uZKzb6dsUtXtvBmVcVT6BUGz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this->fileAccessHelper
  410. ->expects($this->exactly(2))
  411. ->method('file_get_contents')
  412. ->withConsecutive(
  413. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//appinfo/signature.json'],
  414. ['/resources/codesigning/root.crt'],
  415. )->willReturnOnConsecutiveCalls(
  416. $signatureDataFile,
  417. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  418. );
  419. $this->assertSame([], $this->checker->verifyAppSignature('SomeApp'));
  420. }
  421. public function testWriteCoreSignatureWithException(): void {
  422. $this->expectException(\Exception::class);
  423. $this->expectExceptionMessage('Exception message');
  424. $this->fileAccessHelper
  425. ->expects($this->once())
  426. ->method('assertDirectoryExists')
  427. ->will($this->throwException(new \Exception('Exception message')));
  428. $this->fileAccessHelper
  429. ->expects($this->once())
  430. ->method('is_writable')
  431. ->with(__DIR__ . '/core')
  432. ->willReturn(true);
  433. $keyBundle = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.crt');
  434. $rsaPrivateKey = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.key');
  435. $rsa = new RSA();
  436. $rsa->loadKey($rsaPrivateKey);
  437. $x509 = new X509();
  438. $x509->loadX509($keyBundle);
  439. $this->checker->writeCoreSignature($x509, $rsa, __DIR__);
  440. }
  441. public function testWriteCoreSignatureWrongPermissions(): void {
  442. $this->expectException(\Exception::class);
  443. $this->expectExceptionMessageMatches('/[a-zA-Z\\/_-]+ is not writable/');
  444. $this->fileAccessHelper
  445. ->expects($this->once())
  446. ->method('assertDirectoryExists')
  447. ->will($this->throwException(new \Exception('Exception message')));
  448. $this->fileAccessHelper
  449. ->expects($this->once())
  450. ->method('is_writable')
  451. ->with(__DIR__ . '/core')
  452. ->willReturn(false);
  453. $keyBundle = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.crt');
  454. $rsaPrivateKey = file_get_contents(__DIR__ . '/../../data/integritycheck/SomeApp.key');
  455. $rsa = new RSA();
  456. $rsa->loadKey($rsaPrivateKey);
  457. $x509 = new X509();
  458. $x509->loadX509($keyBundle);
  459. $this->checker->writeCoreSignature($x509, $rsa, __DIR__);
  460. }
  461. public function testWriteCoreSignature(): void {
  462. $expectedSignatureFileData = '{
  463. "hashes": {
  464. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  465. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  466. },
  467. "signature": "dYoohBaWIFR\/To1FXEbMQB5apUhVYlEauBGSPo12nq84wxWkBx2EM3KDRgkB5Sub2tr0CgmAc2EVjPhKIEzAam26cyUb48bJziz1V6wvW7z4GZAfaJpzLkyHdSfV5117VSf5w1rDcAeZDXfGUaaNEJPWytaF4ZIxVge7f3NGshHy4odFVPADy\/u6c43BWvaOtJ4m3aJQbP6sxCO9dxwcm5yJJJR3n36jfh229sdWBxyl8BhwhH1e1DEv78\/aiL6ckKFPVNzx01R6yDFt3TgEMR97YZ\/R6lWiXG+dsJ305jNFlusLu518zBUvl7g5yjzGN778H29b2C8VLZKmi\/h1CH9jGdD72fCqCYdenD2uZKzb6dsUtXtvBmVcVT6BUGz41W1pkkEEB+YJpMrHILIxAiHRGv1+aZa9\/Oz8LWFd+BEUQjC2LJgojPnpzaG\/msw1nBkX16NNVDWWtJ25Bc\/r\/mG46rwjWB\/cmV6Lwt6KODiqlxgrC4lm9ALOCEWw+23OcYhLwNfQTYevXqHqsFfXOkhUnM8z5vDUb\/HBraB1DjFXN8iLK+1YewD4P495e+SRzrR79Oi3F8SEqRIzRLfN2rnW1BTms\/wYsz0p67cup1Slk1XlNmHwbWX25NVd2PPlLOvZRGoqcKFpIjC5few8THiZfyjiNFwt3RM0AFdZcXY=",
  468. "certificate": "-----BEGIN CERTIFICATE-----\r\nMIIEvjCCAqagAwIBAgIUc\/0Fx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sRjmtyR1e3A6Ji\r\noNCXUbExC\/0iCPUqdHZIVb+Lc\/vWuv4ByFMybGPydgtLoEUX2ZrKFWmcgZFDUSRd\r\n9Uj26vtUhCC4bU4jgu6hIrR9IuxOBLQUxGTRZyAcXvj7obqRAEZwFAKQgFpfpqTb\r\nH+kjcbZSaAlLVSF7vBc1syyI8RGYbqpwvtREqJtl5IEIwe6huEqJ3zPnlP2th\/55\r\ncf3Fovj6JJgbb9XFxrdnsOsDOu\/tpnaRWlvv5ib4+SzG5wWFT5UUEo4Wg2STQiiX\r\nuVSRQxK1LE1yg84bs3NZk9FSQh4B8vZVuRr5FaJsZZkwlFlhRO\/\/+TJtXRbyNgsf\r\noMRZGi8DLGU2SGEAHcRH\/QZHq\/XDUWVzdxrSBYcy7GSpT7UDVzGv1rEJUrn5veP1\r\n0KmauAqtiIaYRm4f6YBsn0INcZxzIPZ0p8qFtVZBPeHhvQtvOt0iXI\/XUxEWOa2F\r\nK2EqhErgMK\/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  469. }';
  470. $this->environmentHelper
  471. ->expects($this->any())
  472. ->method('getServerRoot')
  473. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/app/');
  474. $this->fileAccessHelper
  475. ->expects($this->once())
  476. ->method('file_put_contents')
  477. ->with(
  478. \OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json',
  479. $this->callback(function ($signature) use ($expectedSignatureFileData) {
  480. $expectedArray = json_decode($expectedSignatureFileData, true);
  481. $actualArray = json_decode($signature, true);
  482. $this->assertEquals($expectedArray, $actualArray);
  483. return true;
  484. })
  485. );
  486. $keyBundle = file_get_contents(__DIR__ . '/../../data/integritycheck/core.crt');
  487. $rsaPrivateKey = file_get_contents(__DIR__ . '/../../data/integritycheck/core.key');
  488. $rsa = new RSA();
  489. $rsa->loadKey($rsaPrivateKey);
  490. $x509 = new X509();
  491. $x509->loadX509($keyBundle);
  492. $this->checker->writeCoreSignature($x509, $rsa, \OC::$SERVERROOT . '/tests/data/integritycheck/app/');
  493. }
  494. public function testWriteCoreSignatureWithUnmodifiedHtaccess(): void {
  495. $expectedSignatureFileData = '{
  496. "hashes": {
  497. ".htaccess": "dc479770a6232061e04a768ee1f9133fdb3aea7b3a99f7105b0e0b6197474733e8d14b5b2bbad054e6b62a410fe5d0b3d790242dee1e0f11274af2100f5289e2",
  498. "subfolder\/.htaccess": "2c57b1e25050e11dc3ae975832f378c452159f7b69f818e47eeeafadd6ba568517461dcb4d843b90b906cd7c89d161bc1b89dff8e3ae0eb6f5088508c47befd1"
  499. },
  500. "signature": "nRtR377DB\/I\/4hmh9q3elMQYfSHnQFlNtjchNgrdfmUQqVmgkU\/4qgGyxDqYkV8mSMbH2gYysfP42nx\/3zSo7n0dBYDfU87Q6f96Cv597vEV27do8CaBkEk8Xjn2SxhHw8hVxracvE2OBAPxk0H3sRp\/cQBgjoXpju4kQin0N5E+DEJMh7Sp+u8aKoFpb+2FaAZJFn\/hnqxLTlVi2nyDxGL3U0eobWY+jWH9XPt52v3Hyh8TDhcAnQ1cN30B8Jn2+jkrm8ib+buchaCXHk0cPX72xuPECdwOEKLCBNrJa3FGSvO1zWiecnCgxCXgt+R8hUgsVPTsbrdFY2YRJGIhHndYZL98XzgG7cw85SnnMMe2SulzeL7xANGF8qiEVyiC7x83bbj5xOkeM\/CUTajrLBO3vyZ23KKOxvskjgI0t+Zw1zFsl+sYW0\/O\/V5WzPOwMwV8+iApQ8k9gEMiYQg98QLEMYnSohncmp0Z9qx2qFcQuHLcKJVa1J6wGtE\/EHR\/4d0aYPd6IRjg+qshCJmdzud\/12xjpGTl+BT0Hi0VsU5o7ZMi7WhmukZmmv8u0uZsvKREQNATm4cO4WCkYySt5O9gZEJOF+j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ZdXPpFuq+CTQJ\r\nQ238lC+A\/KOVAgMBAAEwDQYJKoZIhvcNAQEFBQADggIBAGoKTnh8RfJV4sQItVC2\r\nAvfJagkrIqZ3iiQTUBQGTKBsTnAqE1H7QgUSV9vSd+8rgvHkyZsRjmtyR1e3A6Ji\r\noNCXUbExC\/0iCPUqdHZIVb+Lc\/vWuv4ByFMybGPydgtLoEUX2ZrKFWmcgZFDUSRd\r\n9Uj26vtUhCC4bU4jgu6hIrR9IuxOBLQUxGTRZyAcXvj7obqRAEZwFAKQgFpfpqTb\r\nH+kjcbZSaAlLVSF7vBc1syyI8RGYbqpwvtREqJtl5IEIwe6huEqJ3zPnlP2th\/55\r\ncf3Fovj6JJgbb9XFxrdnsOsDOu\/tpnaRWlvv5ib4+SzG5wWFT5UUEo4Wg2STQiiX\r\nuVSRQxK1LE1yg84bs3NZk9FSQh4B8vZVuRr5FaJsZZkwlFlhRO\/\/+TJtXRbyNgsf\r\noMRZGi8DLGU2SGEAHcRH\/QZHq\/XDUWVzdxrSBYcy7GSpT7UDVzGv1rEJUrn5veP1\r\n0KmauAqtiIaYRm4f6YBsn0INcZxzIPZ0p8qFtVZBPeHhvQtvOt0iXI\/XUxEWOa2F\r\nK2EqhErgMK\/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  502. }';
  503. $this->environmentHelper
  504. ->expects($this->any())
  505. ->method('getServerRoot')
  506. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/htaccessUnmodified/');
  507. $this->fileAccessHelper
  508. ->expects($this->once())
  509. ->method('file_put_contents')
  510. ->with(
  511. \OC::$SERVERROOT . '/tests/data/integritycheck/htaccessUnmodified//core/signature.json',
  512. $this->callback(function ($signature) use ($expectedSignatureFileData) {
  513. $expectedArray = json_decode($expectedSignatureFileData, true);
  514. $actualArray = json_decode($signature, true);
  515. $this->assertEquals($expectedArray, $actualArray);
  516. return true;
  517. })
  518. );
  519. $keyBundle = file_get_contents(__DIR__ . '/../../data/integritycheck/core.crt');
  520. $rsaPrivateKey = file_get_contents(__DIR__ . '/../../data/integritycheck/core.key');
  521. $rsa = new RSA();
  522. $rsa->loadKey($rsaPrivateKey);
  523. $x509 = new X509();
  524. $x509->loadX509($keyBundle);
  525. $this->checker->writeCoreSignature($x509, $rsa, \OC::$SERVERROOT . '/tests/data/integritycheck/htaccessUnmodified/');
  526. }
  527. public function testWriteCoreSignatureWithInvalidModifiedHtaccess(): void {
  528. $expectedSignatureFileData = '{
  529. "hashes": {
  530. ".htaccess": "4a54273dc8d697b2ca615acf2ae2c1ee3c1c643492cb04f42b10984fa9aacff1420dc829fd82f93ad3476fbd0cdab0251142c887dc8f872d03e39a3a3eb6d381"
  531. },
  532. "signature": "qpDddYGgAKNR3TszOgjPXRphUl2P9Ym5OQaetltocgZASGDkOun5D64+1D0QJRKb4SG2+48muxGOHyL2Ngos4NUrrSR+SIkywZacay82YQBCEdr7\/4MjW1WHRPjvboLwEJwViw0EdAjsWRpD68aPnzUGrGsy2BsCo06P5iwjk9cXcHxdjC9R39npvoC3QNvQ2jmNIbh1Lc4U97dbb+CsXEQCLU1OSa9p3q6cEFV98Easwt7uF\/DzHK+CbeZlxVZ0DwLh2\/ylT1PyGou8QC1b3vKAnPjLWMO+UsCPpCKhk3C5pV+5etQ8puGd+0x2t5tEU+qXxLzek91zWNC+rqgC\/WlqLKbwPb\/BCHs4zLGV55Q2fEQmT21x0KCUELdPs4dBnYP4Ox5tEDugtJujWFzOHzoY6gGa\/BY\/78pSZXmq9o8dWkBEtioWWvaNZ1rM0ddE83GBlBTgjigi9Ay1D++bUW\/FCBB7CMk6qyNlV81H+cBuIEODw2aymmkM9LLDD2Qbmvo8gHEPRjiQxPC5OpDlcdSNiL+zcxVxeuX4FpT+9x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0C\/aKc\r\nbDIjujpL1s9HO05pcD\/V3wKcPZ1izymBkmMyIbL52iRVN5FTVHeZdXPpFuq+CTQJ\r\nQ238lC+A\/KOVAgMBAAEwDQYJKoZIhvcNAQEFBQADggIBAGoKTnh8RfJV4sQItVC2\r\nAvfJagkrIqZ3iiQTUBQGTKBsTnAqE1H7QgUSV9vSd+8rgvHkyZsRjmtyR1e3A6Ji\r\noNCXUbExC\/0iCPUqdHZIVb+Lc\/vWuv4ByFMybGPydgtLoEUX2ZrKFWmcgZFDUSRd\r\n9Uj26vtUhCC4bU4jgu6hIrR9IuxOBLQUxGTRZyAcXvj7obqRAEZwFAKQgFpfpqTb\r\nH+kjcbZSaAlLVSF7vBc1syyI8RGYbqpwvtREqJtl5IEIwe6huEqJ3zPnlP2th\/55\r\ncf3Fovj6JJgbb9XFxrdnsOsDOu\/tpnaRWlvv5ib4+SzG5wWFT5UUEo4Wg2STQiiX\r\nuVSRQxK1LE1yg84bs3NZk9FSQh4B8vZVuRr5FaJsZZkwlFlhRO\/\/+TJtXRbyNgsf\r\noMRZGi8DLGU2SGEAHcRH\/QZHq\/XDUWVzdxrSBYcy7GSpT7UDVzGv1rEJUrn5veP1\r\n0KmauAqtiIaYRm4f6YBsn0INcZxzIPZ0p8qFtVZBPeHhvQtvOt0iXI\/XUxEWOa2F\r\nK2EqhErgMK\/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  534. }';
  535. $this->fileAccessHelper
  536. ->expects($this->once())
  537. ->method('file_put_contents')
  538. ->with(
  539. \OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithInvalidModifiedContent//core/signature.json',
  540. $this->callback(function ($signature) use ($expectedSignatureFileData) {
  541. $expectedArray = json_decode($expectedSignatureFileData, true);
  542. $actualArray = json_decode($signature, true);
  543. $this->assertEquals($expectedArray, $actualArray);
  544. return true;
  545. })
  546. );
  547. $keyBundle = file_get_contents(__DIR__ . '/../../data/integritycheck/core.crt');
  548. $rsaPrivateKey = file_get_contents(__DIR__ . '/../../data/integritycheck/core.key');
  549. $rsa = new RSA();
  550. $rsa->loadKey($rsaPrivateKey);
  551. $x509 = new X509();
  552. $x509->loadX509($keyBundle);
  553. $this->checker->writeCoreSignature($x509, $rsa, \OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithInvalidModifiedContent/');
  554. }
  555. public function testWriteCoreSignatureWithValidModifiedHtaccess(): void {
  556. $expectedSignatureFileData = '{
  557. "hashes": {
  558. ".htaccess": "7e6a7a4d8ee4f3fbc45dd579407c643471575a9d127d1c75f6d0a49e80766c3c587104b2139ef76d2a4bffce3f45777900605aaa49519c9532909b71e5030227",
  559. "subfolder\/.htaccess": "2c57b1e25050e11dc3ae975832f378c452159f7b69f818e47eeeafadd6ba568517461dcb4d843b90b906cd7c89d161bc1b89dff8e3ae0eb6f5088508c47befd1"
  560. },
  561. "signature": "YVwQvl9Dh8UebCumfgzFxfz3NiZJLmYG8oJVTfEBhulI4KXBnTG1jZTprf4XxG2XIriEYAZXsoXpu9xWsUFe9QfdncwoEpqJtGq7l6aVDTofX5Be5b03MQFJr4cflgllqW77QZ84D9O9qWF\/vNDAofXcwrzT04CxLDhyQgTCgYUnRjG9pnuP\/gtbDKbTjRvxhTyfg3T0Phv1+XAvpTPnH2q5A+1+LmiqziUJ1sMipsKo+jQP614eCi9qjmqhHIgLRgcuOBvsi4g5WUcdcAIZ6qLt5gm2Y3r6rKNVchosU9ZydMUTfjuejDbVwE2fNH5UUnV57fQBxwg9CfX7iFHqKv1bfv5Zviu12paShgWCB12uR3iH\/3lmTJn8K5Xqit3G4eymFaJ5IChdUThBp\/jhQSI2r8sPcZDYSJ\/UZKuFnezFdKhEBd5hMXe8aKAd6ijGDjLARksFuqpi1sS8llC5K1Q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BZ\/zrM0\r\ni8nfCFwTxWRxp3H9KoECzO\/zS5R5KIS7s3\/wq\/w9T2Ie4rcecgXwDizwnn0C\/aKc\r\nbDIjujpL1s9HO05pcD\/V3wKcPZ1izymBkmMyIbL52iRVN5FTVHeZdXPpFuq+CTQJ\r\nQ238lC+A\/KOVAgMBAAEwDQYJKoZIhvcNAQEFBQADggIBAGoKTnh8RfJV4sQItVC2\r\nAvfJagkrIqZ3iiQTUBQGTKBsTnAqE1H7QgUSV9vSd+8rgvHkyZsRjmtyR1e3A6Ji\r\noNCXUbExC\/0iCPUqdHZIVb+Lc\/vWuv4ByFMybGPydgtLoEUX2ZrKFWmcgZFDUSRd\r\n9Uj26vtUhCC4bU4jgu6hIrR9IuxOBLQUxGTRZyAcXvj7obqRAEZwFAKQgFpfpqTb\r\nH+kjcbZSaAlLVSF7vBc1syyI8RGYbqpwvtREqJtl5IEIwe6huEqJ3zPnlP2th\/55\r\ncf3Fovj6JJgbb9XFxrdnsOsDOu\/tpnaRWlvv5ib4+SzG5wWFT5UUEo4Wg2STQiiX\r\nuVSRQxK1LE1yg84bs3NZk9FSQh4B8vZVuRr5FaJsZZkwlFlhRO\/\/+TJtXRbyNgsf\r\noMRZGi8DLGU2SGEAHcRH\/QZHq\/XDUWVzdxrSBYcy7GSpT7UDVzGv1rEJUrn5veP1\r\n0KmauAqtiIaYRm4f6YBsn0INcZxzIPZ0p8qFtVZBPeHhvQtvOt0iXI\/XUxEWOa2F\r\nK2EqhErgMK\/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  563. }';
  564. $this->environmentHelper
  565. ->expects($this->any())
  566. ->method('getServerRoot')
  567. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithValidModifiedContent');
  568. $this->fileAccessHelper
  569. ->expects($this->once())
  570. ->method('file_put_contents')
  571. ->with(
  572. \OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithValidModifiedContent/core/signature.json',
  573. $this->callback(function ($signature) use ($expectedSignatureFileData) {
  574. $expectedArray = json_decode($expectedSignatureFileData, true);
  575. $actualArray = json_decode($signature, true);
  576. $this->assertEquals($expectedArray, $actualArray);
  577. return true;
  578. })
  579. );
  580. $keyBundle = file_get_contents(__DIR__ . '/../../data/integritycheck/core.crt');
  581. $rsaPrivateKey = file_get_contents(__DIR__ . '/../../data/integritycheck/core.key');
  582. $rsa = new RSA();
  583. $rsa->loadKey($rsaPrivateKey);
  584. $x509 = new X509();
  585. $x509->loadX509($keyBundle);
  586. $this->checker->writeCoreSignature($x509, $rsa, \OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithValidModifiedContent');
  587. }
  588. public function testVerifyCoreSignatureWithoutSignatureData(): void {
  589. $this->serverVersion
  590. ->expects($this->once())
  591. ->method('getChannel')
  592. ->willReturn('stable');
  593. $this->config
  594. ->expects($this->any())
  595. ->method('getSystemValueBool')
  596. ->with('integrity.check.disabled', false)
  597. ->willReturn(false);
  598. $expected = [
  599. 'EXCEPTION' => [
  600. 'class' => 'OC\\IntegrityCheck\\Exceptions\\InvalidSignatureException',
  601. 'message' => 'Signature data not found.',
  602. ],
  603. ];
  604. $this->assertSame($expected, $this->checker->verifyCoreSignature());
  605. }
  606. public function testVerifyCoreSignatureWithValidSignatureData(): void {
  607. $this->serverVersion
  608. ->expects($this->once())
  609. ->method('getChannel')
  610. ->willReturn('stable');
  611. $this->config
  612. ->expects($this->any())
  613. ->method('getSystemValueBool')
  614. ->with('integrity.check.disabled', false)
  615. ->willReturn(false);
  616. $this->environmentHelper
  617. ->expects($this->any())
  618. ->method('getServerRoot')
  619. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/app/');
  620. $signatureDataFile = '{
  621. "hashes": {
  622. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  623. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  624. },
  625. "signature": "dYoohBaWIFR\/To1FXEbMQB5apUhVYlEauBGSPo12nq84wxWkBx2EM3KDRgkB5Sub2tr0CgmAc2EVjPhKIEzAam26cyUb48bJziz1V6wvW7z4GZAfaJpzLkyHdSfV5117VSf5w1rDcAeZDXfGUaaNEJPWytaF4ZIxVge7f3NGshHy4odFVPADy\/u6c43BWvaOtJ4m3aJQbP6sxCO9dxwcm5yJJJR3n36jfh229sdWBxyl8BhwhH1e1DEv78\/aiL6ckKFPVNzx01R6yDFt3TgEMR97YZ\/R6lWiXG+dsJ305jNFlusLu518zBUvl7g5yjzGN778H29b2C8VLZKmi\/h1CH9jGdD72fCqCYdenD2uZKzb6dsUtXtvBmVcVT6BUGz41W1pkkEEB+YJpMrHILIxAiHRGv1+aZa9\/Oz8LWFd+BEUQjC2LJgojPnpzaG\/msw1nBkX16NNVDWWtJ25Bc\/r\/mG46rwjWB\/cmV6Lwt6KODiqlxgrC4lm9ALOCEWw+23OcYhLwNfQTYevXqHqsFfXOkhUnM8z5vDUb\/HBraB1DjFXN8iLK+1YewD4P495e+SRzrR79Oi3F8SEqRIzRLfN2rnW1BTms\/wYsz0p67cup1Slk1XlNmHwbWX25NVd2PPlLOvZRGoqcKFpIjC5few8THiZfyjiNFwt3RM0AFdZcXY=",
  626. "certificate": "-----BEGIN CERTIFICATE-----\r\nMIIEvjCCAqagAwIBAgIUc\/0FxYrsgSs9rDxp03EJmbjN0NwwDQYJKoZIhvcNAQEF\r\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTEw\r\nMzIxMDMzM1oXDTE2MT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AcXvj7obqRAEZwFAKQgFpfpqTb\r\nH+kjcbZSaAlLVSF7vBc1syyI8RGYbqpwvtREqJtl5IEIwe6huEqJ3zPnlP2th\/55\r\ncf3Fovj6JJgbb9XFxrdnsOsDOu\/tpnaRWlvv5ib4+SzG5wWFT5UUEo4Wg2STQiiX\r\nuVSRQxK1LE1yg84bs3NZk9FSQh4B8vZVuRr5FaJsZZkwlFlhRO\/\/+TJtXRbyNgsf\r\noMRZGi8DLGU2SGEAHcRH\/QZHq\/XDUWVzdxrSBYcy7GSpT7UDVzGv1rEJUrn5veP1\r\n0KmauAqtiIaYRm4f6YBsn0INcZxzIPZ0p8qFtVZBPeHhvQtvOt0iXI\/XUxEWOa2F\r\nK2EqhErgMK\/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  627. }';
  628. $this->fileAccessHelper
  629. ->expects($this->exactly(2))
  630. ->method('file_get_contents')
  631. ->withConsecutive(
  632. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json'],
  633. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt'],
  634. )->willReturnOnConsecutiveCalls(
  635. $signatureDataFile,
  636. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  637. );
  638. $this->assertSame([], $this->checker->verifyCoreSignature());
  639. }
  640. public function testVerifyCoreSignatureWithValidModifiedHtaccessSignatureData(): void {
  641. $this->serverVersion
  642. ->expects($this->once())
  643. ->method('getChannel')
  644. ->willReturn('stable');
  645. $this->config
  646. ->expects($this->any())
  647. ->method('getSystemValueBool')
  648. ->with('integrity.check.disabled', false)
  649. ->willReturn(false);
  650. $this->environmentHelper
  651. ->expects($this->any())
  652. ->method('getServerRoot')
  653. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithValidModifiedContent');
  654. $signatureDataFile = '{
  655. "hashes": {
  656. ".htaccess": "7e6a7a4d8ee4f3fbc45dd579407c643471575a9d127d1c75f6d0a49e80766c3c587104b2139ef76d2a4bffce3f45777900605aaa49519c9532909b71e5030227",
  657. "subfolder\/.htaccess": "2c57b1e25050e11dc3ae975832f378c452159f7b69f818e47eeeafadd6ba568517461dcb4d843b90b906cd7c89d161bc1b89dff8e3ae0eb6f5088508c47befd1"
  658. },
  659. "signature": "YVwQvl9Dh8UebCumfgzFxfz3NiZJLmYG8oJVTfEBhulI4KXBnTG1jZTprf4XxG2XIriEYAZXsoXpu9xWsUFe9QfdncwoEpqJtGq7l6aVDTofX5Be5b03MQFJr4cflgllqW77QZ84D9O9qWF\/vNDAofXcwrzT04CxLDhyQgTCgYUnRjG9pnuP\/gtbDKbTjRvxhTyfg3T0Phv1+XAvpTPnH2q5A+1+LmiqziUJ1sMipsKo+jQP614eCi9qjmqhHIgLRgcuOBvsi4g5WUcdcAIZ6qLt5gm2Y3r6rKNVchosU9ZydMUTfjuejDbVwE2fNH5UUnV57fQBxwg9CfX7iFHqKv1bfv5Zviu12paShgWCB12uR3iH\/3lmTJn8K5Xqit3G4eymFaJ5IChdUThBp\/jhQSI2r8sPcZDYSJ\/UZKuFnezFdKhEBd5hMXe8aKAd6ijGDjLARksFuqpi1sS8llC5K1Q+DzktSL\/o64TY4Vuvykiwe\/BAk2SkL9voOtrvU7vfDBcuCPbDJnSBBC0ESpcXeClTBIn6xZ9WaxqoS7sinE\/kUwtWsRd04I7d79\/ouotyNb+mBhTuRsZT12p\/gn4JHXXNUAIpTwchYzGxbfNJ4kxnYBFZWVmvsSqOLFZu1yi5BP3ktA9yhFyWIa5659azRFEKRdXpVHtQVa4IgdhxEqA=",
  660. "certificate": "-----BEGIN CERTIFICATE-----\r\nMIIEvjCCAqagAwIBAgIUc\/0FxYrsgSs9rDxp03EJmbjN0NwwDQYJKoZIhvcNAQE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IVb+Lc\/vWuv4ByFMybGPydgtLoEUX2ZrKFWmcgZFDUSRd\r\n9Uj26vtUhCC4bU4jgu6hIrR9IuxOBLQUxGTRZyAcXvj7obqRAEZwFAKQgFpfpqTb\r\nH+kjcbZSaAlLVSF7vBc1syyI8RGYbqpwvtREqJtl5IEIwe6huEqJ3zPnlP2th\/55\r\ncf3Fovj6JJgbb9XFxrdnsOsDOu\/tpnaRWlvv5ib4+SzG5wWFT5UUEo4Wg2STQiiX\r\nuVSRQxK1LE1yg84bs3NZk9FSQh4B8vZVuRr5FaJsZZkwlFlhRO\/\/+TJtXRbyNgsf\r\noMRZGi8DLGU2SGEAHcRH\/QZHq\/XDUWVzdxrSBYcy7GSpT7UDVzGv1rEJUrn5veP1\r\n0KmauAqtiIaYRm4f6YBsn0INcZxzIPZ0p8qFtVZBPeHhvQtvOt0iXI\/XUxEWOa2F\r\nK2EqhErgMK\/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  661. }';
  662. $this->fileAccessHelper
  663. ->expects($this->exactly(2))
  664. ->method('file_get_contents')
  665. ->withConsecutive(
  666. [\OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithValidModifiedContent/core/signature.json'],
  667. [\OC::$SERVERROOT . '/tests/data/integritycheck/htaccessWithValidModifiedContent/resources/codesigning/root.crt'],
  668. )
  669. ->willReturnOnConsecutiveCalls(
  670. $signatureDataFile,
  671. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  672. );
  673. $this->assertSame([], $this->checker->verifyCoreSignature());
  674. }
  675. /**
  676. * See inline instruction on how to update the test assets when changing mimetypealiases.dist.json
  677. */
  678. public function testVerifyCoreSignatureWithModifiedMimetypelistSignatureData(): void {
  679. $shippedMimetypeAliases = (array)json_decode(file_get_contents(\OC::$SERVERROOT . '/resources/config/mimetypealiases.dist.json'));
  680. $allAliases = array_merge($shippedMimetypeAliases, ['my-custom/mimetype' => 'custom']);
  681. $this->mimeTypeDetector
  682. ->method('getOnlyDefaultAliases')
  683. ->willReturn($shippedMimetypeAliases);
  684. $this->mimeTypeDetector
  685. ->method('getAllAliases')
  686. ->willReturn($allAliases);
  687. $oldMimetypeList = new GenerateMimetypeFileBuilder();
  688. $all = $this->mimeTypeDetector->getAllAliases();
  689. $newFile = $oldMimetypeList->generateFile($all);
  690. // When updating the mimetype list the test assets need to be updated as well
  691. // 1. Update core/js/mimetypelist.js with the new generated js by running the test with the next line uncommented:
  692. // file_put_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/mimetypeListModified/core/js/mimetypelist.js', $newFile);
  693. // 2. Update signature.json using the following occ command:
  694. // occ integrity:sign-core --privateKey=./tests/data/integritycheck/core.key --certificate=./tests/data/integritycheck/core.crt --path=./tests/data/integritycheck/mimetypeListModified
  695. self::assertEquals($newFile, file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/mimetypeListModified/core/js/mimetypelist.js'));
  696. $this->serverVersion
  697. ->expects($this->once())
  698. ->method('getChannel')
  699. ->willReturn('stable');
  700. $this->config
  701. ->expects($this->any())
  702. ->method('getSystemValueBool')
  703. ->with('integrity.check.disabled', false)
  704. ->willReturn(false);
  705. $this->environmentHelper
  706. ->expects($this->any())
  707. ->method('getServerRoot')
  708. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/mimetypeListModified');
  709. $signatureDataFile = file_get_contents(__DIR__ . '/../../data/integritycheck/mimetypeListModified/core/signature.json');
  710. $this->fileAccessHelper
  711. ->method('file_get_contents')
  712. ->willReturnMap([
  713. [\OC::$SERVERROOT . '/tests/data/integritycheck/mimetypeListModified/core/signature.json', $signatureDataFile],
  714. [\OC::$SERVERROOT . '/tests/data/integritycheck/mimetypeListModified/resources/codesigning/root.crt', file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')],
  715. ]);
  716. $this->assertSame([], $this->checker->verifyCoreSignature());
  717. }
  718. public function testVerifyCoreSignatureWithValidSignatureDataAndNotAlphabeticOrder(): void {
  719. $this->serverVersion
  720. ->expects($this->once())
  721. ->method('getChannel')
  722. ->willReturn('stable');
  723. $this->config
  724. ->expects($this->any())
  725. ->method('getSystemValueBool')
  726. ->with('integrity.check.disabled', false)
  727. ->willReturn(false);
  728. $this->environmentHelper
  729. ->expects($this->any())
  730. ->method('getServerRoot')
  731. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/app/');
  732. $signatureDataFile = '{
  733. "hashes": {
  734. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  735. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  736. },
  737. "signature": "dYoohBaWIFR\/To1FXEbMQB5apUhVYlEauBGSPo12nq84wxWkBx2EM3KDRgkB5Sub2tr0CgmAc2EVjPhKIEzAam26cyUb48bJziz1V6wvW7z4GZAfaJpzLkyHdSfV5117VSf5w1rDcAeZDXfGUaaNEJPWytaF4ZIxVge7f3NGshHy4odFVPADy\/u6c43BWvaOtJ4m3aJQbP6sxCO9dxwcm5yJJJR3n36jfh229sdWBxyl8BhwhH1e1DEv78\/aiL6ckKFPVNzx01R6yDFt3TgEMR97YZ\/R6lWiXG+dsJ305jNFlusLu518zBUvl7g5yjzGN778H29b2C8VLZKmi\/h1CH9jGdD72fCqCYdenD2uZKzb6dsUtXtvBmVcVT6BUGz41W1pkkEEB+YJpMrHILIxAiHRGv1+aZa9\/Oz8LWFd+BEUQjC2LJgojPnpzaG\/msw1nBkX16NNVDWWtJ25Bc\/r\/mG46rwjWB\/cmV6Lwt6KODiqlxgrC4lm9ALOCEWw+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S7s3\/wq\/w9T2Ie4rcecgXwDizwnn0C\/aKc\r\nbDIjujpL1s9HO05pcD\/V3wKcPZ1izymBkmMyIbL52iRVN5FTVHeZdXPpFuq+CTQJ\r\nQ238lC+A\/KOVAgMBAAEwDQYJKoZIhvcNAQEFBQADggIBAGoKTnh8RfJV4sQItVC2\r\nAvfJagkrIqZ3iiQTUBQGTKBsTnAqE1H7QgUSV9vSd+8rgvHkyZsRjmtyR1e3A6Ji\r\noNCXUbExC\/0iCPUqdHZIVb+Lc\/vWuv4ByFMybGPydgtLoEUX2ZrKFWmcgZFDUSRd\r\n9Uj26vtUhCC4bU4jgu6hIrR9IuxOBLQUxGTRZyAcXvj7obqRAEZwFAKQgFpfpqTb\r\nH+kjcbZSaAlLVSF7vBc1syyI8RGYbqpwvtREqJtl5IEIwe6huEqJ3zPnlP2th\/55\r\ncf3Fovj6JJgbb9XFxrdnsOsDOu\/tpnaRWlvv5ib4+SzG5wWFT5UUEo4Wg2STQiiX\r\nuVSRQxK1LE1yg84bs3NZk9FSQh4B8vZVuRr5FaJsZZkwlFlhRO\/\/+TJtXRbyNgsf\r\noMRZGi8DLGU2SGEAHcRH\/QZHq\/XDUWVzdxrSBYcy7GSpT7UDVzGv1rEJUrn5veP1\r\n0KmauAqtiIaYRm4f6YBsn0INcZxzIPZ0p8qFtVZBPeHhvQtvOt0iXI\/XUxEWOa2F\r\nK2EqhErgMK\/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  739. }';
  740. $this->fileAccessHelper
  741. ->expects($this->exactly(2))
  742. ->method('file_get_contents')
  743. ->withConsecutive(
  744. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json'],
  745. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt'],
  746. )->willReturnOnConsecutiveCalls(
  747. $signatureDataFile,
  748. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  749. );
  750. $this->assertSame([], $this->checker->verifyCoreSignature());
  751. }
  752. public function testVerifyCoreSignatureWithTamperedSignatureData(): void {
  753. $this->serverVersion
  754. ->expects($this->once())
  755. ->method('getChannel')
  756. ->willReturn('stable');
  757. $this->config
  758. ->expects($this->any())
  759. ->method('getSystemValueBool')
  760. ->with('integrity.check.disabled', false)
  761. ->willReturn(false);
  762. $this->environmentHelper
  763. ->expects($this->any())
  764. ->method('getServerRoot')
  765. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData/');
  766. $signatureDataFile = '{
  767. "hashes": {
  768. "AnotherFile.txt": "tampered",
  769. "subfolder\/file.txt": "tampered"
  770. },
  771. "signature": "eXesvDm3pkek12xSwMG10y9suRES79Nye3jYNe5KYq1tTUPqRRNgxmMGAfcUro0zpLeAr2YgHeSMWtglblGOW7pmwGVPZ0O1Y4r1fE6jnep0kW+35PLIaqCorIOnCAtSzDNKBhwd1ow3zW2wC0DFouuEkIO8u5Fw28g8E8dp8zEk1xMblNPy+xtWkmYHrVJ\/dQgun1bYOF2ZFtAzatwndTI\/bGsy1i3Wsl+x6HyWKQdq8y8VObtOqKDH7uERBEpB9DHVyKflj1v1gQuEH6BhaRdATc7ee0MiQdGblraIySwYRdfo2d8i82OVKrenMB3SLwyCvDPyQ9iKpTOnSF52ZBqaqSXKM2N\/RAkweeBFQQCwcHhqxvB0cfbyHcbkOLeCZe\/tsh68IxwTiYgzvLfl7sOZ5arnZbzrPpZmB+hfV2omkoJ1tDwOWz9hEmLLNtfo2OxyUH1m0+XFaC+Gbn4WkVDgf7YZkwUcG+Qoa3oKDNMss8MEyZxewl2iDGZcf402dlidHRprlfmXbAYuVQ08\/a0HxIKYPGh\/nsMGmwnO15CWtFpAbhUA\/D5oRjsIxnvXaMDg0iAFpdu\/5Ffsj7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5FTVHeZdXPpFuq+CTQJ\r\nQ238lC+A\/KOVAgMBAAEwDQYJKoZIhvcNAQEFBQADggIBAGoKTnh8RfJV4sQItVC2\r\nAvfJagkrIqZ3iiQTUBQGTKBsTnAqE1H7QgUSV9vSd+8rgvHkyZsRjmtyR1e3A6Ji\r\noNCXUbExC\/0iCPUqdHZIVb+Lc\/vWuv4ByFMybGPydgtLoEUX2ZrKFWmcgZFDUSRd\r\n9Uj26vtUhCC4bU4jgu6hIrR9IuxOBLQUxGTRZyAcXvj7obqRAEZwFAKQgFpfpqTb\r\nH+kjcbZSaAlLVSF7vBc1syyI8RGYbqpwvtREqJtl5IEIwe6huEqJ3zPnlP2th\/55\r\ncf3Fovj6JJgbb9XFxrdnsOsDOu\/tpnaRWlvv5ib4+SzG5wWFT5UUEo4Wg2STQiiX\r\nuVSRQxK1LE1yg84bs3NZk9FSQh4B8vZVuRr5FaJsZZkwlFlhRO\/\/+TJtXRbyNgsf\r\noMRZGi8DLGU2SGEAHcRH\/QZHq\/XDUWVzdxrSBYcy7GSpT7UDVzGv1rEJUrn5veP1\r\n0KmauAqtiIaYRm4f6YBsn0INcZxzIPZ0p8qFtVZBPeHhvQtvOt0iXI\/XUxEWOa2F\r\nK2EqhErgMK\/N07U1JJJay5tYZRtvkGq46oP\/5kQG8hYST0MDK6VihJoPpvCmAm4E\r\npEYKQ96x6A4EH9Y9mZlYozH\/eqmxPbTK8n89\/p7Ydun4rI+B2iiLnY8REWWy6+UQ\r\nV204fGUkJqW5CrKy3P3XvY9X\r\n-----END CERTIFICATE-----"
  773. }';
  774. $this->fileAccessHelper
  775. ->expects($this->exactly(2))
  776. ->method('file_get_contents')
  777. ->withConsecutive(
  778. [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//core/signature.json'],
  779. [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//resources/codesigning/root.crt'],
  780. )->willReturnOnConsecutiveCalls(
  781. $signatureDataFile,
  782. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  783. );
  784. $expected = [
  785. 'EXCEPTION' => [
  786. 'class' => 'OC\\IntegrityCheck\\Exceptions\\InvalidSignatureException',
  787. 'message' => 'Signature could not get verified.',
  788. ]
  789. ];
  790. $this->assertSame($expected, $this->checker->verifyCoreSignature());
  791. }
  792. public function testVerifyCoreSignatureWithTamperedFiles(): void {
  793. $this->serverVersion
  794. ->expects($this->once())
  795. ->method('getChannel')
  796. ->willReturn('stable');
  797. $this->config
  798. ->expects($this->any())
  799. ->method('getSystemValueBool')
  800. ->with('integrity.check.disabled', false)
  801. ->willReturn(false);
  802. $this->environmentHelper
  803. ->expects($this->any())
  804. ->method('getServerRoot')
  805. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData/');
  806. $signatureDataFile = '{
  807. "hashes": {
  808. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  809. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  810. },
  811. "signature": "dYoohBaWIFR\/To1FXEbMQB5apUhVYlEauBGSPo12nq84wxWkBx2EM3KDRgkB5Sub2tr0CgmAc2EVjPhKIEzAam26cyUb48bJziz1V6wvW7z4GZAfaJpzLkyHdSfV5117VSf5w1rDcAeZDXfGUaaNEJPWytaF4ZIxVge7f3NGshHy4odFVPADy\/u6c43BWvaOtJ4m3aJQbP6sxCO9dxwcm5yJJJR3n36jfh229sdWBxyl8BhwhH1e1DEv78\/aiL6ckKFPVNzx01R6yDFt3TgEMR97YZ\/R6lWiXG+dsJ305jNFlusLu518zBUvl7g5yjzGN778H29b2C8VLZKmi\/h1CH9jGdD72fCqCYdenD2uZKzb6dsUtXtvBmVcVT6BUGz41W1pkkEEB+YJpMrHILIxAiHRGv1+aZa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. ->method('file_get_contents')
  817. ->withConsecutive(
  818. [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//core/signature.json'],
  819. [\OC::$SERVERROOT . '/tests/data/integritycheck/appWithInvalidData//resources/codesigning/root.crt'],
  820. )->willReturnOnConsecutiveCalls(
  821. $signatureDataFile,
  822. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  823. );
  824. $expected = [
  825. 'INVALID_HASH' => [
  826. 'AnotherFile.txt' => [
  827. 'expected' => '1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112',
  828. 'current' => '7322348ba269c6d5522efe02f424fa3a0da319a7cd9c33142a5afe32a2d9af2da3a411f086fcfc96ff4301ea566f481dba0960c2abeef3594c4d930462f6584c',
  829. ],
  830. ],
  831. 'FILE_MISSING' => [
  832. 'subfolder/file.txt' => [
  833. 'expected' => '410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b',
  834. 'current' => '',
  835. ],
  836. ],
  837. 'EXTRA_FILE' => [
  838. 'UnecessaryFile' => [
  839. 'expected' => '',
  840. 'current' => 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e',
  841. ],
  842. ],
  843. ];
  844. $this->assertSame($expected, $this->checker->verifyCoreSignature());
  845. }
  846. public function testVerifyCoreWithInvalidCertificate(): void {
  847. $this->serverVersion
  848. ->expects($this->once())
  849. ->method('getChannel')
  850. ->willReturn('stable');
  851. $this->config
  852. ->expects($this->any())
  853. ->method('getSystemValueBool')
  854. ->with('integrity.check.disabled', false)
  855. ->willReturn(false);
  856. $this->environmentHelper
  857. ->expects($this->any())
  858. ->method('getServerRoot')
  859. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/app/');
  860. $signatureDataFile = '{
  861. "hashes": {
  862. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  863. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  864. },
  865. "signature": "eXesvDm3pkek12xSwMG10y9suRES79Nye3jYNe5KYq1tTUPqRRNgxmMGAfcUro0zpLeAr2YgHeSMWtglblGOW7pmwGVPZ0O1Y4r1fE6jnep0kW+35PLIaqCorIOnCAtSzDNKBhwd1ow3zW2wC0DFouuEkIO8u5Fw28g8E8dp8zEk1xMblNPy+xtWkmYHrVJ\/dQgun1bYOF2ZFtAzatwndTI\/bGsy1i3Wsl+x6HyWKQdq8y8VObtOqKDH7uERBEpB9DHVyKflj1v1gQuEH6BhaRdATc7ee0MiQdGblraIySwYRdfo2d8i82OVKrenMB3SLwyCvDPyQ9iKpTOnSF52ZBqaqSXKM2N\/RAkweeBFQQCwcHhqxvB0cfbyHcbkOLeCZe\/tsh68IxwTiYgzvLfl7sOZ5arnZbzrPpZmB+hfV2omkoJ1tDwOWz9hEmLLNtfo2OxyUH1m0+XFaC+Gbn4WkVDgf7YZkwUcG+Qoa3oKDNMss8MEyZxewl2iDGZcf402dlidHRprlfmXbAYuVQ08\/a0HxIKYPGh\/nsMGmwnO15CWtFpAbhUA\/D5oRjsIxnvXaMDg0iAFpdu\/5Ffsj7g3EPdBkiQHNYK7YU1RRx609eH0bZyiIYHdUPw7ikLupvrebZmELqi3mqDFO99u4eISlxFJlUbUND3L4BtmWTWrKwI=",
  866. "certificate": "-----BEGIN CERTIFICATE-----\r\nMIIEvjCCAqagAwIBAgIUPYoweUxCPqbDW4ntuh7QvgyqSrgwDQYJKoZIhvcNAQEF\r\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29k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+6wiuqZXqps66DuLbk45g\/ZlrFIrIo3Ix5vj0OVqwT+gO4LYirJK6KgVS\r\nUttbcEsc\/yKU9ThnM8\/n4m2jstZXfzKPgOsJrQcZrFOtpj+CWmBzVElBSPlDT3Nh\r\nHSgOeTFJ8bQBxj2iG5dLA+JZJQKxyJ1gy2ZtxIJ2GyvLtSe8NUSqvfPWOaAKEUV2\r\ngniytnEFLr+PcD+9EGux6jZNuj6HmtWVThTfD5VGFmtlVU2z71ZRYY0kn6J3mmFc\r\nS2ecEcCUwqG5YNLncEUCyZhC2klWql2SHyGctCEyWWY7ikIDjVzYt2EbcFvLNBnP\r\ntybN1TYHRRZxlug00CCoOE9EZfk46FkZpDvU6KmqJRofkNZ5sj+SffyGcwYwNrDH\r\nKqe8m+9lHf3CRTIDeMu8r2xl1I6M6ZZfjabbmVP9Jd6WN4s6f1FlXDWzhlT1N0Qw\r\nGzJj6xB+SPtS3UV05tBlvbfA4e06D5G9uD7Q8ONcINtMS0xsSJ2oo82AqlpvlF\/q\r\noj7YKHsaTVGA+FxBktZHfoxD\r\n-----END CERTIFICATE-----"
  867. }';
  868. $this->fileAccessHelper
  869. ->expects($this->exactly(2))
  870. ->method('file_get_contents')
  871. ->withConsecutive(
  872. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json'],
  873. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt'],
  874. )->willReturnOnConsecutiveCalls(
  875. $signatureDataFile,
  876. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  877. );
  878. $expected = [
  879. 'EXCEPTION' => [
  880. 'class' => 'OC\\IntegrityCheck\\Exceptions\\InvalidSignatureException',
  881. 'message' => 'Certificate is not valid.',
  882. ]
  883. ];
  884. $this->assertSame($expected, $this->checker->verifyCoreSignature());
  885. }
  886. public function testVerifyCoreWithDifferentScope(): void {
  887. $this->serverVersion
  888. ->expects($this->once())
  889. ->method('getChannel')
  890. ->willReturn('stable');
  891. $this->config
  892. ->expects($this->any())
  893. ->method('getSystemValueBool')
  894. ->with('integrity.check.disabled', false)
  895. ->willReturn(false);
  896. $this->environmentHelper
  897. ->expects($this->any())
  898. ->method('getServerRoot')
  899. ->willReturn(\OC::$SERVERROOT . '/tests/data/integritycheck/app/');
  900. $signatureDataFile = '{
  901. "hashes": {
  902. "AnotherFile.txt": "1570ca9420e37629de4328f48c51da29840ddeaa03ae733da4bf1d854b8364f594aac560601270f9e1797ed4cd57c1aea87bf44cf4245295c94f2e935a2f0112",
  903. "subfolder\/file.txt": "410738545fb623c0a5c8a71f561e48ea69e3ada0981a455e920a5ae9bf17c6831ae654df324f9328ff8453de179276ae51931cca0fa71fe8ccde6c083ca0574b"
  904. },
  905. "signature": "EL49UaSeyMAqyMtqId+tgOhhwgOevPZsRLX4j2blnybAB6fN07z0936JqZV7+eMPiE30Idx+UCY6rCFN531Kqe9vAOCdgtHUSOjjKyKc+lvULESlMb6YQcrZrvDlEMMjzjH49ewG7Ai8sNN6HrRUd9U8ws+ewSkW2DOOBItj\/21RBnkrSt+2AtGXGigEvuTm57HrCYDj8\/lSkumC2GVkjLUHeLOKYo4PRNOr6yP5mED5v7zo66AWvXl2fKv54InZcdxsAk35lyK9DGZbk\/027ZRd0AOHT3LImRLvQ+8EAg3XLlRUy0hOFGgPC+jYonMzgYvsAXAXi2j8LnLJlsLwpFwu1k1B+kZVPMumKZvP9OvJb70EirecXmz62V+Jiyuaq7ne4y7Kp5gKZT\/T8SeZ0lFtCmPfYyzBB0y8s5ldmTTmdVYHs54t\/OCCW82HzQZxnFNPzDTRa8HglsaMKrqPtW59+R4UvRKSWhB8M\/Ah57qgzycvPV4KMz\/FbD4l\/\/9chRKSlCfc2k3b8ZSHNmi+EzCKgJjWIoKdgN1yax94puU8jfn8UW+G7H9Y1Jsf\/jox6QLyYEgtV1vOHY2xLT7fVs2vhyvkN2MNjJnmQ70gFG5Qz2lBz5wi6ZpB+tOfCcpbLxWAkoWoIrmC\/Ilqh7mfmRZ43g5upj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FWGKQ3wAKs+k4+0yoti0qm2EKX7rSGQ0PHSas6uW79WstC4Rj+DYkDtIhGMSg8FS\r\nHVGZHGBCc0HwdX+BOAt3zi4p7Sf3oQef70\/4imPoKxbAVCpd\/cveVcFyDC19j1yB\r\nBapwu87oh+muoeaZxOlqQI4UxjBlR\/uRSMhOn2UGauIr3dWJgAF4pGt7TtIzt+1v\r\n0uA6FtN1Y4R5O8AaJPh1bIG0CVvFBE58esGzjEYLhOydgKFnEP94kVPgJD5ds9C3\r\npPhEpo1dRpiXaF7WGIV1X6DI\/ipWvfrF7CEy6I\/kP1InY\/vMDjQjeDnJ\/VrXIWXO\r\nyZvHXVaN\/m+1RlETsH7YO\/QmxRue9ZHN3gvvWtmpCeA95sfpepOk7UcHxHZYyQbF\r\n49\/au8j+5tsr4A83xzsT1JbcKRxkAaQ7WDJpOnE5O1+H0fB+BaLakTg6XX9d4Fo7\r\n7Gin7hVWX7pL+JIyxMzME3LhfI61+CRcqZQIrpyaafUziPQbWIPfEs7h8tCOWyvW\r\nUO8ZLervYCB3j44ivkrxPlcBklDCqqKKBzDP9dYOtS\/P4RB1NkHA9+NTvmBpTonS\r\nSFXdg9fFMD7VfjDE3Vnk+8DWkVH5wBYowTAD7w9Wuzr7DumiAULexnP\/Y7xwxLv7\r\n4B+pXTAcRK0zECDEaX3npS8xWzrB\r\n-----END CERTIFICATE-----"
  907. }';
  908. $this->fileAccessHelper
  909. ->expects($this->exactly(2))
  910. ->method('file_get_contents')
  911. ->withConsecutive(
  912. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//core/signature.json'],
  913. [\OC::$SERVERROOT . '/tests/data/integritycheck/app//resources/codesigning/root.crt'],
  914. )->willReturnOnConsecutiveCalls(
  915. $signatureDataFile,
  916. file_get_contents(__DIR__ . '/../../data/integritycheck/root.crt')
  917. );
  918. $expected = [
  919. 'EXCEPTION' => [
  920. 'class' => 'OC\\IntegrityCheck\\Exceptions\\InvalidSignatureException',
  921. 'message' => 'Certificate is not valid for required scope. (Requested: core, current: CN=SomeApp)',
  922. ]
  923. ];
  924. $this->assertSame($expected, $this->checker->verifyCoreSignature());
  925. }
  926. public function testRunInstanceVerification(): void {
  927. $this->checker = $this->getMockBuilder('\OC\IntegrityCheck\Checker')
  928. ->setConstructorArgs([
  929. $this->serverVersion,
  930. $this->environmentHelper,
  931. $this->fileAccessHelper,
  932. $this->appLocator,
  933. $this->config,
  934. $this->appConfig,
  935. $this->cacheFactory,
  936. $this->appManager,
  937. $this->mimeTypeDetector,
  938. ])
  939. ->setMethods([
  940. 'verifyCoreSignature',
  941. 'verifyAppSignature',
  942. ])
  943. ->getMock();
  944. $this->checker
  945. ->expects($this->once())
  946. ->method('verifyCoreSignature');
  947. $this->appManager
  948. ->expects($this->once())
  949. ->method('getAllAppsInAppsFolders')
  950. ->willReturn([
  951. 'files',
  952. 'calendar',
  953. 'contacts',
  954. 'dav',
  955. ]);
  956. $this->appManager
  957. ->expects($this->exactly(4))
  958. ->method('isShipped')
  959. ->withConsecutive(
  960. ['files'],
  961. ['calendar'],
  962. ['contacts'],
  963. ['dav'],
  964. )->willReturnOnConsecutiveCalls(
  965. true,
  966. false,
  967. false,
  968. true,
  969. );
  970. $this->checker
  971. ->expects($this->exactly(3))
  972. ->method('verifyAppSignature')
  973. ->withConsecutive(
  974. ['files'],
  975. ['calendar'],
  976. ['dav'],
  977. );
  978. $this->appLocator
  979. ->expects($this->exactly(2))
  980. ->method('getAppPath')
  981. ->withConsecutive(
  982. ['calendar'],
  983. ['contacts'],
  984. )->willReturnOnConsecutiveCalls(
  985. '/apps/calendar',
  986. '/apps/contacts',
  987. );
  988. $this->fileAccessHelper
  989. ->expects($this->exactly(2))
  990. ->method('file_exists')
  991. ->withConsecutive(
  992. ['/apps/calendar/appinfo/signature.json'],
  993. ['/apps/contacts/appinfo/signature.json'],
  994. )->willReturnOnConsecutiveCalls(
  995. true,
  996. false,
  997. );
  998. $this->appConfig
  999. ->expects($this->once())
  1000. ->method('deleteKey')
  1001. ->with('core', 'oc.integritycheck.checker');
  1002. $this->checker->runInstanceVerification();
  1003. }
  1004. public function testVerifyAppSignatureWithoutSignatureDataAndCodeCheckerDisabled(): void {
  1005. $this->serverVersion
  1006. ->expects($this->once())
  1007. ->method('getChannel')
  1008. ->willReturn('stable');
  1009. $this->config
  1010. ->expects($this->any())
  1011. ->method('getSystemValueBool')
  1012. ->with('integrity.check.disabled', false)
  1013. ->willReturn(true);
  1014. $expected = [];
  1015. $this->assertSame($expected, $this->checker->verifyAppSignature('SomeApp'));
  1016. }
  1017. /**
  1018. * @return array
  1019. */
  1020. public function channelDataProvider() {
  1021. return [
  1022. ['stable', true],
  1023. ['git', false],
  1024. ];
  1025. }
  1026. /**
  1027. * @param string $channel
  1028. * @param bool $isCodeSigningEnforced
  1029. * @dataProvider channelDataProvider
  1030. */
  1031. public function testIsCodeCheckEnforced($channel, $isCodeSigningEnforced): void {
  1032. $this->serverVersion
  1033. ->expects($this->once())
  1034. ->method('getChannel')
  1035. ->willReturn($channel);
  1036. $this->config
  1037. ->expects($this->any())
  1038. ->method('getSystemValueBool')
  1039. ->with('integrity.check.disabled', false)
  1040. ->willReturn(false);
  1041. $this->assertSame($isCodeSigningEnforced, $this->checker->isCodeCheckEnforced());
  1042. }
  1043. /**
  1044. * @param string $channel
  1045. * @dataProvider channelDataProvider
  1046. */
  1047. public function testIsCodeCheckEnforcedWithDisabledConfigSwitch($channel): void {
  1048. $this->serverVersion
  1049. ->expects($this->once())
  1050. ->method('getChannel')
  1051. ->willReturn($channel);
  1052. $this->config
  1053. ->expects($this->any())
  1054. ->method('getSystemValueBool')
  1055. ->with('integrity.check.disabled', false)
  1056. ->willReturn(true);
  1057. $this->assertFalse(self::invokePrivate($this->checker, 'isCodeCheckEnforced'));
  1058. }
  1059. }