Skip to content

Commit 482ed93

Browse files
authored
Merge pull request #7195 from Turbo87/license-validation
models/version: Move license validation into publish endpoint
2 parents 5bcc19b + 7d63cc7 commit 482ed93

File tree

6 files changed

+15
-30
lines changed

6 files changed

+15
-30
lines changed

src/controllers/krate/publish.rs

Lines changed: 11 additions & 4 deletions
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;
@@ -115,7 +116,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
115116
let package = tarball_info.manifest.package.unwrap();
116117

117118
let description = package.description.map(|it| it.as_local().unwrap());
118-
let license = package.license.map(|it| it.as_local().unwrap());
119+
let mut license = package.license.map(|it| it.as_local().unwrap());
119120
let license_file = package.license_file.map(|it| it.as_local().unwrap());
120121

121122
// Make sure required fields are provided
@@ -136,6 +137,15 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
136137
return Err(cargo_err(&message));
137138
}
138139

140+
if let Some(ref license) = license {
141+
validate_license_expr(license)?;
142+
} else if license_file.is_some() {
143+
// If no license is given, but a license file is given, flag this
144+
// crate as having a nonstandard license. Note that we don't
145+
// actually do anything else with license_file currently.
146+
license = Some(String::from("non-standard"));
147+
}
148+
139149
// Create a transaction on the database, if there are no errors,
140150
// commit the transactions to record a new or updated crate.
141151
conn.transaction(|conn| {
@@ -170,8 +180,6 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
170180
max_upload_size: None,
171181
};
172182

173-
let license_file = license_file.as_deref();
174-
175183
validate_url(persist.homepage, "homepage")?;
176184
validate_url(persist.documentation, "documentation")?;
177185
validate_url(persist.repository, "repository")?;
@@ -219,7 +227,6 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
219227
vers,
220228
&features,
221229
license,
222-
license_file,
223230
// Downcast is okay because the file length must be less than the max upload size
224231
// to get here, and max upload sizes are way less than i32 max
225232
content_length as i32,

src/downloads_counter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ mod tests {
455455
&Version::parse(&format!("{}.0.0", self.next_version)).unwrap(),
456456
&BTreeMap::new(),
457457
None,
458-
None,
459458
0,
460459
self.user.id,
461460
"0000000000000000000000000000000000000000000000000000000000000000".to_string(),

src/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ mod rights;
3030
mod team;
3131
pub mod token;
3232
pub mod user;
33-
mod version;
33+
pub mod version;

src/models/version.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ impl NewVersion {
139139
num: &semver::Version,
140140
features: &BTreeMap<String, Vec<String>>,
141141
license: Option<String>,
142-
license_file: Option<&str>,
143142
crate_size: i32,
144143
published_by: i32,
145144
checksum: String,
@@ -148,7 +147,7 @@ impl NewVersion {
148147
) -> AppResult<Self> {
149148
let features = serde_json::to_value(features)?;
150149

151-
let mut new_version = NewVersion {
150+
Ok(NewVersion {
152151
crate_id,
153152
num: num.to_string(),
154153
features,
@@ -158,11 +157,7 @@ impl NewVersion {
158157
checksum,
159158
links,
160159
rust_version,
161-
};
162-
163-
new_version.validate_license(license_file)?;
164-
165-
Ok(new_version)
160+
})
166161
}
167162

168163
pub fn save(&self, conn: &mut PgConnection, published_by_email: &str) -> AppResult<Version> {
@@ -195,21 +190,9 @@ impl NewVersion {
195190
Ok(version)
196191
})
197192
}
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-
}
210193
}
211194

212-
fn validate_license_expr(s: &str) -> AppResult<()> {
195+
pub fn validate_license_expr(s: &str) -> AppResult<()> {
213196
pub const PARSE_MODE: spdx::ParseMode = spdx::ParseMode {
214197
allow_lower_case_operators: false,
215198
allow_slash_as_or_operator: true,

src/tests/builders/version.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub struct VersionBuilder<'a> {
1414
dependencies: Vec<(i32, Option<&'static str>)>,
1515
features: BTreeMap<String, Vec<String>>,
1616
license: Option<&'a str>,
17-
license_file: Option<&'a str>,
1817
num: semver::Version,
1918
size: i32,
2019
yanked: bool,
@@ -40,7 +39,6 @@ impl<'a> VersionBuilder<'a> {
4039
dependencies: Vec::new(),
4140
features: BTreeMap::new(),
4241
license: None,
43-
license_file: None,
4442
num,
4543
size: 0,
4644
yanked: false,
@@ -106,7 +104,6 @@ impl<'a> VersionBuilder<'a> {
106104
&self.num,
107105
&self.features,
108106
license,
109-
self.license_file,
110107
self.size,
111108
published_by,
112109
self.checksum,

src/worker/update_downloads.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ mod test {
105105
&semver::Version::parse("1.0.0").unwrap(),
106106
&BTreeMap::new(),
107107
None,
108-
None,
109108
0,
110109
user_id,
111110
"0000000000000000000000000000000000000000000000000000000000000000".to_string(),

0 commit comments

Comments
 (0)