Skip to content

Stdlib: mb_scrub() — PHP 8.4 invalid-byte scrubbing missing (ext/mbstring/mbstring.c) #6050

Description

@PurHur

Category

stdlib

Problem

PHP 8.4 adds mb_scrub(string $string, ?string $encoding = null): string — replace invalid byte sequences with ? (or U+FFFD for UTF-8) for the given encoding. Mail sanitizers, log pipelines, and HTTP body normalizers use it alongside mb_convert_encoding() (#3075).

Maintainer audit (Jun 2026): function_exists('mb_scrub')false on bin/vm.php.

php-src reference

Repro (failure today)

Save as test/repro-maintainer/parity_mb_scrub.php:

<?php
var_export(function_exists('mb_scrub'));
// Invalid UTF-8 byte
echo mb_scrub("\xFF", 'UTF-8'), "\n";
// Valid UTF-8 unchanged
echo mb_scrub("café", 'UTF-8'), "\n";
// Default encoding (internal)
echo mb_scrub("\xFF"), "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro-maintainer/parity_mb_scrub.php
php bin/vm.php test/repro-maintainer/parity_mb_scrub.php 2>&1 | head -5
'
Runtime function_exists scrub \xFF UTF-8 valid café
Zend PHP 8.4+ true ? or U+FFFD unchanged
bin/vm.php false fatal

php-src-strict enum operand

<?php
enum E: string { case A = 'x'; }
try {
    mb_scrub(E::A, 'UTF-8');
} catch (Throwable $e) {
    echo get_class($e), ': ', $e->getMessage(), "\n";
}

Must throw catchable TypeError (mirror #5873 mb_strlen pattern), not silent backing-string coercion.

Scope (PHP-in-PHP first)

Path Work
ext/mbstring/mb_scrub.php Algorithm in PHP using existing mbstring module (#5695)
ext/mbstring/Module.php Register function
ext/mbstring/VmMbstring.php Shared invalid-byte scrub helper (reuse for #6038 MIME if applicable)
JIT/AOT VM-only v1

Avoid: new logic in runtime/*.c.

Done when

  • function_exists('mb_scrub') → true on VM
  • Invalid-byte repro output byte-matches Zend on Docker harness
  • Valid UTF-8 string unchanged
  • Enum case $stringTypeError (php-src-strict)
  • test/compliance/cases/stdlib/mb_scrub.phpt green
  • ./script/ci-fast.sh --filter MbScrub green

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter MbScrub'

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions