Skip to content

PHP 8.1: fix various deprecations/errors in newest version of PHP#310

Merged
ezyang merged 9 commits intoezyang:masterfrom
iFixit:php-8.1
Apr 8, 2022
Merged

PHP 8.1: fix various deprecations/errors in newest version of PHP#310
ezyang merged 9 commits intoezyang:masterfrom
iFixit:php-8.1

Conversation

@davidrans
Copy link
Copy Markdown
Contributor

Most of these were caught by the tests. The last commit fixed an issue the tests didn't catch that just came up using the library.

Copy link
Copy Markdown
Contributor

@bytestream bytestream left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice ❤️

May as well add #[\ReturnTypeWillChange] for these two cases as well:

Deprecated: Return type of HTMLPurifier_PropertyListIterator::accept() should either be compatible with FilterIterator::accept(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/htmlpurifier/htmlpurifier/library/HTMLPurifier/PropertyListIterator.php on line 32

Deprecated: Return type of HTMLPurifier_StringHash::offsetGet($index) should either be compatible with ArrayObject::offsetGet(mixed $key): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/htmlpurifier/htmlpurifier/library/HTMLPurifier/StringHash.php on line 23

@davidrans
Copy link
Copy Markdown
Contributor Author

Done!

Comment thread library/HTMLPurifier/URIFilter/Munge.php Outdated
@bytestream
Copy link
Copy Markdown
Contributor

Closes #311

@davidrans
Copy link
Copy Markdown
Contributor Author

Anything I can do to help this get merged?

@davidrans
Copy link
Copy Markdown
Contributor Author

Let me know if you need anything else from me

@bytestream
Copy link
Copy Markdown
Contributor

Just a case of waiting :p - I don't think Edward will ask for any other changes.

@ezyang
Copy link
Copy Markdown
Owner

ezyang commented Jan 28, 2022

What's with all the (string) casts?

@davidrans
Copy link
Copy Markdown
Contributor Author

It's been a few weeks, but I think the issue was that pre-8.1 these functions did implicit string casts, and now they require a value of type string to be passed.

@mhujer
Copy link
Copy Markdown

mhujer commented Jan 28, 2022

if (!empty($def->content_model)) {
$this->content_model =
str_replace("#SUPER", $this->content_model, $def->content_model);
str_replace("#SUPER", (string)$this->content_model, $def->content_model);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This smells like a (possibly benign) bug. The documentation claims that content_model is always a string, so maybe if it is sometimes null we were supposed to not attempt to do this replacement in that case (maybe #SUPER is never set in those cases)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick static analysis: A non-string value for $this->content_model originates from: https://github.com/ezyang/htmlpurifier/blob/master/library/HTMLPurifier/HTMLModule.php#L215
(null is passed to the create method here: https://github.com/ezyang/htmlpurifier/blob/master/library/HTMLPurifier/HTMLModule.php#L157)

return true;
}
if (!ctype_lower($this->unit)) {
if ($this->unit === false || !ctype_lower($this->unit)) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be the other way around: if unit is false, then we never need to strtolower the unit

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just trying to keep the behavior the same, but you may be right that this was masking a bug

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the clearest thing to do is simply return false if unit is false here: you've already passed the "zero" cases where "no unit" is allowed: the old behavior was turning the false into the empty string via strtolower and then failing later when looking that empty string up in allowedUnits.

If there's no objection to the construction, probably the "cleanest" version of that change leaves this ctype_lower check alone and instead replaces the immediately previous check with:

if ($this->unit === false) {
    return ($this->n === '0');
}

Copy link
Copy Markdown
Contributor

@Daijobou Daijobou Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zerocrates I like your suggestion, its remove the invalid unit and no error for ctype_lower() appears (in my case version 14).

if ($config->get('Core.NormalizeNewlines')) {
$html = str_replace("\r\n", "\n", $html);
$html = str_replace("\r", "\n", $html);
$html = str_replace("\r\n", "\n", (string)$html);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like type confusion again. Shouldn't have passed in a non-string html to this function in the first place.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second that - can't find anything within the source code that passes a non-string value (unless user passes a non-string to tokenizeHTML())

$this->replace['%n'] = $token ? $token->name : null;
$this->replace['%m'] = $context->get('CurrentAttr', true);
$this->replace['%p'] = $context->get('CurrentCSSProperty', true);
$this->replace['%r'] = $context->get('EmbeddedURI', true) ?: '';
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, I never knew ?: worked even in PHP 5 LOL

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ezyang It's the ternary operator or "elvis operator", since PHP 5.3 you can leave out the middle part :) - https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

@AgentSmith0
Copy link
Copy Markdown

Any updates to this?

@jstanden
Copy link
Copy Markdown
Contributor

Thanks for the great library. 👍

There's another PHP 8.x warning at: https://github.com/ezyang/htmlpurifier/blob/master/library/HTMLPurifier/AttrTransform/TargetBlank.php#L38

Attempt to read property "browsable" on bool

@pyres01
Copy link
Copy Markdown

pyres01 commented Mar 11, 2022 via email

@henriquegomes6
Copy link
Copy Markdown

any news?

@marcovtwout
Copy link
Copy Markdown

There's another PHP 8.x warning at: https://github.com/ezyang/htmlpurifier/blob/master/library/HTMLPurifier/AttrTransform/TargetBlank.php#L38

url->getSchemeObj() could return false, so this can be fixed with:

-    if ($scheme->browsable && !$url->isBenign($config, $context)) {
+    if ($scheme !== false && $scheme->browsable && !$url->isBenign($config, $context)) {

@marcovtwout
Copy link
Copy Markdown

marcovtwout commented Apr 7, 2022

Tested this branch through a standalone build on a few project configurations - have not seen any new errors yet. :) Looking forward to this being merged and 4.15.0 released.

@marcovtwout marcovtwout mentioned this pull request Apr 7, 2022
6 tasks
@ezyang ezyang merged commit 1dd3e52 into ezyang:master Apr 8, 2022
@AgentSmith0
Copy link
Copy Markdown

Thanks!

@davidrans
Copy link
Copy Markdown
Contributor Author

Thank you!

@pyres01
Copy link
Copy Markdown

pyres01 commented Dec 29, 2022 via email

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

Successfully merging this pull request may close these issues.