Skip to content

Commit 0f035f8

Browse files
Copilotswissspidy
andcommitted
Make slug validation stricter to only allow alphanumeric and dashes
Co-authored-by: swissspidy <[email protected]>
1 parent f29a0cc commit 0f035f8

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

features/scaffold-plugin-tests.feature

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,28 @@ Feature: Scaffold plugin unit tests
236236
When I try `wp scaffold plugin-tests ../`
237237
Then STDERR should be:
238238
"""
239-
Error: Invalid plugin slug specified. The target directory '{RUN_DIR}/wp-content/plugins/../' is not in '{RUN_DIR}/wp-content/plugins'.
239+
Error: Invalid plugin slug specified. The slug can only contain alphanumeric characters and dashes.
240240
"""
241241
And the return code should be 1
242242

243243
When I try `wp scaffold plugin-tests my-plugin/`
244244
Then STDERR should be:
245245
"""
246-
Error: Invalid plugin slug specified. The slug cannot end with a slash.
246+
Error: Invalid plugin slug specified. The slug can only contain alphanumeric characters and dashes.
247247
"""
248248
And the return code should be 1
249249

250250
When I try `wp scaffold plugin-tests my-plugin\\`
251251
Then STDERR should be:
252252
"""
253-
Error: Invalid plugin slug specified. The slug cannot end with a slash.
253+
Error: Invalid plugin slug specified. The slug can only contain alphanumeric characters and dashes.
254+
"""
255+
And the return code should be 1
256+
257+
When I try `wp scaffold plugin-tests my_plugin`
258+
Then STDERR should be:
259+
"""
260+
Error: Invalid plugin slug specified. The slug can only contain alphanumeric characters and dashes.
254261
"""
255262
And the return code should be 1
256263

features/scaffold-theme-tests.feature

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,28 @@ Feature: Scaffold theme unit tests
215215
When I try `wp scaffold theme-tests ../`
216216
Then STDERR should be:
217217
"""
218-
Error: Invalid theme slug specified. The target directory '{RUN_DIR}/wp-content/themes/../' is not in '{RUN_DIR}/wp-content/themes'.
218+
Error: Invalid theme slug specified. The slug can only contain alphanumeric characters and dashes.
219219
"""
220220
And the return code should be 1
221221

222222
When I try `wp scaffold theme-tests t12child/`
223223
Then STDERR should be:
224224
"""
225-
Error: Invalid theme slug specified. The slug cannot end with a slash.
225+
Error: Invalid theme slug specified. The slug can only contain alphanumeric characters and dashes.
226226
"""
227227
And the return code should be 1
228228

229229
When I try `wp scaffold theme-tests t12child\\`
230230
Then STDERR should be:
231231
"""
232-
Error: Invalid theme slug specified. The slug cannot end with a slash.
232+
Error: Invalid theme slug specified. The slug can only contain alphanumeric characters and dashes.
233+
"""
234+
And the return code should be 1
235+
236+
When I try `wp scaffold theme-tests t12_child`
237+
Then STDERR should be:
238+
"""
239+
Error: Invalid theme slug specified. The slug can only contain alphanumeric characters and dashes.
233240
"""
234241
And the return code should be 1
235242

src/Scaffold_Command.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,9 @@ private function scaffold_plugin_theme_tests( $args, $assoc_args, $type ) {
829829
if ( in_array( $slug, [ '.', '..' ], true ) ) {
830830
WP_CLI::error( "Invalid {$type} slug specified. The slug cannot be '.' or '..'." );
831831
}
832-
// Reject slugs ending with slashes to prevent corrupted bootstrap.php files.
833-
if ( '/' === substr( $slug, -1 ) || '\\' === substr( $slug, -1 ) ) {
834-
WP_CLI::error( "Invalid {$type} slug specified. The slug cannot end with a slash." );
832+
// Validate slug contains only alphanumeric characters and dashes.
833+
if ( ! preg_match( '/^[a-zA-Z0-9\-]+$/', $slug ) ) {
834+
WP_CLI::error( "Invalid {$type} slug specified. The slug can only contain alphanumeric characters and dashes." );
835835
}
836836
if ( 'theme' === $type ) {
837837
$theme = wp_get_theme( $slug );

0 commit comments

Comments
 (0)