-
-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathgetFilenameFromUrl.js
More file actions
176 lines (144 loc) · 4.47 KB
/
getFilenameFromUrl.js
File metadata and controls
176 lines (144 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const path = require("node:path");
const querystring = require("node:querystring");
const getPaths = require("./getPaths");
const memorize = require("./memorize");
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
/**
* @param {string} input input
* @returns {string} unescape input
*/
function decode(input) {
return querystring.unescape(input);
}
const memoizedParse = memorize((url) => {
const urlObject = new URL(url, "http://localhost");
// We can't change pathname in URL object directly because don't decode correctly
return { ...urlObject, pathname: decode(urlObject.pathname) };
}, undefined);
const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
/**
* @typedef {object} Extra
* @property {import("fs").Stats} stats stats
* @property {boolean=} immutable true when immutable, otherwise false
*/
/**
* decodeURIComponent.
*
* Allows V8 to only deoptimize this fn instead of all of send().
* @param {string} input
* @returns {string}
*/
class FilenameError extends Error {
/**
* @param {string} message message
* @param {number=} code error code
*/
constructor(message, code) {
super(message);
this.name = "FilenameError";
this.code = code;
}
}
// TODO fix redirect logic when `/` at the end, like https://github.com/pillarjs/send/blob/master/index.js#L586
/**
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
* @param {import("../index.js").FilledContext<Request, Response>} context context
* @param {string} url url
* @returns {{ filename: string, extra: Extra } | undefined} result of get filename from url
*/
function getFilenameFromUrl(context, url) {
/** @type {URL} */
let urlObject;
/** @type {string | undefined} */
let foundFilename;
try {
// The `url` property of the `request` is contains only `pathname`, `search` and `hash`
urlObject = memoizedParse(url);
} catch {
return;
}
const { options } = context;
const paths = getPaths(context);
/** @type {Extra} */
const extra = {};
for (const { publicPath, outputPath, assetsInfo } of paths) {
/** @type {string | undefined} */
let filename;
/** @type {URL} */
let publicPathObject;
try {
publicPathObject = memoizedParse(
publicPath !== "auto" && publicPath ? publicPath : "/",
);
} catch {
continue;
}
const { pathname } = urlObject;
const { pathname: publicPathPathname } = publicPathObject;
if (
pathname &&
publicPathPathname &&
pathname.startsWith(publicPathPathname)
) {
// Null byte(s)
if (pathname.includes("\0")) {
throw new FilenameError("Bad Request", 400);
}
// ".." is malicious
if (UP_PATH_REGEXP.test(path.normalize(`./${pathname}`))) {
throw new FilenameError("Forbidden", 403);
}
// Strip the `pathname` property from the `publicPath` option from the start of requested url
// `/complex/foo.js` => `foo.js`
// and add outputPath
// `foo.js` => `/home/user/my-project/dist/foo.js`
filename = path.join(
outputPath,
pathname.slice(publicPathPathname.length),
);
try {
extra.stats = context.outputFileSystem.statSync(filename);
} catch {
continue;
}
if (extra.stats.isFile()) {
foundFilename = filename;
// Rspack does not yet support `assetsInfo`, so we need to check if `assetsInfo` exists here
if (assetsInfo) {
const assetInfo = assetsInfo.get(
pathname.slice(publicPathPathname.length),
);
extra.immutable = assetInfo ? assetInfo.immutable : false;
}
break;
} else if (
extra.stats.isDirectory() &&
(typeof options.index === "undefined" || options.index)
) {
const indexValue =
typeof options.index === "undefined" ||
typeof options.index === "boolean"
? "index.html"
: options.index;
filename = path.join(filename, indexValue);
try {
extra.stats = context.outputFileSystem.statSync(filename);
} catch {
continue;
}
if (extra.stats.isFile()) {
foundFilename = filename;
break;
}
}
}
}
if (!foundFilename) {
return;
}
return { filename: foundFilename, extra };
}
module.exports = getFilenameFromUrl;
module.exports.FilenameError = FilenameError;