use forge_core::{Repo, Visibility}; use futures_util::TryStreamExt; use http_body::Frame; use http_body_util::StreamBody; use tokio_util::io::ReaderStream; use topcoat::context::{Cx, context}; use topcoat::router::request::method; use topcoat::router::response::Response; use topcoat::router::{ Body, Method, RouteFuture, StatusCode, parse_query_params, path_param_segment, path_param_segments, }; use crate::lfs::{has_object, object_path}; use crate::lfs_pointer; use crate::raw::{ CACHE_NONE, Lookup, Meta, OCTET, TokenQuery, encode_path, finish, lookup, plain, resolve_repo, signed_url, }; use crate::state::AppState; pub fn media_url(state: &AppState, repo: &Repo, rev: &str, path: &str) -> String { let encoded = format!( "{}/{}/-/media/{}/{}", encode_path(&repo.owner_name), encode_path(&repo.name), encode_path(rev), encode_path(path) ); signed_url( state, &encoded, &repo.id, matches!(repo.visibility, Visibility::Private), ) } pub fn media_file(cx: &Cx, _body: Body) -> RouteFuture<'_> { Box::pin(async move { Ok(serve(cx).await) }) } async fn serve(cx: &Cx) -> Response { let state = context::(cx).clone(); let owner = path_param_segment(cx, "owner").to_owned(); let name = path_param_segment(cx, "repo").to_owned(); let rest = path_param_segments(cx, "rest") .collect::>() .join("/"); let is_head = method(cx) == Method::HEAD; let token = parse_query_params::(cx) .ok() .and_then(|query| query.token); let (repo, repo_dir, cache) = match resolve_repo(&state, &owner, &name, token.as_deref()).await { Ok(found) => found, Err(response) => return response, }; let looked = tokio::task::spawn_blocking(move || lookup(&repo_dir, &rest, None)).await; let data = match looked { Ok(Ok(Lookup::Inline { symlink: false, data, .. })) => data, Ok(Ok(_)) => return plain(StatusCode::NOT_FOUND, CACHE_NONE, None), Ok(Err(e)) => { tracing::error!(error = %e, "media: lookup failed"); return plain(StatusCode::INTERNAL_SERVER_ERROR, CACHE_NONE, None); } Err(e) => { tracing::error!(error = %e, "media: lookup task failed"); return plain(StatusCode::INTERNAL_SERVER_ERROR, CACHE_NONE, None); } }; let Some(pointer) = lfs_pointer::parse(&data) else { return plain(StatusCode::NOT_FOUND, CACHE_NONE, None); }; let size = match has_object(&state, &repo.id, &pointer.oid).await { Ok(Some(size)) => size, Ok(None) => return plain(StatusCode::NOT_FOUND, CACHE_NONE, None), Err(e) => { tracing::error!(error = %e, "media: object link lookup failed"); return plain(StatusCode::INTERNAL_SERVER_ERROR, CACHE_NONE, None); } }; let file = match tokio::fs::File::open(object_path(&state.config.data_dir, &pointer.oid)).await { Ok(file) => file, Err(e) if e.kind() == std::io::ErrorKind::NotFound => { return plain(StatusCode::NOT_FOUND, CACHE_NONE, None); } Err(e) => { tracing::error!(error = %e, "media: failed to open object"); return plain(StatusCode::INTERNAL_SERVER_ERROR, CACHE_NONE, None); } }; let on_disk = match file.metadata().await { Ok(meta) => meta.len(), Err(e) => { tracing::error!(error = %e, "media: object metadata failed"); return plain(StatusCode::INTERNAL_SERVER_ERROR, CACHE_NONE, None); } }; if on_disk != size { tracing::error!(oid = %pointer.oid, on_disk, size, "media: object size differs from its link"); return plain(StatusCode::INTERNAL_SERVER_ERROR, CACHE_NONE, None); } let meta = Meta { content_type: OCTET, attachment: true, len: size, }; if is_head { return finish( StatusCode::OK, Body::empty(), &pointer.oid, cache, Some(meta), ); } let chunks = ReaderStream::new(file).map_ok(Frame::data); let body = Body::new(StreamBody::new(chunks)); finish(StatusCode::OK, body, &pointer.oid, cache, Some(meta)) } #[cfg(test)] mod tests { use super::*; use crate::raw::now_unix; use forge_core::raw_token::{sign_raw, verify_raw}; use sha2::{Digest, Sha256}; use topcoat::router::Router; use topcoat::router::request::Request; use crate::raw::router; const POINTER_HEADER: &str = "version https://git-lfs.github.com/spec/v1"; struct Fixture { _tmp: tempfile::TempDir, state: AppState, router: Router, public: Repo, private: Repo, object: Vec, oid: String, } fn sha256_hex(data: &[u8]) -> String { hex::encode(Sha256::digest(data)) } fn pointer_text(oid: &str, size: usize) -> Vec { format!("{POINTER_HEADER}\noid sha256:{oid}\nsize {size}\n").into_bytes() } fn git(dir: &std::path::Path, args: &[&str]) { let out = std::process::Command::new("git") .arg("-C") .arg(dir) .args(["-c", "commit.gpgsign=false"]) .args(args) .env("GIT_AUTHOR_NAME", "test") .env("GIT_AUTHOR_EMAIL", "test@example.com") .env("GIT_COMMITTER_NAME", "test") .env("GIT_COMMITTER_EMAIL", "test@example.com") .output() .expect("git runs"); assert!( out.status.success(), "git {args:?} failed: {}", String::from_utf8_lossy(&out.stderr) ); } fn populate(work: &std::path::Path, bare: &std::path::Path, files: &[(&str, Vec)]) { std::fs::create_dir_all(work).expect("mkdir work"); git(work, &["init", "-q", "-b", "main"]); for (rel, contents) in files { std::fs::write(work.join(rel), contents).expect("write file"); } git(work, &["add", "-A"]); git(work, &["commit", "-q", "-m", "init"]); git(work, &["push", "-q", bare.to_str().expect("utf8"), "main"]); } async fn fixture() -> Fixture { let tmp = tempfile::tempdir().expect("tempdir"); let data_dir = tmp.path().join("data"); std::fs::create_dir_all(&data_dir).expect("data dir"); let data_dir = data_dir.canonicalize().expect("canonical data dir"); let mut config = crate::test_support::test_config(&data_dir); config.user_content_base_url = "http://127.0.0.1:7661".into(); let state = AppState { config: std::sync::Arc::new(config), ..crate::test_support::test_state(&data_dir).await }; let store = state.store.clone(); let owner = store .create_user("tahinli", None, "unused", false) .await .expect("user"); let public = store .create_repo(&owner.id, "pub", "", forge_core::Visibility::Public, "main") .await .expect("public repo"); let private = store .create_repo( &owner.id, "priv", "", forge_core::Visibility::Private, "main", ) .await .expect("private repo"); for repo in [&public, &private] { let bare = forge_git::repo::repo_path(&data_dir, &repo.id).expect("repo path"); forge_git::repo::init_bare(&state.git, &bare, "main") .await .expect("init bare"); } let object = b"lfs object bytes\n".to_vec(); let oid = sha256_hex(&object); let missing_oid = sha256_hex(b"never stored"); let stored_pointer = pointer_text(&oid, object.len()); let missing_pointer = pointer_text(&missing_oid, 12); let files: Vec<(&str, Vec)> = vec![ ("big.bin", stored_pointer.clone()), ("missing.bin", missing_pointer), ("plain.txt", b"just text\n".to_vec()), ]; let pub_bare = forge_git::repo::repo_path(&data_dir, &public.id).expect("path"); populate(&tmp.path().join("work-pub"), &pub_bare, &files); let priv_bare = forge_git::repo::repo_path(&data_dir, &private.id).expect("path"); populate( &tmp.path().join("work-priv"), &priv_bare, &[("big.bin", stored_pointer)], ); for repo in [&public, &private] { store .insert_lfs_object(&repo.id, &oid, object.len() as u64) .await .expect("link object"); } let path = object_path(&data_dir, &oid); std::fs::create_dir_all(path.parent().expect("parent")).expect("mkdir objects"); std::fs::write(&path, &object).expect("write object"); let router = router(state.clone()); Fixture { _tmp: tmp, state, router, public, private, object, oid, } } async fn get(router: &Router, method: &str, uri: &str) -> Response { let req = Request::builder() .method(method) .uri(uri) .body(Body::empty()) .expect("request"); router.handle(req).await } fn header_str(res: &Response, name: &str) -> String { res.headers() .get(name) .and_then(|v| v.to_str().ok()) .unwrap_or_default() .to_owned() } async fn body_bytes(res: Response) -> Vec { topcoat::router::to_bytes(res.into_body(), usize::MAX) .await .expect("body") .to_vec() } #[tokio::test] async fn public_media_serves_downloads_heads_and_404s() { let fx = fixture().await; let res = get(&fx.router, "GET", "/tahinli/pub/-/media/main/big.bin").await; assert_eq!(res.status(), StatusCode::OK, "linked object"); assert_eq!( header_str(&res, "content-disposition"), "attachment", "linked object disposition" ); assert_eq!( header_str(&res, "x-content-type-options"), "nosniff", "linked object nosniff" ); assert_eq!( header_str(&res, "content-type"), OCTET, "linked object content type" ); assert_eq!( header_str(&res, "content-length"), fx.object.len().to_string(), "linked object length" ); assert_eq!( header_str(&res, "etag"), format!("\"{}\"", fx.oid), "linked object etag" ); assert_eq!(body_bytes(res).await, fx.object, "linked object body"); let res = get(&fx.router, "HEAD", "/tahinli/pub/-/media/main/big.bin").await; assert_eq!(res.status(), StatusCode::OK, "head"); assert_eq!( header_str(&res, "content-length"), fx.object.len().to_string(), "head length" ); assert!(body_bytes(res).await.is_empty(), "head body"); let res = get(&fx.router, "GET", "/tahinli/pub/-/media/main/missing.bin").await; assert_eq!(res.status(), StatusCode::NOT_FOUND, "unlinked object"); let text = get(&fx.router, "GET", "/tahinli/pub/-/media/main/plain.txt").await; assert_eq!(text.status(), StatusCode::NOT_FOUND, "non-pointer"); let absent = get(&fx.router, "GET", "/tahinli/pub/-/media/main/nope.bin").await; assert_eq!(absent.status(), StatusCode::NOT_FOUND, "absent path"); let unknown_repo = get(&fx.router, "GET", "/tahinli/nope/-/media/main/big.bin").await; assert_eq!(unknown_repo.status(), StatusCode::NOT_FOUND, "unknown repo"); } #[tokio::test] async fn private_media_needs_a_valid_token_and_urls_sign_private_repos_only() { let fx = fixture().await; let path = "/tahinli/priv/-/media/main/big.bin"; let none = get(&fx.router, "GET", path).await; assert_eq!(none.status(), StatusCode::NOT_FOUND, "no token"); let now = now_unix(); let valid = sign_raw(&fx.state.key, &fx.private.id, now + 60); let ok = get(&fx.router, "GET", &format!("{path}?token={valid}")).await; assert_eq!(ok.status(), StatusCode::OK, "valid token"); assert_eq!(body_bytes(ok).await, fx.object, "valid token body"); let expired = sign_raw(&fx.state.key, &fx.private.id, now - 1); let res = get(&fx.router, "GET", &format!("{path}?token={expired}")).await; assert_eq!(res.status(), StatusCode::NOT_FOUND, "expired token"); let other = sign_raw(&fx.state.key, &fx.public.id, now + 60); let res = get(&fx.router, "GET", &format!("{path}?token={other}")).await; assert_eq!( res.status(), StatusCode::NOT_FOUND, "token for another repo" ); let public = media_url(&fx.state, &fx.public, "main", "a b/big.bin"); assert_eq!( public, "http://127.0.0.1:7661/tahinli/pub/-/media/main/a%20b/big.bin", "public url" ); let private = media_url(&fx.state, &fx.private, "main", "big.bin"); assert!( private.starts_with("http://127.0.0.1:7661/tahinli/priv/-/media/main/big.bin?token="), "private url prefix" ); let token = private .split_once("?token=") .map(|(_, t)| t) .unwrap_or_default(); assert!( verify_raw(&fx.state.key, token, &fx.private.id, now_unix()), "private url token" ); } #[tokio::test] async fn branch_named_media_does_not_shadow_the_media_route() { let fx = fixture().await; let work = fx._tmp.path().join("work-pub"); let bare = forge_git::repo::repo_path(&fx.state.config.data_dir, &fx.public.id) .expect("repo path"); git(&work, &["checkout", "-q", "-b", "media"]); std::fs::write(work.join("README.md"), "# On a media branch\n").expect("write README"); git(&work, &["add", "-A"]); git(&work, &["commit", "-q", "-m", "media branch"]); git( &work, &["push", "-q", bare.to_str().expect("utf8"), "media"], ); let readme = get(&fx.router, "GET", "/tahinli/pub/media/README.md").await; assert_eq!(readme.status(), StatusCode::OK, "branch raw file"); assert_eq!( body_bytes(readme).await, b"# On a media branch\n".to_vec(), "branch raw body" ); let dash = get(&fx.router, "GET", "/tahinli/pub/-/README.md").await; assert_eq!(dash.status(), StatusCode::NOT_FOUND, "dash is not a ref"); let url = media_url(&fx.state, &fx.public, "main", "big.bin"); let path = url .strip_prefix("http://127.0.0.1:7661") .expect("configured base"); let media = get(&fx.router, "GET", path).await; assert_eq!(media.status(), StatusCode::OK, "media route still wins"); assert_eq!(body_bytes(media).await, fx.object, "media body"); } }