Skip to content

Extract licenses module #7199

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 2 commits into from
Sep 28, 2023
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
8 changes: 6 additions & 2 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use crate::models::{
VersionAction,
};

use crate::licenses::parse_license_expr;
use crate::middleware::log_request::RequestLogExt;
use crate::models::token::EndpointScope;
use crate::models::version::validate_license_expr;
use crate::rate_limiter::LimitedAction;
use crate::schema::*;
use crate::sql::canon_crate_name;
Expand All @@ -34,6 +34,10 @@ const MISSING_RIGHTS_ERROR_MESSAGE: &str = "this crate exists but you don't seem
to accept an invitation to be an owner before \
publishing.";

const LICENSE_ERROR: &str = "unknown or invalid license expression; \
see http://opensource.org/licenses for options, \
and http://spdx.org/licenses/ for their identifiers";

/// Handles the `PUT /crates/new` route.
/// Used by `cargo publish` to publish a new crate or to publish a new version of an
/// existing crate.
Expand Down Expand Up @@ -138,7 +142,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
}

if let Some(ref license) = license {
validate_license_expr(license)?;
parse_license_expr(license).map_err(|_| cargo_err(LICENSE_ERROR))?;
} else if license_file.is_some() {
// If no license is given, but a license file is given, flag this
// crate as having a nonstandard license. Note that we don't
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub mod worker;

pub mod auth;
pub mod controllers;
mod licenses;
pub mod models;
mod router;
pub mod sentry;
Expand Down
29 changes: 29 additions & 0 deletions src/licenses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use spdx::{Expression, ParseError};

const PARSE_MODE: spdx::ParseMode = spdx::ParseMode {
allow_lower_case_operators: false,
allow_slash_as_or_operator: true,
allow_imprecise_license_names: false,
allow_postfix_plus_on_gpl: true,
};

pub fn parse_license_expr(s: &str) -> Result<Expression, ParseError> {
Expression::parse_mode(s, PARSE_MODE)
}

#[cfg(test)]
mod tests {
use super::parse_license_expr;

#[test]
fn licenses() {
assert_ok!(parse_license_expr("MIT"));
assert_ok!(parse_license_expr("MIT OR Apache-2.0"));
assert_ok!(parse_license_expr("MIT/Apache-2.0"));
assert_ok!(parse_license_expr("MIT AND Apache-2.0"));
assert_ok!(parse_license_expr("MIT OR (Apache-2.0 AND MIT)"));
assert_ok!(parse_license_expr("GPL-3.0+"));

assert_err!(parse_license_expr("apache 2.0"));
}
}
31 changes: 1 addition & 30 deletions src/models/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,6 @@ impl NewVersion {
}
}

pub fn validate_license_expr(s: &str) -> AppResult<()> {
pub const PARSE_MODE: spdx::ParseMode = spdx::ParseMode {
allow_lower_case_operators: false,
allow_slash_as_or_operator: true,
allow_imprecise_license_names: false,
allow_postfix_plus_on_gpl: true,
};

spdx::Expression::parse_mode(s, PARSE_MODE).map_err(|_| {
cargo_err("unknown or invalid license expression; see http://opensource.org/licenses for options, and http://spdx.org/licenses/ for their identifiers")
})?;

Ok(())
}

fn strip_build_metadata(version: &str) -> &str {
version
.split_once('+')
Expand All @@ -216,7 +201,7 @@ fn strip_build_metadata(version: &str) -> &str {

#[cfg(test)]
mod tests {
use super::{validate_license_expr, TopVersions};
use super::TopVersions;
use chrono::NaiveDateTime;

#[track_caller]
Expand Down Expand Up @@ -286,18 +271,4 @@ mod tests {
}
);
}

#[test]
fn licenses() {
assert_ok!(validate_license_expr("MIT"));
assert_ok!(validate_license_expr("MIT OR Apache-2.0"));
assert_ok!(validate_license_expr("MIT/Apache-2.0"));
assert_ok!(validate_license_expr("MIT AND Apache-2.0"));
assert_ok!(validate_license_expr("MIT OR (Apache-2.0 AND MIT)"));
assert_ok!(validate_license_expr("GPL-3.0+"));

let error = assert_err!(validate_license_expr("apache 2.0"));
let error = format!("{error}");
assert!(error.starts_with("unknown or invalid license expression; see http"));
}
}