ext.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. require(__DIR__ . '/../constants.php');
  4. function get_absolute_filename(string $file_name): string {
  5. $core_extension = realpath(CORE_EXTENSIONS_PATH . '/' . $file_name);
  6. if (false !== $core_extension) {
  7. return $core_extension;
  8. }
  9. $third_party_extension = realpath(THIRDPARTY_EXTENSIONS_PATH . '/' . $file_name);
  10. if (false !== $third_party_extension) {
  11. return $third_party_extension;
  12. }
  13. return '';
  14. }
  15. function is_valid_path_extension(string $path, string $extensionPath): bool {
  16. // It must be under the extension path.
  17. $real_ext_path = realpath($extensionPath);
  18. if ($real_ext_path == false) {
  19. return false;
  20. }
  21. //Windows compatibility
  22. $real_ext_path = str_replace('\\', '/', $real_ext_path);
  23. $path = str_replace('\\', '/', $path);
  24. $in_ext_path = (str_starts_with($path, $real_ext_path));
  25. if (!$in_ext_path) {
  26. return false;
  27. }
  28. // Static files to serve must be under a `ext_dir/static/` directory.
  29. $path_relative_to_ext = substr($path, strlen($real_ext_path) + 1);
  30. [, $static, $file] = sscanf($path_relative_to_ext, '%[^/]/%[^/]/%s') ?? [null, null, null];
  31. if (null === $file || 'static' !== $static) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. /**
  37. * Check if a file can be served by ext.php. A valid file is under a
  38. * CORE_EXTENSIONS_PATH/extension_name/static/ or THIRDPARTY_EXTENSIONS_PATH/extension_name/static/ directory.
  39. *
  40. * You should sanitize path by using the realpath() function.
  41. *
  42. * @param string $path the path to the file we want to serve.
  43. * @return bool true if it can be served, false otherwise.
  44. */
  45. function is_valid_path(string $path): bool {
  46. return is_valid_path_extension($path, CORE_EXTENSIONS_PATH) || is_valid_path_extension($path, THIRDPARTY_EXTENSIONS_PATH);
  47. }
  48. function sendBadRequestResponse(?string $message = null): never {
  49. header('HTTP/1.1 400 Bad Request');
  50. die($message ?? 'Bad Request!');
  51. }
  52. function sendNotFoundResponse(): never {
  53. header('HTTP/1.1 404 Not Found');
  54. die('Not Found!');
  55. }
  56. if (!is_string($_GET['f'] ?? null)) {
  57. sendBadRequestResponse('Query string is incomplete.');
  58. }
  59. $file_name = urldecode($_GET['f']);
  60. $file_type = pathinfo($file_name, PATHINFO_EXTENSION);
  61. if (empty(FreshRSS_extension_Controller::MIME_TYPES[$file_type])) {
  62. sendBadRequestResponse('File type is not supported.');
  63. }
  64. // Forbid absolute paths and path traversal
  65. if (str_contains($file_name, '..') || str_starts_with($file_name, '/') || str_starts_with($file_name, '\\')) {
  66. sendBadRequestResponse('File is not supported.');
  67. }
  68. $absolute_filename = get_absolute_filename($file_name);
  69. if (!is_valid_path($absolute_filename)) {
  70. sendBadRequestResponse('File is not supported.');
  71. }
  72. $content_type = FreshRSS_extension_Controller::MIME_TYPES[$file_type];
  73. header("Content-Type: {$content_type}");
  74. header("Content-Disposition: inline; filename='{$file_name}'");
  75. header('Referrer-Policy: same-origin');
  76. $mtime = @filemtime($absolute_filename);
  77. if ($mtime === false) {
  78. sendNotFoundResponse();
  79. }
  80. require(LIB_PATH . '/http-conditional.php');
  81. if (!httpConditional($mtime, 604800, 2)) {
  82. readfile($absolute_filename);
  83. }