Skip to content

controllers::krate::publish: Move reading of tarball up from uploader to publish #4096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2021
Merged
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
64 changes: 58 additions & 6 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Functionality related to publishing a new crate or version of a crate.

use flate2::read::GzDecoder;
use hex::ToHex;
use sha2::{Digest, Sha256};
use std::io::Read;
use std::sync::Arc;
use swirl::Job;

Expand All @@ -14,7 +17,7 @@ use crate::models::{
use crate::render;
use crate::schema::*;
use crate::util::errors::{cargo_err, AppResult};
use crate::util::{read_fill, read_le_u32, Maximums};
use crate::util::{read_fill, read_le_u32, LimitErrorReader, Maximums};
use crate::views::{
EncodableCrate, EncodableCrateDependency, EncodableCrateUpload, GoodCrate, PublishWarnings,
};
Expand Down Expand Up @@ -186,6 +189,12 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
let ignored_invalid_badges = Badge::update_crate(&conn, &krate, new_crate.badges.as_ref())?;
let top_versions = krate.top_versions(&conn)?;

// Read tarball from request
let mut tarball = Vec::new();
LimitErrorReader::new(req.body(), maximums.max_upload_size).read_to_end(&mut tarball)?;
let hex_cksum: String = Sha256::digest(&tarball).encode_hex();
verify_tarball(&krate, vers, &tarball, maximums.max_unpack_size)?;

if let Some(readme) = new_crate.readme {
render::render_and_upload_readme(
version.id,
Expand All @@ -198,12 +207,10 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
.enqueue(&conn)?;
}

let cksum = app
.config
// Upload crate tarball
app.config
.uploader()
.upload_crate(req, &krate, maximums, vers)?;

let hex_cksum = cksum.encode_hex::<String>();
.upload_crate(&app, tarball, &krate, vers)?;

// Register this crate in our local git repo.
let git_crate = git::Crate {
Expand Down Expand Up @@ -356,6 +363,51 @@ pub fn add_dependencies(
Ok(git_deps)
}

fn verify_tarball(
krate: &Crate,
vers: &semver::Version,
tarball: &[u8],
max_unpack: u64,
) -> AppResult<()> {
// All our data is currently encoded with gzip
let decoder = GzDecoder::new(tarball);

// Don't let gzip decompression go into the weeeds, apply a fixed cap after
// which point we say the decompressed source is "too large".
let decoder = LimitErrorReader::new(decoder, max_unpack);

// Use this I/O object now to take a peek inside
let mut archive = tar::Archive::new(decoder);
let prefix = format!("{}-{}", krate.name, vers);
for entry in archive.entries()? {
let entry = entry.map_err(|err| {
err.chain(cargo_err(
"uploaded tarball is malformed or too large when decompressed",
))
})?;

// Verify that all entries actually start with `$name-$vers/`.
// Historically Cargo didn't verify this on extraction so you could
// upload a tarball that contains both `foo-0.1.0/` source code as well
// as `bar-0.1.0/` source code, and this could overwrite other crates in
// the registry!
if !entry.path()?.starts_with(&prefix) {
return Err(cargo_err("invalid tarball uploaded"));
}

// Historical versions of the `tar` crate which Cargo uses internally
// don't properly prevent hard links and symlinks from overwriting
// arbitrary files on the filesystem. As a bit of a hammer we reject any
// tarball with these sorts of links. Cargo doesn't currently ever
// generate a tarball with these file types so this should work for now.
let entry_type = entry.header().entry_type();
if entry_type.is_hard_link() || entry_type.is_symlink() {
return Err(cargo_err("invalid tarball uploaded"));
}
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::missing_metadata_error_message;
Expand Down
68 changes: 7 additions & 61 deletions src/uploaders.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use anyhow::Result;
use conduit::RequestExt;
use flate2::read::GzDecoder;
use reqwest::{blocking::Client, header};
use sha2::{Digest, Sha256};

use crate::util::errors::{cargo_err, internal, AppError, AppResult};
use crate::util::{LimitErrorReader, Maximums};
use crate::app::App;
use crate::util::errors::{internal, AppResult};

use std::env;
use std::fs::{self, File};
use std::io::{Cursor, Read};
use std::io::Cursor;
use std::sync::Arc;

use crate::middleware::app::RequestApp;
use crate::models::Crate;

const CACHE_CONTROL_IMMUTABLE: &str = "public,max-age=31536000,immutable";
Expand Down Expand Up @@ -129,17 +125,12 @@ impl Uploader {
/// Uploads a crate and returns the checksum of the uploaded crate file.
pub fn upload_crate(
&self,
req: &mut dyn RequestExt,
app: &Arc<App>,
body: Vec<u8>,
krate: &Crate,
maximums: Maximums,
vers: &semver::Version,
) -> AppResult<[u8; 32]> {
let app = Arc::clone(req.app());
) -> AppResult<()> {
let path = Uploader::crate_path(&krate.name, &vers.to_string());
let mut body = Vec::new();
LimitErrorReader::new(req.body(), maximums.max_upload_size).read_to_end(&mut body)?;
verify_tarball(krate, vers, &body, maximums.max_unpack_size)?;
let checksum = Sha256::digest(&body);
let content_length = body.len() as u64;
let content = Cursor::new(body);
let mut extra_headers = header::HeaderMap::new();
Expand All @@ -156,7 +147,7 @@ impl Uploader {
extra_headers,
)
.map_err(|e| internal(&format_args!("failed to upload crate: {}", e)))?;
Ok(checksum.into())
Ok(())
}

pub(crate) fn upload_readme(
Expand Down Expand Up @@ -185,48 +176,3 @@ impl Uploader {
Ok(())
}
}

fn verify_tarball(
krate: &Crate,
vers: &semver::Version,
tarball: &[u8],
max_unpack: u64,
) -> AppResult<()> {
// All our data is currently encoded with gzip
let decoder = GzDecoder::new(tarball);

// Don't let gzip decompression go into the weeeds, apply a fixed cap after
// which point we say the decompressed source is "too large".
let decoder = LimitErrorReader::new(decoder, max_unpack);

// Use this I/O object now to take a peek inside
let mut archive = tar::Archive::new(decoder);
let prefix = format!("{}-{}", krate.name, vers);
for entry in archive.entries()? {
let entry = entry.map_err(|err| {
err.chain(cargo_err(
"uploaded tarball is malformed or too large when decompressed",
))
})?;

// Verify that all entries actually start with `$name-$vers/`.
// Historically Cargo didn't verify this on extraction so you could
// upload a tarball that contains both `foo-0.1.0/` source code as well
// as `bar-0.1.0/` source code, and this could overwrite other crates in
// the registry!
if !entry.path()?.starts_with(&prefix) {
return Err(cargo_err("invalid tarball uploaded"));
}

// Historical versions of the `tar` crate which Cargo uses internally
// don't properly prevent hard links and symlinks from overwriting
// arbitrary files on the filesystem. As a bit of a hammer we reject any
// tarball with these sorts of links. Cargo doesn't currently ever
// generate a tarball with these file types so this should work for now.
let entry_type = entry.header().entry_type();
if entry_type.is_hard_link() || entry_type.is_symlink() {
return Err(cargo_err("invalid tarball uploaded"));
}
}
Ok(())
}