fix(voice-action): route plans through the LLM gateway and validate what reaches the shell

The feature is not wired to the UI yet but is reachable over IPC:
- planning called the premium proxy directly, sending speech to the cloud
  even with the local backend selected and failing in local mode
- LLM-provided app names and key combos were spliced into cmd/PowerShell
  strings; they are now validated, and combos map named keys (alt+tab no
  longer types Alt+T, a, b)
- presets matched substrings and bare nouns ("commute" toggled mute,
  "크롬 캐시 지워줘" opened Chrome); they now match whole words only
- a plan without safe=true is treated as unsafe and unsafe shell commands
  are not run; URLs go through openExternalSafe
This commit is contained in:
Yun Chan 2026-10-21 11:28:30 +09:00
parent 9cf3fd7317
commit 29d22504ae
3 changed files with 158 additions and 27 deletions

View file

@ -6,7 +6,9 @@ import { EventEmitter } from 'events'
import { exec } from 'child_process'
import { shell } from 'electron'
import { getLogger } from './LoggerService'
import { getPremiumLLMService } from './PremiumLLMService'
import { getLlmGateway } from './llm/LlmGateway'
import { openExternalSafe } from '../windows/web-contents-hardening'
import { isSafeAppName, matchVoiceActionPreset, toSendKeys } from './voice-action-policy'
import { getMainWindow } from '../windows/WindowManager'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { D3ROError, ErrorCode } from '@d3ro/core/errors'
@ -54,15 +56,15 @@ If the command is unclear, output: {"action": "type_text", "target": "", "descri
/** 프리셋 명령어 (LLM 없이 바로 실행) */
const PRESETS: VoiceActionPreset[] = [
{
keywords: ['크롬 열어', '크롬', 'chrome', 'open chrome'],
keywords: ['크롬 열어', 'open chrome'],
action: { action: 'open_app', target: 'chrome', description: 'Open Chrome browser', safe: true },
},
{
keywords: ['메모장 열어', '메모장', 'notepad', 'open notepad'],
keywords: ['메모장 열어', 'open notepad'],
action: { action: 'open_app', target: 'notepad', description: 'Open Notepad', safe: true },
},
{
keywords: ['탐색기 열어', '탐색기', 'explorer', 'open explorer'],
keywords: ['탐색기 열어', 'open explorer'],
action: { action: 'open_app', target: 'explorer', description: 'Open File Explorer', safe: true },
},
{
@ -74,7 +76,7 @@ const PRESETS: VoiceActionPreset[] = [
action: { action: 'keyboard_shortcut', target: 'volumedown', description: 'Volume down', safe: true },
},
{
keywords: ['음소거', 'mute'],
keywords: ['음소거', 'mute sound', 'toggle mute'],
action: { action: 'keyboard_shortcut', target: 'volumemute', description: 'Toggle mute', safe: true },
},
]
@ -128,10 +130,9 @@ class VoiceActionService extends EventEmitter {
return
}
// 2. LLM으로 액션 플랜 생성
// 2. LLM으로 액션 플랜 생성 — 게이트웨이를 거쳐야 '로컬' 백엔드 선택 시 말한 내용이 클라우드로 나가지 않는다.
try {
const llmService = getPremiumLLMService()
const result = await llmService.generate(text, {
const result = await getLlmGateway().generate(text, {
systemPrompt: ACTION_SYSTEM_PROMPT,
temperature: 0.1,
})
@ -164,15 +165,7 @@ class VoiceActionService extends EventEmitter {
}
private _matchPreset(text: string): VoiceActionPreset | null {
const lower = text.toLowerCase().trim()
for (const preset of PRESETS) {
for (const keyword of preset.keywords) {
if (lower.includes(keyword.toLowerCase())) {
return preset
}
}
}
return null
return matchVoiceActionPreset(text, PRESETS)
}
private _parsePlan(llmOutput: string): VoiceActionPlan | null {
@ -188,7 +181,8 @@ class VoiceActionService extends EventEmitter {
action: parsed.action as VoiceActionPlan['action'],
target: String(parsed.target),
description: String(parsed.description ?? ''),
safe: parsed.safe !== false,
// safe 를 빠뜨린 응답은 안전하다고 보지 않는다.
safe: parsed.safe === true,
}
} catch {
return null
@ -196,7 +190,12 @@ class VoiceActionService extends EventEmitter {
}
private async _executePlan(plan: VoiceActionPlan, userText: string): Promise<void> {
// 차단된 명령어 체크
// 차단된 명령어 체크 — 모델이 위험하다고 한 셸 명령은 실행하지 않는다.
if (plan.action === 'system_command' && !plan.safe) {
this._sendToRenderer(IPC_CHANNELS.VOICE_ACTION.ACTION_PLANNED, { plan, userText })
logger.warn(`Voice action blocked (unsafe): ${plan.target}`)
return
}
if (plan.action === 'system_command') {
const targetLower = plan.target.toLowerCase()
for (const blocked of BLOCKED_COMMANDS) {
@ -217,7 +216,7 @@ class VoiceActionService extends EventEmitter {
await this._openApp(plan.target)
break
case 'open_url':
await shell.openExternal(plan.target)
if (!(await openExternalSafe(plan.target))) throw new Error(`Blocked URL: ${plan.target}`)
break
case 'open_file':
await shell.openPath(plan.target)
@ -256,6 +255,7 @@ class VoiceActionService extends EventEmitter {
}
private _openApp(appName: string): Promise<void> {
if (!isSafeAppName(appName)) return Promise.reject(new Error(`Unsupported app name: ${appName}`))
return new Promise((resolve, reject) => {
const cmd = `start "" "${appName}"`
exec(cmd, { shell: 'cmd.exe', windowsHide: true }, (err) => {
@ -306,13 +306,13 @@ class VoiceActionService extends EventEmitter {
return
}
// 일반 키보드 단축키 (ctrl+c 등)
if (keys.includes('ctrl')) sendKeysStr += '^'
if (keys.includes('alt')) sendKeysStr += '%'
if (keys.includes('shift')) sendKeysStr += '+'
const key = keys.replace(/ctrl\+|alt\+|shift\+/g, '').trim()
sendKeysStr += key
// 일반 키보드 단축키 (ctrl+c 등). 변환표 밖의 값은 PowerShell 문자열에 넣지 않는다.
const converted = toSendKeys(keys)
if (converted === null) {
reject(new Error(`Unsupported key combo: ${combo}`))
return
}
sendKeysStr = converted
const script = `
$wshell = New-Object -ComObject WScript.Shell

View file

@ -0,0 +1,69 @@
// src/main/services/voice-action-policy.ts
// 음성 액션의 순수 판정 — 프리셋 매칭, 셸로 넘길 문자열 검증, SendKeys 변환.
// target 은 LLM 이 만든 값이라 그대로 셸에 넣으면 인용을 깨고 다른 명령을 붙일 수 있다.
import type { VoiceActionPreset } from '@d3ro/core/types'
/**
* 프리셋 키워드가 문장 안에 '단어로' 있을 때만 맞춘다.
* 부분 문자열로 맞추면 "commute" 의 mute 가 음소거를 켠다.
*/
export function matchVoiceActionPreset(text: string, presets: readonly VoiceActionPreset[]): VoiceActionPreset | null {
const words = ` ${text.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, ' ').trim()} `
for (const preset of presets) {
for (const keyword of preset.keywords) {
const normalized = keyword.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, ' ').trim()
if (normalized && words.includes(` ${normalized} `)) return preset
}
}
return null
}
/** `start "" "<앱>"` 에 넣어도 인용이 깨지지 않는 앱 이름(글자·숫자·공백·._-)만 허용한다. */
export function isSafeAppName(appName: string): boolean {
return /^[\p{L}\p{N} ._-]{1,64}$/u.test(appName) && appName.trim().length > 0
}
const NAMED_KEYS: Readonly<Record<string, string>> = {
tab: '{TAB}',
enter: '{ENTER}',
return: '{ENTER}',
esc: '{ESC}',
escape: '{ESC}',
delete: '{DEL}',
del: '{DEL}',
backspace: '{BACKSPACE}',
space: ' ',
up: '{UP}',
down: '{DOWN}',
left: '{LEFT}',
right: '{RIGHT}',
home: '{HOME}',
end: '{END}',
pageup: '{PGUP}',
pagedown: '{PGDN}',
insert: '{INSERT}',
}
/**
* "ctrl+shift+esc" 같은 조합을 WScript.Shell SendKeys 문자열로 바꾼다. 못 바꾸면 null.
* 키 이름을 그대로 이어 붙이면 alt+tab 이 Alt+T 뒤 a·b 입력이 된다.
*/
export function toSendKeys(combo: string): string | null {
const parts = combo.toLowerCase().split('+').map((p) => p.trim()).filter(Boolean)
const key = parts.pop()
if (!key) return null
let prefix = ''
for (const mod of parts) {
if (mod === 'ctrl' || mod === 'control') prefix += '^'
else if (mod === 'alt') prefix += '%'
else if (mod === 'shift') prefix += '+'
else return null
}
const fn = /^f([1-9]|1[0-6])$/.exec(key)
if (fn) return `${prefix}{F${fn[1]}}`
const named = NAMED_KEYS[key]
if (named) return prefix + named
if (/^[a-z0-9]$/.test(key)) return prefix + key
return null
}

View file

@ -0,0 +1,62 @@
// tests/main/services/voice-action-policy.test.ts
// 음성 액션 순수 판정: 프리셋은 단어로만 맞추고, 셸에 넘길 앱 이름·키 조합은 검증·변환한다.
import { describe, expect, it } from 'vitest'
import type { VoiceActionPreset } from '@d3ro/core/types'
import { isSafeAppName, matchVoiceActionPreset, toSendKeys } from '../../../src/main/services/voice-action-policy'
const PRESETS: VoiceActionPreset[] = [
{
keywords: ['크롬 열어', 'open chrome'],
action: { action: 'open_app', target: 'chrome', description: 'Open Chrome', safe: true },
},
{
keywords: ['음소거', 'mute sound'],
action: { action: 'keyboard_shortcut', target: 'volumemute', description: 'Mute', safe: true },
},
]
describe('matchVoiceActionPreset', () => {
it('키워드가 단어로 있으면 맞춘다', () => {
expect(matchVoiceActionPreset('크롬 열어줘', PRESETS)).toBeNull()
expect(matchVoiceActionPreset('크롬 열어', PRESETS)?.action.target).toBe('chrome')
expect(matchVoiceActionPreset('Please open Chrome.', PRESETS)?.action.target).toBe('chrome')
expect(matchVoiceActionPreset('음소거', PRESETS)?.action.target).toBe('volumemute')
})
it('다른 단어 속 부분 문자열은 맞추지 않는다', () => {
expect(matchVoiceActionPreset('my commute sound was loud', PRESETS)).toBeNull()
expect(matchVoiceActionPreset('크롬 캐시 지워줘', PRESETS)).toBeNull()
})
})
describe('isSafeAppName', () => {
it('평범한 앱 이름은 허용한다', () => {
expect(isSafeAppName('notepad')).toBe(true)
expect(isSafeAppName('Visual Studio Code')).toBe(true)
expect(isSafeAppName('메모장')).toBe(true)
})
it('인용을 깨거나 명령을 이어 붙일 수 있는 값은 거부한다', () => {
expect(isSafeAppName('notepad" & calc & "')).toBe(false)
expect(isSafeAppName('a|b')).toBe(false)
expect(isSafeAppName('%COMSPEC%')).toBe(false)
expect(isSafeAppName(' ')).toBe(false)
})
})
describe('toSendKeys', () => {
it('수정자와 이름 있는 키를 SendKeys 표기로 바꾼다', () => {
expect(toSendKeys('ctrl+c')).toBe('^c')
expect(toSendKeys('alt+tab')).toBe('%{TAB}')
expect(toSendKeys('ctrl+shift+esc')).toBe('^+{ESC}')
expect(toSendKeys('f5')).toBe('{F5}')
expect(toSendKeys('enter')).toBe('{ENTER}')
})
it('모르는 키·수정자나 따옴표가 든 값은 null 이다', () => {
expect(toSendKeys("ctrl+'); Remove-Item x; ('")).toBeNull()
expect(toSendKeys('win+r')).toBeNull()
expect(toSendKeys('')).toBeNull()
})
})