Skip to content

Commit 93bd4bf

Browse files
authored
fix(deps): bump vite to 7.3.2 (security) (#1168)
* fix(deps): bump vite to 7.3.2 (security) * fix(test): replace vi.mock('node:fs') with vi.spyOn in wrapper tests vi.mock auto-mocking of node: built-ins fails intermittently with vitest threads pool + sharding in CI. Switch to vi.spyOn which works reliably. Source files updated to access fs methods via the default import (fs.existsSync) so spies can intercept them.
1 parent 83aa5d8 commit 93bd4bf

File tree

7 files changed

+366
-146
lines changed

7 files changed

+366
-146
lines changed

packages/cli/src/commands/wrapper/postinstall-wrapper.mts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs, { existsSync } from 'node:fs'
1+
import fs from 'node:fs'
22

33
import { debug, debugDir } from '@socketsecurity/lib/debug'
44
import { getDefaultLogger } from '@socketsecurity/lib/logger'
@@ -16,8 +16,8 @@ export async function postinstallWrapper() {
1616
const bashRcPath = getBashRcPath()
1717
const zshRcPath = getZshRcPath()
1818
const socketWrapperEnabled =
19-
(existsSync(bashRcPath) && checkSocketWrapperSetup(bashRcPath)) ||
20-
(existsSync(zshRcPath) && checkSocketWrapperSetup(zshRcPath))
19+
(fs.existsSync(bashRcPath) && checkSocketWrapperSetup(bashRcPath)) ||
20+
(fs.existsSync(zshRcPath) && checkSocketWrapperSetup(zshRcPath))
2121

2222
if (!socketWrapperEnabled) {
2323
await setupSocketWrapper(
@@ -78,10 +78,10 @@ async function setupSocketWrapper(query: string): Promise<void> {
7878
const bashRcPath = getBashRcPath()
7979
const zshRcPath = getZshRcPath()
8080
try {
81-
if (existsSync(bashRcPath)) {
81+
if (fs.existsSync(bashRcPath)) {
8282
await addSocketWrapper(bashRcPath)
8383
}
84-
if (existsSync(zshRcPath)) {
84+
if (fs.existsSync(zshRcPath)) {
8585
await addSocketWrapper(zshRcPath)
8686
}
8787
} catch (e) {

packages/cli/src/commands/wrapper/remove-socket-wrapper.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { readFileSync, writeFileSync } from 'node:fs'
1+
import fs from 'node:fs'
22

33
import { getDefaultLogger } from '@socketsecurity/lib/logger'
44
const logger = getDefaultLogger()
55

66
export function removeSocketWrapper(filepath: string): void {
77
let content: string | undefined
88
try {
9-
content = readFileSync(filepath, 'utf8')
9+
content = fs.readFileSync(filepath, 'utf8')
1010
} catch (e) {
1111
logger.fail(`There was an error removing the alias${e ? ':' : '.'}`)
1212
if (e) {
@@ -22,7 +22,7 @@ export function removeSocketWrapper(filepath: string): void {
2222
)
2323
const updatedContent = linesWithoutSocketAlias.join('\n')
2424
try {
25-
writeFileSync(filepath, updatedContent, 'utf8')
25+
fs.writeFileSync(filepath, updatedContent, 'utf8')
2626
} catch (e) {
2727
if (e) {
2828
logger.error(e)
Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
11
/**
2-
* Unit tests for checkSocketWrapperSetup.
3-
*
4-
* Purpose:
5-
* Tests checking Socket wrapper installation status. Validates detection of installed wrappers across package managers.
6-
*
7-
* Test Coverage:
8-
* - Core functionality validation
9-
* - Edge case handling
10-
* - Error scenarios
11-
* - Input validation
12-
*
13-
* Testing Approach:
14-
* Comprehensive unit testing of module functionality with mocked dependencies
15-
* where appropriate.
16-
*
17-
* Related Files:
18-
* - src/checkSocketWrapperSetup.mts (implementation)
2+
* @fileoverview Unit tests for checkSocketWrapperSetup.
193
*/
204

215
import fs from 'node:fs'
226

23-
import { beforeEach, describe, expect, it, vi } from 'vitest'
7+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
248

25-
import { checkSocketWrapperSetup } from '../../../../src/commands/../../../../src/commands/wrapper/check-socket-wrapper-setup.mts'
26-
27-
// Mock the dependencies.
28-
vi.mock('node:fs')
9+
import { checkSocketWrapperSetup } from '../../../../src/commands/wrapper/check-socket-wrapper-setup.mts'
2910

3011
const mockLogger = vi.hoisted(() => ({
3112
fail: vi.fn(),
@@ -42,13 +23,19 @@ vi.mock('@socketsecurity/lib/logger', () => ({
4223
}))
4324

4425
describe('checkSocketWrapperSetup', () => {
26+
let readFileSyncSpy: ReturnType<typeof vi.spyOn>
27+
4528
beforeEach(() => {
4629
vi.clearAllMocks()
30+
readFileSyncSpy = vi.spyOn(fs, 'readFileSync')
31+
})
32+
33+
afterEach(() => {
34+
readFileSyncSpy.mockRestore()
4735
})
4836

4937
it('detects npm alias in file', () => {
50-
const mockReadFileSync = vi.mocked(fs.readFileSync) as any
51-
mockReadFileSync.mockReturnValue('alias npm="socket npm"\nother content')
38+
readFileSyncSpy.mockReturnValue('alias npm="socket npm"\nother content')
5239

5340
const result = checkSocketWrapperSetup('/home/user/.bashrc')
5441

@@ -57,17 +44,15 @@ describe('checkSocketWrapperSetup', () => {
5744
})
5845

5946
it('detects npx alias in file', () => {
60-
const mockReadFileSync = vi.mocked(fs.readFileSync) as any
61-
mockReadFileSync.mockReturnValue('alias npx="socket npx"\nother content')
47+
readFileSyncSpy.mockReturnValue('alias npx="socket npx"\nother content')
6248

6349
const result = checkSocketWrapperSetup('/home/user/.bashrc')
6450

6551
expect(result).toBe(true)
6652
})
6753

6854
it('detects both aliases in file', () => {
69-
const mockReadFileSync = vi.mocked(fs.readFileSync) as any
70-
mockReadFileSync.mockReturnValue(
55+
readFileSyncSpy.mockReturnValue(
7156
'alias npm="socket npm"\nalias npx="socket npx"\nother content',
7257
)
7358

@@ -77,39 +62,36 @@ describe('checkSocketWrapperSetup', () => {
7762
})
7863

7964
it('returns false when no aliases found', () => {
80-
const mockReadFileSync = vi.mocked(fs.readFileSync) as any
81-
mockReadFileSync.mockReturnValue('some other content\nno aliases here')
65+
readFileSyncSpy.mockReturnValue('some other content\nno aliases here')
8266

8367
const result = checkSocketWrapperSetup('/home/user/.bashrc')
8468

8569
expect(result).toBe(false)
8670
})
8771

8872
it('returns false for empty file', () => {
89-
const mockReadFileSync = vi.mocked(fs.readFileSync) as any
90-
mockReadFileSync.mockReturnValue('')
73+
readFileSyncSpy.mockReturnValue('')
9174

9275
const result = checkSocketWrapperSetup('/home/user/.bashrc')
9376

9477
expect(result).toBe(false)
9578
})
9679

97-
it('logs instructions when wrapper is set up', async () => {
98-
await import('@socketsecurity/lib/logger')
99-
const mockReadFileSync = vi.mocked(fs.readFileSync) as any
100-
mockReadFileSync.mockReturnValue('alias npm="socket npm"')
80+
it('logs instructions when wrapper is set up', () => {
81+
readFileSyncSpy.mockReturnValue('alias npm="socket npm"')
10182

10283
checkSocketWrapperSetup('/home/user/.bashrc')
10384

10485
expect(mockLogger.log).toHaveBeenCalledWith(
10586
'The Socket npm/npx wrapper is set up in your bash profile (/home/user/.bashrc).',
10687
)
107-
expect(mockLogger.log).toHaveBeenCalledWith(' source /home/user/.bashrc')
88+
expect(mockLogger.log).toHaveBeenCalledWith(
89+
' source /home/user/.bashrc',
90+
)
10891
})
10992

11093
it('ignores partial alias matches', () => {
111-
const mockReadFileSync = vi.mocked(fs.readFileSync) as any
112-
mockReadFileSync.mockReturnValue(
94+
readFileSyncSpy.mockReturnValue(
11395
'alias npm="other-tool npm"\nalias npx="other-tool npx"',
11496
)
11597

@@ -119,8 +101,7 @@ describe('checkSocketWrapperSetup', () => {
119101
})
120102

121103
it('handles multiline file with aliases mixed in', () => {
122-
const mockReadFileSync = vi.mocked(fs.readFileSync) as any
123-
mockReadFileSync.mockReturnValue(
104+
readFileSyncSpy.mockReturnValue(
124105
`#!/bin/bash
125106
# User bashrc
126107
export PATH=$PATH:/usr/local/bin
@@ -135,26 +116,20 @@ export NODE_ENV=development`,
135116
})
136117

137118
it('is case-sensitive for alias detection', () => {
138-
const mockReadFileSync = vi.mocked(fs.readFileSync) as any
139-
mockReadFileSync.mockReturnValue('ALIAS NPM="SOCKET NPM"')
119+
readFileSyncSpy.mockReturnValue('ALIAS NPM="SOCKET NPM"')
140120

141121
const result = checkSocketWrapperSetup('/home/user/.bashrc')
142122

143123
expect(result).toBe(false)
144124
})
145125

146126
it('handles files with Windows line endings', () => {
147-
const mockReadFileSync = vi.mocked(fs.readFileSync) as any
148-
// When splitting on \n, Windows line endings leave \r at the end of lines,
149-
// so 'alias npm="socket npm"\r' !== 'alias npm="socket npm"'.
150-
// The function doesn't handle Windows line endings properly.
151-
mockReadFileSync.mockReturnValue(
127+
readFileSyncSpy.mockReturnValue(
152128
'line1\r\nalias npm="socket npm"\r\nalias npx="socket npx"\r\n',
153129
)
154130

155131
const result = checkSocketWrapperSetup('/home/user/.bashrc')
156132

157-
// The function splits by \n, leaving \r at the end, so exact match fails.
158133
expect(result).toBe(false)
159134
})
160135
})

0 commit comments

Comments
 (0)