Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Changed
- `socket manifest dynamic-sbom-inference` now rejects `--dynamic-sbom-inference-build-timeout` and `--dynamic-sbom-inference-ignore-unresolved` instead of ignoring them, and names its own `--build-timeout` and `--ignore-unresolved`.

### Security
- Your saved CLI config, which holds your API token, is now readable only by you: new config files are created with owner-only permissions, and an existing file is locked down the next time the CLI saves it.

## [1.19.0](https://github.com/SocketDev/socket-cli/releases/tag/v1.19.0) - 2026-12-04

### Added
Expand Down
36 changes: 32 additions & 4 deletions src/utils/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
* - updateConfigValue: Persist configuration changes
*/

import { mkdirSync, writeFileSync } from 'node:fs'
import {
closeSync,
fchmodSync,
mkdirSync,
openSync,
writeFileSync,
} from 'node:fs'
import path from 'node:path'

import config from '@socketsecurity/config'
Expand Down Expand Up @@ -64,6 +70,10 @@ export interface LocalConfig {
org?: string | undefined
}

const CONFIG_FILE_MODE = 0o600

const SETTINGS_DIR_MODE = 0o700

const sensitiveConfigKeyLookup: Set<keyof LocalConfig> = new Set([
CONFIG_KEY_API_TOKEN,
])
Expand Down Expand Up @@ -95,6 +105,10 @@ const supportedConfigEntries = [...supportedConfig.entries()].sort((a, b) =>
)
const supportedConfigKeys = supportedConfigEntries.map(p => p[0])

function ensureSettingsDirSync(dirPath: string): void {
mkdirSync(dirPath, { mode: SETTINGS_DIR_MODE, recursive: true })
}

function getConfigValues(): LocalConfig {
if (_cachedConfig === undefined) {
// Order: env var > --config flag > file
Expand Down Expand Up @@ -127,7 +141,7 @@ function getConfigValues(): LocalConfig {
updateConfigValue(CONFIG_KEY_API_TOKEN, token)
}
} else {
mkdirSync(socketAppDataPath, { recursive: true })
ensureSettingsDirSync(socketAppDataPath)
}
}
}
Expand Down Expand Up @@ -164,6 +178,20 @@ export type FoundSocketYml = {
parsed: SocketYml
}

function writeConfigFileSync(filePath: string, content: string): void {
const fd = openSync(filePath, 'w', CONFIG_FILE_MODE)
try {
// The open mode applies only on create, so an existing file keeps its
// old mode unless it is tightened before the token is written.
if (process.platform !== 'win32') {
fchmodSync(fd, CONFIG_FILE_MODE)
}
writeFileSync(fd, content)
} finally {
closeSync(fd)
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed chmod wipes saved config

Medium Severity

writeConfigFileSync opens config.json with 'w', which truncates it, before fchmodSync runs. If fchmodSync throws, the new contents are never written and the saved API token is already gone. That exception escapes the nextTick save and the CLI exits.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 36ca648. Configure here.


export function findSocketYmlSync(
dir = process.cwd(),
): CResult<FoundSocketYml | undefined> {
Expand Down Expand Up @@ -383,7 +411,7 @@ export function updateConfigValue<Key extends keyof LocalConfig>(
const configToSave = { ...localConfig }
const { socketAppDataPath } = constants
if (socketAppDataPath) {
mkdirSync(socketAppDataPath, { recursive: true })
ensureSettingsDirSync(socketAppDataPath)
const configFilePath = path.join(socketAppDataPath, 'config.json')
// Read existing file to preserve formatting, then update with new values.
const existingRaw = safeReadFileSync(configFilePath)
Expand Down Expand Up @@ -441,7 +469,7 @@ export function updateConfigValue<Key extends keyof LocalConfig>(
undefined,
indent,
).replace(/\n/g, newline)
writeFileSync(
writeConfigFileSync(
configFilePath,
Buffer.from(jsonContent + newline).toString('base64'),
)
Expand Down
48 changes: 48 additions & 0 deletions test/unit/src/utils/config.test.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
chmodSync,
promises as fs,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
statSync,
writeFileSync,
} from 'node:fs'
import os from 'node:os'
Expand Down Expand Up @@ -49,6 +51,10 @@ import { testPath } from '../../../utils.mts'
const fixtureBaseDir = path.join(testPath, 'fixtures/utils/config')
const temporaryDirectories: string[] = []

function readFileMode(filePath: string): number {
return statSync(filePath).mode & 0o777
}

function useIsolatedAppDataDirectory(): string {
const directory = mkdtempSync(path.join(os.tmpdir(), 'socket-config-home-'))
temporaryDirectories.push(directory)
Expand Down Expand Up @@ -178,6 +184,48 @@ describe('utils/config', () => {
})
})

describe.skipIf(process.platform === 'win32')('file permissions', () => {
it('creates the settings directory and config file owner-only', async () => {
const settingsDirectory = path.join(
useIsolatedAppDataDirectory(),
'socket',
'settings',
)
mockConfigDirectory.value = settingsDirectory
resetConfigForTesting()

updateConfigValue('defaultOrg', 'socket-team')
await flushConfigWrites()

expect(readFileMode(settingsDirectory)).toBe(0o700)
expect(readFileMode(path.join(settingsDirectory, 'config.json'))).toBe(
0o600,
)
})

it('tightens an existing world-readable config file on save', async () => {
const configDirectory = useIsolatedAppDataDirectory()
const configFile = path.join(configDirectory, 'config.json')
writeFileSync(
configFile,
Buffer.from(JSON.stringify({ defaultOrg: 'socket-team' })).toString(
'base64',
),
)
chmodSync(configFile, 0o644)
resetConfigForTesting()

updateConfigValue('defaultOrg', 'socket-docs')
await flushConfigWrites()

expect(readFileMode(configFile)).toBe(0o600)
const stored = readFileSync(configFile, 'utf8')
expect(
JSON.parse(Buffer.from(stored, 'base64').toString('utf8')),
).toEqual({ defaultOrg: 'socket-docs' })
})
})

describe('read-only state', () => {
it('does not mark the config read-only when only the API token is overridden via env', () => {
// A token from SOCKET_CLI_API_TOKEN / SOCKET_SECURITY_API_TOKEN overrides
Expand Down
Loading