Import packages/plugin/src/harness-remote-bridge.ts

This commit is contained in:
QQ 2026-11-17 08:47:10 +08:00
parent e7b37ac811
commit cd33e6a12c
1 changed files with 24 additions and 8 deletions

View File

@ -74,10 +74,10 @@ const commandExecuteArgsSchema = z.object({
agentId: z.string().min(1).max(128),
line: z.string().min(1).max(2048),
// dsh-commands <= 0.1.2 calls this field `images`; dsh-commands 0.1.5
// renamed it to `submittedAttachments`. Only empty arrays are exposed by
// the Remote bridge; command attachments are deliberately out of scope.
// renamed it to `submittedAttachments`. Current Hosts resolve opaque file
// receipts in the receiving Session; legacy image arguments stay empty.
images: z.array(z.never()).length(0).optional(),
submittedAttachments: z.array(z.never()).length(0).optional(),
submittedAttachments: z.array(z.object({ type: z.literal('file'), receiptId: z.string().min(1).max(512) }).strict()).max(32).optional(),
}).strict().superRefine((value, context) => {
if (value.images !== undefined && value.submittedAttachments !== undefined) {
context.addIssue({
@ -159,6 +159,7 @@ export const HARNESS_REMOTE_ALLOWLIST = [
'schedule/deliveryLimits',
'schedule/edit',
'schedule/run',
'fileUploads/upload',
'session/attachment',
'session/cancel',
'session/canOpenWorkspacePath',
@ -305,12 +306,17 @@ export class HarnessRemoteBridge {
private readonly codexWorkspace?: CodexWorkspaceBridge,
private readonly resolveWorkspaceRoot?: NativeWorkspaceResolver,
private readonly schedule?: ScheduleCreateService,
private readonly uploadAttachment?: (body: unknown) => Promise<unknown>,
) {
this.terminalCarrier = usesDuplexTerminal(harnessVersion) ? new TerminalCarrierCompat(gateway) : undefined
}
async call(input: unknown): Promise<TypertRpcResult> {
const params = callSchema.parse(input) as HarnessRemoteCallParams
if ((params.endpoint === 'remoteUploads/upload' || params.endpoint === 'remoteImages/upload') && this.uploadAttachment !== undefined) {
const payload = z.object({ args: z.unknown() }).strict().parse(params.payload)
return { ok: true, value: await this.uploadAttachment(payload.args) }
}
this.assertAllowed(params.endpoint)
if (TERMINAL_STREAMS.has(params.endpoint)) throw new RpcError('METHOD_NOT_ALLOWED', 'Use a stream for this terminal endpoint.')
if (['settings/update', 'settings/replace', 'settings/mutate'].includes(params.endpoint)) {
@ -858,18 +864,18 @@ export class HarnessRemoteBridge {
if (baseSegments.some(segment => segment === '..')) {
throw workspaceFileFailure('WORKSPACE_PATH_DENIED', 'The requested workspace path is outside the workspace.', 'outside-root')
}
const safeBaseRel = await this.verifyWorkspacePathContainment(sessionWorkspace.root, rawBaseFile, endpoint)
const safeBaseRel = await this.verifyWorkspacePathContainment(sessionWorkspace.root, rawBaseFile, endpoint, sessionWorkspace.cwd)
const baseCandidate = resolve(sessionWorkspace.root, safeBaseRel)
const baseDir = dirname(baseCandidate)
const targetCandidate = resolve(baseDir, path)
safeRel = await this.verifyWorkspacePathContainment(sessionWorkspace.root, targetCandidate, endpoint)
safeRel = await this.verifyWorkspacePathContainment(sessionWorkspace.root, targetCandidate, endpoint, sessionWorkspace.cwd)
} else {
const path = typeof args.path === 'string' ? args.path : '.'
const pathSegments = process.platform === 'win32' ? path.replaceAll('\\', '/').split('/') : path.split('/')
if (pathSegments.some(segment => segment === '..')) {
throw workspaceFileFailure('WORKSPACE_PATH_DENIED', 'The requested workspace path is outside the workspace.', 'outside-root')
}
safeRel = await this.verifyWorkspacePathContainment(sessionWorkspace.root, path, endpoint)
safeRel = await this.verifyWorkspacePathContainment(sessionWorkspace.root, path, endpoint, sessionWorkspace.cwd)
}
let finalArgs: Record<string, unknown> = isRecord(rootPayload.args)
@ -939,8 +945,15 @@ export class HarnessRemoteBridge {
root: string,
path: string,
endpoint: string,
cwd = root,
): Promise<string> {
const candidate = isAbsolute(path) ? resolve(path) : resolve(root, path)
const supplied = isAbsolute(path) ? resolve(path) : resolve(root, path)
const cwdRelative = relative(resolve(cwd), supplied)
// Only the Host-authorized cwd is an accepted alias for the canonical root.
// Do not realpath arbitrary inputs: internal symlinks must still be denied.
const candidate = isAbsolute(path) && !isPathOutside(cwdRelative)
? resolve(root, cwdRelative)
: supplied
if (process.platform === 'win32') {
const rootDrive = parse(root).root.toLowerCase()
const candDrive = parse(candidate).root.toLowerCase()
@ -1245,13 +1258,16 @@ function requiresWorkspaceChangePath(version: string | undefined): boolean {
&& ((Number(match[2]) === 1 && Number(match[3]) >= 7) || Number(match[2]) === 2)
}
async function dispatchCommandForHost(
export async function dispatchCommandForHost(
gateway: LocalTypertGateway,
payload: unknown,
signal: AbortSignal,
harnessVersion?: string,
): Promise<TypertRpcResult> {
const parsed = commandExecutePayloadSchema.parse(payload)
if (harnessSessionGeneration(harnessVersion) !== 'v3' && (parsed.args.submittedAttachments?.length ?? 0) > 0) {
throw new RpcError('METHOD_NOT_ALLOWED', 'This Host does not support command file receipts.')
}
const legacyPayload = {
args: {
agentId: parsed.args.agentId,