1
1
import { Static , Type , TypeBoxTypeProvider } from '@fastify/type-provider-typebox' ;
2
+ import { TypeCompiler } from '@sinclair/typebox/compiler' ;
2
3
import Fastify , {
3
4
FastifyInstance ,
4
5
FastifyPluginCallback ,
@@ -22,7 +23,13 @@ const ServerOptionsSchema = Type.Object({
22
23
port : Type . Integer ( ) ,
23
24
auth_token : Type . String ( ) ,
24
25
external_base_url : Type . String ( ) ,
26
+
27
+ /** Wait for the chainhook node to be available before submitting predicates */
25
28
wait_for_chainhook_node : Type . Boolean ( { default : true } ) ,
29
+ /** Validate the JSON schema of received chainhook payloads and report errors when invalid */
30
+ validate_chainhook_payloads : Type . Boolean ( { default : false } ) ,
31
+ /** Size limit for received chainhook payloads (default 40MB) */
32
+ body_limit : Type . Number ( { default : 41943040 } ) ,
26
33
} ) ;
27
34
/** Local event server connection and authentication options */
28
35
export type ServerOptions = Static < typeof ServerOptionsSchema > ;
@@ -174,6 +181,7 @@ export async function buildServer(
174
181
Server ,
175
182
TypeBoxTypeProvider
176
183
> = ( fastify , options , done ) => {
184
+ const compiledSchema = TypeCompiler . Compile ( PayloadSchema ) ;
177
185
fastify . addHook ( 'preHandler' , isEventAuthorized ) ;
178
186
fastify . post (
179
187
'/chainhook/:uuid' ,
@@ -186,6 +194,14 @@ export async function buildServer(
186
194
} ,
187
195
} ,
188
196
async ( request , reply ) => {
197
+ if ( serverOpts . validate_chainhook_payloads && ! compiledSchema . Check ( request . body ) ) {
198
+ logger . error (
199
+ [ ...compiledSchema . Errors ( request . body ) ] ,
200
+ `ChainhookEventObserver received an invalid payload`
201
+ ) ;
202
+ await reply . code ( 422 ) . send ( ) ;
203
+ return ;
204
+ }
189
205
try {
190
206
await callback ( request . params . uuid , request . body ) ;
191
207
await reply . code ( 200 ) . send ( ) ;
@@ -202,7 +218,7 @@ export async function buildServer(
202
218
trustProxy : true ,
203
219
logger : PINO_CONFIG ,
204
220
pluginTimeout : 0 , // Disable so ping can retry indefinitely
205
- bodyLimit : 41943040 , // 40 MB
221
+ bodyLimit : serverOpts . body_limit ,
206
222
} ) . withTypeProvider < TypeBoxTypeProvider > ( ) ;
207
223
208
224
if ( serverOpts . wait_for_chainhook_node ) {
0 commit comments