Skip to content

Commit 5c2b461

Browse files
authored
Merge pull request #4 from shgysk8zer0/bug/2
Deprecate `parseMultipartFormData()`
2 parents 77cd76a + 8267eed commit 5c2b461

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [v1.0.1] - 2023-09-24
10+
11+
### Deprecated
12+
- Deprecated `parseMultipartFormData()` due to [ReDoS issue](https://github.com/shgysk8zer0/node-http/issues/2)
13+
14+
### Fixed
15+
- Fix `imports` in `package.json` to work with `@shgysk8zer0/http/module` as well as `@shgysk8zer0/http/module.js`
16+
917
## [v1.0.0] - 2023-09-22
1018

1119
Initial Release

README.md

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ npm i @shgysk8zer0/http
4848
### NPM Imports
4949
```js
5050
import { HTTPError } from 'shgysk8zer0/http@shgysk8zer0/http/error.js';
51-
import { NOT_IMPLEMENTED } from 'shgysk8zer0/http@shgysk8zer0/http/status.js';
51+
import { NOT_IMPLEMENTED, INTERNAL_SERVER_ERROR } from 'shgysk8zer0/http@shgysk8zer0/http/status.js';
5252
import { JSON } from 'shgysk8zer0/http@shgysk8zer0/http/types.js';
5353
import { Cookie } from 'shgysk8zer0/http@shgysk8zer0/http/cookie.js';
5454
```
@@ -60,32 +60,46 @@ It is designed to be versatile and is not limited to a specific Node.js environm
6060

6161
```js
6262
import { HTTPError } from 'https://unpkg.com/@shgysk8zer0/http/error.js';
63-
import { NOT_IMPLEMENTED } from 'https://unpkg.com/@shgysk8zer0/http/status.js';
63+
import { NOT_IMPLEMENTED, INTERNAL_SERVER_ERROR } from 'https://unpkg.com/@shgysk8zer0/http/status.js';
6464
import { JSON } from 'https://unpkg.com/@shgysk8zer0/http/types.js';
6565
import { Cookie } from 'https://unpkg.com/@shgysk8zer0/http/cookie.js';
6666
```
6767

6868
### Example Code
6969

70+
```js
7071
export async function handler() {
71-
const error = new HTTPError('Not implemented.', {
72-
status: NOT_IMPLEMENTED,
73-
cause: new Error('I have not done this yet...'),
74-
});
75-
76-
return new Response([error], {
77-
status: error.status,
78-
headers: new Headers({
79-
'Content-Type': JSON,
80-
'Set-Cookie': new Cookie('uid', crypto.randomUUID(), {
81-
domain: 'example.com',
82-
path: '/foo',
83-
maxAge: 86_400_000,
84-
sameSite: 'Strict',
85-
httpOnly: true,
86-
partitioned: true,
87-
}
88-
}),
89-
});
72+
try {
73+
const error = new HTTPError('Not implemented.', {
74+
status: NOT_IMPLEMENTED,
75+
cause: new Error('I have not done this yet...'),
76+
});
77+
78+
throw err;
79+
} catch (err) {
80+
if (err instanceof HTTPError) { // Error has an HTTP status & message for use by client
81+
return Response.json(error, {
82+
status: error.status,
83+
headers: new Headers({
84+
'Content-Type': JSON,
85+
'Set-Cookie': new Cookie('uid', crypto.randomUUID(), {
86+
domain: 'example.com',
87+
path: '/foo',
88+
maxAge: 86_400_000,
89+
sameSite: 'Strict',
90+
httpOnly: true,
91+
partitioned: true,
92+
})
93+
}),
94+
});
95+
} else { // It is not an HTTPError and may contain sensitive into
96+
return Response.json({
97+
error: {
98+
messsage: 'Something broke :(',
99+
status: INTERNAL_SERVER_ERROR,
100+
}
101+
}, { status: INTERNAL_SERVER_ERROR });
102+
}
103+
}
90104
}
91105
```

form-data.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ const PATTERN = /^(\r\n)?(?:Content-Disposition:\s?form-data;\s?name="(?<name>[^
55
/**
66
* Parse a multipart/form-data body and extract form fields and files.
77
*
8+
* @deprecated This function is potentially vulnerable to ReDoS attacks and is not necessary in node >= 20
9+
* @see https://github.com/shgysk8zer0/node-http/issues/2
810
* @param {string} body - The raw string of the multipart/form-data body.
911
* @param {string} contentType - The Content-Type header specifying the boundary.
1012
* @returns {FormData} - A FormData object containing the parsed data.
1113
* @throws {TypeError} - If the body or contentType is not a string.
1214
* @throws {Error} - If the contentType is not valid for multipart/form-data.
1315
*/
1416
export function parseMultipartFormData(body, contentType) {
17+
console.warn('parseMultipartFormData() is deprecated and will be removed in version 2.0.0');
18+
1519
if (typeof body !== 'string') {
1620
throw new TypeError('body must be a string.');
1721
} else if (typeof contentType !== 'string') {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
{
22
"name": "@shgysk8zer0/http",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A JavaScript library that provides various utilities for working with HTTP",
5-
"keywords": ["http", "http-status", "http-error","form-data", "http-cookie", "file-object"],
5+
"keywords": [
6+
"http",
7+
"http-status",
8+
"http-error",
9+
"http-cookie",
10+
"node-http"
11+
],
612
"type": "module",
713
"main": "http.cjs",
814
"module": "http.js",
@@ -12,6 +18,9 @@
1218
"import": "./http.js",
1319
"require": "./http.cjs"
1420
},
21+
"./*.js": {
22+
"import": "./*"
23+
},
1524
"./*": {
1625
"import": "./*.js"
1726
}

0 commit comments

Comments
 (0)