1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use wildmatch::WildMatch;
use crate::{events::EmptyStateKey, ServerName};
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.server_acl", kind = State, state_key_type = EmptyStateKey)]
pub struct RoomServerAclEventContent {
#[serde(default = "crate::serde::default_true", skip_serializing_if = "crate::serde::is_true")]
pub allow_ip_literals: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub allow: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub deny: Vec<String>,
}
impl RoomServerAclEventContent {
pub fn new(allow_ip_literals: bool, allow: Vec<String>, deny: Vec<String>) -> Self {
Self { allow_ip_literals, allow, deny }
}
pub fn is_allowed(&self, server_name: &ServerName) -> bool {
if !self.allow_ip_literals && server_name.is_ip_literal() {
return false;
}
let host = server_name.host();
self.deny.iter().all(|d| !WildMatch::new(d).matches(host))
&& self.allow.iter().any(|a| WildMatch::new(a).matches(host))
}
}
#[cfg(test)]
mod tests {
use serde_json::{from_value as from_json_value, json};
use super::RoomServerAclEventContent;
use crate::{events::OriginalStateEvent, server_name};
#[test]
fn default_values() {
let json_data = json!({
"content": {},
"event_id": "$h29iv0s8:example.com",
"origin_server_ts": 1,
"room_id": "!n8f893n9:example.com",
"sender": "@carl:example.com",
"state_key": "",
"type": "m.room.server_acl"
});
let server_acl_event: OriginalStateEvent<RoomServerAclEventContent> =
from_json_value(json_data).unwrap();
assert!(server_acl_event.content.allow_ip_literals);
assert!(server_acl_event.content.allow.is_empty());
assert!(server_acl_event.content.deny.is_empty());
}
#[test]
fn acl_ignores_port() {
let acl_event = RoomServerAclEventContent {
allow_ip_literals: true,
allow: vec!["*".to_owned()],
deny: vec!["1.1.1.1".to_owned()],
};
assert!(!acl_event.is_allowed(server_name!("1.1.1.1:8000")));
}
#[test]
fn acl_allow_ip_literal() {
let acl_event = RoomServerAclEventContent {
allow_ip_literals: true,
allow: vec!["*".to_owned()],
deny: Vec::new(),
};
assert!(acl_event.is_allowed(server_name!("1.1.1.1")));
}
#[test]
fn acl_deny_ip_literal() {
let acl_event = RoomServerAclEventContent {
allow_ip_literals: false,
allow: vec!["*".to_owned()],
deny: Vec::new(),
};
assert!(!acl_event.is_allowed(server_name!("1.1.1.1")));
}
#[test]
fn acl_deny() {
let acl_event = RoomServerAclEventContent {
allow_ip_literals: false,
allow: vec!["*".to_owned()],
deny: vec!["matrix.org".to_owned()],
};
assert!(!acl_event.is_allowed(server_name!("matrix.org")));
assert!(acl_event.is_allowed(server_name!("conduit.rs")));
}
#[test]
fn acl_explicit_allow() {
let acl_event = RoomServerAclEventContent {
allow_ip_literals: false,
allow: vec!["conduit.rs".to_owned()],
deny: Vec::new(),
};
assert!(!acl_event.is_allowed(server_name!("matrix.org")));
assert!(acl_event.is_allowed(server_name!("conduit.rs")));
}
#[test]
fn acl_explicit_glob_1() {
let acl_event = RoomServerAclEventContent {
allow_ip_literals: false,
allow: vec!["*.matrix.org".to_owned()],
deny: Vec::new(),
};
assert!(!acl_event.is_allowed(server_name!("matrix.org")));
assert!(acl_event.is_allowed(server_name!("server.matrix.org")));
}
#[test]
fn acl_explicit_glob_2() {
let acl_event = RoomServerAclEventContent {
allow_ip_literals: false,
allow: vec!["matrix??.org".to_owned()],
deny: Vec::new(),
};
assert!(!acl_event.is_allowed(server_name!("matrix1.org")));
assert!(acl_event.is_allowed(server_name!("matrix02.org")));
}
#[test]
fn acl_ipv6_glob() {
let acl_event = RoomServerAclEventContent {
allow_ip_literals: true,
allow: vec!["[2001:db8:1234::1]".to_owned()],
deny: Vec::new(),
};
assert!(!acl_event.is_allowed(server_name!("[2001:db8:1234::2]")));
assert!(acl_event.is_allowed(server_name!("[2001:db8:1234::1]")));
}
}