julien-c HF staff commited on
Commit
b865405
1 Parent(s): 2e22a88

Protect webhook with `X-Webhook-Secret`

Browse files
Files changed (1) hide show
  1. server.ts +10 -4
server.ts CHANGED
@@ -1,12 +1,18 @@
1
  import * as express from "express";
2
 
3
- const expressApp = express();
 
 
4
 
5
- expressApp.get("/", (req, res) => {
6
  res.json({ hello: "world" });
7
  });
8
 
9
- expressApp.post("/", (req, res) => {
 
 
 
 
10
  console.log(req.body);
11
 
12
  res.json({ success: true });
@@ -14,6 +20,6 @@ expressApp.post("/", (req, res) => {
14
 
15
 
16
  const PORT = 7860;
17
- expressApp.listen(PORT, () => {
18
  console.debug(`server started at http://localhost:${PORT}`);
19
  });
 
1
  import * as express from "express";
2
 
3
+ const app = express();
4
+ // parse HTTP request bodies as json
5
+ app.use(express.json());
6
 
7
+ app.get("/", (req, res) => {
8
  res.json({ hello: "world" });
9
  });
10
 
11
+ app.post("/", (req, res) => {
12
+
13
+ if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
14
+ return res.status(400).json({ error: "incorrect secret" });
15
+ }
16
  console.log(req.body);
17
 
18
  res.json({ success: true });
 
20
 
21
 
22
  const PORT = 7860;
23
+ app.listen(PORT, () => {
24
  console.debug(`server started at http://localhost:${PORT}`);
25
  });