Problem/Motivation

When add new content of any content types, a warning message appear about preg_match():

Warning: preg_match(): Compilation failed: range out of order in character class at offset 49 in Drupal\pathauto\AliasCleaner->cleanTokenValues() (line 347 of modules/contrib/pathauto/src/AliasCleaner.php).

Steps to reproduce

Proposed resolution

Inside cleanTokenValues() method in AliasCleaner.php class:
Add $safe_tokens variable inside preg_quote() function.

Remaining tasks

User interface changes

Warning:
Warning message

API changes

Data model changes

Issue fork pathauto-3285655

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Abdullah Yassin created an issue. See original summary.

Abdullah Yassin’s picture

Title: preg_match() compilation faild: rang out of order in character class » preg_match() compilation faild: rang out of order
Abdullah Yassin’s picture

Abdullah Yassin’s picture

Assigned: Abdullah Yassin » Unassigned
Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 3: fix-pathauto-preg_match-compilation-faild-rang-out-of-order-3285655-#3.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

rajab natshah’s picture

Status: Needs work » Needs review
rajab natshah’s picture

Version: 8.x-1.10 » 8.x-1.11
Status: Needs review » Needs work
ngkoutsaik’s picture

Hello,

I tried to replicate this issue but I could not. I installed a clean version of pathauto. I created content using as title " " \ ` , . - _ : ; | { [ } ] + = * & % ^ $ # @ ! ~ ( ) ? < > / \".

Could you add some more info on how to recreate this? What title did you use, what settings etc?
Thanks

rajab natshah’s picture

StatusFileSize
new642 bytes

A re-role for the patch to work with Pathauto 1.11

igork96’s picture

Status: Needs work » Postponed (maintainer needs more info)

I tested this on the 8.x-1.11 version with Drupal 9.4.8 and can't reproduce it. Please give more info on how to reproduce this.

sweetchillyphily’s picture

Hello,

Sorry first time posting here or doing something like this.

I have run into this problem in my production site, and was able to replicate the issue with confirmation that `preg_quote()` as suggested fixes it.

When saving my content type with these `$safe_tokens` it fails:

`alias|path|join-path|login-url|url|url-brief|?`

when removing the `?` from the safe tokens it works.

But I need the `?` so also adding `preg_quote()`, as suggested above, wrapped around safe_tokens works

mably made their first commit to this issue’s fork.

mably’s picture

Status: Postponed (maintainer needs more info) » Needs review
mably’s picture

Title: preg_match() compilation faild: rang out of order » preg_match() compilation failed: range out of order
Assigned: Unassigned » berdir

Summary

preg_match(): Compilation failed: range out of order in character class when safe_tokens config contains regex-special characters like ? or -.

Problem

In AliasCleaner::cleanTokenValues(), the safe_tokens config values were concatenated directly into a regex pattern without escaping. Characters like ? (regex quantifier) or - (range operator in character classes) caused preg_match() compilation errors.

Fix

  • AliasCleaner.php: Wrapped each safe token with preg_quote() before building the regex alternation pattern. Also moved the config read and implode() outside the foreach loop to avoid redundant calls on every iteration.

Test coverage

  • PathautoTokenTest.php: Added testCleanTokenValuesWithRegexSpecialChars() kernel test that adds ? to the safe_tokens config and calls cleanTokenValues(). A custom error handler captures any preg_match() warnings and the test asserts none were triggered. Verified that the test fails without the fix and passes with it.
berdir’s picture

Status: Needs review » Needs work
mably’s picture

Status: Needs work » Needs review

Suggested fixes have been implemented.

berdir’s picture

Assigned: berdir » Unassigned
Status: Needs review » Reviewed & tested by the community

  • mably committed 7a9975da on 8.x-1.x
    fix: #3285655 preg_match() compilation failed: range out of order
    
    By:...
mably’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.