Skip to content

Studio CLI sites command - #1958

Closed
youknowriad wants to merge 13 commits into
trunkfrom
add/sites-list-command
Closed

Studio CLI sites command#1958
youknowriad wants to merge 13 commits into
trunkfrom
add/sites-list-command

Conversation

@youknowriad

Copy link
Copy Markdown
Contributor

Related issues

  • Fixes #

Proposed Changes

Testing Instructions

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?
@wojtekn wojtekn changed the title Add/sites list command Oct 29, 2025

await fs.promises.mkdir( appDir, { recursive: true } );

const escapedCliCommand = cliCommand.replace( /"/g, '\\"' );

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI 9 months ago

To fix the problem, we need to ensure that both backslashes and double-quote characters are properly escaped in the cliCommand before it is interpolated into the AppleScript template string. This should be done on line 278 by replacing not only all " with \", but also all \ with \\ (with the correct order: backslashes first). The best way to do this is with two chained .replace operations or, preferably, by using a sanitizer library if available. Since we are restricted to working only within the provided code region, we will use regular expressions: first .replace(/\\/g, '\\\\') (escape any backslashes), then .replace(/"/g, '\\"') (escape quotes). No additional imports are needed.

This change should be made directly to the block where escapedCliCommand is set. No other changes or method definitions are needed.


Suggested changeset 1
cli/lib/protocol-handler.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/cli/lib/protocol-handler.ts b/cli/lib/protocol-handler.ts
--- a/cli/lib/protocol-handler.ts
+++ b/cli/lib/protocol-handler.ts
@@ -275,7 +275,7 @@
 
 		await fs.promises.mkdir( appDir, { recursive: true } );
 
-		const escapedCliCommand = cliCommand.replace( /"/g, '\\"' );
+		const escapedCliCommand = cliCommand.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
 
 		const appleScript = `on open location this_URL
     set command to "${ escapedCliCommand } '" & this_URL & "'"
EOF
@@ -275,7 +275,7 @@

await fs.promises.mkdir( appDir, { recursive: true } );

const escapedCliCommand = cliCommand.replace( /"/g, '\\"' );
const escapedCliCommand = cliCommand.replace(/\\/g, '\\\\').replace(/"/g, '\\"');

const appleScript = `on open location this_URL
set command to "${ escapedCliCommand } '" & this_URL & "'"
Copilot is powered by AI and may make mistakes. Always verify output.
const unzipStream = unzipper.Parse();

unzipStream.on( 'entry', ( entry ) => {
const entryPath = entry.path;

Check failure

Code scanning / CodeQL

Arbitrary file access during archive extraction ("Zip Slip") High

Unsanitized archive entry, which may contain '..', is used in a
file system operation
.

Copilot Autofix

AI 9 months ago

To fix this issue, sanitize and validate each archive entry's path before using it for extraction. The best approach is:

  • Use path.join to construct the output path,
  • Use path.resolve on both the constructed output path and the intended destination directory,
  • Ensure that the output path starts with the resolved extraction destination: in other words, that the output file is inside the intended extraction directory.
  • If the path contains any .. component or the constructed absolute path does not start with destinationFolder, skip extraction and log an error.

You should implement this by adding a validation step before writing the file. Make changes only within the provided code in common/lib/wordpress-version-manager.ts, namely the relevant lines within the downloadFileAndUnzip function.

You do not need to add external dependencies; Node.js's built-in path module suffices.


Suggested changeset 1
common/lib/wordpress-version-manager.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/common/lib/wordpress-version-manager.ts b/common/lib/wordpress-version-manager.ts
--- a/common/lib/wordpress-version-manager.ts
+++ b/common/lib/wordpress-version-manager.ts
@@ -84,6 +84,18 @@
 				const entryPath = entry.path;
 				const fullPath = path.join( destinationFolder, entryPath );
 
+				// Sanitize: ensure that fullPath is inside destinationFolder, and entryPath is not malicious
+				const resolvedDestFolder = path.resolve( destinationFolder );
+				const resolvedFullPath = path.resolve( fullPath );
+				if (
+					!resolvedFullPath.startsWith(resolvedDestFolder + path.sep) &&
+					resolvedFullPath !== resolvedDestFolder // allow extraction directly into base folder
+				) {
+					console.error( `Skipping potentially unsafe archive entry path: ${ entryPath }` );
+					entry.autodrain();
+					return;
+				}
+
 				if ( entry.type === 'Directory' ) {
 					entry.autodrain();
 					return;
EOF
@@ -84,6 +84,18 @@
const entryPath = entry.path;
const fullPath = path.join( destinationFolder, entryPath );

// Sanitize: ensure that fullPath is inside destinationFolder, and entryPath is not malicious
const resolvedDestFolder = path.resolve( destinationFolder );
const resolvedFullPath = path.resolve( fullPath );
if (
!resolvedFullPath.startsWith(resolvedDestFolder + path.sep) &&
resolvedFullPath !== resolvedDestFolder // allow extraction directly into base folder
) {
console.error( `Skipping potentially unsafe archive entry path: ${ entryPath }` );
entry.autodrain();
return;
}

if ( entry.type === 'Directory' ) {
entry.autodrain();
return;
Copilot is powered by AI and may make mistakes. Always verify output.
@fredrikekelund

fredrikekelund commented Dec 10, 2025

Copy link
Copy Markdown
Contributor

We are nearing the launch of the CLI i2 project (see the dev/studio-cli-i2 branch for the latest changes), which includes all the changes @youknowriad initiated in this PR, barring the ai command.

@youknowriad
youknowriad deleted the add/sites-list-command branch December 10, 2025 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants