1use js_int::int;
2use ruma_common::{
3 CanonicalJsonObject, CanonicalJsonValue, ID_MAX_BYTES, RoomId,
4 room_version_rules::EventFormatRules,
5};
6use ruma_signatures::MAX_PDU_BYTES;
7use serde_json::to_string as to_json_string;
8
9const MAX_PREV_EVENTS_LENGTH: usize = 20;
13
14const MAX_AUTH_EVENTS_LENGTH: usize = 10;
18
19pub fn check_pdu_format(pdu: &CanonicalJsonObject, rules: &EventFormatRules) -> Result<(), String> {
41 let json =
43 to_json_string(&pdu).map_err(|e| format!("Failed to serialize canonical JSON: {e}"))?;
44 if json.len() > MAX_PDU_BYTES {
45 return Err(format!("PDU is larger than maximum of {MAX_PDU_BYTES} bytes"));
46 }
47
48 let event_type = extract_required_string_field(pdu, "type")?;
50
51 extract_required_string_field(pdu, "sender")?;
53
54 let room_id = (event_type != "m.room.create" || rules.require_room_create_room_id)
56 .then(|| extract_required_string_field(pdu, "room_id"))
57 .transpose()?;
58
59 if rules.require_event_id {
61 extract_required_string_field(pdu, "event_id")?;
62 }
63
64 extract_optional_string_field(pdu, "state_key")?;
66
67 extract_required_array_field(pdu, "prev_events", MAX_PREV_EVENTS_LENGTH)?;
69
70 let auth_events = extract_required_array_field(pdu, "auth_events", MAX_AUTH_EVENTS_LENGTH)?;
72
73 if !rules.allow_room_create_in_auth_events {
74 if let Some(room_id) = room_id {
77 let room_create_event_reference_hash = <&RoomId>::try_from(room_id.as_str())
78 .map_err(|e| format!("invalid `room_id` field in PDU: {e}"))?
79 .strip_sigil();
80
81 for event_id in auth_events {
82 let CanonicalJsonValue::String(event_id) = event_id else {
83 return Err(format!(
84 "unexpected format of array item in `auth_events` field in PDU: \
85 expected string, got {event_id:?}"
86 ));
87 };
88
89 let reference_hash = event_id.strip_prefix('$').ok_or(
90 "unexpected format of array item in `auth_events` field in PDU: \
91 string not beginning with the `$` sigil",
92 )?;
93
94 if reference_hash == room_create_event_reference_hash {
95 return Err("invalid `auth_events` field in PDU: \
96 cannot contain the `m.room.create` event ID"
97 .to_owned());
98 }
99 }
100 }
101 }
102
103 match pdu.get("depth") {
105 Some(CanonicalJsonValue::Integer(value)) => {
106 if *value < int!(0) {
107 return Err("invalid `depth` field in PDU: cannot be a negative integer".to_owned());
108 }
109 }
110 Some(value) => {
111 return Err(format!(
112 "unexpected format of `depth` field in PDU: \
113 expected integer, got {value:?}"
114 ));
115 }
116 None => return Err("missing `depth` field in PDU".to_owned()),
117 }
118
119 Ok(())
120}
121
122fn extract_optional_string_field<'a>(
128 object: &'a CanonicalJsonObject,
129 field: &'a str,
130) -> Result<Option<&'a String>, String> {
131 match object.get(field) {
132 Some(CanonicalJsonValue::String(value)) => {
133 if value.len() > ID_MAX_BYTES {
134 Err(format!(
135 "invalid `{field}` field in PDU: \
136 string length is larger than maximum of {ID_MAX_BYTES} bytes"
137 ))
138 } else {
139 Ok(Some(value))
140 }
141 }
142 Some(value) => Err(format!(
143 "unexpected format of `{field}` field in PDU: \
144 expected string, got {value:?}"
145 )),
146 None => Ok(None),
147 }
148}
149
150fn extract_required_string_field<'a>(
155 object: &'a CanonicalJsonObject,
156 field: &'a str,
157) -> Result<&'a String, String> {
158 extract_optional_string_field(object, field)?
159 .ok_or_else(|| format!("missing `{field}` field in PDU"))
160}
161
162fn extract_required_array_field<'a>(
167 object: &'a CanonicalJsonObject,
168 field: &'a str,
169 max_len: usize,
170) -> Result<&'a [CanonicalJsonValue], String> {
171 match object.get(field) {
172 Some(CanonicalJsonValue::Array(value)) => {
173 if value.len() > max_len {
174 Err(format!(
175 "invalid `{field}` field in PDU: \
176 array length is larger than maximum of {max_len}"
177 ))
178 } else {
179 Ok(value)
180 }
181 }
182 Some(value) => Err(format!(
183 "unexpected format of `{field}` field in PDU: \
184 expected array, got {value:?}"
185 )),
186 None => Err(format!("missing `{field}` field in PDU")),
187 }
188}
189
190#[cfg(test)]
191mod tests {
192 use std::iter::repeat_n;
193
194 use js_int::int;
195 use ruma_common::{
196 CanonicalJsonObject, CanonicalJsonValue, room_version_rules::EventFormatRules,
197 };
198 use serde_json::{from_value as from_json_value, json, to_string as to_json_string};
199
200 use super::check_pdu_format;
201
202 fn pdu_v1() -> CanonicalJsonObject {
204 let pdu = json!({
205 "auth_events": [
206 [
207 "$af232176:example.org",
208 { "sha256": "abase64encodedsha256hashshouldbe43byteslong" },
209 ],
210 ],
211 "content": {
212 "key": "value",
213 },
214 "depth": 12,
215 "event_id": "$a4ecee13e2accdadf56c1025:example.com",
216 "hashes": {
217 "sha256": "thishashcoversallfieldsincasethisisredacted"
218 },
219 "origin_server_ts": 1_838_188_000,
220 "prev_events": [
221 [
222 "$af232176:example.org",
223 { "sha256": "abase64encodedsha256hashshouldbe43byteslong" }
224 ],
225 ],
226 "room_id": "!UcYsUzyxTGDxLBEvLy:example.org",
227 "sender": "@alice:example.com",
228 "signatures": {
229 "example.com": {
230 "ed25519:key_version": "these86bytesofbase64signaturecoveressentialfieldsincludinghashessocancheckredactedpdus",
231 },
232 },
233 "type": "m.room.message",
234 "unsigned": {
235 "age": 4612,
236 },
237 });
238 from_json_value(pdu).unwrap()
239 }
240
241 fn pdu_v3() -> CanonicalJsonObject {
243 let pdu = json!({
244 "auth_events": [
245 "$base64encodedeventid",
246 "$adifferenteventid",
247 ],
248 "content": {
249 "key": "value",
250 },
251 "depth": 12,
252 "hashes": {
253 "sha256": "thishashcoversallfieldsincasethisisredacted",
254 },
255 "origin_server_ts": 1_838_188_000,
256 "prev_events": [
257 "$base64encodedeventid",
258 "$adifferenteventid",
259 ],
260 "redacts": "$some/old+event",
261 "room_id": "!UcYsUzyxTGDxLBEvLy:example.org",
262 "sender": "@alice:example.com",
263 "signatures": {
264 "example.com": {
265 "ed25519:key_version": "these86bytesofbase64signaturecoveressentialfieldsincludinghashessocancheckredactedpdus",
266 },
267 },
268 "type": "m.room.message",
269 "unsigned": {
270 "age": 4612,
271 }
272 });
273 from_json_value(pdu).unwrap()
274 }
275
276 fn room_create_v12() -> CanonicalJsonObject {
278 let pdu = json!({
279 "auth_events": [],
280 "content": {
281 "room_version": "12",
282 },
283 "depth": 1,
284 "hashes": {
285 "sha256": "thishashcoversallfieldsincasethisisredacted",
286 },
287 "origin_server_ts": 1_838_188_000,
288 "prev_events": [],
289 "sender": "@alice:example.com",
290 "signatures": {
291 "example.com": {
292 "ed25519:key_version": "these86bytesofbase64signaturecoveressentialfieldsincludinghashessocancheckredactedpdus",
293 },
294 },
295 "type": "m.room.create",
296 "unsigned": {
297 "age": 4612,
298 }
299 });
300 from_json_value(pdu).unwrap()
301 }
302
303 fn pdu_v12() -> CanonicalJsonObject {
305 let pdu = json!({
306 "auth_events": [
307 "$base64encodedeventid",
308 "$adifferenteventid",
309 ],
310 "content": {
311 "key": "value",
312 },
313 "depth": 12,
314 "hashes": {
315 "sha256": "thishashcoversallfieldsincasethisisredacted",
316 },
317 "origin_server_ts": 1_838_188_000,
318 "prev_events": [
319 "$base64encodedeventid",
320 ],
321 "room_id": "!roomcreatereferencehash",
322 "sender": "@alice:example.com",
323 "signatures": {
324 "example.com": {
325 "ed25519:key_version": "these86bytesofbase64signaturecoveressentialfieldsincludinghashessocancheckredactedpdus",
326 },
327 },
328 "type": "m.room.message",
329 "unsigned": {
330 "age": 4612,
331 }
332 });
333 from_json_value(pdu).unwrap()
334 }
335
336 #[test]
337 fn check_pdu_format_valid_v1() {
338 check_pdu_format(&pdu_v1(), &EventFormatRules::V1).unwrap();
339 }
340
341 #[test]
342 fn check_pdu_format_valid_v3() {
343 check_pdu_format(&pdu_v3(), &EventFormatRules::V3).unwrap();
344 }
345
346 #[test]
347 fn check_pdu_format_pdu_too_big() {
348 let mut pdu = pdu_v3();
350 let content = pdu.get_mut("content").unwrap().as_object_mut().unwrap();
351 let long_string = repeat_n('a', 66_000).collect::<String>();
352 content.insert("big_data".to_owned(), long_string.into());
353
354 check_pdu_format(&pdu, &EventFormatRules::V3).unwrap_err();
355 }
356
357 fn pdu_v3_with_size(target: usize) -> CanonicalJsonObject {
359 let mut pdu = pdu_v3();
360 let content = pdu.get_mut("content").unwrap().as_object_mut().unwrap();
361 content.insert("big_data".to_owned(), String::new().into());
362 let base_len = to_json_string(&pdu).unwrap().len();
363
364 let content = pdu.get_mut("content").unwrap().as_object_mut().unwrap();
365 let long_string = repeat_n('a', target - base_len).collect::<String>();
366 content.insert("big_data".to_owned(), long_string.into());
367
368 assert_eq!(to_json_string(&pdu).unwrap().len(), target);
369 pdu
370 }
371
372 #[test]
373 fn check_pdu_format_pdu_max_size() {
374 let pdu = pdu_v3_with_size(65_536);
375 check_pdu_format(&pdu, &EventFormatRules::V3).unwrap();
376 }
377
378 #[test]
379 fn check_pdu_format_pdu_one_byte_too_big() {
380 let pdu = pdu_v3_with_size(65_537);
381 let err = check_pdu_format(&pdu, &EventFormatRules::V3).unwrap_err();
382 assert_eq!(err, "PDU is larger than maximum of 65536 bytes");
383 }
384
385 #[test]
386 fn check_pdu_format_fields_missing() {
387 for field in
388 &["event_id", "sender", "room_id", "type", "prev_events", "auth_events", "depth"]
389 {
390 let mut pdu = pdu_v1();
391 pdu.remove(*field).unwrap();
392
393 check_pdu_format(&pdu, &EventFormatRules::V1).unwrap_err();
394 }
395 }
396
397 #[test]
398 fn check_pdu_format_strings_too_big() {
399 for field in &["event_id", "sender", "room_id", "type", "state_key"] {
400 let mut pdu = pdu_v1();
401 let value = repeat_n('a', 300).collect::<String>();
402 pdu.insert((*field).to_owned(), value.into());
403 check_pdu_format(&pdu, &EventFormatRules::V1).unwrap_err();
404 }
405 }
406
407 #[test]
408 fn check_pdu_format_strings_wrong_format() {
409 for field in &["event_id", "sender", "room_id", "type", "state_key"] {
410 let mut pdu = pdu_v1();
411 pdu.insert((*field).to_owned(), true.into());
412 check_pdu_format(&pdu, &EventFormatRules::V1).unwrap_err();
413 }
414 }
415
416 #[test]
417 fn check_pdu_format_arrays_too_big() {
418 for field in &["prev_events", "auth_events"] {
419 let mut pdu = pdu_v3();
420 let value =
421 repeat_n(CanonicalJsonValue::from("$eventid".to_owned()), 30).collect::<Vec<_>>();
422 pdu.insert((*field).to_owned(), value.into());
423 check_pdu_format(&pdu, &EventFormatRules::V3).unwrap_err();
424 }
425 }
426
427 #[test]
428 fn check_pdu_format_arrays_wrong_format() {
429 for field in &["prev_events", "auth_events"] {
430 let mut pdu = pdu_v3();
431 pdu.insert((*field).to_owned(), true.into());
432 check_pdu_format(&pdu, &EventFormatRules::V3).unwrap_err();
433 }
434 }
435
436 #[test]
437 fn check_pdu_format_negative_depth() {
438 let mut pdu = pdu_v3();
439 pdu.insert("depth".to_owned(), int!(-1).into()).unwrap();
440 check_pdu_format(&pdu, &EventFormatRules::V3).unwrap_err();
441 }
442
443 #[test]
444 fn check_pdu_format_depth_wrong_format() {
445 let mut pdu = pdu_v3();
446 pdu.insert("depth".to_owned(), true.into());
447 check_pdu_format(&pdu, &EventFormatRules::V3).unwrap_err();
448 }
449
450 #[test]
451 fn check_pdu_format_valid_room_create_v12() {
452 let pdu = room_create_v12();
453 check_pdu_format(&pdu, &EventFormatRules::V12).unwrap();
454 }
455
456 #[test]
457 fn check_pdu_format_valid_v12() {
458 let pdu = pdu_v12();
459 check_pdu_format(&pdu, &EventFormatRules::V12).unwrap();
460 }
461
462 #[test]
463 fn check_pdu_format_v12_with_room_create() {
464 let mut pdu = pdu_v12();
465 pdu.get_mut("auth_events")
466 .unwrap()
467 .as_array_mut()
468 .unwrap()
469 .push("$roomcreatereferencehash".to_owned().into());
470
471 check_pdu_format(&pdu, &EventFormatRules::V12).unwrap_err();
472 }
473}