| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | use std::{path::PathBuf, time::SystemTime}; |
| 16 | |
| 17 | use topcoat::{ |
| 18 | Result, |
| 19 | context::{Cx, app_context}, |
| 20 | router::{ |
| 21 | Response, |
| 22 | error::not_found, |
| 23 | header::{CACHE_CONTROL, CONTENT_DISPOSITION, CONTENT_LENGTH, CONTENT_TYPE}, |
| 24 | route, |
| 25 | }, |
| 26 | }; |
| 27 | |
| 28 | use crate::{ |
| 29 | application::{AppConfig, Error, archive_name, back_up_for}, |
| 30 | domain::DomainError, |
| 31 | infrastructure::backup::{SqliteDatabaseSnapshot, TarGzBackupArchive, database_file}, |
| 32 | }; |
| 33 | |
| 34 | use super::{ |
| 35 | context::{current_actor, instance_owner, orgs, repos, server_error, users}, |
| 36 | git::{GitBody, too_many_requests}, |
| 37 | profile::profile_for, |
| 38 | rate_limit::backup_throttled, |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | const WORKSPACE: &str = ".backup"; |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | #[route(GET "/{handle}/settings/backup.tar.gz")] |
| 64 | async fn backup_download(cx: &Cx) -> Result<Response<GitBody>> { |
| 65 | |
| 66 | |
| 67 | |
| 68 | let profile = profile_for(cx).await?; |
| 69 | let actor = current_actor(cx).await?; |
| 70 | |
| 71 | if !profile.viewer_is_owner || !instance_owner(cx).await? { |
| 72 | return Err(not_found().into()); |
| 73 | } |
| 74 | |
| 75 | if let Some(retry_after) = backup_throttled(cx) { |
| 76 | return too_many_requests(retry_after); |
| 77 | } |
| 78 | |
| 79 | let config = app_context::<AppConfig>(cx); |
| 80 | let database = database_file(&config.database_url).ok_or_else(|| { |
| 81 | server_error(std::io::Error::other( |
| 82 | "STEID_DATABASE_URL names no file, so there is nothing to back up", |
| 83 | )) |
| 84 | })?; |
| 85 | |
| 86 | let root = config.data_dir.join(WORKSPACE); |
| 87 | tokio::fs::create_dir_all(&root) |
| 88 | .await |
| 89 | .map_err(server_error)?; |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | let workspace = tempfile::TempDir::new_in(&root).map_err(server_error)?; |
| 95 | |
| 96 | let now = SystemTime::now(); |
| 97 | let filename = archive_name(now); |
| 98 | let destination: PathBuf = workspace.path().join(&filename); |
| 99 | |
| 100 | let written = back_up_for( |
| 101 | &actor, |
| 102 | now, |
| 103 | &destination, |
| 104 | &config.data_dir, |
| 105 | workspace.path(), |
| 106 | &users(cx), |
| 107 | &orgs(cx), |
| 108 | &repos(cx), |
| 109 | &SqliteDatabaseSnapshot::new(super::context::pool(cx).clone(), database), |
| 110 | &TarGzBackupArchive::new(), |
| 111 | ) |
| 112 | .await |
| 113 | .map_err(|error| match error { |
| 114 | |
| 115 | |
| 116 | Error::Domain(DomainError::Forbidden) => not_found().into(), |
| 117 | other => server_error(other), |
| 118 | })?; |
| 119 | |
| 120 | let file = tokio::fs::File::open(&destination) |
| 121 | .await |
| 122 | .map_err(server_error)?; |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | let _ = tokio::fs::remove_file(&destination).await; |
| 128 | drop(workspace); |
| 129 | |
| 130 | Response::builder() |
| 131 | .header(CONTENT_TYPE, "application/gzip") |
| 132 | .header(NOSNIFF.0, NOSNIFF.1) |
| 133 | .header(CONTENT_DISPOSITION, disposition(&filename)) |
| 134 | .header(CONTENT_LENGTH, written.bytes) |
| 135 | |
| 136 | |
| 137 | .header(CACHE_CONTROL, "no-store, private") |
| 138 | .body(GitBody::new(Box::pin(file))) |
| 139 | .map_err(server_error) |
| 140 | } |
| 141 | |
| 142 | |
| 143 | const NOSNIFF: (&str, &str) = ("x-content-type-options", "nosniff"); |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | fn disposition(filename: &str) -> String { |
| 152 | let safe: String = filename |
| 153 | .chars() |
| 154 | .map(|char| match char { |
| 155 | 'A'..='Z' | 'a'..='z' | '0'..='9' | '.' | '-' | '_' => char, |
| 156 | _ => '_', |
| 157 | }) |
| 158 | .collect(); |
| 159 | |
| 160 | format!("attachment; filename=\"{safe}\"") |
| 161 | } |
| 162 | |
| 163 | |
| 164 | pub(super) fn backup_url(handle: &str) -> String { |
| 165 | format!("/{handle}/settings/backup.tar.gz") |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | pub(super) fn last_scheduled_backup(cx: &Cx) -> Option<SystemTime> { |
| 175 | let marker = app_context::<AppConfig>(cx).backup_marker.as_ref()?; |
| 176 | |
| 177 | std::fs::metadata(marker) |
| 178 | .ok() |
| 179 | .and_then(|metadata| metadata.modified().ok()) |
| 180 | } |
| 181 | |
| 182 | #[cfg(test)] |
| 183 | mod tests { |
| 184 | use super::*; |
| 185 | |
| 186 | #[test] |
| 187 | fn a_backup_url_hangs_off_the_settings_page() { |
| 188 | assert_eq!(backup_url("james"), "/james/settings/backup.tar.gz"); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn a_filename_is_reduced_to_what_a_header_can_carry() { |
| 193 | assert_eq!( |
| 194 | disposition("steid-0.4.0-20260908T142530Z.tar.gz"), |
| 195 | "attachment; filename=\"steid-0.4.0-20260908T142530Z.tar.gz\"" |
| 196 | ); |
| 197 | assert_eq!( |
| 198 | disposition("a\"b\r\nc.gz"), |
| 199 | "attachment; filename=\"a_b__c.gz\"" |
| 200 | ); |
| 201 | } |
| 202 | } |