Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
641 lines (609 loc) · 27.3 KB
/
Copy pathlib.rs
File metadata and controls
641 lines (609 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#![recursion_limit = "512"]
use salvo::cors::Cors;
use salvo::prelude::*;
#[cfg(test)]
use std::path::PathBuf;
use std::sync::Arc;
use tracing_subscriber::EnvFilter;
#[cfg(test)]
use uuid::Uuid;
mod action_audit;
mod action_audit_sessions;
mod admin_http;
mod admin_project_lifecycle;
mod agent_quic;
mod agent_session;
mod agent_tokens_http;
mod agent_ws;
mod audit_http;
mod auth;
mod client_window;
mod config;
mod connector_runtime;
mod console_web;
mod db;
mod host_console_http;
mod mcp;
mod model_surface;
mod models;
mod oauth_http;
mod openapi;
mod pairing_http;
mod project_entry;
mod projects;
mod runtime_http;
mod shell_client;
mod startup;
mod task_cli;
#[cfg(test)]
mod test_support;
mod tool_request_trace;
mod tool_runtime;
mod users_http;
#[cfg(test)]
pub(crate) use webcodex_admin as admin_cli;
pub(crate) use webcodex_agent_config as agent_init;
pub(crate) use webcodex_core::{
apply_edits_shared, artifact_policy, build_info, lsp_bridge, sensitive_paths, shell_protocol,
validation_bridge,
};
pub(crate) use webcodex_sandbox as command_sandbox;
pub(crate) use webcodex_workspace::{project_context, project_overview, workspace_checkpoint};
pub(crate) use auth::{get_db, json_error, AuthMiddleware};
pub(crate) use config::load_startup_env_files;
#[cfg(test)]
pub(crate) use config::parse_env_file_line;
pub use config::CodexConfig;
pub use config::Config;
pub use config::OAuth2Config;
pub use db::{Database, RotateResult};
pub use models::{ActionEventRecord, ActionSessionRecord};
pub(crate) use openapi::openapi_json;
pub(crate) use shell_client::{
shell_agent_job_update, shell_agent_persistent_shell_result, shell_agent_poll,
shell_agent_register, shell_agent_result, shell_file_op, shell_job, shell_job_log,
shell_job_status, shell_job_stop, shell_jobs_list, shell_run, ShellClientRegistry,
};
pub use startup::{is_project_command, run_project_command, CliCommandOutput};
// ============================================================================
// Main
// ============================================================================
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ServerBinaryAction {
Run,
Exit {
code: i32,
stdout: String,
stderr: String,
},
}
pub fn server_binary_action<I, S>(args: I) -> ServerBinaryAction
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let args: Vec<String> = args
.into_iter()
.map(|arg| arg.as_ref().to_string())
.collect();
match args.as_slice() {
[] => ServerBinaryAction::Run,
[arg] if matches!(arg.as_str(), "--help" | "-h") => ServerBinaryAction::Exit {
code: 0,
stdout: "Usage: webcodex-server [OPTIONS]\n\nRun the WebCodex server runtime.\n\nOptions:\n -h, --help Print help and exit\n -V, --version Print version and exit\n".to_string(),
stderr: String::new(),
},
[arg] if matches!(arg.as_str(), "--version" | "-V") => ServerBinaryAction::Exit {
code: 0,
stdout: build_info::version_output("webcodex-server"),
stderr: String::new(),
},
_ => ServerBinaryAction::Exit {
code: 2,
stdout: String::new(),
stderr: format!(
"unknown argument(s): {}\nRun `webcodex-server --help` for usage.\n",
args.join(" ")
),
},
}
}
/// Whole-service HTTP request timeout (defense in depth). Must stay above the
/// MCP dispatch hard bound (150s) plus response-write margin so the inner,
/// better-reported timeouts always fire first.
const REQUEST_HARD_TIMEOUT_SECS: u64 = 300;
pub async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
let env_loads = load_startup_env_files().map_err(std::io::Error::other)?;
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
)
.init();
for load in &env_loads {
tracing::info!(
"Loaded env file {} ({} variables set{})",
load.path.display(),
load.loaded_count,
if load.legacy {
", legacy deprecated path"
} else {
""
}
);
}
let config = Config::from_env();
let console_asset_source = Arc::new(
console_web::ConsoleAssetSource::from_env(&config.addr).map_err(std::io::Error::other)?,
);
if !config.is_auth_enabled() {
tracing::warn!(
"WEBCODEX_TOKEN is not set! Running in development mode without authentication. \
Use `webcodex server init` to generate a bootstrap/admin key, or set WEBCODEX_ALLOW_ANONYMOUS=true \
only for local/trusted-network demos."
);
tracing::warn!("Anonymous API access is rejected by default in production mode.");
}
let build_info = build_info::current();
tracing::info!(
"Starting WebCodex v{} (commit {})",
build_info.version,
build_info.git_commit.unwrap_or("unknown")
);
tracing::info!("Data directory: {:?}", config.data_dir);
let addr = config.addr.clone();
tracing::info!("Listening on: {}", addr);
tracing::info!("Console assets: {}", console_asset_source.mode_label());
if let Some(directory) = console_asset_source.directory() {
tracing::info!("Console assets directory: {}", directory.display());
}
std::fs::create_dir_all(config.uploads_dir())?;
let db = Database::open(&config.db_path())?;
tracing::info!("Database initialized at {:?}", config.db_path());
// Set max payload size to 2MB for text messages
salvo::http::request::set_global_secure_max_size(config.max_text_size);
let cors = Cors::permissive();
let config = Arc::new(config);
let db = Arc::new(db);
// First-party authorize browser session store (in-memory, short-lived).
// Holds the opaque session id -> user mapping bridging the authorize
// login form to the consent decision. PAT/bootstrap plaintext is never
// stored here — only the resolved user identity.
let authorize_session_store = Arc::new(oauth_http::AuthorizeSessionStore::new());
let shell_registry = Arc::new(ShellClientRegistry::default());
let quic_cfg = config::QuicServerConfig::from_env();
let connector_context =
connector_runtime::ConnectorContext::from_env().map_err(std::io::Error::other)?;
// Resolve the model surface exactly once at startup, after Connector
// configuration has been parsed and validated. Every request-time
// projection reads this immutable enum from ToolRuntime.
let model_surface = model_surface::resolve_model_surface(connector_context.as_ref())
.map_err(std::io::Error::other)?;
let runtime_info = Arc::new(tool_runtime::RuntimeInfo::from_env_with_quic_config(
&quic_cfg,
));
let mut tool_runtime_builder = tool_runtime::ToolRuntime::new(
shell_registry.clone(),
Arc::new(config.codex.clone()),
runtime_info.clone(),
)
.with_model_surface(model_surface)
.with_checkpoint_state_dir(config.runtime_state_dir())
.with_session_ledger(config.session_ledger_path());
if let Some(activity_store) = db::WorkspaceActivityStore::from_env(db.clone()) {
tool_runtime_builder =
tool_runtime_builder.with_activity_recorder(Arc::new(activity_store));
}
let tool_runtime = Arc::new(tool_runtime_builder);
let connector_runtime = connector_runtime::ConnectorRuntime::from_context(
tool_runtime.clone(),
db.clone(),
connector_context,
)
.map_err(std::io::Error::other)?;
if let Some(runtime) = connector_runtime.0.as_ref() {
tracing::info!(
project_id = %runtime.context().project_id,
profile = %runtime.context().profile,
capabilities = connector_runtime::surface::CAPABILITY_NAMES.len(),
model_surface = model_surface.name(),
"Project-bound connector surface enabled"
);
} else {
tracing::info!(
model_surface = model_surface.name(),
config = "WEBCODEX_MCP_MODEL_SURFACE",
"MCP model surface enabled"
);
}
// Custom QUIC agent transport. Default disabled;
// only starts when WEBCODEX_QUIC_ENABLED=true. Runs a separate quinn UDP
// listener in parallel with the HTTP server. HTTP/WebSocket/polling and
// the GPT Actions / Nginx path are completely unaffected. This is NOT
// HTTP/3 and Nginx does not terminate QUIC.
if quic_cfg.enabled {
if let Err(e) = quic_cfg.validate() {
if let Some(status) = runtime_info.quic.as_ref() {
status
.lock()
.expect("quic runtime status mutex poisoned")
.mark_error(&e);
}
tracing::error!(
"QUIC listener disabled due to config error: {}; check WEBCODEX_QUIC_LISTEN/CERT/KEY/ALPN",
e
);
} else {
let quic_config = config.clone();
let quic_db = db.clone();
let quic_registry = shell_registry.clone();
let quic_cfg_task = quic_cfg.clone();
let quic_status = runtime_info.quic.clone();
tokio::spawn(async move {
if let Err(e) = agent_quic::run_quic_agent_listener(
quic_config,
Some(quic_db),
quic_registry,
quic_cfg_task,
quic_status,
)
.await
{
tracing::error!(
"QUIC agent listener exited with error: {}; check bind address, UDP port availability, certificate/key readability, and ALPN",
e
);
}
});
tracing::info!(
"Agent QUIC configured on UDP {} ALPN {}",
quic_cfg.listen,
quic_cfg.alpn
);
}
}
let _admin_route_allowlist = admin_http::ADMIN_ROUTES;
let authed_api_router = Router::new()
.hoop(AuthMiddleware)
.push(connector_runtime::http::routes())
.push(host_console_http::routes())
.push(admin_http::routes())
.push(Router::with_path("tools/list").post(runtime_http::tools_list))
.push(Router::with_path("tools/call").post(runtime_http::tools_call))
.push(
Router::with_path("artifacts/import")
.post(runtime_http::import_conversation_files_to_project),
)
.push(Router::with_path("jobs/status").post(runtime_http::job_status))
.push(Router::with_path("jobs/log").post(runtime_http::job_log))
.push(Router::with_path("jobs/stop").post(runtime_http::job_stop))
.push(Router::with_path("jobs/list").post(runtime_http::jobs_list))
.push(Router::with_path("jobs/tail").post(runtime_http::job_tail))
.push(Router::with_path("projects/list").post(runtime_http::projects_list))
.push(Router::with_path("projects/register").post(runtime_http::projects_register))
.push(Router::with_path("projects/create").post(runtime_http::projects_create))
.push(Router::with_path("projects/read_file").post(runtime_http::projects_read_file))
.push(Router::with_path("projects/git_status").post(runtime_http::projects_git_status))
.push(Router::with_path("projects/git_diff").post(runtime_http::projects_git_diff))
.push(
Router::with_path("projects/git_diff_summary")
.post(runtime_http::projects_git_diff_summary),
)
.push(Router::with_path("projects/list_files").post(runtime_http::projects_list_files))
.push(Router::with_path("projects/search_text").post(runtime_http::projects_search_text))
.push(Router::with_path("projects/apply_patch").post(runtime_http::projects_apply_patch))
.push(
Router::with_path("projects/validate_patch")
.post(runtime_http::projects_validate_patch),
)
.push(Router::with_path("projects/run_shell").post(runtime_http::projects_run_shell))
.push(
Router::with_path("projects/apply_patch_checked")
.post(runtime_http::projects_apply_patch_checked),
)
.push(Router::with_path("projects/delete_files").post(runtime_http::projects_delete_files))
.push(
Router::with_path("projects/git_restore_paths")
.post(runtime_http::projects_git_restore_paths),
)
.push(
Router::with_path("projects/discard_untracked")
.post(runtime_http::projects_discard_untracked),
)
.push(Router::with_path("projects/run_job").post(runtime_http::projects_run_job))
.push(Router::with_path("runtime/status").post(runtime_http::runtime_status))
// Phase 2e-3: first-party OAuth client management API. Behind
// AuthMiddleware; route policy is FirstPartyOnly so OAuth2 access
// tokens are rejected even with account:manage.
.push(Router::with_path("oauth/clients/create").post(oauth_http::oauth_clients_create))
.push(Router::with_path("oauth/clients/list").post(oauth_http::oauth_clients_list))
.push(Router::with_path("oauth/clients/revoke").post(oauth_http::oauth_clients_revoke))
// Phase 2 multi-user auth: user + personal API token management.
// REST-only admin/self-management surface; intentionally NOT
// exposed in /openapi.json (GPT Actions) because token creation is
// sensitive. All behind the shared AuthMiddleware Bearer auth.
.push(Router::with_path("users/create").post(users_http::users_create))
.push(Router::with_path("users/list").post(users_http::users_list))
.push(Router::with_path("users/me").post(users_http::users_me))
.push(Router::with_path("tokens/create").post(users_http::tokens_create))
.push(Router::with_path("tokens/register_hash").post(users_http::tokens_register_hash))
.push(Router::with_path("tokens/list").post(users_http::tokens_list))
.push(Router::with_path("tokens/revoke").post(users_http::tokens_revoke))
// Phase 3 agent token management: REST-only admin/self-management
// surface for agent tokens bound to an owner + allowed_client_id.
// Intentionally NOT exposed in /openapi.json (GPT Actions) because
// token creation is sensitive. All behind the shared AuthMiddleware
// Bearer auth. Agent tokens themselves are rejected from these
// endpoints so a leaked agent token cannot mint more tokens.
.push(Router::with_path("agent-tokens/create").post(agent_tokens_http::agent_tokens_create))
.push(
Router::with_path("agent-tokens/register_hash")
.post(agent_tokens_http::agent_tokens_register_hash),
)
.push(Router::with_path("agent-tokens/list").post(agent_tokens_http::agent_tokens_list))
.push(Router::with_path("agent-tokens/revoke").post(agent_tokens_http::agent_tokens_revoke))
.push(Router::with_path("shell/run").post(shell_run))
.push(Router::with_path("shell/file").post(shell_file_op))
.push(Router::with_path("shell/job").post(shell_job))
.push(Router::with_path("shell/jobs/status").post(shell_job_status))
.push(Router::with_path("shell/jobs/log").post(shell_job_log))
.push(Router::with_path("shell/jobs/stop").post(shell_job_stop))
.push(Router::with_path("shell/jobs/list").post(shell_jobs_list))
.push(Router::with_path("shell/agent/register").post(shell_agent_register))
.push(Router::with_path("shell/agent/poll").post(shell_agent_poll))
.push(Router::with_path("shell/agent/result").post(shell_agent_result))
.push(
Router::with_path("shell/agent/persistent_shell_result")
.post(shell_agent_persistent_shell_result),
)
.push(Router::with_path("shell/agent/job_update").post(shell_agent_job_update))
// WebSocket agent transport (preferred long-lived connection).
// Polling endpoints above remain as fallback. Bearer auth is
// enforced by the shared AuthMiddleware hoop.
.push(Router::with_path("agents/ws").get(agent_ws::agent_ws));
let api_router = Router::with_path("api")
.push(Router::with_path("pairing/enroll").post(pairing_http::pairing_enroll))
.push(
authed_api_router
.push(Router::with_path("pairing/create").post(pairing_http::pairing_create)),
);
let openapi_router = Router::with_path("openapi.json").get(openapi_json);
// Read-only readiness console. Public static entry — the HTML/JS/CSS
// bundle carries no secrets; project facts come from the protected shared
// `POST /api/connector/readiness` application projection. Mirrors
// `/openapi.json` being public. NOT part of the GPT Actions schema.
let console_router = Router::with_path("console")
.get(console_web::console_html)
.push(Router::with_path("app.js").get(console_web::console_app_js))
.push(Router::with_path("styles.css").get(console_web::console_styles_css));
let admin_router = Router::with_path("admin")
.get(console_web::admin_html)
.push(Router::with_path("app.js").get(console_web::admin_app_js))
.push(Router::with_path("styles.css").get(console_web::admin_styles_css));
let mut router = Router::new()
// Whole-service backstop: no handler may hold an HTTP request open
// forever. Sized well above every legitimate request — sync agent
// waits are <= ~122s and MCP dispatch is hard-bounded at 150s — so it
// only fires on a genuinely unbounded hang, converting a permanently
// silent request into an explicit 503. Long-lived work is unaffected:
// agent polling replies immediately and WebSocket connections live in
// a task spawned after the (fast) upgrade handshake completes.
.hoop(salvo::timeout::Timeout::new(
std::time::Duration::from_secs(REQUEST_HARD_TIMEOUT_SECS),
))
.hoop(affix_state::inject(config.clone()))
.hoop(affix_state::inject(db.clone()))
.hoop(affix_state::inject(authorize_session_store.clone()))
.hoop(affix_state::inject(shell_registry.clone()))
.hoop(affix_state::inject(tool_runtime.clone()))
.hoop(affix_state::inject(connector_runtime.clone()))
.hoop(affix_state::inject(console_asset_source))
.hoop(cors.into_handler())
.push(api_router)
.push(openapi_router)
.push(console_router)
.push(admin_router)
// OAuth2 token, revocation, and discovery endpoints — public, no
// AuthMiddleware. Token/revoke clients authenticate via
// client_id + client_secret in the form body.
.push(Router::with_path("oauth/token").post(oauth_http::oauth_token))
.push(Router::with_path("oauth/revoke").post(oauth_http::oauth_revoke))
// /oauth/authorize is NOT behind AuthMiddleware: the handler accepts
// either a first-party Bearer token (Bootstrap / PAT, backward
// compatible direct code issuance) or a short-lived authorize
// session cookie set by the login form. login/consent do their own
// token/session validation.
.push(
Router::with_path("oauth/authorize")
.get(oauth_http::oauth_authorize)
.push(Router::with_path("login").post(oauth_http::oauth_authorize_login))
.push(Router::with_path("consent").post(oauth_http::oauth_authorize_consent))
.push(Router::with_path("bridge").post(oauth_http::oauth_authorize_bridge)),
)
.push(
Router::with_path(".well-known/oauth-protected-resource")
.get(oauth_http::oauth_metadata),
)
.push(
Router::with_path(".well-known/oauth-authorization-server")
.get(oauth_http::oauth_authorization_server_metadata),
)
.push(
Router::with_path("mcp")
.hoop(AuthMiddleware)
.get(mcp::mcp_info)
.post(mcp::mcp_post),
);
// Read-only audit query API. Admin/debug surface only: NOT part of the
// GPT Actions OpenAPI schema. All endpoints are POST + Bearer auth.
router = router.push(
Router::with_path("api/audit")
.hoop(AuthMiddleware)
.push(Router::with_path("sessions").post(audit_http::audit_sessions))
.push(Router::with_path("session").post(audit_http::audit_session))
.push(Router::with_path("stats").post(audit_http::audit_stats)),
);
let acceptor = TcpListener::new(addr.clone()).bind().await;
tracing::info!("Server started successfully!");
let port = addr.split(':').last().unwrap_or("8080");
let base = format!("http://localhost:{}", port);
tracing::info!("Runtime base: {}", base);
tracing::info!("MCP endpoint: {}/mcp", base);
tracing::info!(
tool_request_trace = crate::config::tool_request_trace_enabled(),
"tool_request_trace"
);
tracing::info!(
mcp_compact_schemas = crate::config::mcp_compact_schemas_enabled(),
"mcp_compact_schemas"
);
tracing::info!(
action_compact_responses = crate::config::action_compact_responses_enabled(),
"action_compact_responses"
);
tracing::info!("OpenAPI (GPT Actions): {}/openapi.json", base);
tracing::info!("MCP App console: {}/console", base);
tracing::info!("Runtime status: {}/api/runtime/status", base);
tracing::info!("Agent WebSocket: {}/api/agents/ws", base);
tracing::info!("Agent polling (fallback): {}/api/shell/agent/poll", base);
tracing::info!("Audit API (read-only): {}/api/audit/sessions", base);
// Periodic recovery-timeout sweep for disconnected reconciliation-capable
// runners. A job whose runner disconnected enters `recovering`; if that
// runner never reconnects and nobody queries the job, the on-demand
// deadline check would never run. This background task bounds `recovering`
// to the grace window independently of request traffic. It is pure
// in-memory, holds the registry mutex only for bounded HashMap work, and
// dies with the process. A server restart resets the in-memory registry;
// the deadline is re-anchored only when a runner reconnects and submits its
// inventory. See docs/AGENT_PROTOCOL.md (recovery deadline sweep).
let sweep_registry = shell_registry.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(
shell_client::RECOVERY_SWEEP_INTERVAL_SECS,
));
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
// Skip the first (immediate) tick so a sweep does not race startup
// reconciliation before any runner has had a chance to re-register.
interval.tick().await;
loop {
interval.tick().await;
shell_client::recovery_timeout_sweep(&sweep_registry).await;
}
});
Server::new(acceptor).serve(router).await;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_env_file_line_basic() {
let parsed = parse_env_file_line("WEBCODEX_ADDR=127.0.0.1:8080")
.unwrap()
.unwrap();
assert_eq!(parsed.0, "WEBCODEX_ADDR");
assert_eq!(parsed.1, "127.0.0.1:8080");
}
#[test]
fn test_parse_env_file_line_quotes_and_export() {
let parsed = parse_env_file_line("export RUST_LOG='info,codex.metrics=info'")
.unwrap()
.unwrap();
assert_eq!(parsed.0, "RUST_LOG");
assert_eq!(parsed.1, "info,codex.metrics=info");
}
#[test]
fn test_parse_env_file_line_ignores_empty_and_comments() {
assert!(parse_env_file_line("").is_none());
assert!(parse_env_file_line(" # comment").is_none());
}
#[test]
fn test_parse_env_file_line_rejects_invalid_key() {
assert!(parse_env_file_line("webcodex_token=x").unwrap().is_err());
assert!(parse_env_file_line("DROP TOKEN=x").unwrap().is_err());
}
#[test]
fn test_uuid_generation_not_empty() {
let id = Uuid::new_v4().to_string();
assert!(!id.is_empty());
assert_eq!(id.len(), 36); // UUID v4 with hyphens
assert!(id.contains('-'));
}
#[test]
fn test_uuid_generation_unique() {
let id1 = Uuid::new_v4().to_string();
let id2 = Uuid::new_v4().to_string();
assert_ne!(id1, id2);
}
#[test]
fn test_config_from_env_defaults() {
let _guard = crate::admin_cli::TEST_ENV_LOCK.lock().unwrap();
// Clear env vars to test defaults
std::env::remove_var("WEBCODEX_ADDR");
std::env::remove_var("WEBCODEX_DATA");
std::env::remove_var("WEBCODEX_TOKEN");
std::env::remove_var("CODEX_BIN");
std::env::remove_var("CODEX_APPROVAL_MODE");
std::env::remove_var("CODEX_DEFAULT_TIMEOUT_SECS");
std::env::remove_var("CODEX_MAX_PROMPT_BYTES");
std::env::remove_var("CODEX_ALLOWED_EXTRA_ARGS");
let config = Config::from_env();
assert_eq!(config.addr, "0.0.0.0:8080");
assert_eq!(config.data_dir, PathBuf::from("./data"));
assert_eq!(config.token, None);
assert!(!config.is_auth_enabled());
assert_eq!(config.max_text_size, 2 * 1024 * 1024);
assert_eq!(config.max_file_size, 100 * 1024 * 1024);
assert_eq!(config.codex.bin, "codex");
assert_eq!(config.codex.approval_mode, "");
assert_eq!(config.codex.default_timeout_secs, 3600);
assert_eq!(config.codex.max_prompt_bytes, 100_000);
assert!(config.codex.allowed_extra_args.is_empty());
}
#[test]
fn test_config_validate_token() {
let config = Config {
addr: "0.0.0.0:8080".to_string(),
data_dir: PathBuf::from("./data"),
token: Some("secret123".to_string()),
max_text_size: 2 * 1024 * 1024,
max_file_size: 100 * 1024 * 1024,
codex: CodexConfig::default(),
oauth2: crate::OAuth2Config::default(),
};
assert!(config.is_auth_enabled());
assert!(config.validate_token("secret123"));
assert!(!config.validate_token("wrong"));
assert!(!config.validate_token(""));
}
#[test]
fn test_config_validate_token_none() {
let config = Config {
addr: "0.0.0.0:8080".to_string(),
data_dir: PathBuf::from("./data"),
token: None,
max_text_size: 2 * 1024 * 1024,
max_file_size: 100 * 1024 * 1024,
codex: CodexConfig::default(),
oauth2: crate::OAuth2Config::default(),
};
assert!(!config.is_auth_enabled());
// When no token is set, validation always returns false
assert!(!config.validate_token("anything"));
}
#[test]
fn test_filename_sanitization() {
// Test that path separators are stripped from display names
let filename = "test/file\\name.txt";
let safe: String = filename
.chars()
.filter(|c| !matches!(c, '/' | '\\' | '\0' | '\r' | '\n'))
.collect();
assert_eq!(safe, "testfilename.txt");
}
#[test]
fn test_filename_sanitization_quotes() {
let filename = "file\"name.txt";
let safe = filename.replace('"', "_");
assert_eq!(safe, "file_name.txt");
}
}