Skip to content
1        10        20        30        40        50        60        70        80        901use std::{fs, path::PathBuf};23use actix_web::{App, http::StatusCode, test, web};4use base64::{Engine as _, engine::general_purpose::STANDARD};5use dav_server::fs::DavFileSystem;6use obsidian_sync::{7    config::Settings,8    storage::RootFs,9    webdav::{self, AppState},10};11use tempfile::tempdir;1213fn app_state(storage: PathBuf) -> web::Data<AppState> {14    web::Data::new(15        AppState::new(16            Settings {17                dav_username: "device".to_owned(),18                dav_password: "secret".to_owned(),19            },20            storage,21        )22        .expect("construct app state"),23    )24}2526fn authorization() -> String {27    format!("Basic {}", STANDARD.encode("device:secret"))28}2930#[actix_web::test]31async fn rootfs_persists_after_reopen() {32    let storage = tempdir().expect("create storage");33    {34        let app = test::init_service(35            App::new()36                .app_data(app_state(storage.path().to_path_buf()))37                .configure(webdav::configure),38        )39        .await;40        let mkcol = test::TestRequest::default()41            .method(actix_web::http::Method::from_bytes(b"MKCOL").expect("MKCOL method"))42            .uri("/notes/")43            .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))44            .to_request();45        assert_eq!(46            test::call_service(&app, mkcol).await.status(),47            StatusCode::CREATED48        );4950        let put = test::TestRequest::put()51            .uri("/notes/today.md")52            .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))53            .set_payload("persistent note")54            .to_request();55        let response = test::call_service(&app, put).await;56        assert_eq!(response.status(), StatusCode::CREATED);57    }5859    assert_eq!(60        fs::read(storage.path().join("notes/today.md")).expect("stored note"),61        b"persistent note"62    );6364    let app = test::init_service(65        App::new()66            .app_data(app_state(storage.path().to_path_buf()))67            .configure(webdav::configure),68    )69    .await;70    let get = test::TestRequest::get()71        .uri("/notes/today.md")72        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))73        .to_request();74    let body = test::call_and_read_body(&app, get).await;75    assert_eq!(body.as_ref(), b"persistent note");76}7778#[cfg(unix)]79#[actix_web::test]80async fn rootfs_symlink_cannot_escape_storage() {81    use std::os::unix::fs::symlink;8283    let storage = tempdir().expect("create storage");84    let outside = tempdir().expect("create outside directory");85    fs::write(outside.path().join("secret.md"), "outside secret").expect("create secret");86    symlink(outside.path(), storage.path().join("escape")).expect("create symlink");8788    let app = test::init_service(89        App::new()90            .app_data(app_state(storage.path().to_path_buf()))91            .configure(webdav::configure),92    )93    .await;94    let get = test::TestRequest::get()95        .uri("/escape/secret.md")96        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))97        .to_request();98    let response = test::call_service(&app, get).await;99    assert!(100        !response.status().is_success(),101        "symlink GET status: {}",102        response.status()103    );104105    let put = test::TestRequest::put()106        .uri("/escape/secret.md")107        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))108        .set_payload("overwritten")109        .to_request();110    let response = test::call_service(&app, put).await;111    assert!(112        !response.status().is_success(),113        "symlink PUT status: {}",114        response.status()115    );116    assert_eq!(117        fs::read_to_string(outside.path().join("secret.md")).expect("read secret"),118        "outside secret"119    );120}121122#[actix_web::test]123async fn rootfs_protects_virtual_root() {124    let storage = tempdir().expect("create storage");125    let root = RootFs::open(storage.path()).expect("open rooted filesystem");126    let dav_root = dav_server::davpath::DavPath::new("/").expect("root DAV path");127    let target = dav_server::davpath::DavPath::new("/target").expect("target DAV path");128129    assert_eq!(130        root.remove_dir(&dav_root).await,131        Err(dav_server::fs::FsError::Forbidden)132    );133    assert_eq!(134        root.rename(&dav_root, &target).await,135        Err(dav_server::fs::FsError::Forbidden)136    );137}