Skip to content

Commit

Permalink
Only allow alphanumeric/underscore characters through StringHelper::t…
Browse files Browse the repository at this point in the history
…oHandle()

Resolves #15772
  • Loading branch information
brandonkelly committed Sep 22, 2024
1 parent cbccf95 commit 979e992
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Auto-generated handles, slugs, etc. now update immediately when the source input is changed. ([#15754](https://github.com/craftcms/cms/issues/15754))
- Fixed a bug where Table fields’ Default Values table could lose existing rows if they only consisted of Dropdown columns without configured options.
- Fixed a bug where custom fields’ `required` properties were always `false`. ([#15752](https://github.com/craftcms/cms/issues/15752))
- Fixed a bug where `craft\helpers\StringHelper::toHandle()` was allowing non-alphanumeric/underscore characters through. ([#15772](https://github.com/craftcms/cms/pull/15772))

## 4.12.3 - 2024-09-14

Expand Down
3 changes: 3 additions & 0 deletions src/helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,9 @@ public static function toHandle(string $str): string
// Handle must start with a letter
$handle = preg_replace('/^[^a-z]+/', '', $handle);

// Replace any remaining non-alphanumeric or underscore characters with spaces
$handle = preg_replace('/[^a-z0-9_]/', ' ', $handle);

return static::toCamelCase($handle);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/helpers/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,10 @@ public function toHandleDataProvider(): array
['fooBar', 'Fo’o Bar'],
['fooBarBaz', 'Foo Ba’r Baz'],
['fooBar', '0 Foo Bar'],
['fooBar', 'Foo!Bar'],
['fooBar', 'Foo,Bar'],
['fooBar', 'Foo/Bar'],
['fooBar', 'Foo\\Bar'],
];
}

Expand Down

0 comments on commit 979e992

Please sign in to comment.