Skip to content

Commit de56887

Browse files
authored
fix: Content Type case sensitivity (#134)
* fix: support content type header as Content-Type or content-type
1 parent b584fa1 commit de56887

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

lambda/parse-request-body.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { bodySchema, CONTENT_TYPES, headersSchema } from './schema';
1717

1818
export function parseRequestBody(body: string, headers: IncomingHttpHeaders) {
1919
const headersResult = headersSchema.parse(headers);
20-
const contentType = headersResult['content-type'];
20+
const contentType = headersResult['content-type'] || headersResult['Content-Type'];
2121
switch (contentType) {
2222
case CONTENT_TYPES.JSON:
2323
return JSON.parse(body);

lambda/proxy.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ describe('proxy', () => {
117117
expect(axios.post).toHaveBeenCalled();
118118
});
119119

120+
it('should forward a request when header is Content-Type', async () => {
121+
const destinationUrl = 'https://approved.host/github-webhook/';
122+
const endpointId = encodeURIComponent(destinationUrl);
123+
const event: APIGatewayProxyWithLambdaAuthorizerEvent<any> = {
124+
...baseEvent,
125+
headers: {
126+
...baseEvent.headers,
127+
'content-type': undefined,
128+
'Content-Type': 'application/json'
129+
},
130+
body: JSON.stringify(VALID_PUSH_PAYLOAD),
131+
pathParameters: {
132+
endpointId
133+
}
134+
};
135+
const result = await handler(event);
136+
expect(result).toEqual(expectedResponseObject);
137+
expect(axios.post).toHaveBeenCalled();
138+
});
139+
120140
it('should not forward a request that does not come from an enterprise or managed user suffix', async () => {
121141
const destinationUrl = 'https://approved.host/github-webhook/';
122142
const endpointId = encodeURIComponent(destinationUrl);

lambda/schema.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ export const CONTENT_TYPES = {
1111
URL_ENCODED: 'application/x-www-form-urlencoded'
1212
} as const;
1313

14-
export const headersSchema = z.object({
15-
'content-type': z.nativeEnum(CONTENT_TYPES)
16-
});
14+
export const headersSchema = z
15+
.object({
16+
'content-type': z.string().optional(),
17+
'Content-Type': z.string().optional()
18+
})
19+
.refine(
20+
headers => {
21+
return headers['content-type'] || headers['Content-Type'];
22+
},
23+
{
24+
message: 'Missing Content-Type header'
25+
}
26+
);
1727

1828
export const axiosErrorSchema = z.object({
1929
response: z.object({

0 commit comments

Comments
 (0)