@@ -605,4 +605,51 @@ describe("createNodeMiddleware(webhooks)", () => {
605605
606606 server . close ( ) ;
607607 } ) ;
608+
609+ test ( "Handles invalid signature" , async ( ) => {
610+ expect . assertions ( 3 ) ;
611+
612+ const webhooks = new Webhooks ( {
613+ secret : "mySecret" ,
614+ } ) ;
615+
616+ webhooks . onError ( ( error ) => {
617+ expect ( error . message ) . toContain (
618+ "signature does not match event payload and secret" ,
619+ ) ;
620+ } ) ;
621+
622+ const log = {
623+ debug : jest . fn ( ) ,
624+ info : jest . fn ( ) ,
625+ warn : jest . fn ( ) ,
626+ error : jest . fn ( ) ,
627+ } ;
628+ const middleware = createNodeMiddleware ( webhooks , { log } ) ;
629+ const server = createServer ( middleware ) . listen ( ) ;
630+
631+ // @ts -expect-error complains about { port } although it's included in returned AddressInfo interface
632+ const { port } = server . address ( ) ;
633+
634+ const response = await fetch (
635+ `http://localhost:${ port } /api/github/webhooks` ,
636+ {
637+ method : "POST" ,
638+ headers : {
639+ "Content-Type" : "application/json" ,
640+ "X-GitHub-Delivery" : "1" ,
641+ "X-GitHub-Event" : "push" ,
642+ "X-Hub-Signature-256" : "" ,
643+ } ,
644+ body : pushEventPayload ,
645+ } ,
646+ ) ;
647+
648+ expect ( response . status ) . toEqual ( 400 ) ;
649+ await expect ( response . text ( ) ) . resolves . toBe (
650+ '{"error":"Error: [@octokit/webhooks] signature does not match event payload and secret"}' ,
651+ ) ;
652+
653+ server . close ( ) ;
654+ } ) ;
608655} ) ;
0 commit comments