mas_policy/
lib.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7pub mod model;
8
9use std::sync::Arc;
10
11use arc_swap::ArcSwap;
12use mas_data_model::Ulid;
13use opa_wasm::{
14    Runtime,
15    wasmtime::{Config, Engine, Module, OptLevel, Store},
16};
17use thiserror::Error;
18use tokio::io::{AsyncRead, AsyncReadExt};
19
20pub use self::model::{
21    AuthorizationGrantInput, ClientRegistrationInput, Code as ViolationCode, EmailInput,
22    EvaluationResult, GrantType, RegisterInput, RegistrationMethod, Requester, Violation,
23};
24
25#[derive(Debug, Error)]
26pub enum LoadError {
27    #[error("failed to read module")]
28    Read(#[from] tokio::io::Error),
29
30    #[error("failed to create WASM engine")]
31    Engine(#[source] anyhow::Error),
32
33    #[error("module compilation task crashed")]
34    CompilationTask(#[from] tokio::task::JoinError),
35
36    #[error("failed to compile WASM module")]
37    Compilation(#[source] anyhow::Error),
38
39    #[error("invalid policy data")]
40    InvalidData(#[source] anyhow::Error),
41
42    #[error("failed to instantiate a test instance")]
43    Instantiate(#[source] InstantiateError),
44}
45
46impl LoadError {
47    /// Creates an example of an invalid data error, used for API response
48    /// documentation
49    #[doc(hidden)]
50    #[must_use]
51    pub fn invalid_data_example() -> Self {
52        Self::InvalidData(anyhow::Error::msg("Failed to merge policy data objects"))
53    }
54}
55
56#[derive(Debug, Error)]
57pub enum InstantiateError {
58    #[error("failed to create WASM runtime")]
59    Runtime(#[source] anyhow::Error),
60
61    #[error("missing entrypoint {entrypoint}")]
62    MissingEntrypoint { entrypoint: String },
63
64    #[error("failed to load policy data")]
65    LoadData(#[source] anyhow::Error),
66}
67
68/// Holds the entrypoint of each policy
69#[derive(Debug, Clone)]
70pub struct Entrypoints {
71    pub register: String,
72    pub client_registration: String,
73    pub authorization_grant: String,
74    pub email: String,
75}
76
77impl Entrypoints {
78    fn all(&self) -> [&str; 4] {
79        [
80            self.register.as_str(),
81            self.client_registration.as_str(),
82            self.authorization_grant.as_str(),
83            self.email.as_str(),
84        ]
85    }
86}
87
88#[derive(Debug)]
89pub struct Data {
90    server_name: String,
91
92    rest: Option<serde_json::Value>,
93}
94
95impl Data {
96    #[must_use]
97    pub fn new(server_name: String) -> Self {
98        Self {
99            server_name,
100            rest: None,
101        }
102    }
103
104    #[must_use]
105    pub fn with_rest(mut self, rest: serde_json::Value) -> Self {
106        self.rest = Some(rest);
107        self
108    }
109
110    fn to_value(&self) -> Result<serde_json::Value, anyhow::Error> {
111        let base = serde_json::json!({
112            "server_name": self.server_name,
113        });
114
115        if let Some(rest) = &self.rest {
116            merge_data(base, rest.clone())
117        } else {
118            Ok(base)
119        }
120    }
121}
122
123fn value_kind(value: &serde_json::Value) -> &'static str {
124    match value {
125        serde_json::Value::Object(_) => "object",
126        serde_json::Value::Array(_) => "array",
127        serde_json::Value::String(_) => "string",
128        serde_json::Value::Number(_) => "number",
129        serde_json::Value::Bool(_) => "boolean",
130        serde_json::Value::Null => "null",
131    }
132}
133
134fn merge_data(
135    mut left: serde_json::Value,
136    right: serde_json::Value,
137) -> Result<serde_json::Value, anyhow::Error> {
138    merge_data_rec(&mut left, right)?;
139    Ok(left)
140}
141
142fn merge_data_rec(
143    left: &mut serde_json::Value,
144    right: serde_json::Value,
145) -> Result<(), anyhow::Error> {
146    match (left, right) {
147        (serde_json::Value::Object(left), serde_json::Value::Object(right)) => {
148            for (key, value) in right {
149                if let Some(left_value) = left.get_mut(&key) {
150                    merge_data_rec(left_value, value)?;
151                } else {
152                    left.insert(key, value);
153                }
154            }
155        }
156        (serde_json::Value::Array(left), serde_json::Value::Array(right)) => {
157            left.extend(right);
158        }
159        // Other values override
160        (serde_json::Value::Number(left), serde_json::Value::Number(right)) => {
161            *left = right;
162        }
163        (serde_json::Value::Bool(left), serde_json::Value::Bool(right)) => {
164            *left = right;
165        }
166        (serde_json::Value::String(left), serde_json::Value::String(right)) => {
167            *left = right;
168        }
169
170        // Null gets overridden by anything
171        (left, right) if left.is_null() => *left = right,
172
173        // Null on the right makes the left value null
174        (left, right) if right.is_null() => *left = right,
175
176        (left, right) => anyhow::bail!(
177            "Cannot merge a {} into a {}",
178            value_kind(&right),
179            value_kind(left),
180        ),
181    }
182
183    Ok(())
184}
185
186struct DynamicData {
187    version: Option<Ulid>,
188    merged: serde_json::Value,
189}
190
191pub struct PolicyFactory {
192    engine: Engine,
193    module: Module,
194    data: Data,
195    dynamic_data: ArcSwap<DynamicData>,
196    entrypoints: Entrypoints,
197}
198
199impl PolicyFactory {
200    #[tracing::instrument(name = "policy.load", skip(source), err)]
201    pub async fn load(
202        mut source: impl AsyncRead + std::marker::Unpin,
203        data: Data,
204        entrypoints: Entrypoints,
205    ) -> Result<Self, LoadError> {
206        let mut config = Config::default();
207        config.async_support(true);
208        config.cranelift_opt_level(OptLevel::SpeedAndSize);
209
210        let engine = Engine::new(&config).map_err(LoadError::Engine)?;
211
212        // Read and compile the module
213        let mut buf = Vec::new();
214        source.read_to_end(&mut buf).await?;
215        // Compilation is CPU-bound, so spawn that in a blocking task
216        let (engine, module) = tokio::task::spawn_blocking(move || {
217            let module = Module::new(&engine, buf)?;
218            anyhow::Ok((engine, module))
219        })
220        .await?
221        .map_err(LoadError::Compilation)?;
222
223        let merged = data.to_value().map_err(LoadError::InvalidData)?;
224        let dynamic_data = ArcSwap::new(Arc::new(DynamicData {
225            version: None,
226            merged,
227        }));
228
229        let factory = Self {
230            engine,
231            module,
232            data,
233            dynamic_data,
234            entrypoints,
235        };
236
237        // Try to instantiate
238        factory
239            .instantiate()
240            .await
241            .map_err(LoadError::Instantiate)?;
242
243        Ok(factory)
244    }
245
246    /// Set the dynamic data for the policy.
247    ///
248    /// The `dynamic_data` object is merged with the static data given when the
249    /// policy was loaded.
250    ///
251    /// Returns `true` if the data was updated, `false` if the version
252    /// of the dynamic data was the same as the one we already have.
253    ///
254    /// # Errors
255    ///
256    /// Returns an error if the data can't be merged with the static data, or if
257    /// the policy can't be instantiated with the new data.
258    pub async fn set_dynamic_data(
259        &self,
260        dynamic_data: mas_data_model::PolicyData,
261    ) -> Result<bool, LoadError> {
262        // Check if the version of the dynamic data we have is the same as the one we're
263        // trying to set
264        if self.dynamic_data.load().version == Some(dynamic_data.id) {
265            // Don't do anything if the version is the same
266            return Ok(false);
267        }
268
269        let static_data = self.data.to_value().map_err(LoadError::InvalidData)?;
270        let merged = merge_data(static_data, dynamic_data.data).map_err(LoadError::InvalidData)?;
271
272        // Try to instantiate with the new data
273        self.instantiate_with_data(&merged)
274            .await
275            .map_err(LoadError::Instantiate)?;
276
277        // If instantiation succeeds, swap the data
278        self.dynamic_data.store(Arc::new(DynamicData {
279            version: Some(dynamic_data.id),
280            merged,
281        }));
282
283        Ok(true)
284    }
285
286    #[tracing::instrument(name = "policy.instantiate", skip_all, err)]
287    pub async fn instantiate(&self) -> Result<Policy, InstantiateError> {
288        let data = self.dynamic_data.load();
289        self.instantiate_with_data(&data.merged).await
290    }
291
292    async fn instantiate_with_data(
293        &self,
294        data: &serde_json::Value,
295    ) -> Result<Policy, InstantiateError> {
296        let mut store = Store::new(&self.engine, ());
297        let runtime = Runtime::new(&mut store, &self.module)
298            .await
299            .map_err(InstantiateError::Runtime)?;
300
301        // Check that we have the required entrypoints
302        let policy_entrypoints = runtime.entrypoints();
303
304        for e in self.entrypoints.all() {
305            if !policy_entrypoints.contains(e) {
306                return Err(InstantiateError::MissingEntrypoint {
307                    entrypoint: e.to_owned(),
308                });
309            }
310        }
311
312        let instance = runtime
313            .with_data(&mut store, data)
314            .await
315            .map_err(InstantiateError::LoadData)?;
316
317        Ok(Policy {
318            store,
319            instance,
320            entrypoints: self.entrypoints.clone(),
321        })
322    }
323}
324
325pub struct Policy {
326    store: Store<()>,
327    instance: opa_wasm::Policy<opa_wasm::DefaultContext>,
328    entrypoints: Entrypoints,
329}
330
331#[derive(Debug, Error)]
332#[error("failed to evaluate policy")]
333pub enum EvaluationError {
334    Serialization(#[from] serde_json::Error),
335    Evaluation(#[from] anyhow::Error),
336}
337
338impl Policy {
339    #[tracing::instrument(
340        name = "policy.evaluate_email",
341        skip_all,
342        fields(
343            %input.email,
344        ),
345        err,
346    )]
347    pub async fn evaluate_email(
348        &mut self,
349        input: EmailInput<'_>,
350    ) -> Result<EvaluationResult, EvaluationError> {
351        let [res]: [EvaluationResult; 1] = self
352            .instance
353            .evaluate(&mut self.store, &self.entrypoints.email, &input)
354            .await?;
355
356        Ok(res)
357    }
358
359    #[tracing::instrument(
360        name = "policy.evaluate.register",
361        skip_all,
362        fields(
363            ?input.registration_method,
364            input.username = input.username,
365            input.email = input.email,
366        ),
367        err,
368    )]
369    pub async fn evaluate_register(
370        &mut self,
371        input: RegisterInput<'_>,
372    ) -> Result<EvaluationResult, EvaluationError> {
373        let [res]: [EvaluationResult; 1] = self
374            .instance
375            .evaluate(&mut self.store, &self.entrypoints.register, &input)
376            .await?;
377
378        Ok(res)
379    }
380
381    #[tracing::instrument(skip(self))]
382    pub async fn evaluate_client_registration(
383        &mut self,
384        input: ClientRegistrationInput<'_>,
385    ) -> Result<EvaluationResult, EvaluationError> {
386        let [res]: [EvaluationResult; 1] = self
387            .instance
388            .evaluate(
389                &mut self.store,
390                &self.entrypoints.client_registration,
391                &input,
392            )
393            .await?;
394
395        Ok(res)
396    }
397
398    #[tracing::instrument(
399        name = "policy.evaluate.authorization_grant",
400        skip_all,
401        fields(
402            %input.scope,
403            %input.client.id,
404        ),
405        err,
406    )]
407    pub async fn evaluate_authorization_grant(
408        &mut self,
409        input: AuthorizationGrantInput<'_>,
410    ) -> Result<EvaluationResult, EvaluationError> {
411        let [res]: [EvaluationResult; 1] = self
412            .instance
413            .evaluate(
414                &mut self.store,
415                &self.entrypoints.authorization_grant,
416                &input,
417            )
418            .await?;
419
420        Ok(res)
421    }
422}
423
424#[cfg(test)]
425mod tests {
426
427    use std::time::SystemTime;
428
429    use super::*;
430
431    #[tokio::test]
432    async fn test_register() {
433        let data = Data::new("example.com".to_owned()).with_rest(serde_json::json!({
434            "allowed_domains": ["element.io", "*.element.io"],
435            "banned_domains": ["staging.element.io"],
436        }));
437
438        #[allow(clippy::disallowed_types)]
439        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
440            .join("..")
441            .join("..")
442            .join("policies")
443            .join("policy.wasm");
444
445        let file = tokio::fs::File::open(path).await.unwrap();
446
447        let entrypoints = Entrypoints {
448            register: "register/violation".to_owned(),
449            client_registration: "client_registration/violation".to_owned(),
450            authorization_grant: "authorization_grant/violation".to_owned(),
451            email: "email/violation".to_owned(),
452        };
453
454        let factory = PolicyFactory::load(file, data, entrypoints).await.unwrap();
455
456        let mut policy = factory.instantiate().await.unwrap();
457
458        let res = policy
459            .evaluate_register(RegisterInput {
460                registration_method: RegistrationMethod::Password,
461                username: "hello",
462                email: Some("hello@example.com"),
463                requester: Requester {
464                    ip_address: None,
465                    user_agent: None,
466                },
467            })
468            .await
469            .unwrap();
470        assert!(!res.valid());
471
472        let res = policy
473            .evaluate_register(RegisterInput {
474                registration_method: RegistrationMethod::Password,
475                username: "hello",
476                email: Some("hello@foo.element.io"),
477                requester: Requester {
478                    ip_address: None,
479                    user_agent: None,
480                },
481            })
482            .await
483            .unwrap();
484        assert!(res.valid());
485
486        let res = policy
487            .evaluate_register(RegisterInput {
488                registration_method: RegistrationMethod::Password,
489                username: "hello",
490                email: Some("hello@staging.element.io"),
491                requester: Requester {
492                    ip_address: None,
493                    user_agent: None,
494                },
495            })
496            .await
497            .unwrap();
498        assert!(!res.valid());
499    }
500
501    #[tokio::test]
502    async fn test_dynamic_data() {
503        let data = Data::new("example.com".to_owned());
504
505        #[allow(clippy::disallowed_types)]
506        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
507            .join("..")
508            .join("..")
509            .join("policies")
510            .join("policy.wasm");
511
512        let file = tokio::fs::File::open(path).await.unwrap();
513
514        let entrypoints = Entrypoints {
515            register: "register/violation".to_owned(),
516            client_registration: "client_registration/violation".to_owned(),
517            authorization_grant: "authorization_grant/violation".to_owned(),
518            email: "email/violation".to_owned(),
519        };
520
521        let factory = PolicyFactory::load(file, data, entrypoints).await.unwrap();
522
523        let mut policy = factory.instantiate().await.unwrap();
524
525        let res = policy
526            .evaluate_register(RegisterInput {
527                registration_method: RegistrationMethod::Password,
528                username: "hello",
529                email: Some("hello@example.com"),
530                requester: Requester {
531                    ip_address: None,
532                    user_agent: None,
533                },
534            })
535            .await
536            .unwrap();
537        assert!(res.valid());
538
539        // Update the policy data
540        factory
541            .set_dynamic_data(mas_data_model::PolicyData {
542                id: Ulid::nil(),
543                created_at: SystemTime::now().into(),
544                data: serde_json::json!({
545                    "emails": {
546                        "banned_addresses": {
547                            "substrings": ["hello"]
548                        }
549                    }
550                }),
551            })
552            .await
553            .unwrap();
554        let mut policy = factory.instantiate().await.unwrap();
555        let res = policy
556            .evaluate_register(RegisterInput {
557                registration_method: RegistrationMethod::Password,
558                username: "hello",
559                email: Some("hello@example.com"),
560                requester: Requester {
561                    ip_address: None,
562                    user_agent: None,
563                },
564            })
565            .await
566            .unwrap();
567        assert!(!res.valid());
568    }
569
570    #[tokio::test]
571    async fn test_big_dynamic_data() {
572        let data = Data::new("example.com".to_owned());
573
574        #[allow(clippy::disallowed_types)]
575        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
576            .join("..")
577            .join("..")
578            .join("policies")
579            .join("policy.wasm");
580
581        let file = tokio::fs::File::open(path).await.unwrap();
582
583        let entrypoints = Entrypoints {
584            register: "register/violation".to_owned(),
585            client_registration: "client_registration/violation".to_owned(),
586            authorization_grant: "authorization_grant/violation".to_owned(),
587            email: "email/violation".to_owned(),
588        };
589
590        let factory = PolicyFactory::load(file, data, entrypoints).await.unwrap();
591
592        // That is around 1 MB of JSON data. Each element is a 5-digit string, so 8
593        // characters including the quotes and a comma.
594        let data: Vec<String> = (0..(1024 * 1024 / 8))
595            .map(|i| format!("{:05}", i % 100_000))
596            .collect();
597        let json = serde_json::json!({ "emails": { "banned_addresses": { "substrings": data } } });
598        factory
599            .set_dynamic_data(mas_data_model::PolicyData {
600                id: Ulid::nil(),
601                created_at: SystemTime::now().into(),
602                data: json,
603            })
604            .await
605            .unwrap();
606
607        // Try instantiating the policy, make sure 5-digit numbers are banned from email
608        // addresses
609        let mut policy = factory.instantiate().await.unwrap();
610        let res = policy
611            .evaluate_register(RegisterInput {
612                registration_method: RegistrationMethod::Password,
613                username: "hello",
614                email: Some("12345@example.com"),
615                requester: Requester {
616                    ip_address: None,
617                    user_agent: None,
618                },
619            })
620            .await
621            .unwrap();
622        assert!(!res.valid());
623    }
624
625    #[test]
626    fn test_merge() {
627        use serde_json::json as j;
628
629        // Merging objects
630        let res = merge_data(j!({"hello": "world"}), j!({"foo": "bar"})).unwrap();
631        assert_eq!(res, j!({"hello": "world", "foo": "bar"}));
632
633        // Override a value of the same type
634        let res = merge_data(j!({"hello": "world"}), j!({"hello": "john"})).unwrap();
635        assert_eq!(res, j!({"hello": "john"}));
636
637        let res = merge_data(j!({"hello": true}), j!({"hello": false})).unwrap();
638        assert_eq!(res, j!({"hello": false}));
639
640        let res = merge_data(j!({"hello": 0}), j!({"hello": 42})).unwrap();
641        assert_eq!(res, j!({"hello": 42}));
642
643        // Override a value of a different type
644        merge_data(j!({"hello": "world"}), j!({"hello": 123}))
645            .expect_err("Can't merge different types");
646
647        // Merge arrays
648        let res = merge_data(j!({"hello": ["world"]}), j!({"hello": ["john"]})).unwrap();
649        assert_eq!(res, j!({"hello": ["world", "john"]}));
650
651        // Null overrides a value
652        let res = merge_data(j!({"hello": "world"}), j!({"hello": null})).unwrap();
653        assert_eq!(res, j!({"hello": null}));
654
655        // Null gets overridden by a value
656        let res = merge_data(j!({"hello": null}), j!({"hello": "world"})).unwrap();
657        assert_eq!(res, j!({"hello": "world"}));
658
659        // Objects get deeply merged
660        let res = merge_data(j!({"a": {"b": {"c": "d"}}}), j!({"a": {"b": {"e": "f"}}})).unwrap();
661        assert_eq!(res, j!({"a": {"b": {"c": "d", "e": "f"}}}));
662    }
663}