@@ -13,6 +13,19 @@ const TEST_PORT = parseInt(process.env.TEST_PORT || "1234", 10);
1313
1414const m = middleware ( { channelSecret : "test_channel_secret" } ) ;
1515
16+ // Middleware with skipSignatureVerification function (always true)
17+ const mWithSkipAlwaysTrue = middleware ( {
18+ channelSecret : "test_channel_secret" ,
19+ skipSignatureVerification : ( ) => true ,
20+ } ) ;
21+
22+ // Middleware with skipSignatureVerification function (dynamic behavior based on environment variable)
23+ let shouldSkipSignature = false ;
24+ const mWithDynamicSkip = middleware ( {
25+ channelSecret : "test_channel_secret" ,
26+ skipSignatureVerification : ( ) => shouldSkipSignature ,
27+ } ) ;
28+
1629const getRecentReq = ( ) : { body : Types . WebhookRequestBody } =>
1730 JSON . parse ( readFileSync ( join ( __dirname , "helpers/request.json" ) ) . toString ( ) ) ;
1831
@@ -53,8 +66,95 @@ describe("middleware test", () => {
5366 beforeAll ( ( ) => {
5467 listen ( TEST_PORT , m ) ;
5568 } ) ;
69+
70+ describe ( "With skipSignatureVerification functionality" , ( ) => {
71+ // Port for always-true skip function
72+ let alwaysTruePort : number ;
73+ // Port for dynamic skip function
74+ let dynamicSkipPort : number ;
75+
76+ beforeAll ( ( ) => {
77+ alwaysTruePort = TEST_PORT + 1 ;
78+ dynamicSkipPort = TEST_PORT + 2 ;
79+ listen ( alwaysTruePort , mWithSkipAlwaysTrue ) ;
80+ return listen ( dynamicSkipPort , mWithDynamicSkip ) ;
81+ } ) ;
82+
83+ afterAll ( ( ) => {
84+ close ( alwaysTruePort ) ;
85+ return close ( dynamicSkipPort ) ;
86+ } ) ;
87+
88+ it ( "should skip signature verification when skipSignatureVerification returns true" , async ( ) => {
89+ const client = new HTTPClient ( {
90+ baseURL : `http://localhost:${ alwaysTruePort } ` ,
91+ defaultHeaders : {
92+ "X-Line-Signature" : "invalid_signature" ,
93+ } ,
94+ } ) ;
95+
96+ // This should work even with invalid signature because verification is skipped
97+ await client . post ( "/webhook" , {
98+ events : [ webhook ] ,
99+ destination : DESTINATION ,
100+ } ) ;
101+
102+ const req = getRecentReq ( ) ;
103+ deepEqual ( req . body . destination , DESTINATION ) ;
104+ deepEqual ( req . body . events , [ webhook ] ) ;
105+ } ) ;
106+
107+ it ( "should respect dynamic skipSignatureVerification behavior - when true" , async ( ) => {
108+ // Set to skip verification
109+ shouldSkipSignature = true ;
110+
111+ const client = new HTTPClient ( {
112+ baseURL : `http://localhost:${ dynamicSkipPort } ` ,
113+ defaultHeaders : {
114+ "X-Line-Signature" : "invalid_signature" ,
115+ } ,
116+ } ) ;
117+
118+ // This should work even with invalid signature because verification is skipped
119+ await client . post ( "/webhook" , {
120+ events : [ webhook ] ,
121+ destination : DESTINATION ,
122+ } ) ;
123+
124+ const req = getRecentReq ( ) ;
125+ deepEqual ( req . body . destination , DESTINATION ) ;
126+ deepEqual ( req . body . events , [ webhook ] ) ;
127+ } ) ;
128+
129+ it ( "should respect dynamic skipSignatureVerification behavior - when false" , async ( ) => {
130+ // Set to NOT skip verification
131+ shouldSkipSignature = false ;
132+
133+ const client = new HTTPClient ( {
134+ baseURL : `http://localhost:${ dynamicSkipPort } ` ,
135+ defaultHeaders : {
136+ "X-Line-Signature" : "invalid_signature" ,
137+ } ,
138+ } ) ;
139+
140+ try {
141+ // This should fail because signature verification is not skipped
142+ await client . post ( "/webhook" , {
143+ events : [ webhook ] ,
144+ destination : DESTINATION ,
145+ } ) ;
146+ ok ( false , "Expected to throw an error due to invalid signature" ) ;
147+ } catch ( err ) {
148+ if ( err instanceof HTTPError ) {
149+ equal ( err . statusCode , 401 ) ;
150+ } else {
151+ throw err ;
152+ }
153+ }
154+ } ) ;
155+ } ) ;
56156 afterAll ( ( ) => {
57- close ( ) ;
157+ close ( TEST_PORT ) ;
58158 } ) ;
59159
60160 describe ( "Succeeds on parsing valid request" , ( ) => {
0 commit comments