MtimeSanitizer.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021, Louis Chemineau <louis@chmn.me>
  4. *
  5. * @author Louis Chemineau <louis@chmn.me>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\DAV\Connector\Sabre;
  23. class MtimeSanitizer {
  24. public static function sanitizeMtime(string $mtimeFromRequest): int {
  25. // In PHP 5.X "is_numeric" returns true for strings in hexadecimal
  26. // notation. This is no longer the case in PHP 7.X, so this check
  27. // ensures that strings with hexadecimal notations fail too in PHP 5.X.
  28. $isHexadecimal = preg_match('/^\s*0[xX]/', $mtimeFromRequest);
  29. if ($isHexadecimal || !is_numeric($mtimeFromRequest)) {
  30. throw new \InvalidArgumentException('X-OC-MTime header must be an integer (unix timestamp).');
  31. }
  32. // Prevent writing invalid mtime (timezone-proof)
  33. if ((int)$mtimeFromRequest <= 24 * 60 * 60) {
  34. throw new \InvalidArgumentException('X-OC-MTime header must be a valid positive integer');
  35. }
  36. return (int)$mtimeFromRequest;
  37. }
  38. }