Skip to content

Commit 4b24c53

Browse files
committed
controllers::krate::publish: extract pkg_name from verify_tarball
Will make it easier to test.
1 parent 896515f commit 4b24c53

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

src/controllers/krate/publish.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
192192
let mut tarball = Vec::new();
193193
LimitErrorReader::new(req.body(), maximums.max_upload_size).read_to_end(&mut tarball)?;
194194
let hex_cksum: String = Sha256::digest(&tarball).encode_hex();
195-
verify_tarball(&krate, vers, &tarball, maximums.max_unpack_size)?;
195+
let pkg_name = format!("{}-{}", krate.name, vers);
196+
verify_tarball(&pkg_name, &tarball, maximums.max_unpack_size)?;
196197

197198
let pkg_path_in_vcs = None;
198199

@@ -365,12 +366,7 @@ pub fn add_dependencies(
365366
Ok(git_deps)
366367
}
367368

368-
fn verify_tarball(
369-
krate: &Crate,
370-
vers: &semver::Version,
371-
tarball: &[u8],
372-
max_unpack: u64,
373-
) -> AppResult<()> {
369+
fn verify_tarball(pkg_name: &str, tarball: &[u8], max_unpack: u64) -> AppResult<()> {
374370
// All our data is currently encoded with gzip
375371
let decoder = GzDecoder::new(tarball);
376372

@@ -380,7 +376,6 @@ fn verify_tarball(
380376

381377
// Use this I/O object now to take a peek inside
382378
let mut archive = tar::Archive::new(decoder);
383-
let prefix = format!("{}-{}", krate.name, vers);
384379
for entry in archive.entries()? {
385380
let entry = entry.map_err(|err| {
386381
err.chain(cargo_err(
@@ -393,7 +388,7 @@ fn verify_tarball(
393388
// upload a tarball that contains both `foo-0.1.0/` source code as well
394389
// as `bar-0.1.0/` source code, and this could overwrite other crates in
395390
// the registry!
396-
if !entry.path()?.starts_with(&prefix) {
391+
if !entry.path()?.starts_with(&pkg_name) {
397392
return Err(cargo_err("invalid tarball uploaded"));
398393
}
399394

0 commit comments

Comments
 (0)