diff --git a/CHANGELOG.md b/CHANGELOG.md index a8ffa1b9f..7419b7c29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/utils/config.mts b/src/utils/config.mts index 0aa362060..1e670e2fd 100644 --- a/src/utils/config.mts +++ b/src/utils/config.mts @@ -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' @@ -64,6 +70,10 @@ export interface LocalConfig { org?: string | undefined } +const CONFIG_FILE_MODE = 0o600 + +const SETTINGS_DIR_MODE = 0o700 + const sensitiveConfigKeyLookup: Set = new Set([ CONFIG_KEY_API_TOKEN, ]) @@ -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 @@ -127,7 +141,7 @@ function getConfigValues(): LocalConfig { updateConfigValue(CONFIG_KEY_API_TOKEN, token) } } else { - mkdirSync(socketAppDataPath, { recursive: true }) + ensureSettingsDirSync(socketAppDataPath) } } } @@ -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) + } +} + export function findSocketYmlSync( dir = process.cwd(), ): CResult { @@ -383,7 +411,7 @@ export function updateConfigValue( 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) @@ -441,7 +469,7 @@ export function updateConfigValue( undefined, indent, ).replace(/\n/g, newline) - writeFileSync( + writeConfigFileSync( configFilePath, Buffer.from(jsonContent + newline).toString('base64'), ) diff --git a/test/unit/src/utils/config.test.mts b/test/unit/src/utils/config.test.mts index fd5432bed..c7dbc4901 100644 --- a/test/unit/src/utils/config.test.mts +++ b/test/unit/src/utils/config.test.mts @@ -1,9 +1,11 @@ import { + chmodSync, promises as fs, mkdirSync, mkdtempSync, readFileSync, rmSync, + statSync, writeFileSync, } from 'node:fs' import os from 'node:os' @@ -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) @@ -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