Skip to content

Commit 07a825c

Browse files
committed
crate::publish: Use token scope restrictions
1 parent 0557583 commit 07a825c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/controllers/krate/publish.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::models::{
1818
use crate::worker;
1919

2020
use crate::middleware::log_request::add_custom_metadata;
21+
use crate::models::token::EndpointScope;
2122
use crate::schema::*;
2223
use crate::util::errors::{cargo_err, AppResult};
2324
use crate::util::{read_fill, read_le_u32, CargoVcsInfo, LimitErrorReader, Maximums};
@@ -65,7 +66,24 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
6566
add_custom_metadata("crate_version", new_crate.vers.to_string());
6667

6768
let conn = app.primary_database.get()?;
68-
let auth = AuthCheck::default().check(req)?;
69+
70+
// this query should only be used for the endpoint scope calculation
71+
// since a race condition there would only cause `publish-new` instead of
72+
// `publish-update` to be used.
73+
let existing_crate = Crate::by_name(&new_crate.name)
74+
.first::<Crate>(&*conn)
75+
.optional()?;
76+
77+
let endpoint_scope = match existing_crate {
78+
Some(_) => EndpointScope::PublishUpdate,
79+
None => EndpointScope::PublishNew,
80+
};
81+
82+
let auth = AuthCheck::default()
83+
.with_endpoint_scope(endpoint_scope)
84+
.for_crate(&new_crate.name)
85+
.check(req)?;
86+
6987
let api_token_id = auth.api_token_id();
7088
let user = auth.user();
7189

0 commit comments

Comments
 (0)