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
22 changes: 11 additions & 11 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"strip-ansi": "^7.1.0",
"tsx": "^4.20.3",
"turndown": "^7.2.0",
"undici": "^7.29.0",
"undici": "^7.30.0",
"which": "^5.0.0",
"wrap-ansi": "^9.0.0",
"ws": "^8.21.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/config/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ export function getGlobalConfigCached(): GlobalConfig {
return CACHED_GLOBAL_CONFIG
}

export function checkHasTrustDialogAccepted(): boolean {
let currentPath = getCwd()
export function checkHasTrustDialogAccepted(startPath?: string): boolean {
let currentPath = resolve(startPath ?? getCwd())
const config = getConfig(getGlobalConfigFilePath(), DEFAULT_GLOBAL_CONFIG)

while (true) {
Expand Down
26 changes: 23 additions & 3 deletions packages/core/src/permissions/toolPermissionSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
createDefaultToolPermissionContext,
isPersistableToolPermissionDestination,
} from '#core/types/toolPermissionContext'
import { getCurrentProjectConfig } from '#core/utils/config'
import {
checkHasTrustDialogAccepted,
getCurrentProjectConfig,
} from '#core/utils/config'
import { getCwd } from '#core/utils/state'
import { logError } from '#core/utils/log'
import {
Expand Down Expand Up @@ -62,6 +65,11 @@ export function loadToolPermissionContextFromDisk(options?: {
homeDir?: string
includeKodeProjectConfig?: boolean
isBypassPermissionsModeAvailable?: boolean
/**
* Whether the workspace at `projectDir` is trusted. Defaults to the folder
* trust dialog state for `projectDir`.
*/
isWorkspaceTrusted?: boolean
}): ToolPermissionContext {
const projectDir = options?.projectDir ?? getCwd()
const homeDir = options?.homeDir
Expand All @@ -78,23 +86,35 @@ export function loadToolPermissionContextFromDisk(options?: {
'localSettings',
]

// projectSettings/localSettings live inside the opened project and are
// repository-controlled. Until the folder is trusted, only honor fields that
// restrict (deny/ask); never grant (allow/additionalDirectories), and do not
// migrate legacy files into .kode/.
const workspaceTrusted =
options?.isWorkspaceTrusted ?? checkHasTrustDialogAccepted(projectDir)

for (const destination of destinations) {
const isWorkspaceSourced =
destination === 'projectSettings' || destination === 'localSettings'
const mayGrant = !isWorkspaceSourced || workspaceTrusted

const settings = loadSettingsWithLegacyFallback({
destination,
projectDir,
homeDir,
migrateToPrimary: true,
migrateToPrimary: mayGrant,
}).settings as SettingsFileWithPermissions | null
const perms = settings?.permissions
const allow = uniqueStrings(perms?.allow)
const deny = uniqueStrings(perms?.deny)
const ask = uniqueStrings(perms?.ask)
const additionalDirectories = uniqueStrings(perms?.additionalDirectories)

if (allow.length > 0) base.alwaysAllowRules[destination] = allow
if (deny.length > 0) base.alwaysDenyRules[destination] = deny
if (ask.length > 0) base.alwaysAskRules[destination] = ask
if (!mayGrant) continue

if (allow.length > 0) base.alwaysAllowRules[destination] = allow
for (const dir of additionalDirectories) {
base.additionalWorkingDirectories.set(dir, {
path: dir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ describe('Bash permission dont-ask-again (prefix) parity', () => {
homeDir,
includeKodeProjectConfig: false,
isBypassPermissionsModeAvailable: false,
// Workspace-sourced allow rules are only honored in trusted folders.
isWorkspaceTrusted: true,
})

const result = await checkBashPermissions({
Expand Down
77 changes: 77 additions & 0 deletions packages/core/src/test/unit/tool-permission-settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,92 @@ function writeJson(filePath: string, value: unknown) {
describe('tool permission settings (multi-source load/merge/persist)', () => {
let projectDir: string
let homeDir: string
let configDir: string
let prevConfigDir: string | undefined

function setTrusted(trusted: boolean) {
writeJson(join(configDir, 'config.json'), {
projects: trusted
? { [projectDir]: { hasTrustDialogAccepted: true } }
: {},
})
}

beforeEach(() => {
projectDir = mkdtempSync(join(tmpdir(), 'kode-perm-project-'))
homeDir = mkdtempSync(join(tmpdir(), 'kode-perm-home-'))
configDir = mkdtempSync(join(tmpdir(), 'kode-perm-config-'))
prevConfigDir = process.env.KODE_CONFIG_DIR
process.env.KODE_CONFIG_DIR = configDir
setTrusted(true)
})

afterEach(() => {
if (prevConfigDir === undefined) delete process.env.KODE_CONFIG_DIR
else process.env.KODE_CONFIG_DIR = prevConfigDir
rmSync(projectDir, { recursive: true, force: true })
rmSync(homeDir, { recursive: true, force: true })
rmSync(configDir, { recursive: true, force: true })
})

test('untrusted workspace: repo settings cannot grant, but can restrict', () => {
setTrusted(false)
writeJson(join(homeDir, '.kode', 'settings.json'), {
permissions: { allow: ['Bash(ls:*)'] },
})
writeJson(join(projectDir, '.kode', 'settings.json'), {
permissions: {
allow: ['Bash(mkfifo:*)'],
deny: ['Bash(rm:*)'],
ask: ['Bash(curl:*)'],
additionalDirectories: ['/'],
},
})
writeJson(join(projectDir, '.kode', 'settings.local.json'), {
permissions: { allow: ['Read(~/**)'], additionalDirectories: ['/etc'] },
})

const ctx = loadToolPermissionContextFromDisk({
projectDir,
homeDir,
includeKodeProjectConfig: false,
})

expect(ctx.alwaysAllowRules.userSettings).toEqual(['Bash(ls:*)'])
expect(ctx.alwaysAllowRules.projectSettings).toBeUndefined()
expect(ctx.alwaysAllowRules.localSettings).toBeUndefined()
expect(ctx.alwaysDenyRules.projectSettings).toEqual(['Bash(rm:*)'])
expect(ctx.alwaysAskRules.projectSettings).toEqual(['Bash(curl:*)'])
expect(ctx.additionalWorkingDirectories.size).toBe(0)
})

test('untrusted workspace: legacy .claude settings are not granted or migrated', () => {
setTrusted(false)
writeJson(join(projectDir, '.claude', 'settings.json'), {
permissions: { allow: ['Bash(mkfifo:*)'] },
})

const ctx = loadToolPermissionContextFromDisk({
projectDir,
homeDir,
includeKodeProjectConfig: false,
})

expect(ctx.alwaysAllowRules.projectSettings).toBeUndefined()
expect(existsSync(join(projectDir, '.kode', 'settings.json'))).toBe(false)
})

test('trust is evaluated for projectDir, not process cwd', () => {
// process cwd (the repo) is not trusted; projectDir is.
writeJson(join(projectDir, '.kode', 'settings.json'), {
permissions: { allow: ['Bash(git:*)'] },
})
const ctx = loadToolPermissionContextFromDisk({
projectDir,
homeDir,
includeKodeProjectConfig: false,
})
expect(ctx.alwaysAllowRules.projectSettings).toEqual(['Bash(git:*)'])
})

test('loads .kode/settings.json + .kode/settings.local.json across sources', () => {
Expand Down
Loading