aip-master-node-sumit 1.0.0 → 1.0.2
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.
- package/index.js +47 -41
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
1
|
const axios = require('axios');
|
|
2
|
-
|
|
3
|
-
// 🛡️ THE FIX: Allow clients to define the URL, or default to your live production API
|
|
4
2
|
const AIP_MASTER_API = process.env.AIP_MASTER_URL || 'http://localhost:8080/api/v1';
|
|
5
3
|
const API_KEY = process.env.AIP_MASTER_API_KEY;
|
|
6
4
|
|
|
7
|
-
//
|
|
5
|
+
// 🛡️ The Heartbeat System.
|
|
6
|
+
// This continuously pings the Go Server every 5 minutes
|
|
7
|
+
// to keep the dashboard "Live" without needing to restart the Express server!
|
|
8
8
|
if (API_KEY) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
const sendHeartbeat = async () => {
|
|
10
|
+
try {
|
|
11
|
+
await axios.post(`${AIP_MASTER_API}/iam/ping`, { module: 'middleware' }, {
|
|
12
|
+
headers: { 'Authorization': `Bearer ${API_KEY}` },
|
|
13
|
+
timeout: 5000 // Prevent hanging if master is slow
|
|
14
|
+
});
|
|
15
|
+
} catch (err) {
|
|
16
|
+
// Silently fail so the client's app doesn't crash if Master API is updating
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
sendHeartbeat(); // Fire immediately on server startup
|
|
21
|
+
setInterval(sendHeartbeat, 5 * 60 * 1000); // Fire every 5 minutes forever
|
|
12
22
|
}
|
|
13
23
|
|
|
14
|
-
/**
|
|
15
|
-
* Universal Security Guard (God-Mode)
|
|
16
|
-
* @param {Object} options - { requireLogin: boolean }
|
|
17
|
-
*/
|
|
18
24
|
const aipGuard = (options = { requireLogin: true }) => {
|
|
19
25
|
return async (req, res, next) => {
|
|
20
26
|
|
|
21
|
-
//
|
|
22
|
-
const MAX_PAYLOAD_BYTES = 2 * 1024 * 1024;
|
|
23
|
-
|
|
24
|
-
// Block advanced Chunked Encoding bypass attacks
|
|
27
|
+
// LAYER 1: INFRASTRUCTURE SHIELD
|
|
28
|
+
const MAX_PAYLOAD_BYTES = 2 * 1024 * 1024;
|
|
25
29
|
if (req.headers['transfer-encoding'] && req.headers['transfer-encoding'].includes('chunked')) {
|
|
26
30
|
return res.status(411).send("AIP Infrastructure Shield: Chunked encoding rejected.");
|
|
27
31
|
}
|
|
@@ -30,53 +34,55 @@ const aipGuard = (options = { requireLogin: true }) => {
|
|
|
30
34
|
return res.status(413).send("AIP Infrastructure Shield: Payload Too Large.");
|
|
31
35
|
}
|
|
32
36
|
|
|
33
|
-
// Inject Secure Anti-Hacker Headers into the browser
|
|
34
37
|
res.setHeader('X-Frame-Options', 'DENY');
|
|
35
38
|
res.setHeader('X-Content-Type-Options', 'nosniff');
|
|
36
39
|
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
|
|
37
40
|
res.setHeader('Access-Control-Allow-Origin', process.env.AIP_ALLOWED_ORIGIN || '*');
|
|
38
41
|
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
// Robust Token Extraction (Checks query, cookies, and Authorization headers)
|
|
43
|
+
let token = req.query.token || (req.cookies && req.cookies.aip_session);
|
|
44
|
+
if (!token && req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
|
|
45
|
+
token = req.headers.authorization.split(' ')[1];
|
|
46
|
+
} else if (!token && req.headers['x-aip-token']) {
|
|
47
|
+
token = req.headers['x-aip-token'];
|
|
48
|
+
}
|
|
43
49
|
|
|
50
|
+
// LAYER 2: IDENTITY CHECK & RBAC
|
|
51
|
+
if (options.requireLogin) {
|
|
52
|
+
if (!token) return res.status(401).send("Unauthorized: Missing AIP Session Token");
|
|
44
53
|
try {
|
|
45
|
-
// The Scout: Automatically grabs the route and sends it to AIP Master for RBAC Scanning
|
|
46
54
|
const currentAction = req.method + " " + (req.baseUrl + req.path);
|
|
47
55
|
const authRes = await axios.post(`${AIP_MASTER_API}/iam/verify-session`,
|
|
48
|
-
{
|
|
49
|
-
|
|
50
|
-
action: currentAction
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
headers: { 'Authorization': `Bearer ${API_KEY}` }
|
|
54
|
-
}
|
|
56
|
+
{ session_token: token, action: currentAction },
|
|
57
|
+
{ headers: { 'Authorization': `Bearer ${API_KEY}` } }
|
|
55
58
|
);
|
|
56
|
-
|
|
57
59
|
req.user = authRes.data.user;
|
|
58
|
-
res.cookie('aip_session', token, { httpOnly: true, secure: true });
|
|
59
60
|
|
|
61
|
+
const isProd = process.env.NODE_ENV === 'production';
|
|
62
|
+
res.cookie('aip_session', token, { httpOnly: true, secure: isProd });
|
|
60
63
|
} catch (error) {
|
|
61
|
-
|
|
64
|
+
const status = error.response?.status || 500;
|
|
62
65
|
const errMsg = error.response?.data?.error || "AIP Identity Blocked: Invalid Token.";
|
|
63
|
-
|
|
66
|
+
|
|
67
|
+
// 🛡️ CRITICAL FIX: Only clear the session cookie if the token is actually dead (401).
|
|
68
|
+
// If it is an RBAC block (403), we just deny the action but keep them logged in!
|
|
69
|
+
if (status === 401 && res.clearCookie) {
|
|
70
|
+
res.clearCookie('aip_session');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return res.status(status === 401 ? 401 : 403).send(errMsg);
|
|
64
74
|
}
|
|
65
75
|
}
|
|
66
76
|
|
|
67
|
-
//
|
|
77
|
+
// LAYER 3: WAF THREAT SCAN
|
|
68
78
|
try {
|
|
69
79
|
const payloadData = JSON.stringify({
|
|
70
|
-
body: req.body,
|
|
71
|
-
query: req.query
|
|
80
|
+
body: req.body || {},
|
|
81
|
+
query: req.query || {}
|
|
72
82
|
});
|
|
73
|
-
|
|
74
|
-
// Smart IP Resolver: Protects clients hosted on Vercel, Heroku, or AWS
|
|
75
83
|
const forwardedFor = req.headers['x-forwarded-for'];
|
|
76
84
|
let clientIp = req.ip || (req.connection && req.connection.remoteAddress) || "0.0.0.0";
|
|
77
|
-
|
|
78
85
|
if (forwardedFor) {
|
|
79
|
-
// Handle both string and array formats safely
|
|
80
86
|
clientIp = Array.isArray(forwardedFor)
|
|
81
87
|
? forwardedFor[0].trim()
|
|
82
88
|
: forwardedFor.split(',')[0].trim();
|
|
@@ -89,9 +95,10 @@ const aipGuard = (options = { requireLogin: true }) => {
|
|
|
89
95
|
url: req.originalUrl,
|
|
90
96
|
payload: payloadData,
|
|
91
97
|
origin: req.headers.origin || "",
|
|
92
|
-
user_agent: req.headers['user-agent'] || ""
|
|
98
|
+
user_agent: req.headers['user-agent'] || "",
|
|
99
|
+
session_token: token || "" // Explicitly forwarding the token to the Master WAF
|
|
93
100
|
});
|
|
94
|
-
|
|
101
|
+
|
|
95
102
|
if (wafRes.data.action === "block") {
|
|
96
103
|
return res.status(403).send(`AIP Firewall Blocked Request: ${wafRes.data.reason}`);
|
|
97
104
|
}
|
|
@@ -101,7 +108,6 @@ const aipGuard = (options = { requireLogin: true }) => {
|
|
|
101
108
|
return res.status(500).send("Security verification failed.");
|
|
102
109
|
}
|
|
103
110
|
|
|
104
|
-
// Passed all 3 Layers perfectly!
|
|
105
111
|
next();
|
|
106
112
|
};
|
|
107
113
|
};
|