Skip to content

Commit 9ef2dd1

Browse files
committed
Prevent invalid tokens from breaking HTTP header syntax
1 parent bf825dc commit 9ef2dd1

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/cargo/core/package.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crates_io::check_token;
12
use std::cell::{Cell, Ref, RefCell, RefMut};
23
use std::cmp::Ordering;
34
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
@@ -743,6 +744,7 @@ impl<'a, 'gctx> Downloads<'a, 'gctx> {
743744
// Add authorization header.
744745
if let Some(authorization) = authorization {
745746
let mut headers = curl::easy::List::new();
747+
check_token(&authorization)?;
746748
headers.append(&format!("Authorization: {}", authorization))?;
747749
handle.http_headers(headers)?;
748750
}

src/cargo/sources/registry/http_remote.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::util::{auth, Filesystem, GlobalContext, IntoUrl, Progress, ProgressSt
1515
use anyhow::Context as _;
1616
use cargo_credential::Operation;
1717
use cargo_util::paths;
18+
use crates_io::check_token;
1819
use curl::easy::{Easy, List};
1920
use curl::multi::{EasyHandle, Multi};
2021
use std::cell::RefCell;
@@ -663,6 +664,7 @@ impl<'gctx> RegistryData for HttpRegistry<'gctx> {
663664
self.auth_error_headers.clone(),
664665
true,
665666
)?;
667+
check_token(&authorization)?;
666668
headers.append(&format!("Authorization: {}", authorization))?;
667669
trace!(target: "network", "including authorization for {}", full_url);
668670
}

tests/testsuite/registry_auth.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,34 @@ Caused by:
390390
.run();
391391
}
392392

393+
#[cargo_test]
394+
fn syntactically_invalid_token() {
395+
let _registry = RegistryBuilder::new()
396+
.alternative()
397+
.auth_required()
398+
.no_configure_token()
399+
.http_index()
400+
.build();
401+
402+
let p = make_project();
403+
cargo(&p, "build")
404+
.env("CARGO_REGISTRIES_ALTERNATIVE_TOKEN", "\t\n悪")
405+
.with_status(101)
406+
.with_stderr_data(str![[r#"
407+
[UPDATING] `alternative` index
408+
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.1 ([ROOT]/foo)`
409+
410+
Caused by:
411+
Token for registry `alternative` is invalid (defined in environment variable `CARGO_REGISTRIES_ALTERNATIVE_TOKEN`)
412+
413+
Caused by:
414+
token contains invalid characters.
415+
Only printable ASCII characters are allowed as it is sent in a HTTPS header.
416+
417+
"#]])
418+
.run();
419+
}
420+
393421
#[cargo_test]
394422
fn incorrect_token_git() {
395423
let _registry = RegistryBuilder::new()

0 commit comments

Comments
 (0)