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

[1.10] Fix xpath php-string-to-javascript-string #576

Merged
merged 3 commits into from
Mar 17, 2024

Conversation

divinity76
Copy link
Contributor

@divinity76 divinity76 commented Dec 13, 2023

TL;DR: addslashes() is not the correct way to convert a php-string to a javascript string. json_encode() is.

For example, addslashes will fail on the PHP string "foo".chr(10)."bar" , the old addslashes() code will convert it into "foo
bar"

which is a javascript syntax error.

Previously this code would fail:

$str = "foo".chr(10)."bar";
$xps = new XPathSelector("//span[contains(text(),'" . $str . "')]");
var_dump($xps->expressionCount());

it would generate a javascript syntax error:

string(134) "document.evaluate("//span[contains(text(),\'foo
bar\')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshot Length"

now it generates valid javascript:

string(133) "document.evaluate("//span[contains(text(),'foo\nbar')]", document,
null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength"

TL;DR: addslashes() is not the correct way to convert a php-string to a javascript string. json_encode() is.

For example, addslashes will fail on the PHP string "foo".chr(10)."bar" , the old addslashes() will convert it into
"foo
bar"

which is a javascript syntax error.

Previously this code would fail:
$str = "foo".chr(10)."bar";
$xps = new XPathSelector("//span[contains(text(),'" . $str . "')]");
var_dump($xps->expressionCount());

it would generate a javascript syntax error:
string(134) "document.evaluate("//span[contains(text(),\'foo
bar\')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshot
Length"

now it generates legal javascript:

string(135) "document.evaluate("\/\/span[contains(text(),'foo\nbar')]", document
, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength"
@GrahamCampbell GrahamCampbell changed the title fix xpath php-string-to-javascript-string [1.10] Fix xpath php-string-to-javascript-string Dec 13, 2023
@divinity76
Copy link
Contributor Author

I wonder if perhaps the original code was written to support PHP older than 5.2.0 (released 2006), where json_encode didn't exist yet

@GrahamCampbell
Copy link
Member

Ha, I doubt it. The original author probably just misunderstood how to prepare the string.

@GrahamCampbell
Copy link
Member

My mistake, really. I didn't catch it: #340.

@divinity76
Copy link
Contributor Author

i don't see any reason to postpone merging this

@GrahamCampbell GrahamCampbell merged commit d53f6b5 into chrome-php:1.10 Mar 17, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants