php-src icon indicating copy to clipboard operation
php-src copied to clipboard

DOMAttr unescapes character reference

Open t-tera opened this issue 4 years ago • 10 comments

Description

The following code:

<?php
$node = new DOMAttr('foo','bar');
$node->nodeValue = 'xx&#x31;yy';
var_dump($node->nodeValue);

Resulted in this output:

xx1yy

But I expected this output instead:

xx&#x31;yy

As shown above, setter of nodeValue unescapes character references. Because of this, I need to do the following:

$node->nodeValue = htmlspecialchars("xx&#x31;yy");
var_dump($node->nodeValue);

This outputs xx&#x31;yy. The setter should not unescape character references.

PHP Version

PHP 7.4.3

Operating System

Ubuntu 20.04

t-tera avatar Apr 17 '22 08:04 t-tera

I'm on the phone so I can't verify, but I don't think this has anything to do with DOMAttr but rather just the fact that var_dump doesn't escape its output and thus the browser converts it.

Edit: Also note that PHP 7.4 is unsupported.

iluuu1994 avatar Apr 17 '22 08:04 iluuu1994

No. I'm testing it on CLI.

# php -r '$node = new DOMAttr("foo","bar"); $node->nodeValue = "xx&#x31;yy"; var_dump($node->nodeValue);'
string(5) "xx1yy"

t-tera avatar Apr 17 '22 08:04 t-tera

Bug confirmed https://3v4l.org/Xf27Y , given that Attrs in a browser don't work the same way and that I can't see either the HTML 4 or Living Standard specifying any transformations should happen, but this is likely an issue with libxml and not PHP.

damianwadley avatar Apr 17 '22 08:04 damianwadley

Writing the nodeValue is implemented by calling xmlNodeSetContentLen(), and its documentation states:

Replace the content of a node. NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity references, but XML special chars need to be escaped first by using xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().

Our implementation does not escape, though.

Reading the nodeValue is implemented by calling xmlNodeGetContent(), and its documentation states:

Entity references are substituted.

Changing this would cause a pretty serious BC break.

cmb69 avatar Apr 17 '22 14:04 cmb69

I think this qualifies as bug, but due to the BC break when we fix it, I'm reluctant to do this.

@beberlei, thoughts?

cmb69 avatar Apr 20 '22 12:04 cmb69

Fixed via GH-10132.

ndossche avatar Jun 05 '23 18:06 ndossche

Do you think it needs a changelog table entry in the manual?

tstarling avatar Jun 06 '23 00:06 tstarling

Yes. This should happen for us by the docs maintainers because there's an UPGRADING entry. But I opened PR https://github.com/php/doc-en/pull/2520 already so it will certainly not be forgotten.

ndossche avatar Jun 06 '23 19:06 ndossche

Reverted the fix because of quite big BC breaks. See https://github.com/php/php-src/issues/11469

ndossche avatar Jun 19 '23 17:06 ndossche

This is fixed in the new opt-in spec-compliance mode, which was merged in https://github.com/php/php-src/pull/13031. For more information about the opt-in mode, please see https://wiki.php.net/rfc/opt_in_dom_spec_compliance. In short: the behaviour of the old DOM classes are unaffected for backwards-compatibility reasons. There are new DOM classes where your code is correctly handled.

ndossche avatar Mar 09 '24 16:03 ndossche