Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(skills): cancel imports while GitHub credential lookup is pending

Ooi Pin Jund901718d2a24
diff --git a/server/skill-fetch.test.ts b/server/skill-fetch.test.ts
index 6574721d56..6c6f6c6ad8 100644
--- a/server/skill-fetch.test.ts
+++ b/server/skill-fetch.test.ts
@@ -116,6 +116,35 @@ describe("importing a skill's whole folder", () => {
expect(raw.every((call) => call.headers.get("authorization") === null && call.redirect === "error")).toBe(true);
});
+ it("cancels the import while the GitHub credential lookup is still pending", async () => {
+ let release: (signIn: { token: string | null }) => void = () => {};
+ const credential = new Promise<{ token: string | null }>((resolve) => { release = resolve; });
+ let markEntered: () => void = () => {};
+ const getterEntered = new Promise<void>((resolve) => { markEntered = resolve; });
+ const fetchSpy = vi.fn(async () => new Response("unused")) as typeof fetch;
+ const controller = new AbortController();
+ const result = fetchSkillFromSource("a/b", fetchSpy, controller.signal, async () => {
+ markEntered();
+ return credential;
+ });
+ try {
+ await getterEntered;
+ controller.abort(new Error("fixture import cancelled"));
+ const checkpoint = new Promise<void>((resolve) => { setImmediate(resolve); });
+ const settled = await Promise.race([
+ result.then((value) => ({ settled: true as const, value })),
+ checkpoint.then(() => ({ settled: false as const })),
+ ]);
+ expect(settled).toEqual({ settled: true, value: { error: "fixture import cancelled" } });
+ release({ token: SIGNED_IN });
+ release = () => {};
+ await new Promise<void>((resolve) => { setImmediate(resolve); });
+ expect(fetchSpy).not.toHaveBeenCalled();
+ } finally {
+ release({ token: null });
+ }
+ });
+
it("stays anonymous when the sign-in getter has no token", async () => {
const github = fakeGitHub({ "a/b": PDF_SKILL });
const recorded = recordingFetch(github.fetch);
diff --git a/server/skill-fetch.ts b/server/skill-fetch.ts
index 6831e23e20..c19421e5ea 100644
--- a/server/skill-fetch.ts
+++ b/server/skill-fetch.ts
@@ -67,7 +67,21 @@ function boundedImportFetch(
const timeout = AbortSignal.timeout(IMPORT_LIMITS.ms);
const signal = cancel ? AbortSignal.any([timeout, cancel]) : timeout;
// `gh auth token` starts a process; a null answer stays null for this import.
- const credentialOnce = (): Promise<SkillImportSignIn> => (credential ??= (async () => authorization())());
+ const credentialOnce = (): Promise<SkillImportSignIn> => {
+ if (credential) return credential;
+ if (signal.aborted) return (credential = Promise.reject(signal.reason));
+ const lookup = authorization();
+ void lookup.catch(() => {});
+ return (credential = new Promise((resolve, reject) => {
+ const abort = () => reject(signal.reason);
+ if (signal.aborted) {
+ abort();
+ return;
+ }
+ signal.addEventListener("abort", abort, { once: true });
+ lookup.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
+ }));
+ };
return async (input, init) => {
const api = isGitHubApiRequest(input);
if (api ? ++apiRequests > IMPORT_LIMITS.apiRequests : ++downloads > IMPORT_LIMITS.downloads) {
@@ -85,6 +99,7 @@ function boundedImportFetch(
carried = signIn.refused;
}
}
+ signal.throwIfAborted();
const response = await fetcher(input, sent);
if (carried && response.status === 401 && !reported) {
reported = true;