Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
aa1c8b0
fix(core): read a B value with its padding left out or cut short
LeeroyHannigan Nov 21, 2026
1773e2d
fix(engine): read an ImportTable B value only in the canonical form
LeeroyHannigan Nov 22, 2026
1fe5eff
fix(engine): read a BatchWriteItem B value only with its padding
LeeroyHannigan Nov 22, 2026
38ee3ea
Merge fix/1189-base64-texts into fix/1258-unpadded-base64
LeeroyHannigan Nov 23, 2026
738c712
Merge fix/1189-base64-texts into fix/1258-unpadded-base64
LeeroyHannigan Nov 25, 2026
e030be2
test(engine): say why the two nested import rows expect a refusal
LeeroyHannigan Nov 25, 2026
05c9baf
Merge fix/1189-base64-texts into fix/1258-unpadded-base64
LeeroyHannigan Nov 25, 2026
d53502b
Merge fix/1189-base64-texts into fix/1258-unpadded-base64
LeeroyHannigan Dec 2, 2026
6b21d47
fix(engine): check only the items and keys of BatchWriteItem for padding
LeeroyHannigan Dec 2, 2026
6512dcd
Merge fix/1189-base64-texts into fix/1258-unpadded-base64
LeeroyHannigan Dec 2, 2026
c6589e0
Merge fix/1189-base64-texts into fix/1258-unpadded-base64
LeeroyHannigan Dec 3, 2026
3aadd30
docs(upgrade): list the read of a B value without its full padding
LeeroyHannigan Dec 3, 2026
7dfea6a
fix(engine): report a fault ahead of a B in one value as DynamoDB does
LeeroyHannigan Dec 3, 2026
6825c8e
docs(upgrade): list the BatchWriteItem answers that the read order ch…
LeeroyHannigan Dec 3, 2026
835b5ae
docs(upgrade): give the 1.1.0 answer of BatchWriteItem as the text of…
LeeroyHannigan Dec 3, 2026
f7f1849
Merge fix/1189-base64-texts into fix/1258-unpadded-base64
LeeroyHannigan Dec 3, 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
14 changes: 10 additions & 4 deletions crates/core/src/partiql/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use serde::Deserialize;
use serde_json::{Map, Value};

use super::number::{check_number, parameter_message};
use crate::types::{AttributeValue, decode_base64};
use crate::types::{AttributeValue, decode_padded_base64};
use crate::validation::MAX_ITEM_NESTING_DEPTH;
pub use crate::validation::number::NUMBER_SET_DUPLICATES;
use crate::validation::number::has_equal_numbers;
Expand Down Expand Up @@ -107,7 +107,7 @@ fn check_types(value: &Value) -> Result<(), String> {
for (tag, payload) in typed_members(members) {
match (tag, payload) {
("S" | "N", Value::String(_)) | ("NULL" | "BOOL", Value::Bool(_)) => {}
("B", Value::String(text)) => drop(decode_base64(text)?),
("B", Value::String(text)) => drop(decode_padded_base64(text)?),
("SS" | "NS", Value::Array(items)) => {
if items
.iter()
Expand All @@ -119,7 +119,7 @@ fn check_types(value: &Value) -> Result<(), String> {
("BS", Value::Array(items)) => {
for item in items {
match item {
Value::String(text) => drop(decode_base64(text)?),
Value::String(text) => drop(decode_padded_base64(text)?),
Value::Null => {}
_ => return Err("BS member must be a base64 string".to_owned()),
}
Expand Down Expand Up @@ -582,10 +582,16 @@ mod tests {
),
(deep_bool.as_str(), "BOOL value must be a boolean"),
// A `B` value or a `BS` member the service cannot decode, with
// its text (measured 2026-11-20, 2026-11-21 and 2026-11-25).
// its text (measured 2026-11-20, 2026-11-21 and 2026-11-25). A
// parameter must carry its `=` padding, where an item may leave
// it out (measured 2026-11-21).
("[{\"B\":\"%%%\"}]", LENGTH_3),
("[{\"BS\":[\"%%%\"]}]", LENGTH_3),
("[{\"B\":\"AQ=\"}]", LENGTH_3),
(
"[{\"B\":\"YWJjZA\"}]",
"Base64 encoded length is expected a multiple of 4 bytes but found: 6",
),
("[{\"B\":\"!!!!\"}]", ""),
] {
assert_eq!(
Expand Down
77 changes: 70 additions & 7 deletions crates/core/src/types/attribute_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use base64::engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig};
use serde::de::{self, MapAccess, Visitor};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -82,10 +83,28 @@ const BASE64_TEXTS: [&str; 3] = [
"Invalid last non-pad Base64 character dectected",
];

/// Decode the text of a `B` value or a `BS` member.
/// The decoder of a `B` value or a `BS` member outside a PartiQL parameter.
const UNPADDED: GeneralPurpose = GeneralPurpose::new(
&base64::alphabet::STANDARD,
GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent),
);

/// Decode the text of a `B` value or a `BS` member. Measured 2026-11-21: the
/// service reads it with its `=` padding left out or cut short, in an item
/// and in an expression attribute value.
pub fn decode_base64(text: &str) -> Result<Vec<u8>, String> {
decode_with(&UNPADDED, text)
}

/// Decode the text of a `B` value or a `BS` member of a PartiQL parameter,
/// which must carry its `=` padding (measured 2026-11-21).
pub fn decode_padded_base64(text: &str) -> Result<Vec<u8>, String> {
decode_with(&BASE64, text)
}

fn decode_with(engine: &GeneralPurpose, text: &str) -> Result<Vec<u8>, String> {
let [too_short, length, last_character] = BASE64_TEXTS;
BASE64.decode(text).map_err(|_| match text.len() {
engine.decode(text).map_err(|_| match text.len() {
1 => too_short.to_owned(),
2 if text == "==" => String::new(),
n if n % 4 != 0 => format!("{length}{n}"),
Expand All @@ -107,6 +126,43 @@ pub fn is_base64_refusal(message: &str) -> bool {
message.is_empty() || BASE64_TEXTS.iter().any(|text| message.starts_with(text))
}

/// Check the `B` text and each string member of a `BS` of a wire attribute
/// value with `decode`, in the values of its `M` and `L` too, in the order
/// the deserializer reads them. An operation whose decoder is not the
/// deserializer's calls it on the raw JSON ahead of deserializing. A payload
/// of another JSON type, a `BS` member that is not a string and a value that
/// is not a JSON object are left to the deserializer, which refuses them.
/// `serde_json` caps the depth at 128.
///
/// # Errors
///
/// The refusal of the first text `decode` refuses.
pub fn check_binaries<E>(
value: &serde_json::Value,
decode: fn(&str) -> Result<Vec<u8>, E>,
) -> Result<(), E> {
use serde_json::Value;
let Value::Object(members) = value else {
return Ok(());
};
members
.iter()
.try_for_each(|(tag, payload)| match (tag.as_str(), payload) {
("B", Value::String(text)) => decode(text).map(drop),
("BS", Value::Array(set)) => set
.iter()
.filter_map(Value::as_str)
.try_for_each(|text| decode(text).map(drop)),
("M", Value::Object(entries)) => entries
.values()
.try_for_each(|entry| check_binaries(entry, decode)),
("L", Value::Array(items)) => items
.iter()
.try_for_each(|item| check_binaries(item, decode)),
_ => Ok(()),
})
}

impl<'de> Visitor<'de> for AttributeValueVisitor {
type Value = AttributeValue;

Expand Down Expand Up @@ -738,10 +794,11 @@ mod tests {
);
}

/// The service's answer to each `B` value (measured 2026-11-20, and
/// 2026-11-25 for `//==` and `++==`): the decoded bytes, or the text of
/// the refusal, empty for no message. A `BS` member, and a value inside
/// an `M` or an `L`, get the same text.
/// The service's answer to each `B` value (measured 2026-11-20,
/// 2026-11-21 for the values with their padding left out or cut short,
/// and 2026-11-25 for `//==` and `++==`): the decoded bytes, or the text
/// of the refusal, empty for no message. A `BS` member, and a value
/// inside an `M` or an `L`, get the same text.
#[test]
fn a_value_that_is_not_base64_gets_the_service_text() {
let short = || Err("Buffer is too short for base64 encoding".to_owned());
Expand All @@ -752,12 +809,17 @@ mod tests {
};
let last = || Err("Invalid last non-pad Base64 character dectected".to_owned());
let none = || Err(String::new());
let rows: [(&str, Result<&str, String>); 50] = [
let rows: [(&str, Result<&str, String>); 56] = [
("YWJj", Ok("abc")),
("YQ==", Ok("a")),
("YWE=", Ok("aa")),
("AA==", Ok("\0")),
("AAA=", Ok("\0\0")),
("YQ", Ok("a")),
("YQ=", Ok("a")),
("YWJjZA", Ok("abcd")),
("YWJjZA=", Ok("abcd")),
("YWI", Ok("ab")),
("a", short()),
("Y", short()),
("!", short()),
Expand All @@ -770,6 +832,7 @@ mod tests {
("===", length(3)),
("A==", length(3)),
("YR=", length(3)),
("YWJ", length(3)),
("YQ==\n", length(5)),
("Y Q==", length(5)),
("abcde", length(5)),
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ mod transaction;
mod warm_throughput;

pub use attribute_value::{
AttributeValue, EMPTY_ATTRIBUTE_VALUE, MULTI_ATTRIBUTE_VALUE, decode_base64, is_base64_refusal,
AttributeValue, EMPTY_ATTRIBUTE_VALUE, MULTI_ATTRIBUTE_VALUE, check_binaries, decode_base64,
decode_padded_base64, is_base64_refusal,
};
pub use backup::{
BackupDescription, BackupDetails, BackupSummary, ContinuousBackupsDescription,
Expand Down
54 changes: 53 additions & 1 deletion crates/engine/src/batch_write_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use serde_json::Value;
use extenddb_core::error::DynamoDbError;
use extenddb_core::expression::ExpressionMaps;
use extenddb_core::limits::LimitsConfig;
use extenddb_core::partiql::params::check_parameter_types;
use extenddb_core::types::{
BatchWriteItemInput, BatchWriteItemOutput, Item, KeySchemaElement, ReturnItemCollectionMetrics,
TableKeyInfo, WriteRequest, extract_key, item_size_bytes,
TableKeyInfo, WriteRequest, extract_key, is_base64_refusal, item_size_bytes,
};
use extenddb_core::validation::{
AttributeNameRequestForm, IndexKeyRef, validate_attribute_name_sizes, validate_batch_item_keys,
Expand Down Expand Up @@ -45,6 +46,14 @@ pub async fn handle_batch_write_item(
body: Value,
ctx: &OperationContext,
) -> Result<DispatchResult, DynamoDbError> {
// The service reads a B value or a BS member of this operation only with
// its padding, in a PutRequest item and in a DeleteRequest key. Every
// other operation reads the padding left out or cut short (measured
// 2026-11-22), and so does the deserializer, so the items and keys are
// checked with the decoder that requires the padding first.
if let Some(text) = padding_refusal(&body["RequestItems"]) {
return Err(crate::serialization_exception(text));
}
let input: BatchWriteItemInput =
serde_json::from_value(body).map_err(crate::deserialize_error)?;

Expand Down Expand Up @@ -117,6 +126,47 @@ pub async fn handle_batch_write_item(
.await
}

/// The attribute values of the items and keys of `RequestItems`, in the
/// order the deserializer reads them: the `Item` of each `PutRequest` and
/// the `Key` of each `DeleteRequest`. A member that `WriteRequest`,
/// `PutRequest` or `DeleteRequest` does not model is skipped, as the
/// deserializer skips it.
fn written_values(request_items: &Value) -> impl Iterator<Item = &Value> {
request_items
.as_object()
.into_iter()
.flat_map(serde_json::Map::values)
.filter_map(Value::as_array)
.flatten()
.filter_map(Value::as_object)
.flatten()
.filter_map(|(member, request)| match member.as_str() {
"PutRequest" => request.get("Item"),
"DeleteRequest" => request.get("Key"),
_ => None,
})
.filter_map(Value::as_object)
.flat_map(serde_json::Map::values)
}

/// The refusal of the first `B` value or `BS` member of the
/// [`written_values`] that does not decode with its padding, unless a fault
/// of the wrong JSON type comes first, which the deserializer reports. The
/// service reads the items and keys in request order, inside a value too,
/// as it reads PartiQL parameters in its first pass, so
/// [`check_parameter_types`] reads them. Measured 2026-12-02 and 2026-12-03,
/// each body twice, on a table that does not exist: `{"S": 1}` ahead of
/// `{"B": "YQ"}` answers `NUMBER_VALUE cannot be converted to String`, in an
/// item, an `M` and an `L`, and so does `{"SS": [1]}`. A `BS` member that is
/// an object or a number is a fault at its place. An unknown type
/// descriptor, a second type descriptor, a null payload and a null member of
/// a `BS` or an `L` are not: the length text of `YQ` comes first.
fn padding_refusal(request_items: &Value) -> Option<String> {
written_values(request_items)
.find_map(|value| check_parameter_types(std::slice::from_ref(value)).err())
.filter(|text| is_base64_refusal(text))
}

/// Check the whole batch, then write it.
///
/// The table lookups, the duplicate-key check and the validation of every
Expand Down Expand Up @@ -578,6 +628,8 @@ fn collect_icm_if_needed(
}
}

#[cfg(test)]
mod padding_tests;
#[cfg(test)]
mod scripted;
#[cfg(test)]
Expand Down
Loading
Loading