Skip to content

Commit 1401a8e

Browse files
committed
Do not enforce escaping error codes
1 parent 14d2758 commit 1401a8e

5 files changed

Lines changed: 56 additions & 5 deletions

File tree

HM-Minimum/ruleset.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@
7171
<element value="get_post_gallery" /> <!-- with param 2 set to true, the default -->
7272
</property>
7373
</properties>
74+
75+
<!--
76+
Exception messages are internal program state, not an output boundary.
77+
Escaping belongs at the point of output (catch/render), not at throw.
78+
The OutputNotEscaped rule still covers actual HTML output points.
79+
See: https://github.com/WordPress/WordPress-Coding-Standards/issues/2374
80+
81+
To opt back in to this check, add to your ruleset:
82+
<rule ref="HM.Security.EscapeOutput.ExceptionNotEscaped" />
83+
-->
84+
<exclude name="HM.Security.EscapeOutput.ExceptionNotEscaped" />
7485
</rule>
7586

7687
<!-- Disallow use of __FILE__ in menu slugs, which exposes the filesystem's data. -->

HM/Sniffs/Performance/SlowOrderBySniff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace HM\Sniffs\Performance;
44

55
use PHPCSUtils\Utils\MessageHelper;
6+
use PHPCSUtils\Utils\TextStrings;
67
use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
78

89
/**
@@ -58,6 +59,7 @@ public function process_token( $stackPtr ) {
5859
* with custom error message passed to ->process().
5960
*/
6061
public function callback( $key, $val, $line, $group ) {
62+
$val = TextStrings::stripQuotes( $val );
6163
switch ( $val ) {
6264
case 'rand':
6365
case 'meta_value':
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
use \HM\Other;
6+
use \WP_Post;
7+
8+
function some_function( $some_argument ) {
9+
$variable = 'some string';
10+
11+
return $variable;
12+
}
13+
14+
class MyTest Class {
15+
16+
__construct() {
17+
do_something();
18+
}
19+
20+
public function another_function() {
21+
return null;
22+
}
23+
}

tests/FixtureTests.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ public static function passing_files() {
8585
/**
8686
* Setup our ruleset.
8787
*/
88-
public function setUp() {
88+
public function setUp(): void {
89+
// AllSniffs uses ConfigDouble which sets Config::$configData = [] to prevent
90+
// reading CodeSniffer.conf. Reset it so Ruleset can resolve installed standards.
91+
$configDataProp = new \ReflectionProperty( Config::class, 'configData' );
92+
$configDataProp->setAccessible( true );
93+
$configDataProp->setValue( null, null );
94+
8995
$this->config = new Config();
9096
$this->config->cache = false;
9197
$this->config->standards = [ 'HM' ];
@@ -115,6 +121,10 @@ public function setUp() {
115121

116122
$this->ruleset = new Ruleset( $this->config );
117123

124+
// ExceptionNotEscaped is excluded in HM-Minimum ruleset.xml, but IN_TESTS mode
125+
// with sniff restrictions skips processRuleset(), so apply the exclusion manually.
126+
$this->ruleset->ruleset['HM.Security.EscapeOutput.ExceptionNotEscaped'] = [ 'severity' => 0 ];
127+
118128
// Set configuration as needed too.
119129
$this->ruleset->setSniffProperty( 'HM\\Sniffs\\Security\\EscapeOutputSniff', 'customAutoEscapedFunctions', [
120130
'scope' => 'sniff',

tests/fixtures/pass/escape-output.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
echo esc_html( $var );
77
echo esc_attr( $var );
88

9-
// Triggering errors should be fine too, since they're not sent to the browser.
10-
trigger_error( $var );
9+
// Triggering errors: The message parameter must still be escaped in WPCS 3.x.
10+
trigger_error( esc_html( $var ) );
1111
error_log( $var );
1212

1313
// Deprecations and doin_it_rong too:
14-
_deprecated_file( $var );
15-
_doing_it_wrong( $var );
14+
_deprecated_file( esc_html( $var ) );
15+
_doing_it_wrong( esc_html( $var ) );
1616

1717
// Ignoring via HM or WP codes should work.
1818
// phpcs:ignore HM.Security.EscapeOutput
@@ -33,3 +33,8 @@
3333
// Custom auto-escaped functions from config (see FixtureTests.php)
3434
echo my_custom_func();
3535
echo another_func();
36+
37+
// Exception messages are internal state — escaping belongs at the output
38+
// boundary (catch/render), not at the throw site.
39+
throw new \RuntimeException( $var );
40+
throw new \RuntimeException( sprintf( 'Got: %s', $var ) );

0 commit comments

Comments
 (0)