Skip to content

Implement token scope restrictions for our endpoints #5572

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 3 commits into from
Dec 14, 2022
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: 7 additions & 1 deletion src/controllers/krate/owners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::auth::AuthCheck;
use crate::controllers::prelude::*;
use crate::models::token::EndpointScope;
use crate::models::{Crate, Owner, Rights, Team, User};
use crate::views::EncodableOwner;

Expand Down Expand Up @@ -80,7 +81,12 @@ fn parse_owners_request(req: &mut dyn RequestExt) -> AppResult<Vec<String>> {
}

fn modify_owners(req: &mut dyn RequestExt, add: bool) -> EndpointResult {
let auth = AuthCheck::default().check(req)?;
let crate_name = &req.params()["crate_id"];

let auth = AuthCheck::default()
.with_endpoint_scope(EndpointScope::ChangeOwners)
.for_crate(crate_name)
.check(req)?;

let logins = parse_owners_request(req)?;
let app = req.app();
Expand Down
20 changes: 19 additions & 1 deletion src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::models::{
use crate::worker;

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

let conn = app.primary_database.get()?;
let auth = AuthCheck::default().check(req)?;

// this query should only be used for the endpoint scope calculation
// since a race condition there would only cause `publish-new` instead of
// `publish-update` to be used.
let existing_crate = Crate::by_name(&new_crate.name)
.first::<Crate>(&*conn)
.optional()?;

let endpoint_scope = match existing_crate {
Some(_) => EndpointScope::PublishUpdate,
None => EndpointScope::PublishNew,
};

let auth = AuthCheck::default()
.with_endpoint_scope(endpoint_scope)
.for_crate(&new_crate.name)
.check(req)?;

let api_token_id = auth.api_token_id();
let user = auth.user();

Expand Down
8 changes: 7 additions & 1 deletion src/controllers/version/yank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use swirl::Job;

use super::{extract_crate_name_and_semver, version_and_crate};
use crate::controllers::cargo_prelude::*;
use crate::models::token::EndpointScope;
use crate::models::Rights;
use crate::models::{insert_version_owner_action, VersionAction};
use crate::schema::versions;
Expand Down Expand Up @@ -32,9 +33,14 @@ pub fn unyank(req: &mut dyn RequestExt) -> EndpointResult {
fn modify_yank(req: &mut dyn RequestExt, yanked: bool) -> EndpointResult {
// FIXME: Should reject bad requests before authentication, but can't due to
// lifetime issues with `req`.
let auth = AuthCheck::default().check(req)?;

let (crate_name, semver) = extract_crate_name_and_semver(req)?;

let auth = AuthCheck::default()
.with_endpoint_scope(EndpointScope::Yank)
.for_crate(crate_name)
.check(req)?;

let conn = req.db_write()?;
let (version, krate) = version_and_crate(&conn, crate_name, semver)?;
let api_token_id = auth.api_token_id();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
[
{
"request": {
"uri": "http://alexcrichton-test.s3.amazonaws.com/crates/fyk/fyk-1.0.0.crate",
"method": "PUT",
"headers": [
[
"accept",
"*/*"
],
[
"accept-encoding",
"gzip"
],
[
"content-length",
"35"
],
[
"content-type",
"application/gzip"
]
],
"body": "H4sIAAAAAAAA/+3AAQEAAACCIP+vbkhQwKsBLq+17wAEAAA="
},
"response": {
"status": 200,
"headers": [],
"body": ""
}
},
{
"request": {
"uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk",
"method": "PUT",
"headers": [
[
"accept",
"*/*"
],
[
"accept-encoding",
"gzip"
],
[
"content-length",
"157"
],
[
"content-type",
"text/plain"
]
],
"body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg=="
},
"response": {
"status": 200,
"headers": [],
"body": ""
}
},
{
"request": {
"uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk",
"method": "PUT",
"headers": [
[
"accept",
"*/*"
],
[
"accept-encoding",
"gzip"
],
[
"content-length",
"156"
],
[
"content-type",
"text/plain"
]
],
"body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjp0cnVlLCJsaW5rcyI6bnVsbH0K"
},
"response": {
"status": 200,
"headers": [],
"body": ""
}
},
{
"request": {
"uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk",
"method": "PUT",
"headers": [
[
"accept",
"*/*"
],
[
"accept-encoding",
"gzip"
],
[
"content-length",
"157"
],
[
"content-type",
"text/plain"
]
],
"body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg=="
},
"response": {
"status": 200,
"headers": [],
"body": ""
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
[
{
"request": {
"uri": "http://alexcrichton-test.s3.amazonaws.com/crates/fyk/fyk-1.0.0.crate",
"method": "PUT",
"headers": [
[
"accept",
"*/*"
],
[
"accept-encoding",
"gzip"
],
[
"content-length",
"35"
],
[
"content-type",
"application/gzip"
]
],
"body": "H4sIAAAAAAAA/+3AAQEAAACCIP+vbkhQwKsBLq+17wAEAAA="
},
"response": {
"status": 200,
"headers": [],
"body": ""
}
},
{
"request": {
"uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk",
"method": "PUT",
"headers": [
[
"accept",
"*/*"
],
[
"accept-encoding",
"gzip"
],
[
"content-length",
"157"
],
[
"content-type",
"text/plain"
]
],
"body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg=="
},
"response": {
"status": 200,
"headers": [],
"body": ""
}
},
{
"request": {
"uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk",
"method": "PUT",
"headers": [
[
"accept",
"*/*"
],
[
"accept-encoding",
"gzip"
],
[
"content-length",
"156"
],
[
"content-type",
"text/plain"
]
],
"body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjp0cnVlLCJsaW5rcyI6bnVsbH0K"
},
"response": {
"status": 200,
"headers": [],
"body": ""
}
},
{
"request": {
"uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk",
"method": "PUT",
"headers": [
[
"accept",
"*/*"
],
[
"accept-encoding",
"gzip"
],
[
"content-length",
"157"
],
[
"content-type",
"text/plain"
]
],
"body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg=="
},
"response": {
"status": 200,
"headers": [],
"body": ""
}
}
]
Loading