Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ jobs:
run: python scripts/release/synthetic_onnx_fixture.py --output-dir "${{ runner.temp }}/native-synthetic-fixture"
- name: Verify untrained model lifecycle on ONNX Runtime installed by the packaged Pumas
timeout-minutes: 20
env:
PUMAS_GITHUB_TOKEN: ${{ github.token }}
run: python scripts/release/verify-packaged-onnx.py "${{ runner.temp }}/inference-candidate/extracted/${{ matrix.binary }}" "${{ runner.temp }}/native-synthetic-fixture" > "${{ runner.temp }}/inference-candidate/native-synthetic-lifecycle.log"
- name: Verify required inference archive
run: node scripts/release/check-artifacts.mjs "${{ runner.temp }}/inference-candidate" headless-inference-${{ matrix.check }}
Expand Down Expand Up @@ -359,6 +361,7 @@ jobs:
timeout-minutes: 20
env:
EXPECTED_SOURCE_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
PUMAS_GITHUB_TOKEN: ${{ github.token }}
shell: bash
run: |
set -euo pipefail
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

### Added

- Pumas reads an optional `PUMAS_GITHUB_TOKEN` and sends it only to the
GitHub API (`api.github.com`) when listing or resolving runtime releases,
which lifts the anonymous rate limit. Pumas never stores the token, never
logs it, and never passes it to runtimes or other child processes. Without
it, requests stay anonymous as before. CI passes the job's short-lived
`github.token`, so no credential is committed.

### Fixed

- A copied import no longer reaches the file watcher as an external change,
Expand Down
121 changes: 101 additions & 20 deletions rust/crates/pumas-core/src/network/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,44 @@ use url::Url;
// Re-export for convenience
pub use crate::models::{GitHubAsset, GitHubRelease};

/// Optional token for GitHub API requests. It raises the anonymous rate limit
/// and is never stored by Pumas; an unset or empty variable means anonymous.
pub const GITHUB_API_TOKEN_ENV: &str = "PUMAS_GITHUB_TOKEN";

/// A GitHub API token. It is sent only to the GitHub API origin and never
/// appears in `Debug` output, logs or errors.
#[derive(Clone)]
struct GitHubApiToken(String);

impl std::fmt::Debug for GitHubApiToken {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("GitHubApiToken(<redacted>)")
}
}

impl GitHubApiToken {
/// Accept a token only for the real GitHub API origin, and only when it
/// is a single header-safe word. Anything else stays anonymous.
fn for_api_base(api_base: &str, raw: Option<std::ffi::OsString>) -> Option<Self> {
if api_base.trim_end_matches('/') != NetworkConfig::GITHUB_API_BASE {
return None;
}
let raw = raw?;
let Some(value) = raw.to_str().map(str::trim) else {
warn!("{GITHUB_API_TOKEN_ENV} is not valid UTF-8; using anonymous GitHub API requests");
return None;
};
if value.is_empty() {
return None;
}
if !value.bytes().all(|byte| byte.is_ascii_graphic()) {
warn!("{GITHUB_API_TOKEN_ENV} contains whitespace or control characters; using anonymous GitHub API requests");
return None;
}
Some(Self(value.to_string()))
}
}

#[derive(serde::Serialize, serde::Deserialize)]
struct StoredReleasesCache {
#[serde(flatten)]
Expand Down Expand Up @@ -507,6 +545,7 @@ pub struct GitHubClient {
loopback_fixture: bool,
http: Arc<HttpClient>,
api_base: String,
api_token: Option<GitHubApiToken>,
cache: ReleasesCache,
/// Whether we're currently fetching releases.
is_fetching: AtomicBool,
Expand Down Expand Up @@ -608,12 +647,23 @@ impl GitHubClient {
}

fn with_config(cache_dir: PathBuf, ttl: Duration, api_base: String) -> Result<Self> {
Ok(Self::with_http(
HttpClient::new()?,
cache_dir,
ttl,
api_base,
))
let api_token =
GitHubApiToken::for_api_base(&api_base, std::env::var_os(GITHUB_API_TOKEN_ENV));
let mut client = Self::with_http(HttpClient::new()?, cache_dir, ttl, api_base);
client.api_token = api_token;
Ok(client)
}

/// Headers for a request to the GitHub API origin.
fn api_headers(&self) -> Vec<(String, String)> {
let mut headers = vec![(
"Accept".to_string(),
"application/vnd.github.v3+json".to_string(),
)];
if let Some(GitHubApiToken(token)) = &self.api_token {
headers.push(("Authorization".to_string(), format!("Bearer {token}")));
}
headers
}

fn with_http(http: HttpClient, cache_dir: PathBuf, ttl: Duration, api_base: String) -> Self {
Expand All @@ -622,6 +672,7 @@ impl GitHubClient {
loopback_fixture: false,
http: Arc::new(http),
api_base: api_base.trim_end_matches('/').to_string(),
api_token: None,
cache: ReleasesCache::new(cache_dir, ttl),
is_fetching: AtomicBool::new(false),
fetch_lock: RwLock::new(()),
Expand Down Expand Up @@ -1005,19 +1056,15 @@ impl GitHubClient {
.with_base_delay(Duration::from_secs(2));
let http = self.http.clone();
let url_string = url.to_string();
let headers = self.api_headers();

let (result, stats) = retry_async(
&retry_config,
|| {
let http = http.clone();
let url = url_string.clone();
async move {
let headers = vec![(
"Accept".to_string(),
"application/vnd.github.v3+json".to_string(),
)];
http.get_with_headers(&url, &headers).await
}
let headers = headers.clone();
async move { http.get_with_headers(&url, &headers).await }
},
|error| error.is_retryable(),
)
Expand Down Expand Up @@ -1207,19 +1254,15 @@ impl GitHubClient {

let http = self.http.clone();
let url_clone = url.clone();
let headers = self.api_headers();

let (result, stats) = retry_async(
&retry_config,
|| {
let http = http.clone();
let url = url_clone.clone();
async move {
let headers = vec![(
"Accept".to_string(),
"application/vnd.github.v3+json".to_string(),
)];
http.get_with_headers(&url, &headers).await
}
let headers = headers.clone();
async move { http.get_with_headers(&url, &headers).await }
},
|e| e.is_retryable(),
)
Expand Down Expand Up @@ -1304,6 +1347,44 @@ impl WebSource for GitHubClient {

#[cfg(test)]
mod tests {
#[test]
fn api_token_applies_only_to_the_github_api_origin() {
use super::GitHubApiToken;
let token = Some(std::ffi::OsString::from(" ghs_example123 "));
let accepted = GitHubApiToken::for_api_base("https://api.github.com/", token.clone())
.expect("token for the GitHub API");
assert_eq!(accepted.0, "ghs_example123");
assert_eq!(format!("{accepted:?}"), "GitHubApiToken(<redacted>)");
assert!(GitHubApiToken::for_api_base("http://127.0.0.1:9/api", token).is_none());
for rejected in ["", " ", "two words", "line\nbreak"] {
assert!(
GitHubApiToken::for_api_base("https://api.github.com", Some(rejected.into()))
.is_none(),
"{rejected:?}"
);
}
assert!(GitHubApiToken::for_api_base("https://api.github.com", None).is_none());
}

#[test]
fn api_headers_carry_the_token_only_when_present() {
let root = TempDir::new().unwrap();
let mut client = GitHubClient::with_http(
HttpClient::new().unwrap(),
root.path().to_path_buf(),
Duration::from_secs(60),
"https://api.github.com".into(),
);
assert!(client
.api_headers()
.iter()
.all(|(name, _)| name != "Authorization"));
client.api_token = Some(super::GitHubApiToken("ghs_example".into()));
assert!(client
.api_headers()
.contains(&("Authorization".into(), "Bearer ghs_example".into())));
}

use super::*;
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};
use tempfile::TempDir;
Expand Down
1 change: 1 addition & 0 deletions rust/crates/pumas-core/src/platform/child_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ mod tests {
"PYTHONSTARTUP",
"HF_TOKEN",
"PUMAS_RELEASE_API_TOKEN",
"PUMAS_GITHUB_TOKEN",
"AWS_SECRET_ACCESS_KEY",
]
.map(|name| (OsString::from(name), OsString::from("value")));
Expand Down
6 changes: 6 additions & 0 deletions scripts/release/packaged_onnx_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ def child_environment(root):
WINDIR=buffer.value,
PATH=str(Path(buffer.value) / "System32"),
)
# The GitHub API token is the one deliberate pass-through: Pumas sends it
# only to the GitHub API, and it lifts the shared runner's anonymous
# rate limit on the ONNX Runtime release listing.
token = os.environ.get("PUMAS_GITHUB_TOKEN")
if token:
environment["PUMAS_GITHUB_TOKEN"] = token
return environment


Expand Down
10 changes: 10 additions & 0 deletions scripts/release/test_inference_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@


class ReleaseTests(unittest.TestCase):
def test_child_environment_passes_only_the_github_api_token(self):
ambient = {"PUMAS_GITHUB_TOKEN": "ghs_fixture", "GITHUB_TOKEN": "x", "HF_TOKEN": "y"}
with patch.dict(os.environ, ambient):
environment = support.child_environment(tempfile.gettempdir())
self.assertEqual(environment["PUMAS_GITHUB_TOKEN"], "ghs_fixture")
self.assertNotIn("GITHUB_TOKEN", environment)
self.assertNotIn("HF_TOKEN", environment)
with patch.dict(os.environ, {"PUMAS_GITHUB_TOKEN": ""}):
self.assertNotIn("PUMAS_GITHUB_TOKEN", support.child_environment(tempfile.gettempdir()))

def test_only_shared_libraries_count_as_a_mapped_onnx_runtime(self):
# ONNX Runtime 1.31 maps its own cache database; it is not a library.
for name, expected in [
Expand Down
Loading