Browse Source

fix(api-gateway): add verification if json is valid on request

Karol Sójko 1 year ago
parent
commit
420bf9ec54
1 changed files with 13 additions and 1 deletions
  1. 13 1
      packages/api-gateway/bin/server.ts

+ 13 - 1
packages/api-gateway/bin/server.ts

@@ -36,6 +36,7 @@ import { InversifyExpressServer } from 'inversify-express-utils'
 import { ContainerConfigLoader } from '../src/Bootstrap/Container'
 import { TYPES } from '../src/Bootstrap/Types'
 import { Env } from '../src/Bootstrap/Env'
+import { IncomingMessage, ServerResponse } from 'http'
 
 const container = new ContainerConfigLoader()
 void container.load().then((container) => {
@@ -72,7 +73,18 @@ void container.load().then((container) => {
       }),
     )
 
-    app.use(json({ limit: '50mb' }))
+    app.use(
+      json({
+        limit: '50mb',
+        verify: (_req: IncomingMessage, _res: ServerResponse, buf: Buffer, _encoding: string): void => {
+          try {
+            JSON.parse(buf.toString())
+          } catch (error) {
+            logger.error(`Invalid JSON: ${(error as Error).message}. Request body: ${buf.toString()}`)
+          }
+        },
+      }),
+    )
     app.use(
       text({
         type: ['text/plain', 'application/x-www-form-urlencoded', 'application/x-www-form-urlencoded; charset=utf-8'],