Skip to content

Commit 3e51d9c

Browse files
committed
models/version: Move license validation into publish endpoint
1 parent 14f961b commit 3e51d9c

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

src/controllers/krate/publish.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::models::{
1919

2020
use crate::middleware::log_request::RequestLogExt;
2121
use crate::models::token::EndpointScope;
22+
use crate::models::version::validate_license_expr;
2223
use crate::rate_limiter::LimitedAction;
2324
use crate::schema::*;
2425
use crate::sql::canon_crate_name;
@@ -44,7 +45,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
4445
let (req, bytes) = req.0.into_parts();
4546
let (json_bytes, tarball_bytes) = split_body(bytes)?;
4647

47-
let metadata: PublishMetadata = serde_json::from_slice(&json_bytes)
48+
let mut metadata: PublishMetadata = serde_json::from_slice(&json_bytes)
4849
.map_err(|e| cargo_err(&format_args!("invalid upload request: {e}")))?;
4950

5051
let request_log = req.request_log();
@@ -70,6 +71,15 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
7071
return Err(cargo_err(&message));
7172
}
7273

74+
if let Some(ref license) = metadata.license {
75+
validate_license_expr(license)?;
76+
} else if metadata.license_file.is_some() {
77+
// If no license is given, but a license file is given, flag this
78+
// crate as having a nonstandard license. Note that we don't
79+
// actually do anything else with license_file currently.
80+
metadata.license = Some(String::from("non-standard"));
81+
}
82+
7383
conduit_compat(move || {
7484
let conn = &mut *app.db_write()?;
7585

src/models/version.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl NewVersion {
139139
num: &semver::Version,
140140
features: &BTreeMap<String, Vec<String>>,
141141
license: Option<String>,
142-
license_file: Option<&str>,
142+
#[allow(unused_variables)] license_file: Option<&str>,
143143
crate_size: i32,
144144
published_by: i32,
145145
checksum: String,
@@ -148,7 +148,7 @@ impl NewVersion {
148148
) -> AppResult<Self> {
149149
let features = serde_json::to_value(features)?;
150150

151-
let mut new_version = NewVersion {
151+
Ok(NewVersion {
152152
crate_id,
153153
num: num.to_string(),
154154
features,
@@ -158,11 +158,7 @@ impl NewVersion {
158158
checksum,
159159
links,
160160
rust_version,
161-
};
162-
163-
new_version.validate_license(license_file)?;
164-
165-
Ok(new_version)
161+
})
166162
}
167163

168164
pub fn save(&self, conn: &mut PgConnection, published_by_email: &str) -> AppResult<Version> {
@@ -195,18 +191,6 @@ impl NewVersion {
195191
Ok(version)
196192
})
197193
}
198-
199-
fn validate_license(&mut self, license_file: Option<&str>) -> AppResult<()> {
200-
if let Some(ref license) = self.license {
201-
validate_license_expr(license)?;
202-
} else if license_file.is_some() {
203-
// If no license is given, but a license file is given, flag this
204-
// crate as having a nonstandard license. Note that we don't
205-
// actually do anything else with license_file currently.
206-
self.license = Some(String::from("non-standard"));
207-
}
208-
Ok(())
209-
}
210194
}
211195

212196
pub fn validate_license_expr(s: &str) -> AppResult<()> {

0 commit comments

Comments
 (0)