Skip to content

Commit d4e38a9

Browse files
committed
Allow to skip signature verification
1 parent 046e478 commit d4e38a9

File tree

4 files changed

+148
-9
lines changed

4 files changed

+148
-9
lines changed

lib/middleware.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ export default function middleware(config: Types.MiddlewareConfig): Middleware {
6262
}
6363
})();
6464

65-
if (!validateSignature(body, secret, signature)) {
65+
// Check if signature verification should be skipped
66+
const shouldSkipVerification =
67+
config.skipSignatureVerification && config.skipSignatureVerification();
68+
69+
if (
70+
!shouldSkipVerification &&
71+
!validateSignature(body, secret, signature)
72+
) {
6673
next(
6774
new SignatureValidationFailed("signature validation failed", {
6875
signature,

lib/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ export interface ClientConfig extends Config {
1515

1616
export interface MiddlewareConfig extends Config {
1717
channelSecret: string;
18+
19+
// skipSignatureValidation is a function that determines whether to skip
20+
// webhook signature verification.
21+
//
22+
// If the function returns true, the signature verification step is skipped.
23+
// This can be useful in scenarios such as when you're in the process of updating
24+
// the channel secret and need to temporarily bypass verification to avoid disruptions.
25+
skipSignatureVerification?: () => boolean;
1826
}
1927

2028
export type Profile = {

test/helpers/test-server.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
} from "../../lib/exceptions.js";
1111
import * as finalhandler from "finalhandler";
1212

13-
let server: Server | null = null;
13+
// Use a map to store multiple server instances
14+
let servers: Map<number, Server> = new Map();
1415

1516
function listen(port: number, middleware?: express.RequestHandler) {
1617
const app = express();
@@ -77,17 +78,40 @@ function listen(port: number, middleware?: express.RequestHandler) {
7778
);
7879

7980
return new Promise(resolve => {
80-
server = app.listen(port, () => resolve(undefined));
81+
const server = app.listen(port, () => resolve(undefined));
82+
servers.set(port, server);
8183
});
8284
}
8385

84-
function close() {
86+
function close(port?: number) {
8587
return new Promise(resolve => {
86-
if (!server) {
87-
return resolve(undefined);
88-
}
88+
if (port !== undefined) {
89+
const server = servers.get(port);
90+
if (!server) {
91+
return resolve(undefined);
92+
}
93+
94+
server.close(() => {
95+
servers.delete(port);
96+
resolve(undefined);
97+
});
98+
} else {
99+
// Close all servers if no port is specified
100+
if (servers.size === 0) {
101+
return resolve(undefined);
102+
}
103+
104+
const promises = Array.from(servers.entries()).map(([port, server]) => {
105+
return new Promise(resolveServer => {
106+
server.close(() => {
107+
servers.delete(port);
108+
resolveServer(undefined);
109+
});
110+
});
111+
});
89112

90-
server.close(() => resolve(undefined));
113+
Promise.all(promises).then(() => resolve(undefined));
114+
}
91115
});
92116
}
93117

test/middleware.spec.ts

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ const TEST_PORT = parseInt(process.env.TEST_PORT || "1234", 10);
1313

1414
const 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+
1629
const 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

Comments
 (0)