-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
143 lines (126 loc) ยท 3.8 KB
/
route.ts
File metadata and controls
143 lines (126 loc) ยท 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { NextRequest, NextResponse } from "next/server";
import { hashAccessTokenSecret } from "@/lib/access-token-hash";
import { decryptToUtf8 } from "@/lib/crypto";
import { prisma } from "@/lib/prisma";
import {
assertSafeRelativePath,
assertValidCollectionSlug,
fullObjectKey,
getBucket,
} from "@/lib/paths";
import { getObjectBuffer } from "@/lib/s3";
import { tokenCreatorHasCollectionAccess } from "@/server/access/access-token-runtime";
function parseBearer(req: NextRequest): string | null {
const h = req.headers.get("authorization");
if (!h?.toLowerCase().startsWith("bearer ")) return null;
const t = h.slice(7).trim();
return t || null;
}
function isNotFoundError(err: unknown): boolean {
const e = err as {
name?: string;
Code?: string;
$metadata?: { httpStatusCode?: number };
};
return (
e.name === "NoSuchKey" ||
e.Code === "NoSuchKey" ||
e.name === "NotFound" ||
e.$metadata?.httpStatusCode === 404
);
}
export async function GET(req: NextRequest) {
try {
getBucket();
} catch {
return NextResponse.json(
{ error: "Server misconfigured" },
{ status: 500 },
);
}
const bearer = parseBearer(req);
if (!bearer) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
let tokenLookup: string;
try {
tokenLookup = hashAccessTokenSecret(bearer);
} catch {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const secretParam = req.nextUrl.searchParams.get("secret")?.trim();
if (!secretParam) {
return NextResponse.json({ error: "Missing secret" }, { status: 400 });
}
const slash = secretParam.indexOf("/");
if (slash <= 0) {
return NextResponse.json({ error: "Invalid secret path" }, { status: 400 });
}
const slug = secretParam.slice(0, slash);
const relativePath = secretParam.slice(slash + 1);
if (!relativePath) {
return NextResponse.json({ error: "Invalid secret path" }, { status: 400 });
}
try {
assertValidCollectionSlug(slug);
assertSafeRelativePath(relativePath);
} catch {
return NextResponse.json({ error: "Invalid secret path" }, { status: 400 });
}
const row = await prisma.accessToken.findUnique({
where: { tokenLookup },
include: {
collections: true,
createdBy: {
select: { email: true },
},
},
});
if (!row) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const collection = await prisma.collection.findUnique({
where: { slug },
select: {
id: true,
createdById: true,
accessGrants: {
where: { userId: row.createdById },
select: { userId: true },
},
},
});
if (!collection) {
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
const allowed = row.collections.some((c) => c.collectionId === collection.id);
if (!allowed) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
const creatorHasAccess = tokenCreatorHasCollectionAccess({
creatorUserId: row.createdById,
creatorEmail: row.createdBy.email,
collectionCreatedById: collection.createdById,
hasDirectGrant: collection.accessGrants.length > 0,
});
if (!creatorHasAccess) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const objectKey = fullObjectKey(slug, relativePath);
let body: Buffer;
try {
body = await getObjectBuffer(objectKey);
} catch (err: unknown) {
if (isNotFoundError(err)) {
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
let content: string;
try {
content = decryptToUtf8(body);
} catch {
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
return NextResponse.json({ content });
}