MtimeSanitizer.php 935 B

1234567891011121314151617181920212223242526
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-only
  5. */
  6. namespace OCA\DAV\Connector\Sabre;
  7. class MtimeSanitizer {
  8. public static function sanitizeMtime(string $mtimeFromRequest): int {
  9. // In PHP 5.X "is_numeric" returns true for strings in hexadecimal
  10. // notation. This is no longer the case in PHP 7.X, so this check
  11. // ensures that strings with hexadecimal notations fail too in PHP 5.X.
  12. $isHexadecimal = preg_match('/^\s*0[xX]/', $mtimeFromRequest);
  13. if ($isHexadecimal || !is_numeric($mtimeFromRequest)) {
  14. throw new \InvalidArgumentException('X-OC-MTime header must be an integer (unix timestamp).');
  15. }
  16. // Prevent writing invalid mtime (timezone-proof)
  17. if ((int)$mtimeFromRequest <= 24 * 60 * 60) {
  18. throw new \InvalidArgumentException('X-OC-MTime header must be a valid positive integer');
  19. }
  20. return (int)$mtimeFromRequest;
  21. }
  22. }