mobius-gateway 0.9.21

Headless authenticated gateway for möbius frontends
Documentation
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
use std::collections::BTreeSet;
use std::path::{Component, Path};
use std::time::Duration;

use mobius::backend::sandbox::SandboxBackend as _;

use crate::sandbox::GatewaySandbox;
use crate::wire::{WorkspaceFileRecord, WorkspaceFileScope};

use super::Rejection;

pub(super) const MAX_WORKSPACE_READ_BYTES: usize = 256 * 1024;
const MAX_WORKSPACE_WRITE_BYTES: usize = 1024 * 1024;
const MAX_WORKSPACE_FILES: usize = 20_000;
const MAX_WORKSPACE_PATH_BYTES: usize = 1024 * 1024;
const FILE_TIMEOUT: Duration = Duration::from_secs(30);

pub(crate) struct WorkspaceRead {
    pub(crate) data: Vec<u8>,
    pub(crate) next_offset: Option<u64>,
}

pub(crate) struct WorkspaceFiles {
    pub(crate) files: Vec<WorkspaceFileRecord>,
    pub(crate) truncated: bool,
}

pub(super) async fn list(
    sandbox: &GatewaySandbox,
    workspace: &Path,
    scope: WorkspaceFileScope,
) -> std::result::Result<WorkspaceFiles, Rejection> {
    tokio::time::timeout(FILE_TIMEOUT, list_inner(sandbox, workspace, scope))
        .await
        .map_err(|_| timeout())?
}

pub(super) async fn read(
    sandbox: &GatewaySandbox,
    path: &str,
    offset: u64,
    max_bytes: usize,
) -> std::result::Result<WorkspaceRead, Rejection> {
    if max_bytes == 0 || max_bytes > MAX_WORKSPACE_READ_BYTES {
        return Err(invalid(format!(
            "workspace read size must be 1–{MAX_WORKSPACE_READ_BYTES} bytes"
        )));
    }
    validate_relative(path)?;
    let (data, next_offset) = sandbox
        .read_workspace_range(path, offset, max_bytes)
        .await
        .map_err(error_rejection)?;
    Ok(WorkspaceRead { data, next_offset })
}

pub(super) async fn write(
    sandbox: &GatewaySandbox,
    path: &str,
    content: &str,
) -> std::result::Result<(), Rejection> {
    validate_relative(path)?;
    if content.len() > MAX_WORKSPACE_WRITE_BYTES {
        return Err(invalid(format!(
            "workspace text files are limited to {MAX_WORKSPACE_WRITE_BYTES} bytes"
        )));
    }
    sandbox.write(path, content).await.map_err(error_rejection)
}

async fn list_inner(
    sandbox: &GatewaySandbox,
    workspace: &Path,
    scope: WorkspaceFileScope,
) -> std::result::Result<WorkspaceFiles, Rejection> {
    let args = match scope {
        WorkspaceFileScope::Modified => &[
            "ls-files",
            "-z",
            "--modified",
            "--deleted",
            "--others",
            "--exclude-standard",
            "--",
        ][..],
        WorkspaceFileScope::All => &[
            "ls-files",
            "-z",
            "--cached",
            "--others",
            "--exclude-standard",
            "--",
        ],
    };
    let mut output = sandbox.execute_git(args).await.map_err(error_rejection)?;
    if output.exit_code == 0 {
        let mut truncated = retain_complete_git_paths(&mut output.stdout, output.stdout_truncated)?;
        if scope == WorkspaceFileScope::Modified {
            let mut staged = sandbox
                .execute_git(&[
                    "diff",
                    "--cached",
                    "--name-only",
                    "-z",
                    "--relative",
                    "--no-ext-diff",
                    "--no-renames",
                    "--",
                ])
                .await
                .map_err(error_rejection)?;
            if staged.exit_code != 0 {
                return Err(error_rejection(format!(
                    "listing staged workspace files failed: {}",
                    staged.stderr.trim()
                )));
            }
            truncated |= retain_complete_git_paths(&mut staged.stdout, staged.stdout_truncated)?;
            output.stdout.push_str(&staged.stdout);
        }
        if scope == WorkspaceFileScope::All
            && !output
                .stdout
                .split_terminator('\0')
                .any(|path| path == ".env")
            && std::fs::symlink_metadata(workspace.join(".env"))
                .is_ok_and(|metadata| metadata.is_file() && !metadata.is_symlink())
        {
            output.stdout.push_str(".env\0");
        }
        let workspace = workspace.to_path_buf();
        return tokio::task::spawn_blocking(move || {
            git_workspace_files(&workspace, &output.stdout, truncated)
        })
        .await
        .map_err(error_rejection)?;
    }
    if !output.stderr.contains("not a git repository") {
        return Err(error_rejection(format!(
            "listing Git workspace files failed: {}",
            output.stderr.trim()
        )));
    }
    if scope == WorkspaceFileScope::Modified {
        return Ok(WorkspaceFiles {
            files: Vec::new(),
            truncated: false,
        });
    }
    Err(invalid(
        "all-files catalog requires a Git repository so ignore rules remain authoritative",
    ))
}

fn git_workspace_files(
    workspace: &Path,
    output: &str,
    mut truncated: bool,
) -> std::result::Result<WorkspaceFiles, Rejection> {
    validate_git_paths(output)?;
    let mut files = Vec::new();
    let mut path_bytes = 0_usize;
    for path in output.split_terminator('\0').collect::<BTreeSet<_>>() {
        let relative = validate_relative(path)?;
        let metadata = match std::fs::symlink_metadata(workspace.join(relative)) {
            Ok(metadata) => metadata,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue,
            Err(error) => return Err(error_rejection(error)),
        };
        if !metadata.file_type().is_file() || metadata.file_type().is_symlink() {
            continue;
        }
        let next_path_bytes = path_bytes.saturating_add(path.len());
        if next_path_bytes > MAX_WORKSPACE_PATH_BYTES || files.len() == MAX_WORKSPACE_FILES {
            truncated = true;
            break;
        }
        path_bytes = next_path_bytes;
        files.push(WorkspaceFileRecord {
            path: path.into(),
            size: metadata.len(),
        });
    }
    Ok(WorkspaceFiles { files, truncated })
}

fn retain_complete_git_paths(
    output: &mut String,
    truncated: bool,
) -> std::result::Result<bool, Rejection> {
    if truncated {
        let complete = output.rfind('\0').map_or(0, |index| index + 1);
        output.truncate(complete);
    }
    validate_git_paths(output)?;
    Ok(truncated)
}

fn validate_git_paths(output: &str) -> std::result::Result<(), Rejection> {
    if !output.is_empty() && !output.ends_with('\0') {
        return Err(invalid(
            "Git workspace file output is truncated or malformed",
        ));
    }
    Ok(())
}

fn validate_relative(path: &str) -> std::result::Result<&Path, Rejection> {
    let relative = Path::new(path);
    let safe = relative.components().all(|component| {
        matches!(component, Component::Normal(value) if value != std::ffi::OsStr::new(".git"))
    });
    if path.is_empty() || path.len() > 4096 || !safe {
        return Err(invalid("workspace file path must be a safe relative path"));
    }
    Ok(relative)
}

fn invalid(message: impl Into<String>) -> Rejection {
    Rejection {
        code: "invalid_workspace_file",
        message: message.into(),
        fatal: false,
    }
}

fn error_rejection(error: impl std::fmt::Display) -> Rejection {
    Rejection {
        code: "workspace_file_error",
        message: error.to_string(),
        fatal: false,
    }
}

fn timeout() -> Rejection {
    Rejection {
        code: "workspace_file_timeout",
        message: format!(
            "workspace file operation exceeded {} seconds",
            FILE_TIMEOUT.as_secs()
        ),
        fatal: false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn git(workspace: &Path, args: &[&str]) {
        let status = std::process::Command::new("git")
            .args(args)
            .current_dir(workspace)
            .status()
            .expect("run Git");
        assert!(status.success());
    }

    fn sandbox(workspace: &Path) -> (tempfile::TempDir, GatewaySandbox) {
        let state = tempfile::tempdir().expect("state");
        let sandbox = GatewaySandbox::new(workspace, state.path(), None, Duration::from_secs(5))
            .expect("gateway sandbox");
        (state, sandbox)
    }

    #[tokio::test]
    async fn read_rejects_parent_traversal() {
        let workspace = tempfile::tempdir().expect("workspace");
        let (_state, sandbox) = sandbox(workspace.path());

        let result = read(&sandbox, "../outside", 0, 16).await;

        assert!(matches!(
            result,
            Err(Rejection {
                code: "invalid_workspace_file",
                ..
            })
        ));
    }

    #[tokio::test]
    async fn write_creates_and_atomically_replaces_workspace_text() {
        let workspace = tempfile::tempdir().expect("workspace");
        let (_state, sandbox) = sandbox(workspace.path());

        write(&sandbox, ".env", "TOKEN=first\n")
            .await
            .expect("create workspace file");
        write(&sandbox, ".env", "TOKEN=second\n")
            .await
            .expect("replace workspace file");

        assert_eq!(
            std::fs::read_to_string(workspace.path().join(".env")).expect("read workspace file"),
            "TOKEN=second\n"
        );
        assert!(write(&sandbox, "../outside", "secret").await.is_err());
    }

    #[tokio::test]
    async fn git_catalog_excludes_ignored_directories_and_git_internals() {
        let workspace = tempfile::tempdir().expect("workspace");
        git(workspace.path(), &["init", "--quiet"]);
        std::fs::write(workspace.path().join(".gitignore"), b"ignored/\n.env\n")
            .expect("ignore rules");
        std::fs::create_dir(workspace.path().join("ignored")).expect("ignored directory");
        std::fs::write(workspace.path().join("ignored/generated.txt"), b"ignored")
            .expect("ignored file");
        std::fs::write(workspace.path().join("included.txt"), b"included").expect("included file");
        std::fs::write(workspace.path().join(".env"), b"TOKEN=secret\n").expect("environment file");
        let (_state, sandbox) = sandbox(workspace.path());

        let catalog = list(&sandbox, workspace.path(), WorkspaceFileScope::All)
            .await
            .expect("workspace files");

        assert!(!catalog.truncated);
        assert!(catalog.files.iter().any(|file| file.path == ".env"));
        assert!(catalog.files.iter().any(|file| file.path == ".gitignore"));
        assert!(catalog.files.iter().any(|file| file.path == "included.txt"));
        assert!(
            catalog
                .files
                .iter()
                .all(|file| !file.path.starts_with("ignored/"))
        );
        assert!(
            catalog
                .files
                .iter()
                .all(|file| !file.path.starts_with(".git/"))
        );
        assert!(read(&sandbox, ".git/config", 0, 16).await.is_err());
    }

    #[tokio::test]
    async fn modified_catalog_includes_every_openable_uncommitted_file() {
        let workspace = tempfile::tempdir().expect("workspace");
        git(workspace.path(), &["init", "--quiet"]);
        std::fs::write(workspace.path().join(".gitignore"), b"ignored.txt\n")
            .expect("ignore rules");
        for path in ["clean.txt", "staged.txt", "unstaged.txt", "deleted.txt"] {
            std::fs::write(workspace.path().join(path), b"baseline").expect("baseline file");
        }
        git(workspace.path(), &["add", "."]);
        git(
            workspace.path(),
            &[
                "-c",
                "user.name=möbius Test",
                "-c",
                "user.email=mobius@example.invalid",
                "commit",
                "--quiet",
                "-m",
                "baseline",
            ],
        );
        std::fs::write(workspace.path().join("staged.txt"), b"staged").expect("staged file");
        git(workspace.path(), &["add", "staged.txt"]);
        std::fs::write(workspace.path().join("staged.txt"), b"staged and unstaged")
            .expect("second staged file change");
        std::fs::write(workspace.path().join("unstaged.txt"), b"unstaged").expect("unstaged file");
        std::fs::write(workspace.path().join("untracked.txt"), b"untracked")
            .expect("untracked file");
        std::fs::write(workspace.path().join("ignored.txt"), b"ignored").expect("ignored file");
        std::fs::remove_file(workspace.path().join("deleted.txt")).expect("deleted file");
        let (_state, sandbox) = sandbox(workspace.path());

        let catalog = list(&sandbox, workspace.path(), WorkspaceFileScope::Modified)
            .await
            .expect("modified files");

        assert_eq!(
            catalog
                .files
                .into_iter()
                .map(|file| file.path)
                .collect::<Vec<_>>(),
            ["staged.txt", "unstaged.txt", "untracked.txt"]
        );
    }

    #[tokio::test]
    async fn modified_catalog_is_relative_to_a_nested_workspace() {
        // Bubblewrap intentionally masks host /tmp; keep the parent repository visible.
        let repository = tempfile::tempdir_in(std::env::current_dir().expect("current directory"))
            .expect("repository");
        let workspace = repository.path().join("workspace");
        std::fs::create_dir(&workspace).expect("workspace");
        git(repository.path(), &["init", "--quiet"]);
        std::fs::write(workspace.join("inside.txt"), b"baseline").expect("inside file");
        std::fs::write(repository.path().join("outside.txt"), b"baseline").expect("outside file");
        git(repository.path(), &["add", "."]);
        git(
            repository.path(),
            &[
                "-c",
                "user.name=möbius Test",
                "-c",
                "user.email=mobius@example.invalid",
                "commit",
                "--quiet",
                "-m",
                "baseline",
            ],
        );
        std::fs::write(workspace.join("inside.txt"), b"modified").expect("inside change");
        std::fs::write(repository.path().join("outside.txt"), b"modified").expect("outside change");
        git(repository.path(), &["add", "."]);
        let (_state, sandbox) = sandbox(&workspace);

        let catalog = list(&sandbox, &workspace, WorkspaceFileScope::Modified)
            .await
            .expect("modified files");

        assert_eq!(catalog.files.len(), 1);
        assert_eq!(catalog.files[0].path, "inside.txt");
    }

    #[tokio::test]
    async fn non_git_all_catalog_does_not_bypass_ignore_rules() {
        let workspace = tempfile::tempdir().expect("workspace");
        std::fs::write(workspace.path().join(".gitignore"), b"ignored.txt\n")
            .expect("ignore rules");
        std::fs::write(workspace.path().join("ignored.txt"), b"ignored").expect("ignored file");
        let (_state, sandbox) = sandbox(workspace.path());

        let result = list(&sandbox, workspace.path(), WorkspaceFileScope::All).await;

        assert!(matches!(
            result,
            Err(Rejection {
                code: "invalid_workspace_file",
                ..
            })
        ));
    }

    #[test]
    fn git_catalog_rejects_non_terminated_output() {
        let workspace = tempfile::tempdir().expect("workspace");
        std::fs::write(workspace.path().join("file.txt"), b"file").expect("file");

        assert!(git_workspace_files(workspace.path(), "file.txt", false).is_err());
    }

    #[test]
    fn each_git_catalog_stream_requires_a_terminator() {
        assert!(validate_git_paths("first\0").is_ok());
        assert!(validate_git_paths("truncated").is_err());
    }

    #[test]
    fn workspace_file_timeout_reports_the_configured_budget() {
        let rejection = timeout();

        assert_eq!(FILE_TIMEOUT, Duration::from_secs(30));
        assert_eq!(rejection.code, "workspace_file_timeout");
        assert_eq!(
            rejection.message,
            "workspace file operation exceeded 30 seconds"
        );
    }

    #[test]
    fn truncated_git_catalog_keeps_only_complete_records() {
        let workspace = tempfile::tempdir().expect("workspace");
        std::fs::write(workspace.path().join("first.txt"), b"file").expect("file");
        let mut output = "first.txt\0partial".to_string();

        let truncated = retain_complete_git_paths(&mut output, true).expect("Git paths");
        let catalog =
            git_workspace_files(workspace.path(), &output, truncated).expect("workspace files");

        assert!(catalog.truncated);
        assert_eq!(catalog.files.len(), 1);
        assert_eq!(catalog.files[0].path, "first.txt");
    }
}