mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 10:13:09 -06:00
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.
This commit is contained in:
@@ -378,12 +378,16 @@ pub(crate) fn ok() -> Result<Value> {
|
||||
/// Parse a colour param: `"#RRGGBB"`, `"accent1"`…, or `{"scheme":"accent2","lumMod":75000}`.
|
||||
/// A preset or system colour keeps its base: `{"preset":"red"}`,
|
||||
/// `{"system":"windowText","last":"#000000"}`. `"mods"` lists transforms in order:
|
||||
/// `{"preset":"red","mods":[{"op":"inv"},{"op":"inv"}]}`.
|
||||
/// `{"preset":"red","mods":[{"op":"inv"},{"op":"inv"}]}`. A list longer than [`MAX_COLOR_MODS`]
|
||||
/// makes the color invalid.
|
||||
pub(crate) fn color_param(p: &Value, key: &str) -> Option<deckcraft_model::ColorRef> {
|
||||
let v = p.get(key)?;
|
||||
color_value(v)
|
||||
}
|
||||
|
||||
/// Most transforms a color param's `"mods"` list may hold.
|
||||
pub(crate) const MAX_COLOR_MODS: usize = 256;
|
||||
|
||||
pub(crate) fn color_value(v: &Value) -> Option<deckcraft_model::ColorRef> {
|
||||
use deckcraft_model::ColorRef;
|
||||
match v {
|
||||
@@ -420,7 +424,13 @@ pub(crate) fn color_value(v: &Value) -> Option<deckcraft_model::ColorRef> {
|
||||
}
|
||||
// `mods` lists transforms in order, repeats included: `[{"op":"inv"},{"op":"lumMod","val":50000}]`.
|
||||
if let Some(list) = o.get("mods") {
|
||||
c.mods.extend(serde_json::from_value::<Vec<deckcraft_color::ColorTransform>>(list.clone()).ok()?);
|
||||
let list = list.as_array()?;
|
||||
if list.len() > MAX_COLOR_MODS {
|
||||
return None;
|
||||
}
|
||||
for m in list {
|
||||
c.mods.push(<deckcraft_color::ColorTransform as serde::Deserialize>::deserialize(m).ok()?);
|
||||
}
|
||||
}
|
||||
Some(c)
|
||||
}
|
||||
@@ -710,4 +720,13 @@ mod tests {
|
||||
assert_eq!(color_value(&json!({"scheme": "accent2", "lumMod": 75_000, "mods": [{"op": "lumOff", "val": 10_000}, {"op": "sparkle"}]})), None);
|
||||
assert_eq!(color_value(&json!({"preset": "red", "mods": {"op": "inv"}})), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn color_mods_lists_longer_than_the_cap_are_invalid() {
|
||||
use deckcraft_color::ColorTransform;
|
||||
use serde_json::json;
|
||||
let at_cap = color_value(&json!({"preset": "red", "mods": vec![json!({"op": "inv"}); MAX_COLOR_MODS]}));
|
||||
assert_eq!(at_cap.map(|c| c.mods), Some(vec![ColorTransform::Inv; MAX_COLOR_MODS]));
|
||||
assert_eq!(color_value(&json!({"preset": "red", "mods": vec![json!({"op": "inv"}); MAX_COLOR_MODS + 1]})), None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user