From 5ab1bbe92c29fd68373e5774b8cb16a29be9bb83 Mon Sep 17 00:00:00 2001 From: Jeon Suyeol Date: Thu, 5 Mar 2026 14:50:21 +0900 Subject: [PATCH] Fix spy leak in test and unsafe error cast in ensureSlackAuth Wrap copyFileSync spy restore in finally block so the mock cannot leak when the assertion throws. Use defensive type checks in the catch block to avoid throwing on non-Error objects. --- src/platforms/slack/ensure-auth.ts | 4 +++- src/platforms/slack/token-extractor.test.ts | 10 ++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/platforms/slack/ensure-auth.ts b/src/platforms/slack/ensure-auth.ts index f389f7e..1bc398d 100644 --- a/src/platforms/slack/ensure-auth.ts +++ b/src/platforms/slack/ensure-auth.ts @@ -27,7 +27,9 @@ export async function ensureSlackAuth(): Promise { await credManager.setCurrentWorkspace(validWorkspaces[0].workspace_id) } } catch (error) { - if ((error as NodeJS.ErrnoException).code === 'EBUSY' || (error as Error).message.includes('locking the cookie')) { + const code = typeof error === 'object' && error !== null ? (error as NodeJS.ErrnoException).code : undefined + const message = error instanceof Error ? error.message : String(error) + if (code === 'EBUSY' || message.includes('locking the cookie')) { throw error } // Silently ignore other extraction errors (e.g. Slack not installed) diff --git a/src/platforms/slack/token-extractor.test.ts b/src/platforms/slack/token-extractor.test.ts index 190ba5b..24e5d70 100644 --- a/src/platforms/slack/token-extractor.test.ts +++ b/src/platforms/slack/token-extractor.test.ts @@ -153,10 +153,12 @@ describe('TokenExtractor Windows DPAPI', () => { }) // when — then - const extractor = new TokenExtractor('darwin', slackDir) - await expect(extractor.extract()).rejects.toThrow('Quit the Slack app completely and try again') - - copyFileSyncSpy.mockRestore() + try { + const extractor = new TokenExtractor('darwin', slackDir) + await expect(extractor.extract()).rejects.toThrow('Quit the Slack app completely and try again') + } finally { + copyFileSyncSpy.mockRestore() + } }) test('extract decrypts Windows v10 cookies end-to-end with mocked DPAPI', async () => {