mirror of
https://github.com/lexmount/moli.git
synced 2026-11-17 16:09:13 +00:00
fix(indexeddb): bound compound key depth across conversion and storage
This commit is contained in:
@@ -56,6 +56,22 @@ async function keyModelProbe(name = 'key-model-' + Math.random()) {
|
||||
let nested = 1;
|
||||
for (let i = 0; i < 100; i++) nested = [nested];
|
||||
check('deep array key is valid', () => indexedDB.cmp(nested, nested) === 0);
|
||||
let bounded = nested;
|
||||
for (let i = 100; i < 256; i++) bounded = [bounded];
|
||||
const tooDeep = [bounded];
|
||||
check('maximum depth compares and clones', () => indexedDB.cmp(bounded, bounded) === 0);
|
||||
check('maximum depth range materializes', () => {
|
||||
let value = IDBKeyRange.only(bounded).lower, depth = 0;
|
||||
while (Array.isArray(value)) { depth++; value = value[0]; }
|
||||
return depth === 256 && value === 1;
|
||||
});
|
||||
let excessive = tooDeep;
|
||||
for (let i = 257; i < 10000; i++) excessive = [excessive];
|
||||
check('excessive depth range rejects safely', () => invalid(excessive));
|
||||
check('excessive depth comparison rejects safely', () => {
|
||||
try { indexedDB.cmp(excessive, 0); return false; }
|
||||
catch (error) { return error.name === 'DataError'; }
|
||||
});
|
||||
const allBytes = new Uint8Array([9,0,128,255,7]);
|
||||
check('typed array honors offset and length', () => same(IDBKeyRange.only(allBytes.subarray(1,4)).lower, new Uint8Array([0,128,255])));
|
||||
check('DataView honors offset and length', () => same(IDBKeyRange.only(new DataView(allBytes.buffer,2,2)).lower,new Uint8Array([128,255])));
|
||||
@@ -82,6 +98,7 @@ async function keyModelProbe(name = 'key-model-' + Math.random()) {
|
||||
key=>IDBKeyRange.lowerBound(key), key=>IDBKeyRange.upperBound(key),
|
||||
key=>IDBKeyRange.bound(key,0), key=>IDBKeyRange.bound(0,key), key=>IDBKeyRange.only(0).includes(key)
|
||||
];
|
||||
staticConsumers.forEach((consume,i) => check('static conversion '+i+': depth boundary', () => namedError(() => consume(tooDeep), 'DataError')));
|
||||
for(const [i,error] of [undefined,17,{sentinel:true},new TypeError('original')].entries()) {
|
||||
staticConsumers.forEach((consume,j)=>{
|
||||
check('static conversion '+j+': exact exception '+i,()=>{
|
||||
@@ -108,6 +125,7 @@ async function keyModelProbe(name = 'key-model-' + Math.random()) {
|
||||
];
|
||||
consumers.forEach((consume,i)=>{
|
||||
check('operation '+i+': buffer TypeError',()=>namedError(()=>consume(resizable),'TypeError'));
|
||||
check('operation '+i+': excessive depth DataError',()=>namedError(()=>consume(tooDeep),'DataError'));
|
||||
const sentinel={operation:i};
|
||||
const throwing=Object.defineProperty([],0,{get(){throw sentinel;}});
|
||||
check('operation '+i+': exact getter exception',()=>{try{consume(throwing);return false;}catch(error){return error===sentinel;}});
|
||||
|
||||
@@ -13,7 +13,7 @@ async fn indexed_db_keys_preserve_types_order_and_binary_snapshots() -> Result<(
|
||||
assert_eq!(result["state"], "pass", "{target}: {result}");
|
||||
assert_eq!(
|
||||
result["checks"].as_array().unwrap().len(),
|
||||
259,
|
||||
288,
|
||||
"{target}: {result}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,11 @@ use std::cmp::Ordering;
|
||||
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
/// Maximum array nesting accepted by key conversion and persistent storage.
|
||||
/// Keep recursive comparison, cloning, dropping and JS materialization bounded.
|
||||
pub const MAX_KEY_DEPTH: usize = 256;
|
||||
const KEY_DEPTH_ERROR: &str = "IndexedDB array key exceeds the maximum nesting depth";
|
||||
|
||||
/// A Number key cannot contain NaN. Preserve its bits for value roundtrips;
|
||||
/// equality and ordering treat positive and negative zero as the same key.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@@ -56,6 +61,19 @@ impl Key {
|
||||
pub fn number(value: f64) -> Option<Self> {
|
||||
KeyNumber::new(value).map(Self::Number)
|
||||
}
|
||||
|
||||
fn validate_depth(&self) -> Result<(), &'static str> {
|
||||
let mut pending = vec![(self, 0)];
|
||||
while let Some((key, depth)) = pending.pop() {
|
||||
if let Self::Array(keys) = key {
|
||||
if depth >= MAX_KEY_DEPTH {
|
||||
return Err(KEY_DEPTH_ERROR);
|
||||
}
|
||||
pending.extend(keys.iter().map(|key| (key, depth + 1)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// The original on-disk format used Integer and UTF-8 String variants. Continue
|
||||
@@ -86,6 +104,7 @@ enum StoredKeyRef<'a> {
|
||||
|
||||
impl Serialize for Key {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
self.validate_depth().map_err(serde::ser::Error::custom)?;
|
||||
match self {
|
||||
Self::Number(number) => StoredKeyRef::Number(number.0),
|
||||
Self::Date(ms) => StoredKeyRef::Date(*ms),
|
||||
@@ -120,7 +139,10 @@ fn flat_key_parts(key: &Key) -> Vec<StoredKeyRef<'_>> {
|
||||
|
||||
impl<'de> Deserialize<'de> for Key {
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
decode_stored_key(StoredKey::deserialize(deserializer)?).map_err(serde::de::Error::custom)
|
||||
let key = decode_stored_key(StoredKey::deserialize(deserializer)?)
|
||||
.map_err(serde::de::Error::custom)?;
|
||||
key.validate_depth().map_err(serde::de::Error::custom)?;
|
||||
Ok(key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,6 +177,9 @@ fn decode_array_parts(parts: Vec<StoredKey>) -> Result<Key, &'static str> {
|
||||
if result.is_some() {
|
||||
return Err("trailing compound key parts");
|
||||
}
|
||||
if matches!(part, StoredKey::ArrayLength(_)) && frames.len() >= MAX_KEY_DEPTH {
|
||||
return Err(KEY_DEPTH_ERROR);
|
||||
}
|
||||
let mut key = match part {
|
||||
StoredKey::ArrayLength(0) => Key::Array(Vec::new()),
|
||||
StoredKey::ArrayLength(length) => {
|
||||
@@ -256,7 +281,7 @@ impl From<&str> for KeyPath {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Key, KeyPath};
|
||||
use super::{Key, KeyPath, MAX_KEY_DEPTH};
|
||||
|
||||
#[test]
|
||||
fn key_order_and_json_roundtrip_preserve_all_key_types() {
|
||||
@@ -316,6 +341,48 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compound_key_depth_is_bounded_for_flat_and_legacy_storage() {
|
||||
let mut boundary = Key::from(1);
|
||||
for _ in 0..MAX_KEY_DEPTH {
|
||||
boundary = Key::Array(vec![boundary]);
|
||||
}
|
||||
let accepted = serde_json::to_string(&boundary).unwrap();
|
||||
assert_eq!(serde_json::from_str::<Key>(&accepted).unwrap(), boundary);
|
||||
let legacy_outer = format!(r#"{{"Array":[{accepted}]}}"#);
|
||||
assert!(serde_json::from_str::<Key>(&legacy_outer).is_err());
|
||||
assert!(serde_json::to_vec(&Key::Array(vec![boundary])).is_err());
|
||||
for depth in [MAX_KEY_DEPTH + 1, 10_000] {
|
||||
for terminal in [r#"{"Integer":1}"#, r#"{"ArrayLength":0}"#] {
|
||||
let arrays = r#"{"ArrayLength":1},"#.repeat(depth);
|
||||
let wire = format!(r#"{{"ArrayParts":[{arrays}{terminal}]}}"#);
|
||||
let error = serde_json::from_str::<Key>(&wire).unwrap_err();
|
||||
assert!(error.to_string().contains("maximum nesting depth"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maximum_depth_keys_fit_recursive_operations_on_a_bounded_native_stack() {
|
||||
std::thread::Builder::new()
|
||||
.stack_size(512 * 1024)
|
||||
.spawn(|| {
|
||||
let mut key = Key::from(1);
|
||||
for _ in 0..MAX_KEY_DEPTH {
|
||||
key = Key::Array(vec![key]);
|
||||
}
|
||||
let copy = key.clone();
|
||||
assert_eq!(key.cmp(©), std::cmp::Ordering::Equal);
|
||||
let wire = serde_json::to_vec(&key).unwrap();
|
||||
assert_eq!(serde_json::from_slice::<Key>(&wire).unwrap(), copy);
|
||||
drop(key);
|
||||
drop(copy);
|
||||
})
|
||||
.unwrap()
|
||||
.join()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_keys_decode_into_the_same_ordered_key_model() {
|
||||
let key: Key =
|
||||
|
||||
@@ -24,7 +24,7 @@ pub use cursor::{
|
||||
compare_cursor_direction, compare_cursor_tuple_direction,
|
||||
};
|
||||
pub use error::IndexedDbError;
|
||||
pub use key::{Key, KeyNumber, KeyPath};
|
||||
pub use key::{Key, KeyNumber, KeyPath, MAX_KEY_DEPTH};
|
||||
pub use options::{
|
||||
GetAllOptionsCandidate, IndexOptionsValidationError, ObjectStoreOptionsValidationError,
|
||||
TransactionModeParseError, parse_regular_transaction_mode, should_parse_get_all_options,
|
||||
|
||||
@@ -74,6 +74,9 @@ fn parse_idb_key_value<'s>(
|
||||
let mut input = value;
|
||||
loop {
|
||||
let mut key = if let Ok(array) = v8::Local::<v8::Array>::try_from(input) {
|
||||
if frames.len() >= moli_indexeddb::MAX_KEY_DEPTH {
|
||||
return Err("IndexedDB array key exceeds the maximum nesting depth.");
|
||||
}
|
||||
// Reject a cycle along the current path, while allowing an array
|
||||
// to supply the same subkey again in a later sibling.
|
||||
if !seen.insert(array) {
|
||||
|
||||
Reference in New Issue
Block a user