wyrm-mcp 7.2.1 → 7.2.2

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.
Files changed (150) hide show
  1. package/LICENSE +26 -667
  2. package/NOTICE +14 -33
  3. package/dist/activation.js +1 -60
  4. package/dist/agent-daemon.js +4 -281
  5. package/dist/agent-loop.js +7 -332
  6. package/dist/analytics.js +13 -236
  7. package/dist/attribution.js +1 -49
  8. package/dist/audit.js +2 -457
  9. package/dist/auto-capture.js +3 -138
  10. package/dist/auto-orchestrator.js +1 -325
  11. package/dist/autoconfig.js +39 -840
  12. package/dist/buddy-runner.js +1 -109
  13. package/dist/buddy.js +14 -564
  14. package/dist/build-flags.js +1 -17
  15. package/dist/capabilities.js +3 -183
  16. package/dist/capture.js +1 -56
  17. package/dist/causality.js +6 -107
  18. package/dist/cli.js +20 -281
  19. package/dist/cloud/cli.js +5 -541
  20. package/dist/cloud/client.js +1 -221
  21. package/dist/cloud/crypto.js +1 -85
  22. package/dist/cloud/machine-id.js +2 -113
  23. package/dist/cloud/recovery.js +1 -60
  24. package/dist/cloud/sync-engine.js +7 -543
  25. package/dist/cloud-backup.js +5 -579
  26. package/dist/cloud-profile.js +1 -138
  27. package/dist/cloud-sync-entrypoint.js +1 -47
  28. package/dist/cloud-sync.js +2 -309
  29. package/dist/constellation.js +12 -168
  30. package/dist/context-build-budgeted.js +4 -144
  31. package/dist/context-ranking.js +1 -69
  32. package/dist/crypto.js +1 -179
  33. package/dist/daemon-write-endpoint.js +1 -290
  34. package/dist/daemon-writer.js +2 -406
  35. package/dist/database.js +43 -1110
  36. package/dist/deprecations.js +2 -162
  37. package/dist/design.js +13 -141
  38. package/dist/event-replication.js +1 -112
  39. package/dist/events-sse.js +7 -43
  40. package/dist/events.js +6 -238
  41. package/dist/failure-patterns.js +42 -659
  42. package/dist/federation.js +12 -236
  43. package/dist/goals.js +13 -101
  44. package/dist/golden.js +3 -355
  45. package/dist/handlers/agent.js +4 -165
  46. package/dist/handlers/alias-adapters.js +1 -129
  47. package/dist/handlers/aliases.js +1 -171
  48. package/dist/handlers/audit.js +1 -87
  49. package/dist/handlers/boundary.js +1 -221
  50. package/dist/handlers/capture.js +73 -1109
  51. package/dist/handlers/causality.js +7 -114
  52. package/dist/handlers/cloud.js +85 -382
  53. package/dist/handlers/companion.js +28 -459
  54. package/dist/handlers/datalake.js +7 -187
  55. package/dist/handlers/dispatch-context.js +0 -22
  56. package/dist/handlers/entity.js +25 -256
  57. package/dist/handlers/events.js +16 -335
  58. package/dist/handlers/failure.js +13 -340
  59. package/dist/handlers/goals.js +4 -296
  60. package/dist/handlers/intelligence.js +126 -674
  61. package/dist/handlers/invoicing.js +1 -70
  62. package/dist/handlers/mcpclient.js +6 -137
  63. package/dist/handlers/orchestration.js +40 -125
  64. package/dist/handlers/output-schemas.js +1 -24
  65. package/dist/handlers/presence.js +3 -99
  66. package/dist/handlers/project.js +28 -182
  67. package/dist/handlers/prompts.js +6 -157
  68. package/dist/handlers/quest.js +4 -224
  69. package/dist/handlers/recall.js +11 -218
  70. package/dist/handlers/registry.js +1 -167
  71. package/dist/handlers/resources.js +1 -288
  72. package/dist/handlers/review.js +11 -74
  73. package/dist/handlers/run.js +17 -487
  74. package/dist/handlers/search.js +15 -326
  75. package/dist/handlers/session.js +28 -615
  76. package/dist/handlers/share.js +8 -184
  77. package/dist/handlers/shims.js +1 -464
  78. package/dist/handlers/skill.js +67 -449
  79. package/dist/handlers/survivors.js +1 -120
  80. package/dist/handlers/symbols.js +8 -109
  81. package/dist/handlers/syncops.js +4 -302
  82. package/dist/handlers/types.js +1 -27
  83. package/dist/harvest.js +5 -191
  84. package/dist/hours.js +7 -156
  85. package/dist/http-auth.js +3 -321
  86. package/dist/http-fast.js +21 -1137
  87. package/dist/icons.js +1 -47
  88. package/dist/index.js +2 -924
  89. package/dist/indexer.js +4 -145
  90. package/dist/intelligence.js +31 -261
  91. package/dist/internal-dispatch.js +3 -212
  92. package/dist/keyset.js +1 -110
  93. package/dist/knowledge-graph.js +12 -176
  94. package/dist/license.js +2 -441
  95. package/dist/logger.js +2 -199
  96. package/dist/maintenance.js +2 -148
  97. package/dist/mcp-client.js +6 -262
  98. package/dist/memory-artifacts.js +30 -449
  99. package/dist/migrate-prompt.js +2 -124
  100. package/dist/migrations.js +40 -655
  101. package/dist/performance.js +1 -228
  102. package/dist/presence.js +11 -140
  103. package/dist/priority-embed.js +5 -164
  104. package/dist/providers/embedding-provider.js +1 -196
  105. package/dist/readonly-gate.js +1 -29
  106. package/dist/rehydration.js +9 -157
  107. package/dist/reindex.js +1 -88
  108. package/dist/render-target.js +21 -514
  109. package/dist/render.js +4 -280
  110. package/dist/repl-guard.js +1 -173
  111. package/dist/replication-daemon-entrypoint.js +1 -31
  112. package/dist/replication-daemon.js +2 -262
  113. package/dist/resilience.js +1 -591
  114. package/dist/reverse-bridge.js +5 -360
  115. package/dist/security.js +1 -244
  116. package/dist/session-seen.js +3 -51
  117. package/dist/setup.js +1 -260
  118. package/dist/skill-author.js +5 -168
  119. package/dist/spec-kit.js +1 -191
  120. package/dist/sqlite-busy.js +1 -154
  121. package/dist/statusline.js +11 -315
  122. package/dist/sub-agent.js +13 -262
  123. package/dist/summarizer.js +13 -139
  124. package/dist/symbols.js +7 -283
  125. package/dist/sync.js +5 -359
  126. package/dist/tasks-dispatch.js +1 -84
  127. package/dist/tasks.js +1 -282
  128. package/dist/token-budget.js +1 -143
  129. package/dist/tool-analytics.js +7 -129
  130. package/dist/tool-annotations.js +1 -365
  131. package/dist/tool-manifest-v2.json +1 -1
  132. package/dist/tool-manifest.json +1 -1
  133. package/dist/tool-profiles.js +1 -75
  134. package/dist/trace-harvest.js +6 -244
  135. package/dist/types.js +1 -30
  136. package/dist/ui-dashboard.js +41 -50
  137. package/dist/ulid.js +1 -81
  138. package/dist/validate.js +1 -129
  139. package/dist/vault.js +1 -534
  140. package/dist/vectors.js +3 -184
  141. package/dist/version-check.js +4 -136
  142. package/dist/visibility.js +19 -155
  143. package/dist/wyrm-cli.js +98 -2464
  144. package/dist/wyrm-guard.js +14 -424
  145. package/dist/wyrm-loop.js +3 -150
  146. package/dist/wyrm-manifest.json +1 -1
  147. package/dist/wyrm-statusline-daemon.js +1 -11
  148. package/dist/wyrm-statusline.js +4 -56
  149. package/dist/wyrm-ui.js +9 -77
  150. package/package.json +4 -2
package/dist/http-auth.js CHANGED
@@ -1,321 +1,3 @@
1
- /**
2
- * Wyrm HTTP Authentication - Secure API access control
3
- *
4
- * @copyright 2026 Ghost Protocol (Pvt) Ltd.
5
- * @license AGPL-3.0-or-later — dual-licensed; commercial terms: ghosts.lk@proton.me. See LICENSE.
6
- * @module http-auth
7
- * @version 3.0.0
8
- */
9
- import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
10
- import { join } from 'path';
11
- import { homedir } from 'os';
12
- import { randomBytes, createHash } from 'crypto';
13
- import { WyrmLogger } from './logger.js';
14
- // ==================== CONSTANTS ====================
15
- const CONFIG_DIR = join(homedir(), '.wyrm');
16
- const CONFIG_PATH = join(CONFIG_DIR, 'http-config.json');
17
- const DEFAULT_RATE_LIMIT = { enabled: true, requests: 100, windowMs: 60000 };
18
- const DEFAULT_ORIGINS = ['http://localhost:3333', 'http://127.0.0.1:3333'];
19
- // ==================== STATE ====================
20
- const rateLimitStore = new Map();
21
- let config = null;
22
- const logger = new WyrmLogger();
23
- // ==================== HELPER FUNCTIONS ====================
24
- /**
25
- * Hash a token for secure storage
26
- */
27
- function hashToken(token) {
28
- return createHash('sha256').update(token).digest('hex');
29
- }
30
- /**
31
- * Constant-time string comparison to prevent timing attacks
32
- */
33
- function constantTimeCompare(a, b) {
34
- if (a.length !== b.length) {
35
- return false;
36
- }
37
- let result = 0;
38
- for (let i = 0; i < a.length; i++) {
39
- result |= a.charCodeAt(i) ^ b.charCodeAt(i);
40
- }
41
- return result === 0;
42
- }
43
- /**
44
- * Load or create authentication configuration
45
- */
46
- function loadOrCreateConfig() {
47
- // Ensure config directory exists
48
- if (!existsSync(CONFIG_DIR)) {
49
- mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
50
- }
51
- // Load existing config
52
- if (existsSync(CONFIG_PATH)) {
53
- try {
54
- const data = JSON.parse(readFileSync(CONFIG_PATH, 'utf-8'));
55
- return {
56
- apiKeyHash: data.apiKeyHash,
57
- allowedOrigins: data.allowedOrigins || DEFAULT_ORIGINS,
58
- rateLimit: { ...DEFAULT_RATE_LIMIT, ...data.rateLimit },
59
- requireAuth: data.requireAuth ?? true,
60
- devMode: process.env.WYRM_DEV === 'true',
61
- };
62
- }
63
- catch (error) {
64
- logger.error('Failed to load HTTP config, regenerating', { error: error.message });
65
- }
66
- }
67
- // Generate new API key
68
- const apiKey = randomBytes(32).toString('hex');
69
- logger.info('Generated new API key for HTTP server');
70
- console.log('\n' + '═'.repeat(60));
71
- console.log('🔐 WYRM API KEY (save this securely, shown once):');
72
- console.log('');
73
- console.log(` ${apiKey}`);
74
- console.log('');
75
- console.log(' Use with: Authorization: Bearer <key>');
76
- console.log('═'.repeat(60) + '\n');
77
- const newConfig = {
78
- apiKeyHash: hashToken(apiKey),
79
- allowedOrigins: DEFAULT_ORIGINS,
80
- rateLimit: DEFAULT_RATE_LIMIT,
81
- requireAuth: true,
82
- devMode: process.env.WYRM_DEV === 'true',
83
- };
84
- // Save config with restrictive permissions
85
- writeFileSync(CONFIG_PATH, JSON.stringify(newConfig, null, 2), { mode: 0o600 });
86
- return newConfig;
87
- }
88
- /**
89
- * Get configuration (lazy load)
90
- */
91
- function getConfig() {
92
- if (!config) {
93
- config = loadOrCreateConfig();
94
- }
95
- return config;
96
- }
97
- // ==================== AUTHENTICATION ====================
98
- /**
99
- * Validate API key from Authorization header
100
- */
101
- export function authenticate(req) {
102
- const cfg = getConfig();
103
- // Dev mode: allow local connections without auth
104
- if (cfg.devMode) {
105
- const remoteAddr = req.socket.remoteAddress;
106
- if (remoteAddr === '127.0.0.1' || remoteAddr === '::1' || remoteAddr === '::ffff:127.0.0.1') {
107
- return true;
108
- }
109
- }
110
- // Spec 016: the local Wyrm Web UI announces itself with X-Wyrm-Origin: ui.
111
- // [sec 6.3.1] Hardened: require an EXACT loopback socket (dropped the broad
112
- // `startsWith('127.')`) AND a loopback Host header. The Host check defeats
113
- // DNS-rebinding — a rebinding attack reaches the loopback socket but carries
114
- // an attacker-domain Host. Header alone is never sufficient. (A per-session
115
- // UI nonce, tracked separately, will replace this origin-header trust.)
116
- const remoteAddr = req.socket.remoteAddress ?? '';
117
- const isLoopback = remoteAddr === '127.0.0.1' || remoteAddr === '::1' || remoteAddr === '::ffff:127.0.0.1';
118
- const hostName = (req.headers['host'] ?? '').split(':')[0].toLowerCase();
119
- const hostIsLoopback = hostName === 'localhost' || hostName === '127.0.0.1' || hostName === '::1' || hostName === '[::1]';
120
- if (isLoopback && hostIsLoopback && req.headers['x-wyrm-origin'] === 'ui') {
121
- return true;
122
- }
123
- // Check if auth is required
124
- if (!cfg.requireAuth) {
125
- logger.warn('Authentication disabled - API is open');
126
- return true;
127
- }
128
- // Validate Authorization header
129
- const authHeader = req.headers['authorization'];
130
- if (!authHeader || !authHeader.startsWith('Bearer ')) {
131
- return false;
132
- }
133
- const token = authHeader.slice(7);
134
- if (!token || token.length < 32) {
135
- return false;
136
- }
137
- // Constant-time comparison
138
- const tokenHash = hashToken(token);
139
- return constantTimeCompare(tokenHash, cfg.apiKeyHash);
140
- }
141
- // ==================== RATE LIMITING ====================
142
- /**
143
- * Check and update rate limit for a client
144
- */
145
- export function checkRateLimit(req) {
146
- const cfg = getConfig();
147
- if (!cfg.rateLimit.enabled) {
148
- return { allowed: true, remaining: Infinity, resetAt: 0 };
149
- }
150
- const clientId = req.socket.remoteAddress || 'unknown';
151
- const now = Date.now();
152
- // Clean up expired entries periodically
153
- if (rateLimitStore.size > 10000) {
154
- for (const [key, entry] of rateLimitStore) {
155
- if (entry.resetAt < now) {
156
- rateLimitStore.delete(key);
157
- }
158
- }
159
- }
160
- let entry = rateLimitStore.get(clientId);
161
- if (!entry || entry.resetAt < now) {
162
- entry = { count: 0, resetAt: now + cfg.rateLimit.windowMs };
163
- rateLimitStore.set(clientId, entry);
164
- }
165
- entry.count++;
166
- return {
167
- allowed: entry.count <= cfg.rateLimit.requests,
168
- remaining: Math.max(0, cfg.rateLimit.requests - entry.count),
169
- resetAt: entry.resetAt,
170
- };
171
- }
172
- // ==================== CORS ====================
173
- /**
174
- * Get CORS origin for response
175
- */
176
- export function getCorsOrigin(req) {
177
- const cfg = getConfig();
178
- const origin = req.headers['origin'];
179
- if (origin && cfg.allowedOrigins.includes(origin)) {
180
- return origin;
181
- }
182
- // Return first allowed origin if request origin not in list
183
- return cfg.allowedOrigins[0];
184
- }
185
- /**
186
- * Get security headers for response
187
- */
188
- export function getSecurityHeaders(req) {
189
- return {
190
- 'Access-Control-Allow-Origin': getCorsOrigin(req),
191
- 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
192
- 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
193
- 'X-Content-Type-Options': 'nosniff',
194
- 'X-Frame-Options': 'DENY',
195
- 'Cache-Control': 'no-store',
196
- 'Content-Security-Policy': "default-src 'none'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; frame-ancestors 'none'",
197
- 'Referrer-Policy': 'no-referrer',
198
- 'Permissions-Policy': 'geolocation=(), microphone=(), camera=()',
199
- 'X-Robots-Tag': 'noindex, nofollow',
200
- };
201
- }
202
- // ==================== MIDDLEWARE ====================
203
- // Public endpoints that don't require authentication
204
- const PUBLIC_ENDPOINTS = ['/health', '/auth/status', '/ui'];
205
- /**
206
- * Authentication middleware for HTTP server
207
- * Returns error response if auth fails, null if auth passes
208
- */
209
- export function authMiddleware(req, res) {
210
- // Handle CORS preflight
211
- if (req.method === 'OPTIONS') {
212
- const headers = getSecurityHeaders(req);
213
- res.writeHead(204, headers);
214
- res.end();
215
- return { error: true }; // Request handled, stop processing
216
- }
217
- // Check rate limit (applies to all endpoints)
218
- const rateLimit = checkRateLimit(req);
219
- if (!rateLimit.allowed) {
220
- const headers = getSecurityHeaders(req);
221
- res.writeHead(429, {
222
- ...headers,
223
- 'X-RateLimit-Remaining': '0',
224
- 'X-RateLimit-Reset': String(rateLimit.resetAt),
225
- 'Retry-After': String(Math.ceil((rateLimit.resetAt - Date.now()) / 1000)),
226
- });
227
- res.end(JSON.stringify({ error: 'Rate limit exceeded' }));
228
- logger.warn('Rate limit exceeded', {
229
- ip: req.socket.remoteAddress,
230
- path: req.url,
231
- });
232
- return { error: true };
233
- }
234
- // Skip auth for public endpoints
235
- const pathname = new URL(req.url || '/', `http://localhost`).pathname;
236
- if (PUBLIC_ENDPOINTS.includes(pathname)) {
237
- return { error: false };
238
- }
239
- // Check authentication
240
- if (!authenticate(req)) {
241
- const headers = getSecurityHeaders(req);
242
- res.writeHead(401, {
243
- ...headers,
244
- 'WWW-Authenticate': 'Bearer realm="Wyrm API"',
245
- });
246
- res.end(JSON.stringify({ error: 'Unauthorized' }));
247
- logger.warn('Authentication failed', {
248
- ip: req.socket.remoteAddress,
249
- path: req.url,
250
- });
251
- return { error: true };
252
- }
253
- // Add rate limit headers to successful response later
254
- return { error: false };
255
- }
256
- // ==================== CONFIGURATION MANAGEMENT ====================
257
- /**
258
- * Regenerate API key
259
- */
260
- export function regenerateApiKey() {
261
- const apiKey = randomBytes(32).toString('hex');
262
- const cfg = getConfig();
263
- cfg.apiKeyHash = hashToken(apiKey);
264
- writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 2), { mode: 0o600 });
265
- config = cfg;
266
- logger.info('API key regenerated');
267
- return apiKey;
268
- }
269
- /**
270
- * Update allowed CORS origins
271
- */
272
- export function setAllowedOrigins(origins) {
273
- const cfg = getConfig();
274
- cfg.allowedOrigins = origins;
275
- writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 2), { mode: 0o600 });
276
- config = cfg;
277
- logger.info('CORS origins updated', { origins });
278
- }
279
- /**
280
- * Update rate limit configuration
281
- */
282
- export function setRateLimit(requests, windowMs) {
283
- const cfg = getConfig();
284
- cfg.rateLimit = { enabled: true, requests, windowMs };
285
- writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 2), { mode: 0o600 });
286
- config = cfg;
287
- logger.info('Rate limit updated', { requests, windowMs });
288
- }
289
- /**
290
- * Enable/disable authentication requirement
291
- */
292
- export function setRequireAuth(require) {
293
- const cfg = getConfig();
294
- cfg.requireAuth = require;
295
- writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 2), { mode: 0o600 });
296
- config = cfg;
297
- if (!require) {
298
- logger.warn('Authentication disabled - API is now open');
299
- }
300
- }
301
- /**
302
- * Enable dev mode at runtime (e.g. for --ui flag)
303
- * Safe because auth still verifies remoteAddress is localhost
304
- */
305
- export function enableDevMode() {
306
- const cfg = getConfig();
307
- cfg.devMode = true;
308
- }
309
- /**
310
- * Get current configuration (without exposing API key hash)
311
- */
312
- export function getAuthStatus() {
313
- const cfg = getConfig();
314
- return {
315
- requireAuth: cfg.requireAuth,
316
- devMode: cfg.devMode,
317
- rateLimit: cfg.rateLimit,
318
- allowedOrigins: cfg.allowedOrigins,
319
- };
320
- }
321
- //# sourceMappingURL=http-auth.js.map
1
+ import{existsSync as w,readFileSync as I,writeFileSync as u,mkdirSync as R}from"fs";import{join as y}from"path";import{homedir as x}from"os";import{randomBytes as O,createHash as T}from"crypto";import{WyrmLogger as k}from"./logger.js";const g=y(x(),".wyrm"),a=y(g,"http-config.json"),L={enabled:!0,requests:100,windowMs:6e4},S=["http://localhost:3333","http://127.0.0.1:3333"],d=new Map;let c=null;const i=new k;function m(e){return T("sha256").update(e).digest("hex")}function P(e,t){if(e.length!==t.length)return!1;let r=0;for(let n=0;n<e.length;n++)r|=e.charCodeAt(n)^t.charCodeAt(n);return r===0}function M(){if(w(g)||R(g,{recursive:!0,mode:448}),w(a))try{const r=JSON.parse(I(a,"utf-8"));return{apiKeyHash:r.apiKeyHash,allowedOrigins:r.allowedOrigins||S,rateLimit:{...L,...r.rateLimit},requireAuth:r.requireAuth??!0,devMode:process.env.WYRM_DEV==="true"}}catch(r){i.error("Failed to load HTTP config, regenerating",{error:r.message})}const e=O(32).toString("hex");i.info("Generated new API key for HTTP server"),console.log(`
2
+ `+"\u2550".repeat(60)),console.log("\u{1F510} WYRM API KEY (save this securely, shown once):"),console.log(""),console.log(` ${e}`),console.log(""),console.log(" Use with: Authorization: Bearer <key>"),console.log("\u2550".repeat(60)+`
3
+ `);const t={apiKeyHash:m(e),allowedOrigins:S,rateLimit:L,requireAuth:!0,devMode:process.env.WYRM_DEV==="true"};return u(a,JSON.stringify(t,null,2),{mode:384}),t}function s(){return c||(c=M()),c}function N(e){const t=s();if(t.devMode){const h=e.socket.remoteAddress;if(h==="127.0.0.1"||h==="::1"||h==="::ffff:127.0.0.1")return!0}const r=e.socket.remoteAddress??"",n=r==="127.0.0.1"||r==="::1"||r==="::ffff:127.0.0.1",o=(e.headers.host??"").split(":")[0].toLowerCase();if(n&&(o==="localhost"||o==="127.0.0.1"||o==="::1"||o==="[::1]")&&e.headers["x-wyrm-origin"]==="ui")return!0;if(!t.requireAuth)return i.warn("Authentication disabled - API is open"),!0;const l=e.headers.authorization;if(!l||!l.startsWith("Bearer "))return!1;const f=l.slice(7);if(!f||f.length<32)return!1;const C=m(f);return P(C,t.apiKeyHash)}function H(e){const t=s();if(!t.rateLimit.enabled)return{allowed:!0,remaining:1/0,resetAt:0};const r=e.socket.remoteAddress||"unknown",n=Date.now();if(d.size>1e4)for(const[A,l]of d)l.resetAt<n&&d.delete(A);let o=d.get(r);return(!o||o.resetAt<n)&&(o={count:0,resetAt:n+t.rateLimit.windowMs},d.set(r,o)),o.count++,{allowed:o.count<=t.rateLimit.requests,remaining:Math.max(0,t.rateLimit.requests-o.count),resetAt:o.resetAt}}function v(e){const t=s(),r=e.headers.origin;return r&&t.allowedOrigins.includes(r)?r:t.allowedOrigins[0]}function p(e){return{"Access-Control-Allow-Origin":v(e),"Access-Control-Allow-Methods":"GET, POST, OPTIONS","Access-Control-Allow-Headers":"Content-Type, Authorization","X-Content-Type-Options":"nosniff","X-Frame-Options":"DENY","Cache-Control":"no-store","Content-Security-Policy":"default-src 'none'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; frame-ancestors 'none'","Referrer-Policy":"no-referrer","Permissions-Policy":"geolocation=(), microphone=(), camera=()","X-Robots-Tag":"noindex, nofollow"}}const D=["/health","/auth/status","/ui"];function J(e,t){if(e.method==="OPTIONS"){const o=p(e);return t.writeHead(204,o),t.end(),{error:!0}}const r=H(e);if(!r.allowed){const o=p(e);return t.writeHead(429,{...o,"X-RateLimit-Remaining":"0","X-RateLimit-Reset":String(r.resetAt),"Retry-After":String(Math.ceil((r.resetAt-Date.now())/1e3))}),t.end(JSON.stringify({error:"Rate limit exceeded"})),i.warn("Rate limit exceeded",{ip:e.socket.remoteAddress,path:e.url}),{error:!0}}const n=new URL(e.url||"/","http://localhost").pathname;if(D.includes(n))return{error:!1};if(!N(e)){const o=p(e);return t.writeHead(401,{...o,"WWW-Authenticate":'Bearer realm="Wyrm API"'}),t.end(JSON.stringify({error:"Unauthorized"})),i.warn("Authentication failed",{ip:e.socket.remoteAddress,path:e.url}),{error:!0}}return{error:!1}}function _(){const e=O(32).toString("hex"),t=s();return t.apiKeyHash=m(e),u(a,JSON.stringify(t,null,2),{mode:384}),c=t,i.info("API key regenerated"),e}function U(e){const t=s();t.allowedOrigins=e,u(a,JSON.stringify(t,null,2),{mode:384}),c=t,i.info("CORS origins updated",{origins:e})}function z(e,t){const r=s();r.rateLimit={enabled:!0,requests:e,windowMs:t},u(a,JSON.stringify(r,null,2),{mode:384}),c=r,i.info("Rate limit updated",{requests:e,windowMs:t})}function B(e){const t=s();t.requireAuth=e,u(a,JSON.stringify(t,null,2),{mode:384}),c=t,e||i.warn("Authentication disabled - API is now open")}function G(){const e=s();e.devMode=!0}function X(){const e=s();return{requireAuth:e.requireAuth,devMode:e.devMode,rateLimit:e.rateLimit,allowedOrigins:e.allowedOrigins}}export{J as authMiddleware,N as authenticate,H as checkRateLimit,G as enableDevMode,X as getAuthStatus,v as getCorsOrigin,p as getSecurityHeaders,_ as regenerateApiKey,U as setAllowedOrigins,z as setRateLimit,B as setRequireAuth};