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

Fix check if table exists #16581

Merged
merged 3 commits into from Oct 19, 2020
Merged

Fix check if table exists #16581

merged 3 commits into from Oct 19, 2020

Conversation

sgiehl
Copy link
Member

@sgiehl sgiehl commented Oct 16, 2020

fixes #16580

@sgiehl sgiehl added not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org. Needs Review PRs that need a code review labels Oct 16, 2020
@sgiehl sgiehl added this to the 4.0.0-RC milestone Oct 16, 2020
@@ -37,7 +37,7 @@ public static function getTablesInstalled($forceReload = true)
*/
public static function tableExists($tableName)
{
return Db::get()->query("SHOW TABLES LIKE ?", $tableName)->rowCount() > 0;
Copy link
Member

Choose a reason for hiding this comment

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

Do bind parameters not work there basically @sgiehl ? If we prefix below, then we could also do an equal = comparison and not like?

Copy link
Member Author

Choose a reason for hiding this comment

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

Couldn't find any reliable information on that, but found various reports that variables don't work in SHOW TABLES query. Switching to an equal isn't possible in that case I guess as SHOW TABLES LIKE is a predefined construct, at least I wouldn't know how to convert that to a where condition that works...

@@ -37,7 +37,7 @@ public static function getTablesInstalled($forceReload = true)
*/
public static function tableExists($tableName)
{
return Db::get()->query("SHOW TABLES LIKE ?", $tableName)->rowCount() > 0;
return Db::get()->query(sprintf("SHOW TABLES LIKE '%s'", $tableName))->rowCount() > 0;
Copy link
Member

Choose a reason for hiding this comment

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

could maybe add a comment to the function to not use user input as table name to prevent SQL injections but that kind of applies everywhere for tables in Matomo anyway. At the same time might not hurt to mention it

Copy link
Member

Choose a reason for hiding this comment

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

Could also just remove ' characters from $tableName?

Copy link
Member

Choose a reason for hiding this comment

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

this alone would probably not prevent injections. wouldn't hurt though to do it additionally.

Copy link
Member

@diosmosis diosmosis Oct 19, 2020

Choose a reason for hiding this comment

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

out of curiosity, what kind of value would get past that? I guess we could also remove '"; and the backtick characters?

Copy link
Member

Choose a reason for hiding this comment

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

@diosmosis it depends on the use cases and how it's used. Eg you could trick Matomo into thinking the table is there or not there, or find out what tables exist by changing the input, etc. Make use of _ and %...

Copy link
Member

Choose a reason for hiding this comment

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

👍 that makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated.

@tsteur tsteur merged commit 6029f2c into 4.x-dev Oct 19, 2020
@tsteur tsteur deleted the fixtblexists branch October 19, 2020 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review PRs that need a code review not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Matomo 4 beta: MySQL syntax error
3 participants