Skip to content

ext/uri incorrectly throws ValueError when encountering null byte #20366

Closed

Description

@nyamsprod

Description

The following code:

test

Resulted in this output:

Currently the tests returns a `ValueError`

But I expected this output instead:

Uri\WhatWg\InvalidUrlException: The specified URI is malformed; Invalid URL (DomainInvalidCodePoint)

To me both are acceptable, I would lean toward the second options which is what the WHATWG URL standard would expect but I understand the current output. I just want to know if there is a clear guideline when to throw a ValueError vs InvalidUrlException because from a DX usage it may mean that withers can only throw ValueError ? 🤔

PHP Version

using php master branch (but this is irrelevant)

Operating System

No response

Activity

  1. devnexen commented on Nov 2, 2025

    @devnexen
    Member

    it seems to me the parameter is of type Z_PARAM_PATH_STR thus with the null byte in the middle of your test case, it already takes precedence though I guess (as not even reaching the lexbor parser).

  2. nyamsprod commented on Nov 2, 2025

    @nyamsprod
    ContributorAuthor

    OK so following your reasoning, the null byte character will never reach the lexbor parser so InvalidUrlException can never be thrown.

  3. devnexen commented on Nov 2, 2025

    @devnexen
    Member

    IMHO, those are not bugs at all.

  4. nyamsprod commented on Nov 2, 2025

    @nyamsprod
    ContributorAuthor

    I am not saying it is a bug I just need to understand when one is thrown vs the other to improve my polyfill. Currently the polyfill never throw the ValueError and everything is validated by the WHATWG parser which means I need to add a specific fix for the null byte in order to match PHP implementation.

  5. devnexen commented on Nov 4, 2025

    @devnexen
    Member

    yes so to rephrase it, it is expected outcome in my view. Hope it helps.

  6. nyamsprod commented on Nov 4, 2025

    @nyamsprod
    ContributorAuthor

    @devnexen I have added the following

    !str_contains($uri, "\0") || throw new ValueError('Argument #1 ($uri) must not contain any null bytes');

    prior to any validation by the userland URI parser.

    I hope this is the only gatekeeper to look for 🤔

  7. devnexen commented on Nov 4, 2025

    @devnexen
    Member

    I ll let @TimWolla respond on this particular point ;)

  8. TimWolla commented on Nov 4, 2025

    @TimWolla
    Member

    I hope this is the only gatekeeper to look for 🤔

    Yes. That's Z_PARAM_PATH_STR vs Z_PARAM_STR internally.

    As for this issue, I'd be inclined to just pass along NUL bytes as-is to the parsers for consistent error handling, but I didn't yet have time to check if all the methods are fully NUL-safe (and I would need to check with Mate as well).

  9. nyamsprod commented on Nov 4, 2025

    @nyamsprod
    ContributorAuthor

    @TimWolla thanks for the comment. So if you end up passing it along I will be able to remove my current "patch" then. Let me know if this is the case.

  10. devnexen commented on Nov 4, 2025

    @devnexen
    Member

    @TimWolla I think your point is valid, might be a tougher decision in another hand. i.e. most of other php extensions treat nul bytes from input prevently. But again, making it a thinner wrapper around lexbor is a valid POV.

  11. kocsismate commented on Nov 13, 2025

    @kocsismate
    Member

    I used Z_PARAM_PATH_STR everywhere in ext/uri when a string is expected for safety reasons. Back then, I didn't know that the \0 may be successfully passed to WHATWG URIs: https://timothygu.me/urltester/#input=https%3A%2F%2Fexample.com%2Ffo%5C0o (see the "your browser" column), it gets percent-encoded... (I only realized it a few days ago)

    So it seems we will have to verify if all functions nul-byte safe - I'm not really knowledgable though in this regard. :(

  12. kocsismate commented on Nov 14, 2025

    @kocsismate
    Member

    (I'm working on a fix right now)

  13. self-assigned this
    on Nov 14, 2025
  14. kocsismate commented on Nov 14, 2025

    @kocsismate
    Member

    Fix is here with a lot of tests: #20366 We should still verify if null-byte handling is properly implemented.

  15. changed the title [-]ext-uri when to throw ValueError vs InvalidUriException[/-] [+]ext/uri incorrectly throws ValueError when encountering null byte[/+] on Nov 19, 2025
  16. added 2 commits that reference this issue on Nov 19, 2025
    889a3cf
    9743977
  17. added a commit that references this issue on Nov 19, 2025
    15ca366
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions