tauri/
pattern.rs

1// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5#[cfg(feature = "isolation")]
6use std::sync::Arc;
7
8use serde::Serialize;
9use serialize_to_javascript::{default_template, Template};
10
11/// The domain of the isolation iframe source.
12#[cfg(feature = "isolation")]
13pub const ISOLATION_IFRAME_SRC_DOMAIN: &str = "localhost";
14
15/// An application pattern.
16#[derive(Debug)]
17pub enum Pattern {
18  /// The brownfield pattern.
19  Brownfield,
20  /// Isolation pattern. Recommended for security purposes.
21  #[cfg(feature = "isolation")]
22  Isolation {
23    /// The HTML served on `isolation://index.html`.
24    assets: Arc<tauri_utils::assets::EmbeddedAssets>,
25
26    /// The schema used for the isolation frames.
27    schema: String,
28
29    /// A random string used to ensure that the message went through the isolation frame.
30    ///
31    /// This should be regenerated at runtime.
32    key: String,
33
34    /// Cryptographically secure keys
35    crypto_keys: Box<tauri_utils::pattern::isolation::Keys>,
36  },
37}
38
39/// The shape of the JavaScript Pattern config
40#[derive(Debug, Serialize)]
41#[serde(rename_all = "lowercase", tag = "pattern")]
42pub(crate) enum PatternObject {
43  /// Brownfield pattern.
44  Brownfield,
45  /// Isolation pattern. Recommended for security purposes.
46  #[cfg(feature = "isolation")]
47  Isolation {
48    /// Which `IsolationSide` this `PatternObject` is getting injected into
49    side: IsolationSide,
50  },
51}
52
53impl From<&Pattern> for PatternObject {
54  fn from(pattern: &Pattern) -> Self {
55    match pattern {
56      Pattern::Brownfield => Self::Brownfield,
57      #[cfg(feature = "isolation")]
58      Pattern::Isolation { .. } => Self::Isolation {
59        side: IsolationSide::default(),
60      },
61    }
62  }
63}
64
65/// Where the JavaScript is injected to
66#[cfg(feature = "isolation")]
67#[derive(Debug, Serialize)]
68#[serde(rename_all = "lowercase")]
69pub(crate) enum IsolationSide {
70  /// Original frame, the Brownfield application
71  Original,
72  /// Secure frame, the isolation security application
73  #[allow(dead_code)]
74  Secure,
75}
76
77#[cfg(feature = "isolation")]
78impl Default for IsolationSide {
79  fn default() -> Self {
80    Self::Original
81  }
82}
83
84#[derive(Template)]
85#[default_template("../scripts/pattern.js")]
86pub(crate) struct PatternJavascript {
87  pub(crate) pattern: PatternObject,
88}
89
90#[cfg(feature = "isolation")]
91pub(crate) fn format_real_schema(schema: &str, https: bool) -> String {
92  if cfg!(windows) || cfg!(target_os = "android") {
93    let scheme = if https { "https" } else { "http" };
94    format!("{scheme}://{schema}.{ISOLATION_IFRAME_SRC_DOMAIN}/")
95  } else {
96    format!("{schema}://{ISOLATION_IFRAME_SRC_DOMAIN}/")
97  }
98}