use super::*;
#[test]
fn test_token_source_memo_matches_only_the_supplying_token_and_host() {
let host = "github-source-test.example.com";
remember_token_source(host, "ghp_test", TokenSource::EnvVar("GITHUB_TOKEN"));
assert_eq!(
token_source_for_token(host, "ghp_test"),
Some(TokenSource::EnvVar("GITHUB_TOKEN"))
);
assert_eq!(token_source_for_token(host, "from-netrc"), None);
assert_eq!(
token_source_for_token("another-github.example.com", "ghp_test"),
None
);
}
#[test]
fn test_get_headers_remembers_tokens_file_source() {
let _lock = crate::testing::lock_ignoring_poison(&TEST_ENV_LOCK);
let host = "github-tokens-file-test.example.com";
let _tokens_file = TokensFileOverrideGuard::set(host, "ghp_from_tokens_file");
let api_url = format!("https://{host}/api/v3");
let headers = get_headers(format!("{api_url}/repos/owner/repo/releases"), &api_url).unwrap();
assert_eq!(
headers.get(reqwest::header::AUTHORIZATION).unwrap(),
"Bearer ghp_from_tokens_file"
);
assert_eq!(
token_source_for_token(host, "ghp_from_tokens_file"),
Some(TokenSource::TokensFile)
);
}
#[test]
fn test_get_headers_rejects_token_with_invalid_header_character() {
let _lock = crate::testing::lock_ignoring_poison(&TEST_ENV_LOCK);
let host = "github-bad-token-test.example.com";
let _tokens_file = TokensFileOverrideGuard::set(host, "ghp_bad\nsecret_value");
let api_url = format!("https://{host}/api/v3");
let err = get_headers(format!("{api_url}/repos/owner/repo/releases"), &api_url)
.expect_err("a token that cannot be a header value must be an error, not a panic");
let msg = err.to_string();
assert!(msg.contains("invalid GitHub token"), "{msg}");
assert!(
!msg.contains("secret_value"),
"token leaked into error: {msg}"
);
}
#[test]
fn test_get_headers_ignores_invalid_token_for_other_hosts() {
let _lock = crate::testing::lock_ignoring_poison(&TEST_ENV_LOCK);
let _tokens_file =
TokensFileOverrideGuard::set("github-bad-token-other.example.com", "ghp_bad\ntoken");
let headers = get_headers("https://downloads.example.com/tool.tar.gz", API_URL)
.expect("a non-API URL builds no auth header");
assert!(!headers.contains_key(reqwest::header::AUTHORIZATION));
}
#[test]
fn test_parse_github_tokens() {
let toml = r#"
[tokens."github.com"]
token = "ghp_abc123"
[tokens."github.mycompany.com"]
token = "ghp_def456"
"#;
let result = parse_github_tokens(toml).unwrap();
assert_eq!(result.get("github.com").unwrap(), "ghp_abc123");
assert_eq!(result.get("github.mycompany.com").unwrap(), "ghp_def456");
}
#[test]
fn test_parse_github_tokens_empty() {
assert!(parse_github_tokens("").is_none());
}
#[test]
fn test_parse_github_tokens_empty_tokens() {
let toml = "[tokens]\n";
let result = parse_github_tokens(toml).unwrap();
assert!(result.is_empty());
}
#[test]
fn test_parse_github_tokens_missing_token_field() {
let toml = r#"
[tokens."github.com"]
something_else = "value"
"#;
let result = parse_github_tokens(toml).unwrap();
assert!(result.is_empty());
}
#[test]
fn test_api_host_token_lookup_hosts() {
assert_eq!(
token_lookup_hosts("api.github.com"),
vec!["github.com", "api.github.com"]
);
assert_eq!(
token_lookup_hosts("api.octocorp.ghe.com"),
vec!["octocorp.ghe.com", "api.octocorp.ghe.com"]
);
assert_eq!(
token_lookup_hosts("github.example.com"),
vec!["github.example.com"]
);
}
#[test]
fn test_enterprise_token_is_not_sent_to_public_raw_content() {
let _guard = GithubTokenGuard::new();
env::remove_var("GITHUB_TOKEN");
env::set_var("MISE_GITHUB_ENTERPRISE_TOKEN", "ghes-secret");
for host in ["raw.githubusercontent.com", "github.com", "api.github.com"] {
if let Some((token, _)) = resolve_token_inner(host, false) {
assert_ne!(
token, "ghes-secret",
"{host} must not receive MISE_GITHUB_ENTERPRISE_TOKEN"
);
}
}
}
#[test]
fn test_raw_githubusercontent_over_http_gets_no_token() {
with_github_token(|| {
let headers = get_headers(
"http://raw.githubusercontent.com/owner/repo/main/file.txt",
API_URL,
)
.unwrap();
assert!(
!headers.contains_key(reqwest::header::AUTHORIZATION),
"an http raw-content URL must not carry the token"
);
});
}
#[test]
fn test_raw_githubusercontent_uses_github_token() {
with_github_token(|| {
let headers = get_headers(
"https://raw.githubusercontent.com/owner/repo/main/file.txt",
API_URL,
)
.unwrap();
assert!(
headers.contains_key(reqwest::header::AUTHORIZATION),
"raw.githubusercontent.com should carry the github.com token"
);
assert!(
!headers.contains_key("x-github-api-version"),
"raw.githubusercontent.com is not an API host"
);
});
}
#[test]
fn test_only_github_api_urls_use_github_token() {
with_github_token(|| {
for url in [
"https://github.com/api/v3/repos/owner/repo/releases",
"https://github.com/cuotos/ecs-exec-pf/releases/download/v0.3.0/ecs-exec-pf_0.3.0_Linux_x86_64.tar.gz",
"https://github.example.com/owner/repo/releases/download/v1.0.0/file.tar.gz",
"https://objects.githubusercontent.com/github-production-release-asset",
"https://objects-origin.githubusercontent.com/github-production-release-asset",
"https://release-assets.githubusercontent.com/github-production-release-asset",
"https://octocorp.ghe.com/api/v3/repos/owner/repo/releases",
"https://octocorp.ghe.com/owner/repo/releases/download/v1.0.0/file.tar.gz",
] {
let headers = get_headers(url, API_URL).unwrap();
assert!(
!headers.contains_key(reqwest::header::AUTHORIZATION),
"{url} should not use GitHub auth"
);
assert!(
!headers.contains_key("x-github-api-version"),
"{url} should not use GitHub API version"
);
}
let headers =
get_headers("https://api.github.com/repos/owner/repo/releases", API_URL).unwrap();
assert!(headers.contains_key(reqwest::header::AUTHORIZATION));
assert!(headers.contains_key("x-github-api-version"));
let headers = get_headers(
"https://api.github.com/repos/owner/repo/releases/assets/1",
API_URL,
)
.unwrap();
assert!(headers.contains_key(reqwest::header::AUTHORIZATION));
assert_eq!(headers.get("accept").unwrap(), "application/octet-stream");
let headers = get_headers(
"https://github.example.com/api/v3/repos/owner/repo/releases",
"https://github.example.com/api/v3",
)
.unwrap();
assert!(headers.contains_key(reqwest::header::AUTHORIZATION));
assert!(headers.contains_key("x-github-api-version"));
let headers = get_headers(
"https://api.octocorp.ghe.com/repos/owner/repo/releases",
API_URL,
)
.unwrap();
assert!(headers.contains_key(reqwest::header::AUTHORIZATION));
assert!(headers.contains_key("x-github-api-version"));
});
}
#[test]
fn test_enterprise_api_path_gets_token_only_at_api_url_origin() {
with_github_token(|| {
env::set_var("MISE_GITHUB_ENTERPRISE_TOKEN", "ghes-secret");
let api_url = "https://github.example.com/api/v3";
for (url, api_url) in [
("https://attacker.example/api/v3/checksums.txt", API_URL),
("https://attacker.example/api/v3/checksums.txt", api_url),
("http://github.example.com/api/v3/repos/o/r/tags", api_url),
("https://github.example.com:8443/api/v3/repos/o/r", api_url),
("https://github.example.com/api/v3/repos/o/r/tags", API_URL),
("http://api.github.com/repos/o/r/tags", API_URL),
("http://api.octocorp.ghe.com/repos/o/r/tags", API_URL),
(
"http://github.example.com/api/v3/repos/o/r/tags",
"http://github.example.com/api/v3",
),
] {
let headers = get_headers(url, api_url).unwrap();
assert!(
!headers.contains_key(reqwest::header::AUTHORIZATION),
"{url} should not use GitHub auth with api_url {api_url}"
);
}
for url in [
"https://attacker.example/api/v3/lint.sh",
"http://attacker.example:8080/api/v3/repos/o/r/releases",
] {
let headers = crate::http::host_auth_headers(&url::Url::parse(url).unwrap()).unwrap();
assert!(
!headers.contains_key(reqwest::header::AUTHORIZATION),
"{url} should not get the GitHub token from the generic client"
);
}
let headers = get_headers(format!("{api_url}/repos/o/r/tags?page=2"), api_url).unwrap();
assert!(headers.contains_key(reqwest::header::AUTHORIZATION));
});
}
#[test]
fn test_enterprise_hosts_setting_extends_token_hosts() {
let listed = vec!["GitHub.MyCompany.com".to_string()];
let ok = |url: &str| may_send_token_with(&url::Url::parse(url).unwrap(), API_URL, &listed);
assert!(ok("https://github.mycompany.com/api/v3/repos/o/r/releases"));
assert!(ok("https://github.mycompany.com:8443/api/v3/repos/o/r"));
assert!(!ok("http://github.mycompany.com/api/v3/repos/o/r"));
assert!(!ok(
"https://github.mycompany.com/o/r/releases/download/v1/x"
));
assert!(!ok("https://other.example.com/api/v3/repos/o/r"));
assert!(!ok("https://github.mycompany.com.evil.example/api/v3/x"));
assert!(!may_send_token_with(
&url::Url::parse("https://github.mycompany.com/api/v3/repos/o/r").unwrap(),
API_URL,
&[]
));
}
#[test]
fn test_get_headers_rejects_relative_url() {
let err = get_headers("/repos/jdx/aube/releases", API_URL).unwrap_err();
assert!(
err.to_string()
.contains("invalid request URL for GitHub auth headers"),
"unexpected error: {err}"
);
}