Skip to main content

bitwarden_core/client/
deployment.rs

1use bitwarden_crypto::CipherSuite;
2
3use crate::client::{gov_mode::is_gov_mode, internal::InternalClient};
4
5/// Cloud domains operated by Bitwarden, outside Gov: US, EU, US dev and US QA.
6const CLOUD_DOMAINS: [&str; 4] = [
7    "bitwarden.com",
8    "bitwarden.eu",
9    "usdev.bitwarden.pw",
10    "qa.bitwarden.pw",
11];
12
13/// Where the server the client talks to is hosted.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub(crate) enum Deployment {
16    /// Bitwarden cloud (US, EU, or a US dev/QA environment).
17    Cloud,
18    /// Bitwarden Gov cloud (FedRAMP).
19    Gov,
20    /// Any server not hosted by Bitwarden.
21    SelfHosted,
22}
23
24impl Deployment {
25    /// Returns the [`CipherSuite`] the deployment must use.
26    ///
27    /// Gov and self-hosted deployments use FIPS-approved algorithms (e.g. PBKDF2); cloud uses the
28    /// modern defaults (e.g. Argon2id).
29    pub(crate) fn cipher_suite(self) -> CipherSuite {
30        match self {
31            Deployment::Cloud => CipherSuite::Standard,
32            Deployment::Gov | Deployment::SelfHosted => CipherSuite::Fips,
33        }
34    }
35}
36
37impl InternalClient {
38    /// Returns the [`Deployment`] inferred from the configured API URL host.
39    ///
40    /// Hosts on, or under, a [`CLOUD_DOMAINS`] entry are cloud; Gov hosts per [`is_gov_mode`]; any
41    /// other host, or an unparsable URL, is self-hosted.
42    pub(crate) fn deployment_type(&self) -> Deployment {
43        if is_gov_mode(self) {
44            return Deployment::Gov;
45        }
46
47        let host = url::Url::parse(&self.api_configurations.api_config.base_path)
48            .ok()
49            .and_then(|u| u.host_str().map(str::to_lowercase));
50        let Some(host) = host else {
51            return Deployment::SelfHosted;
52        };
53
54        // Match the domain itself or a subdomain, e.g. `api.bitwarden.com`, but not
55        // `bitwarden.com.evil.example` or `mybitwarden.com`.
56        let is_cloud = CLOUD_DOMAINS
57            .iter()
58            .any(|d| host == *d || host.ends_with(&format!(".{d}")));
59        if is_cloud {
60            return Deployment::Cloud;
61        }
62
63        Deployment::SelfHosted
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70    use crate::{Client, ClientSettings};
71
72    fn deployment_for(url: &str) -> Deployment {
73        let client = Client::new(Some(ClientSettings {
74            api_url: url.into(),
75            ..Default::default()
76        }));
77        client.internal.deployment_type()
78    }
79
80    #[test]
81    fn cloud_for_us_and_eu_hosts() {
82        assert_eq!(
83            deployment_for("https://api.bitwarden.com"),
84            Deployment::Cloud
85        );
86        assert_eq!(
87            deployment_for("https://api.bitwarden.eu"),
88            Deployment::Cloud
89        );
90        assert_eq!(
91            deployment_for("https://API.BITWARDEN.COM"),
92            Deployment::Cloud
93        );
94    }
95
96    #[test]
97    fn cloud_for_us_dev_and_qa_hosts() {
98        assert_eq!(
99            deployment_for("https://api.usdev.bitwarden.pw"),
100            Deployment::Cloud
101        );
102        assert_eq!(
103            deployment_for("https://api.qa.bitwarden.pw"),
104            Deployment::Cloud
105        );
106    }
107
108    #[test]
109    fn self_hosted_for_other_bitwarden_pw_hosts() {
110        assert_eq!(
111            deployment_for("https://api.euqa.bitwarden.pw"),
112            Deployment::SelfHosted
113        );
114    }
115
116    #[test]
117    fn gov_for_gov_host() {
118        assert_eq!(
119            deployment_for("https://api.bitwarden-gov.com"),
120            Deployment::Gov
121        );
122    }
123
124    #[test]
125    fn self_hosted_for_other_hosts() {
126        assert_eq!(
127            deployment_for("https://bitwarden.example.com/api"),
128            Deployment::SelfHosted
129        );
130        assert_eq!(
131            deployment_for("http://localhost:4000"),
132            Deployment::SelfHosted
133        );
134    }
135
136    #[test]
137    fn self_hosted_for_spoofed_cloud_hosts() {
138        assert_eq!(
139            deployment_for("https://api.bitwarden.com.evil.example"),
140            Deployment::SelfHosted
141        );
142        assert_eq!(
143            deployment_for("https://mybitwarden.com"),
144            Deployment::SelfHosted
145        );
146        assert_eq!(
147            deployment_for("https://example.com/bitwarden.com/api"),
148            Deployment::SelfHosted
149        );
150    }
151
152    #[test]
153    fn self_hosted_for_invalid_url() {
154        assert_eq!(deployment_for("not a url"), Deployment::SelfHosted);
155    }
156
157    #[test]
158    fn cipher_suite_is_fips_outside_cloud() {
159        assert_eq!(Deployment::Cloud.cipher_suite(), CipherSuite::Standard);
160        assert_eq!(Deployment::Gov.cipher_suite(), CipherSuite::Fips);
161        assert_eq!(Deployment::SelfHosted.cipher_suite(), CipherSuite::Fips);
162    }
163}