Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Fixes the special characters in bindParam for PDO #224

Merged
merged 4 commits into from
Nov 29, 2017

Conversation

ezimuel
Copy link
Contributor

@ezimuel ezimuel commented Mar 3, 2017

This PR solve #35 and replaces #178. The $name in bindParam are replaced with the following, due to the PDO restriction [0-9a-zA-Z_]:

return ':' . md5($name);

For instance, a filed name test$ will be translated in :79f5d7a7e10367f279c2500dcd03b3de as bind param name. I used the md5() approach after some benchmark tests, see comments and commits below. I also provided unit test and integrational test.

@ezimuel ezimuel mentioned this pull request Mar 3, 2017
@ezimuel
Copy link
Contributor Author

ezimuel commented Mar 8, 2017

In the last commit, I changed the usage of preg_replace_callback in favor of md5() for performance reason. I did some benchmark test showing a big difference. MD5 if from 3x to 4x faster than preg_replace_callback.

@ezimuel ezimuel added the bug label Mar 8, 2017
@ezimuel ezimuel added this to the 2.9.0 milestone Mar 8, 2017
@alshayed
Copy link
Contributor

alshayed commented Nov 9, 2017

Hi, is there any expected time frame for this to get merged in and released? Thanks!

@ezimuel
Copy link
Contributor Author

ezimuel commented Nov 10, 2017

@alshayed I'll release in two weeks, sorry for the delay.

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

Looks good; I've provided a little feedback on a few readability changes, but otherwise, ready to merge!

', :' .
md5('text$') .
')'
);
Copy link
Member

Choose a reason for hiding this comment

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

I'd use sprintf here to make it more readable:

$this->statement->prepare(sprintf(
    'INSERT INTO test (text_, text$) VALUES (:%s, :%s)',
    md5('text_'),
    md5('text$')
));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed.

implode(",", $this->errorInfo())
));
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Return early if $sql is empty:

if (empty($sql)) {
    return;
}

if (false === $this->exec($sql)) {
    throw new \Exception(/* ... */);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@ezimuel
Copy link
Contributor Author

ezimuel commented Nov 29, 2017

@weierophinney thanks for the review. I added all the feedbacks. There are some conflicts due to the date of this PR but I think we can manage it during the merge.

@weierophinney weierophinney merged commit 2e3bf2d into zendframework:master Nov 29, 2017
weierophinney added a commit that referenced this pull request Nov 29, 2017
Fixes the special characters in bindParam for PDO

Conflicts:
	src/Adapter/Driver/Pdo/Pdo.php
	test/Adapter/Driver/Pdo/StatementTest.php
weierophinney added a commit that referenced this pull request Nov 29, 2017
weierophinney added a commit that referenced this pull request Nov 29, 2017
weierophinney added a commit that referenced this pull request Nov 29, 2017
@ghost ghost mentioned this pull request Dec 7, 2017
@ezimuel
Copy link
Contributor Author

ezimuel commented Dec 7, 2017

I'm removing this PR because introduces issue like #288. It's not possible to fix #35 because of PHP bug 43130. This bug won't fix because of this and this. Releasing zend-db 2.9.1 with this revert.

@michalbundyra
Copy link
Member

@ezimuel

It's not possible to fix #35

It is possible, IMHO.
What if we also replace fields in the query, not only on binding?

@ezimuel
Copy link
Contributor Author

ezimuel commented Dec 7, 2017

@webimpress it's not safe to use characters like - in bind param name. Please, read the php.cvs 46848 and 47302.

@michalbundyra
Copy link
Member

@ezimuel yes, I've seen them. And as I understand you replace names with md5 names, but you are not changing these names in the query string but only on binding. What if we change them in both places?

@ezimuel
Copy link
Contributor Author

ezimuel commented Dec 7, 2017

@webimpress It's not considered a best practice to use these characters in bind param name. Why you want to allow a bad practice in our code base? Moreover, it will introduce high CPU usage, with preg_replace() + md5() execution for each param in SQL statements. This is not a reliable solution.

ezimuel added a commit to ezimuel/zend-db that referenced this pull request Dec 7, 2017
@michalbundyra
Copy link
Member

@ezimuel Ok, fine then, so the #35 should be marked as "invalid" or "won't fix" and never tried to be fixed 😄

@ezimuel
Copy link
Contributor Author

ezimuel commented Dec 7, 2017

@webimpress I know, this was my fault, sorry.

@michalbundyra
Copy link
Member

@ezimuel It's fine, you've tried to fix #178. Everyone makes mistake...
Good that you handle it very quickly and we will have shortly 2.9.1. Thanks for it! :)

@alextech
Copy link
Contributor

alextech commented Dec 7, 2017

Automatically changing anything in the query will make it very difficult to debug at database level. A common debugging procedure is looking through database logs, in SqlServer watching through real time activity monitor. Then, copy those queries and search for them in the codebase. Changing the names will no longer match logged queries with whats in code.

I think instead should do validation for special chars at the same place you tried md5() and throw InvalidArgumentException to give a nice message before letting it crash at driver level.

weierophinney added a commit that referenced this pull request Dec 7, 2017
@ezimuel
Copy link
Contributor Author

ezimuel commented Dec 7, 2017

@alextech I added the throw Exception for invalid characters [^a-zA-Z0-9_] in my revert PR #289.

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

Successfully merging this pull request may close these issues.

5 participants