nestjs-security-cli 1.3.3 → 1.3.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.
- package/README.md +123 -77
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,75 +11,88 @@ 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
|
|
50
|
+
|
|
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.forRoot( {
|
|
74
|
+
enableDatabase: true,
|
|
75
|
+
defaultBlockDurationHours: 24,
|
|
76
|
+
enableAutoBlocking: true
|
|
77
|
+
} )
|
|
78
|
+
]
|
|
79
|
+
} )
|
|
80
|
+
export class AppModule {
|
|
81
|
+
}
|
|
41
82
|
```
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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({
|
|
83
|
+
|
|
84
|
+
## With forRootAsync
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
SecurityModule.forRootAsync( {
|
|
88
|
+
enableAdminPanel: true,
|
|
89
|
+
useFactory: (configService: ConfigService) => ({
|
|
63
90
|
enableDatabase: true,
|
|
64
91
|
defaultBlockDurationHours: 24,
|
|
65
|
-
enableAutoBlocking: true
|
|
92
|
+
enableAutoBlocking: true
|
|
66
93
|
}),
|
|
67
|
-
]
|
|
68
|
-
})
|
|
69
|
-
export class AppModule {}
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
## With forRootAsync
|
|
73
|
-
```
|
|
74
|
-
SecurityModule.forRootAsync({
|
|
75
|
-
enableAdminPanel: true,
|
|
76
|
-
useFactory: (configService: ConfigService) => ({
|
|
77
|
-
enableDatabase: true,
|
|
78
|
-
defaultBlockDurationHours: 24,
|
|
79
|
-
enableAutoBlocking: true,
|
|
80
|
-
}),
|
|
81
|
-
inject: [ConfigService]
|
|
82
|
-
})
|
|
94
|
+
inject: [ ConfigService ]
|
|
95
|
+
} )
|
|
83
96
|
```
|
|
84
97
|
|
|
85
98
|
## Cache-Only Mode (No Database)
|
|
@@ -87,22 +100,22 @@ SecurityModule.forRootAsync({
|
|
|
87
100
|
If you don't want to use MongoDB, you can skip the schema registration:
|
|
88
101
|
|
|
89
102
|
```typescript
|
|
90
|
-
import { Module } from '@nestjs/common'
|
|
91
|
-
import { SecurityModule } from 'nestjs-security-cli'
|
|
103
|
+
import { Module } from '@nestjs/common'
|
|
104
|
+
import { SecurityModule } from 'nestjs-security-cli'
|
|
92
105
|
|
|
93
|
-
@Module({
|
|
106
|
+
@Module( {
|
|
94
107
|
imports: [
|
|
95
|
-
SecurityModule.forRoot({
|
|
108
|
+
SecurityModule.forRoot( {
|
|
96
109
|
enableDatabase: false, // This will use only cache
|
|
97
110
|
defaultBlockDurationHours: 24,
|
|
98
|
-
enableAutoBlocking: true
|
|
99
|
-
})
|
|
100
|
-
]
|
|
101
|
-
})
|
|
102
|
-
export class AppModule {
|
|
111
|
+
enableAutoBlocking: true
|
|
112
|
+
} )
|
|
113
|
+
]
|
|
114
|
+
} )
|
|
115
|
+
export class AppModule {
|
|
116
|
+
}
|
|
103
117
|
````
|
|
104
118
|
|
|
105
|
-
|
|
106
119
|
## Clean up cron
|
|
107
120
|
|
|
108
121
|
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 +135,43 @@ the
|
|
|
122
135
|
```
|
|
123
136
|
|
|
124
137
|
## Roles (More on this coming soon)
|
|
138
|
+
|
|
125
139
|
The roles that can be defined in your app.
|
|
140
|
+
|
|
126
141
|
```
|
|
127
142
|
export type Role = 'admin' | 'user' | 'moderator'
|
|
128
143
|
```
|
|
129
144
|
|
|
145
|
+
## Config Options
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
export interface SecurityConfigInterface {
|
|
149
|
+
enableDatabase?: boolean
|
|
150
|
+
mongooseConnection?: string
|
|
151
|
+
cache?: {
|
|
152
|
+
ttl?: number
|
|
153
|
+
max?: number
|
|
154
|
+
store?: any
|
|
155
|
+
}
|
|
156
|
+
enableAdminPanel?: boolean
|
|
157
|
+
adminPath?: string
|
|
158
|
+
enableAutoBlocking?: boolean
|
|
159
|
+
suspiciousPatterns?: Array<{
|
|
160
|
+
pattern: string
|
|
161
|
+
name: string
|
|
162
|
+
blockDurationHours?: number
|
|
163
|
+
}>
|
|
164
|
+
defaultBlockDurationHours?: number
|
|
165
|
+
enableRateLimit?: boolean
|
|
166
|
+
rateLimitOptions?: {
|
|
167
|
+
windowMs?: number
|
|
168
|
+
max?: number
|
|
169
|
+
}
|
|
170
|
+
enableLogging?: boolean
|
|
171
|
+
logLevel?: 'error' | 'warn' | 'info' | 'debug'
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
130
175
|
## Features
|
|
131
176
|
|
|
132
177
|
- 🛡️ IP Blacklisting with MongoDB persistence
|
|
@@ -141,17 +186,18 @@ export type Role = 'admin' | 'user' | 'moderator'
|
|
|
141
186
|
[need to add api docs]
|
|
142
187
|
|
|
143
188
|
## Configuration Options
|
|
144
|
-
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
145
191
|
interface SecurityConfigInterface {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
192
|
+
enableDatabase?: boolean; // Default: true
|
|
193
|
+
enableAutoBlocking?: boolean; // Default: true
|
|
194
|
+
enableAdminPanel?: boolean; // Default: false
|
|
195
|
+
defaultBlockDurationHours?: number; // Default: 24
|
|
196
|
+
enableLogging?: boolean; // Default: true
|
|
197
|
+
cache?: {
|
|
198
|
+
ttl?: number;
|
|
199
|
+
max?: number;
|
|
200
|
+
};
|
|
155
201
|
}
|
|
156
202
|
```
|
|
157
203
|
|
package/package.json
CHANGED