From 8b40e8075017a608d8ef6cf96b824ec23d72d5f9 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 10 Jul 2024 13:26:02 +0200 Subject: [PATCH 1/3] Return 401 instead of 403 --- packages/server/src/middleware/authorized.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/middleware/authorized.ts b/packages/server/src/middleware/authorized.ts index ec8a3711cf..b23a9846b7 100644 --- a/packages/server/src/middleware/authorized.ts +++ b/packages/server/src/middleware/authorized.ts @@ -96,7 +96,7 @@ const authorized = } if (!ctx.user) { - return ctx.throw(403, "No user info found") + return ctx.throw(401, "No user info found") } // get the resource roles @@ -148,7 +148,7 @@ const authorized = // check authenticated if (!ctx.isAuthenticated) { - return ctx.throw(403, "Session not authenticated") + return ctx.throw(401, "Session not authenticated") } // check general builder stuff, this middleware is a good way From 3f5161aaf7fbf2508f5ad195dc870151760906c1 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 10 Jul 2024 13:36:07 +0200 Subject: [PATCH 2/3] Fix tests --- packages/server/src/api/routes/tests/permissions.spec.ts | 8 ++++---- packages/server/src/api/routes/tests/viewV2.spec.ts | 4 ++-- packages/server/src/middleware/tests/authorized.spec.ts | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/routes/tests/permissions.spec.ts b/packages/server/src/api/routes/tests/permissions.spec.ts index bee794da47..838e1aca0b 100644 --- a/packages/server/src/api/routes/tests/permissions.spec.ts +++ b/packages/server/src/api/routes/tests/permissions.spec.ts @@ -203,7 +203,7 @@ describe("/permission", () => { // replicate changes before checking permissions await config.publish() - await config.api.viewV2.publicSearch(view.id, undefined, { status: 403 }) + await config.api.viewV2.publicSearch(view.id, undefined, { status: 401 }) }) it("should ignore the view permissions if the flag is not on", async () => { @@ -221,7 +221,7 @@ describe("/permission", () => { await config.publish() await config.api.viewV2.publicSearch(view.id, undefined, { - status: 403, + status: 401, }) }) @@ -250,8 +250,8 @@ describe("/permission", () => { .send(basicRow(table._id)) .set(config.publicHeaders()) .expect("Content-Type", /json/) - .expect(403) - expect(res.status).toEqual(403) + .expect(401) + expect(res.status).toEqual(401) }) }) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index ba044acf81..e9853e5dff 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -1490,7 +1490,7 @@ describe.each([ it("does not allow public users to fetch by default", async () => { await config.publish() await config.api.viewV2.publicSearch(view.id, undefined, { - status: 403, + status: 401, }) }) @@ -1534,7 +1534,7 @@ describe.each([ await config.publish() await config.api.viewV2.publicSearch(view.id, undefined, { - status: 403, + status: 401, }) }) }) diff --git a/packages/server/src/middleware/tests/authorized.spec.ts b/packages/server/src/middleware/tests/authorized.spec.ts index 79cfeca54e..e8fe8bd914 100644 --- a/packages/server/src/middleware/tests/authorized.spec.ts +++ b/packages/server/src/middleware/tests/authorized.spec.ts @@ -105,7 +105,7 @@ describe("Authorization middleware", () => { it("throws when no user data is present in context", async () => { await config.executeMiddleware() - expect(config.throw).toHaveBeenCalledWith(403, "No user info found") + expect(config.throw).toHaveBeenCalledWith(401, "No user info found") }) it("passes on to next() middleware if user is an admin", async () => { @@ -157,7 +157,7 @@ describe("Authorization middleware", () => { await config.executeMiddleware() expect(config.throw).toHaveBeenCalledWith( - 403, + 401, "Session not authenticated" ) }) From c1eafe5b284c353536ef2a9182ca5d3f5b068d2b Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 10 Jul 2024 14:05:21 +0200 Subject: [PATCH 3/3] Fix --- packages/server/src/api/routes/tests/utilities/TestFunctions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/utilities/TestFunctions.ts b/packages/server/src/api/routes/tests/utilities/TestFunctions.ts index 27d8592849..15a3ede39b 100644 --- a/packages/server/src/api/routes/tests/utilities/TestFunctions.ts +++ b/packages/server/src/api/routes/tests/utilities/TestFunctions.ts @@ -151,7 +151,7 @@ export const checkPermissionsEndpoint = async ({ await exports .createRequest(config.request, method, url, body) .set(failHeader) - .expect(403) + .expect(401) } export const getDB = () => {