@cloudwerk/security 0.1.0
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/LICENSE +21 -0
- package/dist/client.d.ts +207 -0
- package/dist/client.js +145 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +424 -0
- package/dist/index.js +492 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.d.ts +3 -0
- package/dist/middleware.js +492 -0
- package/dist/middleware.js.map +1 -0
- package/dist/types-DtbmXdeT.d.ts +404 -0
- package/package.json +44 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { C as CSRFMiddlewareOptions, a as SetCsrfCookieOptions, b as SecurityHeadersOptions, c as CSPOptions, d as CSPDirectives, O as OriginValidationOptions, R as RequestedWithOptions, e as SecurityMiddlewareOptions } from './types-DtbmXdeT.js';
|
|
2
|
+
export { f as CSPDirectiveValue, g as CookieAttributes, S as SecureFetchOptions } from './types-DtbmXdeT.js';
|
|
3
|
+
import { Middleware } from '@cloudwerk/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @cloudwerk/security - CSRF Middleware
|
|
7
|
+
*
|
|
8
|
+
* Provides CSRF (Cross-Site Request Forgery) protection for mutation requests.
|
|
9
|
+
* Uses the double-submit cookie pattern for stateless CSRF protection.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Create CSRF protection middleware.
|
|
14
|
+
*
|
|
15
|
+
* Validates that mutation requests (POST, PUT, PATCH, DELETE) include a valid
|
|
16
|
+
* CSRF token that matches the token in the cookie. Uses the double-submit
|
|
17
|
+
* cookie pattern for stateless CSRF protection.
|
|
18
|
+
*
|
|
19
|
+
* The token can be provided via:
|
|
20
|
+
* 1. Request header (X-CSRF-Token by default) - for AJAX requests
|
|
21
|
+
* 2. Form field (csrf_token by default) - for traditional form submissions
|
|
22
|
+
*
|
|
23
|
+
* @param options - Middleware configuration options
|
|
24
|
+
* @returns Middleware function
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* // In middleware.ts
|
|
29
|
+
* import { csrfMiddleware } from '@cloudwerk/security/middleware'
|
|
30
|
+
*
|
|
31
|
+
* export const middleware = csrfMiddleware()
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* // Exclude webhook paths
|
|
37
|
+
* export const middleware = csrfMiddleware({
|
|
38
|
+
* excludePaths: ['/api/webhooks/stripe', '/api/webhooks/github'],
|
|
39
|
+
* })
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare function csrfMiddleware(options?: CSRFMiddlewareOptions): Middleware;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @cloudwerk/security - CSRF Token Utilities
|
|
46
|
+
*
|
|
47
|
+
* Functions for generating, setting, and verifying CSRF tokens.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/** Default CSRF cookie name */
|
|
51
|
+
declare const DEFAULT_CSRF_COOKIE_NAME = "cloudwerk.csrf-token";
|
|
52
|
+
/** Default CSRF header name */
|
|
53
|
+
declare const DEFAULT_CSRF_HEADER_NAME = "X-CSRF-Token";
|
|
54
|
+
/** Default CSRF form field name */
|
|
55
|
+
declare const DEFAULT_CSRF_FORM_FIELD_NAME = "csrf_token";
|
|
56
|
+
/**
|
|
57
|
+
* Generate a cryptographically secure CSRF token.
|
|
58
|
+
*
|
|
59
|
+
* Uses Web Crypto API for secure random number generation.
|
|
60
|
+
*
|
|
61
|
+
* @returns A URL-safe base64-encoded random token
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import { generateCsrfToken } from '@cloudwerk/security'
|
|
66
|
+
*
|
|
67
|
+
* const token = generateCsrfToken()
|
|
68
|
+
* // 'Yx8nK2pQ...' (43 characters)
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function generateCsrfToken(): string;
|
|
72
|
+
/**
|
|
73
|
+
* Set a CSRF cookie on a response.
|
|
74
|
+
*
|
|
75
|
+
* Creates a new response with the CSRF cookie set. The cookie is accessible
|
|
76
|
+
* to JavaScript (not httpOnly) so that SPA frameworks can read it and include
|
|
77
|
+
* it in request headers.
|
|
78
|
+
*
|
|
79
|
+
* @param response - The response to add the cookie to
|
|
80
|
+
* @param token - The CSRF token to set (generate with generateCsrfToken())
|
|
81
|
+
* @param options - Cookie configuration options
|
|
82
|
+
* @returns A new response with the Set-Cookie header added
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* import { generateCsrfToken, setCsrfCookie } from '@cloudwerk/security'
|
|
87
|
+
*
|
|
88
|
+
* export function GET(request: Request) {
|
|
89
|
+
* const token = generateCsrfToken()
|
|
90
|
+
* const response = new Response(JSON.stringify({ csrfToken: token }))
|
|
91
|
+
* return setCsrfCookie(response, token)
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare function setCsrfCookie(response: Response, token: string, options?: SetCsrfCookieOptions): Response;
|
|
96
|
+
/**
|
|
97
|
+
* Get the CSRF token from a request's cookie.
|
|
98
|
+
*
|
|
99
|
+
* @param request - The request to extract the token from
|
|
100
|
+
* @param cookieName - The cookie name to look for
|
|
101
|
+
* @returns The CSRF token or null if not found
|
|
102
|
+
*/
|
|
103
|
+
declare function getCsrfTokenFromCookie(request: Request, cookieName?: string): string | null;
|
|
104
|
+
/**
|
|
105
|
+
* Get the CSRF token from a request's header.
|
|
106
|
+
*
|
|
107
|
+
* @param request - The request to extract the token from
|
|
108
|
+
* @param headerName - The header name to look for
|
|
109
|
+
* @returns The CSRF token or null if not found
|
|
110
|
+
*/
|
|
111
|
+
declare function getCsrfTokenFromHeader(request: Request, headerName?: string): string | null;
|
|
112
|
+
/**
|
|
113
|
+
* Get the CSRF token from a request's form body.
|
|
114
|
+
*
|
|
115
|
+
* @param request - The request to extract the token from (will be cloned)
|
|
116
|
+
* @param fieldName - The form field name to look for
|
|
117
|
+
* @returns The CSRF token or null if not found
|
|
118
|
+
*/
|
|
119
|
+
declare function getCsrfTokenFromFormBody(request: Request, fieldName?: string): Promise<string | null>;
|
|
120
|
+
/**
|
|
121
|
+
* Verify a CSRF token against the token in the cookie.
|
|
122
|
+
*
|
|
123
|
+
* Uses timing-safe comparison to prevent timing attacks.
|
|
124
|
+
*
|
|
125
|
+
* @param cookieToken - The token from the cookie
|
|
126
|
+
* @param requestToken - The token from the request (header or form body)
|
|
127
|
+
* @returns True if tokens match
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* import { verifyCsrfToken, getCsrfTokenFromCookie, getCsrfTokenFromHeader } from '@cloudwerk/security'
|
|
132
|
+
*
|
|
133
|
+
* const cookieToken = getCsrfTokenFromCookie(request)
|
|
134
|
+
* const headerToken = getCsrfTokenFromHeader(request)
|
|
135
|
+
*
|
|
136
|
+
* if (cookieToken && headerToken && verifyCsrfToken(cookieToken, headerToken)) {
|
|
137
|
+
* // Token is valid
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
declare function verifyCsrfToken(cookieToken: string, requestToken: string): boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Rotate the CSRF token on a response.
|
|
144
|
+
*
|
|
145
|
+
* Generates a new CSRF token and sets it as a cookie. This should be called
|
|
146
|
+
* after successful authentication to bind the CSRF token to the new session
|
|
147
|
+
* and prevent session fixation attacks.
|
|
148
|
+
*
|
|
149
|
+
* @param response - The response to add the new CSRF cookie to
|
|
150
|
+
* @param options - Cookie configuration options
|
|
151
|
+
* @returns A new response with the rotated CSRF token cookie
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```typescript
|
|
155
|
+
* import { rotateCsrfToken } from '@cloudwerk/security'
|
|
156
|
+
*
|
|
157
|
+
* // After successful login
|
|
158
|
+
* export async function handleLogin(request: Request) {
|
|
159
|
+
* const user = await validateCredentials(request)
|
|
160
|
+
* const session = await createSession(user)
|
|
161
|
+
*
|
|
162
|
+
* let response = createAuthResponse(user, session)
|
|
163
|
+
* response = rotateCsrfToken(response)
|
|
164
|
+
* return response
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
declare function rotateCsrfToken(response: Response, options?: SetCsrfCookieOptions): Response;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* @cloudwerk/security - Security Headers Middleware
|
|
172
|
+
*
|
|
173
|
+
* Sets standard security headers on responses.
|
|
174
|
+
*/
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Create security headers middleware.
|
|
178
|
+
*
|
|
179
|
+
* Sets standard security headers on all responses:
|
|
180
|
+
* - X-Content-Type-Options: nosniff
|
|
181
|
+
* - X-Frame-Options: DENY
|
|
182
|
+
* - Referrer-Policy: strict-origin-when-cross-origin
|
|
183
|
+
* - X-XSS-Protection: 0 (disabled, modern browsers handle XSS)
|
|
184
|
+
* - X-Permitted-Cross-Domain-Policies: none
|
|
185
|
+
* - X-DNS-Prefetch-Control: off
|
|
186
|
+
*
|
|
187
|
+
* @param options - Header configuration options
|
|
188
|
+
* @returns Middleware function
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* import { securityHeadersMiddleware } from '@cloudwerk/security/middleware'
|
|
193
|
+
*
|
|
194
|
+
* // Use defaults
|
|
195
|
+
* export const middleware = securityHeadersMiddleware()
|
|
196
|
+
*
|
|
197
|
+
* // Custom configuration
|
|
198
|
+
* export const middleware = securityHeadersMiddleware({
|
|
199
|
+
* frameOptions: 'SAMEORIGIN',
|
|
200
|
+
* crossOriginOpenerPolicy: 'same-origin',
|
|
201
|
+
* })
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
declare function securityHeadersMiddleware(options?: SecurityHeadersOptions): Middleware;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @cloudwerk/security - Content Security Policy Middleware
|
|
208
|
+
*
|
|
209
|
+
* Generates and sets Content-Security-Policy headers.
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Generate a cryptographically secure nonce for CSP.
|
|
214
|
+
*
|
|
215
|
+
* @returns A base64-encoded random nonce
|
|
216
|
+
*/
|
|
217
|
+
declare function generateNonce(): string;
|
|
218
|
+
/**
|
|
219
|
+
* Generate a Content-Security-Policy header string from directives.
|
|
220
|
+
*
|
|
221
|
+
* @param directives - CSP directives object
|
|
222
|
+
* @param nonce - Optional nonce to include in script-src and style-src
|
|
223
|
+
* @returns CSP header string
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* const csp = generateCSPHeader({
|
|
228
|
+
* defaultSrc: ["'self'"],
|
|
229
|
+
* scriptSrc: ["'self'", 'https://cdn.example.com'],
|
|
230
|
+
* styleSrc: ["'self'", "'unsafe-inline'"],
|
|
231
|
+
* })
|
|
232
|
+
* // "default-src 'self'; script-src 'self' https://cdn.example.com; style-src 'self' 'unsafe-inline'"
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
declare function generateCSPHeader(directives: CSPDirectives, nonce?: string): string;
|
|
236
|
+
/**
|
|
237
|
+
* Create CSP middleware.
|
|
238
|
+
*
|
|
239
|
+
* Generates and sets Content-Security-Policy (or Content-Security-Policy-Report-Only)
|
|
240
|
+
* headers on responses.
|
|
241
|
+
*
|
|
242
|
+
* @param options - CSP configuration options
|
|
243
|
+
* @returns Middleware function
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```typescript
|
|
247
|
+
* import { cspMiddleware } from '@cloudwerk/security/middleware'
|
|
248
|
+
*
|
|
249
|
+
* export const middleware = cspMiddleware({
|
|
250
|
+
* directives: {
|
|
251
|
+
* defaultSrc: ["'self'"],
|
|
252
|
+
* scriptSrc: ["'self'", 'https://cdn.example.com'],
|
|
253
|
+
* styleSrc: ["'self'", "'unsafe-inline'"],
|
|
254
|
+
* imgSrc: ["'self'", 'data:', 'https:'],
|
|
255
|
+
* },
|
|
256
|
+
* })
|
|
257
|
+
* ```
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```typescript
|
|
261
|
+
* // Report-only mode for testing
|
|
262
|
+
* export const middleware = cspMiddleware({
|
|
263
|
+
* directives: {
|
|
264
|
+
* defaultSrc: ["'self'"],
|
|
265
|
+
* reportUri: '/api/csp-report',
|
|
266
|
+
* },
|
|
267
|
+
* reportOnly: true,
|
|
268
|
+
* })
|
|
269
|
+
* ```
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* // With nonce generation for inline scripts
|
|
274
|
+
* export const middleware = cspMiddleware({
|
|
275
|
+
* directives: {
|
|
276
|
+
* defaultSrc: ["'self'"],
|
|
277
|
+
* scriptSrc: ["'self'"],
|
|
278
|
+
* },
|
|
279
|
+
* useNonce: true,
|
|
280
|
+
* })
|
|
281
|
+
*
|
|
282
|
+
* // In your page, access the nonce via context:
|
|
283
|
+
* // const nonce = context.get('cspNonce')
|
|
284
|
+
* // <script nonce={nonce}>...</script>
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
declare function cspMiddleware(options?: CSPOptions): Middleware;
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* @cloudwerk/security - Origin Validation Middleware
|
|
291
|
+
*
|
|
292
|
+
* Validates Origin and Referer headers to prevent cross-origin attacks.
|
|
293
|
+
*/
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Create origin validation middleware.
|
|
297
|
+
*
|
|
298
|
+
* Validates that mutation requests (POST, PUT, PATCH, DELETE) come from
|
|
299
|
+
* allowed origins. This helps prevent CSRF attacks by rejecting requests
|
|
300
|
+
* from unknown origins.
|
|
301
|
+
*
|
|
302
|
+
* @param options - Validation options
|
|
303
|
+
* @returns Middleware function
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```typescript
|
|
307
|
+
* import { originValidationMiddleware } from '@cloudwerk/security/middleware'
|
|
308
|
+
*
|
|
309
|
+
* export const middleware = originValidationMiddleware({
|
|
310
|
+
* allowedOrigins: ['https://myapp.com', 'https://admin.myapp.com'],
|
|
311
|
+
* })
|
|
312
|
+
* ```
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```typescript
|
|
316
|
+
* // Allow all subdomains of a host
|
|
317
|
+
* export const middleware = originValidationMiddleware({
|
|
318
|
+
* allowedHosts: ['myapp.com'],
|
|
319
|
+
* allowSubdomains: true,
|
|
320
|
+
* })
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
declare function originValidationMiddleware(options?: OriginValidationOptions): Middleware;
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* @cloudwerk/security - X-Requested-With Validation Middleware
|
|
327
|
+
*
|
|
328
|
+
* Requires X-Requested-With header to force CORS preflight for cross-origin requests.
|
|
329
|
+
*/
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Create X-Requested-With validation middleware.
|
|
333
|
+
*
|
|
334
|
+
* Requires mutation requests to include an `X-Requested-With` header.
|
|
335
|
+
* This forces CORS preflight for cross-origin requests, providing an
|
|
336
|
+
* additional layer of protection against CSRF attacks.
|
|
337
|
+
*
|
|
338
|
+
* The X-Requested-With header is a custom header that cannot be set
|
|
339
|
+
* cross-origin without a CORS preflight. By requiring it, we ensure
|
|
340
|
+
* that cross-origin requests must pass CORS checks.
|
|
341
|
+
*
|
|
342
|
+
* @param options - Validation options
|
|
343
|
+
* @returns Middleware function
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* import { requestedWithMiddleware } from '@cloudwerk/security/middleware'
|
|
348
|
+
*
|
|
349
|
+
* // Default: requires X-Requested-With: XMLHttpRequest
|
|
350
|
+
* export const middleware = requestedWithMiddleware()
|
|
351
|
+
* ```
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```typescript
|
|
355
|
+
* // Custom header value
|
|
356
|
+
* export const middleware = requestedWithMiddleware({
|
|
357
|
+
* requiredValue: 'fetch',
|
|
358
|
+
* excludePaths: ['/api/webhooks'],
|
|
359
|
+
* })
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
declare function requestedWithMiddleware(options?: RequestedWithOptions): Middleware;
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* @cloudwerk/security - Combined Security Middleware
|
|
366
|
+
*
|
|
367
|
+
* All-in-one middleware that combines CSRF, security headers, CSP,
|
|
368
|
+
* origin validation, and X-Requested-With validation.
|
|
369
|
+
*/
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Create a combined security middleware.
|
|
373
|
+
*
|
|
374
|
+
* This middleware composes multiple security protections with sensible defaults:
|
|
375
|
+
* - **csrf**: Enabled by default - validates CSRF tokens on mutation requests
|
|
376
|
+
* - **requestedWith**: Enabled by default - requires X-Requested-With header
|
|
377
|
+
* - **headers**: Enabled by default - sets security headers (nosniff, DENY, etc.)
|
|
378
|
+
* - **csp**: Disabled by default - requires app-specific configuration
|
|
379
|
+
* - **origin**: Disabled by default - requires allowedOrigins configuration
|
|
380
|
+
*
|
|
381
|
+
* @param options - Configuration options for each protection
|
|
382
|
+
* @returns Middleware function
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```typescript
|
|
386
|
+
* import { securityMiddleware } from '@cloudwerk/security/middleware'
|
|
387
|
+
*
|
|
388
|
+
* // Use with all defaults
|
|
389
|
+
* export const middleware = securityMiddleware()
|
|
390
|
+
* ```
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```typescript
|
|
394
|
+
* // Full configuration
|
|
395
|
+
* export const middleware = securityMiddleware({
|
|
396
|
+
* allowedOrigins: ['https://myapp.com'],
|
|
397
|
+
* csrf: {
|
|
398
|
+
* excludePaths: ['/api/webhooks/stripe'],
|
|
399
|
+
* },
|
|
400
|
+
* csp: {
|
|
401
|
+
* directives: {
|
|
402
|
+
* defaultSrc: ["'self'"],
|
|
403
|
+
* scriptSrc: ["'self'", 'https://cdn.example.com'],
|
|
404
|
+
* },
|
|
405
|
+
* reportOnly: true,
|
|
406
|
+
* },
|
|
407
|
+
* headers: {
|
|
408
|
+
* frameOptions: 'SAMEORIGIN',
|
|
409
|
+
* },
|
|
410
|
+
* })
|
|
411
|
+
* ```
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* ```typescript
|
|
415
|
+
* // Disable specific protections
|
|
416
|
+
* export const middleware = securityMiddleware({
|
|
417
|
+
* csrf: false, // Disable CSRF
|
|
418
|
+
* requestedWith: false, // Disable X-Requested-With
|
|
419
|
+
* })
|
|
420
|
+
* ```
|
|
421
|
+
*/
|
|
422
|
+
declare function securityMiddleware(options?: SecurityMiddlewareOptions): Middleware;
|
|
423
|
+
|
|
424
|
+
export { CSPDirectives, CSPOptions, CSRFMiddlewareOptions, DEFAULT_CSRF_COOKIE_NAME, DEFAULT_CSRF_FORM_FIELD_NAME, DEFAULT_CSRF_HEADER_NAME, OriginValidationOptions, RequestedWithOptions, SecurityHeadersOptions, SecurityMiddlewareOptions, SetCsrfCookieOptions, cspMiddleware, csrfMiddleware, generateCSPHeader, generateCsrfToken, generateNonce, getCsrfTokenFromCookie, getCsrfTokenFromFormBody, getCsrfTokenFromHeader, originValidationMiddleware, requestedWithMiddleware, rotateCsrfToken, securityHeadersMiddleware, securityMiddleware, setCsrfCookie, verifyCsrfToken };
|