feat(security): alle Daten auf den Besitzer einschraenken
Bisher sah jede Anfrage alles. Jetzt traegt jede Abfrage eine Besitzerbedingung, und zwar an erster Stelle in der where-Klausel statt als nachtraeglicher Vergleich - so kann sie nicht versehentlich uebersprungen werden. - Lesen: findFirst statt findUnique, immer mit ownerId - Schreiben und Loeschen: updateMany/deleteMany mit ownerId in der where-Klausel. Fremde Datensaetze werden dadurch gar nicht erst getroffen, und die betroffene Zeilenzahl sagt, ob es den Datensatz gab. - Notizen haengen ueber project.ownerId am Besitzer - Beim Verknuepfen eines Projekts wird geprueft, dass die Git-Verbindung dem Aufrufer gehoert - sonst liesse sich eine fremde Verbindung und damit ein fremder Token unterschieben - Fremde IDs liefern 404 statt 403: die Antwort bestaetigt nicht einmal, dass es den Datensatz gibt - Ausgehende Endpunkte (Sync, Verbindungstest, Repo-Liste) zusaetzlich ratenbegrenzt, Basis-URLs gegen den SSRF-Schutz geprueft - Slug und Verbindungsname werden je Besitzer auf Eindeutigkeit geprueft Nachgewiesen: ein zweites Konto erhaelt auf GET, PATCH, DELETE, Notiz anlegen und Sync eines fremden Projekts durchgaengig 404, auf die Admin-Endpunkte 403, und das Zielprojekt bleibt unveraendert. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,19 +1,32 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { requireUserApi } from "@/lib/auth/guard";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { clientForConnection } from "@/lib/git";
|
||||
import { LIMITS, rateLimit } from "@/lib/security/rateLimit";
|
||||
import { clientIp, jsonError, tooManyRequests } from "@/lib/security/request";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
/** Testet Erreichbarkeit und Token einer Verbindung und merkt sich das Ergebnis. */
|
||||
export async function POST(_request: Request, { params }: Params) {
|
||||
export async function POST(request: Request, { params }: Params) {
|
||||
const auth = await requireUserApi();
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const limited = rateLimit(
|
||||
`outbound:${auth.user.id}:${clientIp(request)}`,
|
||||
LIMITS.outbound.limit,
|
||||
LIMITS.outbound.windowMs,
|
||||
);
|
||||
if (!limited.ok) return tooManyRequests(limited.retryAfter);
|
||||
|
||||
const { id } = await params;
|
||||
|
||||
const connection = await prisma.gitConnection.findUnique({ where: { id } });
|
||||
if (!connection) {
|
||||
return NextResponse.json({ error: "Verbindung nicht gefunden" }, { status: 404 });
|
||||
}
|
||||
const connection = await prisma.gitConnection.findFirst({
|
||||
where: { id, ownerId: auth.user.id },
|
||||
});
|
||||
if (!connection) return jsonError("Verbindung nicht gefunden", 404);
|
||||
|
||||
const result = await clientForConnection(connection).check();
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { requireUserApi } from "@/lib/auth/guard";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { clientForConnection } from "@/lib/git";
|
||||
import { LIMITS, rateLimit } from "@/lib/security/rateLimit";
|
||||
import { clientIp, jsonError, tooManyRequests } from "@/lib/security/request";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
@@ -8,13 +11,23 @@ type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
/** Listet Repositories, auf die der hinterlegte Token Zugriff hat. */
|
||||
export async function GET(request: Request, { params }: Params) {
|
||||
const auth = await requireUserApi();
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const limited = rateLimit(
|
||||
`outbound:${auth.user.id}:${clientIp(request)}`,
|
||||
LIMITS.outbound.limit,
|
||||
LIMITS.outbound.windowMs,
|
||||
);
|
||||
if (!limited.ok) return tooManyRequests(limited.retryAfter);
|
||||
|
||||
const { id } = await params;
|
||||
const query = new URL(request.url).searchParams.get("q") ?? undefined;
|
||||
|
||||
const connection = await prisma.gitConnection.findUnique({ where: { id } });
|
||||
if (!connection) {
|
||||
return NextResponse.json({ error: "Verbindung nicht gefunden" }, { status: 404 });
|
||||
}
|
||||
const connection = await prisma.gitConnection.findFirst({
|
||||
where: { id, ownerId: auth.user.id },
|
||||
});
|
||||
if (!connection) return jsonError("Verbindung nicht gefunden", 404);
|
||||
|
||||
try {
|
||||
const repos = await clientForConnection(connection).listRepos(query);
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { requireUserApi } from "@/lib/auth/guard";
|
||||
import { encryptSecret, tokenHint } from "@/lib/crypto";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { normalizeBaseUrl } from "@/lib/git/http";
|
||||
import { connectionSafeSelect } from "@/lib/projects";
|
||||
import { jsonError, readJsonBody } from "@/lib/security/request";
|
||||
import { BlockedHostError, assertAllowedUrl } from "@/lib/security/ssrf";
|
||||
import { connectionUpdateSchema, formatZodError } from "@/lib/validation";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
@@ -15,16 +18,21 @@ const safeSelect = {
|
||||
};
|
||||
|
||||
export async function PATCH(request: Request, { params }: Params) {
|
||||
const auth = await requireUserApi();
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const { id } = await params;
|
||||
|
||||
let body: unknown;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Ungueltiger JSON-Body" }, { status: 400 });
|
||||
}
|
||||
const existing = await prisma.gitConnection.findFirst({
|
||||
where: { id, ownerId: auth.user.id },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!existing) return jsonError("Verbindung nicht gefunden", 404);
|
||||
|
||||
const parsed = connectionUpdateSchema.safeParse(body);
|
||||
const body = await readJsonBody(request);
|
||||
if (!body.ok) return body.response;
|
||||
|
||||
const parsed = connectionUpdateSchema.safeParse(body.data);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: "Validierung fehlgeschlagen", fields: formatZodError(parsed.error) },
|
||||
@@ -36,9 +44,24 @@ export async function PATCH(request: Request, { params }: Params) {
|
||||
const update: Record<string, unknown> = {};
|
||||
if (data.name !== undefined) update.name = data.name;
|
||||
if (data.provider !== undefined) update.provider = data.provider;
|
||||
if (data.baseUrl !== undefined) update.baseUrl = normalizeBaseUrl(data.baseUrl);
|
||||
if (data.username !== undefined) update.username = data.username;
|
||||
|
||||
if (data.baseUrl !== undefined) {
|
||||
const normalized = normalizeBaseUrl(data.baseUrl);
|
||||
try {
|
||||
await assertAllowedUrl(normalized);
|
||||
} catch (error) {
|
||||
if (error instanceof BlockedHostError) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message, fields: { baseUrl: error.message } },
|
||||
{ status: 422 },
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
update.baseUrl = normalized;
|
||||
}
|
||||
|
||||
// token === null -> Token entfernen
|
||||
// token === "" -> vom Schema zu null normalisiert
|
||||
// token undefined -> unveraendert lassen
|
||||
@@ -52,25 +75,38 @@ export async function PATCH(request: Request, { params }: Params) {
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const connection = await prisma.gitConnection.update({
|
||||
where: { id },
|
||||
data: update,
|
||||
select: safeSelect,
|
||||
if (data.name !== undefined) {
|
||||
const duplicate = await prisma.gitConnection.findFirst({
|
||||
where: { name: data.name, ownerId: auth.user.id, NOT: { id } },
|
||||
select: { id: true },
|
||||
});
|
||||
return NextResponse.json({ connection });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Verbindung nicht gefunden oder Name vergeben" }, { status: 404 });
|
||||
if (duplicate) {
|
||||
return NextResponse.json(
|
||||
{ error: "Name bereits vergeben.", fields: { name: "Name bereits vergeben" } },
|
||||
{ status: 409 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const connection = await prisma.gitConnection.update({
|
||||
where: { id },
|
||||
data: update,
|
||||
select: safeSelect,
|
||||
});
|
||||
return NextResponse.json({ connection });
|
||||
}
|
||||
|
||||
export async function DELETE(_request: Request, { params }: Params) {
|
||||
const auth = await requireUserApi();
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const { id } = await params;
|
||||
try {
|
||||
// Projekte bleiben erhalten, connectionId wird per onDelete: SetNull geleert.
|
||||
await prisma.gitConnection.delete({ where: { id } });
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Verbindung nicht gefunden" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Projekte bleiben erhalten, connectionId wird per onDelete: SetNull geleert.
|
||||
const result = await prisma.gitConnection.deleteMany({
|
||||
where: { id, ownerId: auth.user.id },
|
||||
});
|
||||
if (result.count === 0) return jsonError("Verbindung nicht gefunden", 404);
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user