diff --git a/crates/git-remote-twigpine/src/main.rs b/crates/git-remote-twigpine/src/main.rs index 2d7ab2837..451333180 100644 --- a/crates/git-remote-twigpine/src/main.rs +++ b/crates/git-remote-twigpine/src/main.rs @@ -158,7 +158,12 @@ fn allow_insecure_http_enabled() -> bool { /// the pack it carries, to anyone on the path. `TWIGPINE_ALLOW_INSECURE_HTTP` /// exists for a private LAN where the operator has decided that is acceptable. fn check_transport_security(node_base: &str, allow_insecure: bool) -> Result<()> { - if allow_insecure || !is_insecure_remote(node_base) { + // Validate the configured URL before appending any repository path segments. + let node_url = reqwest::Url::parse(node_base).context("invalid TWIGPINE_NODE URL")?; + if !matches!(node_url.scheme(), "http" | "https") { + bail!("TWIGPINE_NODE must use http or https"); + } + if allow_insecure || !is_insecure_remote(node_url.as_str()) { return Ok(()); } bail!( @@ -1425,6 +1430,18 @@ mod insecure_transport_tests { .expect("TLS must need no opt-in"); } + #[test] + fn gate_requires_a_valid_node_url_even_with_plaintext_opt_in() { + for node_base in ["http://", "https://", "missing-scheme", "file:///tmp"] { + for allow_insecure in [false, true] { + assert!( + check_transport_security(node_base, allow_insecure).is_err(), + "{node_base:?} must be a valid URL before appending the repository path" + ); + } + } + } + /// The opt-in is a value, not presence: only `1` / `true` (any case, trimmed) /// opens the gate, and `0`, `false`, empty, a typo, or a non-UTF-8 value /// leave it closed. diff --git a/crates/twigpine-node/src/git/store.rs b/crates/twigpine-node/src/git/store.rs index 6296d265b..32a0fcd81 100644 --- a/crates/twigpine-node/src/git/store.rs +++ b/crates/twigpine-node/src/git/store.rs @@ -2380,9 +2380,7 @@ impl std::fmt::Display for GitRefValidationError { } } -/// Reject revision shorthands and pseudoref names that `git check-ref-format -/// --branch` accepts but that git later interprets as symbolic revisions rather -/// than literal local branch names. +/// Reject revision shorthands and pseudoref names that are reserved as short names. fn reject_revision_shorthand(name: &str) -> Option<&'static str> { if name == "@" { return Some("branch ref must not be a revision shorthand"); @@ -2408,17 +2406,14 @@ fn reject_revision_shorthand(name: &str) -> Option<&'static str> { } /// Validate a git *branch name* before it is stored or passed to git subprocesses. -/// Delegates to `git check-ref-format --branch`, then rejects fully qualified -/// ref paths, revision-namespace shorthands (`heads/`, `tags/`, `remotes/`), and -/// symbolic names that git would reinterpret at the sink. +/// Enforce the short-name rules, then use `git check-ref-format` on the literal +/// `refs/heads/` name. The fixed prefix makes the argument positional, and full +/// ref mode validates the name independently of the working repository. /// /// At diff/merge sinks, validated short names are passed as `refs/heads/{name}` so /// grammar-valid revision shorthands cannot retarget another ref namespace. The /// merge worktree checks out the local branch name instead, so merge commits -/// advance refs/heads/{target}. Symbolic revision names (`HEAD`), option-shaped -/// trailing dots, and other invalid forms are rejected by check-ref-format itself. -/// Revision shorthands that pass the grammar check (`@`, `@{-1}`, pseudorefs) are -/// rejected explicitly because git treats them as symbolic revisions when bare. +/// advance refs/heads/{target}. /// /// Storage boundaries call this via [`validate_git_ref_with_git`] with the configured /// git binary. Sink functions call [`validate_git_ref`] (system `git`) then prefix @@ -2437,6 +2432,11 @@ pub(crate) fn validate_git_ref_with_git( "branch ref must not be empty".into(), )); } + if name == "HEAD" || name.starts_with('-') { + return Err(GitRefValidationError::Invalid( + "branch ref must not be HEAD or start with '-'".into(), + )); + } if name.starts_with("refs/") { return Err(GitRefValidationError::Invalid( "branch ref must be a local branch name, not a fully qualified ref".into(), @@ -2450,12 +2450,13 @@ pub(crate) fn validate_git_ref_with_git( if let Some(reason) = reject_revision_shorthand(name) { return Err(GitRefValidationError::Invalid(reason.into())); } - // allow-unbounded-git: stateless check-ref-format on a user-supplied name; no repo acquire or concurrency permit + let full_ref = local_branch_ref(name); + // allow-unbounded-git: stateless check-ref-format on a fully qualified ref; no repo acquire or concurrency permit let output = Command::new(git_bin) - .args(["check-ref-format", "--branch", name]) + .args(["check-ref-format", &full_ref]) .output() .map_err(|e| { - // allow-unbounded-git: stateless check-ref-format on user-supplied name; no repo acquire + // allow-unbounded-git: stateless check-ref-format on a fully qualified ref; no repo acquire GitRefValidationError::GitUnavailable(format!( "failed to run git check-ref-format: {e}" )) @@ -2464,8 +2465,9 @@ pub(crate) fn validate_git_ref_with_git( if output.status.success() { Ok(()) } else { - let stderr = String::from_utf8_lossy(&output.stderr); - Err(GitRefValidationError::Invalid(stderr.trim().to_string())) + Err(GitRefValidationError::Invalid( + "branch ref is not a valid Git branch name".into(), + )) } }