CheckApp.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 OC\Core\Command\Integrity;
  8. use OC\Core\Command\Base;
  9. use OC\IntegrityCheck\Checker;
  10. use OC\IntegrityCheck\Helpers\AppLocator;
  11. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  12. use OCP\App\IAppManager;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. * Class CheckApp
  19. *
  20. * @package OC\Core\Command\Integrity
  21. */
  22. class CheckApp extends Base {
  23. public function __construct(
  24. private Checker $checker,
  25. private AppLocator $appLocator,
  26. private FileAccessHelper $fileAccessHelper,
  27. private IAppManager $appManager,
  28. ) {
  29. parent::__construct();
  30. }
  31. /**
  32. * {@inheritdoc }
  33. */
  34. protected function configure() {
  35. parent::configure();
  36. $this
  37. ->setName('integrity:check-app')
  38. ->setDescription('Check integrity of an app using a signature.')
  39. ->addArgument('appid', InputArgument::REQUIRED, 'Application to check')
  40. ->addOption('path', null, InputOption::VALUE_OPTIONAL, 'Path to application. If none is given it will be guessed.');
  41. }
  42. /**
  43. * {@inheritdoc }
  44. */
  45. protected function execute(InputInterface $input, OutputInterface $output): int {
  46. $appid = $input->getArgument('appid');
  47. $path = (string)$input->getOption('path');
  48. if ($path === '') {
  49. $path = $this->appLocator->getAppPath($appid);
  50. }
  51. if ($this->appManager->isShipped($appid) || $this->fileAccessHelper->file_exists($path . '/appinfo/signature.json')) {
  52. // Only verify if the application explicitly ships a signature.json file
  53. $result = $this->checker->verifyAppSignature($appid, $path, true);
  54. $this->writeArrayInOutputFormat($input, $output, $result);
  55. if (count($result) > 0) {
  56. $output->writeln('<error>' . count($result) . ' errors found</error>', OutputInterface::VERBOSITY_VERBOSE);
  57. return 1;
  58. }
  59. $output->writeln('<info>No errors found</info>', OutputInterface::VERBOSITY_VERBOSE);
  60. } else {
  61. $output->writeln('<comment>App signature not found, skipping app integrity check</comment>');
  62. }
  63. return 0;
  64. }
  65. }