Skip to content

Commit 2fe2cd9

Browse files
edburnsCopilot
andcommitted
Fix CodeQL relative-path-command alerts in test code
Use absolute paths for system commands (cat → /usr/bin/cat, cmd → COMSPEC env var) and resolve npx via PATH search in CapiProxy to avoid executing commands with relative paths. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c4e523e3-9d39-4598-90ec-54d959c44ce8
1 parent 2bcafd0 commit 2fe2cd9

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

java/sdk/src/test/java/com/github/copilot/CapiProxy.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ public String start() throws IOException, InterruptedException {
9393
// Start the harness server using npx tsx
9494
// On Windows, npx is installed as npx.cmd which requires cmd /c to launch
9595
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
96+
String npxPath = resolveCommand(isWindows ? "npx.cmd" : "npx");
9697
var pb = isWindows
97-
? new ProcessBuilder("cmd", "/c", "npx", "tsx", "server.ts")
98-
: new ProcessBuilder("npx", "tsx", "server.ts");
98+
? new ProcessBuilder(System.getenv("COMSPEC"), "/c", npxPath, "tsx", "server.ts")
99+
: new ProcessBuilder(npxPath, "tsx", "server.ts");
99100
pb.directory(harnessDir.toFile());
100101
pb.redirectErrorStream(false);
101102
// Tell the replaying proxy to fail fast on unmatched requests rather than
@@ -516,6 +517,24 @@ private Path findHarnessDirectory() {
516517
return null;
517518
}
518519

520+
/**
521+
* Resolves a command name to its absolute path by searching the system
522+
* {@code PATH}. Falls back to the original name if not found.
523+
*/
524+
private static String resolveCommand(String command) {
525+
String pathEnv = System.getenv("PATH");
526+
if (pathEnv == null) {
527+
return command;
528+
}
529+
for (String dir : pathEnv.split(java.io.File.pathSeparator)) {
530+
Path candidate = Path.of(dir, command);
531+
if (java.nio.file.Files.isExecutable(candidate)) {
532+
return candidate.toAbsolutePath().toString();
533+
}
534+
}
535+
return command;
536+
}
537+
519538
/**
520539
* Test information record for configuring the proxy.
521540
*/

java/sdk/src/test/java/com/github/copilot/CliServerManagerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ void connectToServerTcpMode() throws Exception {
7272

7373
private static Process startBlockingProcess() throws IOException {
7474
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
75-
return (isWindows ? new ProcessBuilder("cmd", "/c", "more") : new ProcessBuilder("cat")).start();
75+
return (isWindows
76+
? new ProcessBuilder(System.getenv("COMSPEC"), "/c", "more")
77+
: new ProcessBuilder("/usr/bin/cat")).start();
7678
}
7779

7880
@Test

java/sdk/src/test/java/com/github/copilot/JsonRpcClientTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ void testIsConnectedWithSocketClosed() throws Exception {
135135

136136
private static Process startBlockingProcess() throws IOException {
137137
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
138-
return (isWindows ? new ProcessBuilder("cmd", "/c", "more") : new ProcessBuilder("cat")).start();
138+
return (isWindows
139+
? new ProcessBuilder(System.getenv("COMSPEC"), "/c", "more")
140+
: new ProcessBuilder("/usr/bin/cat")).start();
139141
}
140142

141143
@Test

0 commit comments

Comments
 (0)