const fs = require("fs"); const util = require("util"); const vm = require("node:vm"); const path = require("path"); require('dotenv').config() const express = require("express"); const session = require("express-session"); const fileUpload = require("express-fileupload"); const SQLiteStore = require("better-sqlite3-session-store")(session); const { Eta } = require("eta"); const { match } = require("path-to-regexp"); const bcrypt = require("bcrypt"); const cheerio = require("cheerio"); const webPush = require("web-push"); const app = express(); const _expressWs = require("express-ws")(app); const bodyParser = require("body-parser"); const model = require("./db"); const PORT = process.env.PORT || 3000; const db = model.db; const wsRouter = express.Router() let viewpath = path.join(__dirname, "views"); let eta = new Eta({ views: viewpath, cache: false, autoEscape: true }); // A complete, immutable snapshot of the routes currently available to the // runtime. Route-changing writes replace this object only after the new index // has been built, so requests never observe a partially refreshed table. let routeIndex = Object.freeze({ GET: Object.freeze([]), POST: Object.freeze([]), PUT: Object.freeze([]), DELETE: Object.freeze([]), WS: Object.freeze([]), }); const wsConnections = new Map(); const INSPECT_OPTS = { showHidden: false, depth: null, colors: false, compact: false }; function inspectArgs(args) { return args.map((v) => util.inspect(v, INSPECT_OPTS)).join(" "); } function formatError(e) { return e.stack ? `${e}\n\n${e.stack}` : `${e}`; } // Record a handler error in the structure's logs table. function logError(structureId, routeId, e) { model.createLog(structureId, routeId, formatError(e), true); } app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(express.static("public")); app.use(fileUpload()); app.use( session({ store: new SQLiteStore({ client: db, expired: { clear: true } }), secret: "your secret key", resave: false, saveUninitialized: true, cookie: { secure: false }, }), ); app.use("/", wsRouter); app.use("/plumbing", require("./plumbing")); const vapidPublicKey = process.env.VAPID_PUBLIC_KEY const vapidPrivateKey = process.env.VAPID_PRIVATE_KEY // Configure web-push with your VAPID details webPush.setVapidDetails( "mailto:signups@sheepmail.net", // a mailto URL or URL vapidPublicKey, vapidPrivateKey, ); async function saveFile(structureId, req, uploadedFile, asset=false) { const name = uploadedFile.name; const [mime_type, mime_subtype] = uploadedFile.mimetype.split("/"); // structureId may arrive as a number (route.structure_id is an INTEGER) when // called from a sandboxed handler via require('files').saveFile — the workshop // /files route passes a string param, which masked this. path.join demands // strings, so coerce. const sid = String(structureId); const uploadPath = path.join(__dirname, "public", sid); const storedPath = path.join(sid, uploadedFile.name); fs.mkdirSync(uploadPath, { recursive: true }); await uploadedFile.mv(path.join(uploadPath, name)); let id = model.createFile( structureId, name, storedPath, mime_type, mime_subtype, asset, ); let file = model.getFile(id); file.url = prefixUrlWithHost(req, file.path); return file } // A console whose log() mirrors to stdout and to the structure's logs table. function makeConsole(structureId, routeId) { return { log: function (...content) { content.forEach((c) => console.log(c)); model.createLog(structureId, routeId, inspectArgs(content)); }, }; } // Evaluate a db's "library" script with `sql`, `console`, and `fetch` bound, // returning its exports. Each library runs in its own context so nothing // leaks between dbs. function runLibrary(sql, librarySource, console) { const libContext = vm.createContext({ sql, console, fetch, module: { exports: null } }); vm.runInContext(librarySource, libContext); return libContext.module.exports; } // The `require(name)` targets available to a user handler. function makeLibs(structureId, console) { const dbs = {}; for (let appDb of model.getDbsForStructure(structureId)) { const sql = model.getDbInstance(appDb.id); dbs[appDb.alias] = { library: runLibrary(sql, appDb.library, console), sql }; } return { eta: model.getTemplater(structureId), db: (alias) => dbs[alias], push: webPush, files: { saveFile: (...args) => saveFile(structureId, ...args) }, }; } // A small, safe projection of the logged-in user for user-route sandboxes. // Returns null when nobody is logged in. Never leak the password hash or the // raw session — hand structures exactly what they need to say "hi ". function currentUserFor(req) { const id = req && req.session && req.session.userId; if (!id) return null; try { const u = model.getUserById(id); return u ? { id: u.id, username: u.username } : null; } catch (e) { return null; } } function bootstrapContext(structureId, routeId, initContext) { const structure = model.getStructure(structureId); const console = makeConsole(structureId, routeId); const libs = makeLibs(structureId, console); return vm.createContext({ ...initContext, require: (name) => libs[name], module: { exports: null }, console, vapidPublicKey: vapidPublicKey, fetch: fetch, clearTimeout: clearTimeout, setTimeout: setTimeout, route: makeRoute(structure), }); } function withPrefix(prefix, url) { return prefix ? path.join(prefix, url) : url; } function routeWithPrefix(route) { return withPrefix(route.route_prefix, route.path); } function compileWebsocketHandler(route) { try { // Keep the existing WS handler context for compatibility. Restricting the // exposed capabilities is a separate runtime-sandboxing change. const context = bootstrapContext(route.structure_id, route.id, { app }); const handler = vm.runInContext(`${route.handler}\n\nhandler;`, context); model.updateRoute({ ...route, error: null }); return handler; } catch (e) { logError(route.structure_id, route.id, e); model.updateRoute({ ...route, error: e.stack }); return null; } } function buildRoutes() { const nextIndex = { GET: [], POST: [], PUT: [], DELETE: [], WS: [], }; for (let route of model.getAllRoutes()) { const p = routeWithPrefix(route); const entry = { matcher: match(p, { decode: decodeURIComponent }), route: Object.freeze({ ...route }), }; if (route.verb === "WS") entry.handler = compileWebsocketHandler(route); nextIndex[route.verb].push(Object.freeze(entry)); } for (const verb of Object.keys(nextIndex)) Object.freeze(nextIndex[verb]); routeIndex = Object.freeze(nextIndex); } function findRuntimeRoute(verb, requestPath) { for (const entry of routeIndex[verb] || []) { const result = entry.matcher(requestPath); if (result) return { ...entry, params: result.params }; } return null; } function websocketRequestPath(req) { // express-ws internally appends `/.websocket` before routing the upgrade. return req.path.replace(/\/?\.websocket$/, "") || "/"; } // One permanent WebSocket endpoint dispatches through the same replaceable // route index as HTTP. Moving or deleting a DB-backed WS route therefore does // not leave an old Express route registered forever. wsRouter.ws("*", (ws, req) => { const found = findRuntimeRoute("WS", websocketRequestPath(req)); if (!found || !found.handler) { return ws.close(1008, "WebSocket route not found"); } const { route } = found; req.params = found.params; let clients = wsConnections.get(route.id); if (!clients) { clients = new Set(); wsConnections.set(route.id, clients); } clients.add(ws); ws.once("close", () => { clients.delete(ws); if (clients.size === 0) wsConnections.delete(route.id); }); const originalOn = ws.on.bind(ws); ws.on = (event, callback) => originalOn(event, (...args) => { try { callback(...args); } catch (e) { logError(route.structure_id, route.id, e); } }); ws.clients = clients; req.currentUser = currentUserFor(req); ws.render = (template, context = {}) => { const structure = model.getStructure(route.structure_id); context.route = makeRoute(structure); ws.send( decorate(renderTemplate(route.structure_id, template, context), { headInjection: structure.head_injection, source: sourceFor(route, req), fragment: true, }), ); }; try { return found.handler(ws, req); } catch (e) { logError(route.structure_id, route.id, e); return ws.close(1011, "WebSocket handler failed"); } }); model.applyMigrations(); buildRoutes(); // __ __ _______ _______ ______ // | | | || || || _ | // | | | || _____|| ___|| | || // | |_| || |_____ | |___ | |_||_ // | ||_____ || ___|| __ | // | | _____| || |___ | | | | // |_______||_______||_______||___| |_| app.post("/register", async (req, res) => { try { const { username, password } = req.body; const hashedPassword = await bcrypt.hash(password, 10); // 10 is the saltRounds const userId = model.createUser(username, hashedPassword); req.session.userId = userId; res.redirect("/workshop"); } catch (e) { return res.send(eta.render("auth/register", { error: e })); } }); app.post("/login", async (req, res) => { const { username, password } = req.body; const user = model.getUser(username); if (user && (await bcrypt.compare(password, user.password))) { req.session.userId = user.id; return res.redirect("/"); } return res.send( eta.render("auth/login", { error: "are you sure you entered that right?", }), ); }); app.get("/register", async (req, res) => { if (req.session.userId) { return res.redirect("/"); } return res.send(eta.render("auth/register", { error: null })); }); app.get("/login", async (req, res) => { if (req.session.userId) { return res.redirect("/"); } return res.send(eta.render("auth/login", { error: null })); }); app.all("/logout", async (req, res) => { return req.session.destroy(() => { res.redirect("/"); }); }); // _ _ _______ ______ ___ _ _______ __ __ _______ _______ // | | _ | || || _ | | | | || || | | || || | // | || || || _ || | || | |_| || _____|| |_| || _ || _ | // | || | | || |_||_ | _|| |_____ | || | | || |_| | // | || |_| || __ || |_ |_____ || || |_| || ___| // | _ || || | | || _ | _____| || _ || || | // |__| |__||_______||___| |_||___| |_||_______||__| |__||_______||___| // // The chrome injected into the of every rendered page: the client-side // stack (htmx/hyperscript/tailwind) plus the in-page editor overlay. function headChrome(headInjection) { return ` ${headInjection || ""} `; } // A `source` is the provenance of a rendered fragment: which structure/route // produced it, and (for GET routes) the snippet that re-embeds it. It becomes // the data-bliss-* attributes the inspector reads. function blissAttrs(source) { if (!source) return null; const attrs = { "data-bliss-route": `/workshop/${source.structureId}/route/${source.routeId}`, "data-bliss-clone": `/workshop/${source.structureId}/clone_modal/`, "data-bliss-structure-id": String(source.structureId), "data-bliss-structure-name": source.structureName, "data-bliss-route-id": String(source.routeId), "data-bliss-route-name": `${source.verb} ${source.path}`, "data-bliss-method": source.verb, "data-bliss-request-url": source.requestUrl, }; if (source.templateId) { attrs["data-bliss-template-id"] = String(source.templateId); attrs["data-bliss-template-name"] = source.templateName; } if (source.copyUrl) attrs["data-bliss-copy"] = source.copyUrl; return attrs; } // Full HTML document: inject the head chrome and stamp provenance on . function decoratePage(html, { headInjection, source } = {}) { const $ = cheerio.load(html); let head = $("head"); if (head.length === 0) { $("html").prepend(""); head = $("head"); } head.attr("id", "head"); head.append(headChrome(headInjection)); const attrs = blissAttrs(source); if (attrs) { for (const [k, v] of Object.entries(attrs)) $("body").attr(k, v); } return $.html(); } // Carry rendered head content through HTMX with a neutral OOB wrapper. A // literal in an HTMX response is discarded by the browser's fragment // parser, and HTMX does not discover a top-level