-
-
Notifications
You must be signed in to change notification settings - Fork 91
feat(orb): gate APR repo-transfer requests on idea completion #8000
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
Closed
Closed
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { createApp } from "../../src/api/routes"; | ||
| import { createInstallationToken } from "../../src/github/app"; | ||
| import { createTestEnv } from "../helpers/d1"; | ||
|
|
||
| // #7742: POST /v1/loop/request-apr-transfer — customer-facing request-only APR transfer. Pins the ROUTE | ||
| // contract (status codes + body validation) against the real requestAprRepoTransfer wiring; GitHub is mocked. | ||
| vi.mock("../../src/github/app", async (importOriginal) => ({ | ||
| ...(await importOriginal<typeof import("../../src/github/app")>()), | ||
| createInstallationToken: vi.fn(), | ||
| })); | ||
| const mockedToken = vi.mocked(createInstallationToken); | ||
|
|
||
| function stubFetch(handler: (url: string, init: RequestInit) => Response): void { | ||
| vi.stubGlobal("fetch", async (input: RequestInfo | URL, init?: RequestInit) => handler(String(input), init ?? {})); | ||
| } | ||
|
|
||
| const apiHeaders = (env: Env) => ({ authorization: `Bearer ${env.LOOPOVER_API_TOKEN}`, "content-type": "application/json" }); | ||
| const PATH = "/v1/loop/request-apr-transfer"; | ||
|
|
||
| const validBody = { | ||
| installationId: 42, | ||
| repoFullName: "loopover-repos/widgets", | ||
| newOwner: "customer-acct", | ||
| ideaComplete: true, | ||
| }; | ||
|
|
||
| const post = (env: Env, body: unknown) => | ||
| createApp().request(PATH, { method: "POST", headers: apiHeaders(env), body: JSON.stringify(body) }, env); | ||
|
|
||
| describe("POST /v1/loop/request-apr-transfer (#7742)", () => { | ||
| beforeEach(() => { | ||
| mockedToken.mockReset(); | ||
| mockedToken.mockResolvedValue("ghs_installation_token"); | ||
| }); | ||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it("returns 202 with the initiated transfer when the gate passes and GitHub accepts", async () => { | ||
| stubFetch(() => new Response(JSON.stringify({ full_name: "customer-acct/widgets" }), { status: 202 })); | ||
| const response = await post(createTestEnv(), validBody); | ||
| expect(response.status).toBe(202); | ||
| await expect(response.json()).resolves.toEqual({ | ||
| status: "initiated", | ||
| transfer: { initiated: true, status: 202, newFullName: "customer-acct/widgets" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns 409 without contacting GitHub when the idea is not complete", async () => { | ||
| let fetchCalls = 0; | ||
| stubFetch(() => { | ||
| fetchCalls += 1; | ||
| return new Response("{}", { status: 202 }); | ||
| }); | ||
| const response = await post(createTestEnv(), { ...validBody, ideaComplete: false }); | ||
| expect(response.status).toBe(409); | ||
| await expect(response.json()).resolves.toEqual({ status: "rejected", reason: "idea_not_complete" }); | ||
| expect(fetchCalls).toBe(0); | ||
| expect(mockedToken).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns 502 when the gate passes but GitHub rejects the transfer", async () => { | ||
| stubFetch(() => new Response("", { status: 403 })); | ||
| const response = await post(createTestEnv(), validBody); | ||
| expect(response.status).toBe(502); | ||
| await expect(response.json()).resolves.toEqual({ | ||
| status: "failed", | ||
| transfer: { initiated: false, status: 403, error: "transfer request failed (403)" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("rejects an invalid or unparseable body with 400 before any GitHub call", async () => { | ||
| let fetchCalls = 0; | ||
| stubFetch(() => { | ||
| fetchCalls += 1; | ||
| return new Response("{}", { status: 202 }); | ||
| }); | ||
| const env = createTestEnv(); | ||
| for (const body of [ | ||
| {}, | ||
| { ...validBody, installationId: 0 }, | ||
| { ...validBody, installationId: -1 }, | ||
| { ...validBody, repoFullName: "" }, | ||
| { ...validBody, newOwner: "" }, | ||
| { ...validBody, ideaComplete: "yes" }, | ||
| { installationId: 1, repoFullName: "a/b", newOwner: "c" }, // missing ideaComplete | ||
| ]) { | ||
| const response = await post(env, body); | ||
| expect(response.status, JSON.stringify(body)).toBe(400); | ||
| await expect(response.json()).resolves.toMatchObject({ error: "invalid_request_apr_transfer_request" }); | ||
| } | ||
| const malformed = await createApp().request(PATH, { method: "POST", headers: apiHeaders(env), body: "{not json" }, env); | ||
| expect(malformed.status).toBe(400); | ||
| expect(fetchCalls).toBe(0); | ||
| expect(mockedToken).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("leaks no wallet/hotkey/trust-score terms", async () => { | ||
| stubFetch(() => new Response(JSON.stringify({ full_name: "customer-acct/widgets" }), { status: 202 })); | ||
| const text = JSON.stringify(await (await post(createTestEnv(), validBody)).json()); | ||
| expect(text).not.toMatch(/wallet|hotkey|coldkey|trust score|reward/i); | ||
| }); | ||
| }); |
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.
P1:
ideaCompletesecurity gate is trivially bypassable because it is caller-suppliedThe
ideaCompleteboolean comes from the request body, so any caller can set it to true and bypass the completion gate.Look up idea completion from a server-side persisted source instead of trusting caller input.
AI prompt