Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.
/ api Public archive

Commit a8717dd

Browse files
committed
code cleanup and fix int overflow
1 parent 420ad66 commit a8717dd

File tree

9 files changed

+117
-49
lines changed

9 files changed

+117
-49
lines changed

Cargo.lock

Lines changed: 62 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "api"
3-
version = "3.2.1"
3+
version = "3.2.2"
44
edition = "2024"
55

66
[dependencies]

src/models/build.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,12 @@ impl Build {
186186
96 => Some("sha384"),
187187
128 => Some("sha512"),
188188
_ => {
189-
if identifier.parse::<u32>().is_ok() {
190-
None
189+
if let Ok(id) = identifier.parse::<i32>() {
190+
if id < 1 {
191+
return None;
192+
} else {
193+
None
194+
}
191195
} else {
192196
return None;
193197
}

src/models/organization.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ impl Organization {
165165
cache: &crate::cache::Cache,
166166
id: i32,
167167
) -> Option<Self> {
168+
if id < 1 {
169+
return None;
170+
}
171+
168172
cache
169173
.cached(&format!("organization::{}", id), 300, || async {
170174
sqlx::query(&format!(
@@ -386,6 +390,10 @@ impl OrganizationKey {
386390
}
387391

388392
pub async fn by_id(database: &crate::database::Database, id: i32) -> Option<Self> {
393+
if id < 1 {
394+
return None;
395+
}
396+
389397
sqlx::query(&format!(
390398
"SELECT {} FROM organization_keys WHERE organization_keys.id = $1",
391399
Self::columns_sql(None, None)

src/routes/user/invites/_organization_/accept.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ mod post {
2020
(status = NOT_FOUND, body = inline(ApiError)),
2121
), params(
2222
(
23-
"organization" = u32,
23+
"organization" = i32,
2424
description = "The organization ID",
2525
minimum = 1,
2626
),
2727
))]
2828
pub async fn route(
2929
state: GetState,
3030
user: GetUser,
31-
Path(organization): Path<u32>,
31+
Path(organization): Path<i32>,
3232
) -> axum::Json<serde_json::Value> {
33-
let organization =
34-
Organization::by_id(&state.database, &state.cache, organization as i32).await;
33+
let organization = Organization::by_id(&state.database, &state.cache, organization).await;
3534

3635
if let Some(organization) = organization {
3736
let subuser =

src/routes/user/invites/_organization_/decline.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ mod post {
2020
(status = NOT_FOUND, body = inline(ApiError)),
2121
), params(
2222
(
23-
"organization" = u32,
23+
"organization" = i32,
2424
description = "The organization ID",
2525
minimum = 1,
2626
),
2727
))]
2828
pub async fn route(
2929
state: GetState,
3030
user: GetUser,
31-
Path(organization): Path<u32>,
31+
Path(organization): Path<i32>,
3232
) -> axum::Json<serde_json::Value> {
33-
let organization =
34-
Organization::by_id(&state.database, &state.cache, organization as i32).await;
33+
let organization = Organization::by_id(&state.database, &state.cache, organization).await;
3534

3635
if let Some(organization) = organization {
3736
let deleted =

src/routes/user/organizations/_organization_/api_keys/_key_.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ mod get {
2323
(status = NOT_FOUND, body = inline(ApiError)),
2424
), params(
2525
(
26-
"organization" = u32,
26+
"organization" = i32,
2727
description = "The organization ID",
2828
example = 1,
2929
),
3030
(
31-
"key" = u32,
31+
"key" = i32,
3232
description = "The api key ID",
3333
example = 1,
3434
),
3535
))]
3636
pub async fn route(
3737
state: GetState,
3838
organization: GetOrganization,
39-
Path((_organization, key)): Path<(u32, u32)>,
39+
Path((_organization, key)): Path<(i32, i32)>,
4040
) -> (StatusCode, axum::Json<serde_json::Value>) {
41-
let key = OrganizationKey::by_id(&state.database, key as i32).await;
41+
let key = OrganizationKey::by_id(&state.database, key).await;
4242

4343
if let Some(key) = key {
4444
if key.organization_id != organization.id {
@@ -86,22 +86,22 @@ mod delete {
8686
(status = NOT_FOUND, body = inline(ApiError)),
8787
), params(
8888
(
89-
"organization" = u32,
89+
"organization" = i32,
9090
description = "The organization ID",
9191
example = 1,
9292
),
9393
(
94-
"key" = u32,
94+
"key" = i32,
9595
description = "The api key ID",
9696
example = 1,
9797
),
9898
))]
9999
pub async fn route(
100100
state: GetState,
101101
organization: GetOrganization,
102-
Path((_organization, key)): Path<(u32, u32)>,
102+
Path((_organization, key)): Path<(i32, i32)>,
103103
) -> (StatusCode, axum::Json<serde_json::Value>) {
104-
let key = OrganizationKey::by_id(&state.database, key as i32).await;
104+
let key = OrganizationKey::by_id(&state.database, key).await;
105105

106106
if let Some(key) = key {
107107
if key.organization_id != organization.id {

src/routes/user/organizations/_organization_/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,20 @@ async fn auth(
2525
mut req: Request,
2626
next: Next,
2727
) -> Result<Response, StatusCode> {
28-
let organization = match organization[0].parse::<u32>() {
29-
Ok(organization) => organization as i32,
28+
let organization = match organization[0].parse::<i32>() {
29+
Ok(organization) => {
30+
if organization < 1 {
31+
return Ok(Response::builder()
32+
.status(StatusCode::BAD_REQUEST)
33+
.header("Content-Type", "application/json")
34+
.body(Body::from(
35+
serde_json::to_string(&ApiError::new(&["invalid organization"])).unwrap(),
36+
))
37+
.unwrap());
38+
}
39+
40+
organization
41+
}
3042
Err(_) => {
3143
return Ok(Response::builder()
3244
.status(StatusCode::BAD_REQUEST)

src/routes/v1/builds/_type_/_version_/_build_.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ mod get {
4141
state: GetState,
4242
Path((r#type, version, build)): Path<(ServerType, String, String)>,
4343
) -> (StatusCode, axum::Json<serde_json::Value>) {
44-
let build: Option<u32> = if build == "latest" {
44+
let build: Option<i32> = if build == "latest" {
4545
None
4646
} else {
4747
match build.parse() {
48-
Ok(build) => Some(build),
48+
Ok(build) => {
49+
if build < 0 {
50+
return (
51+
StatusCode::BAD_REQUEST,
52+
axum::Json(ApiError::new(&["invalid build"]).to_value()),
53+
);
54+
}
55+
56+
Some(build)
57+
}
4958
Err(_) => {
5059
return (
5160
StatusCode::BAD_REQUEST,
@@ -68,15 +77,7 @@ mod get {
6877
build.map(|b| b.to_string()).unwrap_or("latest".to_string())
6978
),
7079
3600,
71-
|| {
72-
Build::by_build_number(
73-
&state.database,
74-
r#type,
75-
&location,
76-
&version,
77-
build.map(|b| b as i32),
78-
)
79-
},
80+
|| Build::by_build_number(&state.database, r#type, &location, &version, build),
8081
)
8182
.await;
8283

0 commit comments

Comments
 (0)