@shieldwall/express-middleware 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +49 -0
  2. package/package.json +12 -0
package/index.js ADDED
@@ -0,0 +1,49 @@
1
+ const fetch = require("node-fetch");
2
+
3
+ const SHIELDWALL_API = "https://app-69cd5b67c5454eb378342788.base44.app/api/functions/validateRequest";
4
+
5
+ function shieldwall(options = {}) {
6
+ const { siteId, apiKey, onBlocked, skipPaths = [] } = options;
7
+ if (!siteId) throw new Error("[ShieldWall] siteId is required");
8
+
9
+ return async function (req, res, next) {
10
+ try {
11
+ if (skipPaths.some((p) => req.path.startsWith(p))) return next();
12
+
13
+ const ip =
14
+ req.headers["x-forwarded-for"]?.split(",")[0]?.trim() ||
15
+ req.socket.remoteAddress || "0.0.0.0";
16
+
17
+ const body = {
18
+ site_id: siteId,
19
+ ip,
20
+ user_agent: req.headers["user-agent"] || "",
21
+ path: req.path,
22
+ method: req.method,
23
+ };
24
+
25
+ if (apiKey) body.api_key = apiKey;
26
+
27
+ const response = await fetch(SHIELDWALL_API, {
28
+ method: "POST",
29
+ headers: { "Content-Type": "application/json" },
30
+ body: JSON.stringify(body),
31
+ });
32
+
33
+ const result = await response.json();
34
+
35
+ if (!result.allowed) {
36
+ if (typeof onBlocked === "function") return onBlocked(req, res, result);
37
+ return res.status(403).json({ error: "Blocked by ShieldWall", reason: result.reason });
38
+ }
39
+
40
+ req.shieldwall = result;
41
+ next();
42
+ } catch (err) {
43
+ console.warn("[ShieldWall] Failing open:", err.message);
44
+ next();
45
+ }
46
+ };
47
+ }
48
+
49
+ module.exports = shieldwall;
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@shieldwall/express-middleware",
3
+ "version": "1.0.0",
4
+ "description": "ShieldWall security middleware for Express.js",
5
+ "main": "index.js",
6
+ "keywords": ["security", "middleware", "express", "waf"],
7
+ "author": "ShieldWall",
8
+ "license": "MIT",
9
+ "dependencies": {
10
+ "node-fetch": "^2.7.0"
11
+ }
12
+ }