ext.php 3.1 KB

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