cipher.rs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. use chrono::{Duration, NaiveDateTime, Utc};
  2. use serde_json::Value;
  3. use crate::CONFIG;
  4. use super::{
  5. Attachment,
  6. CollectionCipher,
  7. Favorite,
  8. FolderCipher,
  9. Organization,
  10. User,
  11. UserOrgStatus,
  12. UserOrgType,
  13. UserOrganization,
  14. };
  15. db_object! {
  16. #[derive(Identifiable, Queryable, Insertable, Associations, AsChangeset)]
  17. #[table_name = "ciphers"]
  18. #[changeset_options(treat_none_as_null="true")]
  19. #[belongs_to(User, foreign_key = "user_uuid")]
  20. #[belongs_to(Organization, foreign_key = "organization_uuid")]
  21. #[primary_key(uuid)]
  22. pub struct Cipher {
  23. pub uuid: String,
  24. pub created_at: NaiveDateTime,
  25. pub updated_at: NaiveDateTime,
  26. pub user_uuid: Option<String>,
  27. pub organization_uuid: Option<String>,
  28. /*
  29. Login = 1,
  30. SecureNote = 2,
  31. Card = 3,
  32. Identity = 4
  33. */
  34. pub atype: i32,
  35. pub name: String,
  36. pub notes: Option<String>,
  37. pub fields: Option<String>,
  38. pub data: String,
  39. pub password_history: Option<String>,
  40. pub deleted_at: Option<NaiveDateTime>,
  41. }
  42. }
  43. /// Local methods
  44. impl Cipher {
  45. pub fn new(atype: i32, name: String) -> Self {
  46. let now = Utc::now().naive_utc();
  47. Self {
  48. uuid: crate::util::get_uuid(),
  49. created_at: now,
  50. updated_at: now,
  51. user_uuid: None,
  52. organization_uuid: None,
  53. atype,
  54. name,
  55. notes: None,
  56. fields: None,
  57. data: String::new(),
  58. password_history: None,
  59. deleted_at: None,
  60. }
  61. }
  62. }
  63. use crate::db::DbConn;
  64. use crate::api::EmptyResult;
  65. use crate::error::MapResult;
  66. /// Database methods
  67. impl Cipher {
  68. pub fn to_json(&self, host: &str, user_uuid: &str, conn: &DbConn) -> Value {
  69. use crate::util::format_date;
  70. let attachments = Attachment::find_by_cipher(&self.uuid, conn);
  71. // When there are no attachments use null instead of an empty array
  72. let attachments_json = if attachments.is_empty() {
  73. Value::Null
  74. } else {
  75. attachments.iter().map(|c| c.to_json(host)).collect()
  76. };
  77. let fields_json = self.fields.as_ref().and_then(|s| serde_json::from_str(s).ok()).unwrap_or(Value::Null);
  78. let password_history_json = self.password_history.as_ref().and_then(|s| serde_json::from_str(s).ok()).unwrap_or(Value::Null);
  79. let (read_only, hide_passwords) =
  80. match self.get_access_restrictions(&user_uuid, conn) {
  81. Some((ro, hp)) => (ro, hp),
  82. None => {
  83. error!("Cipher ownership assertion failure");
  84. (true, true)
  85. },
  86. };
  87. // Get the type_data or a default to an empty json object '{}'.
  88. // If not passing an empty object, mobile clients will crash.
  89. let mut type_data_json: Value = serde_json::from_str(&self.data).unwrap_or_else(|_| json!({}));
  90. // NOTE: This was marked as *Backwards Compatibilty Code*, but as of January 2021 this is still being used by upstream
  91. // Set the first element of the Uris array as Uri, this is needed several (mobile) clients.
  92. if self.atype == 1 {
  93. if type_data_json["Uris"].is_array() {
  94. let uri = type_data_json["Uris"][0]["Uri"].clone();
  95. type_data_json["Uri"] = uri;
  96. } else {
  97. // Upstream always has an Uri key/value
  98. type_data_json["Uri"] = Value::Null;
  99. }
  100. }
  101. // Clone the type_data and add some default value.
  102. let mut data_json = type_data_json.clone();
  103. // NOTE: This was marked as *Backwards Compatibilty Code*, but as of January 2021 this is still being used by upstream
  104. // data_json should always contain the following keys with every atype
  105. data_json["Fields"] = json!(fields_json);
  106. data_json["Name"] = json!(self.name);
  107. data_json["Notes"] = json!(self.notes);
  108. data_json["PasswordHistory"] = json!(password_history_json);
  109. // There are three types of cipher response models in upstream
  110. // Bitwarden: "cipherMini", "cipher", and "cipherDetails" (in order
  111. // of increasing level of detail). bitwarden_rs currently only
  112. // supports the "cipherDetails" type, though it seems like the
  113. // Bitwarden clients will ignore extra fields.
  114. //
  115. // Ref: https://github.com/bitwarden/server/blob/master/src/Core/Models/Api/Response/CipherResponseModel.cs
  116. let mut json_object = json!({
  117. "Object": "cipherDetails",
  118. "Id": self.uuid,
  119. "Type": self.atype,
  120. "RevisionDate": format_date(&self.updated_at),
  121. "DeletedDate": self.deleted_at.map_or(Value::Null, |d| Value::String(format_date(&d))),
  122. "FolderId": self.get_folder_uuid(&user_uuid, conn),
  123. "Favorite": self.is_favorite(&user_uuid, conn),
  124. "OrganizationId": self.organization_uuid,
  125. "Attachments": attachments_json,
  126. // We have UseTotp set to true by default within the Organization model.
  127. // This variable together with UsersGetPremium is used to show or hide the TOTP counter.
  128. "OrganizationUseTotp": true,
  129. // This field is specific to the cipherDetails type.
  130. "CollectionIds": self.get_collections(user_uuid, conn),
  131. "Name": self.name,
  132. "Notes": self.notes,
  133. "Fields": fields_json,
  134. "Data": data_json,
  135. // These values are true by default, but can be false if the
  136. // cipher belongs to a collection where the org owner has enabled
  137. // the "Read Only" or "Hide Passwords" restrictions for the user.
  138. "Edit": !read_only,
  139. "ViewPassword": !hide_passwords,
  140. "PasswordHistory": password_history_json,
  141. // All Cipher types are included by default as null, but only the matching one will be populated
  142. "Login": null,
  143. "SecureNote": null,
  144. "Card": null,
  145. "Identity": null,
  146. });
  147. let key = match self.atype {
  148. 1 => "Login",
  149. 2 => "SecureNote",
  150. 3 => "Card",
  151. 4 => "Identity",
  152. _ => panic!("Wrong type"),
  153. };
  154. json_object[key] = type_data_json;
  155. json_object
  156. }
  157. pub fn update_users_revision(&self, conn: &DbConn) -> Vec<String> {
  158. let mut user_uuids = Vec::new();
  159. match self.user_uuid {
  160. Some(ref user_uuid) => {
  161. User::update_uuid_revision(&user_uuid, conn);
  162. user_uuids.push(user_uuid.clone())
  163. }
  164. None => {
  165. // Belongs to Organization, need to update affected users
  166. if let Some(ref org_uuid) = self.organization_uuid {
  167. UserOrganization::find_by_cipher_and_org(&self.uuid, &org_uuid, conn)
  168. .iter()
  169. .for_each(|user_org| {
  170. User::update_uuid_revision(&user_org.user_uuid, conn);
  171. user_uuids.push(user_org.user_uuid.clone())
  172. });
  173. }
  174. }
  175. };
  176. user_uuids
  177. }
  178. pub fn save(&mut self, conn: &DbConn) -> EmptyResult {
  179. self.update_users_revision(conn);
  180. self.updated_at = Utc::now().naive_utc();
  181. db_run! { conn:
  182. sqlite, mysql {
  183. match diesel::replace_into(ciphers::table)
  184. .values(CipherDb::to_db(self))
  185. .execute(conn)
  186. {
  187. Ok(_) => Ok(()),
  188. // Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
  189. Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
  190. diesel::update(ciphers::table)
  191. .filter(ciphers::uuid.eq(&self.uuid))
  192. .set(CipherDb::to_db(self))
  193. .execute(conn)
  194. .map_res("Error saving cipher")
  195. }
  196. Err(e) => Err(e.into()),
  197. }.map_res("Error saving cipher")
  198. }
  199. postgresql {
  200. let value = CipherDb::to_db(self);
  201. diesel::insert_into(ciphers::table)
  202. .values(&value)
  203. .on_conflict(ciphers::uuid)
  204. .do_update()
  205. .set(&value)
  206. .execute(conn)
  207. .map_res("Error saving cipher")
  208. }
  209. }
  210. }
  211. pub fn delete(&self, conn: &DbConn) -> EmptyResult {
  212. self.update_users_revision(conn);
  213. FolderCipher::delete_all_by_cipher(&self.uuid, conn)?;
  214. CollectionCipher::delete_all_by_cipher(&self.uuid, conn)?;
  215. Attachment::delete_all_by_cipher(&self.uuid, conn)?;
  216. Favorite::delete_all_by_cipher(&self.uuid, conn)?;
  217. db_run! { conn: {
  218. diesel::delete(ciphers::table.filter(ciphers::uuid.eq(&self.uuid)))
  219. .execute(conn)
  220. .map_res("Error deleting cipher")
  221. }}
  222. }
  223. pub fn delete_all_by_organization(org_uuid: &str, conn: &DbConn) -> EmptyResult {
  224. for cipher in Self::find_by_org(org_uuid, &conn) {
  225. cipher.delete(&conn)?;
  226. }
  227. Ok(())
  228. }
  229. pub fn delete_all_by_user(user_uuid: &str, conn: &DbConn) -> EmptyResult {
  230. for cipher in Self::find_owned_by_user(user_uuid, &conn) {
  231. cipher.delete(&conn)?;
  232. }
  233. Ok(())
  234. }
  235. /// Purge all ciphers that are old enough to be auto-deleted.
  236. pub fn purge_trash(conn: &DbConn) {
  237. if let Some(auto_delete_days) = CONFIG.trash_auto_delete_days() {
  238. let now = Utc::now().naive_utc();
  239. let dt = now - Duration::days(auto_delete_days);
  240. for cipher in Self::find_deleted_before(&dt, conn) {
  241. cipher.delete(&conn).ok();
  242. }
  243. }
  244. }
  245. pub fn move_to_folder(&self, folder_uuid: Option<String>, user_uuid: &str, conn: &DbConn) -> EmptyResult {
  246. User::update_uuid_revision(user_uuid, conn);
  247. match (self.get_folder_uuid(&user_uuid, conn), folder_uuid) {
  248. // No changes
  249. (None, None) => Ok(()),
  250. (Some(ref old), Some(ref new)) if old == new => Ok(()),
  251. // Add to folder
  252. (None, Some(new)) => FolderCipher::new(&new, &self.uuid).save(conn),
  253. // Remove from folder
  254. (Some(old), None) => match FolderCipher::find_by_folder_and_cipher(&old, &self.uuid, conn) {
  255. Some(old) => old.delete(conn),
  256. None => err!("Couldn't move from previous folder"),
  257. },
  258. // Move to another folder
  259. (Some(old), Some(new)) => {
  260. if let Some(old) = FolderCipher::find_by_folder_and_cipher(&old, &self.uuid, conn) {
  261. old.delete(conn)?;
  262. }
  263. FolderCipher::new(&new, &self.uuid).save(conn)
  264. }
  265. }
  266. }
  267. /// Returns whether this cipher is directly owned by the user.
  268. pub fn is_owned_by_user(&self, user_uuid: &str) -> bool {
  269. self.user_uuid.is_some() && self.user_uuid.as_ref().unwrap() == user_uuid
  270. }
  271. /// Returns whether this cipher is owned by an org in which the user has full access.
  272. pub fn is_in_full_access_org(&self, user_uuid: &str, conn: &DbConn) -> bool {
  273. if let Some(ref org_uuid) = self.organization_uuid {
  274. if let Some(user_org) = UserOrganization::find_by_user_and_org(&user_uuid, &org_uuid, conn) {
  275. return user_org.has_full_access();
  276. }
  277. }
  278. false
  279. }
  280. /// Returns the user's access restrictions to this cipher. A return value
  281. /// of None means that this cipher does not belong to the user, and is
  282. /// not in any collection the user has access to. Otherwise, the user has
  283. /// access to this cipher, and Some(read_only, hide_passwords) represents
  284. /// the access restrictions.
  285. pub fn get_access_restrictions(&self, user_uuid: &str, conn: &DbConn) -> Option<(bool, bool)> {
  286. // Check whether this cipher is directly owned by the user, or is in
  287. // a collection that the user has full access to. If so, there are no
  288. // access restrictions.
  289. if self.is_owned_by_user(&user_uuid) || self.is_in_full_access_org(&user_uuid, &conn) {
  290. return Some((false, false));
  291. }
  292. db_run! {conn: {
  293. // Check whether this cipher is in any collections accessible to the
  294. // user. If so, retrieve the access flags for each collection.
  295. let query = ciphers::table
  296. .filter(ciphers::uuid.eq(&self.uuid))
  297. .inner_join(ciphers_collections::table.on(
  298. ciphers::uuid.eq(ciphers_collections::cipher_uuid)))
  299. .inner_join(users_collections::table.on(
  300. ciphers_collections::collection_uuid.eq(users_collections::collection_uuid)
  301. .and(users_collections::user_uuid.eq(user_uuid))))
  302. .select((users_collections::read_only, users_collections::hide_passwords));
  303. // There's an edge case where a cipher can be in multiple collections
  304. // with inconsistent access flags. For example, a cipher could be in
  305. // one collection where the user has read-only access, but also in
  306. // another collection where the user has read/write access. To handle
  307. // this, we do a boolean OR of all values in each of the `read_only`
  308. // and `hide_passwords` columns. This could ideally be done as part
  309. // of the query, but Diesel doesn't support a max() or bool_or()
  310. // function on booleans and this behavior isn't portable anyway.
  311. if let Ok(vec) = query.load::<(bool, bool)>(conn) {
  312. let mut read_only = false;
  313. let mut hide_passwords = false;
  314. for (ro, hp) in vec.iter() {
  315. read_only |= ro;
  316. hide_passwords |= hp;
  317. }
  318. Some((read_only, hide_passwords))
  319. } else {
  320. // This cipher isn't in any collections accessible to the user.
  321. None
  322. }
  323. }}
  324. }
  325. pub fn is_write_accessible_to_user(&self, user_uuid: &str, conn: &DbConn) -> bool {
  326. match self.get_access_restrictions(&user_uuid, &conn) {
  327. Some((read_only, _hide_passwords)) => !read_only,
  328. None => false,
  329. }
  330. }
  331. pub fn is_accessible_to_user(&self, user_uuid: &str, conn: &DbConn) -> bool {
  332. self.get_access_restrictions(&user_uuid, &conn).is_some()
  333. }
  334. // Returns whether this cipher is a favorite of the specified user.
  335. pub fn is_favorite(&self, user_uuid: &str, conn: &DbConn) -> bool {
  336. Favorite::is_favorite(&self.uuid, user_uuid, conn)
  337. }
  338. // Sets whether this cipher is a favorite of the specified user.
  339. pub fn set_favorite(&self, favorite: Option<bool>, user_uuid: &str, conn: &DbConn) -> EmptyResult {
  340. match favorite {
  341. None => Ok(()), // No change requested.
  342. Some(status) => Favorite::set_favorite(status, &self.uuid, user_uuid, conn),
  343. }
  344. }
  345. pub fn get_folder_uuid(&self, user_uuid: &str, conn: &DbConn) -> Option<String> {
  346. db_run! {conn: {
  347. folders_ciphers::table
  348. .inner_join(folders::table)
  349. .filter(folders::user_uuid.eq(&user_uuid))
  350. .filter(folders_ciphers::cipher_uuid.eq(&self.uuid))
  351. .select(folders_ciphers::folder_uuid)
  352. .first::<String>(conn)
  353. .ok()
  354. }}
  355. }
  356. pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
  357. db_run! {conn: {
  358. ciphers::table
  359. .filter(ciphers::uuid.eq(uuid))
  360. .first::<CipherDb>(conn)
  361. .ok()
  362. .from_db()
  363. }}
  364. }
  365. // Find all ciphers accessible or visible to the specified user.
  366. //
  367. // "Accessible" means the user has read access to the cipher, either via
  368. // direct ownership or via collection access.
  369. //
  370. // "Visible" usually means the same as accessible, except when an org
  371. // owner/admin sets their account to have access to only selected
  372. // collections in the org (presumably because they aren't interested in
  373. // the other collections in the org). In this case, if `visible_only` is
  374. // true, then the non-interesting ciphers will not be returned. As a
  375. // result, those ciphers will not appear in "My Vault" for the org
  376. // owner/admin, but they can still be accessed via the org vault view.
  377. pub fn find_by_user(user_uuid: &str, visible_only: bool, conn: &DbConn) -> Vec<Self> {
  378. db_run! {conn: {
  379. let mut query = ciphers::table
  380. .left_join(ciphers_collections::table.on(
  381. ciphers::uuid.eq(ciphers_collections::cipher_uuid)
  382. ))
  383. .left_join(users_organizations::table.on(
  384. ciphers::organization_uuid.eq(users_organizations::org_uuid.nullable())
  385. .and(users_organizations::user_uuid.eq(user_uuid))
  386. .and(users_organizations::status.eq(UserOrgStatus::Confirmed as i32))
  387. ))
  388. .left_join(users_collections::table.on(
  389. ciphers_collections::collection_uuid.eq(users_collections::collection_uuid)
  390. // Ensure that users_collections::user_uuid is NULL for unconfirmed users.
  391. .and(users_organizations::user_uuid.eq(users_collections::user_uuid))
  392. ))
  393. .filter(ciphers::user_uuid.eq(user_uuid)) // Cipher owner
  394. .or_filter(users_organizations::access_all.eq(true)) // access_all in org
  395. .or_filter(users_collections::user_uuid.eq(user_uuid)) // Access to collection
  396. .into_boxed();
  397. if !visible_only {
  398. query = query.or_filter(
  399. users_organizations::atype.le(UserOrgType::Admin as i32) // Org admin/owner
  400. );
  401. }
  402. query
  403. .select(ciphers::all_columns)
  404. .distinct()
  405. .load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
  406. }}
  407. }
  408. // Find all ciphers visible to the specified user.
  409. pub fn find_by_user_visible(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
  410. Self::find_by_user(user_uuid, true, conn)
  411. }
  412. // Find all ciphers directly owned by the specified user.
  413. pub fn find_owned_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
  414. db_run! {conn: {
  415. ciphers::table
  416. .filter(
  417. ciphers::user_uuid.eq(user_uuid)
  418. .and(ciphers::organization_uuid.is_null())
  419. )
  420. .load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
  421. }}
  422. }
  423. pub fn count_owned_by_user(user_uuid: &str, conn: &DbConn) -> i64 {
  424. db_run! {conn: {
  425. ciphers::table
  426. .filter(ciphers::user_uuid.eq(user_uuid))
  427. .count()
  428. .first::<i64>(conn)
  429. .ok()
  430. .unwrap_or(0)
  431. }}
  432. }
  433. pub fn find_by_org(org_uuid: &str, conn: &DbConn) -> Vec<Self> {
  434. db_run! {conn: {
  435. ciphers::table
  436. .filter(ciphers::organization_uuid.eq(org_uuid))
  437. .load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
  438. }}
  439. }
  440. pub fn count_by_org(org_uuid: &str, conn: &DbConn) -> i64 {
  441. db_run! {conn: {
  442. ciphers::table
  443. .filter(ciphers::organization_uuid.eq(org_uuid))
  444. .count()
  445. .first::<i64>(conn)
  446. .ok()
  447. .unwrap_or(0)
  448. }}
  449. }
  450. pub fn find_by_folder(folder_uuid: &str, conn: &DbConn) -> Vec<Self> {
  451. db_run! {conn: {
  452. folders_ciphers::table.inner_join(ciphers::table)
  453. .filter(folders_ciphers::folder_uuid.eq(folder_uuid))
  454. .select(ciphers::all_columns)
  455. .load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
  456. }}
  457. }
  458. /// Find all ciphers that were deleted before the specified datetime.
  459. pub fn find_deleted_before(dt: &NaiveDateTime, conn: &DbConn) -> Vec<Self> {
  460. db_run! {conn: {
  461. ciphers::table
  462. .filter(ciphers::deleted_at.lt(dt))
  463. .load::<CipherDb>(conn).expect("Error loading ciphers").from_db()
  464. }}
  465. }
  466. pub fn get_collections(&self, user_id: &str, conn: &DbConn) -> Vec<String> {
  467. db_run! {conn: {
  468. ciphers_collections::table
  469. .inner_join(collections::table.on(
  470. collections::uuid.eq(ciphers_collections::collection_uuid)
  471. ))
  472. .inner_join(users_organizations::table.on(
  473. users_organizations::org_uuid.eq(collections::org_uuid).and(
  474. users_organizations::user_uuid.eq(user_id)
  475. )
  476. ))
  477. .left_join(users_collections::table.on(
  478. users_collections::collection_uuid.eq(ciphers_collections::collection_uuid).and(
  479. users_collections::user_uuid.eq(user_id)
  480. )
  481. ))
  482. .filter(ciphers_collections::cipher_uuid.eq(&self.uuid))
  483. .filter(users_collections::user_uuid.eq(user_id).or( // User has access to collection
  484. users_organizations::access_all.eq(true).or( // User has access all
  485. users_organizations::atype.le(UserOrgType::Admin as i32) // User is admin or owner
  486. )
  487. ))
  488. .select(ciphers_collections::collection_uuid)
  489. .load::<String>(conn).unwrap_or_default()
  490. }}
  491. }
  492. }