Skip to content

Commit e3b38fc

Browse files
committed
Auto merge of #3518 - jtgeibel:forbid-token-auth-on-me, r=pietroalbini
Forbid token auth on `/me` endpoint This prohibits access to the `/api/v1/me` endpoint via an API token. I'd like to merge and deploy this before moving forward on rust-lang/simpleinfra#43, which would expose this endpoint. (Previous lockdown work was done in #3470.) The second commit ensure that the user is only authenticated on the search endpoint when querying for followed crates. r? `@pietroalbini`
2 parents d42c1c0 + 5e4f286 commit e3b38fc

File tree

3 files changed

+4
-19
lines changed

3 files changed

+4
-19
lines changed

src/controllers/krate/search.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use diesel_full_text_search::*;
55

66
use crate::controllers::cargo_prelude::*;
77
use crate::controllers::helpers::Paginate;
8-
use crate::controllers::util::AuthenticatedUser;
98
use crate::models::{
109
Crate, CrateBadge, CrateOwner, CrateVersions, OwnerKind, TopVersions, Version,
1110
};
@@ -40,9 +39,6 @@ use crate::models::krate::{canon_crate_name, ALL_COLUMNS};
4039
pub fn search(req: &mut dyn RequestExt) -> EndpointResult {
4140
use diesel::sql_types::{Bool, Text};
4241

43-
// Don't require that authentication succeed, because it's only necessary
44-
// if the "following" param is set.
45-
let authenticated_user: AppResult<AuthenticatedUser> = req.authenticate();
4642
let params = req.query();
4743
let sort = params.get("sort").map(|s| &**s);
4844
let include_yanked = params
@@ -160,7 +156,7 @@ pub fn search(req: &mut dyn RequestExt) -> EndpointResult {
160156
),
161157
);
162158
} else if params.get("following").is_some() {
163-
let user_id = authenticated_user?.user_id();
159+
let user_id = req.authenticate()?.user_id();
164160
query = query.filter(
165161
crates::id.eq_any(
166162
follows::table

src/controllers/user/me.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::views::{EncodableMe, EncodablePrivateUser, EncodableVersion, OwnedCra
1313

1414
/// Handles the `GET /me` route.
1515
pub fn me(req: &mut dyn RequestExt) -> EndpointResult {
16-
let user_id = req.authenticate()?.user_id();
16+
let user_id = req.authenticate()?.forbid_api_token_auth()?.user_id();
1717
let conn = req.db_conn()?;
1818

1919
let (user, verified, email, verification_sent): (User, Option<bool>, Option<String>, bool) =

src/tests/token.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{user::UserShowPrivateResponse, RequestHelper, TestApp};
1+
use crate::{RequestHelper, TestApp};
22
use cargo_registry::{
33
models::ApiToken,
44
schema::api_tokens,
@@ -272,17 +272,6 @@ fn revoke_token_success() {
272272
});
273273
}
274274

275-
#[test]
276-
fn token_gives_access_to_me() {
277-
let url = "/api/v1/me";
278-
let (_, anon, user, token) = TestApp::init().with_token();
279-
280-
anon.get(url).assert_forbidden();
281-
282-
let json: UserShowPrivateResponse = token.get(url).good();
283-
assert_eq!(json.user.name, user.as_model().name);
284-
}
285-
286275
#[test]
287276
fn using_token_updates_last_used_at() {
288277
let url = "/api/v1/me";
@@ -293,7 +282,7 @@ fn using_token_updates_last_used_at() {
293282
assert_none!(token.as_model().last_used_at);
294283

295284
// Use the token once
296-
token.get::<EncodableMe>("/api/v1/me").good();
285+
token.search("following=1");
297286

298287
let token: ApiToken =
299288
app.db(|conn| assert_ok!(ApiToken::belonging_to(user.as_model()).first(conn)));

0 commit comments

Comments
 (0)