-
Notifications
You must be signed in to change notification settings - Fork 199
fix(auth): enforce ADMIN on github/token and MEMBER/ADMIN on write routes #1525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Satyam296
wants to merge
14
commits into
traceroot-ai:main
Choose a base branch
from
Satyam296:fix/viewer-can-mint-github-token
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4cf2c6a
fix(auth): enforce ADMIN role on github/token and MEMBER/ADMIN on wri…
Satyam296 a506960
fix(ai-assistant): surface 403 on session delete, hide trash for non-…
Satyam296 07fd8c4
test(ai-assistant): cover canDelete prop and delete error handling fo…
Satyam296 2abdf06
fix(roles): address round-2 review on viewer-can-mint-github-token
Satyam296 83032a5
fix(detectors): gate Delete action on ADMIN only, not MEMBER
Satyam296 933f54f
fix(roles): default isMember to false while workspace is loading
Satyam296 25b29eb
test(detectors): cover isError display paths to fix diff-cover gate
Satyam296 de53792
Merge remote-tracking branch 'upstream/main' into fix/viewer-can-mint…
Satyam296 83d51a9
chore: retrigger CI (Coverage Gate failed on a transient artifact-dow…
Satyam296 911c53c
test(detectors): cover the isAdmin gate on the row actions Delete button
Satyam296 8d607c1
fix(ai-assistant): surface session-creation failures as an error bubble
Satyam296 156d831
test: close coverage gaps found during full PR verification
Satyam296 76a982d
Merge remote-tracking branch 'upstream/main' into fix/viewer-can-mint…
Satyam296 b3617ba
Merge remote-tracking branch 'upstream/main' into fix/viewer-can-mint…
Satyam296 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
frontend/ui/src/app/api/projects/[projectId]/ai/sessions/[sessionId]/messages/route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| vi.mock("next/server", () => ({ NextRequest: class {} })); | ||
|
|
||
| const requireAuthMock = vi.fn(); | ||
| const requireProjectAccessMock = vi.fn(); | ||
| vi.mock("@/lib/auth-helpers", () => ({ | ||
| requireAuth: (...args: unknown[]) => requireAuthMock(...args), | ||
| requireProjectAccess: (...args: unknown[]) => requireProjectAccessMock(...args), | ||
| successResponse: (data: unknown, status = 200) => ({ | ||
| status, | ||
| json: async () => data, | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock("@traceroot/core", () => ({ | ||
| ModelSource: { BYOK: "byok", SYSTEM: "system" }, | ||
| PlanType: { FREE: "FREE" }, | ||
| isBillingEnabled: () => false, | ||
| prisma: { | ||
| modelProvider: { findFirst: vi.fn().mockResolvedValue(null) }, | ||
| workspace: { findUnique: vi.fn().mockResolvedValue(null) }, | ||
| }, | ||
| })); | ||
|
|
||
| const fetchMock = vi.fn(); | ||
| vi.stubGlobal("fetch", fetchMock); | ||
|
|
||
| import { POST } from "./route"; | ||
|
|
||
| function makeParams(sessionId = "sess-1") { | ||
| return { params: Promise.resolve({ projectId: "proj-1", sessionId }) }; | ||
| } | ||
|
|
||
| function makeRequest(body: unknown = { message: "hello", model: "gpt-5", source: "system" }) { | ||
| return { json: async () => body } as unknown as Parameters<typeof POST>[0]; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| requireAuthMock.mockReset(); | ||
| requireProjectAccessMock.mockReset(); | ||
| fetchMock.mockReset(); | ||
| requireAuthMock.mockResolvedValue({ user: { id: "user-1" } }); | ||
| }); | ||
|
|
||
| describe("POST .../ai/sessions/[sessionId]/messages — role gate", () => { | ||
| it("returns 403 for VIEWER (requires MEMBER)", async () => { | ||
| requireProjectAccessMock.mockResolvedValue({ | ||
| error: { status: 403, json: async () => ({ error: "Requires MEMBER role or higher" }) }, | ||
| }); | ||
| const res = await POST(makeRequest(), makeParams()); | ||
| expect(res.status).toBe(403); | ||
| expect(requireProjectAccessMock).toHaveBeenCalledWith("user-1", "proj-1", "MEMBER"); | ||
| expect(fetchMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("allows MEMBER to post a message", async () => { | ||
| requireProjectAccessMock.mockResolvedValue({ | ||
| project: { id: "proj-1", workspaceId: "ws-1" }, | ||
| }); | ||
| const mockStream = new ReadableStream(); | ||
| fetchMock.mockResolvedValue({ ok: true, body: mockStream }); | ||
| const res = await POST(makeRequest(), makeParams()); | ||
| expect(res.status).toBe(200); | ||
| expect(fetchMock).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
frontend/ui/src/app/api/projects/[projectId]/ai/sessions/[sessionId]/route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| vi.mock("next/server", () => ({ NextRequest: class {} })); | ||
|
|
||
| const requireAuthMock = vi.fn(); | ||
| const requireProjectAccessMock = vi.fn(); | ||
| vi.mock("@/lib/auth-helpers", () => ({ | ||
| requireAuth: (...args: unknown[]) => requireAuthMock(...args), | ||
| requireProjectAccess: (...args: unknown[]) => requireProjectAccessMock(...args), | ||
| successResponse: (data: unknown, status = 200) => ({ | ||
| status, | ||
| json: async () => data, | ||
| }), | ||
| })); | ||
|
|
||
| const fetchMock = vi.fn(); | ||
| vi.stubGlobal("fetch", fetchMock); | ||
|
|
||
| import { DELETE } from "./route"; | ||
|
|
||
| function makeParams(sessionId = "sess-1") { | ||
| return { params: Promise.resolve({ projectId: "proj-1", sessionId }) }; | ||
| } | ||
|
|
||
| function makeRequest() { | ||
| return {} as unknown as Parameters<typeof DELETE>[0]; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| requireAuthMock.mockReset(); | ||
| requireProjectAccessMock.mockReset(); | ||
| fetchMock.mockReset(); | ||
| requireAuthMock.mockResolvedValue({ user: { id: "user-1" } }); | ||
| }); | ||
|
|
||
| describe("DELETE .../ai/sessions/[sessionId] — role gate", () => { | ||
| it("returns 403 for VIEWER (requires ADMIN)", async () => { | ||
| requireProjectAccessMock.mockResolvedValue({ | ||
| error: { status: 403, json: async () => ({ error: "Requires ADMIN role or higher" }) }, | ||
| }); | ||
| const res = await DELETE(makeRequest(), makeParams()); | ||
| expect(res.status).toBe(403); | ||
| expect(requireProjectAccessMock).toHaveBeenCalledWith("user-1", "proj-1", "ADMIN"); | ||
| expect(fetchMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("allows ADMIN to delete a session", async () => { | ||
| requireProjectAccessMock.mockResolvedValue({ | ||
| project: { id: "proj-1", workspaceId: "ws-1" }, | ||
| }); | ||
| fetchMock.mockResolvedValue({ ok: true, json: async () => ({}) }); | ||
| const res = await DELETE(makeRequest(), makeParams()); | ||
| expect(res.status).toBe(200); | ||
| expect(fetchMock).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
frontend/ui/src/app/api/projects/[projectId]/ai/sessions/route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| vi.mock("next/server", () => ({ NextRequest: class {} })); | ||
|
|
||
| const requireAuthMock = vi.fn(); | ||
| const requireProjectAccessMock = vi.fn(); | ||
| vi.mock("@/lib/auth-helpers", () => ({ | ||
| requireAuth: (...args: unknown[]) => requireAuthMock(...args), | ||
| requireProjectAccess: (...args: unknown[]) => requireProjectAccessMock(...args), | ||
| successResponse: (data: unknown, status = 200) => ({ | ||
| status, | ||
| json: async () => data, | ||
| }), | ||
| })); | ||
|
|
||
| const fetchMock = vi.fn(); | ||
| vi.stubGlobal("fetch", fetchMock); | ||
|
|
||
| import { POST } from "./route"; | ||
|
|
||
| function makeParams() { | ||
| return { params: Promise.resolve({ projectId: "proj-1" }) }; | ||
| } | ||
|
|
||
| function makeRequest(body: unknown = {}) { | ||
| return { json: async () => body } as unknown as Parameters<typeof POST>[0]; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| requireAuthMock.mockReset(); | ||
| requireProjectAccessMock.mockReset(); | ||
| fetchMock.mockReset(); | ||
| requireAuthMock.mockResolvedValue({ user: { id: "user-1" } }); | ||
| }); | ||
|
|
||
| describe("POST .../ai/sessions — role gate", () => { | ||
| it("returns 403 for VIEWER (requires MEMBER)", async () => { | ||
| requireProjectAccessMock.mockResolvedValue({ | ||
| error: { status: 403, json: async () => ({ error: "Requires MEMBER role or higher" }) }, | ||
| }); | ||
| const res = await POST(makeRequest({ title: "new session" }), makeParams()); | ||
| expect(res.status).toBe(403); | ||
| expect(requireProjectAccessMock).toHaveBeenCalledWith("user-1", "proj-1", "MEMBER"); | ||
| expect(fetchMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("allows MEMBER to create a session", async () => { | ||
| requireProjectAccessMock.mockResolvedValue({ | ||
| project: { id: "proj-1", workspaceId: "ws-1" }, | ||
| }); | ||
| fetchMock.mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => ({ id: "sess-1" }), | ||
| }); | ||
| const res = await POST(makeRequest({ title: "new session" }), makeParams()); | ||
| expect(res.status).toBe(201); | ||
| expect(fetchMock).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
frontend/ui/src/app/api/projects/[projectId]/detectors/[detectorId]/route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| vi.mock("next/server", () => ({ NextRequest: class {} })); | ||
|
|
||
| const detectorFindFirstMock = vi.fn(); | ||
| const detectorUpdateMock = vi.fn(); | ||
| const detectorDeleteMock = vi.fn(); | ||
| vi.mock("@traceroot/core", () => ({ | ||
| prisma: { | ||
| detector: { | ||
| findFirst: (...args: unknown[]) => detectorFindFirstMock(...args), | ||
| update: (...args: unknown[]) => detectorUpdateMock(...args), | ||
| delete: (...args: unknown[]) => detectorDeleteMock(...args), | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| const requireAuthMock = vi.fn(); | ||
| const requireProjectAccessMock = vi.fn(); | ||
| vi.mock("@/lib/auth-helpers", () => ({ | ||
| requireAuth: (...args: unknown[]) => requireAuthMock(...args), | ||
| requireProjectAccess: (...args: unknown[]) => requireProjectAccessMock(...args), | ||
| errorResponse: (msg: string, status: number) => ({ | ||
| status, | ||
| json: async () => ({ error: msg }), | ||
| }), | ||
| successResponse: (data: unknown, status = 200) => ({ | ||
| status, | ||
| json: async () => data, | ||
| }), | ||
| })); | ||
|
|
||
| import { PATCH, DELETE } from "./route"; | ||
|
|
||
| function makeParams(detectorId = "det-1") { | ||
| return { params: Promise.resolve({ projectId: "proj-1", detectorId }) }; | ||
| } | ||
|
|
||
| function makePatchRequest(body: unknown) { | ||
| return { json: async () => body } as unknown as Parameters<typeof PATCH>[0]; | ||
| } | ||
|
|
||
| function makeDeleteRequest() { | ||
| return {} as unknown as Parameters<typeof DELETE>[0]; | ||
| } | ||
|
|
||
| function viewerForbidden() { | ||
| return { | ||
| error: { status: 403, json: async () => ({ error: "Requires MEMBER role or higher" }) }, | ||
| }; | ||
| } | ||
|
|
||
| function adminForbidden() { | ||
| return { | ||
| error: { status: 403, json: async () => ({ error: "Requires ADMIN role or higher" }) }, | ||
| }; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| detectorFindFirstMock.mockReset(); | ||
| detectorUpdateMock.mockReset(); | ||
| detectorDeleteMock.mockReset(); | ||
| requireAuthMock.mockReset(); | ||
| requireProjectAccessMock.mockReset(); | ||
| requireAuthMock.mockResolvedValue({ user: { id: "user-1" } }); | ||
| }); | ||
|
|
||
| describe("PATCH .../detectors/[detectorId] — role gate", () => { | ||
| it("returns 403 for VIEWER (requires MEMBER)", async () => { | ||
| requireProjectAccessMock.mockResolvedValue(viewerForbidden()); | ||
| const res = await PATCH(makePatchRequest({ name: "updated" }), makeParams()); | ||
| expect(res.status).toBe(403); | ||
| expect(requireProjectAccessMock).toHaveBeenCalledWith("user-1", "proj-1", "MEMBER"); | ||
| expect(detectorUpdateMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("allows MEMBER to update a detector", async () => { | ||
| requireProjectAccessMock.mockResolvedValue({ project: { workspaceId: "ws-1" } }); | ||
| detectorFindFirstMock.mockResolvedValue({ id: "det-1", projectId: "proj-1" }); | ||
| detectorUpdateMock.mockResolvedValue({ id: "det-1", name: "updated" }); | ||
| const res = await PATCH(makePatchRequest({ name: "updated" }), makeParams()); | ||
| expect(res.status).toBe(200); | ||
| expect(detectorUpdateMock).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("DELETE .../detectors/[detectorId] — role gate", () => { | ||
| it("returns 403 for VIEWER (requires ADMIN)", async () => { | ||
| requireProjectAccessMock.mockResolvedValue(adminForbidden()); | ||
| const res = await DELETE(makeDeleteRequest(), makeParams()); | ||
| expect(res.status).toBe(403); | ||
| expect(requireProjectAccessMock).toHaveBeenCalledWith("user-1", "proj-1", "ADMIN"); | ||
| expect(detectorDeleteMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("allows ADMIN to delete a detector", async () => { | ||
| requireProjectAccessMock.mockResolvedValue({ project: { workspaceId: "ws-1" } }); | ||
| detectorFindFirstMock.mockResolvedValue({ id: "det-1", projectId: "proj-1" }); | ||
| detectorDeleteMock.mockResolvedValue({}); | ||
| const res = await DELETE(makeDeleteRequest(), makeParams()); | ||
| expect(res.status).toBe(200); | ||
| expect(detectorDeleteMock).toHaveBeenCalledWith({ where: { id: "det-1" } }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.