@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.
@@ -0,0 +1,404 @@
1
+ /**
2
+ * @cloudwerk/security - Type Definitions
3
+ *
4
+ * Types for security middleware configuration and options.
5
+ */
6
+ /**
7
+ * Cookie attributes for serialization.
8
+ */
9
+ interface CookieAttributes {
10
+ /** Cookie domain */
11
+ domain?: string;
12
+ /** Cookie path */
13
+ path?: string;
14
+ /** Expiration date */
15
+ expires?: Date;
16
+ /** Max age in seconds */
17
+ maxAge?: number;
18
+ /** HTTP-only flag */
19
+ httpOnly?: boolean;
20
+ /** Secure flag */
21
+ secure?: boolean;
22
+ /** SameSite attribute */
23
+ sameSite?: 'strict' | 'lax' | 'none';
24
+ }
25
+ /**
26
+ * Options for the CSRF protection middleware.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * import { csrfMiddleware } from '@cloudwerk/security/middleware'
31
+ *
32
+ * // Default configuration
33
+ * export const middleware = csrfMiddleware()
34
+ *
35
+ * // Custom configuration
36
+ * export const middleware = csrfMiddleware({
37
+ * excludePaths: ['/api/webhooks'],
38
+ * methods: ['POST', 'DELETE'],
39
+ * })
40
+ * ```
41
+ */
42
+ interface CSRFMiddlewareOptions {
43
+ /**
44
+ * Name of the CSRF cookie.
45
+ * @default 'cloudwerk.csrf-token'
46
+ */
47
+ cookieName?: string;
48
+ /**
49
+ * Name of the header that must contain the CSRF token.
50
+ * @default 'X-CSRF-Token'
51
+ */
52
+ headerName?: string;
53
+ /**
54
+ * Name of the form field that can contain the CSRF token.
55
+ * Used as an alternative to the header for traditional form submissions.
56
+ * @default 'csrf_token'
57
+ */
58
+ formFieldName?: string;
59
+ /**
60
+ * HTTP methods that require CSRF validation.
61
+ * Safe methods (GET, HEAD, OPTIONS) are always excluded.
62
+ * @default ['POST', 'PUT', 'PATCH', 'DELETE']
63
+ */
64
+ methods?: string[];
65
+ /**
66
+ * URL path patterns to exclude from CSRF protection.
67
+ * Useful for webhook endpoints that receive external requests.
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * excludePaths: ['/api/webhooks/stripe', '/api/webhooks/github']
72
+ * ```
73
+ */
74
+ excludePaths?: string[];
75
+ }
76
+ /**
77
+ * Options for setting the CSRF cookie.
78
+ */
79
+ interface SetCsrfCookieOptions {
80
+ /**
81
+ * Name of the CSRF cookie.
82
+ * @default 'cloudwerk.csrf-token'
83
+ */
84
+ cookieName?: string;
85
+ /**
86
+ * Cookie path.
87
+ * @default '/'
88
+ */
89
+ path?: string;
90
+ /**
91
+ * Whether the cookie is HTTP-only.
92
+ * Must be false to allow JavaScript access for SPA frameworks.
93
+ * @default false
94
+ */
95
+ httpOnly?: boolean;
96
+ /**
97
+ * Whether the cookie requires HTTPS.
98
+ * @default true
99
+ */
100
+ secure?: boolean;
101
+ /**
102
+ * SameSite attribute.
103
+ * @default 'lax'
104
+ */
105
+ sameSite?: 'strict' | 'lax' | 'none';
106
+ /**
107
+ * Max age in seconds.
108
+ * @default 86400 (24 hours)
109
+ */
110
+ maxAge?: number;
111
+ }
112
+ /**
113
+ * Options for the security headers middleware.
114
+ */
115
+ interface SecurityHeadersOptions {
116
+ /**
117
+ * X-Content-Type-Options header value.
118
+ * Set to false to disable.
119
+ * @default 'nosniff'
120
+ */
121
+ contentTypeOptions?: 'nosniff' | false;
122
+ /**
123
+ * X-Frame-Options header value.
124
+ * Set to false to disable.
125
+ * @default 'DENY'
126
+ */
127
+ frameOptions?: 'DENY' | 'SAMEORIGIN' | false;
128
+ /**
129
+ * Referrer-Policy header value.
130
+ * Set to false to disable.
131
+ * @default 'strict-origin-when-cross-origin'
132
+ */
133
+ referrerPolicy?: 'no-referrer' | 'no-referrer-when-downgrade' | 'origin' | 'origin-when-cross-origin' | 'same-origin' | 'strict-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url' | false;
134
+ /**
135
+ * X-XSS-Protection header value.
136
+ * Set to false to disable.
137
+ * Note: This header is deprecated in modern browsers.
138
+ * @default '0' (disabled, as modern browsers handle XSS protection)
139
+ */
140
+ xssProtection?: '0' | '1' | '1; mode=block' | false;
141
+ /**
142
+ * X-Permitted-Cross-Domain-Policies header value.
143
+ * Set to false to disable.
144
+ * @default 'none'
145
+ */
146
+ permittedCrossDomainPolicies?: 'none' | 'master-only' | 'by-content-type' | 'by-ftp-filename' | 'all' | false;
147
+ /**
148
+ * X-DNS-Prefetch-Control header value.
149
+ * Set to false to disable.
150
+ * @default 'off'
151
+ */
152
+ dnsPrefetchControl?: 'on' | 'off' | false;
153
+ /**
154
+ * Cross-Origin-Opener-Policy header value.
155
+ * Set to false to disable.
156
+ */
157
+ crossOriginOpenerPolicy?: 'same-origin' | 'same-origin-allow-popups' | 'unsafe-none' | false;
158
+ /**
159
+ * Cross-Origin-Embedder-Policy header value.
160
+ * Set to false to disable.
161
+ */
162
+ crossOriginEmbedderPolicy?: 'require-corp' | 'credentialless' | 'unsafe-none' | false;
163
+ /**
164
+ * Cross-Origin-Resource-Policy header value.
165
+ * Set to false to disable.
166
+ */
167
+ crossOriginResourcePolicy?: 'same-site' | 'same-origin' | 'cross-origin' | false;
168
+ }
169
+ /**
170
+ * CSP directive values - can be strings or arrays of strings.
171
+ */
172
+ type CSPDirectiveValue = string | string[];
173
+ /**
174
+ * Content Security Policy directives.
175
+ */
176
+ interface CSPDirectives {
177
+ /** Fallback for other fetch directives */
178
+ defaultSrc?: CSPDirectiveValue;
179
+ /** Valid sources for scripts */
180
+ scriptSrc?: CSPDirectiveValue;
181
+ /** Valid sources for stylesheets */
182
+ styleSrc?: CSPDirectiveValue;
183
+ /** Valid sources for images */
184
+ imgSrc?: CSPDirectiveValue;
185
+ /** Valid sources for fonts */
186
+ fontSrc?: CSPDirectiveValue;
187
+ /** Valid sources for fetch, XMLHttpRequest, WebSocket */
188
+ connectSrc?: CSPDirectiveValue;
189
+ /** Valid sources for media (audio/video) */
190
+ mediaSrc?: CSPDirectiveValue;
191
+ /** Valid sources for <object>, <embed>, <applet> */
192
+ objectSrc?: CSPDirectiveValue;
193
+ /** Valid sources for prefetch, prerender */
194
+ prefetchSrc?: CSPDirectiveValue;
195
+ /** Restricts which URLs can appear in <frame>, <iframe>, etc. */
196
+ frameSrc?: CSPDirectiveValue;
197
+ /** Valid sources for Worker, SharedWorker, ServiceWorker */
198
+ workerSrc?: CSPDirectiveValue;
199
+ /** Valid sources for nested browsing contexts */
200
+ childSrc?: CSPDirectiveValue;
201
+ /** Restricts URLs that can be loaded in a frame */
202
+ frameAncestors?: CSPDirectiveValue;
203
+ /** Restricts URLs for form submissions */
204
+ formAction?: CSPDirectiveValue;
205
+ /** Valid MIME types for plugins */
206
+ pluginTypes?: CSPDirectiveValue;
207
+ /** Restricts the URLs which can be used as the target of form submissions */
208
+ baseUri?: CSPDirectiveValue;
209
+ /** Enables a sandbox for the requested resource */
210
+ sandbox?: CSPDirectiveValue;
211
+ /** Require SRI for scripts/styles */
212
+ requireSriFor?: CSPDirectiveValue;
213
+ /** URI to report CSP violations to */
214
+ reportUri?: string;
215
+ /** Group name for Reporting API */
216
+ reportTo?: string;
217
+ /** Require Trusted Types for DOM XSS sinks */
218
+ requireTrustedTypesFor?: CSPDirectiveValue;
219
+ /** Trusted Types policy name */
220
+ trustedTypes?: CSPDirectiveValue;
221
+ /** Upgrade insecure requests to HTTPS */
222
+ upgradeInsecureRequests?: boolean;
223
+ /** Block all mixed content */
224
+ blockAllMixedContent?: boolean;
225
+ /** Valid sources for manifests */
226
+ manifestSrc?: CSPDirectiveValue;
227
+ /** Valid sources for script elements */
228
+ scriptSrcElem?: CSPDirectiveValue;
229
+ /** Valid sources for inline script event handlers */
230
+ scriptSrcAttr?: CSPDirectiveValue;
231
+ /** Valid sources for style elements */
232
+ styleSrcElem?: CSPDirectiveValue;
233
+ /** Valid sources for inline style attributes */
234
+ styleSrcAttr?: CSPDirectiveValue;
235
+ }
236
+ /**
237
+ * Options for the CSP middleware.
238
+ */
239
+ interface CSPOptions {
240
+ /**
241
+ * CSP directives to apply.
242
+ */
243
+ directives?: CSPDirectives;
244
+ /**
245
+ * Whether to use Content-Security-Policy-Report-Only header instead.
246
+ * Useful for testing CSP rules without blocking content.
247
+ * @default false
248
+ */
249
+ reportOnly?: boolean;
250
+ /**
251
+ * Generate a unique nonce for inline scripts and add it to the CSP.
252
+ * The nonce value is stored in context for use in pages.
253
+ * @default false
254
+ */
255
+ useNonce?: boolean;
256
+ /**
257
+ * Context key to store the CSP nonce value.
258
+ * @default 'cspNonce'
259
+ */
260
+ nonceContextKey?: string;
261
+ }
262
+ /**
263
+ * Options for origin validation middleware.
264
+ */
265
+ interface OriginValidationOptions {
266
+ /**
267
+ * Allowed origins (full URLs like 'https://example.com').
268
+ * If not specified, origin validation is skipped.
269
+ */
270
+ allowedOrigins?: string[];
271
+ /**
272
+ * Allowed hosts (just hostnames like 'example.com').
273
+ * Origins with these hosts are allowed regardless of protocol.
274
+ */
275
+ allowedHosts?: string[];
276
+ /**
277
+ * Allow subdomains of allowed hosts.
278
+ * If true, 'example.com' allows 'sub.example.com'.
279
+ * @default false
280
+ */
281
+ allowSubdomains?: boolean;
282
+ /**
283
+ * HTTP methods that require origin validation.
284
+ * @default ['POST', 'PUT', 'PATCH', 'DELETE']
285
+ */
286
+ methods?: string[];
287
+ /**
288
+ * URL paths to exclude from origin validation.
289
+ */
290
+ excludePaths?: string[];
291
+ /**
292
+ * Whether to reject requests without an Origin header for mutation methods.
293
+ * @default true
294
+ */
295
+ rejectMissingOrigin?: boolean;
296
+ }
297
+ /**
298
+ * Options for X-Requested-With validation middleware.
299
+ */
300
+ interface RequestedWithOptions {
301
+ /**
302
+ * Required header value.
303
+ * @default 'XMLHttpRequest'
304
+ */
305
+ requiredValue?: string;
306
+ /**
307
+ * HTTP methods that require X-Requested-With validation.
308
+ * @default ['POST', 'PUT', 'PATCH', 'DELETE']
309
+ */
310
+ methods?: string[];
311
+ /**
312
+ * URL paths to exclude from validation.
313
+ */
314
+ excludePaths?: string[];
315
+ }
316
+ /**
317
+ * Options for the combined security middleware.
318
+ *
319
+ * @example
320
+ * ```typescript
321
+ * import { securityMiddleware } from '@cloudwerk/security/middleware'
322
+ *
323
+ * export const middleware = securityMiddleware({
324
+ * allowedOrigins: ['https://myapp.com'],
325
+ * csrf: {
326
+ * excludePaths: ['/api/webhooks/stripe'],
327
+ * },
328
+ * csp: {
329
+ * directives: {
330
+ * defaultSrc: ["'self'"],
331
+ * scriptSrc: ["'self'", 'https://cdn.example.com'],
332
+ * },
333
+ * },
334
+ * })
335
+ * ```
336
+ */
337
+ interface SecurityMiddlewareOptions {
338
+ /**
339
+ * CSRF protection options.
340
+ * Set to false to disable CSRF protection.
341
+ * @default enabled
342
+ */
343
+ csrf?: CSRFMiddlewareOptions | false;
344
+ /**
345
+ * X-Requested-With validation options.
346
+ * Set to false to disable.
347
+ * @default enabled
348
+ */
349
+ requestedWith?: RequestedWithOptions | false;
350
+ /**
351
+ * Security headers options.
352
+ * Set to false to disable.
353
+ * @default enabled
354
+ */
355
+ headers?: SecurityHeadersOptions | false;
356
+ /**
357
+ * CSP options.
358
+ * Set to false to disable.
359
+ * @default disabled (requires app-specific config)
360
+ */
361
+ csp?: CSPOptions | false;
362
+ /**
363
+ * Origin validation options.
364
+ * Set to false to disable.
365
+ * @default disabled (requires allowedOrigins config)
366
+ */
367
+ origin?: OriginValidationOptions | false;
368
+ /**
369
+ * Shorthand for origin validation - list of allowed origins.
370
+ * Equivalent to setting origin.allowedOrigins.
371
+ */
372
+ allowedOrigins?: string[];
373
+ }
374
+ /**
375
+ * Options for configuring secure fetch defaults.
376
+ */
377
+ interface SecureFetchOptions {
378
+ /**
379
+ * Name of the CSRF cookie to read.
380
+ * @default 'cloudwerk.csrf-token'
381
+ */
382
+ csrfCookieName?: string;
383
+ /**
384
+ * Name of the CSRF header to send.
385
+ * @default 'X-CSRF-Token'
386
+ */
387
+ csrfHeaderName?: string;
388
+ /**
389
+ * Value for the X-Requested-With header.
390
+ * @default 'XMLHttpRequest'
391
+ */
392
+ requestedWithValue?: string;
393
+ /**
394
+ * Whether to include credentials.
395
+ * @default 'same-origin'
396
+ */
397
+ credentials?: RequestCredentials;
398
+ /**
399
+ * Base URL to prepend to relative URLs.
400
+ */
401
+ baseUrl?: string;
402
+ }
403
+
404
+ export type { CSRFMiddlewareOptions as C, OriginValidationOptions as O, RequestedWithOptions as R, SecureFetchOptions as S, SetCsrfCookieOptions as a, SecurityHeadersOptions as b, CSPOptions as c, CSPDirectives as d, SecurityMiddlewareOptions as e, CSPDirectiveValue as f, CookieAttributes as g };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@cloudwerk/security",
3
+ "version": "0.1.0",
4
+ "description": "Security middleware for Cloudwerk - CSRF, CSP, security headers, origin validation",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/squirrelsoft-dev/cloudwerk.git",
8
+ "directory": "packages/security"
9
+ },
10
+ "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js"
15
+ },
16
+ "./middleware": {
17
+ "types": "./dist/middleware.d.ts",
18
+ "import": "./dist/middleware.js"
19
+ },
20
+ "./client": {
21
+ "types": "./dist/client.d.ts",
22
+ "import": "./dist/client.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "dependencies": {
29
+ "@cloudwerk/core": "0.15.1"
30
+ },
31
+ "devDependencies": {
32
+ "typescript": "^5.4.0",
33
+ "vitest": "^1.0.0",
34
+ "tsup": "^8.0.0"
35
+ },
36
+ "peerDependencies": {
37
+ "typescript": "^5.0.0"
38
+ },
39
+ "scripts": {
40
+ "build": "tsup",
41
+ "test": "vitest --run",
42
+ "test:watch": "vitest"
43
+ }
44
+ }