Compare commits
1 Commits
bugfix/man
...
chore/ci-p
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84a033a0fb |
12
.env.example
12
.env.example
@@ -1,13 +1,23 @@
|
|||||||
# Copy to .env for `docker compose up --build`. Local-dev runs (cargo run
|
# Copy to .env for `docker compose up --build`. Local-dev runs (cargo run
|
||||||
# / npm run dev) read backend/.env if present, or pick up the variables
|
# / npm run dev) read backend/.env if present, or pick up the variables
|
||||||
# from your shell.
|
# from your shell.
|
||||||
|
#
|
||||||
|
# Production note: COOKIE_SECURE=true (the default below) makes browsers
|
||||||
|
# refuse to send the session cookie over plain HTTP. Run with a TLS-
|
||||||
|
# terminating reverse proxy (Caddy, Traefik, nginx) in front — the
|
||||||
|
# compose file here doesn't ship one. Local/dev runs without HTTPS
|
||||||
|
# should set COOKIE_SECURE=false.
|
||||||
|
|
||||||
# ----- Postgres -----
|
# ----- Postgres -----
|
||||||
# These are read by the Postgres container *and* by DATABASE_URL below;
|
# These are read by the Postgres container *and* by DATABASE_URL below;
|
||||||
# changing them after the first boot won't migrate existing data, so set
|
# changing them after the first boot won't migrate existing data, so set
|
||||||
# them up front for any new deployment.
|
# them up front for any new deployment.
|
||||||
|
#
|
||||||
|
# POSTGRES_PASSWORD is REQUIRED — docker-compose.yml fails fast if it
|
||||||
|
# isn't set in this file, to prevent a deploy without an .env booting
|
||||||
|
# Postgres with a publicly-known credential.
|
||||||
POSTGRES_USER=mangalord
|
POSTGRES_USER=mangalord
|
||||||
POSTGRES_PASSWORD=mangalord
|
POSTGRES_PASSWORD=change-me-to-a-strong-random-string
|
||||||
POSTGRES_DB=mangalord
|
POSTGRES_DB=mangalord
|
||||||
|
|
||||||
# ----- Backend -----
|
# ----- Backend -----
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ name: deploy
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [main]
|
branches: [main]
|
||||||
|
pull_request:
|
||||||
|
branches: [main]
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
@@ -63,6 +65,10 @@ jobs:
|
|||||||
build-and-push:
|
build-and-push:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [test-backend, test-frontend]
|
needs: [test-backend, test-frontend]
|
||||||
|
# PRs only run the test jobs; build + deploy are reserved for
|
||||||
|
# post-merge pushes to main. Without this gate every PR would push
|
||||||
|
# a tagged image to the registry and SSH-deploy to prod.
|
||||||
|
if: github.event_name != 'pull_request'
|
||||||
outputs:
|
outputs:
|
||||||
image_tag: ${{ steps.meta.outputs.image_tag }}
|
image_tag: ${{ steps.meta.outputs.image_tag }}
|
||||||
version: ${{ steps.meta.outputs.version }}
|
version: ${{ steps.meta.outputs.version }}
|
||||||
@@ -117,6 +123,7 @@ jobs:
|
|||||||
deploy:
|
deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: build-and-push
|
needs: build-and-push
|
||||||
|
if: github.event_name != 'pull_request'
|
||||||
steps:
|
steps:
|
||||||
- name: SSH deploy
|
- name: SSH deploy
|
||||||
uses: appleboy/ssh-action@v1.0.3
|
uses: appleboy/ssh-action@v1.0.3
|
||||||
|
|||||||
2
backend/Cargo.lock
generated
2
backend/Cargo.lock
generated
@@ -1470,7 +1470,7 @@ checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mangalord"
|
name = "mangalord"
|
||||||
version = "0.34.1"
|
version = "0.34.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"argon2",
|
"argon2",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mangalord"
|
name = "mangalord"
|
||||||
version = "0.34.1"
|
version = "0.34.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
default-run = "mangalord"
|
default-run = "mangalord"
|
||||||
|
|
||||||
|
|||||||
@@ -196,14 +196,16 @@ async fn create(
|
|||||||
|
|
||||||
async fn update(
|
async fn update(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
CurrentUser(user): CurrentUser,
|
CurrentUser(_user): CurrentUser,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
Json(patch): Json<MangaPatch>,
|
Json(patch): Json<MangaPatch>,
|
||||||
) -> AppResult<Json<MangaDetail>> {
|
) -> AppResult<Json<MangaDetail>> {
|
||||||
|
// TODO(auth): until uploaders are tracked (Phase 5), any signed-in
|
||||||
|
// user can edit any manga. Restrict to uploader + admin once that
|
||||||
|
// column lands.
|
||||||
if !repo::manga::exists(&state.db, id).await? {
|
if !repo::manga::exists(&state.db, id).await? {
|
||||||
return Err(AppError::NotFound);
|
return Err(AppError::NotFound);
|
||||||
}
|
}
|
||||||
require_can_edit(&state, id, user.id).await?;
|
|
||||||
|
|
||||||
if let Some(ref status) = patch.status {
|
if let Some(ref status) = patch.status {
|
||||||
let trimmed = status.trim();
|
let trimmed = status.trim();
|
||||||
@@ -267,14 +269,16 @@ async fn update(
|
|||||||
/// `MangaDetail`.
|
/// `MangaDetail`.
|
||||||
async fn put_cover(
|
async fn put_cover(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
CurrentUser(user): CurrentUser,
|
CurrentUser(_user): CurrentUser,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
mut multipart: Multipart,
|
mut multipart: Multipart,
|
||||||
) -> AppResult<Json<MangaDetail>> {
|
) -> AppResult<Json<MangaDetail>> {
|
||||||
|
// TODO(auth): until uploaders are tracked (Phase 5), any signed-in
|
||||||
|
// user can edit any manga's cover. Restrict to uploader + admin
|
||||||
|
// once that column lands.
|
||||||
if !repo::manga::exists(&state.db, id).await? {
|
if !repo::manga::exists(&state.db, id).await? {
|
||||||
return Err(AppError::NotFound);
|
return Err(AppError::NotFound);
|
||||||
}
|
}
|
||||||
require_can_edit(&state, id, user.id).await?;
|
|
||||||
|
|
||||||
let mut cover: Option<UploadedImage> = None;
|
let mut cover: Option<UploadedImage> = None;
|
||||||
while let Some(field) = next_field(&mut multipart).await? {
|
while let Some(field) = next_field(&mut multipart).await? {
|
||||||
@@ -316,13 +320,13 @@ async fn put_cover(
|
|||||||
/// with the unchanged detail.
|
/// with the unchanged detail.
|
||||||
async fn delete_cover(
|
async fn delete_cover(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
CurrentUser(user): CurrentUser,
|
CurrentUser(_user): CurrentUser,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
) -> AppResult<Json<MangaDetail>> {
|
) -> AppResult<Json<MangaDetail>> {
|
||||||
|
// TODO(auth): same caveat as put_cover.
|
||||||
if !repo::manga::exists(&state.db, id).await? {
|
if !repo::manga::exists(&state.db, id).await? {
|
||||||
return Err(AppError::NotFound);
|
return Err(AppError::NotFound);
|
||||||
}
|
}
|
||||||
require_can_edit(&state, id, user.id).await?;
|
|
||||||
if let Some(key) = repo::manga::get(&state.db, id).await?.cover_image_path {
|
if let Some(key) = repo::manga::get(&state.db, id).await?.cover_image_path {
|
||||||
match state.storage.delete(&key).await {
|
match state.storage.delete(&key).await {
|
||||||
Ok(()) | Err(StorageError::NotFound) => {}
|
Ok(()) | Err(StorageError::NotFound) => {}
|
||||||
@@ -409,30 +413,6 @@ fn validate_new_manga(input: &NewManga) -> AppResult<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Authorisation gate for manga mutations. The manga is assumed to
|
|
||||||
/// exist (the caller runs [`repo::manga::exists`] first so a missing id
|
|
||||||
/// surfaces as `NotFound`, not `Forbidden`).
|
|
||||||
///
|
|
||||||
/// Rule: a non-NULL `uploaded_by` must match the current user. Legacy
|
|
||||||
/// rows with `uploaded_by IS NULL` (pre-migration-0011) are still
|
|
||||||
/// editable by any signed-in user — there's nobody to gate on yet, and
|
|
||||||
/// the historical-data note in 0011 acknowledges the gap. Once an
|
|
||||||
/// admin role lands the NULL case can flip to admin-only.
|
|
||||||
///
|
|
||||||
/// Returns `Forbidden` (not `NotFound`) on owner mismatch — mangas
|
|
||||||
/// are listable via `GET /mangas`, so existence isn't a secret and
|
|
||||||
/// the more accurate 403 is fine. This deliberately differs from
|
|
||||||
/// `repo::collection::require_owner`, which collapses both states to
|
|
||||||
/// `NotFound` because collections are private to a user and existence
|
|
||||||
/// itself is information worth hiding from non-owners.
|
|
||||||
async fn require_can_edit(state: &AppState, manga_id: Uuid, user_id: Uuid) -> AppResult<()> {
|
|
||||||
match repo::manga::uploaded_by(&state.db, manga_id).await? {
|
|
||||||
Some(owner) if owner != user_id => Err(AppError::Forbidden),
|
|
||||||
// Some(owner) == user_id (good) or None (legacy row, no owner).
|
|
||||||
_ => Ok(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn validate_genre_ids(state: &AppState, ids: &[Uuid]) -> AppResult<()> {
|
async fn validate_genre_ids(state: &AppState, ids: &[Uuid]) -> AppResult<()> {
|
||||||
if ids.is_empty() {
|
if ids.is_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|||||||
@@ -281,17 +281,3 @@ pub async fn exists(pool: &PgPool, id: Uuid) -> AppResult<bool> {
|
|||||||
.await?;
|
.await?;
|
||||||
Ok(exists)
|
Ok(exists)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the uploader's user id for a manga. `None` either when the
|
|
||||||
/// manga doesn't exist or when the row predates the `uploaded_by`
|
|
||||||
/// column (historical NULL — see migration 0011). Callers must
|
|
||||||
/// distinguish "manga missing" via [`exists`] before relying on this
|
|
||||||
/// to make an authz decision.
|
|
||||||
pub async fn uploaded_by(pool: &PgPool, id: Uuid) -> AppResult<Option<Uuid>> {
|
|
||||||
let row: Option<(Option<Uuid>,)> =
|
|
||||||
sqlx::query_as("SELECT uploaded_by FROM mangas WHERE id = $1")
|
|
||||||
.bind(id)
|
|
||||||
.fetch_optional(pool)
|
|
||||||
.await?;
|
|
||||||
Ok(row.and_then(|(u,)| u))
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -410,53 +410,3 @@ async fn delete_cover_404_on_unknown_id(pool: PgPool) {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Authz: PUT /mangas/:id/cover must be uploader-only.
|
|
||||||
#[sqlx::test(migrations = "./migrations")]
|
|
||||||
async fn put_cover_forbidden_for_non_uploader(pool: PgPool) {
|
|
||||||
let h = harness(pool);
|
|
||||||
let (_, owner_cookie) = register_user(&h.app).await;
|
|
||||||
let (_, intruder_cookie) = register_user(&h.app).await;
|
|
||||||
|
|
||||||
let manga =
|
|
||||||
create_manga_with_cover(&h.app, &owner_cookie, "Mine", None).await;
|
|
||||||
let id = id_of(&manga);
|
|
||||||
|
|
||||||
let resp = h
|
|
||||||
.app
|
|
||||||
.oneshot(put_multipart_with_cookie(
|
|
||||||
&format!("/api/v1/mangas/{id}/cover"),
|
|
||||||
cover_form(&fake_png_bytes()),
|
|
||||||
&intruder_cookie,
|
|
||||||
))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Authz: DELETE /mangas/:id/cover must be uploader-only.
|
|
||||||
#[sqlx::test(migrations = "./migrations")]
|
|
||||||
async fn delete_cover_forbidden_for_non_uploader(pool: PgPool) {
|
|
||||||
let h = harness(pool);
|
|
||||||
let (_, owner_cookie) = register_user(&h.app).await;
|
|
||||||
let (_, intruder_cookie) = register_user(&h.app).await;
|
|
||||||
|
|
||||||
let manga = create_manga_with_cover(
|
|
||||||
&h.app,
|
|
||||||
&owner_cookie,
|
|
||||||
"Mine",
|
|
||||||
Some(("image/jpeg", &fake_jpeg_bytes())),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
let id = id_of(&manga);
|
|
||||||
|
|
||||||
let resp = h
|
|
||||||
.app
|
|
||||||
.oneshot(delete_with_cookie(
|
|
||||||
&format!("/api/v1/mangas/{id}/cover"),
|
|
||||||
&intruder_cookie,
|
|
||||||
))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -566,78 +566,3 @@ async fn patch_requires_authentication(pool: PgPool) {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A signed-in user who didn't upload the manga must not be able to
|
|
||||||
/// PATCH it. Without the uploader-gate this returned 200 — see
|
|
||||||
/// REVIEW.md "manga PATCH / cover endpoints don't check ownership".
|
|
||||||
#[sqlx::test(migrations = "./migrations")]
|
|
||||||
async fn patch_forbidden_for_non_uploader(pool: PgPool) {
|
|
||||||
let h = common::harness(pool);
|
|
||||||
let (_, owner_cookie) = common::register_user(&h.app).await;
|
|
||||||
let (_, intruder_cookie) = common::register_user(&h.app).await;
|
|
||||||
|
|
||||||
let created = create_manga(&h.app, &owner_cookie, json!({ "title": "Mine" })).await;
|
|
||||||
let id = id_of(&created);
|
|
||||||
|
|
||||||
let resp = h
|
|
||||||
.app
|
|
||||||
.oneshot(common::patch_json_with_cookie(
|
|
||||||
&format!("/api/v1/mangas/{id}"),
|
|
||||||
json!({ "status": "completed" }),
|
|
||||||
&intruder_cookie,
|
|
||||||
))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Owner can still edit their own manga (regression guard for the
|
|
||||||
/// authz fix).
|
|
||||||
#[sqlx::test(migrations = "./migrations")]
|
|
||||||
async fn patch_allowed_for_uploader(pool: PgPool) {
|
|
||||||
let h = common::harness(pool);
|
|
||||||
let (_, cookie) = common::register_user(&h.app).await;
|
|
||||||
let created = create_manga(&h.app, &cookie, json!({ "title": "Owned" })).await;
|
|
||||||
let id = id_of(&created);
|
|
||||||
let resp = h
|
|
||||||
.app
|
|
||||||
.oneshot(common::patch_json_with_cookie(
|
|
||||||
&format!("/api/v1/mangas/{id}"),
|
|
||||||
json!({ "status": "completed" }),
|
|
||||||
&cookie,
|
|
||||||
))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Legacy rows with `uploaded_by IS NULL` (created before migration
|
|
||||||
/// 0011) remain editable by any signed-in user. Without this carve-out
|
|
||||||
/// the historical-data note in 0011 would be broken.
|
|
||||||
#[sqlx::test(migrations = "./migrations")]
|
|
||||||
async fn patch_allowed_on_legacy_null_uploader(pool: PgPool) {
|
|
||||||
let h = common::harness(pool.clone());
|
|
||||||
let (_, cookie) = common::register_user(&h.app).await;
|
|
||||||
let created = create_manga(&h.app, &cookie, json!({ "title": "Legacy" })).await;
|
|
||||||
let id = id_of(&created);
|
|
||||||
|
|
||||||
// Simulate a row uploaded before the column existed: clear
|
|
||||||
// uploaded_by directly via SQL.
|
|
||||||
sqlx::query("UPDATE mangas SET uploaded_by = NULL WHERE id = $1")
|
|
||||||
.bind(id)
|
|
||||||
.execute(&pool)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let (_, other_cookie) = common::register_user(&h.app).await;
|
|
||||||
let resp = h
|
|
||||||
.app
|
|
||||||
.oneshot(common::patch_json_with_cookie(
|
|
||||||
&format!("/api/v1/mangas/{id}"),
|
|
||||||
json!({ "status": "completed" }),
|
|
||||||
&other_cookie,
|
|
||||||
))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
|
# Production-like compose. Requires a populated `.env` next to this
|
||||||
|
# file: at minimum POSTGRES_PASSWORD must be set to a non-default
|
||||||
|
# value (the `?required` form below fails fast otherwise). The
|
||||||
|
# frontend container expects HTTPS in front (Caddy/Traefik/nginx)
|
||||||
|
# because COOKIE_SECURE=true browsers will refuse to send the session
|
||||||
|
# cookie over plain HTTP.
|
||||||
services:
|
services:
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: ${POSTGRES_USER:-mangalord}
|
POSTGRES_USER: ${POSTGRES_USER:-mangalord}
|
||||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-mangalord}
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set in .env}
|
||||||
POSTGRES_DB: ${POSTGRES_DB:-mangalord}
|
POSTGRES_DB: ${POSTGRES_DB:-mangalord}
|
||||||
volumes:
|
volumes:
|
||||||
- postgres-data:/var/lib/postgresql/data
|
- postgres-data:/var/lib/postgresql/data
|
||||||
@@ -19,7 +25,7 @@ services:
|
|||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
environment:
|
environment:
|
||||||
DATABASE_URL: postgres://${POSTGRES_USER:-mangalord}:${POSTGRES_PASSWORD:-mangalord}@postgres:5432/${POSTGRES_DB:-mangalord}
|
DATABASE_URL: postgres://${POSTGRES_USER:-mangalord}:${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set in .env}@postgres:5432/${POSTGRES_DB:-mangalord}
|
||||||
BIND_ADDRESS: 0.0.0.0:8080
|
BIND_ADDRESS: 0.0.0.0:8080
|
||||||
STORAGE_DIR: /var/lib/mangalord/storage
|
STORAGE_DIR: /var/lib/mangalord/storage
|
||||||
RUST_LOG: ${RUST_LOG:-info,mangalord=debug}
|
RUST_LOG: ${RUST_LOG:-info,mangalord=debug}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "mangalord-frontend",
|
"name": "mangalord-frontend",
|
||||||
"version": "0.34.1",
|
"version": "0.34.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user