Skip to content

Commit

Permalink
Tighten lambda-http event deserializer (#788)
Browse files Browse the repository at this point in the history
* Tighten lambda-http event deserializer

* Add 'deny_unknown_fields' for alb request

* Removed 'deny_unknown_fields' and make request_context mandatory for 'ApiGatewayProxyRequest' and 'ApiGatewayWebsocketProxyRequest'
  • Loading branch information
bnusunny authored Jan 23, 2024
1 parent b8880d2 commit fde1d37
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
9 changes: 4 additions & 5 deletions lambda-events/src/event/apigw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::collections::HashMap;
#[serde(rename_all = "camelCase")]
pub struct ApiGatewayProxyRequest<T1 = Value>
where
T1: DeserializeOwned,
T1: DeserializeOwned + Default,
T1: Serialize,
{
/// The resource path defined in API Gateway
Expand Down Expand Up @@ -43,7 +43,6 @@ where
#[serde(deserialize_with = "deserialize_lambda_map")]
#[serde(default)]
pub stage_variables: HashMap<String, String>,
#[serde(default)]
#[serde(bound = "")]
pub request_context: ApiGatewayProxyRequestContext<T1>,
#[serde(default)]
Expand Down Expand Up @@ -335,9 +334,9 @@ pub struct ApiGatewayRequestIdentity {
#[serde(rename_all = "camelCase")]
pub struct ApiGatewayWebsocketProxyRequest<T1 = Value, T2 = Value>
where
T1: DeserializeOwned,
T1: DeserializeOwned + Default,
T1: Serialize,
T2: DeserializeOwned,
T2: DeserializeOwned + Default,
T2: Serialize,
{
/// The resource path defined in API Gateway
Expand Down Expand Up @@ -367,7 +366,7 @@ where
#[serde(deserialize_with = "deserialize_lambda_map")]
#[serde(default)]
pub stage_variables: HashMap<String, String>,
#[serde(bound = "", default)]
#[serde(bound = "")]
pub request_context: ApiGatewayWebsocketProxyRequestContext<T1, T2>,
#[serde(default)]
pub body: Option<String>,
Expand Down
3 changes: 1 addition & 2 deletions lambda-events/src/fixtures/example-sqs-event.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@
}
}
]
}

}
37 changes: 33 additions & 4 deletions lambda-http/src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod tests {
fn test_deserialize_apigw_rest() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-apigw-request.json");

let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialze apigw rest data");
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize apigw rest data");
match req {
LambdaRequest::ApiGatewayV1(req) => {
assert_eq!("12345678912", req.request_context.account_id.unwrap());
Expand All @@ -69,7 +69,7 @@ mod tests {
fn test_deserialize_apigw_http() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-apigw-v2-request-iam.json");

let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialze apigw http data");
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize apigw http data");
match req {
LambdaRequest::ApiGatewayV2(req) => {
assert_eq!("123456789012", req.request_context.account_id.unwrap());
Expand All @@ -84,7 +84,7 @@ mod tests {
"../../lambda-events/src/fixtures/example-alb-lambda-target-request-multivalue-headers.json"
);

let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialze alb rest data");
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize alb rest data");
match req {
LambdaRequest::Alb(req) => {
assert_eq!(
Expand All @@ -101,12 +101,41 @@ mod tests {
let data =
include_bytes!("../../lambda-events/src/fixtures/example-apigw-websocket-request-without-method.json");

let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialze apigw websocket data");
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize apigw websocket data");
match req {
LambdaRequest::WebSocket(req) => {
assert_eq!("CONNECT", req.request_context.event_type.unwrap());
}
other => panic!("unexpected request variant: {:?}", other),
}
}

#[test]
#[cfg(feature = "pass_through")]
fn test_deserialize_bedrock_agent() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-bedrock-agent-runtime-event.json");

let req: LambdaRequest =
serde_json::from_slice(data).expect("failed to deserialize bedrock agent request data");
match req {
LambdaRequest::PassThrough(req) => {
assert_eq!(String::from_utf8_lossy(data), req);
}
other => panic!("unexpected request variant: {:?}", other),
}
}

#[test]
#[cfg(feature = "pass_through")]
fn test_deserialize_sqs() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-sqs-event.json");

let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize sqs event data");
match req {
LambdaRequest::PassThrough(req) => {
assert_eq!(String::from_utf8_lossy(data), req);
}
other => panic!("unexpected request variant: {:?}", other),
}
}
}

0 comments on commit fde1d37

Please sign in to comment.