Skip to content

Commit c3aab3a

Browse files
committed
Format: a color's mods list holds at most 256 transforms
color_value reads a mods list element by element instead of cloning it, and a list longer than MAX_COLOR_MODS (256) makes the color invalid.
1 parent 2427c60 commit c3aab3a

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

‎crates/engine/src/cmd.rs‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,16 @@ pub(crate) fn ok() -> Result<Value> {
378378
/// Parse a colour param: `"#RRGGBB"`, `"accent1"`…, or `{"scheme":"accent2","lumMod":75000}`.
379379
/// A preset or system colour keeps its base: `{"preset":"red"}`,
380380
/// `{"system":"windowText","last":"#000000"}`. `"mods"` lists transforms in order:
381-
/// `{"preset":"red","mods":[{"op":"inv"},{"op":"inv"}]}`.
381+
/// `{"preset":"red","mods":[{"op":"inv"},{"op":"inv"}]}`. A list longer than [`MAX_COLOR_MODS`]
382+
/// makes the color invalid.
382383
pub(crate) fn color_param(p: &Value, key: &str) -> Option<deckcraft_model::ColorRef> {
383384
let v = p.get(key)?;
384385
color_value(v)
385386
}
386387

388+
/// Most transforms a color param's `"mods"` list may hold.
389+
pub(crate) const MAX_COLOR_MODS: usize = 256;
390+
387391
pub(crate) fn color_value(v: &Value) -> Option<deckcraft_model::ColorRef> {
388392
use deckcraft_model::ColorRef;
389393
match v {
@@ -420,7 +424,13 @@ pub(crate) fn color_value(v: &Value) -> Option<deckcraft_model::ColorRef> {
420424
}
421425
// `mods` lists transforms in order, repeats included: `[{"op":"inv"},{"op":"lumMod","val":50000}]`.
422426
if let Some(list) = o.get("mods") {
423-
c.mods.extend(serde_json::from_value::<Vec<deckcraft_color::ColorTransform>>(list.clone()).ok()?);
427+
let list = list.as_array()?;
428+
if list.len() > MAX_COLOR_MODS {
429+
return None;
430+
}
431+
for m in list {
432+
c.mods.push(<deckcraft_color::ColorTransform as serde::Deserialize>::deserialize(m).ok()?);
433+
}
424434
}
425435
Some(c)
426436
}
@@ -710,4 +720,13 @@ mod tests {
710720
assert_eq!(color_value(&json!({"scheme": "accent2", "lumMod": 75_000, "mods": [{"op": "lumOff", "val": 10_000}, {"op": "sparkle"}]})), None);
711721
assert_eq!(color_value(&json!({"preset": "red", "mods": {"op": "inv"}})), None);
712722
}
723+
724+
#[test]
725+
fn color_mods_lists_longer_than_the_cap_are_invalid() {
726+
use deckcraft_color::ColorTransform;
727+
use serde_json::json;
728+
let at_cap = color_value(&json!({"preset": "red", "mods": vec![json!({"op": "inv"}); MAX_COLOR_MODS]}));
729+
assert_eq!(at_cap.map(|c| c.mods), Some(vec![ColorTransform::Inv; MAX_COLOR_MODS]));
730+
assert_eq!(color_value(&json!({"preset": "red", "mods": vec![json!({"op": "inv"}); MAX_COLOR_MODS + 1]})), None);
731+
}
713732
}

0 commit comments

Comments
 (0)