2018-12-30 23:34:31 +01:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2020-12-06 01:22:20 +01:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2021-11-07 18:53:39 +01:00
|
|
|
use rocket::fs::TempFile;
|
|
|
|
use rocket::serde::json::Json;
|
|
|
|
use rocket::{
|
|
|
|
form::{Form, FromForm},
|
|
|
|
Route,
|
|
|
|
};
|
2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2020-07-14 18:00:09 +02:00
|
|
|
use crate::{
|
|
|
|
api::{self, EmptyResult, JsonResult, JsonUpcase, Notify, PasswordData, UpdateType},
|
|
|
|
auth::Headers,
|
|
|
|
crypto,
|
2021-04-03 05:52:15 +02:00
|
|
|
db::{models::*, DbConn, DbPool},
|
2020-07-14 18:00:09 +02:00
|
|
|
CONFIG,
|
|
|
|
};
|
2018-02-15 00:40:34 +01:00
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn routes() -> Vec<Route> {
|
2020-10-23 12:42:22 +02:00
|
|
|
// Note that many routes have an `admin` variant; this seems to be
|
|
|
|
// because the stored procedure that upstream Bitwarden uses to determine
|
|
|
|
// whether the user can edit a cipher doesn't take into account whether
|
|
|
|
// the user is an org owner/admin. The `admin` variant first checks
|
|
|
|
// whether the user is an owner/admin of the relevant org, and if so,
|
|
|
|
// allows the operation unconditionally.
|
|
|
|
//
|
2021-04-27 23:18:32 +02:00
|
|
|
// vaultwarden factors in the org owner/admin status as part of
|
2020-10-23 12:42:22 +02:00
|
|
|
// determining the write accessibility of a cipher, so most
|
|
|
|
// admin/non-admin implementations can be shared.
|
2018-10-10 20:40:39 +02:00
|
|
|
routes![
|
|
|
|
sync,
|
|
|
|
get_ciphers,
|
|
|
|
get_cipher,
|
|
|
|
get_cipher_admin,
|
|
|
|
get_cipher_details,
|
|
|
|
post_ciphers,
|
|
|
|
put_cipher_admin,
|
|
|
|
post_ciphers_admin,
|
2018-11-19 20:21:02 +01:00
|
|
|
post_ciphers_create,
|
2018-10-10 20:40:39 +02:00
|
|
|
post_ciphers_import,
|
2021-05-16 07:46:57 +02:00
|
|
|
get_attachment,
|
2021-05-25 12:48:57 +02:00
|
|
|
post_attachment_v2,
|
|
|
|
post_attachment_v2_data,
|
|
|
|
post_attachment, // legacy
|
|
|
|
post_attachment_admin, // legacy
|
2018-10-10 20:40:39 +02:00
|
|
|
post_attachment_share,
|
|
|
|
delete_attachment_post,
|
|
|
|
delete_attachment_post_admin,
|
|
|
|
delete_attachment,
|
|
|
|
delete_attachment_admin,
|
|
|
|
post_cipher_admin,
|
|
|
|
post_cipher_share,
|
|
|
|
put_cipher_share,
|
2020-12-10 09:17:34 +01:00
|
|
|
put_cipher_share_selected,
|
2018-10-10 20:40:39 +02:00
|
|
|
post_cipher,
|
|
|
|
put_cipher,
|
|
|
|
delete_cipher_post,
|
|
|
|
delete_cipher_post_admin,
|
2020-04-17 22:35:27 +02:00
|
|
|
delete_cipher_put,
|
|
|
|
delete_cipher_put_admin,
|
2018-10-10 20:40:39 +02:00
|
|
|
delete_cipher,
|
|
|
|
delete_cipher_admin,
|
|
|
|
delete_cipher_selected,
|
|
|
|
delete_cipher_selected_post,
|
2020-04-17 22:35:27 +02:00
|
|
|
delete_cipher_selected_put,
|
2020-10-23 12:42:22 +02:00
|
|
|
delete_cipher_selected_admin,
|
|
|
|
delete_cipher_selected_post_admin,
|
|
|
|
delete_cipher_selected_put_admin,
|
2020-04-17 22:35:27 +02:00
|
|
|
restore_cipher_put,
|
|
|
|
restore_cipher_put_admin,
|
|
|
|
restore_cipher_selected,
|
2018-10-10 20:40:39 +02:00
|
|
|
delete_all,
|
|
|
|
move_cipher_selected,
|
|
|
|
move_cipher_selected_put,
|
2019-01-08 20:27:28 +01:00
|
|
|
put_collections_update,
|
2018-10-10 20:40:39 +02:00
|
|
|
post_collections_update,
|
|
|
|
post_collections_admin,
|
|
|
|
put_collections_admin,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
pub async fn purge_trashed_ciphers(pool: DbPool) {
|
2021-04-03 05:52:15 +02:00
|
|
|
debug!("Purging trashed ciphers");
|
2021-11-07 18:53:39 +01:00
|
|
|
if let Ok(conn) = pool.get().await {
|
2021-04-03 05:52:15 +02:00
|
|
|
Cipher::purge_trash(&conn);
|
|
|
|
} else {
|
|
|
|
error!("Failed to get DB connection while purging trashed ciphers")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
#[derive(FromForm, Default)]
|
2018-10-17 23:22:07 +02:00
|
|
|
struct SyncData {
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = "excludeDomains")]
|
2018-10-10 20:40:39 +02:00
|
|
|
exclude_domains: bool, // Default: 'false'
|
2018-10-17 23:22:07 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
#[get("/sync?<data..>")]
|
2021-11-07 18:53:39 +01:00
|
|
|
fn sync(data: SyncData, headers: Headers, conn: DbConn) -> Json<Value> {
|
2018-04-24 22:01:55 +02:00
|
|
|
let user_json = headers.user.to_json(&conn);
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-02-20 14:09:00 +01:00
|
|
|
let folders = Folder::find_by_user(&headers.user.uuid, &conn);
|
2019-02-27 17:21:04 +01:00
|
|
|
let folders_json: Vec<Value> = folders.iter().map(Folder::to_json).collect();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-06-01 00:20:09 +02:00
|
|
|
let collections = Collection::find_by_user_uuid(&headers.user.uuid, &conn);
|
2021-04-06 22:54:42 +02:00
|
|
|
let collections_json: Vec<Value> =
|
|
|
|
collections.iter().map(|c| c.to_json_details(&headers.user.uuid, &conn)).collect();
|
2018-06-01 00:20:09 +02:00
|
|
|
|
2021-09-24 17:55:49 +02:00
|
|
|
let policies = OrgPolicy::find_confirmed_by_user(&headers.user.uuid, &conn);
|
2020-03-14 13:22:30 +01:00
|
|
|
let policies_json: Vec<Value> = policies.iter().map(OrgPolicy::to_json).collect();
|
|
|
|
|
2020-09-01 09:49:48 +02:00
|
|
|
let ciphers = Cipher::find_by_user_visible(&headers.user.uuid, &conn);
|
2021-04-06 22:54:42 +02:00
|
|
|
let ciphers_json: Vec<Value> =
|
|
|
|
ciphers.iter().map(|c| c.to_json(&headers.host, &headers.user.uuid, &conn)).collect();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2021-03-14 23:35:55 +01:00
|
|
|
let sends = Send::find_by_user(&headers.user.uuid, &conn);
|
2021-03-31 22:18:35 +02:00
|
|
|
let sends_json: Vec<Value> = sends.iter().map(|s| s.to_json()).collect();
|
2021-03-14 23:35:55 +01:00
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
let domains_json = if data.exclude_domains {
|
|
|
|
Value::Null
|
|
|
|
} else {
|
2021-03-27 16:07:26 +01:00
|
|
|
api::core::_get_eq_domains(headers, true).into_inner()
|
2018-12-30 23:34:31 +01:00
|
|
|
};
|
2018-02-20 14:09:00 +01:00
|
|
|
|
2021-03-27 16:07:26 +01:00
|
|
|
Json(json!({
|
2018-02-20 14:09:00 +01:00
|
|
|
"Profile": user_json,
|
2018-02-10 01:00:55 +01:00
|
|
|
"Folders": folders_json,
|
2018-06-01 00:20:09 +02:00
|
|
|
"Collections": collections_json,
|
2020-03-14 13:22:30 +01:00
|
|
|
"Policies": policies_json,
|
2018-02-10 01:00:55 +01:00
|
|
|
"Ciphers": ciphers_json,
|
2018-02-20 14:09:00 +01:00
|
|
|
"Domains": domains_json,
|
2021-03-14 23:35:55 +01:00
|
|
|
"Sends": sends_json,
|
2021-04-06 20:38:22 +02:00
|
|
|
"unofficialServer": true,
|
2018-02-10 01:00:55 +01:00
|
|
|
"Object": "sync"
|
2021-03-27 16:07:26 +01:00
|
|
|
}))
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/ciphers")]
|
2021-03-27 16:07:26 +01:00
|
|
|
fn get_ciphers(headers: Headers, conn: DbConn) -> Json<Value> {
|
2020-09-01 09:49:48 +02:00
|
|
|
let ciphers = Cipher::find_by_user_visible(&headers.user.uuid, &conn);
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2021-04-06 22:54:42 +02:00
|
|
|
let ciphers_json: Vec<Value> =
|
|
|
|
ciphers.iter().map(|c| c.to_json(&headers.host, &headers.user.uuid, &conn)).collect();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2021-03-27 16:07:26 +01:00
|
|
|
Json(json!({
|
2018-02-10 01:00:55 +01:00
|
|
|
"Data": ciphers_json,
|
|
|
|
"Object": "list",
|
2018-10-01 18:02:58 +02:00
|
|
|
"ContinuationToken": null
|
2021-03-27 16:07:26 +01:00
|
|
|
}))
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/ciphers/<uuid>")]
|
2018-02-17 20:47:13 +01:00
|
|
|
fn get_cipher(uuid: String, headers: Headers, conn: DbConn) -> JsonResult {
|
2018-02-10 01:00:55 +01:00
|
|
|
let cipher = match Cipher::find_by_uuid(&uuid, &conn) {
|
|
|
|
Some(cipher) => cipher,
|
2018-12-30 23:34:31 +01:00
|
|
|
None => err!("Cipher doesn't exist"),
|
2018-02-10 01:00:55 +01:00
|
|
|
};
|
|
|
|
|
2018-04-30 11:52:15 +02:00
|
|
|
if !cipher.is_accessible_to_user(&headers.user.uuid, &conn) {
|
2018-02-15 00:40:34 +01:00
|
|
|
err!("Cipher is not owned by user")
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-04-30 11:52:15 +02:00
|
|
|
Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn)))
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-05-04 19:25:50 +02:00
|
|
|
#[get("/ciphers/<uuid>/admin")]
|
|
|
|
fn get_cipher_admin(uuid: String, headers: Headers, conn: DbConn) -> JsonResult {
|
|
|
|
// TODO: Implement this correctly
|
|
|
|
get_cipher(uuid, headers, conn)
|
|
|
|
}
|
|
|
|
|
2018-05-18 00:03:08 +02:00
|
|
|
#[get("/ciphers/<uuid>/details")]
|
|
|
|
fn get_cipher_details(uuid: String, headers: Headers, conn: DbConn) -> JsonResult {
|
|
|
|
get_cipher(uuid, headers, conn)
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[allow(non_snake_case)]
|
2018-09-13 15:16:24 +02:00
|
|
|
pub struct CipherData {
|
2018-08-13 13:35:41 +02:00
|
|
|
// Id is optional as it is included only in bulk share
|
2018-11-24 23:00:41 +01:00
|
|
|
pub Id: Option<String>,
|
2018-02-23 00:38:54 +01:00
|
|
|
// Folder id is not included in import
|
2018-06-01 00:18:50 +02:00
|
|
|
FolderId: Option<String>,
|
2018-02-23 00:38:54 +01:00
|
|
|
// TODO: Some of these might appear all the time, no need for Option
|
2018-06-01 00:18:50 +02:00
|
|
|
OrganizationId: Option<String>,
|
2018-03-06 00:02:36 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Login = 1,
|
|
|
|
SecureNote = 2,
|
|
|
|
Card = 3,
|
|
|
|
Identity = 4
|
|
|
|
*/
|
2018-09-13 15:16:24 +02:00
|
|
|
pub Type: i32, // TODO: Change this to NumberOrString
|
|
|
|
pub Name: String,
|
2018-06-01 00:18:50 +02:00
|
|
|
Notes: Option<String>,
|
|
|
|
Fields: Option<Value>,
|
2018-02-16 00:32:26 +01:00
|
|
|
|
2018-03-06 00:02:36 +01:00
|
|
|
// Only one of these should exist, depending on type
|
2018-06-01 00:18:50 +02:00
|
|
|
Login: Option<Value>,
|
|
|
|
SecureNote: Option<Value>,
|
|
|
|
Card: Option<Value>,
|
|
|
|
Identity: Option<Value>,
|
2018-02-16 00:32:26 +01:00
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
Favorite: Option<bool>,
|
2021-05-01 10:06:06 +02:00
|
|
|
Reprompt: Option<i32>,
|
2018-08-27 23:08:58 +02:00
|
|
|
|
|
|
|
PasswordHistory: Option<Value>,
|
2018-11-27 17:24:12 +01:00
|
|
|
|
|
|
|
// These are used during key rotation
|
|
|
|
#[serde(rename = "Attachments")]
|
|
|
|
_Attachments: Option<Value>, // Unused, contains map of {id: filename}
|
2018-12-30 23:34:31 +01:00
|
|
|
Attachments2: Option<HashMap<String, Attachments2Data>>,
|
2020-12-06 01:22:20 +01:00
|
|
|
|
|
|
|
// The revision datetime (in ISO 8601 format) of the client's local copy
|
|
|
|
// of the cipher. This is used to prevent a client from updating a cipher
|
|
|
|
// when it doesn't have the latest version, as that can result in data
|
|
|
|
// loss. It's not an error when no value is provided; this can happen
|
|
|
|
// when using older client versions, or if the operation doesn't involve
|
|
|
|
// updating an existing cipher.
|
|
|
|
LastKnownRevisionDate: Option<String>,
|
2018-11-27 17:24:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub struct Attachments2Data {
|
|
|
|
FileName: String,
|
|
|
|
Key: String,
|
2018-04-27 13:49:34 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 09:17:34 +01:00
|
|
|
/// Called when an org admin clones an org cipher.
|
2018-04-27 13:49:34 +02:00
|
|
|
#[post("/ciphers/admin", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn post_ciphers_admin(data: JsonUpcase<ShareCipherData>, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
2020-12-10 09:17:34 +01:00
|
|
|
post_ciphers_create(data, headers, conn, nt)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when creating a new org-owned cipher, or cloning a cipher (whether
|
|
|
|
/// user- or org-owned). When cloning a cipher to a user-owned cipher,
|
|
|
|
/// `organizationId` is null.
|
|
|
|
#[post("/ciphers/create", data = "<data>")]
|
|
|
|
fn post_ciphers_create(data: JsonUpcase<ShareCipherData>, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
|
|
|
let mut data: ShareCipherData = data.into_inner().data;
|
2018-11-13 16:34:37 +01:00
|
|
|
|
2021-01-31 21:46:37 +01:00
|
|
|
// Check if there are one more more collections selected when this cipher is part of an organization.
|
|
|
|
// err if this is not the case before creating an empty cipher.
|
2021-03-31 22:18:35 +02:00
|
|
|
if data.Cipher.OrganizationId.is_some() && data.CollectionIds.is_empty() {
|
2021-01-31 21:46:37 +01:00
|
|
|
err!("You must select at least one collection.");
|
|
|
|
}
|
|
|
|
|
2021-01-24 05:50:06 +01:00
|
|
|
// This check is usually only needed in update_cipher_from_data(), but we
|
|
|
|
// need it here as well to avoid creating an empty cipher in the call to
|
|
|
|
// cipher.save() below.
|
2021-09-09 08:26:15 +02:00
|
|
|
enforce_personal_ownership_policy(Some(&data.Cipher), &headers, &conn)?;
|
2021-01-24 05:50:06 +01:00
|
|
|
|
2018-12-07 15:01:29 +01:00
|
|
|
let mut cipher = Cipher::new(data.Cipher.Type, data.Cipher.Name.clone());
|
2018-11-13 16:34:37 +01:00
|
|
|
cipher.user_uuid = Some(headers.user.uuid.clone());
|
2018-12-19 21:52:53 +01:00
|
|
|
cipher.save(&conn)?;
|
2018-11-13 16:34:37 +01:00
|
|
|
|
2020-12-10 09:17:34 +01:00
|
|
|
// When cloning a cipher, the Bitwarden clients seem to set this field
|
|
|
|
// based on the cipher being cloned (when creating a new cipher, it's set
|
|
|
|
// to null as expected). However, `cipher.created_at` is initialized to
|
|
|
|
// the current time, so the stale data check will end up failing down the
|
|
|
|
// line. Since this function only creates new ciphers (whether by cloning
|
|
|
|
// or otherwise), we can just ignore this field entirely.
|
|
|
|
data.Cipher.LastKnownRevisionDate = None;
|
2018-11-13 16:34:37 +01:00
|
|
|
|
2020-12-10 09:17:34 +01:00
|
|
|
share_cipher_by_uuid(&cipher.uuid, data, &headers, &conn, &nt)
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2020-12-10 09:17:34 +01:00
|
|
|
/// Called when creating a new user-owned cipher.
|
2018-02-10 01:00:55 +01:00
|
|
|
#[post("/ciphers", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn post_ciphers(data: JsonUpcase<CipherData>, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
2021-06-20 06:32:11 +02:00
|
|
|
let mut data: CipherData = data.into_inner().data;
|
|
|
|
|
|
|
|
// The web/browser clients set this field to null as expected, but the
|
|
|
|
// mobile clients seem to set the invalid value `0001-01-01T00:00:00`,
|
|
|
|
// which results in a warning message being logged. This field isn't
|
|
|
|
// needed when creating a new cipher, so just ignore it unconditionally.
|
|
|
|
data.LastKnownRevisionDate = None;
|
2018-02-17 23:38:55 +01:00
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
let mut cipher = Cipher::new(data.Type, data.Name.clone());
|
2018-12-30 23:34:31 +01:00
|
|
|
update_cipher_from_data(&mut cipher, data, &headers, false, &conn, &nt, UpdateType::CipherCreate)?;
|
2018-02-16 00:32:26 +01:00
|
|
|
|
2018-04-30 11:52:15 +02:00
|
|
|
Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn)))
|
2018-02-16 00:32:26 +01:00
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2021-01-24 05:50:06 +01:00
|
|
|
/// Enforces the personal ownership policy on user-owned ciphers, if applicable.
|
|
|
|
/// A non-owner/admin user belonging to an org with the personal ownership policy
|
|
|
|
/// enabled isn't allowed to create new user-owned ciphers or modify existing ones
|
|
|
|
/// (that were created before the policy was applicable to the user). The user is
|
|
|
|
/// allowed to delete or share such ciphers to an org, however.
|
|
|
|
///
|
|
|
|
/// Ref: https://bitwarden.com/help/article/policies/#personal-ownership
|
2021-09-09 08:26:15 +02:00
|
|
|
fn enforce_personal_ownership_policy(data: Option<&CipherData>, headers: &Headers, conn: &DbConn) -> EmptyResult {
|
|
|
|
if data.is_none() || data.unwrap().OrganizationId.is_none() {
|
2021-01-24 05:50:06 +01:00
|
|
|
let user_uuid = &headers.user.uuid;
|
2021-03-16 10:07:45 +01:00
|
|
|
let policy_type = OrgPolicyType::PersonalOwnership;
|
|
|
|
if OrgPolicy::is_applicable_to_user(user_uuid, policy_type, conn) {
|
2021-03-31 22:45:05 +02:00
|
|
|
err!("Due to an Enterprise Policy, you are restricted from saving items to your personal vault.")
|
2021-01-24 05:50:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
pub fn update_cipher_from_data(
|
|
|
|
cipher: &mut Cipher,
|
|
|
|
data: CipherData,
|
|
|
|
headers: &Headers,
|
|
|
|
shared_to_collection: bool,
|
|
|
|
conn: &DbConn,
|
|
|
|
nt: &Notify,
|
|
|
|
ut: UpdateType,
|
|
|
|
) -> EmptyResult {
|
2021-09-09 08:26:15 +02:00
|
|
|
enforce_personal_ownership_policy(Some(&data), headers, conn)?;
|
2021-01-24 05:50:06 +01:00
|
|
|
|
2020-12-06 01:22:20 +01:00
|
|
|
// Check that the client isn't updating an existing cipher with stale data.
|
|
|
|
if let Some(dt) = data.LastKnownRevisionDate {
|
2021-03-31 22:18:35 +02:00
|
|
|
match NaiveDateTime::parse_from_str(&dt, "%+") {
|
|
|
|
// ISO 8601 format
|
|
|
|
Err(err) => warn!("Error parsing LastKnownRevisionDate '{}': {}", dt, err),
|
|
|
|
Ok(dt) if cipher.updated_at.signed_duration_since(dt).num_seconds() > 1 => {
|
|
|
|
err!("The client copy of this cipher is out of date. Resync the client and try again.")
|
|
|
|
}
|
2020-12-06 01:22:20 +01:00
|
|
|
Ok(_) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 12:33:43 +01:00
|
|
|
if cipher.organization_uuid.is_some() && cipher.organization_uuid != data.OrganizationId {
|
2019-01-15 16:35:08 +01:00
|
|
|
err!("Organization mismatch. Please resync the client before updating the cipher")
|
|
|
|
}
|
|
|
|
|
2018-08-29 15:22:03 +02:00
|
|
|
if let Some(org_id) = data.OrganizationId {
|
2021-06-19 22:02:03 +02:00
|
|
|
match UserOrganization::find_by_user_and_org(&headers.user.uuid, &org_id, conn) {
|
2018-08-29 15:22:03 +02:00
|
|
|
None => err!("You don't have permission to add item to organization"),
|
2018-12-30 23:34:31 +01:00
|
|
|
Some(org_user) => {
|
|
|
|
if shared_to_collection
|
|
|
|
|| org_user.has_full_access()
|
2021-06-19 22:02:03 +02:00
|
|
|
|| cipher.is_write_accessible_to_user(&headers.user.uuid, conn)
|
2018-12-30 23:34:31 +01:00
|
|
|
{
|
|
|
|
cipher.organization_uuid = Some(org_id);
|
2021-01-31 21:46:37 +01:00
|
|
|
// After some discussion in PR #1329 re-added the user_uuid = None again.
|
|
|
|
// TODO: Audit/Check the whole save/update cipher chain.
|
|
|
|
// Upstream uses the user_uuid to allow a cipher added by a user to an org to still allow the user to view/edit the cipher
|
|
|
|
// even when the user has hide-passwords configured as there policy.
|
|
|
|
// Removing the line below would fix that, but we have to check which effect this would have on the rest of the code.
|
2018-12-30 23:34:31 +01:00
|
|
|
cipher.user_uuid = None;
|
|
|
|
} else {
|
|
|
|
err!("You don't have permission to add cipher directly to organization")
|
|
|
|
}
|
2018-05-04 19:02:19 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-29 15:22:03 +02:00
|
|
|
} else {
|
|
|
|
cipher.user_uuid = Some(headers.user.uuid.clone());
|
2018-05-04 19:02:19 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
if let Some(ref folder_id) = data.FolderId {
|
2018-04-19 17:54:56 +02:00
|
|
|
match Folder::find_by_uuid(folder_id, conn) {
|
2018-02-15 00:40:34 +01:00
|
|
|
Some(folder) => {
|
|
|
|
if folder.user_uuid != headers.user.uuid {
|
|
|
|
err!("Folder is not owned by user")
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 23:34:31 +01:00
|
|
|
None => err!("Folder doesn't exist"),
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-11-27 17:24:12 +01:00
|
|
|
// Modify attachments name and keys when rotating
|
|
|
|
if let Some(attachments) = data.Attachments2 {
|
|
|
|
for (id, attachment) in attachments {
|
2021-06-19 22:02:03 +02:00
|
|
|
let mut saved_att = match Attachment::find_by_id(&id, conn) {
|
2018-11-27 17:24:12 +01:00
|
|
|
Some(att) => att,
|
2018-12-30 23:34:31 +01:00
|
|
|
None => err!("Attachment doesn't exist"),
|
2018-11-27 17:24:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if saved_att.cipher_uuid != cipher.uuid {
|
2020-06-07 17:57:04 +02:00
|
|
|
// Warn and break here since cloning ciphers provides attachment data but will not be cloned.
|
|
|
|
// If we error out here it will break the whole cloning and causes empty ciphers to appear.
|
|
|
|
warn!("Attachment is not owned by the cipher");
|
|
|
|
break;
|
2018-11-27 17:24:12 +01:00
|
|
|
}
|
|
|
|
|
2019-05-20 21:24:29 +02:00
|
|
|
saved_att.akey = Some(attachment.Key);
|
2018-11-27 17:24:12 +01:00
|
|
|
saved_att.file_name = attachment.FileName;
|
|
|
|
|
2021-06-19 22:02:03 +02:00
|
|
|
saved_att.save(conn)?;
|
2018-11-27 17:24:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 21:46:37 +01:00
|
|
|
// Cleanup cipher data, like removing the 'Response' key.
|
|
|
|
// This key is somewhere generated during Javascript so no way for us this fix this.
|
|
|
|
// Also, upstream only retrieves keys they actually want to store, and thus skip the 'Response' key.
|
|
|
|
// We do not mind which data is in it, the keep our model more flexible when there are upstream changes.
|
|
|
|
// But, we at least know we do not need to store and return this specific key.
|
|
|
|
fn _clean_cipher_data(mut json_data: Value) -> Value {
|
|
|
|
if json_data.is_array() {
|
2021-03-31 22:18:35 +02:00
|
|
|
json_data.as_array_mut().unwrap().iter_mut().for_each(|ref mut f| {
|
|
|
|
f.as_object_mut().unwrap().remove("Response");
|
|
|
|
});
|
2021-01-31 21:46:37 +01:00
|
|
|
};
|
|
|
|
json_data
|
|
|
|
}
|
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
let type_data_opt = match data.Type {
|
|
|
|
1 => data.Login,
|
|
|
|
2 => data.SecureNote,
|
|
|
|
3 => data.Card,
|
|
|
|
4 => data.Identity,
|
2018-12-30 23:34:31 +01:00
|
|
|
_ => err!("Invalid type"),
|
2018-02-16 00:32:26 +01:00
|
|
|
};
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2021-01-31 21:46:37 +01:00
|
|
|
let type_data = match type_data_opt {
|
|
|
|
Some(mut data) => {
|
|
|
|
// Remove the 'Response' key from the base object.
|
|
|
|
data.as_object_mut().unwrap().remove("Response");
|
|
|
|
// Remove the 'Response' key from every Uri.
|
|
|
|
if data["Uris"].is_array() {
|
|
|
|
data["Uris"] = _clean_cipher_data(data["Uris"].clone());
|
|
|
|
}
|
|
|
|
data
|
2021-03-31 22:18:35 +02:00
|
|
|
}
|
2018-12-30 23:34:31 +01:00
|
|
|
None => err!("Data missing"),
|
2018-02-16 00:32:26 +01:00
|
|
|
};
|
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
cipher.name = data.Name;
|
|
|
|
cipher.notes = data.Notes;
|
2021-03-31 22:18:35 +02:00
|
|
|
cipher.fields = data.Fields.map(|f| _clean_cipher_data(f).to_string());
|
2018-06-12 23:15:27 +02:00
|
|
|
cipher.data = type_data.to_string();
|
2018-08-27 23:08:58 +02:00
|
|
|
cipher.password_history = data.PasswordHistory.map(|f| f.to_string());
|
2021-05-01 10:06:06 +02:00
|
|
|
cipher.reprompt = data.Reprompt;
|
2019-01-25 18:23:51 +01:00
|
|
|
|
2021-06-19 22:02:03 +02:00
|
|
|
cipher.save(conn)?;
|
|
|
|
cipher.move_to_folder(data.FolderId, &headers.user.uuid, conn)?;
|
|
|
|
cipher.set_favorite(data.Favorite, &headers.user.uuid, conn)?;
|
2018-05-19 22:33:15 +02:00
|
|
|
|
2019-01-28 00:39:14 +01:00
|
|
|
if ut != UpdateType::None {
|
2021-06-19 22:02:03 +02:00
|
|
|
nt.send_cipher_update(ut, cipher, &cipher.update_users_revision(conn));
|
2019-01-28 00:39:14 +01:00
|
|
|
}
|
2018-05-19 22:33:15 +02:00
|
|
|
|
2019-01-15 15:28:25 +01:00
|
|
|
Ok(())
|
2018-02-16 00:32:26 +01:00
|
|
|
}
|
|
|
|
|
2018-02-23 00:38:54 +01:00
|
|
|
use super::folders::FolderData;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct ImportData {
|
2018-06-01 00:18:50 +02:00
|
|
|
Ciphers: Vec<CipherData>,
|
|
|
|
Folders: Vec<FolderData>,
|
|
|
|
FolderRelationships: Vec<RelationsData>,
|
2018-02-23 00:38:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct RelationsData {
|
|
|
|
// Cipher id
|
2018-06-13 14:25:50 +02:00
|
|
|
Key: usize,
|
2018-02-23 00:38:54 +01:00
|
|
|
// Folder id
|
2018-06-13 14:25:50 +02:00
|
|
|
Value: usize,
|
2018-02-23 00:38:54 +01:00
|
|
|
}
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
#[post("/ciphers/import", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn post_ciphers_import(data: JsonUpcase<ImportData>, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
2021-09-09 08:26:15 +02:00
|
|
|
enforce_personal_ownership_policy(None, &headers, &conn)?;
|
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
let data: ImportData = data.into_inner().data;
|
2018-02-16 00:32:26 +01:00
|
|
|
|
|
|
|
// Read and create the folders
|
2018-10-14 14:55:00 +02:00
|
|
|
let mut folders: Vec<_> = Vec::new();
|
|
|
|
for folder in data.Folders.into_iter() {
|
|
|
|
let mut new_folder = Folder::new(headers.user.uuid.clone(), folder.Name);
|
2018-12-19 21:52:53 +01:00
|
|
|
new_folder.save(&conn)?;
|
|
|
|
|
|
|
|
folders.push(new_folder);
|
2018-10-14 14:55:00 +02:00
|
|
|
}
|
2018-02-16 00:32:26 +01:00
|
|
|
|
|
|
|
// Read the relations between folders and ciphers
|
2018-02-23 00:38:54 +01:00
|
|
|
let mut relations_map = HashMap::new();
|
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
for relation in data.FolderRelationships {
|
2018-06-13 14:25:50 +02:00
|
|
|
relations_map.insert(relation.Key, relation.Value);
|
2018-02-23 00:38:54 +01:00
|
|
|
}
|
2018-02-16 00:32:26 +01:00
|
|
|
|
|
|
|
// Read and create the ciphers
|
2019-01-28 00:39:14 +01:00
|
|
|
for (index, mut cipher_data) in data.Ciphers.into_iter().enumerate() {
|
2018-12-30 23:34:31 +01:00
|
|
|
let folder_uuid = relations_map.get(&index).map(|i| folders[*i].uuid.clone());
|
2019-01-28 00:39:14 +01:00
|
|
|
cipher_data.FolderId = folder_uuid;
|
2018-02-16 00:32:26 +01:00
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
let mut cipher = Cipher::new(cipher_data.Type, cipher_data.Name.clone());
|
2019-01-28 00:39:14 +01:00
|
|
|
update_cipher_from_data(&mut cipher, cipher_data, &headers, false, &conn, &nt, UpdateType::None)?;
|
2018-02-23 00:38:54 +01:00
|
|
|
}
|
2018-02-16 00:32:26 +01:00
|
|
|
|
2018-08-13 11:58:39 +02:00
|
|
|
let mut user = headers.user;
|
2019-01-28 00:39:14 +01:00
|
|
|
user.update_revision(&conn)?;
|
|
|
|
nt.send_user_update(UpdateType::Vault, &user);
|
|
|
|
Ok(())
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2020-12-10 09:17:34 +01:00
|
|
|
/// Called when an org admin modifies an existing org cipher.
|
2018-08-15 15:27:37 +02:00
|
|
|
#[put("/ciphers/<uuid>/admin", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn put_cipher_admin(
|
|
|
|
uuid: String,
|
|
|
|
data: JsonUpcase<CipherData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> JsonResult {
|
|
|
|
put_cipher(uuid, data, headers, conn, nt)
|
2018-08-15 15:27:37 +02:00
|
|
|
}
|
|
|
|
|
2018-05-04 19:25:50 +02:00
|
|
|
#[post("/ciphers/<uuid>/admin", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn post_cipher_admin(
|
|
|
|
uuid: String,
|
|
|
|
data: JsonUpcase<CipherData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> JsonResult {
|
|
|
|
post_cipher(uuid, data, headers, conn, nt)
|
2018-05-04 19:25:50 +02:00
|
|
|
}
|
|
|
|
|
2018-02-16 00:32:26 +01:00
|
|
|
#[post("/ciphers/<uuid>", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn post_cipher(uuid: String, data: JsonUpcase<CipherData>, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
|
|
|
put_cipher(uuid, data, headers, conn, nt)
|
2018-02-16 00:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/ciphers/<uuid>", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn put_cipher(uuid: String, data: JsonUpcase<CipherData>, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
2018-06-01 00:18:50 +02:00
|
|
|
let data: CipherData = data.into_inner().data;
|
2018-02-17 23:38:55 +01:00
|
|
|
|
2018-02-16 00:32:26 +01:00
|
|
|
let mut cipher = match Cipher::find_by_uuid(&uuid, &conn) {
|
|
|
|
Some(cipher) => cipher,
|
2018-12-30 23:34:31 +01:00
|
|
|
None => err!("Cipher doesn't exist"),
|
2018-02-16 00:32:26 +01:00
|
|
|
};
|
|
|
|
|
2020-08-19 11:16:27 +02:00
|
|
|
// TODO: Check if only the folder ID or favorite status is being changed.
|
|
|
|
// These are per-user properties that technically aren't part of the
|
|
|
|
// cipher itself, so the user shouldn't need write access to change these.
|
|
|
|
// Interestingly, upstream Bitwarden doesn't properly handle this either.
|
|
|
|
|
2018-04-30 11:52:15 +02:00
|
|
|
if !cipher.is_write_accessible_to_user(&headers.user.uuid, &conn) {
|
|
|
|
err!("Cipher is not write accessible")
|
2018-02-16 00:32:26 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
update_cipher_from_data(&mut cipher, data, &headers, false, &conn, &nt, UpdateType::CipherUpdate)?;
|
2018-02-16 00:32:26 +01:00
|
|
|
|
2018-04-30 11:52:15 +02:00
|
|
|
Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn)))
|
2018-02-16 00:32:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-09 12:55:05 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct CollectionsAdminData {
|
2018-06-01 00:18:50 +02:00
|
|
|
CollectionIds: Vec<String>,
|
2018-05-09 12:55:05 +02:00
|
|
|
}
|
|
|
|
|
2019-01-08 20:27:28 +01:00
|
|
|
#[put("/ciphers/<uuid>/collections", data = "<data>")]
|
|
|
|
fn put_collections_update(
|
|
|
|
uuid: String,
|
|
|
|
data: JsonUpcase<CollectionsAdminData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
) -> EmptyResult {
|
|
|
|
post_collections_admin(uuid, data, headers, conn)
|
|
|
|
}
|
|
|
|
|
2018-05-18 00:03:08 +02:00
|
|
|
#[post("/ciphers/<uuid>/collections", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn post_collections_update(
|
|
|
|
uuid: String,
|
|
|
|
data: JsonUpcase<CollectionsAdminData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
) -> EmptyResult {
|
2018-05-18 00:03:08 +02:00
|
|
|
post_collections_admin(uuid, data, headers, conn)
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:00:10 +02:00
|
|
|
#[put("/ciphers/<uuid>/collections-admin", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn put_collections_admin(
|
|
|
|
uuid: String,
|
|
|
|
data: JsonUpcase<CollectionsAdminData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
) -> EmptyResult {
|
2018-08-13 17:00:10 +02:00
|
|
|
post_collections_admin(uuid, data, headers, conn)
|
|
|
|
}
|
|
|
|
|
2018-05-09 12:55:05 +02:00
|
|
|
#[post("/ciphers/<uuid>/collections-admin", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn post_collections_admin(
|
|
|
|
uuid: String,
|
|
|
|
data: JsonUpcase<CollectionsAdminData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
) -> EmptyResult {
|
2018-06-01 00:18:50 +02:00
|
|
|
let data: CollectionsAdminData = data.into_inner().data;
|
2018-05-09 12:55:05 +02:00
|
|
|
|
|
|
|
let cipher = match Cipher::find_by_uuid(&uuid, &conn) {
|
|
|
|
Some(cipher) => cipher,
|
2018-12-30 23:34:31 +01:00
|
|
|
None => err!("Cipher doesn't exist"),
|
2018-05-09 12:55:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if !cipher.is_write_accessible_to_user(&headers.user.uuid, &conn) {
|
|
|
|
err!("Cipher is not write accessible")
|
|
|
|
}
|
|
|
|
|
2018-06-01 00:18:50 +02:00
|
|
|
let posted_collections: HashSet<String> = data.CollectionIds.iter().cloned().collect();
|
2021-04-06 22:54:42 +02:00
|
|
|
let current_collections: HashSet<String> =
|
|
|
|
cipher.get_collections(&headers.user.uuid, &conn).iter().cloned().collect();
|
2018-05-09 12:55:05 +02:00
|
|
|
|
|
|
|
for collection in posted_collections.symmetric_difference(¤t_collections) {
|
2021-06-19 22:02:03 +02:00
|
|
|
match Collection::find_by_uuid(collection, &conn) {
|
2018-05-11 12:45:55 +02:00
|
|
|
None => err!("Invalid collection ID provided"),
|
2018-05-09 12:55:05 +02:00
|
|
|
Some(collection) => {
|
|
|
|
if collection.is_writable_by_user(&headers.user.uuid, &conn) {
|
2018-12-30 23:34:31 +01:00
|
|
|
if posted_collections.contains(&collection.uuid) {
|
|
|
|
// Add to collection
|
2018-12-19 21:52:53 +01:00
|
|
|
CollectionCipher::save(&cipher.uuid, &collection.uuid, &conn)?;
|
2018-12-30 23:34:31 +01:00
|
|
|
} else {
|
|
|
|
// Remove from collection
|
2018-12-19 21:52:53 +01:00
|
|
|
CollectionCipher::delete(&cipher.uuid, &collection.uuid, &conn)?;
|
2018-05-09 12:55:05 +02:00
|
|
|
}
|
2018-05-11 12:45:55 +02:00
|
|
|
} else {
|
|
|
|
err!("No rights to modify the collection")
|
2018-05-09 12:55:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-02-16 00:32:26 +01:00
|
|
|
|
2018-05-14 17:13:59 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct ShareCipherData {
|
2018-06-01 00:18:50 +02:00
|
|
|
Cipher: CipherData,
|
|
|
|
CollectionIds: Vec<String>,
|
2018-05-14 17:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/ciphers/<uuid>/share", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn post_cipher_share(
|
|
|
|
uuid: String,
|
|
|
|
data: JsonUpcase<ShareCipherData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> JsonResult {
|
2018-06-01 00:18:50 +02:00
|
|
|
let data: ShareCipherData = data.into_inner().data;
|
2018-05-14 17:13:59 +02:00
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
share_cipher_by_uuid(&uuid, data, &headers, &conn, &nt)
|
2018-08-13 13:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/ciphers/<uuid>/share", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn put_cipher_share(
|
|
|
|
uuid: String,
|
|
|
|
data: JsonUpcase<ShareCipherData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> JsonResult {
|
2018-08-13 13:35:41 +02:00
|
|
|
let data: ShareCipherData = data.into_inner().data;
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
share_cipher_by_uuid(&uuid, data, &headers, &conn, &nt)
|
2018-08-13 13:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct ShareSelectedCipherData {
|
|
|
|
Ciphers: Vec<CipherData>,
|
2018-12-30 23:34:31 +01:00
|
|
|
CollectionIds: Vec<String>,
|
2018-08-13 13:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/ciphers/share", data = "<data>")]
|
2020-12-10 09:17:34 +01:00
|
|
|
fn put_cipher_share_selected(
|
2018-12-30 23:34:31 +01:00
|
|
|
data: JsonUpcase<ShareSelectedCipherData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> EmptyResult {
|
2018-08-13 13:35:41 +02:00
|
|
|
let mut data: ShareSelectedCipherData = data.into_inner().data;
|
|
|
|
let mut cipher_ids: Vec<String> = Vec::new();
|
|
|
|
|
2018-09-13 21:55:23 +02:00
|
|
|
if data.Ciphers.is_empty() {
|
2018-08-13 13:35:41 +02:00
|
|
|
err!("You must select at least one cipher.")
|
|
|
|
}
|
2018-08-29 15:22:19 +02:00
|
|
|
|
2018-09-13 21:55:23 +02:00
|
|
|
if data.CollectionIds.is_empty() {
|
2018-08-13 13:35:41 +02:00
|
|
|
err!("You must select at least one collection.")
|
|
|
|
}
|
2018-08-29 15:22:19 +02:00
|
|
|
|
2018-08-13 13:35:41 +02:00
|
|
|
for cipher in data.Ciphers.iter() {
|
|
|
|
match cipher.Id {
|
|
|
|
Some(ref id) => cipher_ids.push(id.to_string()),
|
2018-12-30 23:34:31 +01:00
|
|
|
None => err!("Request missing ids field"),
|
2018-08-13 13:35:41 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
while let Some(cipher) = data.Ciphers.pop() {
|
|
|
|
let mut shared_cipher_data = ShareCipherData {
|
|
|
|
Cipher: cipher,
|
2018-12-30 23:34:31 +01:00
|
|
|
CollectionIds: data.CollectionIds.clone(),
|
2018-08-13 13:35:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
match shared_cipher_data.Cipher.Id.take() {
|
2018-12-30 23:34:31 +01:00
|
|
|
Some(id) => share_cipher_by_uuid(&id, shared_cipher_data, &headers, &conn, &nt)?,
|
|
|
|
None => err!("Request missing ids field"),
|
2018-08-13 13:35:41 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
fn share_cipher_by_uuid(
|
|
|
|
uuid: &str,
|
|
|
|
data: ShareCipherData,
|
|
|
|
headers: &Headers,
|
|
|
|
conn: &DbConn,
|
|
|
|
nt: &Notify,
|
|
|
|
) -> JsonResult {
|
2021-06-19 22:02:03 +02:00
|
|
|
let mut cipher = match Cipher::find_by_uuid(uuid, conn) {
|
2018-05-15 14:57:00 +02:00
|
|
|
Some(cipher) => {
|
2021-06-19 22:02:03 +02:00
|
|
|
if cipher.is_write_accessible_to_user(&headers.user.uuid, conn) {
|
2018-05-15 14:57:00 +02:00
|
|
|
cipher
|
|
|
|
} else {
|
|
|
|
err!("Cipher is not write accessible")
|
|
|
|
}
|
2018-12-30 23:34:31 +01:00
|
|
|
}
|
|
|
|
None => err!("Cipher doesn't exist"),
|
2018-05-14 17:13:59 +02:00
|
|
|
};
|
|
|
|
|
2020-03-15 17:26:07 +01:00
|
|
|
let mut shared_to_collection = false;
|
|
|
|
|
2018-08-29 15:22:03 +02:00
|
|
|
match data.Cipher.OrganizationId.clone() {
|
2020-03-14 14:08:57 +01:00
|
|
|
// If we don't get an organization ID, we don't do anything
|
|
|
|
// No error because this is used when using the Clone functionality
|
2020-07-14 18:00:09 +02:00
|
|
|
None => {}
|
2018-08-29 15:22:03 +02:00
|
|
|
Some(organization_uuid) => {
|
2018-06-11 15:44:37 +02:00
|
|
|
for uuid in &data.CollectionIds {
|
2021-06-19 22:02:03 +02:00
|
|
|
match Collection::find_by_uuid_and_org(uuid, &organization_uuid, conn) {
|
2018-05-14 17:13:59 +02:00
|
|
|
None => err!("Invalid collection ID provided"),
|
|
|
|
Some(collection) => {
|
2021-06-19 22:02:03 +02:00
|
|
|
if collection.is_writable_by_user(&headers.user.uuid, conn) {
|
|
|
|
CollectionCipher::save(&cipher.uuid, &collection.uuid, conn)?;
|
2018-10-01 16:59:10 +02:00
|
|
|
shared_to_collection = true;
|
2018-05-14 17:13:59 +02:00
|
|
|
} else {
|
|
|
|
err!("No rights to modify the collection")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-15 17:26:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
update_cipher_from_data(
|
|
|
|
&mut cipher,
|
|
|
|
data.Cipher,
|
2021-06-19 22:02:03 +02:00
|
|
|
headers,
|
2020-03-15 17:26:07 +01:00
|
|
|
shared_to_collection,
|
2021-06-19 22:02:03 +02:00
|
|
|
conn,
|
|
|
|
nt,
|
2020-03-15 17:26:07 +01:00
|
|
|
UpdateType::CipherUpdate,
|
|
|
|
)?;
|
|
|
|
|
2021-06-19 22:02:03 +02:00
|
|
|
Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, conn)))
|
2018-05-14 17:13:59 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:48:57 +02:00
|
|
|
/// v2 API for downloading an attachment. This just redirects the client to
|
|
|
|
/// the actual location of an attachment.
|
|
|
|
///
|
|
|
|
/// Upstream added this v2 API to support direct download of attachments from
|
|
|
|
/// their object storage service. For self-hosted instances, it basically just
|
|
|
|
/// redirects to the same location as before the v2 API.
|
2021-05-16 07:46:57 +02:00
|
|
|
#[get("/ciphers/<uuid>/attachment/<attachment_id>")]
|
|
|
|
fn get_attachment(uuid: String, attachment_id: String, headers: Headers, conn: DbConn) -> JsonResult {
|
|
|
|
match Attachment::find_by_id(&attachment_id, &conn) {
|
|
|
|
Some(attachment) if uuid == attachment.cipher_uuid => Ok(Json(attachment.to_json(&headers.host))),
|
|
|
|
Some(_) => err!("Attachment doesn't belong to cipher"),
|
|
|
|
None => err!("Attachment doesn't exist"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:48:57 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct AttachmentRequestData {
|
|
|
|
Key: String,
|
|
|
|
FileName: String,
|
|
|
|
FileSize: i32,
|
2021-08-19 05:30:18 +02:00
|
|
|
AdminRequest: Option<bool>, // true when attaching from an org vault view
|
2021-05-25 12:48:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum FileUploadType {
|
|
|
|
Direct = 0,
|
|
|
|
// Azure = 1, // only used upstream
|
|
|
|
}
|
|
|
|
|
|
|
|
/// v2 API for creating an attachment associated with a cipher.
|
|
|
|
/// This redirects the client to the API it should use to upload the attachment.
|
|
|
|
/// For upstream's cloud-hosted service, it's an Azure object storage API.
|
|
|
|
/// For self-hosted instances, it's another API on the local instance.
|
|
|
|
#[post("/ciphers/<uuid>/attachment/v2", data = "<data>")]
|
|
|
|
fn post_attachment_v2(
|
2019-01-28 00:39:14 +01:00
|
|
|
uuid: String,
|
2021-05-25 12:48:57 +02:00
|
|
|
data: JsonUpcase<AttachmentRequestData>,
|
2019-01-28 00:39:14 +01:00
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
) -> JsonResult {
|
2018-02-15 00:40:34 +01:00
|
|
|
let cipher = match Cipher::find_by_uuid(&uuid, &conn) {
|
2021-05-25 12:48:57 +02:00
|
|
|
Some(cipher) => cipher,
|
|
|
|
None => err!("Cipher doesn't exist"),
|
|
|
|
};
|
|
|
|
|
|
|
|
if !cipher.is_write_accessible_to_user(&headers.user.uuid, &conn) {
|
|
|
|
err!("Cipher is not write accessible")
|
|
|
|
}
|
|
|
|
|
2021-05-26 08:15:24 +02:00
|
|
|
let attachment_id = crypto::generate_attachment_id();
|
2021-05-25 12:48:57 +02:00
|
|
|
let data: AttachmentRequestData = data.into_inner().data;
|
|
|
|
let attachment =
|
|
|
|
Attachment::new(attachment_id.clone(), cipher.uuid.clone(), data.FileName, data.FileSize, Some(data.Key));
|
|
|
|
attachment.save(&conn).expect("Error saving attachment");
|
|
|
|
|
|
|
|
let url = format!("/ciphers/{}/attachment/{}", cipher.uuid, attachment_id);
|
2021-08-19 05:30:18 +02:00
|
|
|
let response_key = match data.AdminRequest {
|
|
|
|
Some(b) if b => "CipherMiniResponse",
|
|
|
|
_ => "CipherResponse",
|
|
|
|
};
|
2021-05-25 12:48:57 +02:00
|
|
|
|
|
|
|
Ok(Json(json!({ // AttachmentUploadDataResponseModel
|
|
|
|
"Object": "attachment-fileUpload",
|
|
|
|
"AttachmentId": attachment_id,
|
|
|
|
"Url": url,
|
|
|
|
"FileUploadType": FileUploadType::Direct as i32,
|
2021-08-19 05:30:18 +02:00
|
|
|
response_key: cipher.to_json(&headers.host, &headers.user.uuid, &conn),
|
2021-05-25 12:48:57 +02:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct UploadData<'f> {
|
|
|
|
key: Option<String>,
|
|
|
|
data: TempFile<'f>,
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:48:57 +02:00
|
|
|
/// Saves the data content of an attachment to a file. This is common code
|
|
|
|
/// shared between the v2 and legacy attachment APIs.
|
|
|
|
///
|
|
|
|
/// When used with the legacy API, this function is responsible for creating
|
|
|
|
/// the attachment database record, so `attachment` is None.
|
|
|
|
///
|
|
|
|
/// When used with the v2 API, post_attachment_v2() has already created the
|
|
|
|
/// database record, which is passed in as `attachment`.
|
2021-11-07 18:53:39 +01:00
|
|
|
async fn save_attachment(
|
2021-05-25 12:48:57 +02:00
|
|
|
mut attachment: Option<Attachment>,
|
|
|
|
cipher_uuid: String,
|
2021-11-07 18:53:39 +01:00
|
|
|
data: Form<UploadData<'_>>,
|
2021-05-25 12:48:57 +02:00
|
|
|
headers: &Headers,
|
2021-11-07 18:53:39 +01:00
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify<'_>,
|
|
|
|
) -> Result<(Cipher, DbConn), crate::error::Error> {
|
|
|
|
let cipher = match Cipher::find_by_uuid(&cipher_uuid, &conn) {
|
2018-02-15 00:40:34 +01:00
|
|
|
Some(cipher) => cipher,
|
2021-11-07 18:53:39 +01:00
|
|
|
None => err!("Cipher doesn't exist"),
|
2018-02-15 00:40:34 +01:00
|
|
|
};
|
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
if !cipher.is_write_accessible_to_user(&headers.user.uuid, &conn) {
|
|
|
|
err!("Cipher is not write accessible")
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
2020-03-14 13:22:30 +01:00
|
|
|
|
2021-05-26 08:17:22 +02:00
|
|
|
// In the v2 API, the attachment record has already been created,
|
|
|
|
// so the size limit needs to be adjusted to account for that.
|
|
|
|
let size_adjust = match &attachment {
|
|
|
|
None => 0, // Legacy API
|
|
|
|
Some(a) => a.file_size as i64, // v2 API
|
|
|
|
};
|
|
|
|
|
2020-02-17 22:56:26 +01:00
|
|
|
let size_limit = if let Some(ref user_uuid) = cipher.user_uuid {
|
|
|
|
match CONFIG.user_attachment_limit() {
|
2021-11-07 18:53:39 +01:00
|
|
|
Some(0) => err!("Attachments are disabled"),
|
2020-03-31 11:30:28 +02:00
|
|
|
Some(limit_kb) => {
|
2021-11-07 18:53:39 +01:00
|
|
|
let left = (limit_kb * 1024) - Attachment::size_by_user(user_uuid, &conn) + size_adjust;
|
2020-02-17 22:56:26 +01:00
|
|
|
if left <= 0 {
|
2021-11-07 18:53:39 +01:00
|
|
|
err!("Attachment storage limit reached! Delete some attachments to free up space")
|
2020-02-17 22:56:26 +01:00
|
|
|
}
|
|
|
|
Some(left as u64)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
} else if let Some(ref org_uuid) = cipher.organization_uuid {
|
|
|
|
match CONFIG.org_attachment_limit() {
|
2021-11-07 18:53:39 +01:00
|
|
|
Some(0) => err!("Attachments are disabled"),
|
2020-03-31 11:30:28 +02:00
|
|
|
Some(limit_kb) => {
|
2021-11-07 18:53:39 +01:00
|
|
|
let left = (limit_kb * 1024) - Attachment::size_by_org(org_uuid, &conn) + size_adjust;
|
2020-02-17 22:56:26 +01:00
|
|
|
if left <= 0 {
|
2021-11-07 18:53:39 +01:00
|
|
|
err!("Attachment storage limit reached! Delete some attachments to free up space")
|
2020-02-17 22:56:26 +01:00
|
|
|
}
|
|
|
|
Some(left as u64)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-07 18:53:39 +01:00
|
|
|
err!("Cipher is neither owned by a user nor an organization");
|
2020-02-17 22:56:26 +01:00
|
|
|
};
|
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
let mut data = data.into_inner();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
if let Some(size_limit) = size_limit {
|
|
|
|
if data.data.len() > size_limit {
|
|
|
|
err!("Attachment storage limit exceeded with this file");
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 12:48:57 +02:00
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
let file_id = match &attachment {
|
|
|
|
Some(attachment) => attachment.id.clone(), // v2 API
|
|
|
|
None => crypto::generate_attachment_id(), // Legacy API
|
|
|
|
};
|
2021-05-25 12:48:57 +02:00
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
let folder_path = tokio::fs::canonicalize(&CONFIG.attachments_folder()).await?.join(&cipher_uuid);
|
|
|
|
let file_path = folder_path.join(&file_id);
|
|
|
|
tokio::fs::create_dir_all(&folder_path).await?;
|
|
|
|
|
|
|
|
let size = data.data.len() as i32;
|
|
|
|
if let Some(attachment) = &mut attachment {
|
|
|
|
// v2 API
|
|
|
|
|
|
|
|
// Check the actual size against the size initially provided by
|
|
|
|
// the client. Upstream allows +/- 1 MiB deviation from this
|
|
|
|
// size, but it's not clear when or why this is needed.
|
|
|
|
const LEEWAY: i32 = 1024 * 1024; // 1 MiB
|
|
|
|
let min_size = attachment.file_size - LEEWAY;
|
|
|
|
let max_size = attachment.file_size + LEEWAY;
|
|
|
|
|
|
|
|
if min_size <= size && size <= max_size {
|
|
|
|
if size != attachment.file_size {
|
|
|
|
// Update the attachment with the actual file size.
|
|
|
|
attachment.file_size = size;
|
|
|
|
attachment.save(&conn).expect("Error updating attachment");
|
2018-12-30 23:34:31 +01:00
|
|
|
}
|
2021-11-07 18:53:39 +01:00
|
|
|
} else {
|
|
|
|
attachment.delete(&conn).ok();
|
2018-02-15 00:40:34 +01:00
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
err!(format!("Attachment size mismatch (expected within [{}, {}], got {})", min_size, max_size, size));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Legacy API
|
|
|
|
let encrypted_filename = data.data.raw_name().map(|s| s.dangerous_unsafe_unsanitized_raw().to_string());
|
|
|
|
|
|
|
|
if encrypted_filename.is_none() {
|
|
|
|
err!("No filename provided")
|
|
|
|
}
|
|
|
|
if data.key.is_none() {
|
|
|
|
err!("No attachment key provided")
|
|
|
|
}
|
|
|
|
let attachment = Attachment::new(file_id, cipher_uuid.clone(), encrypted_filename.unwrap(), size, data.key);
|
|
|
|
attachment.save(&conn).expect("Error saving attachment");
|
2020-02-17 22:56:26 +01:00
|
|
|
}
|
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
data.data.persist_to(file_path).await?;
|
|
|
|
|
|
|
|
nt.send_cipher_update(UpdateType::CipherUpdate, &cipher, &cipher.update_users_revision(&conn));
|
2019-01-11 01:08:35 +01:00
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
Ok((cipher, conn))
|
2021-05-25 12:48:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// v2 API for uploading the actual data content of an attachment.
|
|
|
|
/// This route needs a rank specified so that Rocket prioritizes the
|
|
|
|
/// /ciphers/<uuid>/attachment/v2 route, which would otherwise conflict
|
|
|
|
/// with this one.
|
|
|
|
#[post("/ciphers/<uuid>/attachment/<attachment_id>", format = "multipart/form-data", data = "<data>", rank = 1)]
|
2021-11-07 18:53:39 +01:00
|
|
|
async fn post_attachment_v2_data(
|
2021-05-25 12:48:57 +02:00
|
|
|
uuid: String,
|
|
|
|
attachment_id: String,
|
2021-11-07 18:53:39 +01:00
|
|
|
data: Form<UploadData<'_>>,
|
2021-05-25 12:48:57 +02:00
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
2021-11-07 18:53:39 +01:00
|
|
|
nt: Notify<'_>,
|
2021-05-25 12:48:57 +02:00
|
|
|
) -> EmptyResult {
|
|
|
|
let attachment = match Attachment::find_by_id(&attachment_id, &conn) {
|
|
|
|
Some(attachment) if uuid == attachment.cipher_uuid => Some(attachment),
|
|
|
|
Some(_) => err!("Attachment doesn't belong to cipher"),
|
|
|
|
None => err!("Attachment doesn't exist"),
|
|
|
|
};
|
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
save_attachment(attachment, uuid, data, &headers, conn, nt).await?;
|
2021-05-25 12:48:57 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Legacy API for creating an attachment associated with a cipher.
|
|
|
|
#[post("/ciphers/<uuid>/attachment", format = "multipart/form-data", data = "<data>")]
|
2021-11-07 18:53:39 +01:00
|
|
|
async fn post_attachment(
|
2021-05-25 12:48:57 +02:00
|
|
|
uuid: String,
|
2021-11-07 18:53:39 +01:00
|
|
|
data: Form<UploadData<'_>>,
|
2021-05-25 12:48:57 +02:00
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
2021-11-07 18:53:39 +01:00
|
|
|
nt: Notify<'_>,
|
2021-05-25 12:48:57 +02:00
|
|
|
) -> JsonResult {
|
|
|
|
// Setting this as None signifies to save_attachment() that it should create
|
|
|
|
// the attachment database record as well as saving the data to disk.
|
|
|
|
let attachment = None;
|
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
let (cipher, conn) = save_attachment(attachment, uuid, data, &headers, conn, nt).await?;
|
2021-05-25 12:48:57 +02:00
|
|
|
|
2018-04-30 11:52:15 +02:00
|
|
|
Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn)))
|
2018-02-15 00:40:34 +01:00
|
|
|
}
|
|
|
|
|
2018-07-14 08:09:20 +02:00
|
|
|
#[post("/ciphers/<uuid>/attachment-admin", format = "multipart/form-data", data = "<data>")]
|
2021-11-07 18:53:39 +01:00
|
|
|
async fn post_attachment_admin(
|
2018-12-30 23:34:31 +01:00
|
|
|
uuid: String,
|
2021-11-07 18:53:39 +01:00
|
|
|
data: Form<UploadData<'_>>,
|
2018-12-30 23:34:31 +01:00
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
2021-11-07 18:53:39 +01:00
|
|
|
nt: Notify<'_>,
|
2018-12-30 23:34:31 +01:00
|
|
|
) -> JsonResult {
|
2021-11-07 18:53:39 +01:00
|
|
|
post_attachment(uuid, data, headers, conn, nt).await
|
2018-07-14 08:09:20 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 22:54:42 +02:00
|
|
|
#[post("/ciphers/<uuid>/attachment/<attachment_id>/share", format = "multipart/form-data", data = "<data>")]
|
2021-11-07 18:53:39 +01:00
|
|
|
async fn post_attachment_share(
|
2018-12-30 23:34:31 +01:00
|
|
|
uuid: String,
|
|
|
|
attachment_id: String,
|
2021-11-07 18:53:39 +01:00
|
|
|
data: Form<UploadData<'_>>,
|
2018-12-30 23:34:31 +01:00
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
2021-11-07 18:53:39 +01:00
|
|
|
nt: Notify<'_>,
|
2018-12-30 23:34:31 +01:00
|
|
|
) -> JsonResult {
|
|
|
|
_delete_cipher_attachment_by_id(&uuid, &attachment_id, &headers, &conn, &nt)?;
|
2021-11-07 18:53:39 +01:00
|
|
|
post_attachment(uuid, data, headers, conn, nt).await
|
2018-07-14 08:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/ciphers/<uuid>/attachment/<attachment_id>/delete-admin")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn delete_attachment_post_admin(
|
|
|
|
uuid: String,
|
|
|
|
attachment_id: String,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> EmptyResult {
|
|
|
|
delete_attachment(uuid, attachment_id, headers, conn, nt)
|
2018-07-14 08:09:20 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 23:38:55 +01:00
|
|
|
#[post("/ciphers/<uuid>/attachment/<attachment_id>/delete")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn delete_attachment_post(
|
|
|
|
uuid: String,
|
|
|
|
attachment_id: String,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> EmptyResult {
|
|
|
|
delete_attachment(uuid, attachment_id, headers, conn, nt)
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/ciphers/<uuid>/attachment/<attachment_id>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn delete_attachment(uuid: String, attachment_id: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
|
|
|
_delete_cipher_attachment_by_id(&uuid, &attachment_id, &headers, &conn, &nt)
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 00:48:53 +02:00
|
|
|
#[delete("/ciphers/<uuid>/attachment/<attachment_id>/admin")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn delete_attachment_admin(
|
|
|
|
uuid: String,
|
|
|
|
attachment_id: String,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> EmptyResult {
|
|
|
|
_delete_cipher_attachment_by_id(&uuid, &attachment_id, &headers, &conn, &nt)
|
2018-08-29 00:48:53 +02:00
|
|
|
}
|
|
|
|
|
2018-02-16 00:32:26 +01:00
|
|
|
#[post("/ciphers/<uuid>/delete")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn delete_cipher_post(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
2020-04-17 22:35:27 +02:00
|
|
|
_delete_cipher_by_uuid(&uuid, &headers, &conn, false, &nt)
|
2018-02-15 19:05:57 +01:00
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-07-01 13:43:11 +02:00
|
|
|
#[post("/ciphers/<uuid>/delete-admin")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn delete_cipher_post_admin(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
2020-04-17 22:35:27 +02:00
|
|
|
_delete_cipher_by_uuid(&uuid, &headers, &conn, false, &nt)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/ciphers/<uuid>/delete")]
|
|
|
|
fn delete_cipher_put(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
|
|
|
_delete_cipher_by_uuid(&uuid, &headers, &conn, true, &nt)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/ciphers/<uuid>/delete-admin")]
|
|
|
|
fn delete_cipher_put_admin(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
|
|
|
_delete_cipher_by_uuid(&uuid, &headers, &conn, true, &nt)
|
2018-07-01 13:43:11 +02:00
|
|
|
}
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
#[delete("/ciphers/<uuid>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn delete_cipher(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
2020-04-17 22:35:27 +02:00
|
|
|
_delete_cipher_by_uuid(&uuid, &headers, &conn, false, &nt)
|
2018-02-15 19:05:57 +01:00
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-08-29 00:48:53 +02:00
|
|
|
#[delete("/ciphers/<uuid>/admin")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn delete_cipher_admin(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
2020-04-17 22:35:27 +02:00
|
|
|
_delete_cipher_by_uuid(&uuid, &headers, &conn, false, &nt)
|
2018-08-29 00:48:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 15:53:27 +02:00
|
|
|
#[delete("/ciphers", data = "<data>")]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn delete_cipher_selected(data: JsonUpcase<Value>, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
2020-04-17 22:35:27 +02:00
|
|
|
_delete_multiple_ciphers(data, headers, conn, false, nt)
|
|
|
|
}
|
2018-03-06 00:02:36 +01:00
|
|
|
|
2020-04-17 22:35:27 +02:00
|
|
|
#[post("/ciphers/delete", data = "<data>")]
|
|
|
|
fn delete_cipher_selected_post(data: JsonUpcase<Value>, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
|
|
|
_delete_multiple_ciphers(data, headers, conn, false, nt)
|
|
|
|
}
|
2018-04-19 16:32:11 +02:00
|
|
|
|
2020-04-17 22:35:27 +02:00
|
|
|
#[put("/ciphers/delete", data = "<data>")]
|
|
|
|
fn delete_cipher_selected_put(data: JsonUpcase<Value>, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
2020-10-23 12:42:22 +02:00
|
|
|
_delete_multiple_ciphers(data, headers, conn, true, nt) // soft delete
|
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/ciphers/admin", data = "<data>")]
|
|
|
|
fn delete_cipher_selected_admin(data: JsonUpcase<Value>, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
|
|
|
delete_cipher_selected(data, headers, conn, nt)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/ciphers/delete-admin", data = "<data>")]
|
2021-03-31 22:18:35 +02:00
|
|
|
fn delete_cipher_selected_post_admin(
|
|
|
|
data: JsonUpcase<Value>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> EmptyResult {
|
2020-10-23 12:42:22 +02:00
|
|
|
delete_cipher_selected_post(data, headers, conn, nt)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/ciphers/delete-admin", data = "<data>")]
|
2021-03-31 22:18:35 +02:00
|
|
|
fn delete_cipher_selected_put_admin(
|
|
|
|
data: JsonUpcase<Value>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> EmptyResult {
|
2020-10-23 12:42:22 +02:00
|
|
|
delete_cipher_selected_put(data, headers, conn, nt)
|
2020-04-17 22:35:27 +02:00
|
|
|
}
|
2018-04-19 16:32:11 +02:00
|
|
|
|
2020-04-17 22:35:27 +02:00
|
|
|
#[put("/ciphers/<uuid>/restore")]
|
2021-01-25 06:57:32 +01:00
|
|
|
fn restore_cipher_put(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
2020-04-17 22:35:27 +02:00
|
|
|
_restore_cipher_by_uuid(&uuid, &headers, &conn, &nt)
|
2018-04-19 16:32:11 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:35:27 +02:00
|
|
|
#[put("/ciphers/<uuid>/restore-admin")]
|
2021-01-25 06:57:32 +01:00
|
|
|
fn restore_cipher_put_admin(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
2020-04-17 22:35:27 +02:00
|
|
|
_restore_cipher_by_uuid(&uuid, &headers, &conn, &nt)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/ciphers/restore", data = "<data>")]
|
2021-01-25 06:57:32 +01:00
|
|
|
fn restore_cipher_selected(data: JsonUpcase<Value>, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
|
|
|
_restore_multiple_ciphers(data, &headers, &conn, &nt)
|
2018-08-03 15:53:27 +02:00
|
|
|
}
|
|
|
|
|
2019-01-28 00:39:14 +01:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct MoveCipherData {
|
|
|
|
FolderId: Option<String>,
|
|
|
|
Ids: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2018-08-01 07:51:05 +02:00
|
|
|
#[post("/ciphers/move", data = "<data>")]
|
2019-01-28 00:39:14 +01:00
|
|
|
fn move_cipher_selected(data: JsonUpcase<MoveCipherData>, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
2018-06-01 00:18:50 +02:00
|
|
|
let data = data.into_inner().data;
|
2019-01-28 00:39:14 +01:00
|
|
|
let user_uuid = headers.user.uuid;
|
2018-06-01 00:18:50 +02:00
|
|
|
|
2019-01-28 00:39:14 +01:00
|
|
|
if let Some(ref folder_id) = data.FolderId {
|
|
|
|
match Folder::find_by_uuid(folder_id, &conn) {
|
|
|
|
Some(folder) => {
|
|
|
|
if folder.user_uuid != user_uuid {
|
|
|
|
err!("Folder is not owned by user")
|
2018-04-24 22:01:55 +02:00
|
|
|
}
|
2019-01-28 00:39:14 +01:00
|
|
|
}
|
|
|
|
None => err!("Folder doesn't exist"),
|
|
|
|
}
|
|
|
|
}
|
2018-08-01 07:51:05 +02:00
|
|
|
|
2019-01-28 00:39:14 +01:00
|
|
|
for uuid in data.Ids {
|
2019-02-05 14:52:30 +01:00
|
|
|
let cipher = match Cipher::find_by_uuid(&uuid, &conn) {
|
2018-08-01 07:51:05 +02:00
|
|
|
Some(cipher) => cipher,
|
2018-12-30 23:34:31 +01:00
|
|
|
None => err!("Cipher doesn't exist"),
|
2018-08-01 07:51:05 +02:00
|
|
|
};
|
|
|
|
|
2019-01-28 00:39:14 +01:00
|
|
|
if !cipher.is_accessible_to_user(&user_uuid, &conn) {
|
2018-08-01 07:51:05 +02:00
|
|
|
err!("Cipher is not accessible by user")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move cipher
|
2019-01-28 00:39:14 +01:00
|
|
|
cipher.move_to_folder(data.FolderId.clone(), &user_uuid, &conn)?;
|
2018-12-19 21:52:53 +01:00
|
|
|
|
2019-03-03 16:11:55 +01:00
|
|
|
nt.send_cipher_update(UpdateType::CipherUpdate, &cipher, &[user_uuid.clone()]);
|
2018-08-01 07:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/ciphers/move", data = "<data>")]
|
2019-01-28 00:39:14 +01:00
|
|
|
fn move_cipher_selected_put(
|
|
|
|
data: JsonUpcase<MoveCipherData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> EmptyResult {
|
2018-12-30 23:34:31 +01:00
|
|
|
move_cipher_selected(data, headers, conn, nt)
|
2018-03-06 00:02:36 +01:00
|
|
|
}
|
|
|
|
|
2019-04-14 22:06:08 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct OrganizationId {
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = "organizationId")]
|
2019-04-14 22:06:08 +02:00
|
|
|
org_id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/ciphers/purge?<organization..>", data = "<data>")]
|
2019-04-26 22:08:26 +02:00
|
|
|
fn delete_all(
|
2021-11-07 18:53:39 +01:00
|
|
|
organization: Option<OrganizationId>,
|
2019-04-26 22:08:26 +02:00
|
|
|
data: JsonUpcase<PasswordData>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
nt: Notify,
|
|
|
|
) -> EmptyResult {
|
2018-06-01 00:18:50 +02:00
|
|
|
let data: PasswordData = data.into_inner().data;
|
|
|
|
let password_hash = data.MasterPasswordHash;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2019-01-28 00:39:14 +01:00
|
|
|
let mut user = headers.user;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-02-23 00:38:54 +01:00
|
|
|
if !user.check_valid_password(&password_hash) {
|
2018-02-10 01:00:55 +01:00
|
|
|
err!("Invalid password")
|
|
|
|
}
|
|
|
|
|
2019-04-14 22:06:08 +02:00
|
|
|
match organization {
|
|
|
|
Some(org_data) => {
|
|
|
|
// Organization ID in query params, purging organization vault
|
|
|
|
match UserOrganization::find_by_user_and_org(&user.uuid, &org_data.org_id, &conn) {
|
|
|
|
None => err!("You don't have permission to purge the organization vault"),
|
|
|
|
Some(user_org) => {
|
2019-05-20 21:24:29 +02:00
|
|
|
if user_org.atype == UserOrgType::Owner {
|
2019-04-14 22:06:08 +02:00
|
|
|
Cipher::delete_all_by_organization(&org_data.org_id, &conn)?;
|
|
|
|
nt.send_user_update(UpdateType::Vault, &user);
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
err!("You don't have permission to purge the organization vault");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 22:08:26 +02:00
|
|
|
}
|
2019-04-14 22:06:08 +02:00
|
|
|
None => {
|
|
|
|
// No organization ID in query params, purging user vault
|
|
|
|
// Delete ciphers and their attachments
|
|
|
|
for cipher in Cipher::find_owned_by_user(&user.uuid, &conn) {
|
|
|
|
cipher.delete(&conn)?;
|
|
|
|
}
|
2018-02-15 19:05:57 +01:00
|
|
|
|
2019-04-14 22:06:08 +02:00
|
|
|
// Delete folders
|
|
|
|
for f in Folder::find_by_user(&user.uuid, &conn) {
|
|
|
|
f.delete(&conn)?;
|
|
|
|
}
|
2018-02-15 19:05:57 +01:00
|
|
|
|
2019-04-14 22:06:08 +02:00
|
|
|
user.update_revision(&conn)?;
|
|
|
|
nt.send_user_update(UpdateType::Vault, &user);
|
|
|
|
Ok(())
|
2019-04-26 22:08:26 +02:00
|
|
|
}
|
2019-04-14 22:06:08 +02:00
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
2018-04-19 18:57:17 +02:00
|
|
|
|
2020-04-17 22:35:27 +02:00
|
|
|
fn _delete_cipher_by_uuid(uuid: &str, headers: &Headers, conn: &DbConn, soft_delete: bool, nt: &Notify) -> EmptyResult {
|
2021-06-19 22:02:03 +02:00
|
|
|
let mut cipher = match Cipher::find_by_uuid(uuid, conn) {
|
2018-04-19 18:57:17 +02:00
|
|
|
Some(cipher) => cipher,
|
|
|
|
None => err!("Cipher doesn't exist"),
|
|
|
|
};
|
|
|
|
|
2021-06-19 22:02:03 +02:00
|
|
|
if !cipher.is_write_accessible_to_user(&headers.user.uuid, conn) {
|
2018-04-30 11:52:15 +02:00
|
|
|
err!("Cipher can't be deleted by user")
|
2018-04-19 18:57:17 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:35:27 +02:00
|
|
|
if soft_delete {
|
2020-12-06 01:22:20 +01:00
|
|
|
cipher.deleted_at = Some(Utc::now().naive_utc());
|
2021-06-19 22:02:03 +02:00
|
|
|
cipher.save(conn)?;
|
|
|
|
nt.send_cipher_update(UpdateType::CipherUpdate, &cipher, &cipher.update_users_revision(conn));
|
2020-04-17 22:35:27 +02:00
|
|
|
} else {
|
2021-06-19 22:02:03 +02:00
|
|
|
cipher.delete(conn)?;
|
|
|
|
nt.send_cipher_update(UpdateType::CipherDelete, &cipher, &cipher.update_users_revision(conn));
|
2020-04-17 22:35:27 +02:00
|
|
|
}
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
Ok(())
|
2018-04-19 18:57:17 +02:00
|
|
|
}
|
2018-07-14 08:09:20 +02:00
|
|
|
|
2021-03-31 22:18:35 +02:00
|
|
|
fn _delete_multiple_ciphers(
|
|
|
|
data: JsonUpcase<Value>,
|
|
|
|
headers: Headers,
|
|
|
|
conn: DbConn,
|
|
|
|
soft_delete: bool,
|
|
|
|
nt: Notify,
|
|
|
|
) -> EmptyResult {
|
2020-04-17 22:35:27 +02:00
|
|
|
let data: Value = data.into_inner().data;
|
|
|
|
|
|
|
|
let uuids = match data.get("Ids") {
|
|
|
|
Some(ids) => match ids.as_array() {
|
|
|
|
Some(ids) => ids.iter().filter_map(Value::as_str),
|
|
|
|
None => err!("Posted ids field is not an array"),
|
|
|
|
},
|
|
|
|
None => err!("Request missing ids field"),
|
|
|
|
};
|
|
|
|
|
|
|
|
for uuid in uuids {
|
|
|
|
if let error @ Err(_) = _delete_cipher_by_uuid(uuid, &headers, &conn, soft_delete, &nt) {
|
|
|
|
return error;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-25 06:57:32 +01:00
|
|
|
fn _restore_cipher_by_uuid(uuid: &str, headers: &Headers, conn: &DbConn, nt: &Notify) -> JsonResult {
|
2021-06-19 22:02:03 +02:00
|
|
|
let mut cipher = match Cipher::find_by_uuid(uuid, conn) {
|
2020-04-17 22:35:27 +02:00
|
|
|
Some(cipher) => cipher,
|
|
|
|
None => err!("Cipher doesn't exist"),
|
|
|
|
};
|
|
|
|
|
2021-06-19 22:02:03 +02:00
|
|
|
if !cipher.is_write_accessible_to_user(&headers.user.uuid, conn) {
|
2020-04-17 22:35:27 +02:00
|
|
|
err!("Cipher can't be restored by user")
|
|
|
|
}
|
|
|
|
|
|
|
|
cipher.deleted_at = None;
|
2021-06-19 22:02:03 +02:00
|
|
|
cipher.save(conn)?;
|
2020-04-17 22:35:27 +02:00
|
|
|
|
2021-06-19 22:02:03 +02:00
|
|
|
nt.send_cipher_update(UpdateType::CipherUpdate, &cipher, &cipher.update_users_revision(conn));
|
|
|
|
Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, conn)))
|
2020-04-17 22:35:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-25 06:57:32 +01:00
|
|
|
fn _restore_multiple_ciphers(data: JsonUpcase<Value>, headers: &Headers, conn: &DbConn, nt: &Notify) -> JsonResult {
|
2020-04-17 22:35:27 +02:00
|
|
|
let data: Value = data.into_inner().data;
|
|
|
|
|
|
|
|
let uuids = match data.get("Ids") {
|
|
|
|
Some(ids) => match ids.as_array() {
|
|
|
|
Some(ids) => ids.iter().filter_map(Value::as_str),
|
|
|
|
None => err!("Posted ids field is not an array"),
|
|
|
|
},
|
|
|
|
None => err!("Request missing ids field"),
|
|
|
|
};
|
|
|
|
|
2021-01-25 06:57:32 +01:00
|
|
|
let mut ciphers: Vec<Value> = Vec::new();
|
2020-04-17 22:35:27 +02:00
|
|
|
for uuid in uuids {
|
2021-01-25 06:57:32 +01:00
|
|
|
match _restore_cipher_by_uuid(uuid, headers, conn, nt) {
|
|
|
|
Ok(json) => ciphers.push(json.into_inner()),
|
2021-03-31 22:18:35 +02:00
|
|
|
err => return err,
|
2021-01-25 06:57:32 +01:00
|
|
|
}
|
2020-04-17 22:35:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-25 06:57:32 +01:00
|
|
|
Ok(Json(json!({
|
|
|
|
"Data": ciphers,
|
|
|
|
"Object": "list",
|
|
|
|
"ContinuationToken": null
|
|
|
|
})))
|
2020-04-17 22:35:27 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
fn _delete_cipher_attachment_by_id(
|
|
|
|
uuid: &str,
|
|
|
|
attachment_id: &str,
|
|
|
|
headers: &Headers,
|
|
|
|
conn: &DbConn,
|
|
|
|
nt: &Notify,
|
|
|
|
) -> EmptyResult {
|
2021-06-19 22:02:03 +02:00
|
|
|
let attachment = match Attachment::find_by_id(attachment_id, conn) {
|
2018-07-14 08:09:20 +02:00
|
|
|
Some(attachment) => attachment,
|
2018-12-30 23:34:31 +01:00
|
|
|
None => err!("Attachment doesn't exist"),
|
2018-07-14 08:09:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if attachment.cipher_uuid != uuid {
|
|
|
|
err!("Attachment from other cipher")
|
|
|
|
}
|
|
|
|
|
2021-06-19 22:02:03 +02:00
|
|
|
let cipher = match Cipher::find_by_uuid(uuid, conn) {
|
2018-07-18 22:35:45 +02:00
|
|
|
Some(cipher) => cipher,
|
2018-12-30 23:34:31 +01:00
|
|
|
None => err!("Cipher doesn't exist"),
|
2018-07-18 22:35:45 +02:00
|
|
|
};
|
|
|
|
|
2021-06-19 22:02:03 +02:00
|
|
|
if !cipher.is_write_accessible_to_user(&headers.user.uuid, conn) {
|
2018-07-18 22:35:45 +02:00
|
|
|
err!("Cipher cannot be deleted by user")
|
|
|
|
}
|
|
|
|
|
2018-07-14 08:09:20 +02:00
|
|
|
// Delete attachment
|
2021-06-19 22:02:03 +02:00
|
|
|
attachment.delete(conn)?;
|
|
|
|
nt.send_cipher_update(UpdateType::CipherUpdate, &cipher, &cipher.update_users_revision(conn));
|
2018-12-19 21:52:53 +01:00
|
|
|
Ok(())
|
2018-07-14 08:09:20 +02:00
|
|
|
}
|