PluginProbe
Plugin Check (PCP) / 1.5.0
Plugin Check (PCP) v1.5.0
2.1.0 trunk 0.1 0.2.0 0.2.1 0.2.2 0.2.3 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 ci-artifacts
plugin-check / includes / Checker / Checks / Plugin_Repo / Code_Obfuscation_Check.php

Code_Obfuscation_Check.php in Plugin Check (PCP) 1.5.0, at includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php

207 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Code_Obfuscation_Check.
4 *
5 * @package plugin-check
6 */
7
8 namespace WordPress\Plugin_Check\Checker\Checks\Plugin_Repo;
9
10 use Exception;
11 use WordPress\Plugin_Check\Checker\Check_Categories;
12 use WordPress\Plugin_Check\Checker\Check_Result;
13 use WordPress\Plugin_Check\Checker\Checks\Abstract_File_Check;
14 use WordPress\Plugin_Check\Traits\Amend_Check_Result;
15 use WordPress\Plugin_Check\Traits\Stable_Check;
16
17 /**
18 * Check to detect PHP code obfuscation.
19 *
20 * @since 1.0.0
21 */
22 class Code_Obfuscation_Check extends Abstract_File_Check {
23
24 use Amend_Check_Result;
25 use Stable_Check;
26
27 const TYPE_ZEND = 1;
28 const TYPE_SOURCEGUARDIAN = 2;
29 const TYPE_IONCUBE = 4;
30 const TYPE_ALL = 7; // Same as all of the above with bitwise OR.
31
32 /**
33 * Bitwise flags to control check behavior.
34 *
35 * @since 1.0.0
36 * @var int
37 */
38 protected $flags = 0;
39
40 /**
41 * Constructor.
42 *
43 * @since 1.0.0
44 *
45 * @param int $flags Bitwise flags to control check behavior.
46 */
47 public function __construct( $flags = self::TYPE_ALL ) {
48 $this->flags = $flags;
49 }
50
51 /**
52 * Gets the categories for the check.
53 *
54 * Every check must have at least one category.
55 *
56 * @since 1.0.0
57 *
58 * @return array The categories for the check.
59 */
60 public function get_categories() {
61 return array( Check_Categories::CATEGORY_PLUGIN_REPO );
62 }
63
64 /**
65 * Amends the given result by running the check on the given list of files.
66 *
67 * @since 1.0.0
68 *
69 * @param Check_Result $result The check result to amend, including the plugin context to check.
70 * @param array $files List of absolute file paths.
71 *
72 * @throws Exception Thrown when the check fails with a critical error (unrelated to any errors detected as part of
73 * the check).
74 */
75 protected function check_files( Check_Result $result, array $files ) {
76 $php_files = self::filter_files_by_extension( $files, 'php' );
77
78 if ( $this->flags & self::TYPE_ZEND ) {
79 $this->look_for_zendguard( $result, $php_files );
80 }
81 if ( $this->flags & self::TYPE_SOURCEGUARDIAN ) {
82 $this->look_for_sourceguardian( $result, $php_files );
83 }
84 if ( $this->flags & self::TYPE_IONCUBE ) {
85 $this->look_for_ioncube( $result, $php_files );
86 }
87 }
88
89 /**
90 * Looks for Zend Guard obfuscated files and amends the given result with an error if found.
91 *
92 * @since 1.0.0
93 *
94 * @param Check_Result $result The check result to amend, including the plugin context to check.
95 * @param array $php_files List of absolute PHP file paths.
96 */
97 protected function look_for_zendguard( Check_Result $result, array $php_files ) {
98 $files = self::files_preg_match_all( '/(\<\?php \@Zend;)|(This file was encoded by)/', $php_files );
99
100 if ( ! empty( $files ) ) {
101 foreach ( $files as $file ) {
102 $this->add_result_error_for_file(
103 $result,
104 sprintf(
105 /* translators: %s: tool name */
106 __( 'Code Obfuscation tools are not permitted. Detected: %s', 'plugin-check' ),
107 __( 'Zend Guard', 'plugin-check' )
108 ),
109 'obfuscated_code_detected',
110 $file['file'],
111 $file['line'],
112 $file['column'],
113 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource',
114 7
115 );
116 }
117 }
118 }
119
120 /**
121 * Looks for Source Guardian obfuscated files and amends the given result with an error if found.
122 *
123 * @since 1.0.0
124 *
125 * @param Check_Result $result The check result to amend, including the plugin context to check.
126 * @param array $php_files List of absolute PHP file paths.
127 */
128 protected function look_for_sourceguardian( Check_Result $result, array $php_files ) {
129 $files = self::files_preg_match_all( "/(sourceguardian\.com)|(function_exists\('sg_load'\))|(\$__x=)/", $php_files );
130
131 if ( ! empty( $files ) ) {
132 foreach ( $files as $file ) {
133 $this->add_result_error_for_file(
134 $result,
135 sprintf(
136 /* translators: %s: tool name */
137 __( 'Code Obfuscation tools are not permitted. Detected: %s', 'plugin-check' ),
138 __( 'Source Guardian', 'plugin-check' )
139 ),
140 'obfuscated_code_detected',
141 $file['file'],
142 $file['line'],
143 $file['column'],
144 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource',
145 7
146 );
147 }
148 }
149 }
150
151 /**
152 * Looks for ionCube obfuscated files and amends the given result with an error if found.
153 *
154 * @since 1.0.0
155 *
156 * @param Check_Result $result The check result to amend, including the plugin context to check.
157 * @param array $php_files List of absolute PHP file paths.
158 */
159 protected function look_for_ioncube( Check_Result $result, array $php_files ) {
160 $files = self::files_preg_match_all( '/ionCube/', $php_files );
161
162 if ( ! empty( $files ) ) {
163 foreach ( $files as $file ) {
164 $this->add_result_error_for_file(
165 $result,
166 sprintf(
167 /* translators: %s: tool name */
168 __( 'Code Obfuscation tools are not permitted. Detected: %s', 'plugin-check' ),
169 __( 'ionCube', 'plugin-check' )
170 ),
171 'obfuscated_code_detected',
172 $file['file'],
173 $file['line'],
174 $file['column'],
175 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource',
176 7
177 );
178 }
179 }
180 }
181 /**
182 * Gets the description for the check.
183 *
184 * Every check must have a short description explaining what the check does.
185 *
186 * @since 1.1.0
187 *
188 * @return string Description.
189 */
190 public function get_description(): string {
191 return __( 'Detects the usage of code obfuscation tools.', 'plugin-check' );
192 }
193
194 /**
195 * Gets the documentation URL for the check.
196 *
197 * Every check must have a URL with further information about the check.
198 *
199 * @since 1.1.0
200 *
201 * @return string The documentation URL.
202 */
203 public function get_documentation_url(): string {
204 return __( 'https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/', 'plugin-check' );
205 }
206 }
207