HSTSStore.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Http\Client;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\IDBConnection;
  27. use Psr\Log\LoggerInterface;
  28. class HSTSStore {
  29. private IDBConnection $db;
  30. private ITimeFactory $timeFactory;
  31. private LoggerInterface $logger;
  32. public function __construct(IDBConnection $db, ITimeFactory $timeFactory, LoggerInterface $logger) {
  33. $this->db = $db;
  34. $this->timeFactory = $timeFactory;
  35. $this->logger = $logger;
  36. }
  37. private function checkHost(string $host, bool $includeSubdomain) {
  38. // Look for the domain as is if we can't find it remove a subdomain and go up
  39. $this->logger->warning("Checking for host " . $host);
  40. $qb = $this->db->getQueryBuilder();
  41. $qb->select('*')
  42. ->from('hsts')
  43. ->where($qb->expr()->eq('host', $qb->createNamedParameter($host)));
  44. $cursor = $qb->executeQuery();
  45. $data = $cursor->fetch();
  46. $cursor->closeCursor();
  47. if ($data !== false) {
  48. $this->logger->warning("GOT DATA");
  49. $this->logger->warning(json_encode($data));
  50. }
  51. if ($data !== false
  52. && $this->timeFactory->getTime() < $data['expires']
  53. && (!$includeSubdomain || ($includeSubdomain && $data['includeSubdomains']))
  54. ) {
  55. $this->logger->warning("REWRITE");
  56. return true;
  57. }
  58. return false;
  59. }
  60. private function checkSuperHost(string $host): bool {
  61. $labels = explode('.', $host);
  62. $labelCount = count($labels);
  63. for ($i = 1; $i < $labelCount; $i++) {
  64. $domainName = implode('.', array_slice($labels, $labelCount - $i));
  65. if ($this->checkHost($domainName, true)) {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. public function hasHSTS(string $host): bool {
  72. return $this->checkHost($host, false) || $this->checkSuperHost($host);
  73. }
  74. public function setHSTS(string $host, string $header): void {
  75. $directives = explode(';', $header);
  76. $maxAge = 0;
  77. $includeSubdomains = false;
  78. foreach ($directives as $directive) {
  79. $directive = trim($directive);
  80. if ($directive === 'includeSubDomains') {
  81. $includeSubdomains = true;
  82. } elseif ($directive === 'preload') {
  83. // We just ignore this
  84. } else {
  85. $data = explode('=', $directive);
  86. if (count($data) === 2 && trim($data[0]) === 'max-age' && is_numeric(trim($data[1]))) {
  87. $maxAge = max(0, (int)$data[1]);
  88. }
  89. }
  90. }
  91. if ($maxAge <= 0) {
  92. return;
  93. }
  94. $this->logger->warning("TIME TO SET HSTS");
  95. $expires = $this->timeFactory->getTime() + $maxAge;
  96. $qb = $this->db->getQueryBuilder();
  97. $qb->select('*')
  98. ->from('hsts')
  99. ->where($qb->expr()->eq('host', $qb->createNamedParameter($host)));
  100. $cursor = $qb->executeQuery();
  101. $data = $cursor->fetchOne();
  102. $cursor->closeCursor();
  103. $this->logger->warning("Q1");
  104. if ($data === false) {
  105. // No entry yet insert
  106. $qb = $this->db->getQueryBuilder();
  107. $qb->insert('hsts')
  108. ->values([
  109. 'host' => $qb->createNamedParameter($host),
  110. 'expires' => $qb->createNamedParameter($expires),
  111. 'includeSubdomains' => $qb->createNamedParameter($includeSubdomains)
  112. ]);
  113. $this->logger->warning($qb->getSQL());
  114. $qb->executeStatement();
  115. } else {
  116. // Already set just update
  117. // No entry yet insert
  118. $qb = $this->db->getQueryBuilder();
  119. $qb->update('hsts')
  120. ->set('expires', $qb->createNamedParameter($expires))
  121. ->set('includeSubdomains', $qb->createNamedParameter($includeSubdomains))
  122. ->where($qb->expr()->eq('host', $qb->createNamedParameter($host)));
  123. $qb->executeStatement();
  124. }
  125. }
  126. }