@lenne.tech/nest-server 11.46.5 → 11.46.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/rules/package-management.md +4 -3
- package/.claude/rules/testing.md +4 -4
- package/FRAMEWORK-API.md +1 -1
- package/dist/core/common/helpers/graphql-upload.helper.d.ts +10 -0
- package/dist/core/common/helpers/graphql-upload.helper.js +240 -0
- package/dist/core/common/helpers/graphql-upload.helper.js.map +1 -0
- package/dist/core/common/plugins/complexity.plugin.js +71 -7
- package/dist/core/common/plugins/complexity.plugin.js.map +1 -1
- package/dist/core/modules/hub/helpers/hub-mask.helper.js +5 -1
- package/dist/core/modules/hub/helpers/hub-mask.helper.js.map +1 -1
- package/dist/core.module.js +2 -1
- package/dist/core.module.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/graphql-upload.d.ts +35 -1
- package/docs/REQUEST-LIFECYCLE.md +26 -3
- package/migration-guides/11.46.5-to-11.46.6.md +203 -0
- package/package.json +9 -2
- package/src/core/common/helpers/graphql-upload.helper.ts +522 -0
- package/src/core/common/plugins/complexity.plugin.ts +150 -9
- package/src/core/modules/hub/helpers/hub-mask.helper.ts +16 -3
- package/src/core/modules/system-setup/INTEGRATION-CHECKLIST.md +8 -8
- package/src/core/modules/system-setup/README.md +6 -6
- package/src/core.module.ts +5 -1
- package/src/index.ts +1 -0
- package/src/types/graphql-upload.d.ts +35 -1
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GraphQL multipart request processing for `graphqlUploadExpress()`: graphql-upload's `processRequest` with the
|
|
3
|
+
* fixes for GHSA-5vm7-vc2x-p3w2 (high) and GHSA-qwwm-252j-9whw (medium) applied.
|
|
4
|
+
*
|
|
5
|
+
* Source: package `graphql-upload` 15.0.2, file `processRequest.js`
|
|
6
|
+
* (https://github.com/jaydenseric/graphql-upload), ported to TypeScript. Upstream fixed both advisories in
|
|
7
|
+
* graphql-upload 18.0.2, an ESM-only release that requires graphql 17 — this line runs graphql 16 with graphql-upload
|
|
8
|
+
* 15.0.2 (CommonJS), so the fix is carried here instead. Both hunks of the upstream fix are applied unchanged; the
|
|
9
|
+
* code they patch is the same in 15.0.2 (later releases changed the module format and added busboy's
|
|
10
|
+
* `defParamCharset: 'utf8'`, which this port leaves out so file names decode as they did on this line before):
|
|
11
|
+
*
|
|
12
|
+
* 1. GHSA-5vm7-vc2x-p3w2: a `map` path with a `__proto__` key, or one that goes through an already placed
|
|
13
|
+
* `Upload`, is refused with the existing 400 "Invalid object path". Without it one anonymous request crashed the
|
|
14
|
+
* process: `{"1":["variables.file"],"2":["variables.file.reject"]}` without file fields replaces the first
|
|
15
|
+
* upload's `reject`, and the parser's `finish` handler throws `TypeError: upload.reject is not a function` outside
|
|
16
|
+
* any request — an uncaught exception, which `installProcessDiagnostics()` (and Node itself) answers with exit 1.
|
|
17
|
+
* `variables.file.__proto__` gave a client-supplied object an `Upload` prototype, so it passed the scalar's
|
|
18
|
+
* `instanceof Upload` check.
|
|
19
|
+
* 2. GHSA-qwwm-252j-9whw: a second file field with the name of an already resolved upload is ignored. Before, each
|
|
20
|
+
* duplicate created a new temporary file whose reference replaced the previous one, so only the last was released
|
|
21
|
+
* when the response closed — temporary files and file descriptors leaked per request (with `maxFiles` > 1; the
|
|
22
|
+
* default is unlimited).
|
|
23
|
+
*
|
|
24
|
+
* One addition of the framework's own, not in graphql-upload (15.0.2 and 18.0.2 have the same flaws): the `map` is
|
|
25
|
+
* bounded, and with it everything the parser builds from it. A path may have at most
|
|
26
|
+
* {@link GRAPHQL_UPLOAD_MAX_PATH_KEYS} keys. The whole `map` may name at most {@link GRAPHQL_UPLOAD_MAX_MAP_KEYS}
|
|
27
|
+
* units: one per entry, one per path key and one per array element a numeric path key creates, holes included. The
|
|
28
|
+
* entry or path that goes over a bound is refused with 400 before the parser builds anything for it. Measured on
|
|
29
|
+
* Node 24, one anonymous request each, before the bounds:
|
|
30
|
+
*
|
|
31
|
+
* - object-path's `set()` recurses once per key and copies the remaining keys each time, so a path of n keys costs
|
|
32
|
+
* O(n²): an 80 KB path (40,000 keys) exhausted a 1 GB heap and ended the process, 16 KB (8,000 keys) took 300 MB and
|
|
33
|
+
* blocked the event loop for half a second; 1 MB of paths under the per-path bound blocked it for up to 640 ms.
|
|
34
|
+
* - Every entry becomes an `Upload` that is rejected with an error of its own when its file never arrives: 1 MB of
|
|
35
|
+
* entries without a path (83,000) took 230 MB and blocked the event loop for 410 ms.
|
|
36
|
+
* - object-path reads a key like `4294967294` as a number and sets that index of an array it creates, so the 258-byte
|
|
37
|
+
* request `{"1":["variables.f.4294967294"]}` handed GraphQL an array of 4,294,967,295 elements. Coercing its holes in
|
|
38
|
+
* the complexity plugin exhausted a 1 GB heap within seconds.
|
|
39
|
+
*
|
|
40
|
+
* With the bounds such requests are refused within about 30 ms, most of it spent parsing the field's JSON.
|
|
41
|
+
*
|
|
42
|
+
* `CoreModule` passes this function to `graphqlUploadExpress({ processRequest })`. A project that registers
|
|
43
|
+
* `graphqlUploadExpress()` itself must pass it too — graphql-upload's own default is the unpatched function.
|
|
44
|
+
*
|
|
45
|
+
* `Upload` comes from graphql-upload's own `Upload.js`, the class the `GraphQLUpload` scalar checks with
|
|
46
|
+
* `instanceof`: an `Upload` of any other class would be refused as an invalid upload. `ignoreStream` is not exported
|
|
47
|
+
* by graphql-upload and is re-implemented below.
|
|
48
|
+
*
|
|
49
|
+
* ---------------------------------------------------------------------------------------------------------------
|
|
50
|
+
* MIT License
|
|
51
|
+
*
|
|
52
|
+
* Copyright Jayden Seric
|
|
53
|
+
*
|
|
54
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
55
|
+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
|
|
56
|
+
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
57
|
+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
58
|
+
*
|
|
59
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
|
|
60
|
+
* the Software.
|
|
61
|
+
*
|
|
62
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
63
|
+
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
64
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
65
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
66
|
+
* SOFTWARE.
|
|
67
|
+
* ---------------------------------------------------------------------------------------------------------------
|
|
68
|
+
*/
|
|
69
|
+
import { WriteStream } from 'fs-capacitor';
|
|
70
|
+
import busboy = require('busboy');
|
|
71
|
+
import Upload = require('graphql-upload/Upload.js');
|
|
72
|
+
import createError = require('http-errors');
|
|
73
|
+
import objectPath = require('object-path');
|
|
74
|
+
|
|
75
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
76
|
+
import type { Readable } from 'node:stream';
|
|
77
|
+
|
|
78
|
+
/** graphql-upload's `GRAPHQL_MULTIPART_REQUEST_SPEC_URL`, used in the error messages exactly as upstream does. */
|
|
79
|
+
const GRAPHQL_MULTIPART_REQUEST_SPEC_URL = 'https://github.com/jaydenseric/graphql-multipart-request-spec';
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Most keys one `map` path may have (`variables.input.files.3` has 4). A longer path is refused with 400 "Invalid
|
|
83
|
+
* object path" before anything walks it: object-path's `set()` costs O(n²) in the number of keys. No request a client
|
|
84
|
+
* library builds comes near it — batched operations add one key (`0.variables.file`).
|
|
85
|
+
*/
|
|
86
|
+
export const GRAPHQL_UPLOAD_MAX_PATH_KEYS = 64;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Most units one `map` may name: one per entry, one per key of its paths, and one per element a numeric path key adds
|
|
90
|
+
* to an array, holes included — object-path sets index i, so the array becomes i + 1 long.
|
|
91
|
+
* `{"1":["variables.files.0"]}` counts 5 when `variables.files` does not exist yet and 4 when the operations already
|
|
92
|
+
* hold the slot; 2,000 files at `variables.files.N` count at most 10,000. The entry that goes over it is refused before
|
|
93
|
+
* its `Upload` exists, with 400 "Invalid ‘map’ multipart field entry key …"; the path that goes over it with 400
|
|
94
|
+
* "Invalid object path". It bounds the work of many paths (1 MB of 64-key paths blocked the event loop for up to
|
|
95
|
+
* 640 ms), of many entries (83,000 path-less entries in 1 MB took 230 MB and 410 ms) and of sparse arrays
|
|
96
|
+
* (`variables.f.4294967294` became an array of 2^32 − 1 elements, and GraphQL's list coercion walked them until the
|
|
97
|
+
* heap ran out).
|
|
98
|
+
*/
|
|
99
|
+
export const GRAPHQL_UPLOAD_MAX_MAP_KEYS = 10_000;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Options of {@link processGraphQlUploadRequest} — the same as graphql-upload's `ProcessRequestOptions`, so
|
|
103
|
+
* `graphqlUploadExpress({ processRequest, ...options })` forwards them unchanged.
|
|
104
|
+
*/
|
|
105
|
+
export interface GraphQlUploadProcessRequestOptions {
|
|
106
|
+
/** Maximum size of a non-file multipart field (`operations`, `map`) in bytes. Defaults to `1000000` (1 MB). */
|
|
107
|
+
maxFieldSize?: number;
|
|
108
|
+
/** Maximum size of one file in bytes. Defaults to `Infinity`. */
|
|
109
|
+
maxFileSize?: number;
|
|
110
|
+
/** Maximum number of files. Defaults to `Infinity`. */
|
|
111
|
+
maxFiles?: number;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The GraphQL operation (or batch of operations) a multipart request resolves to, with `Upload` instances placed. */
|
|
115
|
+
export type GraphQlUploadOperations = Array<Record<string, unknown>> | Record<string, unknown>;
|
|
116
|
+
|
|
117
|
+
/** File details an `Upload` resolves with (graphql-upload's `FileUpload`). */
|
|
118
|
+
interface ProcessedFileUpload {
|
|
119
|
+
capacitor: WriteStream;
|
|
120
|
+
createReadStream: (options?: { encoding?: BufferEncoding; highWaterMark?: number }) => Readable;
|
|
121
|
+
encoding: string;
|
|
122
|
+
filename: string;
|
|
123
|
+
mimetype: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** An error created with `http-errors` (or any other error the parser reports). */
|
|
127
|
+
type ProcessRequestError = Error & { expose?: boolean; status?: number };
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Safely ignores a readable stream (graphql-upload's `ignoreStream`, which the package does not export): an error
|
|
131
|
+
* on it must not crash the process, and its data is drained so the parser can go on.
|
|
132
|
+
*/
|
|
133
|
+
function ignoreStream(stream: Readable): void {
|
|
134
|
+
// Prevent an unhandled error from crashing the process.
|
|
135
|
+
stream.on('error', () => {});
|
|
136
|
+
|
|
137
|
+
// Waste the stream.
|
|
138
|
+
stream.resume();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* How many elements setting `key` in `container` adds to an array, the way object-path's `set()` sees it. object-path
|
|
143
|
+
* reads a key that `parseInt()` turns back into the same string as a NUMBER, and creates an array — not an object —
|
|
144
|
+
* for a missing container whose key is a number. Setting index i makes an array of length n i + 1 long when i ≥ n,
|
|
145
|
+
* every element in between a hole, and GraphQL's list coercion walks each of them. 0 for an object container, a
|
|
146
|
+
* string key, or a number that is not a non-negative integer (`NaN`, `-1`: a property, not an element).
|
|
147
|
+
*/
|
|
148
|
+
function arrayGrowth(container: unknown, key: string): number {
|
|
149
|
+
// object-path's getKey() calls parseInt() without a radix; for a key that round-trips the result is the same.
|
|
150
|
+
const index = Number.parseInt(key, 10);
|
|
151
|
+
if (index.toString() !== key || !Number.isInteger(index) || index < 0) {
|
|
152
|
+
return 0;
|
|
153
|
+
}
|
|
154
|
+
if (container === undefined) {
|
|
155
|
+
return index + 1;
|
|
156
|
+
}
|
|
157
|
+
return Array.isArray(container) ? Math.max(0, index + 1 - container.length) : 0;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Processes an incoming GraphQL multipart request
|
|
162
|
+
* (https://github.com/jaydenseric/graphql-multipart-request-spec): parses the `operations` and `map` fields, places
|
|
163
|
+
* an `Upload` wherever the `map` names one and resolves each with its file once that file streams in.
|
|
164
|
+
*
|
|
165
|
+
* graphql-upload 15.0.2's `processRequest` with the fixes of graphql-upload 18.0.2 (see the file header). Pass it to
|
|
166
|
+
* `graphqlUploadExpress({ processRequest: processGraphQlUploadRequest })`; `CoreModule` does so for `/graphql`.
|
|
167
|
+
*
|
|
168
|
+
* @returns The operation(s), with an `Upload` at every mapped path — resolved once the `map` is parsed. Rejects with
|
|
169
|
+
* an `http-errors` error (400 for a malformed request, 413 above a limit, 499 on a disconnect).
|
|
170
|
+
*/
|
|
171
|
+
export function processGraphQlUploadRequest(
|
|
172
|
+
request: IncomingMessage,
|
|
173
|
+
response: ServerResponse,
|
|
174
|
+
{ maxFieldSize = 1000000, maxFileSize = Infinity, maxFiles = Infinity }: GraphQlUploadProcessRequestOptions = {},
|
|
175
|
+
): Promise<GraphQlUploadOperations> {
|
|
176
|
+
return new Promise((resolve, reject) => {
|
|
177
|
+
let released: boolean;
|
|
178
|
+
let exitError: ProcessRequestError;
|
|
179
|
+
let operations: GraphQlUploadOperations;
|
|
180
|
+
let operationsPath: ReturnType<typeof objectPath>;
|
|
181
|
+
let map: Map<string, Upload>;
|
|
182
|
+
|
|
183
|
+
const parser = busboy({
|
|
184
|
+
headers: request.headers,
|
|
185
|
+
limits: {
|
|
186
|
+
fieldSize: maxFieldSize,
|
|
187
|
+
fields: 2, // Only operations and map.
|
|
188
|
+
fileSize: maxFileSize,
|
|
189
|
+
files: maxFiles,
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Exits request processing with an error. Successive calls have no effect.
|
|
195
|
+
*/
|
|
196
|
+
function exit(error: ProcessRequestError, isParserError = false): void {
|
|
197
|
+
if (exitError) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
exitError = error;
|
|
202
|
+
|
|
203
|
+
if (map) {
|
|
204
|
+
for (const upload of map.values()) {
|
|
205
|
+
if (!upload.file) {
|
|
206
|
+
upload.reject(exitError);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// If the error came from the parser, don't cause it to be emitted again.
|
|
212
|
+
if (isParserError) {
|
|
213
|
+
parser.destroy();
|
|
214
|
+
} else {
|
|
215
|
+
parser.destroy(exitError);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
request.unpipe(parser);
|
|
219
|
+
|
|
220
|
+
// With a sufficiently large request body, subsequent events in the same event frame cause the stream to pause
|
|
221
|
+
// after the parser is destroyed. To ensure that the request resumes, the call to .resume() is scheduled for
|
|
222
|
+
// later in the event loop.
|
|
223
|
+
setImmediate(() => {
|
|
224
|
+
request.resume();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
reject(exitError);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
parser.on('field', (fieldName: string, value: string, { valueTruncated }: { valueTruncated: boolean }) => {
|
|
231
|
+
if (valueTruncated) {
|
|
232
|
+
return exit(
|
|
233
|
+
createError(413, `The ‘${fieldName}’ multipart field value exceeds the ${maxFieldSize} byte size limit.`),
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
switch (fieldName) {
|
|
238
|
+
case 'operations':
|
|
239
|
+
try {
|
|
240
|
+
operations = JSON.parse(value);
|
|
241
|
+
} catch {
|
|
242
|
+
return exit(
|
|
243
|
+
createError(
|
|
244
|
+
400,
|
|
245
|
+
`Invalid JSON in the ‘operations’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`,
|
|
246
|
+
),
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// `operations` should be an object or an array. Note that arrays and `null` have an `object` type.
|
|
251
|
+
if (typeof operations !== 'object' || !operations) {
|
|
252
|
+
return exit(
|
|
253
|
+
createError(
|
|
254
|
+
400,
|
|
255
|
+
`Invalid type for the ‘operations’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`,
|
|
256
|
+
),
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
operationsPath = objectPath(operations);
|
|
261
|
+
|
|
262
|
+
break;
|
|
263
|
+
case 'map': {
|
|
264
|
+
if (!operations) {
|
|
265
|
+
return exit(
|
|
266
|
+
createError(
|
|
267
|
+
400,
|
|
268
|
+
`Misordered multipart fields; ‘map’ should follow ‘operations’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`,
|
|
269
|
+
),
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
let parsedMap: unknown;
|
|
274
|
+
try {
|
|
275
|
+
parsedMap = JSON.parse(value);
|
|
276
|
+
} catch {
|
|
277
|
+
return exit(
|
|
278
|
+
createError(400, `Invalid JSON in the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`),
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// `map` should be an object.
|
|
283
|
+
if (typeof parsedMap !== 'object' || !parsedMap || Array.isArray(parsedMap)) {
|
|
284
|
+
return exit(
|
|
285
|
+
createError(400, `Invalid type for the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`),
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const mapEntries = Object.entries(parsedMap);
|
|
290
|
+
|
|
291
|
+
// Check max files is not exceeded, even though the number of files to parse might not match the map
|
|
292
|
+
// provided by the client.
|
|
293
|
+
if (mapEntries.length > maxFiles) {
|
|
294
|
+
return exit(createError(413, `${maxFiles} max file uploads exceeded.`));
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
map = new Map();
|
|
298
|
+
// Map entries and path keys so far, against GRAPHQL_UPLOAD_MAX_MAP_KEYS.
|
|
299
|
+
let mapKeyCount = 0;
|
|
300
|
+
for (const [mapFieldName, paths] of mapEntries) {
|
|
301
|
+
if (!Array.isArray(paths)) {
|
|
302
|
+
return exit(
|
|
303
|
+
createError(
|
|
304
|
+
400,
|
|
305
|
+
`Invalid type for the ‘map’ multipart field entry key ‘${mapFieldName}’ array (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`,
|
|
306
|
+
),
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// Every entry counts one against GRAPHQL_UPLOAD_MAX_MAP_KEYS, before its Upload exists: an Upload whose
|
|
311
|
+
// file never arrives is rejected with an error of its own at the end of the request.
|
|
312
|
+
mapKeyCount += 1;
|
|
313
|
+
if (mapKeyCount > GRAPHQL_UPLOAD_MAX_MAP_KEYS) {
|
|
314
|
+
return exit(
|
|
315
|
+
createError(
|
|
316
|
+
400,
|
|
317
|
+
`Invalid ‘map’ multipart field entry key ‘${mapFieldName}’: the map has more than ${GRAPHQL_UPLOAD_MAX_MAP_KEYS} entries and object path keys together (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`,
|
|
318
|
+
),
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
map.set(mapFieldName, new Upload());
|
|
323
|
+
|
|
324
|
+
for (const [index, path] of paths.entries()) {
|
|
325
|
+
if (typeof path !== 'string') {
|
|
326
|
+
return exit(
|
|
327
|
+
createError(
|
|
328
|
+
400,
|
|
329
|
+
`Invalid type for the ‘map’ multipart field entry key ‘${mapFieldName}’ array index ‘${index}’ value (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`,
|
|
330
|
+
),
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
try {
|
|
335
|
+
// GHSA-5vm7-vc2x-p3w2 (graphql-upload 18.0.2): check the upload's mapped path in the operations is
|
|
336
|
+
// safe before setting it.
|
|
337
|
+
const keys = path.split('.');
|
|
338
|
+
|
|
339
|
+
// Bound the path, and the whole map, before anything walks it: object-path's `set()` recurses once per
|
|
340
|
+
// key with a copy of the remaining keys — O(n²) for a path of n keys (see the file header).
|
|
341
|
+
if (keys.length > GRAPHQL_UPLOAD_MAX_PATH_KEYS) {
|
|
342
|
+
throw new RangeError(`Object path has more than ${GRAPHQL_UPLOAD_MAX_PATH_KEYS} keys.`);
|
|
343
|
+
}
|
|
344
|
+
mapKeyCount += keys.length;
|
|
345
|
+
if (mapKeyCount > GRAPHQL_UPLOAD_MAX_MAP_KEYS) {
|
|
346
|
+
throw new RangeError(
|
|
347
|
+
`The map has more than ${GRAPHQL_UPLOAD_MAX_MAP_KEYS} entries and object path keys.`,
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
let pathValue: unknown = operations;
|
|
352
|
+
|
|
353
|
+
for (const [keyIndex, key] of keys.entries()) {
|
|
354
|
+
// Forbid `__proto__` keys to prevent prototype manipulation that could make client-supplied
|
|
355
|
+
// objects pass `instanceof Upload` checks.
|
|
356
|
+
if (key === '__proto__') {
|
|
357
|
+
throw new TypeError('Object path has a `__proto__` key.');
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// `pathValue` is this key's container. A key object-path reads as a number extends an array — a new
|
|
361
|
+
// one when the container does not exist yet — up to that index: every element it adds counts against
|
|
362
|
+
// GRAPHQL_UPLOAD_MAX_MAP_KEYS, holes included (`variables.f.4294967294` adds 4,294,967,295).
|
|
363
|
+
const growth = arrayGrowth(pathValue, key);
|
|
364
|
+
if (growth > 0) {
|
|
365
|
+
mapKeyCount += growth;
|
|
366
|
+
if (mapKeyCount > GRAPHQL_UPLOAD_MAX_MAP_KEYS) {
|
|
367
|
+
throw new RangeError(`The map's arrays have more than ${GRAPHQL_UPLOAD_MAX_MAP_KEYS} elements.`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Forbid setting an upload within an already set upload, but allow a total replacement. Resolved
|
|
372
|
+
// with object-path's own `get`, so it sees what `set` will reach, inherited array elements included.
|
|
373
|
+
if (keyIndex < keys.length - 1) {
|
|
374
|
+
pathValue = objectPath.get(pathValue as object, [key]);
|
|
375
|
+
|
|
376
|
+
if (pathValue instanceof Upload) {
|
|
377
|
+
throw new TypeError('Object path traverses an upload.');
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
operationsPath.set(path, map.get(mapFieldName));
|
|
383
|
+
} catch {
|
|
384
|
+
return exit(
|
|
385
|
+
createError(
|
|
386
|
+
400,
|
|
387
|
+
`Invalid object path for the ‘map’ multipart field entry key ‘${mapFieldName}’ array index ‘${index}’ value ‘${path}’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`,
|
|
388
|
+
),
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
resolve(operations);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
parser.on(
|
|
400
|
+
'file',
|
|
401
|
+
(
|
|
402
|
+
fieldName: string,
|
|
403
|
+
stream: Readable,
|
|
404
|
+
{ encoding, filename, mimeType: mimetype }: { encoding: string; filename: string; mimeType: string },
|
|
405
|
+
) => {
|
|
406
|
+
if (!map) {
|
|
407
|
+
ignoreStream(stream);
|
|
408
|
+
return exit(
|
|
409
|
+
createError(
|
|
410
|
+
400,
|
|
411
|
+
`Misordered multipart fields; files should follow ‘map’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`,
|
|
412
|
+
),
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const upload = map.get(fieldName);
|
|
417
|
+
|
|
418
|
+
// GHSA-qwwm-252j-9whw (graphql-upload 18.0.2): `|| upload.file` — a duplicate file field would create a new
|
|
419
|
+
// capacitor and replace the reference to the first one, which then never gets released.
|
|
420
|
+
if (!upload || upload.file) {
|
|
421
|
+
// The file is extraneous or a duplicate. As the rest can still be processed, just ignore it and don't
|
|
422
|
+
// exit with an error.
|
|
423
|
+
ignoreStream(stream);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
let fileError: ProcessRequestError;
|
|
428
|
+
|
|
429
|
+
const capacitor = new WriteStream();
|
|
430
|
+
|
|
431
|
+
capacitor.on('error', () => {
|
|
432
|
+
stream.unpipe();
|
|
433
|
+
stream.resume();
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
stream.on('limit', () => {
|
|
437
|
+
fileError = createError(413, `File truncated as it exceeds the ${maxFileSize} byte size limit.`);
|
|
438
|
+
stream.unpipe();
|
|
439
|
+
capacitor.destroy(fileError);
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
stream.on('error', (error: Error) => {
|
|
443
|
+
fileError = error;
|
|
444
|
+
stream.unpipe();
|
|
445
|
+
capacitor.destroy(fileError);
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
const file: ProcessedFileUpload = {
|
|
449
|
+
filename,
|
|
450
|
+
mimetype,
|
|
451
|
+
encoding,
|
|
452
|
+
createReadStream(options) {
|
|
453
|
+
const error = fileError || (released ? exitError : null);
|
|
454
|
+
if (error) {
|
|
455
|
+
throw error;
|
|
456
|
+
}
|
|
457
|
+
return capacitor.createReadStream(options);
|
|
458
|
+
},
|
|
459
|
+
capacitor,
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
Object.defineProperty(file, 'capacitor', {
|
|
463
|
+
enumerable: false,
|
|
464
|
+
configurable: false,
|
|
465
|
+
writable: false,
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
stream.pipe(capacitor);
|
|
469
|
+
upload.resolve(file);
|
|
470
|
+
},
|
|
471
|
+
);
|
|
472
|
+
|
|
473
|
+
parser.once('filesLimit', () => exit(createError(413, `${maxFiles} max file uploads exceeded.`)));
|
|
474
|
+
|
|
475
|
+
parser.once('finish', () => {
|
|
476
|
+
request.unpipe(parser);
|
|
477
|
+
request.resume();
|
|
478
|
+
|
|
479
|
+
if (!operations) {
|
|
480
|
+
return exit(createError(400, `Missing multipart field ‘operations’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`));
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (!map) {
|
|
484
|
+
return exit(createError(400, `Missing multipart field ‘map’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`));
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
for (const upload of map.values()) {
|
|
488
|
+
if (!upload.file) {
|
|
489
|
+
upload.reject(createError(400, 'File missing in the request.'));
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
// Use the `on` method instead of `once` as in edge cases the same parser could have multiple `error` events and
|
|
495
|
+
// all must be handled to prevent the Node.js process exiting with an error. One edge case is if there is a
|
|
496
|
+
// malformed part header as well as an unexpected end of the form.
|
|
497
|
+
parser.on('error', (error: Error) => {
|
|
498
|
+
exit(error, true);
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
response.once('close', () => {
|
|
502
|
+
released = true;
|
|
503
|
+
|
|
504
|
+
if (map) {
|
|
505
|
+
for (const upload of map.values()) {
|
|
506
|
+
if (upload.file) {
|
|
507
|
+
// Release resources and clean up temporary files.
|
|
508
|
+
(upload.file as ProcessedFileUpload).capacitor.release();
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
request.once('close', () => {
|
|
515
|
+
if (!request.readableEnded) {
|
|
516
|
+
exit(createError(499, 'Request disconnected during file upload stream parsing.'));
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
request.pipe(parser);
|
|
521
|
+
});
|
|
522
|
+
}
|