Skip to content

Commit fdfd3a0

Browse files
fix(sources): map capped-body rejections to client errors in firehose and splunk_hec sources (#25826)
1 parent e835dcb commit fdfd3a0

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

src/sources/aws_kinesis_firehose/filters.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use super::{
1818
};
1919
use crate::{
2020
SourceSender, codecs,
21+
common::http::ErrorMessage,
2122
internal_events::{AwsKinesisFirehoseRequestError, AwsKinesisFirehoseRequestReceived},
2223
sources::http_server::HttpConfigParamKind,
2324
sources::util::{decompression::CappedDecoder, http::capped_body},
@@ -208,6 +209,10 @@ async fn handle_firehose_rejection(err: warp::Rejection) -> Result<impl warp::Re
208209
message = e.to_string();
209210
code = e.status();
210211
request_id = e.request_id();
212+
} else if let Some(e) = err.find::<ErrorMessage>() {
213+
code = e.status_code();
214+
message = e.message().to_string();
215+
request_id = None;
211216
} else if let Some(e) = err.find::<warp::reject::MissingHeader>() {
212217
code = StatusCode::BAD_REQUEST;
213218
message = format!("Required header missing: {}", e.name());
@@ -282,4 +287,23 @@ mod tests {
282287

283288
assert_eq!(parsed.access_key, Some("secret123".to_owned()));
284289
}
290+
291+
#[tokio::test]
292+
async fn capped_body_rejection_maps_to_client_error() {
293+
use warp::Reply;
294+
295+
// `capped_body()` rejects oversized payloads with an `ErrorMessage` (e.g. 413). The
296+
// recovery must surface that status instead of falling through to a 500.
297+
let rejection = warp::reject::custom(ErrorMessage::new(
298+
StatusCode::PAYLOAD_TOO_LARGE,
299+
"Request body exceeds limit of 1024 bytes.".to_owned(),
300+
));
301+
302+
let response = handle_firehose_rejection(rejection)
303+
.await
304+
.unwrap()
305+
.into_response();
306+
307+
assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
308+
}
285309
}

src/sources/splunk_hec/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use self::{
6565
use crate::{
6666
SourceSender,
6767
codecs::DecodingConfig,
68+
common::http::ErrorMessage,
6869
config::{DataType, Resource, SourceConfig, SourceContext, SourceOutput, log_schema},
6970
event::{Event, LogEvent, Value},
7071
http::{KeepaliveConfig, MaxConnectionAgeLayer, build_http_trace_layer},
@@ -2014,6 +2015,8 @@ async fn finish_err(rejection: Rejection) -> Result<(Response,), Rejection> {
20142015
response_json(StatusCode::BAD_REQUEST, splunk_response::ACK_IS_DISABLED)
20152016
}
20162017
},))
2018+
} else if let Some(error) = rejection.find::<ErrorMessage>() {
2019+
Ok((response_json(error.status_code(), error),))
20172020
} else {
20182021
Err(rejection)
20192022
}
@@ -2084,6 +2087,22 @@ mod tests {
20842087
crate::test_util::test_generate_config::<SplunkConfig>();
20852088
}
20862089

2090+
#[tokio::test]
2091+
async fn finish_err_maps_capped_body_to_client_error() {
2092+
// `capped_body()` rejects oversized payloads with an `ErrorMessage` (e.g. 413). The
2093+
// recovery must surface that status instead of letting it fall through to a 500.
2094+
let rejection = warp::reject::custom(ErrorMessage::new(
2095+
StatusCode::PAYLOAD_TOO_LARGE,
2096+
"Request body exceeds limit of 1024 bytes.".to_owned(),
2097+
));
2098+
2099+
let (response,) = finish_err(rejection)
2100+
.await
2101+
.expect("capped-body rejection should be recovered");
2102+
2103+
assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
2104+
}
2105+
20872106
/// Splunk token
20882107
const TOKEN: &str = "token";
20892108
const VALID_TOKENS: &[&str; 2] = &[TOKEN, "secondary-token"];

0 commit comments

Comments
 (0)