4
0

ext.php 3.3 KB

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