You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TemplateLayout.php 7.8KB

8 years ago
8 years ago
8 years ago
8 years ago
Add code integrity check This PR implements the base foundation of the code signing and integrity check. In this PR implemented is the signing and verification logic, as well as commands to sign single apps or the core repository. Furthermore, there is a basic implementation to display problems with the code integrity on the update screen. Code signing basically happens the following way: - There is a ownCloud Root Certificate authority stored `resources/codesigning/root.crt` (in this PR I also ship the private key which we obviously need to change before a release :wink:). This certificate is not intended to be used for signing directly and only is used to sign new certificates. - Using the `integrity:sign-core` and `integrity:sign-app` commands developers can sign either the core release or a single app. The core release needs to be signed with a certificate that has a CN of `core`, apps need to be signed with a certificate that either has a CN of `core` (shipped apps!) or the AppID. - The command generates a signature.json file of the following format: ```json { "hashes": { "/filename.php": "2401fed2eea6f2c1027c482a633e8e25cd46701f811e2d2c10dc213fd95fa60e350bccbbebdccc73a042b1a2799f673fbabadc783284cc288e4f1a1eacb74e3d", "/lib/base.php": "55548cc16b457cd74241990cc9d3b72b6335f2e5f45eee95171da024087d114fcbc2effc3d5818a6d5d55f2ae960ab39fd0414d0c542b72a3b9e08eb21206dd9" }, "certificate": "-----BEGIN CERTIFICATE-----MIIBvTCCASagAwIBAgIUPvawyqJwCwYazcv7iz16TWxfeUMwDQYJKoZIhvcNAQEF\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTAx\nNDEzMTcxMFoXDTE2MTAxNDEzMTcxMFowEzERMA8GA1UEAwwIY29udGFjdHMwgZ8w\nDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANoQesGdCW0L2L+a2xITYipixkScrIpB\nkX5Snu3fs45MscDb61xByjBSlFgR4QI6McoCipPw4SUr28EaExVvgPSvqUjYLGps\nfiv0Cvgquzbx/X3mUcdk9LcFo1uWGtrTfkuXSKX41PnJGTr6RQWGIBd1V52q1qbC\nJKkfzyeMeuQfAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAvF/KIhRMQ3tYTmgHWsiM\nwDMgIDb7iaHF0fS+/Nvo4PzoTO/trev6tMyjLbJ7hgdCpz/1sNzE11Cibf6V6dsz\njCE9invP368Xv0bTRObRqeSNsGogGl5ceAvR0c9BG+NRIKHcly3At3gLkS2791bC\niG+UxI/MNcWV0uJg9S63LF8=\n-----END CERTIFICATE-----", "signature": "U29tZVNpZ25lZERhdGFFeGFtcGxl" } ``` `hashes` is an array of all files in the folder with their corresponding SHA512 hashes (this is actually quite cheap to calculate), the `certificate` is the certificate used for signing. It has to be issued by the ownCloud Root Authority and it's CN needs to be permitted to perform the required action. The `signature` is then a signature of the `hashes` which can be verified using the `certificate`. Steps to do in other PRs, this is already a quite huge one: - Add nag screen in case the code check fails to ensure that administrators are aware of this. - Add code verification also to OCC upgrade and unify display code more. - Add enforced code verification to apps shipped from the appstore with a level of "official" - Add enfocrced code verification to apps shipped from the appstore that were already signed in a previous release - Add some developer documentation on how devs can request their own certificate - Check when installing ownCloud - Add support for CRLs to allow revoking certificates **Note:** The upgrade checks are only run when the instance has a defined release channel of `stable` (defined in `version.php`). If you want to test this, you need to change the channel thus and then generate the core signature: ``` ➜ master git:(add-integrity-checker) ✗ ./occ integrity:sign-core --privateKey=resources/codesigning/core.key --certificate=resources/codesigning/core.crt Successfully signed "core" ``` Then increase the version and you should see something like the following: ![2015-11-04_12-02-57](https://cloud.githubusercontent.com/assets/878997/10936336/6adb1d14-82ec-11e5-8f06-9a74801c9abf.png) As you can see a failed code check will not prevent the further update. It will instead just be a notice to the admin. In a next step we will add some nag screen. For packaging stable releases this requires the following additional steps as a last action before zipping: 1. Run `./occ integrity:sign-core` once 2. Run `./occ integrity:sign-app` _for each_ app. However, this can be simply automated using a simple foreach on the apps folder.
8 years ago
Add code integrity check This PR implements the base foundation of the code signing and integrity check. In this PR implemented is the signing and verification logic, as well as commands to sign single apps or the core repository. Furthermore, there is a basic implementation to display problems with the code integrity on the update screen. Code signing basically happens the following way: - There is a ownCloud Root Certificate authority stored `resources/codesigning/root.crt` (in this PR I also ship the private key which we obviously need to change before a release :wink:). This certificate is not intended to be used for signing directly and only is used to sign new certificates. - Using the `integrity:sign-core` and `integrity:sign-app` commands developers can sign either the core release or a single app. The core release needs to be signed with a certificate that has a CN of `core`, apps need to be signed with a certificate that either has a CN of `core` (shipped apps!) or the AppID. - The command generates a signature.json file of the following format: ```json { "hashes": { "/filename.php": "2401fed2eea6f2c1027c482a633e8e25cd46701f811e2d2c10dc213fd95fa60e350bccbbebdccc73a042b1a2799f673fbabadc783284cc288e4f1a1eacb74e3d", "/lib/base.php": "55548cc16b457cd74241990cc9d3b72b6335f2e5f45eee95171da024087d114fcbc2effc3d5818a6d5d55f2ae960ab39fd0414d0c542b72a3b9e08eb21206dd9" }, "certificate": "-----BEGIN CERTIFICATE-----MIIBvTCCASagAwIBAgIUPvawyqJwCwYazcv7iz16TWxfeUMwDQYJKoZIhvcNAQEF\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTAx\nNDEzMTcxMFoXDTE2MTAxNDEzMTcxMFowEzERMA8GA1UEAwwIY29udGFjdHMwgZ8w\nDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANoQesGdCW0L2L+a2xITYipixkScrIpB\nkX5Snu3fs45MscDb61xByjBSlFgR4QI6McoCipPw4SUr28EaExVvgPSvqUjYLGps\nfiv0Cvgquzbx/X3mUcdk9LcFo1uWGtrTfkuXSKX41PnJGTr6RQWGIBd1V52q1qbC\nJKkfzyeMeuQfAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAvF/KIhRMQ3tYTmgHWsiM\nwDMgIDb7iaHF0fS+/Nvo4PzoTO/trev6tMyjLbJ7hgdCpz/1sNzE11Cibf6V6dsz\njCE9invP368Xv0bTRObRqeSNsGogGl5ceAvR0c9BG+NRIKHcly3At3gLkS2791bC\niG+UxI/MNcWV0uJg9S63LF8=\n-----END CERTIFICATE-----", "signature": "U29tZVNpZ25lZERhdGFFeGFtcGxl" } ``` `hashes` is an array of all files in the folder with their corresponding SHA512 hashes (this is actually quite cheap to calculate), the `certificate` is the certificate used for signing. It has to be issued by the ownCloud Root Authority and it's CN needs to be permitted to perform the required action. The `signature` is then a signature of the `hashes` which can be verified using the `certificate`. Steps to do in other PRs, this is already a quite huge one: - Add nag screen in case the code check fails to ensure that administrators are aware of this. - Add code verification also to OCC upgrade and unify display code more. - Add enforced code verification to apps shipped from the appstore with a level of "official" - Add enfocrced code verification to apps shipped from the appstore that were already signed in a previous release - Add some developer documentation on how devs can request their own certificate - Check when installing ownCloud - Add support for CRLs to allow revoking certificates **Note:** The upgrade checks are only run when the instance has a defined release channel of `stable` (defined in `version.php`). If you want to test this, you need to change the channel thus and then generate the core signature: ``` ➜ master git:(add-integrity-checker) ✗ ./occ integrity:sign-core --privateKey=resources/codesigning/core.key --certificate=resources/codesigning/core.crt Successfully signed "core" ``` Then increase the version and you should see something like the following: ![2015-11-04_12-02-57](https://cloud.githubusercontent.com/assets/878997/10936336/6adb1d14-82ec-11e5-8f06-9a74801c9abf.png) As you can see a failed code check will not prevent the further update. It will instead just be a notice to the admin. In a next step we will add some nag screen. For packaging stable releases this requires the following additional steps as a last action before zipping: 1. Run `./occ integrity:sign-core` once 2. Run `./occ integrity:sign-app` _for each_ app. However, this can be simply automated using a simple foreach on the apps folder.
8 years ago
Add code integrity check This PR implements the base foundation of the code signing and integrity check. In this PR implemented is the signing and verification logic, as well as commands to sign single apps or the core repository. Furthermore, there is a basic implementation to display problems with the code integrity on the update screen. Code signing basically happens the following way: - There is a ownCloud Root Certificate authority stored `resources/codesigning/root.crt` (in this PR I also ship the private key which we obviously need to change before a release :wink:). This certificate is not intended to be used for signing directly and only is used to sign new certificates. - Using the `integrity:sign-core` and `integrity:sign-app` commands developers can sign either the core release or a single app. The core release needs to be signed with a certificate that has a CN of `core`, apps need to be signed with a certificate that either has a CN of `core` (shipped apps!) or the AppID. - The command generates a signature.json file of the following format: ```json { "hashes": { "/filename.php": "2401fed2eea6f2c1027c482a633e8e25cd46701f811e2d2c10dc213fd95fa60e350bccbbebdccc73a042b1a2799f673fbabadc783284cc288e4f1a1eacb74e3d", "/lib/base.php": "55548cc16b457cd74241990cc9d3b72b6335f2e5f45eee95171da024087d114fcbc2effc3d5818a6d5d55f2ae960ab39fd0414d0c542b72a3b9e08eb21206dd9" }, "certificate": "-----BEGIN CERTIFICATE-----MIIBvTCCASagAwIBAgIUPvawyqJwCwYazcv7iz16TWxfeUMwDQYJKoZIhvcNAQEF\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTAx\nNDEzMTcxMFoXDTE2MTAxNDEzMTcxMFowEzERMA8GA1UEAwwIY29udGFjdHMwgZ8w\nDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANoQesGdCW0L2L+a2xITYipixkScrIpB\nkX5Snu3fs45MscDb61xByjBSlFgR4QI6McoCipPw4SUr28EaExVvgPSvqUjYLGps\nfiv0Cvgquzbx/X3mUcdk9LcFo1uWGtrTfkuXSKX41PnJGTr6RQWGIBd1V52q1qbC\nJKkfzyeMeuQfAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAvF/KIhRMQ3tYTmgHWsiM\nwDMgIDb7iaHF0fS+/Nvo4PzoTO/trev6tMyjLbJ7hgdCpz/1sNzE11Cibf6V6dsz\njCE9invP368Xv0bTRObRqeSNsGogGl5ceAvR0c9BG+NRIKHcly3At3gLkS2791bC\niG+UxI/MNcWV0uJg9S63LF8=\n-----END CERTIFICATE-----", "signature": "U29tZVNpZ25lZERhdGFFeGFtcGxl" } ``` `hashes` is an array of all files in the folder with their corresponding SHA512 hashes (this is actually quite cheap to calculate), the `certificate` is the certificate used for signing. It has to be issued by the ownCloud Root Authority and it's CN needs to be permitted to perform the required action. The `signature` is then a signature of the `hashes` which can be verified using the `certificate`. Steps to do in other PRs, this is already a quite huge one: - Add nag screen in case the code check fails to ensure that administrators are aware of this. - Add code verification also to OCC upgrade and unify display code more. - Add enforced code verification to apps shipped from the appstore with a level of "official" - Add enfocrced code verification to apps shipped from the appstore that were already signed in a previous release - Add some developer documentation on how devs can request their own certificate - Check when installing ownCloud - Add support for CRLs to allow revoking certificates **Note:** The upgrade checks are only run when the instance has a defined release channel of `stable` (defined in `version.php`). If you want to test this, you need to change the channel thus and then generate the core signature: ``` ➜ master git:(add-integrity-checker) ✗ ./occ integrity:sign-core --privateKey=resources/codesigning/core.key --certificate=resources/codesigning/core.crt Successfully signed "core" ``` Then increase the version and you should see something like the following: ![2015-11-04_12-02-57](https://cloud.githubusercontent.com/assets/878997/10936336/6adb1d14-82ec-11e5-8f06-9a74801c9abf.png) As you can see a failed code check will not prevent the further update. It will instead just be a notice to the admin. In a next step we will add some nag screen. For packaging stable releases this requires the following additional steps as a last action before zipping: 1. Run `./occ integrity:sign-core` once 2. Run `./occ integrity:sign-app` _for each_ app. However, this can be simply automated using a simple foreach on the apps folder.
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Clark Tomlinson <fallen013@gmail.com>
  8. * @author Hendrik Leppelsack <hendrik@leppelsack.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Remco Brenninkmeijer <requist1@starmail.nl>
  15. * @author Robin Appelman <robin@icewind.nl>
  16. * @author Robin McCorkell <robin@mccorkell.me.uk>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Thomas Müller <thomas.mueller@tmit.eu>
  19. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. namespace OC;
  37. use Assetic\Asset\AssetCollection;
  38. use Assetic\Asset\FileAsset;
  39. use Assetic\AssetWriter;
  40. use Assetic\Filter\CssImportFilter;
  41. use Assetic\Filter\CssMinFilter;
  42. use Assetic\Filter\CssRewriteFilter;
  43. use Assetic\Filter\JSqueezeFilter;
  44. use Assetic\Filter\SeparatorFilter;
  45. use OC\Template\JSConfigHelper;
  46. class TemplateLayout extends \OC_Template {
  47. private static $versionHash = '';
  48. /**
  49. * @var \OCP\IConfig
  50. */
  51. private $config;
  52. /**
  53. * @param string $renderAs
  54. * @param string $appId application id
  55. */
  56. public function __construct( $renderAs, $appId = '' ) {
  57. // yes - should be injected ....
  58. $this->config = \OC::$server->getConfig();
  59. // Decide which page we show
  60. if($renderAs == 'user') {
  61. parent::__construct( 'core', 'layout.user' );
  62. if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
  63. $this->assign('bodyid', 'body-settings');
  64. }else{
  65. $this->assign('bodyid', 'body-user');
  66. }
  67. // Code integrity notification
  68. $integrityChecker = \OC::$server->getIntegrityCodeChecker();
  69. if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
  70. \OCP\Util::addScript('core', 'integritycheck-failed-notification');
  71. }
  72. // Add navigation entry
  73. $this->assign( 'application', '');
  74. $this->assign( 'appid', $appId );
  75. $navigation = \OC_App::getNavigation();
  76. $this->assign( 'navigation', $navigation);
  77. $settingsNavigation = \OC_App::getSettingsNavigation();
  78. $this->assign( 'settingsnavigation', $settingsNavigation);
  79. foreach($navigation as $entry) {
  80. if ($entry['active']) {
  81. $this->assign( 'application', $entry['name'] );
  82. break;
  83. }
  84. }
  85. foreach($settingsNavigation as $entry) {
  86. if ($entry['active']) {
  87. $this->assign( 'application', $entry['name'] );
  88. break;
  89. }
  90. }
  91. $userDisplayName = \OC_User::getDisplayName();
  92. $appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0;
  93. if ($appsMgmtActive) {
  94. $l = \OC::$server->getL10N('lib');
  95. $this->assign('application', $l->t('Apps'));
  96. }
  97. $this->assign('user_displayname', $userDisplayName);
  98. $this->assign('user_uid', \OC_User::getUser());
  99. $this->assign('appsmanagement_active', $appsMgmtActive);
  100. $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true);
  101. if (\OC_User::getUser() === false) {
  102. $this->assign('userAvatarSet', false);
  103. } else {
  104. $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
  105. $this->assign('userAvatarVersion', \OC::$server->getConfig()->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
  106. }
  107. } else if ($renderAs == 'error') {
  108. parent::__construct('core', 'layout.guest', '', false);
  109. $this->assign('bodyid', 'body-login');
  110. } else if ($renderAs == 'guest') {
  111. parent::__construct('core', 'layout.guest');
  112. $this->assign('bodyid', 'body-login');
  113. } else {
  114. parent::__construct('core', 'layout.base');
  115. }
  116. // Send the language to our layouts
  117. $this->assign('language', \OC::$server->getL10NFactory()->findLanguage());
  118. if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
  119. if (empty(self::$versionHash)) {
  120. $v = \OC_App::getAppVersions();
  121. $v['core'] = implode('.', \OCP\Util::getVersion());
  122. self::$versionHash = md5(implode(',', $v));
  123. }
  124. } else {
  125. self::$versionHash = md5('not installed');
  126. }
  127. // Add the js files
  128. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  129. $this->assign('jsfiles', array());
  130. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  131. if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
  132. $jsConfigHelper = new JSConfigHelper(
  133. \OC::$server->getL10N('core'),
  134. \OC::$server->getThemingDefaults(),
  135. \OC::$server->getAppManager(),
  136. \OC::$server->getUserSession()->getUser(),
  137. \OC::$server->getConfig(),
  138. \OC::$server->getGroupManager(),
  139. \OC::$server->getIniWrapper(),
  140. \OC::$server->getURLGenerator()
  141. );
  142. $this->assign('inline_ocjs', $jsConfigHelper->getConfig());
  143. $this->assign('foo', 'bar');
  144. } else {
  145. $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
  146. }
  147. }
  148. foreach($jsFiles as $info) {
  149. $web = $info[1];
  150. $file = $info[2];
  151. $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  152. }
  153. // Add the css files
  154. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  155. $this->assign('cssfiles', array());
  156. $this->assign('printcssfiles', []);
  157. $this->assign('versionHash', self::$versionHash);
  158. foreach($cssFiles as $info) {
  159. $web = $info[1];
  160. $file = $info[2];
  161. if (substr($file, -strlen('print.css')) === 'print.css') {
  162. $this->append( 'printcssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  163. } else {
  164. $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  165. }
  166. }
  167. }
  168. /**
  169. * @param array $styles
  170. * @return array
  171. */
  172. static public function findStylesheetFiles($styles) {
  173. // Read the selected theme from the config file
  174. $theme = \OC_Util::getTheme();
  175. $locator = new \OC\Template\CSSResourceLocator(
  176. \OC::$server->getLogger(),
  177. $theme,
  178. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  179. array( \OC::$SERVERROOT => \OC::$WEBROOT ));
  180. $locator->find($styles);
  181. return $locator->getResources();
  182. }
  183. /**
  184. * @param array $scripts
  185. * @return array
  186. */
  187. static public function findJavascriptFiles($scripts) {
  188. // Read the selected theme from the config file
  189. $theme = \OC_Util::getTheme();
  190. $locator = new \OC\Template\JSResourceLocator(
  191. \OC::$server->getLogger(),
  192. $theme,
  193. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  194. array( \OC::$SERVERROOT => \OC::$WEBROOT ));
  195. $locator->find($scripts);
  196. return $locator->getResources();
  197. }
  198. /**
  199. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  200. * @param string $filePath Absolute path
  201. * @return string Relative path
  202. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  203. */
  204. public static function convertToRelativePath($filePath) {
  205. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  206. if(count($relativePath) !== 2) {
  207. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  208. }
  209. return $relativePath[1];
  210. }
  211. }