Skip to content

Commit 49c1d43

Browse files
unhappychoiceclaude
andcommitted
fix(fetcher): percent-encode branch in gitlab_pipeline_status URL
Naive interpolation of `branch` into the `&ref=<branch>` query parameter broke the request semantics for any branch name carrying `&`, `=`, `#`, `+`, `?`, or a slash. The fetcher silently returned the wrong pipeline (or a parse error from GitLab) instead of failing fast. Route the value through `url::form_urlencoded::byte_serialize` so the encoded payload stays inside a single query pair. Adds a unit test pinning the encoding for the canonical reserved-character set so a future url-crate loosening surfaces here. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 97a493a commit 49c1d43

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

‎src/fetcher/gitlab/pipeline_status.rs‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use async_trait::async_trait;
66
use serde::Deserialize;
7+
use url::form_urlencoded::byte_serialize;
78

89
use crate::options::OptionSchema;
910
use crate::payload::{BadgeData, Body, EntriesData, Entry, Payload, Status, TextData};
@@ -101,7 +102,12 @@ impl Fetcher for GitlabPipelineStatus {
101102
let project = resolve_project(opts.project.as_deref(), &host)?;
102103
let mut path = format!("/projects/{}/pipelines?per_page=1", project.encoded());
103104
if let Some(branch) = opts.branch.as_deref() {
104-
path.push_str(&format!("&ref={branch}"));
105+
// Branch names can carry `&`, `=`, `#`, `+`, `?`, and slashes (release/v2,
106+
// feature/foo&bar). Naive interpolation rewrote the URL semantics and silently
107+
// returned the wrong pipeline; percent-encode so the value stays a single
108+
// `ref=<branch>` pair.
109+
let encoded: String = byte_serialize(branch.as_bytes()).collect();
110+
path.push_str(&format!("&ref={encoded}"));
105111
}
106112
let pipelines: Vec<Pipeline> = rest_get(&host, &path).await?;
107113
let shape = ctx.shape.unwrap_or(Shape::Badge);
@@ -332,6 +338,23 @@ mod tests {
332338
assert_eq!(format_duration(None), "?");
333339
}
334340

341+
#[test]
342+
fn byte_serialize_handles_reserved_branch_characters() {
343+
// Pin the encoding so a future url-crate version that loosens this set fails the
344+
// test rather than silently shipping a request that the GitLab API misroutes.
345+
let cases = [
346+
("main", "main"),
347+
("release/v2", "release%2Fv2"),
348+
("feature/foo&bar", "feature%2Ffoo%26bar"),
349+
("name=weird", "name%3Dweird"),
350+
("with spaces", "with+spaces"),
351+
];
352+
for (raw, expected) in cases {
353+
let actual: String = byte_serialize(raw.as_bytes()).collect();
354+
assert_eq!(actual, expected, "raw={raw}");
355+
}
356+
}
357+
335358
#[test]
336359
fn no_runs_body_respects_each_advertised_shape() {
337360
// Empty pipeline list used to always degrade to Badge regardless of `ctx.shape`,

0 commit comments

Comments
 (0)