mirror of
https://github.com/max-sixty/worktrunk.git
synced 2026-11-22 02:53:04 +08:00
Three tests in `tests/integration_tests/security.rs` passed without running. Each built a branch name around `__WORKTRUNK_EXEC__echo PWNED > /tmp/hackedN`, asked `git branch` to create it, and returned early when git refused — and git refuses that name on every platform and every version, because [ref-name rule 4](https://git-scm.com/docs/git-check-ref-format) rejects a space. The `wt switch --create` invocation, its snapshot assertion, and (for the `--execute` test) the whole point of the test never executed. The trailing `assert!(!Path::new("/tmp/hackedN").exists())` then passed against a command that had never run. The symptom was visible in the repo: `tests/snapshots/` held snapshots for the other tests in this file but none for these three, because `assert_cmd_snapshot!` was never reached. ## What changed - **`test_branch_name_is_directive_not_executed`** and **`test_execute_flag_with_directive_like_branch_name`** now use `__WORKTRUNK_EXEC__touch$IFS/tmp/hackedN`. `$IFS` word-splitting makes it a space-free payload that still creates the canary if a shell ever evaluates it, and it is a legal ref name. They also drop the `git branch` pre-creation and let `wt switch --create` create the branch, so the new snapshots cover the success path rather than an "already exists" error — the `--execute` snapshot now shows `echo 'legitimate command'` running and `/tmp/hacked7` staying absent, which is the claim the test's name makes. - **Both of those tests now read the CD directive file back.** They configured `WORKTRUNK_DIRECTIVE_CD_FILE` and never looked at it, which left `/tmp/hackedN` as the only post-run assertion — and that canary cannot fail here, because these tests run the `wt` binary directly and nothing in that process evaluates a branch name as shell. `assert_cd_file_holds_one_path` pins the contract `src/output/global.rs` documents for the file: one line, an absolute path that exists. A directive-shaped branch name that smuggled a second line or a directive token past the display layer fails there. `tests/integration_tests/directives.rs` already reads the file back this way; `security.rs` did so nowhere. - **`test_branch_name_with_newline_directive_not_executed`** → **`test_git_rejects_newline_directive_in_branch_name`**. A newline is a control character git always refuses, so no such branch can exist for `wt` to read. Git's refusal *is* the defense here (layer 3 in the module's security model), so the test asserts it instead of returning from it — and its `/tmp/hacked3` canary is gone, since the test no longer runs `wt` at all. - The two remaining skip guards in the file — in `test_branch_name_with_cd_directive_not_executed` and `test_literal_escape_like_branch_names_displayed_safely` — become assertions. Both branch names are valid today; asserting stops them going vacuous the same way. - The module docstring gains a short section on writing a payload git will accept, so the next author doesn't reach for a space again. ## Payload choice The payloads avoid `< > : " | ? *`. These tests now create a real worktree, and [`sanitize_branch_name`](https://github.com/max-sixty/worktrunk/blob/09c1c4539a86b7719a1ee10475c64eec8bdc0e66/src/config/expansion.rs#L542) rewrites only `/` and `\`, so any other Windows-illegal character would survive into the worktree directory name and fail `test (windows)`. An earlier draft used `echo>/tmp/hackedN`, which git accepts and Windows does not. <details><summary>Verification</summary> `git branch` against git 2.55 confirms the rule the old payloads tripped: ``` REJECTED: __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked2 REJECTED: feature\n__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked3 ACCEPTED: __WORKTRUNK_CD__/tmp ACCEPTED: __WORKTRUNK_EXEC__touch$IFS/tmp/hacked2 ``` `cargo test --test integration security` is 10 passed / 0 failed, with the two new snapshots generated and a second run clean under `INSTA_UPDATE=no`. `cargo fmt --check` and `cargo clippy --all-targets` are clean. The new CD-file assertion was checked for liveness by inverting its expected line count, which fails with the file's real contents — `"…/repo.__WORKTRUNK_EXEC__touch$IFS-tmp-hacked2\n"` — rather than with an empty or missing file. The full `cargo test --test integration` run in this sandbox leaves three failures in `step_copy_ignored` (umask 0002 giving 0664 where 0644 is expected; `mkfifo` returning `PermissionDenied`) and `merge::test_merge_no_ff_signs_merge_commit` (no signing key). All three are environment restrictions in files this PR does not touch; CI is the check that matters for them. </details> ## Not fixed here `test_branch_name_with_cd_directive_not_executed` still pre-creates its branch with `git branch`, so it snapshots the `Branch … already exists` error and never reaches a CD write. That snapshot is the only coverage of how a directive-shaped name renders in an error message, so moving it onto the success path would trade one kind of coverage for another rather than add one. `sanitize_branch_name` passing `< > : " | ? *` through into a worktree directory name is a real cross-platform gap — a branch named `feat>x` is legal in git and would fail worktree creation on Windows. Widening it changes the worktree path users already have on disk, so it's a product decision rather than a test fix; noting it rather than folding it in. --------- Co-authored-by: worktrunk-bot <254187624+worktrunk-bot@users.noreply.github.com>
481 lines
18 KiB
Rust
481 lines
18 KiB
Rust
//! Security tests for shell injection vulnerabilities
|
|
//!
|
|
//! # Security Model
|
|
//!
|
|
//! The CD directive (`WORKTRUNK_DIRECTIVE_CD_FILE`) contains a raw path. The shell
|
|
//! wrapper changes to that path without evaluating it. `--execute` starts one
|
|
//! external program directly; no shell parses its argv.
|
|
//!
|
|
//! 1. **Structured inputs**: CD is a raw path and execute is argv; neither is
|
|
//! parsed as shell.
|
|
//!
|
|
//! 2. **Channel separation**: User messages go to stderr; directive files are
|
|
//! separate. Malicious content in stderr cannot reach the directive files.
|
|
//!
|
|
//! 3. **Git layer**: Git REJECTS invalid characters in ref names (NUL, most
|
|
//! control characters, shell metacharacters like backtick).
|
|
//!
|
|
//! 4. **Filesystem layer**: OS enforces valid path characters (NUL is
|
|
//! universally invalid in paths).
|
|
//!
|
|
//! ## What These Tests Verify
|
|
//!
|
|
//! The CD file holds a raw path, so path-based shell injection is structurally
|
|
//! impossible. These tests verify the remaining attack surface:
|
|
//!
|
|
//! 1. Branch names with shell metacharacters don't corrupt the cd path
|
|
//! 2. Malicious branch names don't create unexpected files
|
|
//! 3. Git's ref-name validation rejects the most dangerous characters
|
|
//!
|
|
//! ## Testing Limitations
|
|
//!
|
|
//! These tests run the Rust binary, not the shell wrapper. They verify that
|
|
//! the direct execution input is safe, but they don't test the wrapper. Full
|
|
//! end-to-end tests with the shell wrapper
|
|
//! are in `tests/integration_tests/shell_wrapper.rs`.
|
|
//!
|
|
//! ## Writing a payload git will accept
|
|
//!
|
|
//! A branch-name payload has to survive git's ref-name rules (git-check-ref-format(1)
|
|
//! rule 4), which reject a space and every ASCII control character — so
|
|
//! `echo PWNED > /tmp/…` and anything carrying a newline can never become a
|
|
//! branch, and a test built on one exercises nothing. Word-split the payload
|
|
//! with `$IFS` instead: `touch$IFS/tmp/hacked2` is a legal ref name and still
|
|
//! creates the canary if a shell ever evaluates it. A payload that reaches a
|
|
//! worktree path must also be a legal filename on Windows, which rules out
|
|
//! `< > : " | ? *` — `sanitize_branch_name` only rewrites `/` and `\`.
|
|
//!
|
|
//! Where git's own rules are the defense, assert the refusal rather than
|
|
//! returning early from it: a skip reads as a pass on every platform.
|
|
|
|
use crate::common::{
|
|
TestRepo, configure_directive_file, directive_file, repo, setup_snapshot_settings, wt_command,
|
|
};
|
|
use insta_cmd::assert_cmd_snapshot;
|
|
use rstest::rstest;
|
|
use std::process::Command;
|
|
use worktrunk::config::sanitize_branch_name;
|
|
|
|
///
|
|
/// Git provides the first line of defense by refusing to create commits
|
|
/// with NUL bytes in the message.
|
|
#[rstest]
|
|
fn test_git_rejects_nul_in_commit_messages(repo: TestRepo) {
|
|
use std::process::Stdio;
|
|
|
|
// Try to create a commit with NUL in the message
|
|
// We can't use Command::arg() because Rust rejects NUL bytes,
|
|
// so we use printf piped to git commit -F -
|
|
let malicious_message = "Fix bug\0__WORKTRUNK_EXEC__echo PWNED";
|
|
|
|
// Create a file to commit
|
|
std::fs::write(repo.root_path().join("test.txt"), "content").unwrap();
|
|
repo.run_git(&["add", "."]);
|
|
|
|
// Try to commit with NUL in message using shell redirection
|
|
let shell_cmd = format!(
|
|
"printf '{}' | git commit -F -",
|
|
malicious_message.replace('\0', r"\0")
|
|
);
|
|
|
|
let mut cmd = Command::new("sh");
|
|
repo.configure_git_cmd(&mut cmd);
|
|
cmd.arg("-c")
|
|
.arg(&shell_cmd)
|
|
.current_dir(repo.root_path())
|
|
.stdout(Stdio::null())
|
|
.stderr(Stdio::piped());
|
|
|
|
let output = cmd.output().unwrap();
|
|
|
|
// Git should reject this
|
|
assert!(
|
|
!output.status.success(),
|
|
"Expected git to reject NUL bytes in commit message"
|
|
);
|
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("NUL byte") || stderr.contains("nul byte"),
|
|
"Expected git to complain about NUL bytes, got: {}",
|
|
stderr
|
|
);
|
|
}
|
|
|
|
///
|
|
/// This verifies that the OS/Rust provides protection against NUL injection.
|
|
/// Rust's Command API uses C strings internally, which reject NUL bytes.
|
|
#[rstest]
|
|
fn test_rust_prevents_nul_bytes_in_args(repo: TestRepo) {
|
|
// Rust's Command API should reject NUL bytes in arguments
|
|
let malicious_branch = "feature\0__WORKTRUNK_EXEC__echo PWNED";
|
|
|
|
// Cmd::run() should fail with InvalidInput error (NUL bytes rejected by Command API)
|
|
let result = repo.git_command().args(["branch", malicious_branch]).run();
|
|
|
|
match result {
|
|
Err(e) if e.kind() == std::io::ErrorKind::InvalidInput => {
|
|
// Good! Rust prevented the NUL byte injection
|
|
}
|
|
Ok(output) => {
|
|
panic!(
|
|
"Expected Rust to reject NUL bytes in args, but command succeeded: {:?}",
|
|
output
|
|
);
|
|
}
|
|
Err(e) => {
|
|
panic!(
|
|
"Expected InvalidInput error for NUL bytes, got different error: {:?}",
|
|
e
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A branch name that is entirely an EXEC directive must not be executed.
|
|
///
|
|
/// The payload carries no space: git's ref-name rule 4 rejects a space outright
|
|
/// (as it does control characters), so a name built around `echo PWNED >
|
|
/// /tmp/…` can never be created and everything below it would be skipped.
|
|
/// `touch$IFS/tmp/hacked2` is the space-free equivalent — a valid ref name, a
|
|
/// legal path component on every platform `wt` builds a worktree on, and a
|
|
/// shell command that still creates the canary through `$IFS` word splitting
|
|
/// if anything ever evaluates it.
|
|
///
|
|
/// `wt switch --create` creates the branch itself, so this covers the success
|
|
/// path: the snapshot shows the directive-shaped name reaching the worktree
|
|
/// path and the success message verbatim, and the canary assertion shows
|
|
/// nothing evaluated it on the way.
|
|
#[rstest]
|
|
fn test_branch_name_is_directive_not_executed(repo: TestRepo) {
|
|
let malicious_branch = "__WORKTRUNK_EXEC__touch$IFS/tmp/hacked2";
|
|
let expected_worktree = expected_worktree_path(&repo, malicious_branch);
|
|
|
|
let settings = setup_snapshot_settings(&repo);
|
|
|
|
settings.bind(|| {
|
|
let (cd_path, _guard) = directive_file();
|
|
let mut cmd = wt_command();
|
|
repo.configure_wt_cmd(&mut cmd);
|
|
configure_directive_file(&mut cmd, &cd_path);
|
|
cmd.arg("switch")
|
|
.arg("--create")
|
|
.arg(malicious_branch)
|
|
.current_dir(repo.root_path());
|
|
|
|
assert_cmd_snapshot!(cmd);
|
|
|
|
assert_cd_file_holds_worktree_path(&cd_path, &expected_worktree);
|
|
});
|
|
|
|
// Verify the malicious file was NOT created
|
|
assert!(
|
|
!std::path::Path::new("/tmp/hacked2").exists(),
|
|
"Malicious code was executed! File /tmp/hacked2 should not exist"
|
|
);
|
|
}
|
|
|
|
/// The worktree path `wt switch --create <branch>` produces under the default
|
|
/// layout: a sibling of the repo named `<repo>.<branch>`, with `/` and `\`
|
|
/// dashed out by `sanitize_branch_name` and nothing else rewritten.
|
|
fn expected_worktree_path(repo: &TestRepo, branch: &str) -> std::path::PathBuf {
|
|
let root = repo.root_path();
|
|
let repo_name = root.file_name().expect("repo root has a name");
|
|
root.parent().expect("repo root has a parent").join(format!(
|
|
"{}.{}",
|
|
repo_name.to_string_lossy(),
|
|
sanitize_branch_name(branch)
|
|
))
|
|
}
|
|
|
|
/// Assert the CD directive file holds what `wt` promises a wrapper it holds:
|
|
/// one line, and that line is the worktree `wt` just created.
|
|
///
|
|
/// This is the assertion that carries the directive tests. `wt` writes the CD
|
|
/// file itself (`src/output/global.rs`), so a branch name that smuggled a
|
|
/// second line or a different destination past the display layer would show up
|
|
/// here — whereas the `/tmp/hackedN` canaries in the callers can only ever
|
|
/// pass: these tests run the binary directly, and no shell evaluates the
|
|
/// file's contents.
|
|
///
|
|
/// It compares against `expected` rather than asking whether the line names
|
|
/// *a* directory, because the directive these tests model
|
|
/// (`__WORKTRUNK_CD__/tmp`) smuggles a path that exists — a verifier that only
|
|
/// checked `is_dir()` would pass on the leak it is named for. Both sides are
|
|
/// canonicalized: `wt` writes the logical, symlink-preserved path, which is
|
|
/// not textually the tempdir path on macOS.
|
|
fn assert_cd_file_holds_worktree_path(cd_path: &std::path::Path, expected: &std::path::Path) {
|
|
let cd_content = std::fs::read_to_string(cd_path).unwrap_or_default();
|
|
assert_eq!(
|
|
cd_content.lines().count(),
|
|
1,
|
|
"the CD file must hold a single line, got {cd_content:?}"
|
|
);
|
|
let written = std::fs::canonicalize(cd_content.trim()).unwrap_or_else(|e| {
|
|
panic!("the CD file must hold an existing path, got {cd_content:?}: {e}")
|
|
});
|
|
let expected = std::fs::canonicalize(expected)
|
|
.unwrap_or_else(|e| panic!("the new worktree {} should exist: {e}", expected.display()));
|
|
assert_eq!(
|
|
written, expected,
|
|
"the CD file must hold the new worktree's path, got {cd_content:?}"
|
|
);
|
|
}
|
|
|
|
/// Assert `wt` left the CD directive file untouched.
|
|
///
|
|
/// The counterpart to [`assert_cd_file_holds_worktree_path`] for the tests
|
|
/// whose `wt switch` fails: no switch happened, so nothing may move the user's
|
|
/// shell. `directive_file()` hands over an empty file, and `wt` only writes one
|
|
/// after a successful switch (`handle_switch_output`), so any content here is a
|
|
/// destination that came from somewhere other than a completed switch.
|
|
fn assert_cd_file_unwritten(cd_path: &std::path::Path) {
|
|
let cd_content = std::fs::read_to_string(cd_path).unwrap_or_default();
|
|
assert!(
|
|
cd_content.is_empty(),
|
|
"a failed switch must not write a cd directive, got {cd_content:?}"
|
|
);
|
|
}
|
|
|
|
/// A directive smuggled onto a second line never reaches `wt` at all.
|
|
///
|
|
/// Git's ref-name validation is the layer that stops this one (defense layer 3
|
|
/// above): a newline is a control character, so `git branch` refuses the name
|
|
/// on every platform and no such branch can exist for `wt` to read. Assert that
|
|
/// refusal rather than skipping past it — the earlier form of this test tried
|
|
/// to create the branch and returned early when git said no, so its body never
|
|
/// ran and it passed without exercising anything.
|
|
#[rstest]
|
|
fn test_git_rejects_newline_directive_in_branch_name(repo: TestRepo) {
|
|
let malicious_branch = "feature\n__WORKTRUNK_EXEC__touch$IFS/tmp/hacked3";
|
|
|
|
let result = repo
|
|
.git_command()
|
|
.args(["branch", malicious_branch])
|
|
.run()
|
|
.unwrap();
|
|
|
|
assert!(
|
|
!result.status.success(),
|
|
"git should reject a branch name containing a newline"
|
|
);
|
|
}
|
|
|
|
///
|
|
/// This tests if commit messages shown in output (e.g., wt list, logs) could inject directives
|
|
#[rstest]
|
|
fn test_commit_message_with_directive_not_executed(mut repo: TestRepo) {
|
|
// Create commit with malicious message (no NUL - Rust prevents those)
|
|
let malicious_message = "Fix bug\n__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked4";
|
|
repo.commit_with_message(malicious_message);
|
|
|
|
// Create a worktree
|
|
let _feature_wt = repo.add_worktree("feature");
|
|
|
|
let mut settings = setup_snapshot_settings(&repo);
|
|
// Filter SHAs because commit_with_message creates non-deterministic hashes
|
|
settings.add_filter(r"\b[0-9a-f]{7,40}\b", "[SHA]");
|
|
|
|
// Run 'wt list' which might show commit messages
|
|
settings.bind(|| {
|
|
let mut cmd = wt_command();
|
|
repo.configure_wt_cmd(&mut cmd);
|
|
cmd.arg("list").current_dir(repo.root_path());
|
|
|
|
// Verify output - commit message should be escaped/sanitized
|
|
assert_cmd_snapshot!(cmd);
|
|
});
|
|
|
|
// Verify the malicious file was NOT created
|
|
assert!(
|
|
!std::path::Path::new("/tmp/hacked4").exists(),
|
|
"Malicious code was executed from commit message!"
|
|
);
|
|
}
|
|
|
|
///
|
|
/// Similar to EXEC injection, but for CD directives
|
|
#[rstest]
|
|
fn test_branch_name_with_cd_directive_not_executed(repo: TestRepo) {
|
|
// Branch name that IS a CD directive (no NUL - git allows this)
|
|
let malicious_branch = "__WORKTRUNK_CD__/tmp";
|
|
|
|
let result = repo
|
|
.git_command()
|
|
.args(["branch", malicious_branch])
|
|
.run()
|
|
.unwrap();
|
|
|
|
assert!(
|
|
result.status.success(),
|
|
"git should accept {malicious_branch} as a branch name; \
|
|
without the branch there is nothing for this test to exercise"
|
|
);
|
|
|
|
let settings = setup_snapshot_settings(&repo);
|
|
|
|
settings.bind(|| {
|
|
let (cd_path, _guard) = directive_file();
|
|
let mut cmd = wt_command();
|
|
repo.configure_wt_cmd(&mut cmd);
|
|
configure_directive_file(&mut cmd, &cd_path);
|
|
cmd.arg("switch")
|
|
.arg("--create")
|
|
.arg(malicious_branch)
|
|
.current_dir(repo.root_path());
|
|
|
|
// The branch is pre-created above, so `--create` fails: this snapshots
|
|
// how a directive-shaped name renders in an error message.
|
|
assert_cmd_snapshot!(cmd);
|
|
|
|
// The payload names `/tmp`, which exists: the proof that nothing
|
|
// honored it is that the failed switch wrote no destination at all.
|
|
assert_cd_file_unwritten(&cd_path);
|
|
});
|
|
}
|
|
|
|
///
|
|
/// This tests if error messages (e.g., from git) could inject directives
|
|
#[rstest]
|
|
fn test_error_message_with_directive_not_executed(repo: TestRepo) {
|
|
// Try to switch to a non-existent branch with a name that looks like a directive
|
|
let malicious_branch = "__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6";
|
|
|
|
let settings = setup_snapshot_settings(&repo);
|
|
|
|
settings.bind(|| {
|
|
let (cd_path, _guard) = directive_file();
|
|
let mut cmd = wt_command();
|
|
repo.configure_wt_cmd(&mut cmd);
|
|
configure_directive_file(&mut cmd, &cd_path);
|
|
cmd.arg("switch")
|
|
.arg(malicious_branch)
|
|
.current_dir(repo.root_path());
|
|
|
|
// Should fail with error, but not execute directive
|
|
assert_cmd_snapshot!(cmd);
|
|
|
|
assert_cd_file_unwritten(&cd_path);
|
|
});
|
|
|
|
assert!(
|
|
!std::path::Path::new("/tmp/hacked6").exists(),
|
|
"Malicious code was executed from error message!"
|
|
);
|
|
}
|
|
|
|
///
|
|
/// User content in branch names that looks like old directives must not become
|
|
/// part of the explicitly requested command.
|
|
#[rstest]
|
|
fn test_execute_flag_with_directive_like_branch_name(repo: TestRepo) {
|
|
// Branch name that looks like a directive. Space-free for the same reason
|
|
// as `test_branch_name_is_directive_not_executed`: git rejects a ref-name
|
|
// containing a space, so the old payload could never be created and the
|
|
// branch it needs never existed.
|
|
let malicious_branch = "__WORKTRUNK_EXEC__touch$IFS/tmp/hacked7";
|
|
let expected_worktree = expected_worktree_path(&repo, malicious_branch);
|
|
|
|
let settings = setup_snapshot_settings(&repo);
|
|
|
|
settings.bind(|| {
|
|
let (cd_path, _guard) = directive_file();
|
|
let mut cmd = wt_command();
|
|
repo.configure_wt_cmd(&mut cmd);
|
|
configure_directive_file(&mut cmd, &cd_path);
|
|
cmd.arg("switch")
|
|
.arg("--create")
|
|
.arg(malicious_branch)
|
|
.arg("-x")
|
|
.arg("echo")
|
|
.args(["--", "legitimate command"])
|
|
.current_dir(repo.root_path());
|
|
|
|
assert_cmd_snapshot!(cmd);
|
|
|
|
assert_cd_file_holds_worktree_path(&cd_path, &expected_worktree);
|
|
});
|
|
|
|
assert!(
|
|
!std::path::Path::new("/tmp/hacked7").exists(),
|
|
"Malicious code was executed alongside legitimate -x command!"
|
|
);
|
|
}
|
|
|
|
// =============================================================================
|
|
// ANSI escape sequence handling in branch names
|
|
// =============================================================================
|
|
|
|
/// Test that git rejects branch names containing ANSI escape sequences.
|
|
///
|
|
/// ANSI escape sequences could theoretically corrupt terminal output if they
|
|
/// appeared in branch names displayed by `wt list`. However, git blocks this
|
|
/// at the ref validation level: control characters (bytes < 0x20 or 0x7F)
|
|
/// are rejected by git check-ref-format rule 4.
|
|
///
|
|
/// The escape character (`\x1b` = 27) is a control character, so git rejects it.
|
|
///
|
|
/// Note: Git for Windows with MSYS2 bash behaves differently and may accept
|
|
/// these branch names, so this test is Unix-only.
|
|
#[rstest]
|
|
#[cfg(unix)]
|
|
fn test_git_rejects_ansi_escape_in_branch_names(repo: TestRepo) {
|
|
let shell_cmd = r#"git branch $'feature-\x1b[31mRED\x1b[0m-test'"#;
|
|
|
|
// The `git` this bash child spawns inherits its environment, so the
|
|
// isolation goes on the shell (as it does for the `sh` child above).
|
|
// `LC_ALL`/`LANG=C` from `git_test_env` is what the stderr assertion below
|
|
// rests on — git ships translated messages, so on a contributor's machine
|
|
// with a non-English locale the check for "not a valid branch name" reads a
|
|
// German or French rendering of it and fails.
|
|
let mut cmd = Command::new("bash");
|
|
repo.configure_git_cmd(&mut cmd);
|
|
let output = cmd
|
|
.args(["-c", shell_cmd])
|
|
.current_dir(repo.root_path())
|
|
.output()
|
|
.unwrap();
|
|
|
|
assert!(
|
|
!output.status.success(),
|
|
"Expected git to reject ANSI escape sequences in branch name"
|
|
);
|
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("not a valid branch name") || stderr.contains("invalid"),
|
|
"Expected git to complain about invalid branch name, got: {}",
|
|
stderr
|
|
);
|
|
}
|
|
|
|
/// Test that literal escape-like text in branch names displays safely.
|
|
///
|
|
/// Branch names like "fix-backslash-x1b-test" contain literal characters
|
|
/// (not actual escape codes). Git allows this and they should display literally.
|
|
#[rstest]
|
|
fn test_literal_escape_like_branch_names_displayed_safely(repo: TestRepo) {
|
|
let branch_name = "fix-backslash-x1b-test";
|
|
|
|
let output = repo
|
|
.git_command()
|
|
.args(["branch", branch_name])
|
|
.run()
|
|
.expect("git branch should run");
|
|
assert!(
|
|
output.status.success(),
|
|
"git should accept {branch_name} as a branch name"
|
|
);
|
|
|
|
let mut settings = setup_snapshot_settings(&repo);
|
|
settings.add_filter(r"\b[0-9a-f]{7,40}\b", "[SHA]");
|
|
|
|
settings.bind(|| {
|
|
let mut cmd = wt_command();
|
|
repo.configure_wt_cmd(&mut cmd);
|
|
cmd.args(["list", "--branches"])
|
|
.current_dir(repo.root_path());
|
|
assert_cmd_snapshot!(cmd);
|
|
});
|
|
}
|