Skip to content

Commit

Permalink
feat(api): deny unknown query fields (#492)
Browse files Browse the repository at this point in the history
Deny unknown query fields
  • Loading branch information
Alexandcoats authored Jul 29, 2022
1 parent 14b32bc commit 7258d58
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 50 deletions.
46 changes: 2 additions & 44 deletions bin/inx-chronicle/src/api/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use time::{Duration, OffsetDateTime};
use super::{error::ApiError, DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE};

#[derive(Debug, Copy, Clone, Deserialize, PartialEq, Eq)]
#[serde(default, rename_all = "camelCase")]
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct Pagination {
pub page_size: usize,
pub page: usize,
Expand Down Expand Up @@ -38,7 +38,7 @@ impl<B: Send> FromRequest<B> for Pagination {
}

#[derive(Copy, Clone, Default, Deserialize)]
#[serde(default, rename_all = "camelCase")]
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct TimeRangeQuery {
start_timestamp: Option<u32>,
end_timestamp: Option<u32>,
Expand Down Expand Up @@ -91,48 +91,6 @@ mod stardust {
#[cfg(feature = "stardust")]
pub use stardust::*;

#[derive(Copy, Clone, Deserialize)]
#[serde(default)]
pub struct Included {
pub included: bool,
}

impl Default for Included {
fn default() -> Self {
Self { included: true }
}
}

#[async_trait]
impl<B: Send> FromRequest<B> for Included {
type Rejection = ApiError;

async fn from_request(req: &mut axum::extract::RequestParts<B>) -> Result<Self, Self::Rejection> {
let Query(included) = Query::<Included>::from_request(req)
.await
.map_err(ApiError::QueryError)?;
Ok(included)
}
}

#[derive(Copy, Clone, Default, Deserialize)]
#[serde(default)]
pub struct Expanded {
pub expanded: bool,
}

#[async_trait]
impl<B: Send> FromRequest<B> for Expanded {
type Rejection = ApiError;

async fn from_request(req: &mut axum::extract::RequestParts<B>) -> Result<Self, Self::Rejection> {
let Query(expanded) = Query::<Expanded>::from_request(req)
.await
.map_err(ApiError::QueryError)?;
Ok(expanded)
}
}

#[cfg(test)]
mod test {
use axum::{
Expand Down
4 changes: 2 additions & 2 deletions bin/inx-chronicle/src/api/stardust/history/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct LedgerUpdatesByAddressPagination {
}

#[derive(Clone, Deserialize, Default)]
#[serde(default, rename_all = "camelCase")]
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct LedgerUpdatesByAddressPaginationQuery {
pub page_size: Option<usize>,
pub sort: Option<String>,
Expand Down Expand Up @@ -110,7 +110,7 @@ pub struct LedgerUpdatesByMilestonePagination {
}

#[derive(Clone, Deserialize, Default)]
#[serde(default, rename_all = "camelCase")]
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct LedgerUpdatesByMilestonePaginationQuery {
pub page_size: Option<usize>,
pub cursor: Option<String>,
Expand Down
8 changes: 4 additions & 4 deletions bin/inx-chronicle/src/api/stardust/indexer/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Display for IndexedOutputsCursor {
}

#[derive(Clone, Deserialize, Default)]
#[serde(default, rename_all = "camelCase")]
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct BasicOutputsPaginationQuery {
pub address: Option<String>,
pub has_native_tokens: Option<bool>,
Expand Down Expand Up @@ -166,7 +166,7 @@ impl<B: Send> FromRequest<B> for IndexedOutputsPagination<BasicOutputsQuery> {
}

#[derive(Clone, Deserialize, Default)]
#[serde(default, rename_all = "camelCase")]
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct AliasOutputsPaginationQuery {
pub state_controller: Option<String>,
pub governor: Option<String>,
Expand Down Expand Up @@ -250,7 +250,7 @@ impl<B: Send> FromRequest<B> for IndexedOutputsPagination<AliasOutputsQuery> {
}

#[derive(Clone, Deserialize, Default)]
#[serde(default, rename_all = "camelCase")]
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct FoundryOutputsPaginationQuery {
pub alias_address: Option<String>,
pub has_native_tokens: Option<bool>,
Expand Down Expand Up @@ -316,7 +316,7 @@ impl<B: Send> FromRequest<B> for IndexedOutputsPagination<FoundryOutputsQuery> {
}

#[derive(Clone, Deserialize, Default)]
#[serde(default, rename_all = "camelCase")]
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct NftOutputsPaginationQuery {
pub address: Option<String>,
pub issuer: Option<String>,
Expand Down

0 comments on commit 7258d58

Please sign in to comment.