graphql-upload-nextjs 2.0.0 → 2.0.1
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/CODE_OF_CONDUCT.md +25 -0
- package/CONTRIBUTING.md +45 -0
- package/LICENSE +1 -1
- package/README.md +13 -5
- package/SECURITY.md +25 -0
- package/index.d.ts +1 -1
- package/index.js +71 -8
- package/package.json +18 -11
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
This project operates on respect, practical engineering, and clear communication.
|
|
4
|
+
|
|
5
|
+
## Expected Behavior
|
|
6
|
+
|
|
7
|
+
* Default to patience and assume good intent.
|
|
8
|
+
* Keep discussions anchored to code, documentation, and user outcomes.
|
|
9
|
+
* Deliver feedback constructively.
|
|
10
|
+
* Respect varying experience levels and communication styles.
|
|
11
|
+
|
|
12
|
+
## Unacceptable Behavior
|
|
13
|
+
|
|
14
|
+
We maintain a strict policy against:
|
|
15
|
+
|
|
16
|
+
* Harassment, threats, or personal attacks.
|
|
17
|
+
* Discriminatory language or conduct.
|
|
18
|
+
* Publishing private information without explicit consent.
|
|
19
|
+
* Persistent disruption after a maintainer requests it to stop.
|
|
20
|
+
|
|
21
|
+
## Enforcement
|
|
22
|
+
|
|
23
|
+
Maintainers enforce these standards. We will remove comments, close issues, or permanently ban contributors who violate them.
|
|
24
|
+
|
|
25
|
+
If you encounter an issue, contact a maintainer directly through the project repository.
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Thank you for dedicating time to improve `graphql-upload-nextjs`.
|
|
4
|
+
|
|
5
|
+
## Development Setup
|
|
6
|
+
|
|
7
|
+
Initialize, build, and test the core package:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install
|
|
11
|
+
npm run build
|
|
12
|
+
npm test
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Verify the example application independently:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
cd examples/example-graphql-upload-nextjs
|
|
20
|
+
npm install
|
|
21
|
+
npm run build
|
|
22
|
+
npm run lint
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Pull Requests
|
|
27
|
+
|
|
28
|
+
We maintain strict engineering standards for incoming code:
|
|
29
|
+
|
|
30
|
+
* Keep scope tight. Maintain public API compatibility unless breaking changes are explicitly discussed in advance.
|
|
31
|
+
* Write or update tests to cover any behavioral changes.
|
|
32
|
+
* Pass the root build and test suite entirely before opening a pull request.
|
|
33
|
+
* Update the README when altering user-facing behavior, configuration options, or examples.
|
|
34
|
+
* Exclude generated tarballs, local upload outputs, and dependency directories from your commits.
|
|
35
|
+
|
|
36
|
+
## Reporting Issues
|
|
37
|
+
|
|
38
|
+
Clear, reproducible reports lead to faster fixes. When opening an issue, please include:
|
|
39
|
+
|
|
40
|
+
* The `graphql-upload-nextjs` package version.
|
|
41
|
+
* Your active Node.js and Next.js versions.
|
|
42
|
+
* A minimal reproduction repository or the exact request payload.
|
|
43
|
+
* A concise description of the expected and actual behavior.
|
|
44
|
+
|
|
45
|
+
For security vulnerabilities, please refer directly to `SECURITY.md` for proper disclosure steps.
|
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://github.com/lafittemehdy/graphql-upload-nextjs/blob/master/LICENSE)
|
|
6
6
|
[](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
7
7
|
[](https://nodejs.org)
|
|
8
|
-
[](https://www.typescriptlang.org)
|
|
9
9
|
|
|
10
10
|
A [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec) implementation for [Next.js](https://nextjs.org) App Router with [Apollo Server](https://www.apollographql.com/docs/apollo-server). Enables file uploads via GraphQL mutations using the `Upload` scalar, with built-in MIME type verification via magic bytes.
|
|
11
11
|
|
|
@@ -16,6 +16,7 @@ A [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multip
|
|
|
16
16
|
- Supports single file uploads, multiple file uploads, and operation batching.
|
|
17
17
|
- File deduplication (one file mapped to multiple operation paths).
|
|
18
18
|
- MIME type verified via magic bytes using [file-type](https://npm.im/file-type), not trusting client headers.
|
|
19
|
+
- Unknown binary files fall back to `application/octet-stream` instead of the client-provided MIME type.
|
|
19
20
|
- Configurable `allowedTypes`, `maxFileSize`, and `maxFiles`.
|
|
20
21
|
- Only reads 4KB for MIME detection — streams the rest directly from `Blob`.
|
|
21
22
|
- Spec-compatible property names: `filename`, `mimetype`, `encoding`, `createReadStream`.
|
|
@@ -166,16 +167,16 @@ export const OPTIONS = requestHandler;
|
|
|
166
167
|
```typescript
|
|
167
168
|
await uploadProcess(request, context, server, {
|
|
168
169
|
allowedTypes: ["image/jpeg", "image/png", "text/plain"],
|
|
169
|
-
maxFiles: 10,
|
|
170
170
|
maxFileSize: 10 * 1024 * 1024, // 10MB
|
|
171
|
+
maxFiles: 10,
|
|
171
172
|
});
|
|
172
173
|
```
|
|
173
174
|
|
|
174
175
|
| Option | Type | Default | Description |
|
|
175
176
|
|---|---|---|---|
|
|
176
|
-
| `allowedTypes` | `string[]` | `undefined` | Restrict MIME types.
|
|
177
|
-
| `
|
|
178
|
-
| `
|
|
177
|
+
| `allowedTypes` | `string[]` | `undefined` | Restrict MIME types. Checked against the detected MIME type; unknown binary files are `application/octet-stream`. |
|
|
178
|
+
| `maxFileSize` | `number` | `undefined` | Max file size in bytes. Must be finite and non-negative. Returns 413 if exceeded. |
|
|
179
|
+
| `maxFiles` | `number` | `undefined` | Max files per request. Must be finite and non-negative. Returns 413 if exceeded. |
|
|
179
180
|
|
|
180
181
|
### Client Usage
|
|
181
182
|
|
|
@@ -268,6 +269,7 @@ The spec defines a [multipart form field structure](https://github.com/jaydenser
|
|
|
268
269
|
|---|---|
|
|
269
270
|
| MIME magic byte verification | Real file type detected via [file-type](https://npm.im/file-type), not trusting client-provided `Content-Type`. |
|
|
270
271
|
| `allowedTypes` filtering | Server-side restriction of accepted MIME types. |
|
|
272
|
+
| Unknown binary fallback | Files with no recognized magic bytes and non-text contents are reported as `application/octet-stream`. |
|
|
271
273
|
| `fileSize` property | File size in bytes available on the resolved file object. |
|
|
272
274
|
|
|
273
275
|
### Limitations
|
|
@@ -300,6 +302,12 @@ npm test
|
|
|
300
302
|
|
|
301
303
|
CI runs automatically on push and pull requests via GitHub Actions (build + test on Node.js 22/24, example build + lint).
|
|
302
304
|
|
|
305
|
+
Please read [CONTRIBUTING.md](CONTRIBUTING.md) and [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) before opening a pull request.
|
|
306
|
+
|
|
307
|
+
## Security
|
|
308
|
+
|
|
309
|
+
Please read [SECURITY.md](SECURITY.md) before reporting security-sensitive issues.
|
|
310
|
+
|
|
303
311
|
## License
|
|
304
312
|
|
|
305
313
|
MIT. See [LICENSE](https://github.com/lafittemehdy/graphql-upload-nextjs/blob/master/LICENSE).
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Supported Versions
|
|
4
|
+
|
|
5
|
+
We provide security fixes exclusively for the latest major version.
|
|
6
|
+
|
|
7
|
+
| Version | Supported |
|
|
8
|
+
| --- | --- |
|
|
9
|
+
| 2.x | Yes |
|
|
10
|
+
| < 2.0 | No |
|
|
11
|
+
|
|
12
|
+
## Reporting a Vulnerability
|
|
13
|
+
|
|
14
|
+
If GitHub private vulnerability reporting is active on this repository, please utilize it to submit your report securely.
|
|
15
|
+
|
|
16
|
+
If private reporting is unavailable, open a regular GitHub issue with a concise, non-sensitive description. Request a private disclosure channel from a maintainers before sharing exploits, secrets, or payloads.
|
|
17
|
+
|
|
18
|
+
Please include:
|
|
19
|
+
|
|
20
|
+
* The affected package version.
|
|
21
|
+
* A clear description of the potential impact.
|
|
22
|
+
* Safe steps to reproduce the issue, if they can be shared securely.
|
|
23
|
+
* Any known temporary workarounds.
|
|
24
|
+
|
|
25
|
+
We review valid reports promptly to coordinate fixes and issue security releases.
|
package/index.d.ts
CHANGED
|
@@ -64,7 +64,7 @@ export declare function uploadProcess<TContext extends Record<string, unknown>>(
|
|
|
64
64
|
}) => Promise<GraphQLResponse<TContext>>;
|
|
65
65
|
}, settings?: {
|
|
66
66
|
allowedTypes?: string[];
|
|
67
|
-
maxFiles?: number;
|
|
68
67
|
maxFileSize?: number;
|
|
68
|
+
maxFiles?: number;
|
|
69
69
|
}): Promise<NextResponse>;
|
|
70
70
|
export {};
|
package/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { GraphQLError, GraphQLScalarType } from 'graphql';
|
|
|
3
3
|
import { isText } from 'istextorbinary';
|
|
4
4
|
import { NextResponse } from 'next/server.js';
|
|
5
5
|
import { Readable } from 'stream';
|
|
6
|
+
const DEFAULT_MIME_TYPE = 'application/octet-stream';
|
|
6
7
|
/**
|
|
7
8
|
* Represents an instance of a file upload.
|
|
8
9
|
* Holds a promise that resolves with the file details once processed.
|
|
@@ -90,8 +91,8 @@ export function parseOperationsJSON(input) {
|
|
|
90
91
|
* and returns a resolved Upload instance with streaming file access.
|
|
91
92
|
*/
|
|
92
93
|
async function processUpload(file, allowedTypes) {
|
|
93
|
-
if (!file.name || typeof file.size !== 'number' || !file.
|
|
94
|
-
throw new Error('Invalid file properties: name
|
|
94
|
+
if (!file.name || typeof file.size !== 'number' || !Number.isFinite(file.size) || file.size < 0) {
|
|
95
|
+
throw new Error('Invalid file properties: name or size is missing or invalid.');
|
|
95
96
|
}
|
|
96
97
|
const blob = file;
|
|
97
98
|
// Only read the first 4100 bytes for MIME type detection via magic bytes.
|
|
@@ -99,7 +100,7 @@ async function processUpload(file, allowedTypes) {
|
|
|
99
100
|
const DETECTION_BYTES = 4100;
|
|
100
101
|
const headerBuffer = Buffer.from(await blob.slice(0, DETECTION_BYTES).arrayBuffer());
|
|
101
102
|
const fileTypeInfo = await fileTypeFromBuffer(new Uint8Array(headerBuffer.buffer, headerBuffer.byteOffset, headerBuffer.byteLength));
|
|
102
|
-
let mimetype =
|
|
103
|
+
let mimetype = DEFAULT_MIME_TYPE;
|
|
103
104
|
if (fileTypeInfo) {
|
|
104
105
|
mimetype = fileTypeInfo.mime;
|
|
105
106
|
}
|
|
@@ -110,7 +111,7 @@ async function processUpload(file, allowedTypes) {
|
|
|
110
111
|
}
|
|
111
112
|
}
|
|
112
113
|
catch {
|
|
113
|
-
// Fallback: isText detection failed,
|
|
114
|
+
// Fallback: isText detection failed, keeping the unknown binary default.
|
|
114
115
|
}
|
|
115
116
|
}
|
|
116
117
|
if (allowedTypes && !allowedTypes.includes(mimetype)) {
|
|
@@ -142,6 +143,37 @@ export function sanitizeAndValidateJSON(input) {
|
|
|
142
143
|
}
|
|
143
144
|
/** Dangerous property names that could lead to prototype pollution. */
|
|
144
145
|
const BLOCKED_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
146
|
+
/** Checks whether a value is a plain object record suitable for GraphQL variables. */
|
|
147
|
+
function isObjectRecord(value) {
|
|
148
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
149
|
+
}
|
|
150
|
+
/** Validates the minimal GraphQL operation shape required for execution. */
|
|
151
|
+
function validateOperation(operation, label) {
|
|
152
|
+
if (!isObjectRecord(operation)) {
|
|
153
|
+
return `Invalid ${label}: expected an operation object.`;
|
|
154
|
+
}
|
|
155
|
+
if (typeof operation.query !== 'string') {
|
|
156
|
+
return `Invalid ${label}: expected query to be a string.`;
|
|
157
|
+
}
|
|
158
|
+
if (operation.variables !== undefined && !isObjectRecord(operation.variables)) {
|
|
159
|
+
return `Invalid ${label}: expected variables to be an object when provided.`;
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
/** Validates upload process settings before request body processing starts. */
|
|
164
|
+
function validateSettings(settings) {
|
|
165
|
+
if (settings?.maxFileSize !== undefined && (typeof settings.maxFileSize !== 'number' ||
|
|
166
|
+
!Number.isFinite(settings.maxFileSize) ||
|
|
167
|
+
settings.maxFileSize < 0)) {
|
|
168
|
+
return 'Invalid maxFileSize setting: expected a finite non-negative number.';
|
|
169
|
+
}
|
|
170
|
+
if (settings?.maxFiles !== undefined && (typeof settings.maxFiles !== 'number' ||
|
|
171
|
+
!Number.isFinite(settings.maxFiles) ||
|
|
172
|
+
settings.maxFiles < 0)) {
|
|
173
|
+
return 'Invalid maxFiles setting: expected a finite non-negative number.';
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
145
177
|
/** Sets a value at a dot-notation path in a nested object, creating intermediate containers as needed. */
|
|
146
178
|
export function setValueAtPath(obj, path, value) {
|
|
147
179
|
const keys = path.split('.');
|
|
@@ -192,6 +224,18 @@ export function validateMap(map) {
|
|
|
192
224
|
if (typeof p !== 'string') {
|
|
193
225
|
return `Invalid path in map entry '${key}': expected string.`;
|
|
194
226
|
}
|
|
227
|
+
if (!p) {
|
|
228
|
+
return `Invalid path in map entry '${key}': path cannot be empty.`;
|
|
229
|
+
}
|
|
230
|
+
const segments = p.split('.');
|
|
231
|
+
for (const segment of segments) {
|
|
232
|
+
if (!segment) {
|
|
233
|
+
return `Invalid path in map entry '${key}': path cannot contain empty segments.`;
|
|
234
|
+
}
|
|
235
|
+
if (BLOCKED_KEYS.has(segment)) {
|
|
236
|
+
return `Invalid path segment in map entry '${key}': '${segment}' is not allowed.`;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
195
239
|
}
|
|
196
240
|
}
|
|
197
241
|
return null;
|
|
@@ -199,6 +243,10 @@ export function validateMap(map) {
|
|
|
199
243
|
/** Processes a GraphQL multipart request with file uploads. Supports batching per the spec. */
|
|
200
244
|
export async function uploadProcess(request, contextValueInput, server, settings) {
|
|
201
245
|
try {
|
|
246
|
+
const settingsError = validateSettings(settings);
|
|
247
|
+
if (settingsError) {
|
|
248
|
+
return NextResponse.json({ errors: [{ message: settingsError }] }, { status: 400 });
|
|
249
|
+
}
|
|
202
250
|
const formData = await request.formData();
|
|
203
251
|
const files = await extractFiles(formData);
|
|
204
252
|
const mapRaw = formData.get('map');
|
|
@@ -228,8 +276,22 @@ export async function uploadProcess(request, contextValueInput, server, settings
|
|
|
228
276
|
catch {
|
|
229
277
|
return NextResponse.json({ errors: [{ message: 'Invalid JSON in the operations multipart field.' }] }, { status: 400 });
|
|
230
278
|
}
|
|
279
|
+
if (Array.isArray(parsedOperations)) {
|
|
280
|
+
for (let i = 0; i < parsedOperations.length; i++) {
|
|
281
|
+
const operationError = validateOperation(parsedOperations[i], `operation at index ${i}`);
|
|
282
|
+
if (operationError) {
|
|
283
|
+
return NextResponse.json({ errors: [{ message: operationError }] }, { status: 400 });
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
const operationError = validateOperation(parsedOperations, 'operation');
|
|
289
|
+
if (operationError) {
|
|
290
|
+
return NextResponse.json({ errors: [{ message: operationError }] }, { status: 400 });
|
|
291
|
+
}
|
|
292
|
+
}
|
|
231
293
|
// Check maxFiles limit.
|
|
232
|
-
if (settings?.maxFiles && Object.keys(map).length > settings.maxFiles) {
|
|
294
|
+
if (settings?.maxFiles !== undefined && Object.keys(map).length > settings.maxFiles) {
|
|
233
295
|
return NextResponse.json({ errors: [{ message: `${settings.maxFiles} max file uploads exceeded.` }] }, { status: 413 });
|
|
234
296
|
}
|
|
235
297
|
// Normalize operations into a keyed object for path resolution.
|
|
@@ -238,9 +300,10 @@ export async function uploadProcess(request, contextValueInput, server, settings
|
|
|
238
300
|
const isBatch = Array.isArray(parsedOperations);
|
|
239
301
|
let operationsRoot;
|
|
240
302
|
if (isBatch) {
|
|
303
|
+
const batchOperations = parsedOperations;
|
|
241
304
|
operationsRoot = {};
|
|
242
|
-
for (let i = 0; i <
|
|
243
|
-
const op =
|
|
305
|
+
for (let i = 0; i < batchOperations.length; i++) {
|
|
306
|
+
const op = batchOperations[i];
|
|
244
307
|
if (!op.variables)
|
|
245
308
|
op.variables = {};
|
|
246
309
|
operationsRoot[String(i)] = op;
|
|
@@ -265,7 +328,7 @@ export async function uploadProcess(request, contextValueInput, server, settings
|
|
|
265
328
|
}
|
|
266
329
|
continue;
|
|
267
330
|
}
|
|
268
|
-
if (settings?.maxFileSize && file.size > settings.maxFileSize) {
|
|
331
|
+
if (settings?.maxFileSize !== undefined && file.size > settings.maxFileSize) {
|
|
269
332
|
return NextResponse.json({ errors: [{ message: `File ${file.name} size is too large. Maximum allowed size is ${(settings.maxFileSize / (1024 * 1024)).toFixed(2)}MB.` }] }, { status: 413 });
|
|
270
333
|
}
|
|
271
334
|
const variablePaths = map[fileKeyInMap];
|
package/package.json
CHANGED
|
@@ -7,26 +7,29 @@
|
|
|
7
7
|
"url": "https://github.com/lafittemehdy/graphql-upload-nextjs/issues"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@apollo/server": "^5.
|
|
11
|
-
"file-type": "^
|
|
12
|
-
"graphql": "^16.
|
|
10
|
+
"@apollo/server": "^5.5.1",
|
|
11
|
+
"file-type": "^22.0.1",
|
|
12
|
+
"graphql": "^16.14.0",
|
|
13
13
|
"istextorbinary": "^9.5.0",
|
|
14
|
-
"next": "^16.
|
|
14
|
+
"next": "^16.2.6"
|
|
15
15
|
},
|
|
16
16
|
"description": "The middleware and Upload scalar in this package enable GraphQL multipart requests (file uploads via queries and mutations) in your Apollo Server Next.js integration.",
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@types/jest": "^30.0.0",
|
|
19
|
-
"@types/node": "^25.
|
|
19
|
+
"@types/node": "^25.9.1",
|
|
20
20
|
"husky": "^9.1.7",
|
|
21
|
-
"jest": "^30.
|
|
21
|
+
"jest": "^30.4.2",
|
|
22
22
|
"nodemon": "^3.1.14",
|
|
23
|
-
"ts-jest": "^29.4.
|
|
24
|
-
"typescript": "^
|
|
25
|
-
"undici": "^
|
|
23
|
+
"ts-jest": "^29.4.10",
|
|
24
|
+
"typescript": "^6.0.3",
|
|
25
|
+
"undici": "^8.3.0"
|
|
26
26
|
},
|
|
27
27
|
"files": [
|
|
28
|
+
"CODE_OF_CONDUCT.md",
|
|
29
|
+
"CONTRIBUTING.md",
|
|
28
30
|
"LICENSE",
|
|
29
31
|
"README.md",
|
|
32
|
+
"SECURITY.md",
|
|
30
33
|
"index.d.ts",
|
|
31
34
|
"index.js"
|
|
32
35
|
],
|
|
@@ -49,6 +52,10 @@
|
|
|
49
52
|
"license": "MIT",
|
|
50
53
|
"main": "index.js",
|
|
51
54
|
"name": "graphql-upload-nextjs",
|
|
55
|
+
"overrides": {
|
|
56
|
+
"postcss": "8.5.14"
|
|
57
|
+
},
|
|
58
|
+
"packageManager": "npm@10.9.3",
|
|
52
59
|
"publishConfig": {
|
|
53
60
|
"access": "public",
|
|
54
61
|
"provenance": true
|
|
@@ -63,9 +70,9 @@
|
|
|
63
70
|
"prepublishOnly": "npm run build && npm test",
|
|
64
71
|
"start": "tsc && node index.js",
|
|
65
72
|
"test": "jest",
|
|
66
|
-
"prepare": "husky"
|
|
73
|
+
"prepare": "husky || exit 0"
|
|
67
74
|
},
|
|
68
75
|
"type": "module",
|
|
69
76
|
"types": "index.d.ts",
|
|
70
|
-
"version": "2.0.
|
|
77
|
+
"version": "2.0.1"
|
|
71
78
|
}
|