Skip to content

Commit 97d9f5b

Browse files
committed
security: fail closed when line webhook secret is missing (#17587)
1 parent d8d9d37 commit 97d9f5b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/line/webhook.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import crypto from "node:crypto";
22
import { describe, expect, it, vi } from "vitest";
3-
import { createLineWebhookMiddleware } from "./webhook.js";
3+
import { createLineWebhookMiddleware, startLineWebhook } from "./webhook.js";
44

55
const sign = (body: string, secret: string) =>
66
crypto.createHmac("SHA256", secret).update(body).digest("base64");
@@ -18,6 +18,15 @@ const createRes = () => {
1818
};
1919

2020
describe("createLineWebhookMiddleware", () => {
21+
it("rejects startup when channel secret is missing", () => {
22+
expect(() =>
23+
startLineWebhook({
24+
channelSecret: " ",
25+
onEvents: async () => {},
26+
}),
27+
).toThrow(/requires a non-empty channel secret/i);
28+
});
29+
2130
it("parses JSON from raw string body", async () => {
2231
const onEvents = vi.fn(async () => {});
2332
const secret = "secret";

src/line/webhook.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,17 @@ export function startLineWebhook(options: StartLineWebhookOptions): {
101101
path: string;
102102
handler: (req: Request, res: Response, _next: NextFunction) => Promise<void>;
103103
} {
104+
const channelSecret =
105+
typeof options.channelSecret === "string" ? options.channelSecret.trim() : "";
106+
if (!channelSecret) {
107+
throw new Error(
108+
"LINE webhook mode requires a non-empty channel secret. " +
109+
"Set channels.line.channelSecret in your config.",
110+
);
111+
}
104112
const path = options.path ?? "/line/webhook";
105113
const middleware = createLineWebhookMiddleware({
106-
channelSecret: options.channelSecret,
114+
channelSecret,
107115
onEvents: options.onEvents,
108116
runtime: options.runtime,
109117
});

0 commit comments

Comments
 (0)