bitwarden_core/client/
deployment.rs1use bitwarden_crypto::CipherSuite;
2
3use crate::client::{gov_mode::is_gov_mode, internal::InternalClient};
4
5const CLOUD_DOMAINS: [&str; 4] = [
7 "bitwarden.com",
8 "bitwarden.eu",
9 "usdev.bitwarden.pw",
10 "qa.bitwarden.pw",
11];
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub(crate) enum Deployment {
16 Cloud,
18 Gov,
20 SelfHosted,
22}
23
24impl Deployment {
25 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 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 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}