Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0ff163a
fix(server): parse request bodies nested to the depth the service val…
LeeroyHannigan Nov 3, 2026
bd05ef8
Merge remote-tracking branch 'origin/main' into merge/807-redo2
LeeroyHannigan Nov 12, 2026
3a94df0
fix(server): parse a body past serde's depth only after authentication
LeeroyHannigan Nov 12, 2026
a3a991b
test(nesting): accept the service's varying answer past 1700 wrappers
LeeroyHannigan Nov 12, 2026
e2e58e3
test(nesting): retry a 500 in the raw sender and align the depth figures
LeeroyHannigan Nov 13, 2026
99542cb
fix(server): check a deep body's syntax before authentication
LeeroyHannigan Nov 20, 2026
b8fee45
docs(server): say what the service does with an unknown deep member
LeeroyHannigan Nov 20, 2026
5d71902
test(server): pin the 5200 level ceiling in the depth scan rows
LeeroyHannigan Nov 20, 2026
1f49125
docs(server): say which deep malformed bodies reach authentication
LeeroyHannigan Nov 20, 2026
c73c1b0
fix(server): skip a deep body with the service's grammar before authe…
LeeroyHannigan Nov 24, 2026
bb44727
test(server): pin the message boundary of the syntax check's refusal
LeeroyHannigan Nov 24, 2026
3dad856
docs(server): list the forms the syntax check passes to authentication
LeeroyHannigan Nov 24, 2026
51cc638
refactor(server): move the syntax scan into its own module
LeeroyHannigan Nov 24, 2026
ce77837
Merge origin/main into fix/request-nesting-depth
LeeroyHannigan Nov 24, 2026
4c2995c
Merge origin/main into fix/request-nesting-depth
LeeroyHannigan Nov 24, 2026
b9baaf5
Merge origin/main into fix/request-nesting-depth
LeeroyHannigan Nov 24, 2026
acfd6a6
Merge origin/main into fix/request-nesting-depth
LeeroyHannigan Nov 25, 2026
1a9efd7
refactor(server): run the engine's operation check and admit around t…
LeeroyHannigan Nov 25, 2026
027483d
docs(wasm): say that admit runs on the result of parse_request
LeeroyHannigan Nov 25, 2026
71b3e1b
test(server): pin the edges of the syntax check's looser rules
LeeroyHannigan Nov 25, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,16 @@ step does not see.
Before the HTTP server passes a request to the engine, its request handler
(`handle_request` in `crates/server/src/handler.rs`) and its authorization
(`authorize_request` in `crates/server/src/request_helpers.rs`) check it. Both
builds run the checks the engine owns, `parse_request` and
`ParsedRequest::admit` in `crates/engine/src/pre_dispatch.rs`, except on the
operations this build refuses by name before them, whatever the body (see
"What it refuses"). This build makes none of the
server's other checks: it passes the request to the engine's handler for the
operation, so where a check below refuses a request, this build answers what
the handler answers, and the two answers can differ.
builds run the checks the engine owns in `crates/engine/src/pre_dispatch.rs`,
the operation name check `check_operation` and `ParsedRequest::admit`, except
on the operations this build refuses by name before them, whatever the body
(see "What it refuses"). Between the two, this build parses the body with the
engine's `parse_request`, and the server with its own parser
(`crates/server/src/request_parse.rs`), so the two builds can answer a body
differently; the table below does not list those answers. This build makes
none of the server's other checks: it passes the request to the engine's
handler for the operation, so where a check below refuses a request, this
build answers what the handler answers, and the two answers can differ.

The server makes the checks in the table for every caller, in developer mode
too, except the IAM decision of **signature**, which it makes only outside
Expand Down Expand Up @@ -541,7 +544,7 @@ PostgreSQL backend registers again with its catalog, or the
`AccessDeniedException` "User: is not authorized to perform this operation" it
gives a request that passes its signature check and `admit` when its state has
no catalog store, which `extenddb serve` never builds. The server's name checks
of a `RestoreTableToPointInTime` never run: `parse_request` refuses one whose
of a `RestoreTableToPointInTime` never run: the body parse refuses one whose
body does not parse, the server's authentication one it does not accept, and
`admit` every other one, first (this build authenticates nothing, so here
`admit` refuses every one whose body parses). Nor do its **table names**,
Expand Down
6 changes: 6 additions & 0 deletions crates/app/src/cmd_serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ pub fn run(args: &ServeArgs, build: BuildInfo) -> anyhow::Result<()> {
// join it, holding the process open after the drain until SIGKILL.
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
// An authenticated request body nested to the parser's ceiling is
// parsed, converted to typed AttributeValues and dropped on a worker
// thread, each step recursing to the body's depth; the server crate
// that sets the ceiling sizes the stack it needs (about twice the
// measured need, far above Tokio's 2 MiB default).
.thread_stack_size(extenddb_server::REQUEST_WORKER_STACK_SIZE)
.build()?;
let result = crate::run_then_shut_down(runtime, extenddb_server::serve(params));
// `serve` has printed the refusal; end with its code, which `migrate`
Expand Down
7 changes: 4 additions & 3 deletions crates/core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,10 @@ fn message_key(error: &DynamoDbError) -> &'static str {
///
/// Real DynamoDB validates request format before authentication. The message
/// is `Start of structure or map found where not expected: ` followed by the
/// `Display` text of the `serde_json` parse error. Every entry point that
/// parses a raw request body builds the error from this helper so a malformed
/// request gets the same answer on every transport.
/// `Display` text of the `serde_json` parse error. The engine's
/// `parse_request` builds its error from this helper. The HTTP server parses
/// a request body with its own parser (`crates/server/src/request_parse.rs`),
/// whose answer to a body can differ.
#[doc(hidden)]
#[must_use]
pub fn malformed_body_error(error: &serde_json::Error) -> DynamoDbError {
Expand Down
6 changes: 3 additions & 3 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ mod list_tables;
#[cfg(feature = "partiql")]
mod partiql;
mod pre_dispatch;
pub use pre_dispatch::{ParsedRequest, parse_request};
pub use pre_dispatch::{KnownOperation, ParsedRequest, check_operation, parse_request};
// `private_fs` creates the private directories the filesystem-backed
// import/export use; it has no wasm32 consumer and uses unix/windows file
// APIs, so it is compiled only off wasm.
Expand Down Expand Up @@ -119,8 +119,8 @@ use extenddb_core::types::TableKeyInfo;
///
/// Real `DynamoDB` validates the operation name before checking authentication.
/// An unknown operation with no auth headers returns `UnknownOperationException`,
/// not `MissingAuthenticationToken`. `parse_request` applies this check, and
/// the server calls it before auth.
/// not `MissingAuthenticationToken`. `check_operation` applies this check:
/// the server calls it before auth, and `parse_request` calls it first.
#[must_use]
pub fn is_known_operation(operation: &str) -> bool {
matches!(
Expand Down
106 changes: 81 additions & 25 deletions crates/engine/src/pre_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,54 @@

//! The request checks the engine owns, which every entry point calls in one
//! order before it dispatches a request into the engine: the operation name,
//! the body parse, and the refusal of point-in-time recovery.
//! then the refusal of point-in-time recovery, with the entry point's parse of
//! the body between them.
//!
//! An entry point (the HTTP server, the WebAssembly adapter) calls
//! [`parse_request`] with the operation name and the raw request body, then
//! calls [`ParsedRequest::admit`] on the result to get the body it passes to
//! [`crate::dispatch`]. A check that every request the engine dispatches must
//! pass belongs in one of the two steps: a check that an entry point makes
//! itself reaches only the requests that come through that entry point.
//! An entry point calls [`check_operation`] with the operation name, parses
//! the raw request body, passes the parsed body to
//! [`KnownOperation::with_body`], and calls [`ParsedRequest::admit`] on the
//! result to get the body it passes to [`crate::dispatch`]. [`parse_request`]
//! is `check_operation` and a `serde_json` parse of the body in one call. The
//! WebAssembly adapter calls `parse_request(..)?.admit()`. The HTTP server
//! calls `check_operation` before it parses the body, parses the body with
//! its own parser (`crates/server/src/request_parse.rs`, which bounds the
//! nesting depth and checks the syntax of a deep body before
//! authentication), and calls `with_body(..).admit()` after it authenticates
//! the caller.
//!
//! The types fix the order of the two steps: `admit` is a method of
//! [`ParsedRequest`], which only `parse_request` constructs, so `admit` runs
//! only on a request whose operation is known and whose body parsed. The
//! refusal `admit` applies, [`crate::refuse_point_in_time_recovery`], is also
//! public on its own and checks whatever body it is given.
//! A check that every request the engine dispatches must pass belongs in
//! `check_operation` or in `admit`. A check that an entry point makes itself,
//! its parse of the body included, reaches only the requests that come
//! through that entry point.
//!
//! The types fix the order of the steps: `admit` is a method of
//! [`ParsedRequest`], which only `with_body` constructs, and `with_body` is a
//! method of [`KnownOperation`], which only `check_operation` constructs. So
//! `admit` runs only on a request whose operation is known, with the body the
//! entry point parsed. The refusal `admit` applies,
//! [`crate::refuse_point_in_time_recovery`], is also public on its own and
//! checks whatever body it is given.

use extenddb_core::error::{DynamoDbError, malformed_body_error};
use serde_json::Value;

/// An operation name the engine dispatches.
///
/// Returned by [`check_operation`]; [`KnownOperation::with_body`] pairs it
/// with the parsed request body.
// Public for the HTTP server's `handle_request`, not for other users of the
// crate.
#[doc(hidden)]
#[derive(Debug)]
pub struct KnownOperation<'a> {
operation: &'a str,
}

/// A request whose operation name is known and whose body parsed as JSON.
///
/// Returned by [`parse_request`]; [`ParsedRequest::admit`] applies the
/// refusals that follow and yields the body to dispatch.
/// Returned by [`KnownOperation::with_body`] and [`parse_request`];
/// [`ParsedRequest::admit`] applies the refusals that follow and yields the
/// body to dispatch.
// Public for its two callers, the HTTP server's `handle_request` and the
// WebAssembly adapter's `Engine::dispatch`, not for other users of the crate.
#[doc(hidden)]
Expand All @@ -34,31 +60,61 @@ pub struct ParsedRequest<'a> {
body: Value,
}

/// The first step: check that `operation` is one the engine dispatches, then
/// parse `body` as JSON.
/// The first step: check that `operation` is one the engine dispatches.
///
/// An unknown operation answers `UnknownOperationException` whatever the
/// body, so an entry point calls this before it parses the body. The check
/// does not read the caller's identity, so the server calls it before it
/// authenticates the request: real `DynamoDB` answers an unknown operation
/// before it checks authentication.
///
/// # Errors
///
/// `UnknownOperationException` for an operation the engine does not
/// dispatch.
// Public for the HTTP server's `handle_request`, not for other users of the
// crate.
#[doc(hidden)]
pub fn check_operation(operation: &str) -> Result<KnownOperation<'_>, DynamoDbError> {
if crate::is_known_operation(operation) {
Ok(KnownOperation { operation })
} else {
Err(DynamoDbError::UnknownOperationException(String::new()))
}
}

/// [`check_operation`], then a parse of `body` as JSON with `serde_json`'s
/// defaults.
///
/// An unknown operation answers `UnknownOperationException` even when the
/// body is also malformed, and a body that does not parse answers the
/// `SerializationException` of [`malformed_body_error`]. Neither reads the
/// caller's identity, so the server calls this before it authenticates the
/// request: real `DynamoDB` answers both before it checks authentication.
/// `SerializationException` of [`malformed_body_error`].
///
/// # Errors
///
/// `UnknownOperationException` for an operation the engine does not
/// dispatch; `SerializationException` for a body that does not parse as JSON.
// Public for its two callers, the HTTP server's `handle_request` and the
// WebAssembly adapter's `Engine::dispatch`, not for other users of the crate.
// Public for the WebAssembly adapter's `Engine::dispatch`, not for other users
// of the crate.
#[doc(hidden)]
pub fn parse_request<'a>(
operation: &'a str,
body: &[u8],
) -> Result<ParsedRequest<'a>, DynamoDbError> {
if !crate::is_known_operation(operation) {
return Err(DynamoDbError::UnknownOperationException(String::new()));
}
let operation = check_operation(operation)?;
let body = serde_json::from_slice(body).map_err(|e| malformed_body_error(&e))?;
Ok(ParsedRequest { operation, body })
Ok(operation.with_body(body))
}

impl<'a> KnownOperation<'a> {
/// Pair the operation with the request body the entry point parsed.
#[must_use]
pub fn with_body(self, body: Value) -> ParsedRequest<'a> {
ParsedRequest {
operation: self.operation,
body,
}
}
}

impl ParsedRequest<'_> {
Expand Down
2 changes: 1 addition & 1 deletion crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ hyper = { workspace = true }
hyper-util = { workspace = true }
tokio = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_json = { workspace = true, features = ["unbounded_depth"] }
tracing = { workspace = true }
crc32fast = { workspace = true }
uuid = { workspace = true }
Expand Down
53 changes: 42 additions & 11 deletions crates/server/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,40 @@ pub(crate) async fn handle_request(
Err(e) => return error_response(&e, &request_id),
};

// --- Pre-auth request checks ---
// Real DynamoDB validates the operation name, then the request format
// (empty body, invalid JSON), before checking authentication: an unknown
// operation returns UnknownOperationException even without auth headers,
// and a malformed request gets 400 regardless of auth state.
// `extenddb_engine::parse_request` makes both checks, in that order; the
// module doc of extenddb-engine's `src/pre_dispatch.rs` says which checks
// belong in the engine rather than here.
let request = match extenddb_engine::parse_request(&operation, &body) {
Ok(request) => request,
// --- Pre-auth operation validation ---
// Real DynamoDB validates the operation name before checking authentication.
// An unknown operation returns UnknownOperationException even without auth
// headers. `extenddb_engine::check_operation` is the first of the engine's
// two steps, and `admit` below is the second; the module doc of
// extenddb-engine's `src/pre_dispatch.rs` says which checks belong in the
// engine rather than here.
let known_operation = match extenddb_engine::check_operation(&operation) {
Ok(known_operation) => known_operation,
Err(e) => return error_response(&e, &request_id),
};

// --- Pre-auth body validation ---
// The service checks the request's JSON syntax before authentication at
// every depth but refuses an over deep body only after it (measured
// 2026-11-19: unsigned, a 20000 wrapper PutItem is
// MissingAuthenticationTokenException, and the same body without its last
// byte SerializationException). A body within serde_json's default depth
// is parsed here, as it always was. A deeper one only has its syntax
// checked, by a walk that does not recurse, until the caller is
// authenticated, so no unauthenticated request makes this thread recurse
// past that depth (see request_parse).
let depth = crate::request_parse::nesting_depth(&body);
let pre_auth_input = if depth <= crate::request_parse::UNAUTHENTICATED_PARSE_DEPTH {
match crate::request_parse::parse_request_value(&body, depth) {
Ok(v) => Some(v),
Err(e) => return error_response(&e, &request_id),
}
} else if let Err(e) = crate::request_parse::check_request_syntax(&body, depth) {
return error_response(&e, &request_id);
} else {
None

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A malformed body deeper than 127 levels skips the syntax check before authentication. Measured 2026-11-16, an unsigned PutItem of x followed by 6000 [ is 400 {"__type":"com.amazon.coral.service#SerializationException"} on the service on 2 of 2 sends, and on the previous head too. Here it is MissingAuthenticationToken, and InvalidSignatureException with a bad signature. Unsigned truncated PutItem bodies at 63, 200 and 2000 wrappers get the SerializationException from the service and the authentication error here. A syntax check that walks the body in one loop like nesting_depth, with a stack of container kinds, would refuse these before authentication without recursing.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 99542cb, the handler runs check_request_syntax before authentication on a body deeper than 127 levels. The check deserializes the body as serde::de::IgnoredAny, which serde_json skips in one loop with a Vec of open container kinds, so it doesn't recurse.

Unsigned and badly signed, x plus 6000 [ and truncated PutItem bodies at 63, 200, 2000 and 20000 wrappers are now a 400 SerializationException. That's the service's status and type on 2 of 2 sends each (us-east-1, 2026-11-20). Past the 5200 level ceiling the whole response body matches. Within it, our response keeps the message field a shallow malformed body has always had, where the service sends none (#1195).

};

// --- Auth segment ---
let auth_start = std::time::Instant::now();
let identity = match state.auth.authenticate(&headers, &body).await {
Expand All @@ -105,6 +126,16 @@ pub(crate) async fn handle_request(
#[allow(clippy::cast_precision_loss)]
let auth_us = auth_start.elapsed().as_micros() as f64;

// A body deeper than the pre-auth depth is parsed now, on a thread whose
// stack `REQUEST_WORKER_STACK_SIZE` sizes for the ceiling.
let input: Value = match pre_auth_input {
Some(v) => v,
None => match crate::request_parse::parse_request_value(&body, depth) {
Ok(v) => v,
Err(e) => return error_response(&e, &request_id),
},
};

let account_id: Arc<str> = match &identity {
extenddb_auth::AuthIdentity::User { account_id, .. }
| extenddb_auth::AuthIdentity::RoleSession { account_id, .. } => {
Expand All @@ -116,7 +147,7 @@ pub(crate) async fn handle_request(
// and an enable here, for every caller and before any table is read, where
// the service would authorize them first. The point-in-time refusal is the
// only one `admit` makes, so the log line names it.
let input: Value = match request.admit() {
let input: Value = match known_operation.with_body(input).admit() {
Ok(input) => input,
Err(e) => {
tracing::warn!(
Expand Down
8 changes: 8 additions & 0 deletions crates/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ mod registration;
mod repeated_headers;
mod request_attributes;
mod request_helpers;
mod request_parse;
mod request_syntax;
mod request_tags;
mod response;
mod serve;
mod table_name_gate;
mod throttle_helpers;
mod workers;

pub use request_parse::REQUEST_WORKER_STACK_SIZE;
pub use serve::{
BuildInfo, CatalogRefusal, JOB_DRAIN_TIMEOUT, LogTarget, RegistrationLost, ServeParams,
WORKER_STOP_TIMEOUT, catalog_refusal, log_to_syslog_raw, serve,
Expand Down Expand Up @@ -145,6 +148,11 @@ pub const CONNECTION_DRAIN_TIMEOUT: Duration = Duration::from_secs(5);
/// It stops on SIGINT or SIGTERM, or when `state`'s admission asks for a
/// stop after the serving registration was lost; both drain the listener
/// the same way and remove the PID file.
///
/// The runtime this runs on must give its threads at least
/// [`REQUEST_WORKER_STACK_SIZE`] of stack: an authenticated request body nested
/// to the parser's ceiling recurses to that depth on the handling thread, and
/// Tokio's default 2 MiB overflows there.
pub async fn start_server(
listener: tokio::net::TcpListener,
state: AppState,
Expand Down
Loading
Loading