Skip to content

Commit

Permalink
fix(fastify): Support large RAMF messages and trust proxy headers (#203)
Browse files Browse the repository at this point in the history
* fix(fastify): Support large RAMF messages and trust proxy headers

* fix formatting

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
gnarea and kodiakhq[bot] authored Sep 22, 2020
1 parent 4218661 commit e7878b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/services/fastifyUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pino from 'pino';

import { mockSpy } from '../_test_utils';
import { configureMockEnvVars, getMockContext, getMockInstance } from './_test_utils';
import { MAX_RAMF_MESSAGE_SIZE } from './constants';
import { configureFastify, runFastify } from './fastifyUtils';

const mockFastify: FastifyInstance = {
Expand Down Expand Up @@ -61,6 +62,20 @@ describe('configureFastify', () => {
expect(fastifyCallArgs[0]).toHaveProperty('requestIdHeader', requestIdHeader.toLowerCase());
});

test('Maximum request body should allow for the largest RAMF message', () => {
configureFastify([dummyRoutes]);

const fastifyCallArgs = getMockContext(fastify).calls[0];
expect(fastifyCallArgs[0]).toHaveProperty('bodyLimit', MAX_RAMF_MESSAGE_SIZE);
});

test('Proxy request headers should be trusted', () => {
configureFastify([dummyRoutes]);

const fastifyCallArgs = getMockContext(fastify).calls[0];
expect(fastifyCallArgs[0]).toHaveProperty('trustProxy', true);
});

test('Routes should be loaded', async () => {
await configureFastify([dummyRoutes]);

Expand Down
3 changes: 3 additions & 0 deletions src/services/fastifyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
HTTPMethods,
} from 'fastify';
import { Logger } from 'pino';
import { MAX_RAMF_MESSAGE_SIZE } from './constants';

const DEFAULT_REQUEST_ID_HEADER = 'X-Request-Id';
const SERVER_PORT = 8080;
Expand Down Expand Up @@ -55,11 +56,13 @@ export async function configureFastify<RouteOptions extends FastifyPluginOptions
logger: FastifyLogger = true,
): Promise<FastifyInstance> {
const server = fastify({
bodyLimit: MAX_RAMF_MESSAGE_SIZE,
logger,
requestIdHeader: getEnvVar('REQUEST_ID_HEADER')
.default(DEFAULT_REQUEST_ID_HEADER)
.asString()
.toLowerCase(),
trustProxy: true,
});

await Promise.all(routes.map((route) => server.register(route, routeOptions)));
Expand Down

0 comments on commit e7878b9

Please sign in to comment.