diff --git a/services/backend/src/tests.rs b/services/backend/src/tests.rs index 86c543d..4d0ad66 100644 --- a/services/backend/src/tests.rs +++ b/services/backend/src/tests.rs @@ -632,6 +632,58 @@ async fn export_and_delete_account(pool: PgPool) { assert_eq!(app.get("/v1/me", Some(&bela)).await.status, StatusCode::OK); } +/// Only these answer without a session: login and sign-up, the public +/// config and catalog, shared links, invitation pages, the waitlist and +/// unsubscribing. Every other route (charts, calculations, analyses) needs a +/// logged-in user, so other apps cannot use them. +const PUBLIC_ROUTES: [&str; 14] = [ + "/healthz", + "/v1/config", + "/v1/packages", + "/v1/auth/register", + "/v1/auth/login", + "/v1/auth/google", + "/v1/auth/verify-email", + "/v1/auth/password-reset", + "/v1/auth/password-reset/confirm", + "/v1/public/shares/{slug}", + "/v1/public/invites/{token}", + "/v1/public/invitations/unsubscribe", + "/v1/public/invitations/{token}", + "/v1/public/waitlist", +]; + +#[sqlx::test(migrator = "db::MIGRATOR")] +async fn everything_else_needs_login(pool: PgPool) { + let app = TestApp::new(pool).await; + let source = include_str!("routes/mod.rs"); + let id = uuid::Uuid::now_v7().to_string(); + let mut checked = 0; + for line in source.lines().map(str::trim).filter(|l| l.starts_with(".route(\"")) { + let path = line.split('"').nth(1).unwrap(); + if PUBLIC_ROUTES.contains(&path) { + continue; + } + let uri = path + .split('/') + .map(|seg| if seg.starts_with('{') { id.as_str() } else { seg }) + .collect::>() + .join("/"); + for (name, method) in + [("get(", "GET"), ("post(", "POST"), ("patch(", "PATCH"), ("put(", "PUT"), ("delete(", "DELETE")] + { + if !line.contains(name) { + continue; + } + let body = (method != "GET" && method != "DELETE").then(|| json!({})); + let r = app.call(method, &uri, None, body).await; + assert_eq!(r.status, StatusCode::UNAUTHORIZED, "{method} {uri} answers without login: {}", r.body); + checked += 1; + } + } + assert!(checked > 40, "{checked}"); +} + #[sqlx::test(migrator = "db::MIGRATOR")] async fn public_config_and_health(pool: PgPool) { let app = TestApp::new(pool).await; diff --git a/services/frontend/src/app/api/geo/[kind]/route.ts b/services/frontend/src/app/api/geo/[kind]/route.ts index c5517dc..0a3ca16 100644 --- a/services/frontend/src/app/api/geo/[kind]/route.ts +++ b/services/frontend/src/app/api/geo/[kind]/route.ts @@ -1,13 +1,19 @@ -// Country list and place search for the birth data form (public data from -// the horoscope-api; only these two endpoints are passed through). +// Country list and place search for the birth data form (from the +// horoscope-api; only these two endpoints are passed through, and only for +// logged-in users, so other apps cannot use the place search). import type { NextRequest } from "next/server"; +import { getMe } from "@/lib/server"; + const HOROSCOPE_API_URL = (process.env.HOROSCOPE_API_URL ?? "http://localhost:8080").replace(/\/$/, ""); const ALLOWED = new Set(["countries", "places"]); export async function GET(req: NextRequest, ctx: { params: Promise<{ kind: string }> }) { const { kind } = await ctx.params; if (!ALLOWED.has(kind)) return Response.json({ error: { code: "not_found", message: "not found" } }, { status: 404 }); + if (!(await getMe())) { + return Response.json({ error: { code: "unauthorized", message: "Jelentkezz be." } }, { status: 401 }); + } try { const res = await fetch(`${HOROSCOPE_API_URL}/v1/${kind}${req.nextUrl.search}`, { // The country list rarely changes.