Skip to content

Commit

Permalink
fix: Content Type case sensitivity (#134)
Browse files Browse the repository at this point in the history
* fix: support content type header as Content-Type or content-type
  • Loading branch information
alexholtz authored Nov 15, 2023
1 parent b584fa1 commit de56887
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lambda/parse-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { bodySchema, CONTENT_TYPES, headersSchema } from './schema';

export function parseRequestBody(body: string, headers: IncomingHttpHeaders) {
const headersResult = headersSchema.parse(headers);
const contentType = headersResult['content-type'];
const contentType = headersResult['content-type'] || headersResult['Content-Type'];
switch (contentType) {
case CONTENT_TYPES.JSON:
return JSON.parse(body);
Expand Down
20 changes: 20 additions & 0 deletions lambda/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ describe('proxy', () => {
expect(axios.post).toHaveBeenCalled();
});

it('should forward a request when header is Content-Type', async () => {
const destinationUrl = 'https://approved.host/github-webhook/';
const endpointId = encodeURIComponent(destinationUrl);
const event: APIGatewayProxyWithLambdaAuthorizerEvent<any> = {
...baseEvent,
headers: {
...baseEvent.headers,
'content-type': undefined,
'Content-Type': 'application/json'
},
body: JSON.stringify(VALID_PUSH_PAYLOAD),
pathParameters: {
endpointId
}
};
const result = await handler(event);
expect(result).toEqual(expectedResponseObject);
expect(axios.post).toHaveBeenCalled();
});

it('should not forward a request that does not come from an enterprise or managed user suffix', async () => {
const destinationUrl = 'https://approved.host/github-webhook/';
const endpointId = encodeURIComponent(destinationUrl);
Expand Down
16 changes: 13 additions & 3 deletions lambda/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ export const CONTENT_TYPES = {
URL_ENCODED: 'application/x-www-form-urlencoded'
} as const;

export const headersSchema = z.object({
'content-type': z.nativeEnum(CONTENT_TYPES)
});
export const headersSchema = z
.object({
'content-type': z.string().optional(),
'Content-Type': z.string().optional()
})
.refine(
headers => {
return headers['content-type'] || headers['Content-Type'];
},
{
message: 'Missing Content-Type header'
}
);

export const axiosErrorSchema = z.object({
response: z.object({
Expand Down

0 comments on commit de56887

Please sign in to comment.