Skip to content

Commit e043369

Browse files
fix(wp-cli-console): pass native runner archive review for v1.0.4
1 parent e6cec0a commit e043369

7 files changed

Lines changed: 121 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
77
where applicable.
88

9+
## [1.0.4] - 2026-10-02
10+
11+
- Fix production-archive Plugin Check findings with documented, call-specific
12+
exceptions for native shell-free WP-CLI execution and private subprocess pipes.
13+
- Validate forwarded CLI host environment values as DNS names or IP addresses
14+
with optional valid ports; invalid values fall back to `localhost`.
15+
- Remove broad runner-related coding-standard exclusions. Archive checks remain
16+
mandatory; published 1.0.3 archives and tags stay unchanged.
17+
918
## [1.0.3] - 2026-10-02
1019

1120
- Repair archive caller permissions for the pinned reusable build workflow.

‎README.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,19 @@ dependencies and include the artifact manifest and checksums. Tests, development
7878
documentation, QA configuration and coverage output are excluded. Source ZIPs
7979
also use `.gitattributes` export exclusions. Artifact attestation remains disabled.
8080

81-
Version 1.0.3 prepares a fresh archive with corrected caller permissions; earlier
81+
Version 1.0.4 resolves the native-runner Plugin Check findings in a fresh archive; earlier
8282
tags and published archives retain their original contents. The new archive must
8383
pass the hosted archive check before it is attached to the release.
8484

85+
The only native process boundary passes a resolved WP-CLI executable and argument
86+
array to `proc_open()`, with no shell. Its call-specific PHPCS exceptions also
87+
cover private subprocess pipes; `WP_Filesystem` cannot manage those streams.
88+
`HTTP_HOST` and `SERVER_NAME` retain valid DNS/IP values and optional TCP ports.
89+
Invalid values, control characters and URLs fall back to `localhost`; WordPress
90+
text sanitization or unslashing would change these CLI environment tokens.
91+
These exceptions travel with the production source and do not disable archive
92+
checks or unrelated process, filesystem and input-validation checks.
93+
8594
## Positional value validation
8695

8796
Externally supplied positional option names are validated before invoking the

‎docs/releases/1.0.4.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 1.0.4 release candidate
2+
3+
This patch addresses the 1.0.3 production archive's Plugin Check findings at the
4+
native WP-CLI process boundary. It retains argument-array execution without a
5+
shell and incremental stdout/stderr pipe handling. Documented inline PHPCS
6+
exceptions cover only the reviewed process and private pipe operations.
7+
8+
Forwarded CLI host environment values now accept DNS names, IP addresses and
9+
valid optional ports; malformed values fall back to `localhost`. Process tests
10+
cover accepted and rejected host tokens, inherited environment fallback and
11+
literal shell metacharacters. Option-looking positional values remain rejected
12+
before reaching the runner.
13+
14+
The archive caller, pinned reusable workflows and mandatory archive-check gate
15+
remain unchanged. Publish a new 1.0.4 tag and archive only after review and the
16+
fresh hosted archive check; do not retag or replace 1.0.3. Local package QA and
17+
source-sniff checks do not establish that a hosted archive has passed.

‎phpcs.xml.dist‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
<exclude name="SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter" />
2323
<exclude name="SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable" />
2424
<exclude name="SymPress.Files.LineLength.TooLong" />
25-
<exclude name="WordPress.PHP.DiscouragedPHPFunctions.system_calls_proc_open" />
26-
<exclude name="WordPress.Security.ValidatedSanitizedInput.InputNotSanitized" />
27-
<exclude name="WordPress.Security.ValidatedSanitizedInput.MissingUnslash" />
28-
<exclude name="WordPress.WP.AlternativeFunctions.file_system_operations_fclose" />
29-
<exclude name="WordPress.WP.AlternativeFunctions.file_system_operations_fread" />
3025
</rule>
3126

3227
<rule ref="SymPress.Namespaces.Psr4">

‎src/Application/WpCliRunner.php‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function run(array $arguments, OutputInterface $output): int
2828

2929
$pipes = [];
3030

31+
// phpcs:ignore Generic.PHP.ForbiddenFunctions.Found, WordPress.PHP.DiscouragedPHPFunctions.system_calls_proc_open -- Native WP-CLI argv bypasses the shell at this single reviewed CLI process boundary.
3132
$process = proc_open(
3233
$command,
3334
[
@@ -44,6 +45,7 @@ public function run(array $arguments, OutputInterface $output): int
4445
return Command::FAILURE;
4546
}
4647

48+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- Close private subprocess stdin, not a WordPress file.
4749
fclose($pipes[0]);
4850
stream_set_blocking($pipes[1], false);
4951
stream_set_blocking($pipes[2], false);
@@ -54,7 +56,9 @@ public function run(array $arguments, OutputInterface $output): int
5456
usleep(10000);
5557
}
5658

59+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- Close private subprocess stdout, not a WordPress file.
5760
fclose($pipes[1]);
61+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- Close private subprocess stderr, not a WordPress file.
5862
fclose($pipes[2]);
5963

6064
$exitCode = proc_close($process);
@@ -108,14 +112,33 @@ private function environment(): array
108112
/** @param array<string, string> $environment */
109113
private function serverEnvironmentValue(string $name, array $environment): string
110114
{
115+
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- CLI host tokens are not WordPress-slashed; strict DNS/IP/port validation below rejects unsafe values.
111116
$value = $_SERVER[$name] ?? $environment[$name] ?? 'localhost';
112117

113-
return is_string($value) ? $value : 'localhost';
118+
if (!is_string($value) || strlen($value) > 259) {
119+
return 'localhost';
120+
}
121+
if (filter_var($value, FILTER_VALIDATE_IP) !== false) {
122+
return $value;
123+
}
124+
if (
125+
preg_match('/^(\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9.-]+)(?::([0-9]{1,5}))?$/D', $value, $parts) !== 1
126+
|| (isset($parts[2]) && ((int) $parts[2] < 1 || (int) $parts[2] > 65535))
127+
) {
128+
return 'localhost';
129+
}
130+
$host = $parts[1];
131+
if (str_starts_with($host, '[')) {
132+
return filter_var(substr($host, 1, -1), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false ? $value : 'localhost';
133+
}
134+
135+
return strlen($host) <= 253 && filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) !== false ? $value : 'localhost';
114136
}
115137

116138
/** @param resource $pipe */
117139
private function drainPipe(mixed $pipe, string $type, OutputInterface $output): void
118140
{
141+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread -- Drain the private subprocess pipe incrementally; WP_Filesystem cannot read process streams.
119142
while (($buffer = fread($pipe, 8192)) !== false && $buffer !== '') {
120143
$this->write($type, $buffer, $output);
121144
}

‎tests/Unit/WpCliRunnerTest.php‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,66 @@ public function testItFallsBackToTheGlobalWpBinary(): void
9696
self::assertSame('global:["cli","info","--no-color"]', $output->fetch());
9797
}
9898

99+
public function testItPreservesValidatedHostAndIpEnvironmentValues(): void
100+
{
101+
$kernel = $this->createStub(KernelInterface::class);
102+
$kernel->method('getProjectDir')->willReturn($this->projectWithLocalWpBinary());
103+
$runner = new WpCliRunner($kernel);
104+
foreach (['localhost', 'shop.example.test:8443', '127.0.0.1:8080', '[2001:db8::1]:443', '::1', 'xn--bcher-kva.example'] as $host) {
105+
$_SERVER['HTTP_HOST'] = $host;
106+
$_SERVER['SERVER_NAME'] = $host;
107+
$output = new BufferedOutput();
108+
self::assertSame(7, $runner->run(['cli', 'info'], $output));
109+
self::assertStringContainsString($host . '|' . $host, $output->fetch());
110+
}
111+
}
112+
113+
public function testItRejectsUnsafeHostEnvironmentValues(): void
114+
{
115+
$kernel = $this->createStub(KernelInterface::class);
116+
$kernel->method('getProjectDir')->willReturn($this->projectWithLocalWpBinary());
117+
$runner = new WpCliRunner($kernel);
118+
foreach (['user@example.test', 'example.test/path', 'example.test\\path', "example.test\r\nINJECTED=value", "example.test\0", ' example.test', '-bad.example', '[not-an-ip]', 'example.test:0', 'example.test:65536', str_repeat('a', 64) . '.example', str_repeat('a', 260), []] as $host) {
119+
$_SERVER['HTTP_HOST'] = $host;
120+
$_SERVER['SERVER_NAME'] = $host;
121+
$output = new BufferedOutput();
122+
self::assertSame(7, $runner->run(['cli', 'info'], $output));
123+
self::assertStringContainsString("localhost|localhost\n", $output->fetch());
124+
}
125+
}
126+
127+
public function testItValidatesProcessEnvironmentFallbackValues(): void
128+
{
129+
$kernel = $this->createStub(KernelInterface::class);
130+
$kernel->method('getProjectDir')->willReturn($this->projectWithLocalWpBinary());
131+
unset($_SERVER['HTTP_HOST'], $_SERVER['SERVER_NAME']);
132+
$previousHost = getenv('HTTP_HOST');
133+
$previousName = getenv('SERVER_NAME');
134+
putenv('HTTP_HOST=shop.example.test:8443');
135+
putenv('SERVER_NAME=https://unsafe.example');
136+
$output = new BufferedOutput();
137+
try {
138+
self::assertSame(7, (new WpCliRunner($kernel))->run(['cli', 'info'], $output));
139+
self::assertStringContainsString("shop.example.test:8443|localhost\n", $output->fetch());
140+
} finally {
141+
$previousHost === false ? putenv('HTTP_HOST') : putenv('HTTP_HOST=' . $previousHost);
142+
$previousName === false ? putenv('SERVER_NAME') : putenv('SERVER_NAME=' . $previousName);
143+
}
144+
}
145+
146+
public function testItPassesShellMetacharactersLiterallyWithoutDuplicatingNoColor(): void
147+
{
148+
$projectDirectory = $this->projectWithLocalWpBinary();
149+
$marker = $projectDirectory . '/shell-must-not-run';
150+
$arguments = ['option', 'get', 'value with spaces; $(touch ' . $marker . ')', '--no-color'];
151+
$kernel = $this->createStub(KernelInterface::class);
152+
$kernel->method('getProjectDir')->willReturn($projectDirectory);
153+
$output = new BufferedOutput();
154+
self::assertSame(7, (new WpCliRunner($kernel))->run($arguments, $output));
155+
self::assertStringContainsString(json_encode($arguments, JSON_THROW_ON_ERROR), $output->fetch());
156+
self::assertFileDoesNotExist($marker);
157+
}
158+
99159
public function testItReturnsFailureWhenNoWpBinaryCanBeStarted(): void
100160
{
101161
$this->projectDirectory = sys_get_temp_dir() . '/sympress_wp_cli_project_' . bin2hex(random_bytes(8));

‎wp-cli-console.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Plugin Name: WP-CLI Console
55
* Description: Symfony Console wrappers for useful WP-CLI workflows.
6-
* Version: 1.0.3
6+
* Version: 1.0.4
77
* Requires at least: 6.9
88
* Requires PHP: 8.5
99
* Author: Brian Schaeffner

0 commit comments

Comments
 (0)