Check terminal request pids

This commit is contained in:
crschnick
2026-09-22 23:49:56 +00:00
parent 54d386c9d8
commit 121049e898
3 changed files with 26 additions and 1 deletions
@@ -23,6 +23,7 @@ public class TerminalLaunchExchange extends BeaconInterface<TerminalLaunchExchan
@Override
public Object handle(HttpExchange exchange, Request msg) throws BeaconClientException, BeaconServerException {
TerminalLauncherManager.verifyPid(msg.getRequest(), msg.getPid());
var r = TerminalLauncherManager.launchExchange(msg.getRequest());
return Response.builder().targetFile(r).build();
}
@@ -38,6 +39,8 @@ public class TerminalLaunchExchange extends BeaconInterface<TerminalLaunchExchan
public static class Request {
@NonNull
UUID request;
long pid;
}
@Jacksonized
@@ -1,9 +1,11 @@
package io.xpipe.app.beacon.api;
import io.xpipe.app.beacon.BeaconClientException;
import io.xpipe.app.beacon.BeaconInterface;
import io.xpipe.app.prefs.AppPrefs;
import com.sun.net.httpserver.HttpExchange;
import io.xpipe.app.terminal.TerminalLauncherManager;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
@@ -19,7 +21,8 @@ public class TerminalPrepareExchange extends BeaconInterface<TerminalPrepareExch
}
@Override
public Object handle(HttpExchange exchange, Request msg) {
public Object handle(HttpExchange exchange, Request msg) throws BeaconClientException {
TerminalLauncherManager.verifyPid(msg.getRequest(), msg.getPid());
var term = AppPrefs.get().terminalType().getValue();
var unicode = term.supportsUnicode();
var escapes = term.supportsEscapes();
@@ -89,6 +89,25 @@ public class TerminalLauncherManager {
req.setShellPid(shell.pid());
}
public static void verifyPid(UUID request, long pid) throws BeaconClientException {
TerminalLaunchRequest req;
synchronized (entries) {
req = entries.get(request);
}
if (req.getShellPid() != -1) {
ProcessHandle current = ProcessHandle.of(pid).orElseThrow();
do {
if (current.pid() == req.getShellPid()) {
return;
}
current = current.parent().orElse(null);
} while (current != null);
}
throw new BeaconClientException("Wrong launch context");
}
public static void waitExchange(UUID request) throws BeaconServerException {
TerminalLaunchRequest req;
synchronized (entries) {