use crate::mxc_common::mxc_error::MxcError;
use serde::{Deserialize, Serialize};
use serde_json::Value;
pub(crate) const MAX_REQUEST_BYTES: usize = 8 * 1024 * 1024;
pub(crate) const MAX_RESULT_BYTES: usize = 32 * 1024 * 1024;
pub(crate) const MAX_CHANGES: usize = 4096;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum PolicyChangeOperation {
Add,
Remove,
Replace,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct PolicyChange {
pub operation: PolicyChangeOperation,
pub path: String,
#[serde(deserialize_with = "required_value")]
pub before: Value,
#[serde(deserialize_with = "required_value")]
pub after: Value,
}
fn required_value<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Value, D::Error> {
UniqueKeysValue::deserialize(deserializer).map(|value| value.0)
}
struct UniqueKeysValue(Value);
impl<'de> Deserialize<'de> for UniqueKeysValue {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(UniqueKeysVisitor).map(Self)
}
}
struct UniqueKeysVisitor;
impl<'de> serde::de::Visitor<'de> for UniqueKeysVisitor {
type Value = Value;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("a JSON value")
}
fn visit_unit<E>(self) -> Result<Value, E> {
Ok(Value::Null)
}
fn visit_none<E>(self) -> Result<Value, E> {
Ok(Value::Null)
}
fn visit_some<D: serde::Deserializer<'de>>(self, deserializer: D) -> Result<Value, D::Error> {
UniqueKeysValue::deserialize(deserializer).map(|value| value.0)
}
fn visit_bool<E>(self, value: bool) -> Result<Value, E> {
Ok(Value::Bool(value))
}
fn visit_i64<E>(self, value: i64) -> Result<Value, E> {
Ok(value.into())
}
fn visit_u64<E>(self, value: u64) -> Result<Value, E> {
Ok(value.into())
}
fn visit_f64<E>(self, value: f64) -> Result<Value, E> {
Ok(serde_json::Number::from_f64(value).map_or(Value::Null, Value::Number))
}
fn visit_str<E>(self, value: &str) -> Result<Value, E> {
Ok(Value::String(value.to_owned()))
}
fn visit_string<E>(self, value: String) -> Result<Value, E> {
Ok(Value::String(value))
}
fn visit_seq<A: serde::de::SeqAccess<'de>>(self, mut seq: A) -> Result<Value, A::Error> {
let mut values = Vec::new();
while let Some(UniqueKeysValue(value)) = seq.next_element()? {
values.push(value);
}
Ok(Value::Array(values))
}
fn visit_map<A: serde::de::MapAccess<'de>>(self, mut map: A) -> Result<Value, A::Error> {
let mut object = serde_json::Map::new();
while let Some(key) = map.next_key::<String>()? {
if object.contains_key(&key) {
return Err(serde::de::Error::custom("duplicate JSON object key"));
}
let UniqueKeysValue(value) = map.next_value()?;
object.insert(key, value);
}
Ok(Value::Object(object))
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct PreparedPolicyRequest {
pub request_json: String,
pub changes: Vec<PolicyChange>,
}
impl PreparedPolicyRequest {
pub(crate) fn validate(&self) -> Result<(), String> {
if self.changes.len() > MAX_CHANGES {
return Err("policy preparation exceeds its change limit".into());
}
if self.request_json.len() > MAX_RESULT_BYTES {
return Err("policy preparation exceeds its result byte limit".into());
}
if !serde_json::from_str::<Value>(&self.request_json)
.map_err(|_| "prepared request is not valid JSON".to_owned())?
.is_object()
{
return Err("prepared request must be a JSON object".into());
}
for change in &self.changes {
if (!change.path.is_empty() && !change.path.starts_with('/'))
|| change
.path
.split('~')
.skip(1)
.any(|tail| !tail.starts_with('0') && !tail.starts_with('1'))
|| matches!(change.operation, PolicyChangeOperation::Add)
&& !change.before.is_null()
|| matches!(change.operation, PolicyChangeOperation::Remove)
&& !change.after.is_null()
{
return Err("invalid policy preparation change".into());
}
}
serde_json::to_writer(&mut ResultSizeCounter::default(), self)
.map_err(|_| "policy preparation exceeds its result byte limit".to_owned())
}
pub(crate) fn parse(json: &str) -> Result<Self, String> {
if json.len() > MAX_RESULT_BYTES {
return Err("policy preparation exceeds its result byte limit".into());
}
let result: Self =
serde_json::from_str(json).map_err(|_| "invalid policy preparation JSON".to_owned())?;
result.validate()?;
Ok(result)
}
}
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
pub(crate) fn collect_changes(
path: &str,
before: Option<&Value>,
after: Option<&Value>,
changes: &mut Vec<PolicyChange>,
) -> Result<(), MxcError> {
if before == after {
return Ok(());
}
if let (Some(Value::Object(old)), Some(Value::Object(new))) = (before, after) {
let keys: std::collections::BTreeSet<_> = old.keys().chain(new.keys()).collect();
for key in keys {
let pointer = format!("{path}/{}", key.replace('~', "~0").replace('/', "~1"));
collect_changes(&pointer, old.get(key), new.get(key), changes)?;
}
return Ok(());
}
if changes.len() >= MAX_CHANGES {
return Err(MxcError::policy_validation(
"policy preparation exceeds its change limit",
));
}
changes.push(PolicyChange {
operation: match (before, after) {
(None, Some(_)) => PolicyChangeOperation::Add,
(Some(_), None) => PolicyChangeOperation::Remove,
_ => PolicyChangeOperation::Replace,
},
path: path.to_owned(),
before: before.cloned().unwrap_or(Value::Null),
after: after.cloned().unwrap_or(Value::Null),
});
Ok(())
}
#[derive(Default)]
struct ResultSizeCounter(usize);
impl std::io::Write for ResultSizeCounter {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.0 = self.0.saturating_add(bytes.len());
if self.0 > MAX_RESULT_BYTES {
return Err(std::io::Error::other(
"policy preparation exceeds its result byte limit",
));
}
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn preparation_changes_describe_exact_json_values_and_pointer_locations() {
let before = json!({
"version": "1.0.0",
"a/b": { "~field": null, "removed": null },
"array": ["first", "second"],
"unchanged": { "optional": null }
});
let after = json!({
"version": "1.0.0",
"a/b": { "~field": false, "added": null },
"array": ["first"],
"unchanged": { "optional": null }
});
let mut changes = Vec::new();
collect_changes("", Some(&before), Some(&after), &mut changes).unwrap();
assert_eq!(changes.len(), 4);
for change in &changes {
match change.operation {
PolicyChangeOperation::Add => {
assert!(before.pointer(&change.path).is_none());
assert_eq!(change.before, Value::Null);
assert_eq!(after.pointer(&change.path), Some(&change.after));
}
PolicyChangeOperation::Remove => {
assert_eq!(before.pointer(&change.path), Some(&change.before));
assert!(after.pointer(&change.path).is_none());
assert_eq!(change.after, Value::Null);
}
PolicyChangeOperation::Replace => {
assert_eq!(before.pointer(&change.path), Some(&change.before));
assert_eq!(after.pointer(&change.path), Some(&change.after));
}
}
}
let null_replacement = changes
.iter()
.find(|change| change.path == "/a~1b/~0field")
.unwrap();
assert_eq!(null_replacement.operation, PolicyChangeOperation::Replace);
assert_eq!(null_replacement.before, Value::Null);
let array = changes
.iter()
.find(|change| change.path == "/array")
.unwrap();
assert_eq!(array.before, before["array"]);
assert_eq!(array.after, after["array"]);
assert!(!changes
.iter()
.any(|change| change.path.starts_with("/unchanged")));
}
#[test]
fn preparation_changes_exceeding_the_budget_fail_instead_of_returning_partial_data() {
let before = json!({});
let mut after = serde_json::Map::new();
for index in 0..MAX_CHANGES {
after.insert(index.to_string(), Value::Bool(true));
}
let mut changes = Vec::new();
collect_changes(
"",
Some(&before),
Some(&Value::Object(after.clone())),
&mut changes,
)
.unwrap();
after.insert("overflow".into(), Value::Bool(true));
let error = collect_changes(
"",
Some(&before),
Some(&Value::Object(after)),
&mut Vec::new(),
)
.unwrap_err();
assert_eq!(
error.code,
crate::mxc_common::mxc_error::MxcErrorCode::PolicyValidation
);
assert_eq!(error.message, "policy preparation exceeds its change limit");
}
#[test]
fn preparation_storage_requires_both_values_and_preserves_real_nulls() {
let original = json!({
"requestJson": "{\"a\":null}",
"changes": [
{"operation":"add","path":"/a~1b","before":null,"after":null},
{"operation":"remove","path":"/removed","before":null,"after":null},
{"operation":"replace","path":"/a","before":1,"after":null}
]
});
let stored = PreparedPolicyRequest::from_json(&original.to_string()).unwrap();
assert_eq!(
serde_json::from_str::<Value>(&stored.to_json().unwrap()).unwrap(),
original,
);
for index in 0..3 {
for field in ["before", "after"] {
let mut incomplete = original.clone();
incomplete["changes"][index]
.as_object_mut()
.unwrap()
.remove(field);
assert!(PreparedPolicyRequest::from_json(&incomplete.to_string()).is_err());
}
}
for (path, operation, before, after) in [
("/a~2b", "replace", Value::Null, Value::Null),
("not-a-pointer", "replace", Value::Null, Value::Null),
("/trailing~", "replace", Value::Null, Value::Null),
("/a", "add", json!(1), Value::Null),
("/a", "remove", Value::Null, json!(1)),
("/a", "unknown", Value::Null, Value::Null),
] {
let invalid = json!({
"requestJson": "{}",
"changes": [{"operation":operation,"path":path,"before":before,"after":after}],
});
assert!(PreparedPolicyRequest::from_json(&invalid.to_string()).is_err());
}
for request_json in ["[]", "1", "null", "\"{}\""] {
let non_object = json!({"requestJson": request_json, "changes": []});
assert!(PreparedPolicyRequest::from_json(&non_object.to_string()).is_err());
}
let stored_change = |before: &str, after: &str| {
format!(
r#"{{"requestJson":"{{}}","changes":[{{"operation":"replace","path":"/a","before":{before},"after":{after}}}]}}"#
)
};
assert!(PreparedPolicyRequest::from_json(&stored_change(
r#"{"x":1,"y":2}"#,
r#"[{"x":1}]"#
))
.is_ok());
for (before, after) in [
(r#"{"x":1,"x":2}"#, "null"),
("null", r#"{"x":1,"x":2}"#),
(r#"{"a":{"x":1,"x":1}}"#, "null"),
("1", r#"[{"x":true},{"y":1,"y":1}]"#),
] {
assert!(PreparedPolicyRequest::from_json(&stored_change(before, after)).is_err());
}
let caller_request = json!({
"requestJson": r#"{"x":1,"x":2}"#,
"changes": [{"operation":"replace","path":"/a","before":{"x":1},"after":[{"y":2}]}],
});
let stored = PreparedPolicyRequest::from_json(&caller_request.to_string()).unwrap();
assert_eq!(stored.request_json, r#"{"x":1,"x":2}"#);
assert_eq!(stored.changes[0].before, json!({"x":1}));
assert_eq!(stored.changes[0].after, json!([{"y":2}]));
}
#[test]
fn preparation_storage_limits_are_whole_result_limits() {
let mut prepared = PreparedPolicyRequest {
request_json: "{}".into(),
changes: (0..MAX_CHANGES)
.map(|index| PolicyChange {
operation: PolicyChangeOperation::Add,
path: format!("/{index}"),
before: Value::Null,
after: json!(index),
})
.collect(),
};
assert_eq!(
PreparedPolicyRequest::from_json(&prepared.to_json().unwrap()).unwrap(),
prepared,
);
prepared.changes.push(prepared.changes[0].clone());
assert!(prepared.to_json().is_err());
prepared.changes.clear();
prepared.request_json = json!({"data": "x".repeat(MAX_REQUEST_BYTES)}).to_string();
prepared
.to_json()
.expect("candidate storage is bounded by the result limit");
prepared.request_json = json!({"data": "x".repeat(MAX_RESULT_BYTES)}).to_string();
assert!(prepared.to_json().is_err());
assert!(PreparedPolicyRequest::from_json(&" ".repeat(MAX_RESULT_BYTES + 1)).is_err());
}
}