getFilenameFromUrl.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. const path = require("path");
  3. const {
  4. parse
  5. } = require("url");
  6. const querystring = require("querystring");
  7. const getPaths = require("./getPaths");
  8. const memorize = require("./memorize");
  9. /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
  10. /** @typedef {import("../index.js").ServerResponse} ServerResponse */
  11. // eslint-disable-next-line no-undefined
  12. const memoizedParse = memorize(parse, undefined, value => {
  13. if (value.pathname) {
  14. // eslint-disable-next-line no-param-reassign
  15. value.pathname = decode(value.pathname);
  16. }
  17. return value;
  18. });
  19. const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
  20. /**
  21. * @typedef {Object} Extra
  22. * @property {import("fs").Stats=} stats
  23. * @property {number=} errorCode
  24. */
  25. /**
  26. * decodeURIComponent.
  27. *
  28. * Allows V8 to only deoptimize this fn instead of all of send().
  29. *
  30. * @param {string} input
  31. * @returns {string}
  32. */
  33. function decode(input) {
  34. return querystring.unescape(input);
  35. }
  36. // TODO refactor me in the next major release, this function should return `{ filename, stats, error }`
  37. // TODO fix redirect logic when `/` at the end, like https://github.com/pillarjs/send/blob/master/index.js#L586
  38. /**
  39. * @template {IncomingMessage} Request
  40. * @template {ServerResponse} Response
  41. * @param {import("../index.js").FilledContext<Request, Response>} context
  42. * @param {string} url
  43. * @param {Extra=} extra
  44. * @returns {string | undefined}
  45. */
  46. function getFilenameFromUrl(context, url, extra = {}) {
  47. const {
  48. options
  49. } = context;
  50. const paths = getPaths(context);
  51. /** @type {string | undefined} */
  52. let foundFilename;
  53. /** @type {URL} */
  54. let urlObject;
  55. try {
  56. // The `url` property of the `request` is contains only `pathname`, `search` and `hash`
  57. urlObject = memoizedParse(url, false, true);
  58. } catch (_ignoreError) {
  59. return;
  60. }
  61. for (const {
  62. publicPath,
  63. outputPath
  64. } of paths) {
  65. /** @type {string | undefined} */
  66. let filename;
  67. /** @type {URL} */
  68. let publicPathObject;
  69. try {
  70. publicPathObject = memoizedParse(publicPath !== "auto" && publicPath ? publicPath : "/", false, true);
  71. } catch (_ignoreError) {
  72. // eslint-disable-next-line no-continue
  73. continue;
  74. }
  75. const {
  76. pathname
  77. } = urlObject;
  78. const {
  79. pathname: publicPathPathname
  80. } = publicPathObject;
  81. if (pathname && pathname.startsWith(publicPathPathname)) {
  82. // Null byte(s)
  83. if (pathname.includes("\0")) {
  84. // eslint-disable-next-line no-param-reassign
  85. extra.errorCode = 400;
  86. return;
  87. }
  88. // ".." is malicious
  89. if (UP_PATH_REGEXP.test(path.normalize(`./${pathname}`))) {
  90. // eslint-disable-next-line no-param-reassign
  91. extra.errorCode = 403;
  92. return;
  93. }
  94. // Strip the `pathname` property from the `publicPath` option from the start of requested url
  95. // `/complex/foo.js` => `foo.js`
  96. // and add outputPath
  97. // `foo.js` => `/home/user/my-project/dist/foo.js`
  98. filename = path.join(outputPath, pathname.slice(publicPathPathname.length));
  99. try {
  100. // eslint-disable-next-line no-param-reassign
  101. extra.stats = context.outputFileSystem.statSync(filename);
  102. } catch (_ignoreError) {
  103. // eslint-disable-next-line no-continue
  104. continue;
  105. }
  106. if (extra.stats.isFile()) {
  107. foundFilename = filename;
  108. break;
  109. } else if (extra.stats.isDirectory() && (typeof options.index === "undefined" || options.index)) {
  110. const indexValue = typeof options.index === "undefined" || typeof options.index === "boolean" ? "index.html" : options.index;
  111. filename = path.join(filename, indexValue);
  112. try {
  113. // eslint-disable-next-line no-param-reassign
  114. extra.stats = context.outputFileSystem.statSync(filename);
  115. } catch (__ignoreError) {
  116. // eslint-disable-next-line no-continue
  117. continue;
  118. }
  119. if (extra.stats.isFile()) {
  120. foundFilename = filename;
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. // eslint-disable-next-line consistent-return
  127. return foundFilename;
  128. }
  129. module.exports = getFilenameFromUrl;