HttpsUrlGeneration.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OCP\IL10N;
  9. use OCP\IRequest;
  10. use OCP\IURLGenerator;
  11. use OCP\SetupCheck\ISetupCheck;
  12. use OCP\SetupCheck\SetupResult;
  13. class HttpsUrlGeneration implements ISetupCheck {
  14. public function __construct(
  15. private IL10N $l10n,
  16. private IURLGenerator $urlGenerator,
  17. private IRequest $request,
  18. ) {
  19. }
  20. public function getCategory(): string {
  21. return 'security';
  22. }
  23. public function getName(): string {
  24. return $this->l10n->t('HTTPS access and URLs');
  25. }
  26. public function run(): SetupResult {
  27. if (!\OC::$CLI && $this->request->getServerProtocol() !== 'https') {
  28. if (!preg_match('/(?:^(?:localhost|127\.0\.0\.1|::1)|\.onion)$/', $this->request->getInsecureServerHost())) {
  29. return SetupResult::error(
  30. $this->l10n->t('Accessing site insecurely via HTTP. You are strongly advised to set up your server to require HTTPS instead. Without it some important web functionality like "copy to clipboard" or "service workers" will not work!'),
  31. $this->urlGenerator->linkToDocs('admin-security')
  32. );
  33. } else {
  34. return SetupResult::info(
  35. $this->l10n->t('Accessing site insecurely via HTTP.'),
  36. $this->urlGenerator->linkToDocs('admin-security')
  37. );
  38. }
  39. }
  40. $generatedUrl = $this->urlGenerator->getAbsoluteURL('index.php');
  41. if (!str_starts_with($generatedUrl, 'https://')) {
  42. if (!\OC::$CLI) {
  43. return SetupResult::warning(
  44. $this->l10n->t('You are accessing your instance over a secure connection, however your instance is generating insecure URLs. This likely means that your instance is behind a reverse proxy and the Nextcloud `overwrite*` config values are not set correctly.'),
  45. $this->urlGenerator->linkToDocs('admin-reverse-proxy')
  46. );
  47. /* We were called from CLI so we can't be 100% sure which scenario is applicable */
  48. } else {
  49. return SetupResult::info(
  50. $this->l10n->t('Your instance is generating insecure URLs. If you access your instance over HTTPS, this likely means that your instance is behind a reverse proxy and the Nextcloud `overwrite*` config values are not set correctly.'),
  51. $this->urlGenerator->linkToDocs('admin-reverse-proxy')
  52. );
  53. }
  54. }
  55. return SetupResult::success($this->l10n->t('You are accessing your instance over a secure connection, and your instance is generating secure URLs.'));
  56. }
  57. }