Place search only for logged-in users; test that routes need login
- /api/geo/countries and /api/geo/places (the frontend's pass-through to the horoscope-api) now answer 401 without a session, so other apps cannot use the place search. - New backend test: every route in routes/mod.rs except the listed public ones (login, config, catalog, shared links, invitation pages, waitlist, unsubscribe) answers 401 without a session. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -632,6 +632,58 @@ async fn export_and_delete_account(pool: PgPool) {
|
|||||||
assert_eq!(app.get("/v1/me", Some(&bela)).await.status, StatusCode::OK);
|
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::<Vec<_>>()
|
||||||
|
.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")]
|
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||||
async fn public_config_and_health(pool: PgPool) {
|
async fn public_config_and_health(pool: PgPool) {
|
||||||
let app = TestApp::new(pool).await;
|
let app = TestApp::new(pool).await;
|
||||||
|
|||||||
@@ -1,13 +1,19 @@
|
|||||||
// Country list and place search for the birth data form (public data from
|
// Country list and place search for the birth data form (from the
|
||||||
// the horoscope-api; only these two endpoints are passed through).
|
// 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 type { NextRequest } from "next/server";
|
||||||
|
|
||||||
|
import { getMe } from "@/lib/server";
|
||||||
|
|
||||||
const HOROSCOPE_API_URL = (process.env.HOROSCOPE_API_URL ?? "http://localhost:8080").replace(/\/$/, "");
|
const HOROSCOPE_API_URL = (process.env.HOROSCOPE_API_URL ?? "http://localhost:8080").replace(/\/$/, "");
|
||||||
const ALLOWED = new Set(["countries", "places"]);
|
const ALLOWED = new Set(["countries", "places"]);
|
||||||
|
|
||||||
export async function GET(req: NextRequest, ctx: { params: Promise<{ kind: string }> }) {
|
export async function GET(req: NextRequest, ctx: { params: Promise<{ kind: string }> }) {
|
||||||
const { kind } = await ctx.params;
|
const { kind } = await ctx.params;
|
||||||
if (!ALLOWED.has(kind)) return Response.json({ error: { code: "not_found", message: "not found" } }, { status: 404 });
|
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 {
|
try {
|
||||||
const res = await fetch(`${HOROSCOPE_API_URL}/v1/${kind}${req.nextUrl.search}`, {
|
const res = await fetch(`${HOROSCOPE_API_URL}/v1/${kind}${req.nextUrl.search}`, {
|
||||||
// The country list rarely changes.
|
// The country list rarely changes.
|
||||||
|
|||||||
Reference in New Issue
Block a user