diff --git a/crates/in-web/src/files.rs b/crates/in-web/src/files.rs index acf5de3..aa4fd60 100644 --- a/crates/in-web/src/files.rs +++ b/crates/in-web/src/files.rs @@ -201,8 +201,9 @@ async fn visible_file( /// Serves one file's bytes, or the same not-found a stranger would see for /// a file that does not exist — never a `403`, which would confirm the id -/// belongs to somebody else's drive. Only `?dl=1` counts a download: the -/// viewer reads the same bytes inline, and looking is not downloading. +/// belongs to somebody else's drive. Inline bytes are the viewer's preview: +/// anyone who may see the file reads them. Taking the file away is a +/// different act — `?dl=1` needs the download grant, and only it counts. #[route(GET "/file/{id}")] async fn download(cx: &Cx) -> topcoat::Result<(StatusCode, HeaderMap, Vec)> { let id: &str = path_param::(cx); @@ -218,23 +219,23 @@ async fn download(cx: &Cx) -> topcoat::Result<(StatusCode, HeaderMap, Vec)> let Some(file) = visible_file(store.as_ref(), &user.id, id).await else { return Ok(not_found()); }; - // A view-only grant opens the file's page but not its bytes. - if file.owner_id != user.id - && !store + let forced = query_params::(cx) + .ok() + .and_then(|query| query.dl.clone()) + .is_some(); + // A view-only grant previews but never downloads. + let may_download = file.owner_id == user.id + || store .can_download(ShareKind::File, id, &user.id) .await - .unwrap_or(false) - { + .unwrap_or(false); + if forced && !may_download { return Ok(not_found()); } let Ok(Some(bytes)) = store.file_bytes(id).await else { return Ok(not_found()); }; - let forced = query_params::(cx) - .ok() - .and_then(|query| query.dl.clone()) - .is_some(); let inline = !forced && inline_ok(&file.mime); let mut headers = HeaderMap::new(); @@ -432,11 +433,14 @@ fn is_archive_mime(mime: &str) -> bool { /// `GET /view/{id}`: one file's viewer page — its name, the inline viewer /// for its kind, a download link and its details. Authorization mirrors -/// trashed files open for nobody. A view-only grant opens the page but not -/// the bytes: the download link shows only while the reader may download, -/// and the inline viewer gives way to the unavailable note, since every -/// viewer element reads the same gated bytes. Bytes for another owner's -/// file answer 404, not 403. +/// trashed files open for nobody. Anyone who may see the file gets the +/// media preview — "view only" means preview, no download: the media +/// elements carry no save affordance (no download in the controls, no +/// drag, no context menu — `viewer_guard_script`), the Download link shows +/// only while the reader may download, and `?dl=1` stays gated on the +/// byte route. Documents (PDF, text) frame the bytes in the browser's own +/// chrome, whose toolbar downloads — they preview only under the grant. +/// Bytes for another owner's file answer 404, not 403. #[page("/view/{id}")] async fn view_file(cx: &Cx) -> Result { let id: &str = path_param::(cx); @@ -486,11 +490,13 @@ async fn view_file(cx: &Cx) -> Result {

(entry_chip(cx, &file).await?) (file.name.clone())

(meta)

- if may_download { + // Media previews for every reader; documents only under the + // download grant (their browser chrome saves the file). + if matches!(viewer_kind(&file.mime), Some(ViewerKind::Image | ViewerKind::Video | ViewerKind::Audio)) || may_download { match viewer_kind(&file.mime) { - Some(ViewerKind::Image) => (file.name.clone()), - Some(ViewerKind::Video) => , - Some(ViewerKind::Audio) =>
, + Some(ViewerKind::Image) => (file.name.clone()), + Some(ViewerKind::Video) => , + Some(ViewerKind::Audio) =>
, Some(ViewerKind::Pdf) =>

(t(language, Key::PreviewUnavailable))

, @@ -500,9 +506,6 @@ async fn view_file(cx: &Cx) -> Result {
} } else { - // A view-only grant opens the page but not the bytes, so - // the viewer elements — every one reads /file/{id} — - // would only frame a 404. The note instead.

(t(language, Key::PreviewUnavailable))

@@ -528,9 +531,29 @@ async fn view_file(cx: &Cx) -> Result { + (viewer_guard_script(cx).await?) } } +/// The preview's save-path cleanup: the media elements already carry no +/// download affordance (`controlslist="nodownload"`, `draggable="false"`), +/// and this drops the context menu over the stage, so "save as" never +/// opens on a preview. A page's bytes on the wire can always be captured — +/// this keeps the surface from offering it, which is what a view-only +/// grant promises. +async fn viewer_guard_script(cx: &Cx) -> Result { + use topcoat::view::Unescaped; + const JS: &str = "\ + (function () { \ + if (window.__inViewerGuard) { return; } \ + window.__inViewerGuard = true; \ + document.addEventListener('contextmenu', function (e) { \ + if (e.target && e.target.closest && e.target.closest('.viewer-stage')) { e.preventDefault(); } \ + }); \ + })();"; + view! { cx => } +} + /// Takes the files the drive's upload form carries — one per `file` part, /// and a `multiple` picker posts several. The route's body guard sits at 2 /// GiB (`main.rs`); each part is buffered whole because the store writes diff --git a/crates/in-web/tests/http.rs b/crates/in-web/tests/http.rs index f88c398..1904937 100644 --- a/crates/in-web/tests/http.rs +++ b/crates/in-web/tests/http.rs @@ -1639,6 +1639,60 @@ async fn per_user_share_lands_in_shared_with_me() { assert!(!bobs.text().contains("joint.txt"), "{}", bobs.text()); } +/// View only means preview, no download: the reader sees the media inline +/// on the viewer page and reads the inline bytes, while `?dl=1` stays dead +/// and the page never offers the Download link. +#[tokio::test] +async fn view_only_reader_previews_media_but_cannot_download() { + let app = TestApp::build().await; + let admin = app.sign_in("sub-admin", "ada@in.test", "Ada").await; + let bob = app.sign_in("sub-bob", "bob@in.test", "Bob").await; + let owner = owner_of(&app, "sub-admin").await; + let file = file_id( + &app, + &owner, + "photo.png", + b"\x89PNG\r\n\x1a\n preview bytes", + ) + .await; + + let answer = app + .post( + "/api/share/user/add", + Some(&admin), + &[ + ("kind", "file"), + ("target_id", &file), + ("email", "bob@in.test"), + ("can_download", "0"), + ], + ) + .await; + assert!(answer.accepted(), "add refused: {:?}", answer.location); + + // The viewer page renders the media, not the unavailable note, and + // offers no download. + let page = app.get(&format!("/view/{file}"), Some(&bob)).await; + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); + assert!(page.text().contains("viewer-media"), "{}", page.text()); + assert!(!page.text().contains("No preview"), "{}", page.text()); + assert!(!page.text().contains("?dl=1"), "{}", page.text()); + + // The inline bytes serve for the preview... + let inline = app.get(&format!("/file/{file}"), Some(&bob)).await; + assert_eq!(inline.status, StatusCode::OK, "{}", inline.text()); + assert_eq!(inline.bytes, b"\x89PNG\r\n\x1a\n preview bytes"); + // ...and taking the file away stays the stranger's answer. + let taken = app.get(&format!("/file/{file}?dl=1"), Some(&bob)).await; + assert_eq!(taken.status, StatusCode::NOT_FOUND, "{}", taken.text()); + + // A preview is not a download: the counter never moves. + assert_eq!( + app.store.file(&file).await.unwrap().unwrap().download_count, + 0 + ); +} + // -- trash ------------------------------------------------------------------ #[tokio::test]