Skip to content

Empty json dump due to encode_json silently failing #1064

@simkoc

Description

@simkoc

When trying to work with encoded strings in a php file I am having trouble getting json to work:

proofOcConcept.php

<?php
echo gzuncompress("KL");
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";

Dumping Json:

PHP-Parser $> ./bin/php-parse -j proofOfConcept.php
====> File ./proofOcConcept.php:
==> JSON dump:

Dumping Nodes:

PHP-Parser $> ./bin/php-parse -d proofOfConceptPhp.php 
====> File proofOfConceptPhp.php:
==> Node dump:
array(
    0: Stmt_Echo(
        exprs: array(
            0: Expr_FuncCall(
                name: Name(
                    name: gzuncompress
                )
                args: array(
                    0: Arg(
                        name: null
                        value: Scalar_String(
                            value: KL
                        )
                        byRef: false
                        unpack: false
                    )
                )
            )
        )
    )
    1: Stmt_Echo(
        exprs: array(
            0: Scalar_String(
                value: �
            )
        )
    )
)

Diving deeper into the issue and adding the the JSON_THROW_ON_ERROR flag in line 85 of bin/php-parse results in:

PHP-Parser $> ./bin/php-parse -j proofOfConcept.php
====> File ./proofOcConcept.php:
==> JSON dump:
PHP Fatal error:  Uncaught JsonException: Malformed UTF-8 characters, possibly incorrectly encoded in /home/simon/tmp/PHP-Parser/bin/php-parse:85
Stack Trace:
#0 /home/simon/tmp/PHP-Parser/bin/php-parse(85): json_encode()
#1 {main}
  thrown in /home/simon/tmp/PHP-Parser/bin/php-parse on line 85

also at the same line adding the JSON_INVALID_UTF8_IGNORE seems to fix the issue and produce the expected output:

PHP-Parser $> ./bin/php-parse -j proofOfConcept.php
====> File ./proofOcConcept.php:
==> JSON dump:
[
   {
        ... very long json file ....
   }
]

I am using an up-to-date arch Linux which currently has PHP 8.3.15.

I would propose to either only add the JSON_TRHOW_ON_ERROR flag to prevent silently failing or to also add JSON_INVALID_UTF8_IGNORE flag. I have made a PR for the latter proposal, as this fixes my immediate issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions