diff --git a/CLI/cmux.swift b/CLI/cmux.swift index 10073fc6cc7d..4441706781b4 100644 --- a/CLI/cmux.swift +++ b/CLI/cmux.swift @@ -7662,6 +7662,17 @@ struct CMUXCLI { let surfaceArg = sfArg ?? (wsArg == nil && windowRaw == nil ? ProcessInfo.processInfo.environment["CMUX_SURFACE_ID"] : nil) let keyArgs = rem2.first == "--" ? Array(rem2.dropFirst()) : rem2 guard let key = keyArgs.first else { throw CLIError(message: "send-key requires a key") } + if keyArgs.count > 1 { + let trailing = keyArgs.dropFirst().joined(separator: " ") + throw CLIError(message: String( + format: String( + localized: "cli.readSelection.error.unexpectedArguments", + defaultValue: "%@: unexpected arguments: %@" + ), + "send-key", + trailing + )) + } var params: [String: Any] = ["key": key] let winId = try normalizeWindowHandle(windowRaw, client: client) if let winId { params["window_id"] = winId } @@ -7706,6 +7717,17 @@ struct CMUXCLI { let skpArgs = rem2.first == "--" ? Array(rem2.dropFirst()) : rem2 let key = skpArgs.first ?? "" guard !key.isEmpty else { throw CLIError(message: "send-key-panel requires a key") } + if skpArgs.count > 1 { + let trailing = skpArgs.dropFirst().joined(separator: " ") + throw CLIError(message: String( + format: String( + localized: "cli.readSelection.error.unexpectedArguments", + defaultValue: "%@: unexpected arguments: %@" + ), + "send-key-panel", + trailing + )) + } var params: [String: Any] = ["key": key] let winId = try normalizeWindowHandle(windowRaw, client: client) if let winId { params["window_id"] = winId } diff --git a/cmuxCLITests/CLIExplicitSurfaceRoutingTests.swift b/cmuxCLITests/CLIExplicitSurfaceRoutingTests.swift index dda1fbd188f1..ac6fa5bae265 100644 --- a/cmuxCLITests/CLIExplicitSurfaceRoutingTests.swift +++ b/cmuxCLITests/CLIExplicitSurfaceRoutingTests.swift @@ -43,6 +43,34 @@ struct CLIExplicitSurfaceRoutingTests { ) } + @Test func sendKeyCommandsRejectExtraArgumentsWithoutSocketRequest() throws { + let cases: [[String]] = [ + ["send-key", "--surface", Self.targetSurfaceRef, "ctrl+c", "enter"], + ["send-key-panel", "--panel", Self.targetSurfaceRef, "ctrl+c", "enter"], + ] + + for (index, arguments) in cases.enumerated() { + let execution = try runMockCommand( + arguments: arguments, + socketName: "key-arity-\(index)" + ) { line in + Self.malformedRequestResponse(raw: line) + } + + let requests = try execution.state.requestObjects() + #expect(requests.isEmpty, Comment(rawValue: String(describing: requests))) + #expect(!execution.result.timedOut, Comment(rawValue: execution.result.stderr)) + #expect( + execution.result.status != 0, + Comment(rawValue: execution.result.stderr + execution.result.stdout) + ) + #expect( + execution.result.stderr.contains("unexpected arguments"), + Comment(rawValue: execution.result.stderr) + ) + } + } + @Test func numericSurfaceHandleStillInheritsCallerWorkspaceForIndexResolution() throws { let socketPath = Self.makeSocketPath("numeric") let listenerFD = try Self.bindUnixSocket(at: socketPath)