Skip to main content

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(Default, Debug, Serialize)]
68#[serde(rename_all = "lowercase")]
69pub(crate) enum IsolationSide {
70  /// Original frame, the Brownfield application
71  #[default]
72  Original,
73  /// Secure frame, the isolation security application
74  #[allow(dead_code)]
75  Secure,
76}
77
78#[derive(Template)]
79#[default_template("../scripts/pattern.js")]
80pub(crate) struct PatternJavascript {
81  pub(crate) pattern: PatternObject,
82}
83
84#[cfg(feature = "isolation")]
85pub(crate) fn format_real_schema(schema: &str, https: bool) -> String {
86  if cfg!(windows) || cfg!(target_os = "android") {
87    let scheme = if https { "https" } else { "http" };
88    format!("{scheme}://{schema}.{ISOLATION_IFRAME_SRC_DOMAIN}/")
89  } else {
90    format!("{schema}://{ISOLATION_IFRAME_SRC_DOMAIN}/")
91  }
92}