Fix paste ownership, input bounds, and IPC validation
Supersedes #5745, #5746, and #5747.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import {
|
||||
CLIPBOARD_TEXT_MEASURE_YIELD_CODE_UNITS,
|
||||
CLIPBOARD_TEXT_WRITE_TOO_LARGE_ERROR,
|
||||
assertClipboardTextWriteWithinLimit,
|
||||
assertClipboardTextWriteWithinLimitWithYield,
|
||||
isClipboardTextWriteTooLargeError
|
||||
} from '../../shared/clipboard-text'
|
||||
import { RuntimeClientError } from './runtime-client-error'
|
||||
|
||||
export function validateComputerClipboardPasteText(text: string): void {
|
||||
try {
|
||||
assertClipboardTextWriteWithinLimit(text)
|
||||
} catch (error) {
|
||||
if (isClipboardTextWriteTooLargeError(error)) {
|
||||
throw new RuntimeClientError('invalid_argument', CLIPBOARD_TEXT_WRITE_TOO_LARGE_ERROR)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function validateComputerClipboardPasteTextWithYield(text: string): Promise<void> {
|
||||
try {
|
||||
// Why: accepted paste-sized payloads must not monopolize the main process
|
||||
// while preserving the provider-facing invalid_argument error contract.
|
||||
await assertClipboardTextWriteWithinLimitWithYield(text)
|
||||
} catch (error) {
|
||||
if (isClipboardTextWriteTooLargeError(error)) {
|
||||
throw new RuntimeClientError('invalid_argument', CLIPBOARD_TEXT_WRITE_TOO_LARGE_ERROR)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export function validateComputerClipboardPasteTextWithBoundedYield(
|
||||
text: string
|
||||
): Promise<void> | void {
|
||||
if (text.length <= CLIPBOARD_TEXT_MEASURE_YIELD_CODE_UNITS) {
|
||||
validateComputerClipboardPasteText(text)
|
||||
return
|
||||
}
|
||||
return validateComputerClipboardPasteTextWithYield(text)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
computerUseHotkeyValidationMessage,
|
||||
computerUsePressKeyValidationMessage
|
||||
} from '../../shared/computer-use-key-spec'
|
||||
import { validateComputerClipboardPasteTextWithBoundedYield } from './computer-clipboard-paste-validation'
|
||||
import { RuntimeClientError } from './runtime-client-error'
|
||||
|
||||
type ComputerProviderActionMethod =
|
||||
@@ -15,10 +16,10 @@ type ComputerProviderActionMethod =
|
||||
| 'pasteText'
|
||||
| 'setValue'
|
||||
|
||||
export function validateComputerProviderActionParams(
|
||||
export async function validateComputerProviderActionParams(
|
||||
method: ComputerProviderActionMethod,
|
||||
params: Record<string, unknown>
|
||||
): string {
|
||||
): Promise<string> {
|
||||
const app = requireNonEmptyString(params, 'app')
|
||||
validateWindowTarget(params)
|
||||
switch (method) {
|
||||
@@ -40,9 +41,11 @@ export function validateComputerProviderActionParams(
|
||||
validateDragTarget(params)
|
||||
return app
|
||||
case 'typeText':
|
||||
case 'pasteText':
|
||||
requireNonEmptyString(params, 'text')
|
||||
return app
|
||||
case 'pasteText':
|
||||
await validatePasteText(params)
|
||||
return app
|
||||
case 'pressKey':
|
||||
validatePressKey(params)
|
||||
return app
|
||||
@@ -182,6 +185,11 @@ function validateHotkey(params: Record<string, unknown>): void {
|
||||
}
|
||||
}
|
||||
|
||||
function validatePasteText(params: Record<string, unknown>): Promise<void> | void {
|
||||
const text = requireNonEmptyString(params, 'text')
|
||||
return validateComputerClipboardPasteTextWithBoundedYield(text)
|
||||
}
|
||||
|
||||
function requireStringAllowingEmpty(params: Record<string, unknown>, key: string): string {
|
||||
const value = params[key]
|
||||
if (typeof value !== 'string') {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { validateComputerClipboardPasteTextWithBoundedYield } from './computer-clipboard-paste-validation'
|
||||
|
||||
export function validateComputerSidecarPasteText(
|
||||
method: string,
|
||||
params: unknown
|
||||
): Promise<void> | void {
|
||||
if (method !== 'pasteText' || !params || typeof params !== 'object') {
|
||||
return
|
||||
}
|
||||
const text = (params as Record<string, unknown>).text
|
||||
if (typeof text !== 'string') {
|
||||
return
|
||||
}
|
||||
return validateComputerClipboardPasteTextWithBoundedYield(text)
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import {
|
||||
CLIPBOARD_TEXT_WRITE_MAX_BYTES,
|
||||
CLIPBOARD_TEXT_WRITE_TOO_LARGE_ERROR
|
||||
} from '../../shared/clipboard-text'
|
||||
import {
|
||||
createDesktopScriptProviderClient,
|
||||
expectDesktopProviderSubprocessStartCount,
|
||||
@@ -175,6 +179,15 @@ describe('DesktopScriptProviderClient action errors', () => {
|
||||
message: expect.stringContaining('Missing text')
|
||||
}
|
||||
)
|
||||
await expect(
|
||||
client.action('pasteText', {
|
||||
app: 'Text Editor',
|
||||
text: ['native-provider-secret', 'x'.repeat(CLIPBOARD_TEXT_WRITE_MAX_BYTES + 1)].join('')
|
||||
})
|
||||
).rejects.toMatchObject({
|
||||
code: 'invalid_argument',
|
||||
message: CLIPBOARD_TEXT_WRITE_TOO_LARGE_ERROR
|
||||
})
|
||||
await expect(
|
||||
client.action('pressKey', { app: 'Text Editor', key: 'CmdOrCtrl+V' })
|
||||
).rejects.toMatchObject({
|
||||
|
||||
@@ -125,7 +125,7 @@ export class DesktopScriptProviderClient {
|
||||
method: NativeActionMethod,
|
||||
params: Record<string, unknown>
|
||||
): Promise<ComputerActionResult> {
|
||||
const app = validateComputerProviderActionParams(method, params)
|
||||
const app = await validateComputerProviderActionParams(method, params)
|
||||
const explicitWindowId = optionalNumberParam(params, 'windowId')
|
||||
const explicitWindowIndex = optionalNumberParam(params, 'windowIndex')
|
||||
const current = this.snapshotStore.current(app, explicitWindowId, params)
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { CLIPBOARD_TEXT_MEASURE_YIELD_CODE_UNITS } from '../../shared/clipboard-text'
|
||||
import {
|
||||
createDesktopScriptProviderClient,
|
||||
expectDesktopProviderSubprocessStartCount,
|
||||
mockBridgeResponse,
|
||||
resetDesktopScriptProviderTestHarness,
|
||||
sampleBridgeSnapshot,
|
||||
sampleCapabilities
|
||||
} from './desktop-script-provider-test-harness'
|
||||
|
||||
describe('DesktopScriptProviderClient paste validation', () => {
|
||||
afterEach(resetDesktopScriptProviderTestHarness)
|
||||
|
||||
it('yields while validating large accepted pasteText payloads before launching the provider', async () => {
|
||||
vi.useFakeTimers()
|
||||
mockBridgeResponse({
|
||||
ok: true,
|
||||
capabilities: sampleCapabilities()
|
||||
})
|
||||
mockBridgeResponse({
|
||||
ok: true,
|
||||
action: {
|
||||
path: 'clipboard',
|
||||
actionName: 'paste',
|
||||
fallbackReason: null
|
||||
},
|
||||
snapshot: sampleBridgeSnapshot('Text Editor', 'pasted')
|
||||
})
|
||||
const client = await createDesktopScriptProviderClient('linux', '/tmp/runtime.py')
|
||||
const text = 'é'.repeat(CLIPBOARD_TEXT_MEASURE_YIELD_CODE_UNITS + 1)
|
||||
|
||||
const call = client.action('pasteText', {
|
||||
app: 'Text Editor',
|
||||
text,
|
||||
noScreenshot: true
|
||||
})
|
||||
await Promise.resolve()
|
||||
|
||||
expectDesktopProviderSubprocessStartCount(0)
|
||||
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
await expect(call).resolves.toMatchObject({
|
||||
action: { path: 'clipboard' }
|
||||
})
|
||||
expectDesktopProviderSubprocessStartCount(2)
|
||||
})
|
||||
})
|
||||
@@ -56,7 +56,7 @@ export class MacOSNativeProviderClient {
|
||||
return (await this.call('getAppState', params)) as ComputerSnapshotResult
|
||||
}
|
||||
async action(method: NativeActionMethod, params: unknown): Promise<ComputerActionResult> {
|
||||
validateComputerProviderActionParams(
|
||||
await validateComputerProviderActionParams(
|
||||
method,
|
||||
params && typeof params === 'object' ? (params as Record<string, unknown>) : {}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { CLIPBOARD_TEXT_MEASURE_YIELD_CODE_UNITS } from '../../shared/clipboard-text'
|
||||
|
||||
const {
|
||||
chmodSyncMock,
|
||||
connectMacOSProviderSocketMock,
|
||||
mkdtempSyncMock,
|
||||
resolveMacOSComputerUseExecutablePathMock,
|
||||
rmSyncMock,
|
||||
spawnMock,
|
||||
writeFileSyncMock
|
||||
} = vi.hoisted(() => ({
|
||||
chmodSyncMock: vi.fn(),
|
||||
connectMacOSProviderSocketMock: vi.fn(),
|
||||
mkdtempSyncMock: vi.fn(),
|
||||
resolveMacOSComputerUseExecutablePathMock: vi.fn(),
|
||||
rmSyncMock: vi.fn(),
|
||||
spawnMock: vi.fn(),
|
||||
writeFileSyncMock: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('child_process', () => ({
|
||||
spawn: spawnMock
|
||||
}))
|
||||
|
||||
vi.mock('fs', () => ({
|
||||
chmodSync: chmodSyncMock,
|
||||
mkdtempSync: mkdtempSyncMock,
|
||||
rmSync: rmSyncMock,
|
||||
writeFileSync: writeFileSyncMock
|
||||
}))
|
||||
|
||||
vi.mock('./macos-native-provider-paths', () => ({
|
||||
resolveMacOSComputerUseExecutablePath: resolveMacOSComputerUseExecutablePathMock
|
||||
}))
|
||||
|
||||
vi.mock('./macos-native-provider-socket', () => ({
|
||||
connectMacOSProviderSocket: connectMacOSProviderSocketMock
|
||||
}))
|
||||
|
||||
class FakeSocket extends EventEmitter {
|
||||
destroyed = false
|
||||
writes: string[] = []
|
||||
|
||||
setEncoding(): void {}
|
||||
|
||||
write(line: string, callback?: (error?: Error | null) => void): boolean {
|
||||
this.writes.push(line)
|
||||
callback?.(null)
|
||||
return true
|
||||
}
|
||||
|
||||
end(): void {
|
||||
this.destroyed = true
|
||||
}
|
||||
|
||||
destroy(): this {
|
||||
this.destroyed = true
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
class FakeProvider extends EventEmitter {
|
||||
kill = vi.fn()
|
||||
unref = vi.fn()
|
||||
}
|
||||
|
||||
async function loadClientModule() {
|
||||
vi.resetModules()
|
||||
return await import('./macos-native-provider-client')
|
||||
}
|
||||
|
||||
function macOSProviderCapabilities() {
|
||||
return {
|
||||
platform: 'darwin',
|
||||
provider: 'orca-computer-use-macos',
|
||||
providerVersion: '1.0.0',
|
||||
protocolVersion: 1,
|
||||
supports: {
|
||||
actions: {
|
||||
pasteText: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe('MacOSNativeProviderClient paste validation', () => {
|
||||
const sockets: FakeSocket[] = []
|
||||
const providers: FakeProvider[] = []
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
sockets.length = 0
|
||||
providers.length = 0
|
||||
mkdtempSyncMock.mockImplementation((prefix: string) => `${prefix}${sockets.length}`)
|
||||
resolveMacOSComputerUseExecutablePathMock.mockReturnValue(
|
||||
'/Applications/Orca Computer Use.app/Contents/MacOS/orca-computer-use-macos'
|
||||
)
|
||||
spawnMock.mockImplementation(() => {
|
||||
const provider = new FakeProvider()
|
||||
providers.push(provider)
|
||||
return provider
|
||||
})
|
||||
connectMacOSProviderSocketMock.mockImplementation(async () => {
|
||||
const socket = new FakeSocket()
|
||||
sockets.push(socket)
|
||||
return socket
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
chmodSyncMock.mockReset()
|
||||
connectMacOSProviderSocketMock.mockReset()
|
||||
mkdtempSyncMock.mockReset()
|
||||
resolveMacOSComputerUseExecutablePathMock.mockReset()
|
||||
rmSyncMock.mockReset()
|
||||
spawnMock.mockReset()
|
||||
writeFileSyncMock.mockReset()
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('yields while validating large accepted pasteText payloads before starting the helper', async () => {
|
||||
const { MacOSNativeProviderClient } = await loadClientModule()
|
||||
const client = new MacOSNativeProviderClient()
|
||||
const text = 'é'.repeat(CLIPBOARD_TEXT_MEASURE_YIELD_CODE_UNITS + 1)
|
||||
|
||||
const call = client.action('pasteText', { app: 'TextEdit', text })
|
||||
await Promise.resolve()
|
||||
|
||||
expect(providers).toHaveLength(0)
|
||||
expect(sockets).toHaveLength(0)
|
||||
expect(spawnMock).not.toHaveBeenCalled()
|
||||
expect(connectMacOSProviderSocketMock).not.toHaveBeenCalled()
|
||||
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
await vi.waitFor(() => expect(sockets).toHaveLength(1))
|
||||
const socket = sockets[0]!
|
||||
await vi.waitFor(() => expect(socket.writes).toHaveLength(1))
|
||||
const handshakeRequest = JSON.parse(socket.writes[0]!) as { id: number }
|
||||
socket.emit(
|
||||
'data',
|
||||
`${JSON.stringify({
|
||||
id: handshakeRequest.id,
|
||||
ok: true,
|
||||
result: macOSProviderCapabilities()
|
||||
})}\n`
|
||||
)
|
||||
await vi.waitFor(() => expect(socket.writes).toHaveLength(2))
|
||||
const actionRequest = JSON.parse(socket.writes[1]!) as { id: number }
|
||||
socket.emit(
|
||||
'data',
|
||||
`${JSON.stringify({
|
||||
id: actionRequest.id,
|
||||
ok: true,
|
||||
result: { action: { path: 'clipboard', actionName: 'paste' } }
|
||||
})}\n`
|
||||
)
|
||||
|
||||
await expect(call).resolves.toMatchObject({
|
||||
action: { path: 'clipboard' }
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,10 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
CLIPBOARD_TEXT_MEASURE_YIELD_CODE_UNITS,
|
||||
CLIPBOARD_TEXT_WRITE_MAX_BYTES,
|
||||
CLIPBOARD_TEXT_WRITE_TOO_LARGE_ERROR
|
||||
} from '../../shared/clipboard-text'
|
||||
import {
|
||||
callComputerSidecarAction,
|
||||
callComputerSidecarCapabilities,
|
||||
@@ -235,6 +240,45 @@ describe('computer sidecar client', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects oversized pasteText payloads before forking the sidecar', async () => {
|
||||
const secret = 'sidecar-secret-token'
|
||||
|
||||
await expect(
|
||||
callComputerSidecarAction('pasteText', {
|
||||
app: 'Finder',
|
||||
text: secret + 'x'.repeat(CLIPBOARD_TEXT_WRITE_MAX_BYTES + 1)
|
||||
})
|
||||
).rejects.toMatchObject({
|
||||
code: 'invalid_argument',
|
||||
message: CLIPBOARD_TEXT_WRITE_TOO_LARGE_ERROR
|
||||
})
|
||||
|
||||
expect(children).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('yields while validating large accepted pasteText payloads before forking', async () => {
|
||||
const text = 'é'.repeat(CLIPBOARD_TEXT_MEASURE_YIELD_CODE_UNITS + 1)
|
||||
|
||||
const call = callComputerSidecarAction('pasteText', { app: 'Finder', text })
|
||||
await Promise.resolve()
|
||||
|
||||
expect(children).toHaveLength(0)
|
||||
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
expect(children).toHaveLength(1)
|
||||
const child = children[0]!
|
||||
const request = child.sent[0]!
|
||||
child.emit('message', {
|
||||
id: request.id,
|
||||
ok: true,
|
||||
result: { action: { path: 'clipboard', actionName: 'paste' } }
|
||||
})
|
||||
|
||||
await expect(call).resolves.toMatchObject({
|
||||
action: { path: 'clipboard' }
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects queued requests after the active request times out', async () => {
|
||||
const firstCall = callComputerSidecarCapabilities()
|
||||
const secondCall = callComputerSidecarCapabilities()
|
||||
|
||||
@@ -8,6 +8,7 @@ import type {
|
||||
ComputerSnapshotResult
|
||||
} from '../../shared/runtime-types'
|
||||
import { normalizeComputerActionResult } from './computer-action-verification-normalization'
|
||||
import { validateComputerSidecarPasteText } from './computer-sidecar-paste-validation'
|
||||
import { RuntimeClientError } from './runtime-client-error'
|
||||
|
||||
type ComputerSidecarMethod =
|
||||
@@ -85,6 +86,10 @@ export async function callComputerSidecarAction(
|
||||
>,
|
||||
params: unknown
|
||||
): Promise<ComputerActionResult> {
|
||||
const validation = validateComputerSidecarPasteText(method, params)
|
||||
if (validation) {
|
||||
await validation
|
||||
}
|
||||
return normalizeComputerActionResult(
|
||||
(await getComputerSidecar().call(method, params)) as ComputerActionResult
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user