Skip to content

Commit 228476d

Browse files
unhappychoiceclaude
andcommitted
fix(github): normalize trailing slash in env-provided host/URL vars
Strip whitespace and a trailing `/` so values like `GH_API_URL=https://ghe.example.com/api/v3/` no longer produce double-slash URLs (`.../api/v3//repos/...`). Also update the module doc to reflect that host selection is env-var-only (still no config-file path to attacker-chosen origins) and fix the missing blank line before `const ACCEPT` so `cargo fmt --check` passes in CI. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 24ed307 commit 228476d

2 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎src/fetcher/github/avatar.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::payload::{Body, ImageData, Payload};
2222
use crate::render::Shape;
2323

2424
use super::super::{FetchContext, FetchError, Fetcher, Safety};
25-
use super::client::{host, http, rest_get, resolve_authenticated_user, web_base};
25+
use super::client::{host, http, resolve_authenticated_user, rest_get, web_base};
2626
use super::common::cache_key;
2727

2828
const DEFAULT_SIZE: u32 = 200;
@@ -141,7 +141,11 @@ struct UserAvatar {
141141
/// query so the source is small enough for the media_image renderer.
142142
async fn resolve_ghe_avatar_url(user: &str, size: u32) -> Result<String, FetchError> {
143143
let me: UserAvatar = rest_get(&format!("/users/{user}")).await?;
144-
let sep = if me.avatar_url.contains('?') { '&' } else { '?' };
144+
let sep = if me.avatar_url.contains('?') {
145+
'&'
146+
} else {
147+
'?'
148+
};
145149
Ok(format!("{}{sep}s={size}", me.avatar_url))
146150
}
147151

‎src/fetcher/github/client.rs‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Shared HTTP client + auth for the `github_*` fetcher family. Reads `GH_TOKEN` or
2-
//! `GITHUB_TOKEN` (in that order) once per process. Every fetcher talks to `api.github.com`
3-
//! only — there is no config-controlled host, so leaking a user's token to an attacker-chosen
4-
//! origin is not possible by design.
2+
//! `GITHUB_TOKEN` (in that order) once per process. The host defaults to `github.com` but
3+
//! can be redirected to a GitHub Enterprise Server via `GH_HOST` / `GITHUB_HOST` (and
4+
//! optionally `GH_API_URL` / `GH_GRAPHQL_URL` for older `<host>/api/v3` deployments). Host
5+
//! selection is env-var-only — a `.splashboard.toml` cannot redirect traffic to an
6+
//! attacker-chosen origin, so the token cannot leak by design.
57
//!
68
//! The rest helpers accept a path like `"/user"` or `"/repos/foo/bar"`; the base URL is joined
79
//! here so fetchers stay free of URL plumbing. GraphQL goes through a single POST helper.
@@ -19,10 +21,16 @@ const DEFAULT_HOST: &str = "github.com";
1921
const USER_AGENT: &str = concat!("splashboard/", env!("CARGO_PKG_VERSION"));
2022

2123
fn read_env(var: &str) -> Option<String> {
22-
std::env::var(var)
23-
.ok()
24-
.map(|s| s.trim().to_owned())
25-
.filter(|s| !s.is_empty())
24+
std::env::var(var).ok().and_then(|raw| normalize_env(&raw))
25+
}
26+
27+
/// Trim whitespace and a trailing `/` so callers can rely on host / URL values being free of
28+
/// surface-level user typos like `GH_API_URL=https://ghe.example.com/api/v3/`. Returns `None`
29+
/// when the value collapses to empty after normalization so the caller can fall back to the
30+
/// default.
31+
fn normalize_env(raw: &str) -> Option<String> {
32+
let v = raw.trim().trim_end_matches('/');
33+
(!v.is_empty()).then(|| v.to_owned())
2634
}
2735

2836
pub(crate) fn host() -> &'static str {
@@ -73,6 +81,7 @@ pub(crate) fn api_repos_prefix() -> &'static str {
7381
static API_REPOS_PREFIX: OnceLock<String> = OnceLock::new();
7482
API_REPOS_PREFIX.get_or_init(|| format!("{}/repos/", api_base()))
7583
}
84+
7685
const ACCEPT: &str = "application/vnd.github+json";
7786
const API_VERSION: &str = "2022-11-28";
7887
const REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
@@ -270,6 +279,34 @@ mod tests {
270279
}
271280
}
272281

282+
#[test]
283+
fn normalize_env_strips_trailing_slash() {
284+
assert_eq!(
285+
normalize_env("https://ghe.example.com/api/v3/").as_deref(),
286+
Some("https://ghe.example.com/api/v3")
287+
);
288+
}
289+
290+
#[test]
291+
fn normalize_env_handles_whitespace_and_empty() {
292+
assert_eq!(
293+
normalize_env(" github.com ").as_deref(),
294+
Some("github.com")
295+
);
296+
assert_eq!(normalize_env(" ").as_deref(), None);
297+
assert_eq!(normalize_env("").as_deref(), None);
298+
assert_eq!(normalize_env("/").as_deref(), None);
299+
}
300+
301+
#[test]
302+
fn normalize_env_preserves_clean_values() {
303+
assert_eq!(
304+
normalize_env("https://ghe.example.com/api/v3").as_deref(),
305+
Some("https://ghe.example.com/api/v3")
306+
);
307+
assert_eq!(normalize_env("github.com").as_deref(), Some("github.com"));
308+
}
309+
273310
#[test]
274311
fn resolve_token_prefers_gh_token() {
275312
let _g = EnvGuard::set(&[

0 commit comments

Comments
 (0)