Compare commits
1 Commits
chore/cont
...
bugfix/log
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4863219cf6 |
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mangalord"
|
name = "mangalord"
|
||||||
version = "0.34.0"
|
version = "0.34.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
default-run = "mangalord"
|
default-run = "mangalord"
|
||||||
|
|
||||||
|
|||||||
@@ -19,49 +19,12 @@ COPY migrations ./migrations
|
|||||||
RUN touch src/main.rs src/lib.rs && cargo build --locked --release
|
RUN touch src/main.rs src/lib.rs && cargo build --locked --release
|
||||||
|
|
||||||
FROM debian:bookworm-slim
|
FROM debian:bookworm-slim
|
||||||
# `curl` is for the container HEALTHCHECK; `ca-certificates` is for
|
|
||||||
# outbound HTTPS (crawler covers/pages).
|
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get install -y --no-install-recommends ca-certificates curl \
|
&& apt-get install -y --no-install-recommends ca-certificates \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Non-root runtime user. The API binary doesn't need any root
|
|
||||||
# privilege; the crawler daemon's Chromium launcher uses --no-sandbox
|
|
||||||
# precisely because user-namespace sandboxing is fragile, so dropping
|
|
||||||
# privileges costs nothing operationally and shrinks the blast radius
|
|
||||||
# of any RCE.
|
|
||||||
ARG APP_UID=10001
|
|
||||||
ARG APP_GID=10001
|
|
||||||
RUN groupadd --system --gid ${APP_GID} app \
|
|
||||||
&& useradd --system --uid ${APP_UID} --gid app --home-dir /home/app --create-home --shell /usr/sbin/nologin app
|
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=builder /app/target/release/mangalord /usr/local/bin/mangalord
|
COPY --from=builder /app/target/release/mangalord /usr/local/bin/mangalord
|
||||||
COPY --from=builder /app/migrations /app/migrations
|
COPY --from=builder /app/migrations /app/migrations
|
||||||
|
|
||||||
ENV STORAGE_DIR=/var/lib/mangalord/storage
|
ENV STORAGE_DIR=/var/lib/mangalord/storage
|
||||||
# Pre-create the storage dir so the entrypoint doesn't need to
|
|
||||||
# mkdir-as-root and so the named volume mount inherits the right
|
|
||||||
# ownership.
|
|
||||||
#
|
|
||||||
# UPGRADE NOTE for operators: if you're moving from an older image
|
|
||||||
# that ran as root, the existing `storage-data` volume has files owned
|
|
||||||
# by UID 0 and the new UID-10001 user can't write them. Run once
|
|
||||||
# before the upgrade:
|
|
||||||
# docker compose run --rm --user 0 backend \
|
|
||||||
# chown -R 10001:10001 /var/lib/mangalord/storage
|
|
||||||
# (Postgres is unaffected — that image's `postgres` user UID hasn't
|
|
||||||
# changed.)
|
|
||||||
RUN mkdir -p ${STORAGE_DIR} \
|
|
||||||
&& chown -R app:app ${STORAGE_DIR} /app /home/app
|
|
||||||
|
|
||||||
USER app
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
# `--start-period` is generous because first boot runs sqlx::migrate
|
|
||||||
# against postgres which can take a few seconds; subsequent restarts
|
|
||||||
# are sub-second.
|
|
||||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
|
|
||||||
CMD curl -fsS http://localhost:8080/api/v1/health > /dev/null || exit 1
|
|
||||||
|
|
||||||
CMD ["mangalord"]
|
CMD ["mangalord"]
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
//! expire naturally rather than being explicitly invalidated, so other
|
//! expire naturally rather than being explicitly invalidated, so other
|
||||||
//! devices keep their existing logins).
|
//! devices keep their existing logins).
|
||||||
|
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
use axum::extract::{Path, State};
|
use axum::extract::{Path, State};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
@@ -102,9 +104,15 @@ async fn login(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let user = repo::user::find_by_username(&state.db, username)
|
let user = repo::user::find_by_username(&state.db, username).await?;
|
||||||
.await?
|
let Some(user) = user else {
|
||||||
.ok_or(AppError::Unauthenticated)?;
|
// No such user. Run argon2 against a stable dummy hash so the
|
||||||
|
// response time matches the wrong-password branch — otherwise
|
||||||
|
// an attacker can enumerate usernames by timing the no-user
|
||||||
|
// 401 against the wrong-password 401.
|
||||||
|
let _ = verify_password(&input.password, dummy_password_hash());
|
||||||
|
return Err(AppError::Unauthenticated);
|
||||||
|
};
|
||||||
if !verify_password(&input.password, &user.password_hash) {
|
if !verify_password(&input.password, &user.password_hash) {
|
||||||
return Err(AppError::Unauthenticated);
|
return Err(AppError::Unauthenticated);
|
||||||
}
|
}
|
||||||
@@ -113,6 +121,21 @@ async fn login(
|
|||||||
Ok((StatusCode::OK, jar, Json(AuthResponse { user })))
|
Ok((StatusCode::OK, jar, Json(AuthResponse { user })))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Lazily-computed argon2 hash used to equalise login response time
|
||||||
|
/// across the "no such user" and "wrong password" branches. Computing
|
||||||
|
/// it once (on the first login of the process) is enough — the hash is
|
||||||
|
/// never compared against a real password, only used to force argon2
|
||||||
|
/// to do the same amount of work it would for a real verify.
|
||||||
|
fn dummy_password_hash() -> &'static str {
|
||||||
|
static DUMMY: OnceLock<String> = OnceLock::new();
|
||||||
|
DUMMY
|
||||||
|
.get_or_init(|| {
|
||||||
|
crate::auth::password::hash_password("login-timing-equaliser")
|
||||||
|
.expect("hash_password on a fixed input cannot fail")
|
||||||
|
})
|
||||||
|
.as_str()
|
||||||
|
}
|
||||||
|
|
||||||
async fn logout(
|
async fn logout(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
jar: CookieJar,
|
jar: CookieJar,
|
||||||
|
|||||||
@@ -567,6 +567,91 @@ async fn user_a_cannot_delete_user_b_token(pool: PgPool) {
|
|||||||
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Username enumeration via login response time: an attacker probes
|
||||||
|
/// for valid usernames by measuring how long /auth/login takes. Before
|
||||||
|
/// the equalisation fix, the no-user branch returned 401 in <1 ms
|
||||||
|
/// while the wrong-password branch took ~50-100 ms (the argon2 verify
|
||||||
|
/// cost). This test asserts the no-user branch now spends at least
|
||||||
|
/// some meaningful fraction of the wrong-password branch's time.
|
||||||
|
///
|
||||||
|
/// Tolerance is intentionally loose so CI variance doesn't flap the
|
||||||
|
/// test. The unequalised gap is large enough (~50x) that even a noisy
|
||||||
|
/// CI run with a 5x slack still catches it.
|
||||||
|
#[sqlx::test(migrations = "./migrations")]
|
||||||
|
async fn login_no_user_branch_runs_argon2_for_timing_equalisation(pool: PgPool) {
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
let h = common::harness(pool);
|
||||||
|
|
||||||
|
// Register the victim user so the wrong-password branch has a real
|
||||||
|
// argon2 hash to verify against.
|
||||||
|
let _ = h
|
||||||
|
.app
|
||||||
|
.clone()
|
||||||
|
.oneshot(common::post_json(
|
||||||
|
"/api/v1/auth/register",
|
||||||
|
json!({ "username": "victim", "password": "hunter2hunter2" }),
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Warm-up: first login of the process initialises the dummy hash
|
||||||
|
// lazily. Skip that cost when measuring.
|
||||||
|
let _ = h
|
||||||
|
.app
|
||||||
|
.clone()
|
||||||
|
.oneshot(common::post_json(
|
||||||
|
"/api/v1/auth/login",
|
||||||
|
json!({ "username": "victim", "password": "wrong" }),
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let _ = h
|
||||||
|
.app
|
||||||
|
.clone()
|
||||||
|
.oneshot(common::post_json(
|
||||||
|
"/api/v1/auth/login",
|
||||||
|
json!({ "username": "ghost", "password": "wrong" }),
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Median-of-N is more stable than a single sample.
|
||||||
|
async fn sample_min(
|
||||||
|
app: &axum::Router,
|
||||||
|
username: &str,
|
||||||
|
n: u32,
|
||||||
|
) -> std::time::Duration {
|
||||||
|
let mut samples = Vec::with_capacity(n as usize);
|
||||||
|
for _ in 0..n {
|
||||||
|
let req = common::post_json(
|
||||||
|
"/api/v1/auth/login",
|
||||||
|
json!({ "username": username, "password": "wrong-guess" }),
|
||||||
|
);
|
||||||
|
let t = Instant::now();
|
||||||
|
let resp = app.clone().oneshot(req).await.unwrap();
|
||||||
|
let d = t.elapsed();
|
||||||
|
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
||||||
|
samples.push(d);
|
||||||
|
}
|
||||||
|
// Use the minimum: it's the floor that argon2 takes, robust
|
||||||
|
// against unrelated stalls (DB connection acquisition, etc.).
|
||||||
|
*samples.iter().min().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
let wrong_pwd = sample_min(&h.app, "victim", 3).await;
|
||||||
|
let no_user = sample_min(&h.app, "ghost", 3).await;
|
||||||
|
|
||||||
|
// 5x slack: argon2 dominates both branches, so they should be
|
||||||
|
// within an order of magnitude. Unequalised, no_user would be
|
||||||
|
// ~50-100x faster. Asserting "no_user >= wrong_pwd / 5" catches
|
||||||
|
// the bug without being flaky in CI.
|
||||||
|
assert!(
|
||||||
|
no_user * 5 >= wrong_pwd,
|
||||||
|
"login timing leaks user existence: no_user={no_user:?}, wrong_pwd={wrong_pwd:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[sqlx::test(migrations = "./migrations")]
|
#[sqlx::test(migrations = "./migrations")]
|
||||||
async fn delete_unknown_token_is_404(pool: PgPool) {
|
async fn delete_unknown_token_is_404(pool: PgPool) {
|
||||||
let h = common::harness(pool);
|
let h = common::harness(pool);
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
FROM node:22-alpine AS builder
|
FROM node:22-alpine AS builder
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY package.json package-lock.json* ./
|
COPY package.json package-lock.json* ./
|
||||||
# `npm ci` installs the locked versions exactly; `npm install` would
|
RUN npm install
|
||||||
# silently rewrite package-lock.json mid-build. CI (.gitea/workflows)
|
|
||||||
# also uses `npm ci`, so this keeps the image build deterministic and
|
|
||||||
# matches what the test job validated.
|
|
||||||
RUN npm ci
|
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
@@ -14,20 +10,8 @@ WORKDIR /app
|
|||||||
ENV NODE_ENV=production
|
ENV NODE_ENV=production
|
||||||
ENV HOST=0.0.0.0
|
ENV HOST=0.0.0.0
|
||||||
ENV PORT=3000
|
ENV PORT=3000
|
||||||
|
COPY --from=builder /app/build ./build
|
||||||
# node:22-alpine ships a `node` user (UID 1000); use it instead of
|
COPY --from=builder /app/node_modules ./node_modules
|
||||||
# running the SvelteKit server as root.
|
COPY --from=builder /app/package.json ./
|
||||||
COPY --from=builder --chown=node:node /app/build ./build
|
|
||||||
COPY --from=builder --chown=node:node /app/node_modules ./node_modules
|
|
||||||
COPY --from=builder --chown=node:node /app/package.json ./
|
|
||||||
|
|
||||||
USER node
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
# Alpine's busybox `wget` is the canonical lightweight HTTP probe.
|
|
||||||
# `--spider` doesn't follow redirects; `node build` serves a 200 on
|
|
||||||
# `/` for the homepage so this works without a dedicated /health.
|
|
||||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
|
||||||
CMD wget -q --spider http://localhost:3000/ || exit 1
|
|
||||||
|
|
||||||
CMD ["node", "build"]
|
CMD ["node", "build"]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "mangalord-frontend",
|
"name": "mangalord-frontend",
|
||||||
"version": "0.34.0",
|
"version": "0.34.1",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user