Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty json dump due to encode_json silently failing #1064

Open
simkoc opened this issue Jan 23, 2025 · 0 comments
Open

Empty json dump due to encode_json silently failing #1064

simkoc opened this issue Jan 23, 2025 · 0 comments

Comments

@simkoc
Copy link

simkoc commented Jan 23, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant