diff --git a/crates/engine/src/partiql/mod.rs b/crates/engine/src/partiql/mod.rs index b461f28dc..1dc26c017 100644 --- a/crates/engine/src/partiql/mod.rs +++ b/crates/engine/src/partiql/mod.rs @@ -57,7 +57,7 @@ mod returning; mod select; mod snapshot; #[cfg(test)] -mod test_context; +pub(crate) mod test_context; mod transaction; mod write; diff --git a/crates/engine/src/partiql/test_context.rs b/crates/engine/src/partiql/test_context.rs index 443514af0..bf44de0f9 100644 --- a/crates/engine/src/partiql/test_context.rs +++ b/crates/engine/src/partiql/test_context.rs @@ -30,7 +30,7 @@ pub(super) fn block_on(future: F) -> F::Output { } /// A context whose item authorizer is `authorizer`. -pub(super) async fn context(authorizer: Option>) -> OperationContext { +pub(crate) async fn context(authorizer: Option>) -> OperationContext { let storage = SqliteEngine::new(":memory:", 1, "us-east-1", 409_600) .await .expect("an in-memory database opens"); diff --git a/crates/engine/src/streams.rs b/crates/engine/src/streams.rs index 28fbb7b6f..36af7e4b7 100755 --- a/crates/engine/src/streams.rs +++ b/crates/engine/src/streams.rs @@ -16,25 +16,45 @@ use crate::OperationContext; use crate::serialize_output; use crate::streams_iterator_params as iterator_params; +/// Largest `Limit` `DescribeStream` accepts. The API Reference gives the +/// range 1 to 100 ("The upper limit is 100", minimum value 1, +/// ); +/// the service's refusals are quoted in `streams/describe_limit_tests.rs`. +const DESCRIBE_STREAM_LIMIT_MAX: i64 = 100; + /// Handle `DescribeStream`. /// /// # Errors /// /// Returns [`DynamoDbError::ResourceNotFoundException`] if the stream does not exist. -/// Returns [`DynamoDbError::ValidationException`] on invalid input. +/// Returns [`DynamoDbError::ValidationException`] on invalid input, including +/// a `Limit` below 1 or above 100. pub async fn handle_describe_stream( body: Value, ctx: &OperationContext, ) -> Result { let input: DescribeStreamInput = serde_json::from_value(body) .map_err(|e| DynamoDbError::SerializationException(e.to_string()))?; + // The service names every failed member constraint in one message before + // it looks the stream up, and refuses a Limit above 100 only once it has + // found the stream (measured in us-east-1; the answers are quoted in + // streams/describe_limit_tests.rs). + let mut clauses = Vec::new(); if let Some(start) = &input.exclusive_start_shard_id && start.chars().count() < SHARD_ID_MIN_LEN { - return Err(DynamoDbError::ValidationException(constraint( + clauses.push(clause( "exclusiveStartShardId", "have length greater than or equal to 28", - ))); + )); + } + if input.limit.is_some_and(|limit| limit < 1) { + clauses.push(clause("limit", "have value greater than or equal to 1")); + } + if !clauses.is_empty() { + return Err(DynamoDbError::ValidationException( + extenddb_core::error::validation_errors_detected(&clauses), + )); } let desc = ctx @@ -42,6 +62,15 @@ pub async fn handle_describe_stream( .describe_stream(&ctx.account_id, &input) .await .map_err(storage_to_dynamo)?; + if input + .limit + .is_some_and(|limit| limit > DESCRIBE_STREAM_LIMIT_MAX) + { + return Err(DynamoDbError::ValidationException(format!( + "One or more parameter values were invalid: Limit cannot be greater than \ + the maximum: {DESCRIBE_STREAM_LIMIT_MAX}" + ))); + } let output = DescribeStreamOutput { stream_description: desc, @@ -554,3 +583,6 @@ fn storage_to_dynamo(e: StorageError) -> DynamoDbError { #[cfg(test)] #[path = "streams_tests.rs"] mod tests; + +#[cfg(all(test, feature = "partiql"))] +mod describe_limit_tests; diff --git a/crates/engine/src/streams/describe_limit_tests.rs b/crates/engine/src/streams/describe_limit_tests.rs new file mode 100644 index 000000000..a171d565d --- /dev/null +++ b/crates/engine/src/streams/describe_limit_tests.rs @@ -0,0 +1,138 @@ +// Copyright 2026 ExtendDB contributors +// SPDX-License-Identifier: Apache-2.0 + +//! `DescribeStream` refuses a `Limit` below 1 together with the other member +//! constraints before it looks the stream up, and a `Limit` above 100 only +//! after it has found the stream. +//! +//! Measured in us-east-1 with the SDK's own parameter validation switched +//! off, so the requests reached the service. On 2026-10-22, on an existing +//! stream: +//! +//! ```text +//! Limit 0 and Limit -1: +//! 400 {"__type":"com.amazon.coral.validate#ValidationException","message":"1 validation error detected: Value at 'limit' failed to satisfy constraint: Member must have value greater than or equal to 1"} +//! Limit 101 and Limit 1000: +//! 400 {"__type":"com.amazon.coral.validate#ValidationException","message":"One or more parameter values were invalid: Limit cannot be greater than the maximum: 100"} +//! ``` +//! +//! On 2026-11-25, each request twice, on a stream of a table that does not +//! exist: Limit 0 and -1 got the first body above; Limit 1, 101 and 1000, and +//! Limit 101 with a 36-character `ExclusiveStartShardId`, got 400 +//! `ResourceNotFoundException` "Requested resource not found: Stream: +//! not found"; Limit 0 with a 13-character `ExclusiveStartShardId` got the +//! two clauses in one message ([`SHORT_START_AND_BELOW_ONE`]); Limit 101 with +//! that start got the start clause alone ([`SHORT_START`]). +//! +//! The tests run the handler on the engine's test context over a migrated +//! in-memory engine, so a request that reaches storage for a missing stream +//! gets `ResourceNotFoundException`. + +use std::sync::Arc; + +use serde_json::json; + +use extenddb_core::error::DynamoDbError; + +use super::handle_describe_stream; +use crate::OperationContext; +use crate::partiql::test_context::context; + +const MISSING: &str = + "arn:aws:dynamodb:us-east-1:000000000000:table/Missing/stream/2026-10-22T00:00:00.000"; +const SHORT_ID: &str = "shardId-short"; +const VALID_ID: &str = "shardId-00000001700000000000-00000000"; + +const BELOW_ONE: &str = "1 validation error detected: Value at 'limit' failed to satisfy \ + constraint: Member must have value greater than or equal to 1"; +const ABOVE_MAX: &str = + "One or more parameter values were invalid: Limit cannot be greater than the maximum: 100"; +const SHORT_START: &str = "1 validation error detected: Value at 'exclusiveStartShardId' \ + failed to satisfy constraint: Member must have length greater \ + than or equal to 28"; +const SHORT_START_AND_BELOW_ONE: &str = "2 validation errors detected: Value at \ + 'exclusiveStartShardId' failed to satisfy constraint: Member must have length greater \ + than or equal to 28; Value at 'limit' failed to satisfy constraint: Member must have \ + value greater than or equal to 1"; + +/// The answer a row expects. +enum Answer { + Refused(&'static str), + NotFound, + Served, +} + +/// The test context over a migrated in-memory engine holding the table +/// `Orders` with a stream. Returns the context and the stream's ARN. +async fn context_with_stream() -> (OperationContext, String) { + let mut ctx = context(None).await; + let storage = + extenddb_storage_sqlite::SqliteEngine::new_in_memory("us-east-1", &ctx.account_id, 409_600) + .await + .expect("a migrated in-memory engine opens"); + ctx.storage = Arc::new(storage); + let created = crate::handle_create_table( + json!({ + "TableName": "Orders", + "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}], + "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}], + "BillingMode": "PAY_PER_REQUEST", + "StreamSpecification": {"StreamEnabled": true, "StreamViewType": "KEYS_ONLY"} + }), + &ctx, + ) + .await + .expect("the table is created"); + let arn = created["TableDescription"]["LatestStreamArn"] + .as_str() + .expect("the table has a stream") + .to_owned(); + (ctx, arn) +} + +#[tokio::test] +async fn describe_stream_checks_limit_where_the_service_does() { + let (ctx, existing) = context_with_stream().await; + let not_found = format!("Requested resource not found: Stream: {MISSING} not found"); + let rows: [(&str, i64, Option<&str>, Answer); 14] = [ + (existing.as_str(), 0, None, Answer::Refused(BELOW_ONE)), + (existing.as_str(), -1, None, Answer::Refused(BELOW_ONE)), + (existing.as_str(), 101, None, Answer::Refused(ABOVE_MAX)), + (existing.as_str(), 1000, None, Answer::Refused(ABOVE_MAX)), + (existing.as_str(), 1, None, Answer::Served), + (existing.as_str(), 100, None, Answer::Served), + (MISSING, 0, None, Answer::Refused(BELOW_ONE)), + (MISSING, -1, None, Answer::Refused(BELOW_ONE)), + (MISSING, 1, None, Answer::NotFound), + (MISSING, 101, None, Answer::NotFound), + (MISSING, 1000, None, Answer::NotFound), + ( + MISSING, + 0, + Some(SHORT_ID), + Answer::Refused(SHORT_START_AND_BELOW_ONE), + ), + (MISSING, 101, Some(SHORT_ID), Answer::Refused(SHORT_START)), + (MISSING, 101, Some(VALID_ID), Answer::NotFound), + ]; + for (arn, limit, start, expected) in rows { + let mut body = json!({"StreamArn": arn, "Limit": limit}); + if let Some(start) = start { + body["ExclusiveStartShardId"] = json!(start); + } + let row = format!("{arn} Limit {limit} start {start:?}"); + let answer = handle_describe_stream(body, &ctx).await; + match (answer, expected) { + (Err(DynamoDbError::ValidationException(m)), Answer::Refused(e)) => { + assert_eq!(m, e, "{row}"); + } + (Err(DynamoDbError::ResourceNotFoundException(m)), Answer::NotFound) => { + assert_eq!(m, not_found, "{row}"); + } + (Ok(v), Answer::Served) => { + assert_eq!(v["StreamDescription"]["StreamArn"], json!(arn), "{row}"); + } + (other, _) => panic!("{row}: got {other:?}"), + } + } +} diff --git a/crates/wasm-adapter/parity/corpus.json b/crates/wasm-adapter/parity/corpus.json index bb8840a7a..131dd30e1 100644 --- a/crates/wasm-adapter/parity/corpus.json +++ b/crates/wasm-adapter/parity/corpus.json @@ -2858,25 +2858,17 @@ } }, { - "name": "DescribeStream with a Limit above 2^32 returns all four shards and no LastEvaluatedShardId", + "name": "DescribeStream with a Limit above 2^32 is refused as a Limit above 100, not read as a Limit of 1", "op": "DescribeStream", "body": { "StreamArn": "${strmArn}", "Limit": 4294967297 }, - "error": null, + "error": "ValidationException", "checks": [ { - "path": "/StreamDescription/Shards/3/ShardId", - "present": true - }, - { - "path": "/StreamDescription/Shards/4", - "absent": true - }, - { - "path": "/StreamDescription/LastEvaluatedShardId", - "absent": true + "path": "/message", + "equals": "One or more parameter values were invalid: Limit cannot be greater than the maximum: 100" } ] },