-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(web): gate /api/video/transcribe/status on video access policy #2032
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
DPS0340
wants to merge
4
commits into
CapSoftware:main
Choose a base branch
from
DPS0340:fix/transcribe-status-access-check
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.
+185
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6f09b4a
fix(web): gate /api/video/transcribe/status on video access policy
DPS0340 9fdbada
perf: limit the transcribe status lookup to one row
DPS0340 601fa1d
perf: provide the already-loaded user instead of re-resolving it
DPS0340 236db34
fix(web): distinguish a database failure from an access denial on tra…
DPS0340 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
136 changes: 136 additions & 0 deletions
136
apps/web/__tests__/unit/transcribe-status-access.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,136 @@ | ||
| import { Exit, Layer } from "effect"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mockDbChain = { | ||
| select: vi.fn(), | ||
| from: vi.fn(), | ||
| where: vi.fn(), | ||
| limit: vi.fn(), | ||
| }; | ||
|
|
||
| function resetDbChain() { | ||
| for (const fn of Object.values(mockDbChain)) fn.mockClear(); | ||
| mockDbChain.select.mockReturnValue(mockDbChain); | ||
| mockDbChain.from.mockReturnValue(mockDbChain); | ||
| mockDbChain.where.mockReturnValue(mockDbChain); | ||
| mockDbChain.limit.mockReturnValue( | ||
| Promise.resolve([{ id: "video-1", transcriptionStatus: "COMPLETE" }]), | ||
| ); | ||
| } | ||
|
|
||
| vi.mock("@cap/database", () => ({ db: () => mockDbChain })); | ||
|
|
||
| vi.mock("@cap/database/schema", () => ({ | ||
| videos: { id: "id" }, | ||
| })); | ||
|
|
||
| const mockGetCurrentUser = vi.fn(); | ||
| vi.mock("@cap/database/auth/session", () => ({ | ||
| getCurrentUser: () => mockGetCurrentUser(), | ||
| })); | ||
|
|
||
| const mockCanView = vi.fn(); | ||
| vi.mock("@cap/web-backend", () => ({ | ||
| makeCurrentUserLayer: () => Layer.empty, | ||
| VideosPolicy: { | ||
| key: "VideosPolicy", | ||
| }, | ||
| })); | ||
|
|
||
| // The route runs its query through the Effect runtime; the parts under test are | ||
| // which failures reach the handler and how it maps them, so the runtime is | ||
| // replaced with one that returns the exit the policy would have produced. | ||
| vi.mock("@/lib/server", () => ({ | ||
| runPromiseExit: () => mockCanView(), | ||
| })); | ||
|
|
||
| vi.mock("@cap/web-domain", async () => { | ||
| const actual = | ||
| await vi.importActual<typeof import("@cap/web-domain")>("@cap/web-domain"); | ||
| return { | ||
| ...actual, | ||
| Policy: { | ||
| ...actual.Policy, | ||
| withPublicPolicy: () => (self: unknown) => self, | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| const { GET } = await import("@/app/api/video/transcribe/status/route"); | ||
| const { DatabaseError, Policy } = await import("@cap/web-domain"); | ||
|
|
||
| const request = (videoId: string) => | ||
| new Request( | ||
| `https://cap.test/api/video/transcribe/status?videoId=${videoId}`, | ||
| ) as never; | ||
|
|
||
| describe("GET /api/video/transcribe/status", () => { | ||
| beforeEach(() => { | ||
| resetDbChain(); | ||
| mockGetCurrentUser.mockResolvedValue({ id: "user-1" }); | ||
| mockCanView.mockReset(); | ||
| }); | ||
|
|
||
| it("returns 401 when there is no session", async () => { | ||
| mockGetCurrentUser.mockResolvedValue(null); | ||
|
|
||
| const res = await GET(request("video-1")); | ||
|
|
||
| expect(res.status).toBe(401); | ||
| }); | ||
|
|
||
| it("returns 400 when videoId is missing", async () => { | ||
| const res = await GET( | ||
| new Request("https://cap.test/api/video/transcribe/status") as never, | ||
| ); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| }); | ||
|
|
||
| it("returns 404 when the access policy denies the caller", async () => { | ||
| mockCanView.mockResolvedValue( | ||
| Exit.fail(new Policy.PolicyDeniedError({ reason: "denied" })), | ||
| ); | ||
|
|
||
| const res = await GET(request("video-1")); | ||
|
|
||
| expect(res.status).toBe(404); | ||
| await expect(res.json()).resolves.toMatchObject({ | ||
| message: "Video does not exist", | ||
| }); | ||
| }); | ||
|
|
||
| it("returns 500, not 404, when the access check hits a database failure", async () => { | ||
| mockCanView.mockResolvedValue( | ||
| Exit.fail(new DatabaseError({ cause: new Error("connection reset") })), | ||
| ); | ||
|
|
||
| const res = await GET(request("video-1")); | ||
|
|
||
| expect(res.status).toBe(500); | ||
| await expect(res.json()).resolves.toMatchObject({ | ||
| message: "Failed to fetch transcription status", | ||
| }); | ||
| }); | ||
|
|
||
| it("returns the transcription status when access is granted", async () => { | ||
| mockCanView.mockResolvedValue( | ||
| Exit.succeed([{ id: "video-1", transcriptionStatus: "COMPLETE" }]), | ||
| ); | ||
|
|
||
| const res = await GET(request("video-1")); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| await expect(res.json()).resolves.toEqual({ | ||
| transcriptionStatus: "COMPLETE", | ||
| }); | ||
| }); | ||
|
|
||
| it("returns 404 when the video row is missing", async () => { | ||
| mockCanView.mockResolvedValue(Exit.succeed([])); | ||
|
|
||
| const res = await GET(request("video-1")); | ||
|
|
||
| expect(res.status).toBe(404); | ||
| }); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exit.isFailure(exit)can also represent unexpected errors (DB connectivity, etc.). If you want to keep the 404-for-deny behavior to avoid an existence oracle, it might be worth distinguishing policy denial from other failures (and logging/returning 500 for the latter) so real outages aren't silently masked.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wrong to skip this one, and re-checking it showed the case is real. Adopted in
236db3478.Why my first answer was wrong. I'd argued the failure channel here was effectively only
PolicyDenied, so collapsing everything to 404 was safe. It isn't.canViewreads throughVideosRepo.getById,spacesRepo.passwordsForVideo,orgsRepo.membershipForVideoandorgsRepo.allowedEmailDomain— all of which go throughDatabase.use, which wraps failures asDatabaseError:So
DatabaseErrorshares the failure channel withPolicyDeniedError, and my code mapped both to404 Video does not exist. During a database blip, a user asking about a video they genuinely own gets told it doesn't exist, and nothing is logged.The fix keys off the tag rather than treating every failure alike, using the same
Schema.isnarrowingOrganisationsRpcs.tsuses for error mapping:I kept the 404-for-denied behaviour, since as you noted that's the existence-oracle defence and it's worth keeping.
Proof it bites, rather than just asserting it. Added
__tests__/unit/transcribe-status-access.test.ts(6 cases: 401, 400, denial→404, DB failure→500, success, missing row→404). All 6 pass with the fix. Reverting onlyroute.tsto the previous commit and re-running:Exactly the one case, so the test is pinned to this behaviour and not to incidental structure.
tsc --noEmitclean for both files,biome checkclean.Note on verifying this locally:
tscinapps/webreportsTS6305for every workspace import untilnpx tsc --build packages/web-domain packages/web-backend packages/databasehas been run — worth knowing before trusting a "type error" in this app.