Skip to content

Commit e977d06

Browse files
authored
fix: IDOR patch (#982)
1 parent f46681b commit e977d06

File tree

1 file changed

+10
-4
lines changed
  • packages/backend/src/api/v1/auth

1 file changed

+10
-4
lines changed

packages/backend/src/api/v1/auth/utils.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ async function checkApiKey(ctx: Context, key: string) {
140140
}
141141

142142
export async function authMiddleware(ctx: Context, next: Next) {
143-
ctx.state.projectId = ctx.request?.query?.projectId as string;
144-
145143
const isPublicRoute = publicRoutes.some((route) =>
146144
typeof route === "string" ? route === ctx.path : route.test(ctx.path),
147145
);
@@ -172,12 +170,12 @@ export async function authMiddleware(ctx: Context, next: Next) {
172170
ctx.state.privateKey = true;
173171
}
174172
} else {
175-
// Check if JWT is valid
176173
try {
177174
if (!bearer) {
178175
throw new Error("No bearer token provided.");
179176
}
180177
const { payload } = await verifyJWT<SessionData>(key);
178+
ctx.state.projectId = ctx.request?.query?.projectId as string;
181179
ctx.state.userId = payload.userId;
182180
ctx.state.orgId = payload.orgId;
183181

@@ -194,9 +192,10 @@ export async function authMiddleware(ctx: Context, next: Next) {
194192
const [project] = await sql`
195193
select * from account_project where account_id = ${ctx.state.userId} and project_id = ${ctx.state.projectId}
196194
`;
195+
console.log("Project", project);
197196

198197
if (!project) {
199-
throw new Error("Project not found");
198+
throw new Error("Unauthorized access to project");
200199
}
201200
}
202201
} catch (error) {
@@ -210,6 +209,13 @@ export async function authMiddleware(ctx: Context, next: Next) {
210209
ctx.body = { message: "Invalid access token" };
211210
return;
212211
}
212+
213+
if (error?.message === "Unauthorized access to project") {
214+
ctx.status = 401;
215+
ctx.body = { message: "Unauthorized access to project" };
216+
return;
217+
}
218+
return;
213219
}
214220
}
215221

0 commit comments

Comments
 (0)