mulguard 1.1.1 → 1.1.3
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 +34 -0
- package/dist/core/auth/oauth-providers.d.ts +175 -47
- package/dist/core/mulguard/auth-handlers.d.ts +100 -0
- package/dist/core/mulguard/defaults.d.ts +58 -0
- package/dist/core/mulguard/index.d.ts +9 -0
- package/dist/core/mulguard/oauth-handler.d.ts +93 -0
- package/dist/core/mulguard/session-manager.d.ts +94 -0
- package/dist/core/security/index.d.ts +113 -9
- package/dist/core/security/validation.d.ts +231 -33
- package/dist/core/types/auth.d.ts +234 -75
- package/dist/core/types/errors.d.ts +174 -18
- package/dist/core/types/index.d.ts +399 -306
- package/dist/core/utils/logger.d.ts +112 -8
- package/dist/handlers/route.d.ts +59 -5
- package/dist/index/index.js +1 -1
- package/dist/index/index.mjs +1540 -1190
- package/dist/mulguard.d.ts +76 -1
- package/dist/server/helpers.d.ts +3 -3
- package/dist/server/utils.d.ts +3 -3
- package/package.json +1 -1
|
@@ -1,28 +1,132 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Security utilities
|
|
2
|
+
* Security utilities for Mulguard Authentication Library.
|
|
3
|
+
*
|
|
4
|
+
* Provides token generation, CSRF protection, input sanitization, and validation.
|
|
5
|
+
*
|
|
6
|
+
* @module @mulguard/core/security
|
|
3
7
|
*/
|
|
4
8
|
/**
|
|
5
|
-
*
|
|
9
|
+
* Generates a cryptographically secure random token.
|
|
10
|
+
*
|
|
11
|
+
* @param length - Token length in bytes (default: 32)
|
|
12
|
+
* @returns Base64url-encoded token
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const token = generateToken(32)
|
|
17
|
+
* // Returns: 'abc123xyz...' (base64url encoded)
|
|
18
|
+
* ```
|
|
6
19
|
*/
|
|
7
20
|
export declare function generateToken(length?: number): string;
|
|
8
21
|
/**
|
|
9
|
-
*
|
|
22
|
+
* Generates a CSRF token for state validation.
|
|
23
|
+
*
|
|
24
|
+
* @returns Base64url-encoded CSRF token
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const state = generateCSRFToken()
|
|
29
|
+
* // Store state for validation
|
|
30
|
+
* ```
|
|
10
31
|
*/
|
|
11
32
|
export declare function generateCSRFToken(): string;
|
|
12
33
|
/**
|
|
13
|
-
*
|
|
34
|
+
* Validates a CSRF token using constant-time comparison.
|
|
35
|
+
*
|
|
36
|
+
* Uses constant-time comparison to prevent timing attacks.
|
|
37
|
+
*
|
|
38
|
+
* @param token - Token to validate
|
|
39
|
+
* @param expected - Expected token value
|
|
40
|
+
* @returns True if tokens match
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const isValid = validateCSRFToken(receivedToken, storedToken)
|
|
45
|
+
* if (!isValid) {
|
|
46
|
+
* throw new Error('Invalid CSRF token')
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
14
49
|
*/
|
|
15
|
-
export declare function validateCSRFToken(token:
|
|
50
|
+
export declare function validateCSRFToken(token: unknown, expected: unknown): boolean;
|
|
16
51
|
/**
|
|
17
|
-
*
|
|
52
|
+
* Type predicate to check if CSRF token is valid.
|
|
53
|
+
*
|
|
54
|
+
* @param token - Token to check
|
|
55
|
+
* @param expected - Expected token
|
|
56
|
+
* @returns True if token is valid
|
|
18
57
|
*/
|
|
19
|
-
export declare function
|
|
58
|
+
export declare function isValidCSRFToken(token: unknown, expected: unknown): token is string;
|
|
20
59
|
/**
|
|
21
|
-
*
|
|
60
|
+
* Sanitizes string input by trimming and removing dangerous characters.
|
|
61
|
+
*
|
|
62
|
+
* @param input - Input to sanitize
|
|
63
|
+
* @returns Sanitized string
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const sanitized = sanitizeInput(' <script>alert("xss")</script> ')
|
|
68
|
+
* // Returns: 'scriptalert("xss")script'
|
|
69
|
+
* ```
|
|
22
70
|
*/
|
|
23
|
-
export declare function
|
|
71
|
+
export declare function sanitizeInput(input: unknown): string;
|
|
72
|
+
/**
|
|
73
|
+
* Validates email format.
|
|
74
|
+
*
|
|
75
|
+
* @param email - Email to validate
|
|
76
|
+
* @returns True if email is valid
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* if (isValidEmail('user@example.com')) {
|
|
81
|
+
* // Email is valid
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function isValidEmail(email: unknown): email is string;
|
|
24
86
|
export * from './rate-limit';
|
|
25
87
|
export * from './headers';
|
|
26
88
|
export * from './validation';
|
|
27
89
|
export * from './csrf';
|
|
28
90
|
export * from './xss';
|
|
91
|
+
/**
|
|
92
|
+
* TODO: Performance
|
|
93
|
+
* - [ ] Add token generation caching for high-frequency operations
|
|
94
|
+
* - [ ] Optimize constant-time comparison for very long tokens
|
|
95
|
+
* - [ ] Consider using Web Crypto API for token generation
|
|
96
|
+
* - [ ] Add input sanitization result caching
|
|
97
|
+
*
|
|
98
|
+
* TODO: Features
|
|
99
|
+
* - [ ] Add token expiration validation
|
|
100
|
+
* - [ ] Implement token rotation support
|
|
101
|
+
* - [ ] Add rate limiting for token generation
|
|
102
|
+
* - [ ] Create token strength scoring
|
|
103
|
+
* - [ ] Add token format validation helpers
|
|
104
|
+
*
|
|
105
|
+
* TODO: Type Safety
|
|
106
|
+
* - [ ] Add branded types for tokens
|
|
107
|
+
* - [ ] Create type-safe token validation
|
|
108
|
+
* - [ ] Add type guards for all security functions
|
|
109
|
+
* - [ ] Implement type-level security constraints
|
|
110
|
+
*
|
|
111
|
+
* TODO: Security
|
|
112
|
+
* - [ ] Add token generation logging (with masking)
|
|
113
|
+
* - [ ] Implement token blacklisting
|
|
114
|
+
* - [ ] Add security event monitoring
|
|
115
|
+
* - [ ] Create security audit logging
|
|
116
|
+
*
|
|
117
|
+
* TODO: Testing
|
|
118
|
+
* - [ ] Add comprehensive security tests
|
|
119
|
+
* - [ ] Test timing attack resistance
|
|
120
|
+
* - [ ] Test token generation randomness
|
|
121
|
+
* - [ ] Add fuzzing tests
|
|
122
|
+
*
|
|
123
|
+
* TODO: Documentation
|
|
124
|
+
* - [ ] Document security best practices
|
|
125
|
+
* - [ ] Add security considerations guide
|
|
126
|
+
* - [ ] Document token lifecycle
|
|
127
|
+
*
|
|
128
|
+
* TODO: Limitations
|
|
129
|
+
* - [ ] Token generation uses Node.js Buffer (consider Web Crypto API for browsers)
|
|
130
|
+
* - [ ] Constant-time comparison may have micro-optimizations
|
|
131
|
+
* - [ ] Email validation is basic (use validation.ts for comprehensive validation)
|
|
132
|
+
*/
|
|
@@ -1,53 +1,251 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Input validation utilities
|
|
2
|
+
* Input validation and sanitization utilities with type safety.
|
|
3
|
+
*
|
|
4
|
+
* @module @mulguard/core/security/validation
|
|
3
5
|
*/
|
|
4
6
|
/**
|
|
5
|
-
*
|
|
7
|
+
* Validation result type.
|
|
8
|
+
*
|
|
9
|
+
* @template T - Type of sanitized value
|
|
6
10
|
*/
|
|
7
|
-
export
|
|
8
|
-
valid: boolean;
|
|
9
|
-
sanitized?:
|
|
10
|
-
error?: string;
|
|
11
|
+
export interface ValidationResult<T = string> {
|
|
12
|
+
readonly valid: boolean;
|
|
13
|
+
readonly sanitized?: T;
|
|
14
|
+
readonly error?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Email validation result.
|
|
18
|
+
*/
|
|
19
|
+
export type EmailValidationResult = ValidationResult<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Password validation result with strength indicator.
|
|
22
|
+
*/
|
|
23
|
+
export interface PasswordValidationResult extends ValidationResult<string> {
|
|
24
|
+
readonly strength?: 'weak' | 'medium' | 'strong';
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Name validation result.
|
|
28
|
+
*/
|
|
29
|
+
export type NameValidationResult = ValidationResult<string>;
|
|
30
|
+
/**
|
|
31
|
+
* Token validation result.
|
|
32
|
+
*/
|
|
33
|
+
export type TokenValidationResult = ValidationResult<string>;
|
|
34
|
+
/**
|
|
35
|
+
* URL validation result.
|
|
36
|
+
*/
|
|
37
|
+
export type URLValidationResult = ValidationResult<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Input sanitization options.
|
|
40
|
+
*/
|
|
41
|
+
export interface SanitizeOptions {
|
|
42
|
+
readonly maxLength?: number;
|
|
43
|
+
readonly allowHtml?: boolean;
|
|
44
|
+
readonly required?: boolean;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Validates and sanitizes an email address.
|
|
48
|
+
*
|
|
49
|
+
* @param email - Email address to validate
|
|
50
|
+
* @returns Validation result with sanitized email if valid
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const result = validateAndSanitizeEmail(' User@Example.COM ')
|
|
55
|
+
* if (result.valid) {
|
|
56
|
+
* console.log(result.sanitized) // 'user@example.com'
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function validateAndSanitizeEmail(email: unknown): EmailValidationResult;
|
|
61
|
+
/**
|
|
62
|
+
* Type predicate to check if email validation result is valid.
|
|
63
|
+
*
|
|
64
|
+
* @param result - Validation result to check
|
|
65
|
+
* @returns True if validation is successful
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const result = validateAndSanitizeEmail(email)
|
|
70
|
+
* if (isValidEmail(result)) {
|
|
71
|
+
* // TypeScript knows result.sanitized exists
|
|
72
|
+
* console.log(result.sanitized)
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export declare function isValidEmail(result: EmailValidationResult): result is EmailValidationResult & {
|
|
77
|
+
valid: true;
|
|
78
|
+
sanitized: string;
|
|
11
79
|
};
|
|
12
80
|
/**
|
|
13
|
-
*
|
|
81
|
+
* Validates and sanitizes a password with strength assessment.
|
|
82
|
+
*
|
|
83
|
+
* @param password - Password to validate
|
|
84
|
+
* @param minLength - Minimum password length (default: 8)
|
|
85
|
+
* @returns Validation result with strength indicator if valid
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const result = validateAndSanitizePassword('MyP@ssw0rd!')
|
|
90
|
+
* if (result.valid) {
|
|
91
|
+
* console.log(result.strength) // 'strong'
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export declare function validateAndSanitizePassword(password: unknown, minLength?: number): PasswordValidationResult;
|
|
96
|
+
/**
|
|
97
|
+
* Type predicate to check if password validation result is valid.
|
|
98
|
+
*
|
|
99
|
+
* @param result - Validation result to check
|
|
100
|
+
* @returns True if validation is successful
|
|
14
101
|
*/
|
|
15
|
-
export declare function
|
|
16
|
-
valid:
|
|
17
|
-
|
|
18
|
-
strength?: 'weak' | 'medium' | 'strong';
|
|
102
|
+
export declare function isValidPassword(result: PasswordValidationResult): result is PasswordValidationResult & {
|
|
103
|
+
valid: true;
|
|
104
|
+
sanitized: string;
|
|
19
105
|
};
|
|
20
106
|
/**
|
|
21
|
-
*
|
|
107
|
+
* Validates and sanitizes a name.
|
|
108
|
+
*
|
|
109
|
+
* @param name - Name to validate
|
|
110
|
+
* @returns Validation result with sanitized name if valid
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const result = validateAndSanitizeName(' John Doe ')
|
|
115
|
+
* if (result.valid) {
|
|
116
|
+
* console.log(result.sanitized) // 'John Doe'
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
22
119
|
*/
|
|
23
|
-
export declare function validateAndSanitizeName(name:
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
120
|
+
export declare function validateAndSanitizeName(name: unknown): NameValidationResult;
|
|
121
|
+
/**
|
|
122
|
+
* Type predicate to check if name validation result is valid.
|
|
123
|
+
*
|
|
124
|
+
* @param result - Validation result to check
|
|
125
|
+
* @returns True if validation is successful
|
|
126
|
+
*/
|
|
127
|
+
export declare function isValidName(result: NameValidationResult): result is NameValidationResult & {
|
|
128
|
+
valid: true;
|
|
129
|
+
sanitized: string;
|
|
27
130
|
};
|
|
28
131
|
/**
|
|
29
|
-
*
|
|
132
|
+
* Validates a URL.
|
|
133
|
+
*
|
|
134
|
+
* @param url - URL to validate
|
|
135
|
+
* @returns Validation result
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* const result = validateURL('https://example.com')
|
|
140
|
+
* if (result.valid) {
|
|
141
|
+
* // URL is valid
|
|
142
|
+
* }
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export declare function validateURL(url: unknown): URLValidationResult;
|
|
146
|
+
/**
|
|
147
|
+
* Type predicate to check if URL validation result is valid.
|
|
148
|
+
*
|
|
149
|
+
* @param result - Validation result to check
|
|
150
|
+
* @returns True if validation is successful
|
|
30
151
|
*/
|
|
31
|
-
export declare function
|
|
32
|
-
valid:
|
|
33
|
-
|
|
152
|
+
export declare function isValidURL(result: URLValidationResult): result is URLValidationResult & {
|
|
153
|
+
valid: true;
|
|
154
|
+
sanitized: string;
|
|
34
155
|
};
|
|
35
156
|
/**
|
|
36
|
-
*
|
|
157
|
+
* Validates a token format with security checks.
|
|
158
|
+
*
|
|
159
|
+
* @param token - Token to validate
|
|
160
|
+
* @param minLength - Minimum token length (default: 16)
|
|
161
|
+
* @returns Validation result
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* const result = validateToken('abc123xyz')
|
|
166
|
+
* if (result.valid) {
|
|
167
|
+
* // Token is valid
|
|
168
|
+
* }
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export declare function validateToken(token: unknown, minLength?: number): TokenValidationResult;
|
|
172
|
+
/**
|
|
173
|
+
* Type predicate to check if token validation result is valid.
|
|
174
|
+
*
|
|
175
|
+
* @param result - Validation result to check
|
|
176
|
+
* @returns True if validation is successful
|
|
37
177
|
*/
|
|
38
|
-
export declare function
|
|
39
|
-
valid:
|
|
40
|
-
|
|
178
|
+
export declare function isValidToken(result: TokenValidationResult): result is TokenValidationResult & {
|
|
179
|
+
valid: true;
|
|
180
|
+
sanitized: string;
|
|
41
181
|
};
|
|
42
182
|
/**
|
|
43
|
-
*
|
|
183
|
+
* Validates and sanitizes generic input with XSS prevention.
|
|
184
|
+
*
|
|
185
|
+
* @param input - Input to validate and sanitize
|
|
186
|
+
* @param options - Sanitization options
|
|
187
|
+
* @returns Validation result with sanitized input if valid
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* const result = validateAndSanitizeInput('<script>alert("xss")</script>', { allowHtml: false })
|
|
192
|
+
* if (result.valid) {
|
|
193
|
+
* console.log(result.sanitized) // HTML escaped
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
44
196
|
*/
|
|
45
|
-
export declare function validateAndSanitizeInput(input:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
197
|
+
export declare function validateAndSanitizeInput(input: unknown, options?: SanitizeOptions): ValidationResult<string>;
|
|
198
|
+
/**
|
|
199
|
+
* Type predicate to check if input validation result is valid.
|
|
200
|
+
*
|
|
201
|
+
* @param result - Validation result to check
|
|
202
|
+
* @returns True if validation is successful
|
|
203
|
+
*/
|
|
204
|
+
export declare function isValidInput(result: ValidationResult<string>): result is ValidationResult<string> & {
|
|
205
|
+
valid: true;
|
|
206
|
+
sanitized: string;
|
|
53
207
|
};
|
|
208
|
+
/**
|
|
209
|
+
* TODO: Performance
|
|
210
|
+
* - [ ] Cache compiled regex patterns
|
|
211
|
+
* - [ ] Optimize password strength calculation
|
|
212
|
+
* - [ ] Add input length pre-check before full validation
|
|
213
|
+
* - [ ] Consider using Web Crypto API for token validation
|
|
214
|
+
*
|
|
215
|
+
* TODO: Features
|
|
216
|
+
* - [ ] Add internationalized email validation (IDN support)
|
|
217
|
+
* - [ ] Implement password breach checking (Have I Been Pwned API)
|
|
218
|
+
* - [ ] Add phone number validation
|
|
219
|
+
* - [ ] Create validation rule builder pattern
|
|
220
|
+
* - [ ] Add custom validation rule support
|
|
221
|
+
* - [ ] Implement validation result chaining
|
|
222
|
+
*
|
|
223
|
+
* TODO: Type Safety
|
|
224
|
+
* - [ ] Add branded types for validated inputs
|
|
225
|
+
* - [ ] Create type-level validation constraints
|
|
226
|
+
* - [ ] Implement compile-time validation rules
|
|
227
|
+
* - [ ] Add type guards for all validation results
|
|
228
|
+
*
|
|
229
|
+
* TODO: Security
|
|
230
|
+
* - [ ] Add rate limiting for validation attempts
|
|
231
|
+
* - [ ] Implement validation result caching (with TTL)
|
|
232
|
+
* - [ ] Add validation logging for security monitoring
|
|
233
|
+
* - [ ] Create validation error reporting
|
|
234
|
+
*
|
|
235
|
+
* TODO: Testing
|
|
236
|
+
* - [ ] Add comprehensive unit tests for all validators
|
|
237
|
+
* - [ ] Test edge cases (Unicode, emoji, etc.)
|
|
238
|
+
* - [ ] Test performance with large inputs
|
|
239
|
+
* - [ ] Add fuzzing tests for security
|
|
240
|
+
*
|
|
241
|
+
* TODO: Documentation
|
|
242
|
+
* - [ ] Add more JSDoc examples
|
|
243
|
+
* - [ ] Document validation rules and limits
|
|
244
|
+
* - [ ] Create validation best practices guide
|
|
245
|
+
*
|
|
246
|
+
* TODO: Limitations
|
|
247
|
+
* - [ ] Email validation is simplified (not full RFC 5322)
|
|
248
|
+
* - [ ] Password strength is basic (consider zxcvbn library)
|
|
249
|
+
* - [ ] HTML sanitization is basic (consider DOMPurify for complex cases)
|
|
250
|
+
* - [ ] No support for custom validation rules yet
|
|
251
|
+
*/
|