@@ -18,6 +18,7 @@ use crate::models::{
18
18
use crate :: worker;
19
19
20
20
use crate :: middleware:: log_request:: add_custom_metadata;
21
+ use crate :: models:: token:: EndpointScope ;
21
22
use crate :: schema:: * ;
22
23
use crate :: util:: errors:: { cargo_err, AppResult } ;
23
24
use crate :: util:: { read_fill, read_le_u32, CargoVcsInfo , LimitErrorReader , Maximums } ;
@@ -65,7 +66,24 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
65
66
add_custom_metadata ( "crate_version" , new_crate. vers . to_string ( ) ) ;
66
67
67
68
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
+
69
87
let api_token_id = auth. api_token_id ( ) ;
70
88
let user = auth. user ( ) ;
71
89
0 commit comments