auto-smart-security 1.0.7 → 1.0.9

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/README.md CHANGED
@@ -41,6 +41,7 @@ applySecurity(app, {
41
41
  mode: 'prod',
42
42
  pathWhitelist: ['admin/', 'media', 'oauth2'],
43
43
  rateLimit: { max: 120, windowMs: 60_000 },
44
+ trustProxy: 1
44
45
  bot: { enabled: true },
45
46
  blacklistTTL: 10 * 60 * 1000,
46
47
 
@@ -83,6 +84,9 @@ interface SecurityOptions {
83
84
  max: number;
84
85
  };
85
86
 
87
+ /** express trust proxy setting */
88
+ trustProxy?: number;
89
+
86
90
  /** Bot detection */
87
91
  bot?: {
88
92
  enabled: boolean;
@@ -142,6 +146,7 @@ const redis = new Redis({
142
146
  applySecurity(app, {
143
147
  mode: process.env.NODE_ENV === 'development' ? 'dev' : 'prod',
144
148
  rateLimit: { max: 120, windowMs: 60_000 },
149
+ trustProxy: 1,
145
150
  bot: { enabled: true },
146
151
  blacklistTTL: 10 * 60 * 1000,
147
152
  pathWhitelist: ['api', '/media'],
@@ -15,13 +15,15 @@ function applySecurity(app, options) {
15
15
  return;
16
16
  if (options.rateLimit) {
17
17
  if (options.trustProxy === undefined) {
18
- console.warn('[auto-smart-security] rateLimit enabled without trustProxy, defaulting to trust proxy = true');
19
- app.set('trust proxy', true);
20
- }
21
- else {
22
- app.set('trust proxy', options.trustProxy);
18
+ throw new Error('[auto-smart-security] rateLimit requires trustProxy to be a number (e.g. 1). Do NOT use true.');
23
19
  }
20
+ app.set('trust proxy', options.trustProxy);
24
21
  }
22
+ /** ================= HELMET ================= */
23
+ app.use((0, helmet_1.default)({
24
+ crossOriginResourcePolicy: false, // 🔥
25
+ crossOriginOpenerPolicy: false, // disable API
26
+ }));
25
27
  /** ================= BLACKLIST STORE ================= */
26
28
  const blacklist = options.blacklist?.store ??
27
29
  new memory_store_1.MemoryBlacklistStore(options.staticBlacklist, options.blacklistTTL);
@@ -44,8 +46,12 @@ function applySecurity(app, options) {
44
46
  }
45
47
  /** ================= MAIN SECURITY ================= */
46
48
  app.use(async (req, res, next) => {
49
+ // pass OPTIONS requests
50
+ if (req.method === 'OPTIONS')
51
+ return next();
47
52
  const ip = (0, utils_1.getClientIP)(req);
48
53
  const url = req.originalUrl;
54
+ console.log('url =========>', url, ip);
49
55
  /** 1️⃣ Blacklist */
50
56
  if (await blacklist.isBlocked(ip)) {
51
57
  return res.status(403).send('Access denied');
@@ -79,6 +85,4 @@ function applySecurity(app, options) {
79
85
  }
80
86
  next();
81
87
  });
82
- /** ================= HELMET ================= */
83
- app.use((0, helmet_1.default)());
84
88
  }
package/dist/types.d.ts CHANGED
@@ -19,7 +19,7 @@ export interface SecurityOptions {
19
19
  /** dev → skip security */
20
20
  mode?: 'prod' | 'dev';
21
21
  /** express trust proxy setting */
22
- trustProxy?: boolean | number | string;
22
+ trustProxy?: number;
23
23
  /** allow only these paths */
24
24
  pathWhitelist: string[];
25
25
  /** hard blacklist */
package/dist/utils.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getClientIP = getClientIP;
4
4
  function getClientIP(req) {
5
5
  return (req.headers?.['x-real-ip'] ||
6
- req.headers?.['x-forwarded-for']?.split(',')[0] ||
6
+ // (req.headers?.['x-forwarded-for'] as string)?.split(',')[0] ||
7
7
  req.socket?.remoteAddress ||
8
8
  req.ip ||
9
9
  'unknown');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-smart-security",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Production-ready security middleware for Express / NestJS",
5
5
  "author": "Hai Vinh <haivinhinspirit@gmail.com>",
6
6
  "main": "dist/index.js",