From 9a56065bbb93a3c7c6dd655b37a2ac825d175f14 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 14 May 2021 15:43:35 -0400 Subject: build endpoint --- .gitignore | 2 ++ index.js | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0ce09c75..b3bbeb6d 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ verified data lnurl-server.sqlite3 watcher.log +build.json +build.sh diff --git a/index.js b/index.js index 7304862f..871e7530 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,12 @@ const axios = require("axios"); +const crypto = require("crypto"); const bodyParser = require("body-parser"); const compression = require("compression"); const cookieParser = require("cookie-parser"); const cors = require("cors"); const express = require("express"); const { Op } = require("sequelize"); +const fs = require("fs"); ah = require("express-async-handler"); @@ -80,6 +82,44 @@ if (config.lnurl) { require("./routes/lnurl"); } +const sigHeaderName = "X-Hub-Signature-256"; +const sigHashAlg = "sha256"; +function verifyPostData(req, res, next) { + try { + if (!req.rawBody) { + return next("Request body empty"); + } + + const sig = Buffer.from(req.get(sigHeaderName) || "", "utf8"); + const hmac = crypto.createHmac(sigHashAlg, config.github); + const digest = Buffer.from( + sigHashAlg + "=" + hmac.update(req.rawBody).digest("hex"), + "utf8" + ); + + if (sig.length !== digest.length || !crypto.timingSafeEqual(digest, sig)) { + return next( + `Request body digest (${digest}) did not match ${sigHeaderName} (${sig})` + ); + } + } catch (e) { + console.log(e.message); + } + + return next(); +} + +app.post("/build", verifyPostData, (req, res) => { + fs.writeFileSync("build.json", req.body.payload); + + const { exec } = require("child_process"); + exec("bash build.sh", (error, stdout, stderr) => { + if (error !== null) { + console.log(`exec error: ${error}`); + } + }); +}); + app.use((err, req, res, next) => { const details = { path: req.path, @@ -104,6 +144,6 @@ server.listen(config.port, () => process.on("SIGINT", process.exit); -process.on('uncaughtException', function (exception) { +process.on("uncaughtException", function(exception) { console.log(exception); }); -- cgit v1.2.3