| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
pub const DEFAULT_LOG_FILTER: &str = |
| 17 |
"makenotwork=debug,tower_http=debug,sqlx=info,csp_violation=warn"; |
| 18 |
|
| 19 |
pub mod access_gate; |
| 20 |
pub mod auth; |
| 21 |
pub mod background; |
| 22 |
pub mod build_runner; |
| 23 |
pub mod changelog; |
| 24 |
pub mod cloudflare; |
| 25 |
pub mod config; |
| 26 |
pub mod constants; |
| 27 |
pub mod crypto; |
| 28 |
pub mod csrf; |
| 29 |
pub mod currency; |
| 30 |
pub mod custom_pages; |
| 31 |
pub mod db; |
| 32 |
pub mod email; |
| 33 |
pub mod error; |
| 34 |
pub mod extractors; |
| 35 |
pub mod fee_calculator; |
| 36 |
pub mod formatting; |
| 37 |
pub mod fragment_redirect; |
| 38 |
pub mod git; |
| 39 |
pub mod git_ssh; |
| 40 |
pub mod helpers; |
| 41 |
pub mod import; |
| 42 |
pub mod license_templates; |
| 43 |
pub mod markdown; |
| 44 |
pub mod metrics; |
| 45 |
pub mod monitor; |
| 46 |
pub mod mt_client; |
| 47 |
pub mod oauth_scope; |
| 48 |
pub mod openapi; |
| 49 |
pub mod payments; |
| 50 |
pub mod pricing; |
| 51 |
pub mod rate_limit; |
| 52 |
pub mod routes; |
| 53 |
pub mod rss; |
| 54 |
pub mod scanning; |
| 55 |
pub mod scheduler; |
| 56 |
pub mod security_signals; |
| 57 |
pub mod seed; |
| 58 |
pub mod site_docs; |
| 59 |
pub mod storage; |
| 60 |
pub mod synckit_auth; |
| 61 |
pub mod synckit_billing; |
| 62 |
pub mod templates; |
| 63 |
pub mod theming; |
| 64 |
pub mod tier_prices; |
| 65 |
pub mod types; |
| 66 |
pub mod validation; |
| 67 |
pub mod wam_client; |
| 68 |
pub mod wordlist; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#[cfg(test)] |
| 73 |
mod deploy_lint; |
| 74 |
|
| 75 |
use axum::{Router, extract::FromRef, http::HeaderValue, middleware}; |
| 76 |
use std::time::Instant; |
| 77 |
use tower_http::limit::RequestBodyLimitLayer; |
| 78 |
use tower_http::services::ServeDir; |
| 79 |
use tower_http::set_header::SetResponseHeaderLayer; |
| 80 |
use tower_sessions::SessionManagerLayer; |
| 81 |
use tower_sessions_sqlx_store::PostgresStore; |
| 82 |
|
| 83 |
use std::sync::Arc; |
| 84 |
|
| 85 |
use dashmap::DashMap; |
| 86 |
use db::{SyncAppId, UserId, UserSessionId}; |
| 87 |
|
| 88 |
use config::Config; |
| 89 |
use docengine::DocLoader; |
| 90 |
use email::EmailClient; |
| 91 |
use payments::PaymentProvider; |
| 92 |
use routes::{ |
| 93 |
admin_routes, api_routes, auth_routes, build_routes, git_issue_routes, git_routes, |
| 94 |
git_write_routes, oauth_routes, ota_routes, page_routes, postmark_routes, sso_routes, |
| 95 |
storage_routes, stripe_routes, synckit_routes, |
| 96 |
}; |
| 97 |
use scanning::ScanPipeline; |
| 98 |
use storage::StorageBackend; |
| 99 |
use webauthn_rs::Webauthn; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#[derive(Clone, FromRef)] |
| 110 |
pub struct AppState { |
| 111 |
pub db: sqlx::PgPool, |
| 112 |
pub config: Config, |
| 113 |
|
| 114 |
|
| 115 |
pub storage: AppStorage, |
| 116 |
pub stripe: Option<Arc<dyn PaymentProvider>>, |
| 117 |
pub email: EmailClient, |
| 118 |
pub docs: Arc<DocLoader>, |
| 119 |
pub tier_prices: tier_prices::TierPrices, |
| 120 |
pub runway_config: tier_prices::RunwayConfig, |
| 121 |
|
| 122 |
|
| 123 |
pub fee_calculator: fee_calculator::FeeCalculator, |
| 124 |
pub scanner: Option<Arc<ScanPipeline>>, |
| 125 |
pub webauthn: Arc<Webauthn>, |
| 126 |
pub syntax: Option<Arc<git::SyntaxHighlighter>>, |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
#[from_ref(skip)] |
| 131 |
pub started_at: chrono::DateTime<chrono::Utc>, |
| 132 |
#[from_ref(skip)] |
| 133 |
pub start_instant: Instant, |
| 134 |
|
| 135 |
pub mt_client: Option<mt_client::MtClient>, |
| 136 |
|
| 137 |
pub wam: Option<wam_client::WamClient>, |
| 138 |
|
| 139 |
pub caches: AppCaches, |
| 140 |
|
| 141 |
pub limiters: AppLimiters, |
| 142 |
|
| 143 |
|
| 144 |
pub restart_at: Arc<std::sync::atomic::AtomicI64>, |
| 145 |
|
| 146 |
pub metrics_handle: Option<metrics_exporter_prometheus::PrometheusHandle>, |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
pub page_view_tx: db::page_views::PageViewTx, |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
pub bg: background::BackgroundTx, |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
#[derive(Clone)] |
| 160 |
pub struct AppStorage { |
| 161 |
|
| 162 |
pub s3: Option<Arc<dyn StorageBackend>>, |
| 163 |
|
| 164 |
pub synckit_s3: Option<Arc<dyn StorageBackend>>, |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
pub public_s3: Option<Arc<dyn StorageBackend>>, |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
#[derive(Clone)] |
| 174 |
pub struct AppCaches { |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
pub session_cache: Arc<DashMap<UserSessionId, Instant>>, |
| 179 |
|
| 180 |
|
| 181 |
pub domain_cache: Arc<DashMap<String, db::UserId>>, |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
pub sync_notify: Arc<DashMap<(SyncAppId, UserId), tokio::sync::broadcast::Sender<i64>>>, |
| 186 |
|
| 187 |
pub sse_connections: Arc<DashMap<UserId, std::sync::atomic::AtomicUsize>>, |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
pub notes_cache: Arc<git::notes::NotesCache>, |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
#[derive(Clone)] |
| 198 |
pub struct AppLimiters { |
| 199 |
|
| 200 |
|
| 201 |
pub scan_semaphore: Arc<tokio::sync::Semaphore>, |
| 202 |
|
| 203 |
|
| 204 |
pub caddy_ask_semaphore: Arc<tokio::sync::Semaphore>, |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
pub git_smart_http_semaphore: Arc<tokio::sync::Semaphore>, |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
#[derive(Clone)] |
| 232 |
pub struct Billing { |
| 233 |
pub stripe: Option<Arc<dyn PaymentProvider>>, |
| 234 |
pub tier_prices: tier_prices::TierPrices, |
| 235 |
pub runway_config: tier_prices::RunwayConfig, |
| 236 |
pub fee_calculator: fee_calculator::FeeCalculator, |
| 237 |
} |
| 238 |
|
| 239 |
impl FromRef<AppState> for Billing { |
| 240 |
fn from_ref(s: &AppState) -> Self { |
| 241 |
Self { |
| 242 |
stripe: s.stripe.clone(), |
| 243 |
tier_prices: s.tier_prices.clone(), |
| 244 |
runway_config: s.runway_config.clone(), |
| 245 |
fee_calculator: s.fee_calculator.clone(), |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
#[derive(Clone)] |
| 252 |
pub struct Integrations { |
| 253 |
pub mt_client: Option<mt_client::MtClient>, |
| 254 |
pub wam: Option<wam_client::WamClient>, |
| 255 |
} |
| 256 |
|
| 257 |
impl FromRef<AppState> for Integrations { |
| 258 |
fn from_ref(s: &AppState) -> Self { |
| 259 |
Self { |
| 260 |
mt_client: s.mt_client.clone(), |
| 261 |
wam: s.wam.clone(), |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
#[derive(Clone)] |
| 275 |
pub struct Scanning { |
| 276 |
pub scanner: Option<Arc<ScanPipeline>>, |
| 277 |
} |
| 278 |
|
| 279 |
impl FromRef<AppState> for Scanning { |
| 280 |
fn from_ref(s: &AppState) -> Self { |
| 281 |
Self { |
| 282 |
scanner: s.scanner.clone(), |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
#[derive(Clone)] |
| 290 |
pub struct Sync { |
| 291 |
pub synckit_s3: Option<Arc<dyn StorageBackend>>, |
| 292 |
pub sync_notify: Arc<DashMap<(SyncAppId, UserId), tokio::sync::broadcast::Sender<i64>>>, |
| 293 |
pub sse_connections: Arc<DashMap<UserId, std::sync::atomic::AtomicUsize>>, |
| 294 |
} |
| 295 |
|
| 296 |
impl FromRef<AppState> for Sync { |
| 297 |
fn from_ref(s: &AppState) -> Self { |
| 298 |
Self { |
| 299 |
synckit_s3: s.storage.synckit_s3.clone(), |
| 300 |
sync_notify: s.caches.sync_notify.clone(), |
| 301 |
sse_connections: s.caches.sse_connections.clone(), |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
const SYNC_NOTIFY_CHANNEL_DEPTH: usize = 16; |
| 311 |
|
| 312 |
impl Sync { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
pub fn notify_push(&self, app_id: SyncAppId, user_id: UserId, cursor: i64) { |
| 317 |
if let Some(sender) = self.sync_notify.get(&(app_id, user_id)) { |
| 318 |
let _ = sender.send(cursor); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
pub fn subscribe_channel( |
| 326 |
&self, |
| 327 |
app_id: SyncAppId, |
| 328 |
user_id: UserId, |
| 329 |
) -> tokio::sync::broadcast::Receiver<i64> { |
| 330 |
self.sync_notify |
| 331 |
.entry((app_id, user_id)) |
| 332 |
.or_insert_with(|| tokio::sync::broadcast::channel(SYNC_NOTIFY_CHANNEL_DEPTH).0) |
| 333 |
.value() |
| 334 |
.subscribe() |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
#[derive(Clone)] |
| 343 |
pub struct Git { |
| 344 |
pub syntax: Option<Arc<git::SyntaxHighlighter>>, |
| 345 |
pub smart_http_semaphore: Arc<tokio::sync::Semaphore>, |
| 346 |
pub notes_cache: Arc<git::notes::NotesCache>, |
| 347 |
} |
| 348 |
|
| 349 |
impl FromRef<AppState> for Git { |
| 350 |
fn from_ref(s: &AppState) -> Self { |
| 351 |
Self { |
| 352 |
syntax: s.syntax.clone(), |
| 353 |
smart_http_semaphore: s.limiters.git_smart_http_semaphore.clone(), |
| 354 |
notes_cache: Arc::clone(&s.caches.notes_cache), |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
#[derive(Clone)] |
| 364 |
pub struct Ops { |
| 365 |
pub started_at: chrono::DateTime<chrono::Utc>, |
| 366 |
pub start_instant: Instant, |
| 367 |
pub restart_at: Arc<std::sync::atomic::AtomicI64>, |
| 368 |
pub metrics_handle: Option<metrics_exporter_prometheus::PrometheusHandle>, |
| 369 |
} |
| 370 |
|
| 371 |
impl FromRef<AppState> for Ops { |
| 372 |
fn from_ref(s: &AppState) -> Self { |
| 373 |
Self { |
| 374 |
started_at: s.started_at, |
| 375 |
start_instant: s.start_instant, |
| 376 |
restart_at: s.restart_at.clone(), |
| 377 |
metrics_handle: s.metrics_handle.clone(), |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
pub struct AppStateParts { |
| 390 |
pub db: sqlx::PgPool, |
| 391 |
pub config: Config, |
| 392 |
pub storage: AppStorage, |
| 393 |
pub stripe: Option<Arc<dyn PaymentProvider>>, |
| 394 |
pub email: EmailClient, |
| 395 |
pub docs: Arc<DocLoader>, |
| 396 |
pub tier_prices: tier_prices::TierPrices, |
| 397 |
pub runway_config: tier_prices::RunwayConfig, |
| 398 |
pub fee_calculator: fee_calculator::FeeCalculator, |
| 399 |
pub scanner: Option<Arc<ScanPipeline>>, |
| 400 |
pub webauthn: Arc<Webauthn>, |
| 401 |
pub syntax: Option<Arc<git::SyntaxHighlighter>>, |
| 402 |
pub mt_client: Option<mt_client::MtClient>, |
| 403 |
pub wam: Option<wam_client::WamClient>, |
| 404 |
|
| 405 |
pub domain_cache: Arc<DashMap<String, db::UserId>>, |
| 406 |
pub metrics_handle: Option<metrics_exporter_prometheus::PrometheusHandle>, |
| 407 |
pub page_view_tx: db::page_views::PageViewTx, |
| 408 |
pub bg: background::BackgroundTx, |
| 409 |
} |
| 410 |
|
| 411 |
impl AppStorage { |
| 412 |
|
| 413 |
pub fn require_s3(&self) -> error::Result<&Arc<dyn StorageBackend>> { |
| 414 |
self.s3.as_ref().ok_or_else(|| { |
| 415 |
error::AppError::ServiceUnavailable("File storage is not configured".to_string()) |
| 416 |
}) |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
pub fn require_synckit_s3(&self) -> error::Result<&Arc<dyn StorageBackend>> { |
| 421 |
self.synckit_s3.as_ref().ok_or_else(|| { |
| 422 |
error::AppError::ServiceUnavailable("SyncKit storage is not configured".to_string()) |
| 423 |
}) |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
pub fn require_public_s3(&self) -> error::Result<&Arc<dyn StorageBackend>> { |
| 428 |
self.public_s3.as_ref().ok_or_else(|| { |
| 429 |
error::AppError::ServiceUnavailable( |
| 430 |
"Public storage bucket is not configured".to_string(), |
| 431 |
) |
| 432 |
}) |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
impl AppState { |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
pub fn build(parts: AppStateParts) -> Self { |
| 447 |
Self { |
| 448 |
db: parts.db, |
| 449 |
config: parts.config, |
| 450 |
storage: parts.storage, |
| 451 |
stripe: parts.stripe, |
| 452 |
email: parts.email, |
| 453 |
docs: parts.docs, |
| 454 |
tier_prices: parts.tier_prices, |
| 455 |
runway_config: parts.runway_config, |
| 456 |
fee_calculator: parts.fee_calculator, |
| 457 |
scanner: parts.scanner, |
| 458 |
webauthn: parts.webauthn, |
| 459 |
syntax: parts.syntax, |
| 460 |
started_at: chrono::Utc::now(), |
| 461 |
start_instant: Instant::now(), |
| 462 |
mt_client: parts.mt_client, |
| 463 |
wam: parts.wam, |
| 464 |
restart_at: Arc::new(std::sync::atomic::AtomicI64::new(0)), |
| 465 |
metrics_handle: parts.metrics_handle, |
| 466 |
page_view_tx: parts.page_view_tx, |
| 467 |
bg: parts.bg, |
| 468 |
caches: AppCaches { |
| 469 |
session_cache: Arc::new(DashMap::new()), |
| 470 |
domain_cache: parts.domain_cache, |
| 471 |
sync_notify: Arc::new(DashMap::new()), |
| 472 |
sse_connections: Arc::new(DashMap::new()), |
| 473 |
notes_cache: Arc::new(git::notes::NotesCache::default()), |
| 474 |
}, |
| 475 |
limiters: AppLimiters { |
| 476 |
scan_semaphore: Arc::new(tokio::sync::Semaphore::new( |
| 477 |
constants::SCAN_MAX_CONCURRENT, |
| 478 |
)), |
| 479 |
caddy_ask_semaphore: Arc::new(tokio::sync::Semaphore::new( |
| 480 |
constants::CADDY_ASK_MAX_CONCURRENT, |
| 481 |
)), |
| 482 |
git_smart_http_semaphore: Arc::new(tokio::sync::Semaphore::new( |
| 483 |
constants::GIT_SMART_HTTP_MAX_CONCURRENT, |
| 484 |
)), |
| 485 |
}, |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
pub fn require_s3(&self) -> error::Result<&Arc<dyn StorageBackend>> { |
| 491 |
self.storage.require_s3() |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
pub fn require_synckit_s3(&self) -> error::Result<&Arc<dyn StorageBackend>> { |
| 496 |
self.storage.require_synckit_s3() |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
pub fn require_public_s3(&self) -> error::Result<&Arc<dyn StorageBackend>> { |
| 501 |
self.storage.require_public_s3() |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
pub async fn delete_user_account( |
| 518 |
db: &sqlx::PgPool, |
| 519 |
caches: &AppCaches, |
| 520 |
user_id: db::UserId, |
| 521 |
) -> error::Result<()> { |
| 522 |
db::users::delete_user(db, user_id).await?; |
| 523 |
caches.domain_cache.retain(|_, uid| *uid != user_id); |
| 524 |
Ok(()) |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
pub fn build_app(state: &AppState, session_layer: SessionManagerLayer<PostgresStore>) -> Router { |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
let csrf_routes = csrf::CsrfRouter::new() |
| 535 |
.merge(auth_routes(state.config.rate_limits)) |
| 536 |
.merge(api_routes(state.config.rate_limits)) |
| 537 |
.merge(storage_routes()) |
| 538 |
.merge(stripe_routes()) |
| 539 |
.merge(admin_routes(state.clone())) |
| 540 |
.merge(synckit_routes( |
| 541 |
state |
| 542 |
.config |
| 543 |
.synckit_jwt_secret |
| 544 |
.clone() |
| 545 |
.map(std::sync::Arc::new), |
| 546 |
)) |
| 547 |
.merge(oauth_routes()) |
| 548 |
.merge(postmark_routes()) |
| 549 |
.merge(git_issue_routes()) |
| 550 |
.merge(git_write_routes()) |
| 551 |
.merge(ota_routes()) |
| 552 |
.merge(build_routes()) |
| 553 |
.finalize(); |
| 554 |
let app = Router::new() |
| 555 |
.merge(page_routes(state.config.rate_limits)) |
| 556 |
.merge(sso_routes()) |
| 557 |
.merge(csrf_routes) |
| 558 |
.merge(git_routes()) |
| 559 |
.merge(routes::embed::embed_routes()) |
| 560 |
.route( |
| 561 |
"/api/openapi.json", |
| 562 |
axum::routing::get(openapi::openapi_json), |
| 563 |
) |
| 564 |
.merge(utoipa_swagger_ui::SwaggerUi::new("/api/docs").url( |
| 565 |
"/api-docs/openapi.json", |
| 566 |
<openapi::ApiDoc as utoipa::OpenApi>::openapi(), |
| 567 |
)) |
| 568 |
.nest_service( |
| 569 |
"/static", |
| 570 |
tower::ServiceBuilder::new() |
| 571 |
.layer(SetResponseHeaderLayer::overriding( |
| 572 |
axum::http::header::CACHE_CONTROL, |
| 573 |
HeaderValue::from_static( |
| 574 |
"public, max-age=604800, stale-while-revalidate=86400", |
| 575 |
), |
| 576 |
)) |
| 577 |
.service(ServeDir::new("static")), |
| 578 |
) |
| 579 |
.fallback(routes::custom_domain::custom_domain_fallback) |
| 580 |
.with_state(state.clone()); |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
crate::rate_limit::start_governor_sweeper(); |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
app.layer(middleware::from_fn( |
| 596 |
fragment_redirect::fragment_redirect_middleware, |
| 597 |
)) |
| 598 |
.layer(middleware::from_fn(request_timeout_middleware)) |
| 599 |
.layer(middleware::from_fn_with_state( |
| 600 |
state.clone(), |
| 601 |
access_gate::access_gate_middleware, |
| 602 |
)) |
| 603 |
.layer(middleware::from_fn_with_state( |
| 604 |
state.clone(), |
| 605 |
security_headers_middleware, |
| 606 |
)) |
| 607 |
.layer(middleware::from_fn(metrics::cache_control_middleware)) |
| 608 |
.layer(middleware::from_fn(metrics::metrics_middleware)) |
| 609 |
.layer(middleware::from_fn_with_state( |
| 610 |
state.clone(), |
| 611 |
metrics::idempotency_middleware, |
| 612 |
)) |
| 613 |
.layer(session_layer) |
| 614 |
.layer(RequestBodyLimitLayer::new(1024 * 1024)) |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
.layer(middleware::from_fn_with_state( |
| 620 |
state.clone(), |
| 621 |
routes::user_pages::dispatch, |
| 622 |
)) |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
const REQUEST_TIMEOUT: std::time::Duration = std::time::Duration::from_mins(2); |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
fn timeout_exempt(path: &str) -> bool { |
| 641 |
path.starts_with("/git") |
| 642 |
|| path.starts_with("/api/export/") |
| 643 |
|| path.starts_with("/api/internal/creator/export/") |
| 644 |
|| path.starts_with("/api/sync/subscribe") |
| 645 |
|| path.starts_with("/api/v1/sync/subscribe") |
| 646 |
|| path.starts_with("/api/sync/ota") |
| 647 |
|| path.starts_with("/api/v1/sync/ota") |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
async fn request_timeout_middleware( |
| 656 |
request: axum::http::Request<axum::body::Body>, |
| 657 |
next: middleware::Next, |
| 658 |
) -> axum::response::Response { |
| 659 |
use axum::response::IntoResponse; |
| 660 |
if timeout_exempt(request.uri().path()) { |
| 661 |
return next.run(request).await; |
| 662 |
} |
| 663 |
match tokio::time::timeout(REQUEST_TIMEOUT, next.run(request)).await { |
| 664 |
Ok(response) => response, |
| 665 |
Err(_) => (axum::http::StatusCode::GATEWAY_TIMEOUT, "Request timed out").into_response(), |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
async fn security_headers_middleware( |
| 672 |
axum::extract::State(state): axum::extract::State<AppState>, |
| 673 |
request: axum::http::Request<axum::body::Body>, |
| 674 |
next: middleware::Next, |
| 675 |
) -> axum::response::Response { |
| 676 |
let path = request.uri().path(); |
| 677 |
let is_embed = path.starts_with("/embed/"); |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
let is_oauth_authorize = path == "/oauth/authorize"; |
| 684 |
let mut response = next.run(request).await; |
| 685 |
let headers = response.headers_mut(); |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
let report_endpoint = format!( |
| 692 |
"{}/api/csp-report", |
| 693 |
state.config.host_url.trim_end_matches('/') |
| 694 |
); |
| 695 |
let reporting = format!("; report-uri {report_endpoint}; report-to mnw-csp"); |
| 696 |
if let Ok(value) = HeaderValue::from_str(&format!("mnw-csp=\"{report_endpoint}\"")) { |
| 697 |
headers.insert( |
| 698 |
axum::http::header::HeaderName::from_static("reporting-endpoints"), |
| 699 |
value, |
| 700 |
); |
| 701 |
} |
| 702 |
|
| 703 |
if is_embed { |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
headers.insert( |
| 712 |
axum::http::header::X_FRAME_OPTIONS, |
| 713 |
HeaderValue::from_static("ALLOWALL"), |
| 714 |
); |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
let embed_csp = format!( |
| 720 |
"default-src 'none'; \ |
| 721 |
img-src 'self' data: https:; \ |
| 722 |
media-src 'self'; \ |
| 723 |
style-src 'unsafe-inline'; \ |
| 724 |
script-src 'self'; \ |
| 725 |
font-src 'self'; \ |
| 726 |
base-uri 'none'; \ |
| 727 |
form-action 'none'; \ |
| 728 |
frame-ancestors *{reporting}" |
| 729 |
); |
| 730 |
if let Ok(value) = HeaderValue::from_str(&embed_csp) { |
| 731 |
headers.insert( |
| 732 |
axum::http::header::HeaderName::from_static("content-security-policy"), |
| 733 |
value, |
| 734 |
); |
| 735 |
} |
| 736 |
} else { |
| 737 |
|
| 738 |
headers.insert( |
| 739 |
axum::http::header::X_FRAME_OPTIONS, |
| 740 |
HeaderValue::from_static("DENY"), |
| 741 |
); |
| 742 |
|
| 743 |
let s3_origin = std::env::var("S3_ENDPOINT").unwrap_or_default(); |
| 744 |
let s3_origin = s3_origin.as_str(); |
| 745 |
let cdn = state.config.cdn_base_url.as_str(); |
| 746 |
let storage_origins = match (s3_origin.is_empty(), cdn.is_empty()) { |
| 747 |
(false, false) => format!(" {s3_origin} {cdn}"), |
| 748 |
(false, true) => format!(" {s3_origin}"), |
| 749 |
(true, false) => format!(" {cdn}"), |
| 750 |
(true, true) => String::new(), |
| 751 |
}; |
| 752 |
|
| 753 |
|
| 754 |
let scheme = if state.config.host_url.starts_with("https") { |
| 755 |
"https" |
| 756 |
} else { |
| 757 |
"http" |
| 758 |
}; |
| 759 |
let user_pages_origin = format!("{scheme}://{}", state.config.user_pages_host); |
| 760 |
let form_action = if is_oauth_authorize { |
| 761 |
"'self' http://127.0.0.1:* http://[::1]:* http://localhost:*" |
| 762 |
} else { |
| 763 |
"'self'" |
| 764 |
}; |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
let policy = |style_src: &str| { |
| 771 |
format!( |
| 772 |
"default-src 'self'; \ |
| 773 |
script-src 'self' https://js.stripe.com; \ |
| 774 |
style-src {style_src}; \ |
| 775 |
img-src 'self' data: https:; \ |
| 776 |
font-src 'self'; \ |
| 777 |
connect-src 'self' https://api.stripe.com{storage_origins}; \ |
| 778 |
media-src 'self'{storage_origins}; \ |
| 779 |
frame-src 'self' https://js.stripe.com {user_pages_origin}; \ |
| 780 |
base-uri 'self'; \ |
| 781 |
form-action {form_action}; \ |
| 782 |
frame-ancestors 'none'{reporting}" |
| 783 |
) |
| 784 |
}; |
| 785 |
if let Ok(value) = HeaderValue::from_str(&policy("'self' 'unsafe-inline'")) { |
| 786 |
headers.insert( |
| 787 |
axum::http::header::HeaderName::from_static("content-security-policy"), |
| 788 |
value, |
| 789 |
); |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
if let Ok(value) = HeaderValue::from_str(&policy("'self'")) { |
| 800 |
headers.insert( |
| 801 |
axum::http::header::HeaderName::from_static("content-security-policy-report-only"), |
| 802 |
value, |
| 803 |
); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
headers.insert( |
| 808 |
axum::http::header::HeaderName::from_static("strict-transport-security"), |
| 809 |
HeaderValue::from_static("max-age=31536000; includeSubDomains"), |
| 810 |
); |
| 811 |
headers.insert( |
| 812 |
axum::http::header::X_CONTENT_TYPE_OPTIONS, |
| 813 |
HeaderValue::from_static("nosniff"), |
| 814 |
); |
| 815 |
headers.insert( |
| 816 |
axum::http::header::REFERRER_POLICY, |
| 817 |
HeaderValue::from_static("strict-origin-when-cross-origin"), |
| 818 |
); |
| 819 |
headers.insert( |
| 820 |
axum::http::header::HeaderName::from_static("permissions-policy"), |
| 821 |
HeaderValue::from_static("camera=(), microphone=(), geolocation=()"), |
| 822 |
); |
| 823 |
response |
| 824 |
} |
| 825 |
|
| 826 |
#[cfg(test)] |
| 827 |
mod from_ref_slices { |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
use super::*; |
| 835 |
|
| 836 |
fn assert_from_ref<T: FromRef<AppState>>() {} |
| 837 |
|
| 838 |
#[test] |
| 839 |
fn slices_are_extractable() { |
| 840 |
|
| 841 |
assert_from_ref::<sqlx::PgPool>(); |
| 842 |
assert_from_ref::<Config>(); |
| 843 |
assert_from_ref::<EmailClient>(); |
| 844 |
assert_from_ref::<background::BackgroundTx>(); |
| 845 |
|
| 846 |
assert_from_ref::<AppStorage>(); |
| 847 |
assert_from_ref::<AppCaches>(); |
| 848 |
assert_from_ref::<AppLimiters>(); |
| 849 |
|
| 850 |
assert_from_ref::<Billing>(); |
| 851 |
assert_from_ref::<Integrations>(); |
| 852 |
assert_from_ref::<Scanning>(); |
| 853 |
assert_from_ref::<Sync>(); |
| 854 |
assert_from_ref::<Git>(); |
| 855 |
assert_from_ref::<Ops>(); |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
assert_from_ref::<monitor::MonitorCtx>(); |
| 860 |
assert_from_ref::<build_runner::BuildCtx>(); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
#[cfg(test)] |
| 865 |
mod timeout_exempt_tests { |
| 866 |
use super::timeout_exempt; |
| 867 |
|
| 868 |
#[test] |
| 869 |
fn exempts_only_anchored_long_running_routes() { |
| 870 |
|
| 871 |
for p in [ |
| 872 |
"/git/foo/bar.git/info/refs", |
| 873 |
"/api/export/content", |
| 874 |
"/api/internal/creator/export/sales", |
| 875 |
"/api/sync/subscribe", |
| 876 |
"/api/v1/sync/subscribe", |
| 877 |
"/api/sync/ota/apps/x/releases", |
| 878 |
"/api/v1/sync/ota/slug/macos/arm64/1.0.0", |
| 879 |
] { |
| 880 |
assert!(timeout_exempt(p), "{p} should be exempt"); |
| 881 |
} |
| 882 |
|
| 883 |
for p in [ |
| 884 |
"/dashboard/export", |
| 885 |
"/u/somecreator/export-notes", |
| 886 |
"/items/sync/ota-recap", |
| 887 |
] { |
| 888 |
assert!(!timeout_exempt(p), "{p} must not be exempt"); |
| 889 |
} |
| 890 |
} |
| 891 |
} |
| 892 |
|