Skip to content

Commit

Permalink
Disable costly WAF
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Mar 25, 2024
1 parent 5967095 commit 18df428
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cdk/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ new Stack(app, `mfng-ai-demo`, {
},
bucketName: `mfng-ai-demo-assets`,
customDomain: {domainName: `strict.software`, subdomainName: `ai-demo.mfng`},
webAclName: `mfng-ai-demo`,
// webAclName: `mfng-ai-demo`,
});
2 changes: 2 additions & 0 deletions src/handler/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import {
reactSsrManifest,
} from './manifests.js';
import {ratelimitMiddleware} from './ratelimit-middleware.js';
import {requestBodyMiddleware} from './request-body-middleware.js';

export const app = new Hono();

app.use(authMiddleware);
app.use(requestBodyMiddleware);
app.use(ratelimitMiddleware);
app.use(loggerMiddleware);
app.get(`/*`, async (context) => handleGet(context.req.raw));
Expand Down
27 changes: 27 additions & 0 deletions src/handler/request-body-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type {LambdaFunctionURLEvent} from 'aws-lambda';
import type {MiddlewareHandler} from 'hono';

export const requestBodyMiddleware: MiddlewareHandler<{
Bindings: {
// Not available in dev server.
event?: LambdaFunctionURLEvent;
};
}> = async (context, next) => {
const {req, env} = context;
const {event} = env;

if (req.method === `POST` && event?.body) {
const bodyByteLength = Buffer.byteLength(
event.body,
event.isBase64Encoded ? `base64` : `utf-8`,
);

if (bodyByteLength > 10240) {
console.warn(`Request body too large`);

return context.text(`Content Too Large`, 413);
}
}

return next();
};

0 comments on commit 18df428

Please sign in to comment.