From a962ba242687de186d285c7b9908f5001e69d578 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Tue, 3 Nov 2020 16:44:50 -0800 Subject: [PATCH] add timeout when verifying auth to remote --- package.json | 7 ++++++- packages/cli/__tests__/main.test.ts | 1 + .../core/src/utils/__tests__/verify-auth.test.ts | 3 +++ packages/core/src/utils/verify-auth.ts | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 6c7001ecc..d80ccc994 100644 --- a/package.json +++ b/package.json @@ -120,7 +120,12 @@ "html", "lcov", "text" - ] + ], + "globals": { + "ts-jest": { + "isolatedModules": true + } + } }, "ignite": { "repo": "intuit/auto", diff --git a/packages/cli/__tests__/main.test.ts b/packages/cli/__tests__/main.test.ts index fe3b3b714..f241a348f 100644 --- a/packages/cli/__tests__/main.test.ts +++ b/packages/cli/__tests__/main.test.ts @@ -3,6 +3,7 @@ import { runCli, execute } from "../src/run"; process.env.GH_TOKEN = "XXXX"; jest.mock("@octokit/rest"); +jest.mock("../../core/src/utils/verify-auth"); test("throws error for unknown args", async () => { process.exit = jest.fn() as any; diff --git a/packages/core/src/utils/__tests__/verify-auth.test.ts b/packages/core/src/utils/__tests__/verify-auth.test.ts index e7d84391b..3b369acb4 100644 --- a/packages/core/src/utils/__tests__/verify-auth.test.ts +++ b/packages/core/src/utils/__tests__/verify-auth.test.ts @@ -8,6 +8,7 @@ describe("verify-auth", () => { test("should handle error", async () => { spawn.mockImplementationOnce(() => ({ stderr: { on: () => {} }, + kill: () => {}, on: () => { throw new Error(); }, @@ -18,6 +19,7 @@ describe("verify-auth", () => { test("should verify auth when we can push to remote", async () => { spawn.mockImplementationOnce(() => ({ stderr: { on: () => {} }, + kill: () => {}, on: (_: string, cb: () => void) => cb(), })); expect(await verifyAuth("origin", "master")).toBe(true); @@ -29,6 +31,7 @@ describe("verify-auth", () => { on: (_: string, cb: (data: string) => void) => cb("fatal: could not read Username"), }, + kill: () => {}, on: (_: string, cb: () => void) => cb(), })); expect(await verifyAuth("bad", "master")).toBe(false); diff --git a/packages/core/src/utils/verify-auth.ts b/packages/core/src/utils/verify-auth.ts index c8338f3c9..6525367f0 100644 --- a/packages/core/src/utils/verify-auth.ts +++ b/packages/core/src/utils/verify-auth.ts @@ -5,6 +5,15 @@ import { spawn } from "child_process"; */ export default function verifyAuth(remote: string, branch: string) { return new Promise((resolve) => { + let timeout: NodeJS.Timeout | null = null; + + /** Clear the timeout timeout */ + const clear = () => { + if (timeout) { + clearTimeout(timeout); + } + }; + try { const child = spawn( `git push --dry-run --no-verify ${remote} HEAD:${branch} -q`, @@ -16,6 +25,11 @@ export default function verifyAuth(remote: string, branch: string) { } ); + timeout = setTimeout(() => { + child.kill(0); + resolve(false); + }, 5 * 1000); + let err = ""; child.stderr.on("data", (data) => { @@ -23,6 +37,7 @@ export default function verifyAuth(remote: string, branch: string) { }); child.on("exit", () => { + clear(); resolve( !err.startsWith("fatal: could not read Username") && !err.startsWith("ssh_askpass") && @@ -30,6 +45,7 @@ export default function verifyAuth(remote: string, branch: string) { ); }); } catch (error) { + clear(); resolve(false); } });