nestjs-security-cli 1.3.3 → 1.3.5
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 +116 -80
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,75 +11,78 @@ npm install nestjs-security-cli
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
|
-
import { Module } from '@nestjs/common'
|
|
15
|
-
import { MongooseModule } from '@nestjs/mongoose'
|
|
16
|
-
import { SecurityModule, BlacklistedIp, BlacklistedIpSchema, SecurityMiddleware } from 'nestjs-security-cli'
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
14
|
+
import { Module } from '@nestjs/common'
|
|
15
|
+
import { MongooseModule } from '@nestjs/mongoose'
|
|
16
|
+
import { SecurityModule, BlacklistedIp, BlacklistedIpSchema, SecurityMiddleware } from 'nestjs-security-cli'
|
|
17
|
+
|
|
18
|
+
@Module( {
|
|
19
|
+
imports: [
|
|
20
|
+
// 1. Set up MongoDB connection first MongooseModule.forRoot('mongodb://localhost:27017/myapp'),
|
|
21
|
+
MongooseModule.forRootAsync( {
|
|
22
|
+
imports: [ ConfigModule ],
|
|
23
|
+
useFactory: async (configService: ConfigService) => ({
|
|
24
|
+
uri: configService.get<string>( 'MONGODB_URI' )
|
|
25
|
+
}),
|
|
26
|
+
inject: [ ConfigService ]
|
|
27
|
+
} ),
|
|
28
|
+
// 2. Register the schema in your app
|
|
29
|
+
MongooseModule.forFeature( [
|
|
30
|
+
{ name: BlacklistedIp.name, schema: BlacklistedIpSchema }
|
|
31
|
+
] ),
|
|
32
|
+
// 3. Then add SecurityModule
|
|
33
|
+
SecurityModule.forRoot( {
|
|
34
|
+
enableDatabase: true,
|
|
35
|
+
defaultBlockDurationHours: 24,
|
|
36
|
+
enableAutoBlocking: true,
|
|
37
|
+
enableAdminPanel: false
|
|
38
|
+
} )
|
|
39
|
+
]
|
|
40
|
+
} )
|
|
32
41
|
export class AppModule {
|
|
33
42
|
configure(consumer: MiddlewareConsumer) {
|
|
34
43
|
// This protects ALL requests, including non-existent routes
|
|
35
|
-
consumer.apply(SecurityMiddleware).forRoutes('*')
|
|
44
|
+
consumer.apply( SecurityMiddleware ).forRoutes( '*' )
|
|
36
45
|
}
|
|
37
46
|
}
|
|
38
47
|
```
|
|
39
48
|
|
|
40
49
|
## with ConfigService
|
|
41
|
-
```
|
|
42
|
-
import { Module } from '@nestjs/common';
|
|
43
|
-
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
44
|
-
import { MongooseModule } from '@nestjs/mongoose';
|
|
45
|
-
import { SecurityModule, BlacklistedIp, BlacklistedIpSchema } from 'nestjs-security-cli';
|
|
46
|
-
@Module({ imports: [ ConfigModule.forRoot(),
|
|
47
|
-
// MongoDB connection
|
|
48
|
-
MongooseModule.forRootAsync({
|
|
49
|
-
imports: [ConfigModule],
|
|
50
|
-
useFactory: async (configService: ConfigService) => ({
|
|
51
|
-
uri: configService.get<string>('MONGODB_URI'),
|
|
52
|
-
}),
|
|
53
|
-
inject: [ConfigService],
|
|
54
|
-
}),
|
|
55
|
-
|
|
56
|
-
// Register schema
|
|
57
|
-
MongooseModule.forFeature([
|
|
58
|
-
{ name: BlacklistedIp.name, schema: BlacklistedIpSchema }
|
|
59
|
-
]),
|
|
60
|
-
|
|
61
|
-
// Security module
|
|
62
|
-
SecurityModule.forRoot({
|
|
63
|
-
enableDatabase: true,
|
|
64
|
-
defaultBlockDurationHours: 24,
|
|
65
|
-
enableAutoBlocking: true,
|
|
66
|
-
}),
|
|
67
|
-
],
|
|
68
|
-
})
|
|
69
|
-
export class AppModule {}
|
|
70
|
-
```
|
|
71
50
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
51
|
+
```typescript
|
|
52
|
+
import { Module } from '@nestjs/common'
|
|
53
|
+
import { ConfigModule, ConfigService } from '@nestjs/config'
|
|
54
|
+
import { MongooseModule } from '@nestjs/mongoose'
|
|
55
|
+
import { SecurityModule, BlacklistedIp, BlacklistedIpSchema } from 'nestjs-security-cli'
|
|
56
|
+
|
|
57
|
+
@Module( {
|
|
58
|
+
imports: [
|
|
59
|
+
ConfigModule.forRoot(),
|
|
60
|
+
// MongoDB connection
|
|
61
|
+
MongooseModule.forRootAsync( {
|
|
62
|
+
imports: [ ConfigModule ],
|
|
63
|
+
useFactory: async (configService: ConfigService) => ({
|
|
64
|
+
uri: configService.get<string>( 'MONGODB_URI' )
|
|
65
|
+
}),
|
|
66
|
+
inject: [ ConfigService ]
|
|
67
|
+
} ),
|
|
68
|
+
// Register schema
|
|
69
|
+
MongooseModule.forFeature( [
|
|
70
|
+
{ name: BlacklistedIp.name, schema: BlacklistedIpSchema }
|
|
71
|
+
] ),
|
|
72
|
+
// Security module
|
|
73
|
+
SecurityModule.forRootAsync( {
|
|
74
|
+
enableAdminPanel: true,
|
|
75
|
+
useFactory: (configService: ConfigService) => ({
|
|
76
|
+
enableDatabase: true,
|
|
77
|
+
defaultBlockDurationHours: 24,
|
|
78
|
+
enableAutoBlocking: true
|
|
79
|
+
}),
|
|
80
|
+
inject: [ ConfigService ]
|
|
81
|
+
} )
|
|
82
|
+
]
|
|
83
|
+
} )
|
|
84
|
+
export class AppModule {
|
|
85
|
+
}
|
|
83
86
|
```
|
|
84
87
|
|
|
85
88
|
## Cache-Only Mode (No Database)
|
|
@@ -87,22 +90,22 @@ SecurityModule.forRootAsync({
|
|
|
87
90
|
If you don't want to use MongoDB, you can skip the schema registration:
|
|
88
91
|
|
|
89
92
|
```typescript
|
|
90
|
-
import { Module } from '@nestjs/common'
|
|
91
|
-
import { SecurityModule } from 'nestjs-security-cli'
|
|
93
|
+
import { Module } from '@nestjs/common'
|
|
94
|
+
import { SecurityModule } from 'nestjs-security-cli'
|
|
92
95
|
|
|
93
|
-
@Module({
|
|
96
|
+
@Module( {
|
|
94
97
|
imports: [
|
|
95
|
-
SecurityModule.forRoot({
|
|
98
|
+
SecurityModule.forRoot( {
|
|
96
99
|
enableDatabase: false, // This will use only cache
|
|
97
100
|
defaultBlockDurationHours: 24,
|
|
98
|
-
enableAutoBlocking: true
|
|
99
|
-
})
|
|
100
|
-
]
|
|
101
|
-
})
|
|
102
|
-
export class AppModule {
|
|
101
|
+
enableAutoBlocking: true
|
|
102
|
+
} )
|
|
103
|
+
]
|
|
104
|
+
} )
|
|
105
|
+
export class AppModule {
|
|
106
|
+
}
|
|
103
107
|
````
|
|
104
108
|
|
|
105
|
-
|
|
106
109
|
## Clean up cron
|
|
107
110
|
|
|
108
111
|
There's a cron that runs to clean up old blocks. By default, it runs every 10 minutes. You can change this by setting
|
|
@@ -122,11 +125,43 @@ the
|
|
|
122
125
|
```
|
|
123
126
|
|
|
124
127
|
## Roles (More on this coming soon)
|
|
128
|
+
|
|
125
129
|
The roles that can be defined in your app.
|
|
130
|
+
|
|
126
131
|
```
|
|
127
132
|
export type Role = 'admin' | 'user' | 'moderator'
|
|
128
133
|
```
|
|
129
134
|
|
|
135
|
+
## Config Options
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
export interface SecurityConfigInterface {
|
|
139
|
+
enableDatabase?: boolean
|
|
140
|
+
mongooseConnection?: string
|
|
141
|
+
cache?: {
|
|
142
|
+
ttl?: number
|
|
143
|
+
max?: number
|
|
144
|
+
store?: any
|
|
145
|
+
}
|
|
146
|
+
enableAdminPanel?: boolean
|
|
147
|
+
adminPath?: string
|
|
148
|
+
enableAutoBlocking?: boolean
|
|
149
|
+
suspiciousPatterns?: Array<{
|
|
150
|
+
pattern: string
|
|
151
|
+
name: string
|
|
152
|
+
blockDurationHours?: number
|
|
153
|
+
}>
|
|
154
|
+
defaultBlockDurationHours?: number
|
|
155
|
+
enableRateLimit?: boolean
|
|
156
|
+
rateLimitOptions?: {
|
|
157
|
+
windowMs?: number
|
|
158
|
+
max?: number
|
|
159
|
+
}
|
|
160
|
+
enableLogging?: boolean
|
|
161
|
+
logLevel?: 'error' | 'warn' | 'info' | 'debug'
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
130
165
|
## Features
|
|
131
166
|
|
|
132
167
|
- 🛡️ IP Blacklisting with MongoDB persistence
|
|
@@ -141,17 +176,18 @@ export type Role = 'admin' | 'user' | 'moderator'
|
|
|
141
176
|
[need to add api docs]
|
|
142
177
|
|
|
143
178
|
## Configuration Options
|
|
144
|
-
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
145
181
|
interface SecurityConfigInterface {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
182
|
+
enableDatabase?: boolean; // Default: true
|
|
183
|
+
enableAutoBlocking?: boolean; // Default: true
|
|
184
|
+
enableAdminPanel?: boolean; // Default: false
|
|
185
|
+
defaultBlockDurationHours?: number; // Default: 24
|
|
186
|
+
enableLogging?: boolean; // Default: true
|
|
187
|
+
cache?: {
|
|
188
|
+
ttl?: number;
|
|
189
|
+
max?: number;
|
|
190
|
+
};
|
|
155
191
|
}
|
|
156
192
|
```
|
|
157
193
|
|
package/package.json
CHANGED