mdefender-pro 1.2.2 → 1.2.4

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 +23 -10
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -35,10 +35,10 @@ function getDefaultTemplate() {
35
35
  const DEFAULT_CONFIG = {
36
36
  apiKey: '',
37
37
  domain: '',
38
- apiEndpoint: 'http://127.0.0.1:8000',
38
+ apiEndpoint: 'https://mdefenderapi.onrender.com',
39
39
  mode: 'block', // 'block' | 'monitor' | 'off'
40
40
  blockStatusCode: 403,
41
- timeout: 5000,
41
+ timeout: 10000,
42
42
  maxBodySize: 1024 * 1024, // 1MB
43
43
  logBlocked: true,
44
44
  customBlockPage: null,
@@ -137,7 +137,11 @@ function extractBody(req) {
137
137
  function sendAnalyzeRequest(endpointUrl, apiKey, data, timeoutMs = 5000) {
138
138
  return new Promise((resolve, reject) => {
139
139
  try {
140
- const parsed = new URL('/api/analyze', endpointUrl);
140
+ let analyzePath = '/api/v1/waf/analyze';
141
+ if (endpointUrl.endsWith('/waf/analyze') || endpointUrl.endsWith('/analyze')) {
142
+ analyzePath = '';
143
+ }
144
+ const parsed = new URL(analyzePath, endpointUrl.replace(/\/+$/, '') + '/');
141
145
  const postData = JSON.stringify(data);
142
146
  const isHttps = parsed.protocol === 'https:';
143
147
  const transport = isHttps ? https : http;
@@ -154,7 +158,7 @@ function sendAnalyzeRequest(endpointUrl, apiKey, data, timeoutMs = 5000) {
154
158
  'Content-Type': 'application/json',
155
159
  'Content-Length': Buffer.byteLength(postData),
156
160
  'Authorization': `Bearer ${apiKey}`,
157
- 'X-MDefender-Version': '1.1.0'
161
+ 'X-MDefender-Version': '1.2.4'
158
162
  }
159
163
  };
160
164
 
@@ -165,7 +169,8 @@ function sendAnalyzeRequest(endpointUrl, apiKey, data, timeoutMs = 5000) {
165
169
  res.on('end', () => {
166
170
  try {
167
171
  const json = JSON.parse(body);
168
- resolve({ statusCode: res.statusCode, data: json });
172
+ const decisionData = json && json.data ? json.data : json;
173
+ resolve({ statusCode: res.statusCode, data: decisionData });
169
174
  } catch (e) {
170
175
  resolve({ statusCode: res.statusCode, data: { status: res.statusCode === 200 ? 'allowed' : 'error', raw: body } });
171
176
  }
@@ -271,7 +276,7 @@ function mdefender(overrides = {}) {
271
276
  domain: config.domain || host.split(':')[0],
272
277
  request: {
273
278
  method: req.method,
274
- url: parsedUrl.pathname,
279
+ url: req.originalUrl || req.url || parsedUrl.pathname,
275
280
  query_string: parsedUrl.search || '',
276
281
  query_params: Object.fromEntries(parsedUrl.searchParams),
277
282
  ip: clientIp,
@@ -287,11 +292,18 @@ function mdefender(overrides = {}) {
287
292
  };
288
293
 
289
294
  const resp = await sendAnalyzeRequest(config.apiEndpoint, config.apiKey, payload, config.timeout);
290
- const result = resp.data;
291
-
292
- if (result.status === 'blocked') {
295
+ const isBlocked = Boolean(
296
+ result && (
297
+ result.decision === 'BLOCK' ||
298
+ result.action === 'block' ||
299
+ result.status === 'blocked' ||
300
+ result.blocked === true
301
+ )
302
+ );
303
+
304
+ if (isBlocked) {
293
305
  if (config.logBlocked) {
294
- console.warn(`[MDefender] BLOCKED ${req.method} ${parsedUrl.pathname} - ${result.attack_type || 'Malicious Payload'} (${((result.confidence || 0.95) * 100).toFixed(0)}%) [IP: ${clientIp}]`);
306
+ console.warn(`[MDefender] BLOCKED ${req.method} ${parsedUrl.pathname} - ${result.attack_type || result.reason || 'Malicious Payload'} (${((result.confidence || 0.95) * 100).toFixed(0)}%) [IP: ${clientIp}]`);
295
307
  }
296
308
 
297
309
  const blockPage = result.block_page || renderBlockPage(config, result, req);
@@ -337,3 +349,4 @@ mdefender.DEFAULT_CONFIG = DEFAULT_CONFIG;
337
349
  mdefender.sendAnalyzeRequest = sendAnalyzeRequest;
338
350
 
339
351
  module.exports = mdefender;
352
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mdefender-pro",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "MDefender Pro - Web Application Firewall middleware for Node.js/Express. Protects against XSS, SQLi, CSRF, and other OWASP Top 10 attacks.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -50,7 +50,7 @@
50
50
  "type": "git",
51
51
  "url": "git+https://github.com/mdefender/mdefender.git"
52
52
  },
53
- "homepage": "https://mdefender-pro-6e3r.onrender.com",
53
+ "homepage": "https://mdefenderapi.onrender.com",
54
54
  "bugs": {
55
55
  "url": "https://github.com/mdefender/mdefender/issues"
56
56
  }