web.rs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. use std::path::{Path, PathBuf};
  2. use rocket::{
  3. fs::NamedFile,
  4. http::ContentType,
  5. response::{content::RawCss as Css, content::RawHtml as Html, Redirect},
  6. serde::json::Json,
  7. Catcher, Route,
  8. };
  9. use serde_json::Value;
  10. use crate::{
  11. api::{core::now, ApiResult, EmptyResult},
  12. auth::decode_file_download,
  13. error::Error,
  14. util::{Cached, SafeString},
  15. CONFIG,
  16. };
  17. pub fn routes() -> Vec<Route> {
  18. // If adding more routes here, consider also adding them to
  19. // crate::utils::LOGGED_ROUTES to make sure they appear in the log
  20. let mut routes = routes![attachments, alive, alive_head, static_files];
  21. if CONFIG.web_vault_enabled() {
  22. routes.append(&mut routes![web_index, web_index_direct, web_index_head, app_id, web_files, vaultwarden_css]);
  23. }
  24. #[cfg(debug_assertions)]
  25. if CONFIG.reload_templates() {
  26. routes.append(&mut routes![_static_files_dev]);
  27. }
  28. routes
  29. }
  30. pub fn catchers() -> Vec<Catcher> {
  31. if CONFIG.web_vault_enabled() {
  32. catchers![not_found]
  33. } else {
  34. catchers![]
  35. }
  36. }
  37. #[catch(404)]
  38. fn not_found() -> ApiResult<Html<String>> {
  39. // Return the page
  40. let json = json!({
  41. "urlpath": CONFIG.domain_path()
  42. });
  43. let text = CONFIG.render_template("404", &json)?;
  44. Ok(Html(text))
  45. }
  46. #[get("/css/vaultwarden.css")]
  47. fn vaultwarden_css() -> Cached<Css<String>> {
  48. let css_options = json!({
  49. "signup_disabled": !CONFIG.signups_allowed() && CONFIG.signups_domains_whitelist().is_empty(),
  50. "mail_enabled": CONFIG.mail_enabled(),
  51. "yubico_enabled": CONFIG._enable_yubico() && (CONFIG.yubico_client_id().is_some() == CONFIG.yubico_secret_key().is_some()),
  52. "emergency_access_allowed": CONFIG.emergency_access_allowed(),
  53. "sends_allowed": CONFIG.sends_allowed(),
  54. "load_user_scss": true,
  55. });
  56. let scss = match CONFIG.render_template("scss/vaultwarden.scss", &css_options) {
  57. Ok(t) => t,
  58. Err(e) => {
  59. // Something went wrong loading the template. Use the fallback
  60. warn!("Loading scss/vaultwarden.scss.hbs or scss/user.vaultwarden.scss.hbs failed. {e}");
  61. CONFIG
  62. .render_fallback_template("scss/vaultwarden.scss", &css_options)
  63. .expect("Fallback scss/vaultwarden.scss.hbs to render")
  64. }
  65. };
  66. let css = match grass_compiler::from_string(
  67. scss,
  68. &grass_compiler::Options::default().style(grass_compiler::OutputStyle::Compressed),
  69. ) {
  70. Ok(css) => css,
  71. Err(e) => {
  72. // Something went wrong compiling the scss. Use the fallback
  73. warn!("Compiling the Vaultwarden SCSS styles failed. {e}");
  74. let mut css_options = css_options;
  75. css_options["load_user_scss"] = json!(false);
  76. let scss = CONFIG
  77. .render_fallback_template("scss/vaultwarden.scss", &css_options)
  78. .expect("Fallback scss/vaultwarden.scss.hbs to render");
  79. grass_compiler::from_string(
  80. scss,
  81. &grass_compiler::Options::default().style(grass_compiler::OutputStyle::Compressed),
  82. )
  83. .expect("SCSS to compile")
  84. }
  85. };
  86. // Cache for one day should be enough and not too much
  87. Cached::ttl(Css(css), 86_400, false)
  88. }
  89. #[get("/")]
  90. async fn web_index() -> Cached<Option<NamedFile>> {
  91. Cached::short(NamedFile::open(Path::new(&CONFIG.web_vault_folder()).join("index.html")).await.ok(), false)
  92. }
  93. // Make sure that `/index.html` redirect to actual domain path.
  94. // If not, this might cause issues with the web-vault
  95. #[get("/index.html")]
  96. fn web_index_direct() -> Redirect {
  97. Redirect::to(format!("{}/", CONFIG.domain_path()))
  98. }
  99. #[head("/")]
  100. fn web_index_head() -> EmptyResult {
  101. // Add an explicit HEAD route to prevent uptime monitoring services from
  102. // generating "No matching routes for HEAD /" error messages.
  103. //
  104. // Rocket automatically implements a HEAD route when there's a matching GET
  105. // route, but relying on this behavior also means a spurious error gets
  106. // logged due to <https://github.com/SergioBenitez/Rocket/issues/1098>.
  107. Ok(())
  108. }
  109. #[get("/app-id.json")]
  110. fn app_id() -> Cached<(ContentType, Json<Value>)> {
  111. let content_type = ContentType::new("application", "fido.trusted-apps+json");
  112. Cached::long(
  113. (
  114. content_type,
  115. Json(json!({
  116. "trustedFacets": [
  117. {
  118. "version": { "major": 1, "minor": 0 },
  119. "ids": [
  120. // Per <https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-appid-and-facets-v2.0-id-20180227.html#determining-the-facetid-of-a-calling-application>:
  121. //
  122. // "In the Web case, the FacetID MUST be the Web Origin [RFC6454]
  123. // of the web page triggering the FIDO operation, written as
  124. // a URI with an empty path. Default ports are omitted and any
  125. // path component is ignored."
  126. //
  127. // This leaves it unclear as to whether the path must be empty,
  128. // or whether it can be non-empty and will be ignored. To be on
  129. // the safe side, use a proper web origin (with empty path).
  130. &CONFIG.domain_origin(),
  131. "ios:bundle-id:com.8bit.bitwarden",
  132. "android:apk-key-hash:dUGFzUzf3lmHSLBDBIv+WaFyZMI" ]
  133. }]
  134. })),
  135. ),
  136. true,
  137. )
  138. }
  139. #[get("/<p..>", rank = 10)] // Only match this if the other routes don't match
  140. async fn web_files(p: PathBuf) -> Cached<Option<NamedFile>> {
  141. Cached::long(NamedFile::open(Path::new(&CONFIG.web_vault_folder()).join(p)).await.ok(), true)
  142. }
  143. #[get("/attachments/<uuid>/<file_id>?<token>")]
  144. async fn attachments(uuid: SafeString, file_id: SafeString, token: String) -> Option<NamedFile> {
  145. let Ok(claims) = decode_file_download(&token) else {
  146. return None;
  147. };
  148. if claims.sub != *uuid || claims.file_id != *file_id {
  149. return None;
  150. }
  151. NamedFile::open(Path::new(&CONFIG.attachments_folder()).join(uuid).join(file_id)).await.ok()
  152. }
  153. // We use DbConn here to let the alive healthcheck also verify the database connection.
  154. use crate::db::DbConn;
  155. #[get("/alive")]
  156. fn alive(_conn: DbConn) -> Json<String> {
  157. now()
  158. }
  159. #[head("/alive")]
  160. fn alive_head(_conn: DbConn) -> EmptyResult {
  161. // Avoid logging spurious "No matching routes for HEAD /alive" errors
  162. // due to <https://github.com/SergioBenitez/Rocket/issues/1098>.
  163. Ok(())
  164. }
  165. // This endpoint/function is used during development and development only.
  166. // It allows to easily develop the admin interface by always loading the files from disk instead from a slice of bytes
  167. // This will only be active during a debug build and only when `RELOAD_TEMPLATES` is set to `true`
  168. // NOTE: Do not forget to add any new files added to the `static_files` function below!
  169. #[cfg(debug_assertions)]
  170. #[get("/vw_static/<filename>", rank = 1)]
  171. pub async fn _static_files_dev(filename: PathBuf) -> Option<NamedFile> {
  172. warn!("LOADING STATIC FILES FROM DISK");
  173. let file = filename.to_str().unwrap_or_default();
  174. let ext = filename.extension().unwrap_or_default();
  175. let path = if ext == "png" || ext == "svg" {
  176. tokio::fs::canonicalize(Path::new(file!()).parent().unwrap().join("../static/images/").join(file)).await
  177. } else {
  178. tokio::fs::canonicalize(Path::new(file!()).parent().unwrap().join("../static/scripts/").join(file)).await
  179. };
  180. if let Ok(path) = path {
  181. return NamedFile::open(path).await.ok();
  182. };
  183. None
  184. }
  185. #[get("/vw_static/<filename>", rank = 2)]
  186. pub fn static_files(filename: &str) -> Result<(ContentType, &'static [u8]), Error> {
  187. match filename {
  188. "404.png" => Ok((ContentType::PNG, include_bytes!("../static/images/404.png"))),
  189. "mail-github.png" => Ok((ContentType::PNG, include_bytes!("../static/images/mail-github.png"))),
  190. "logo-gray.png" => Ok((ContentType::PNG, include_bytes!("../static/images/logo-gray.png"))),
  191. "error-x.svg" => Ok((ContentType::SVG, include_bytes!("../static/images/error-x.svg"))),
  192. "hibp.png" => Ok((ContentType::PNG, include_bytes!("../static/images/hibp.png"))),
  193. "vaultwarden-icon.png" => Ok((ContentType::PNG, include_bytes!("../static/images/vaultwarden-icon.png"))),
  194. "vaultwarden-favicon.png" => Ok((ContentType::PNG, include_bytes!("../static/images/vaultwarden-favicon.png"))),
  195. "404.css" => Ok((ContentType::CSS, include_bytes!("../static/scripts/404.css"))),
  196. "admin.css" => Ok((ContentType::CSS, include_bytes!("../static/scripts/admin.css"))),
  197. "admin.js" => Ok((ContentType::JavaScript, include_bytes!("../static/scripts/admin.js"))),
  198. "admin_settings.js" => Ok((ContentType::JavaScript, include_bytes!("../static/scripts/admin_settings.js"))),
  199. "admin_users.js" => Ok((ContentType::JavaScript, include_bytes!("../static/scripts/admin_users.js"))),
  200. "admin_organizations.js" => {
  201. Ok((ContentType::JavaScript, include_bytes!("../static/scripts/admin_organizations.js")))
  202. }
  203. "admin_diagnostics.js" => {
  204. Ok((ContentType::JavaScript, include_bytes!("../static/scripts/admin_diagnostics.js")))
  205. }
  206. "bootstrap.css" => Ok((ContentType::CSS, include_bytes!("../static/scripts/bootstrap.css"))),
  207. "bootstrap.bundle.js" => Ok((ContentType::JavaScript, include_bytes!("../static/scripts/bootstrap.bundle.js"))),
  208. "jdenticon-3.3.0.js" => Ok((ContentType::JavaScript, include_bytes!("../static/scripts/jdenticon-3.3.0.js"))),
  209. "datatables.js" => Ok((ContentType::JavaScript, include_bytes!("../static/scripts/datatables.js"))),
  210. "datatables.css" => Ok((ContentType::CSS, include_bytes!("../static/scripts/datatables.css"))),
  211. "jquery-3.7.1.slim.js" => {
  212. Ok((ContentType::JavaScript, include_bytes!("../static/scripts/jquery-3.7.1.slim.js")))
  213. }
  214. _ => err!(format!("Static file not found: {filename}")),
  215. }
  216. }