Skip to content

PHP 8.3: Make unserialize() emit a warning for trailing bytes #1987

Open

Description

@afilina

Related to #1589

Analysis

Summary of existing PHP 8.2 behavior based on the RFC:

// Extra data (after 1st semicolon) will be silently ignored
var_dump(unserialize('i:5;i:6;')); // int(5)

Updated behavior in PHP 8.3:

// Warning: unserialize(): Extra data starting at offset 4 of 8 bytes
var_dump(unserialize('i:5;i:6;'));

Top 2000 Packages

Found thousands of occurrences, but only 2 where it can be reliably inferred:

  • drupal/core - plain serialized string in a test
  • magento/magento-coding-standard - empty string in a test

Source: https://gist.github.com/afilina/d385de0733d0293fd4067824bae60b34

Detection in PHP 8.2

  • unserialize('i:5;i:6;') - Trailing data: valid
  • @unserialize('i:5;i:6;') - Trailing data: valid

See "Syntax Variations" for more examples.

Detection in PHP 8.3

  • unserialize('i:5;i:6;') - Trailing data: warning
  • @unserialize('i:5;i:6;') - not sure what to expect, since this silences the PHP warning, but also might get promoted to an error in the future, so maybe worth flagging anyway.

See "Syntax Variations" for more examples.

Syntax Variations & Detectability

Since unserialize() is useful mostly in contexts where the input is a variable or a complex expression, reliably detecting it might be difficult.

Additionally, the sniff needs to be able to reproduce the deserialization to detect trailing bytes, which I believe might be overly complex.

At first glance, this seems not sniffable. Feedback welcome.

References

Activity

  1. afilina commented on Nov 6, 2025

    @afilina
    ContributorAuthor

    @jrfnl I'm changing the formula a bit by focusing "Detection" sections on our actual target (things that observably changed), and move all the variations, false positives, etc to "Syntax Variations & Detectability", with 3v4l for various snippet categories. I still don't think that these should be viewed as test cases, but helps capture initial syntax ideas in one place.

  2. afilina commented on Nov 6, 2025

    @afilina
    ContributorAuthor

    @jrfnl Please postpone the review of the above findings until I do a related RFC, which might have an impact on how we approach this one. I'll let you know.

  3. afilina commented on Nov 6, 2025

    @afilina
    ContributorAuthor

    @jrfnl Research into both related unserialize() are done and should probably be reviewed at the same time.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions