Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multipart/form-data in the request body #2321

Merged
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f0f4fc0
add test for (currently failing) multipart form data POST requests --…
ilanashapiro Jun 30, 2023
a541db8
implement support for multipart/form-data requests with working test
ilanashapiro Jul 2, 2023
cbf87de
added tests to the test harness for multipart/form-data
ilanashapiro Jul 3, 2023
9baa519
delete commented out code
ilanashapiro Jul 3, 2023
84a6490
Parse multiform data passed with -F flag rather than --data flag
ilanashapiro Jul 11, 2023
55364bd
Parse multiform data passed with -F flag rather than --data flag
ilanashapiro Jul 11, 2023
51f6722
Merge branch 'stoplightio:master' into feature/multipart-form-data-va…
ilanashapiro Jul 11, 2023
c0410d0
resolve merge
ilanashapiro Jul 11, 2023
b954b44
add test files and cleanup whitespace
ilanashapiro Jul 11, 2023
57b53d9
delete tests not relevant to multipart form data
ilanashapiro Jul 11, 2023
fffddeb
resolve conflict in yarn.lock file
ilanashapiro Jul 11, 2023
b568f73
use npm package to parse boundary string and form data from multipart
ilanashapiro Jul 16, 2023
4a0a530
resolve yarn.lock conflict
ilanashapiro Jul 16, 2023
2c9f3a3
improve error message for missing boundary
ilanashapiro Jul 16, 2023
2466aff
add null checks for parsing boundary from content-type hea
ilanashapiro Jul 17, 2023
c81993f
handle whitespace in the other location media type and boundary strin…
ilanashapiro Jul 18, 2023
79dedc5
resolve merge conflict in yarn.lock
ilanashapiro Jul 23, 2023
53bfedd
parse MIME header with library, improve error handling/add tests for …
ilanashapiro Jul 24, 2023
e209c1b
attempt to resolve node install error on server by committing generat…
ilanashapiro Jul 24, 2023
fb50bc7
fix formatting
ilanashapiro Jul 25, 2023
8a6a229
condense verbose code block
ilanashapiro Jul 25, 2023
9818ba0
condense verbose code block
ilanashapiro Jul 25, 2023
362a79d
Merge branch 'feature/multipart-form-data-validation' of https://gith…
ilanashapiro Jul 25, 2023
b2df062
Merge branch 'master' into feature/multipart-form-data-validation
chohmann Jul 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23,305 changes: 23,305 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@types/type-is": "^1.6.3",
"@types/uri-template-lite": "^19.12.1",
"@types/urijs": "^1.19.17",
"@types/whatwg-mimetype": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^2.34.0",
"@typescript-eslint/parser": "^4.33.0",
"abstract-logging": "^2.0.1",
Expand All @@ -50,6 +51,7 @@
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^4.2.1",
"fast-xml-parser": "^4.2.0",
"form-data": "^4.0.0",
"gavel": "^10.0.3",
"glob": "^7.2.0",
"http-string-parser": "^0.0.6",
Expand Down
118 changes: 118 additions & 0 deletions packages/http-server/src/__tests__/body-params-validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import fetch, { RequestInit } from 'node-fetch';
import { createServer } from '../';
import { ThenArg } from '../types';
import * as faker from '@faker-js/faker/locale/en';
import { Dictionary } from '@stoplight/types';
import * as FormData from 'form-data';

const logger = createLogger('TEST', { enabled: false });

Expand Down Expand Up @@ -718,6 +720,83 @@ describe('body params validation', () => {
tags: [],
security: [],
},
{
id: '?http-operation-id?',
method: 'post',
path: '/multipart-form-data-body-required',
responses: [
{
id: faker.random.word(),
code: '200',
headers: [],
contents: [
{
id: faker.random.word(),
mediaType: 'application/json',
schema: {
type: 'object',
properties: {
type: {
type: 'string',
},
},
},
examples: [
{
id: faker.random.word(),
key: 'test',
value: { type: 'foo' },
},
],
encodings: [],
},
],
},
],
servers: [],
request: {
body: {
id: faker.random.word(),
required: true,
contents: [
{
id: faker.random.word(),
mediaType: 'multipart/form-data',
schema: {
type: 'object',
properties: {
status: {
type: 'string'
},
lines: {
type: 'string'
},
test_img_file: {
type: 'string'
},
test_json_file: {
type: 'string'
},
num: {
type: 'integer'
},
},
required: ['status'],
$schema: 'http://json-schema.org/draft-07/schema#',
},
examples: [],
encodings: [],
},
],
},
headers: [],
query: [],
cookie: [],
path: [],
},
tags: [],
security: [],
},
]);
});

Expand Down Expand Up @@ -796,5 +875,44 @@ describe('body params validation', () => {
expect(response.status).toBe(200);
});
});

describe('valid multipart form data parameter provided', () => {
let requestParams: Dictionary<any>;
beforeEach(() => {
const formData = new FormData();
formData.append("status", "--=\"");
formData.append("lines", "\r\n\r\n\s");
formData.append("test_img_file", "@test_img.png");
formData.append("test_json_file", "<test_json.json");
formData.append("num", "10");

requestParams = {
method: 'POST',
body: formData
};
});

describe('boundary string generated correctly', () =>{
test('returns 200', async () => {
const response = await makeRequest('/multipart-form-data-body-required', requestParams);
expect(response.status).toBe(200);
expect(response.json()).resolves.toMatchObject({ type: 'foo' });
});
});

describe('missing generated boundary string due to content-type manually specified in the header', () => {
test('returns 415 & error message', async () => {
requestParams['headers'] = { 'content-type':'multipart/form-data' };
const response = await makeRequest('/multipart-form-data-body-required', requestParams);
expect(response.status).toBe(415);
expect(response.json()).resolves.toMatchObject({
detail: "Boundary parameter for multipart/form-data is not defined or generated in the request header. Try removing manually defined content-type from your request header if it exists.",
status: 415,
title: "Invalid content type",
type: "https://stoplight.io/prism/errors#INVALID_CONTENT_TYPE",
});
});
});
});
});
});
Binary file added packages/http-server/src/__tests__/test_img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions packages/http-server/src/__tests__/test_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"surname" : "Doe",
"name" : "John",
"city" : "Manchester",
"address" : "5 Main Street",
"hobbies" : ["painting","lawnbowls"]
}
4 changes: 3 additions & 1 deletion packages/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
"json-schema-faker": "0.5.0-rcv.40",
"lodash": "^4.17.21",
"node-fetch": "^2.6.5",
"parse-multipart-data": "^1.5.0",
"pino": "^6.13.3",
"tslib": "^2.3.1",
"type-is": "^1.6.18",
"uri-template-lite": "^22.9.0"
"uri-template-lite": "^22.9.0",
"whatwg-mimetype": "^3.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
19 changes: 13 additions & 6 deletions packages/http/src/mocker/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IPrismComponents, IPrismDiagnostic, IPrismInput } from '@stoplight/prism-core';
import {
DiagnosticSeverity,
Dictionary,
IHttpHeaderParam,
IHttpOperation,
IHttpOperationResponse,
Expand Down Expand Up @@ -42,7 +43,9 @@ import {
deserializeFormBody,
findContentByMediaTypeOrFirst,
splitUriParams,
parseMultipartFormDataParams
} from '../validator/validators/body';
import { parseMIMEHeader } from '../validator/validators/headers';
import { NonEmptyArray } from 'fp-ts/NonEmptyArray';
export { resetGenerator as resetJSONSchemaGenerator } from './generator/JSONSchema';

Expand All @@ -68,7 +71,6 @@ const mock: IPrismComponents<IHttpOperation, IHttpRequest, IHttpResponse, IHttpM
logger.info(`Request contains an accept header: ${acceptMediaType}`);
config.mediaTypes = acceptMediaType.split(',');
}

return config;
}),
R.chain(mockConfig => negotiateResponse(mockConfig, input, resource)),
Expand Down Expand Up @@ -137,19 +139,24 @@ function runCallbacks({
we cannot carry parsed informations in case of an error — which is what we do need instead.
*/
function parseBodyIfUrlEncoded(request: IHttpRequest, resource: IHttpOperation) {
const mediaType = caseless(request.headers || {}).get('content-type');
if (!mediaType) return request;
const contentTypeHeader = caseless(request.headers || {}).get('content-type');
if (!contentTypeHeader) return request;
const [multipartBoundary, mediaType] = parseMIMEHeader(contentTypeHeader);

if (!is(mediaType, ['application/x-www-form-urlencoded'])) return request;
if (!is(mediaType, ['application/x-www-form-urlencoded', 'multipart/form-data'])) return request;

const specs = pipe(
O.fromNullable(resource.request),
O.chainNullableK(request => request.body),
O.chainNullableK(body => body.contents),
O.getOrElse(() => [] as IMediaTypeContent[])
);

const encodedUriParams = splitUriParams(request.body as string);

const requestBody = request.body as string;
const encodedUriParams = pipe(
mediaType === "multipart/form-data" ? parseMultipartFormDataParams(requestBody, multipartBoundary) : splitUriParams(requestBody),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseMultipartFormDataParams returns an error wrapped in E.Left if the boundary sting is empty. Here I convert that to a default value (i.e. empty dictionary) if that occurs. I'm not sure if I'm supposed to be doing anything else here -- this same behavior (unwrapping the monad using defaults) is implemented for findContentByMediaTypeOrFirst just below on line 169, so I attempted to emulate that.

E.getOrElse<IPrismDiagnostic[], Dictionary<string>>(() => ({} as Dictionary<string>))
);

if (specs.length < 1) {
return Object.assign(request, { body: encodedUriParams });
Expand Down
19 changes: 10 additions & 9 deletions packages/http/src/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { findOperationResponse } from './utils/spec';
import { validateBody, validateHeaders, validatePath, validateQuery } from './validators';
import { NonEmptyArray } from 'fp-ts/NonEmptyArray';
import { ValidationContext } from './validators/types';
import { parseMIMEHeader } from '../validator/validators/headers';
import { wildcardMediaTypeMatch } from './utils/wildcardMediaTypeMatch';

export { validateSecurity } from './validators/security';
Expand Down Expand Up @@ -53,15 +54,16 @@ const isMediaTypeSupportedInContents = (mediaType?: string, contents?: IMediaTyp

const validateInputIfBodySpecIsProvided = (
body: O.Option<unknown>,
mediaType: string,
requestBody: O.Option<IHttpOperationRequestBody>,
mediaType?: string,
multipartBoundary?: string,
bundle?: unknown
) =>
pipe(
sequenceOption(body, requestBody),
O.fold(
() => E.right(body),
([body, contents]) => validateBody(body, contents.contents ?? [], ValidationContext.Input, mediaType, bundle)
([body, contents]) => validateBody(body, contents.contents ?? [], ValidationContext.Input, mediaType, multipartBoundary, bundle)
)
);

Expand All @@ -75,17 +77,19 @@ const validateInputBody = (
checkRequiredBodyIsProvided(requestBody, body),
E.map(b => [...b, caseless(headers || {})] as const),
E.chain(([requestBody, body, headers]) => {
const contentTypeHeader = headers.get('content-type');
const [multipartBoundary, mediaType] = contentTypeHeader ? parseMIMEHeader(contentTypeHeader) : [undefined, undefined];

const contentLength = parseInt(headers.get('content-length')) || 0;
if (contentLength === 0) {
// generously allow this content type if there isn't a body actually provided
return E.right([requestBody, body, headers] as const);
return E.right([requestBody, body, mediaType, multipartBoundary] as const);
}

let errorMessage = 'No supported content types, but request included a non-empty body';
if (O.isSome(requestBody)) {
const mediaType = headers.get('content-type');
if (isMediaTypeSupportedInContents(mediaType, requestBody.value.contents)) {
return E.right([requestBody, body, headers] as const);
return E.right([requestBody, body, mediaType, multipartBoundary] as const);
}

const specRequestBodyContents = requestBody.value.contents || [];
Expand All @@ -103,10 +107,7 @@ const validateInputBody = (
},
]);
}),
E.chain(([requestBody, body, headers]) => {
const mediaType = headers.get('content-type');
return validateInputIfBodySpecIsProvided(body, mediaType, requestBody, bundle);
})
E.chain(([requestBody, body, mediaType, multipartBoundary]) => validateInputIfBodySpecIsProvided(body, requestBody, mediaType, multipartBoundary, bundle))
);

export const validateInput: ValidatorFn<IHttpOperation, IHttpRequest> = ({ resource, element }) => {
Expand Down
Loading