Skip to content

Add IN helper for prepared statements #12219

Closed

Description

@LukasReschke

Our codebase contains currently gems such as:

$ids = array();
                    $parents = array($item['id']);
                    while (!empty($parents)) {
                        $parents = "'".implode("','", $parents)."'";
                        $query = \OC_DB::prepare('SELECT `id`, `permissions` FROM `*PREFIX*share`'
                            .' WHERE `parent` IN ('.$parents.')');
                        $result = $query->execute();
                        // Reset parents array, only go through loop again if
                        // items are found that need permissions removed
                        $parents = array();
                        while ($item = $result->fetchRow()) {
                            // Check if permissions need to be removed
                            if ($item['permissions'] & ~$permissions) {
                                // Add to list of items that need permissions removed
                                $ids[] = $item['id'];
                                $parents[] = $item['id'];
                            }
                        }
                    }
                    // Remove the permissions for all reshares of this item
                    if (!empty($ids)) {
                        $ids = "'".implode("','", $ids)."'";
                        // TODO this should be done with Doctrine platform objects
                        if (\OC_Config::getValue( "dbtype") === 'oci') {
                            $andOp = 'BITAND(`permissions`, ?)';
                        } else {
                            $andOp = '`permissions` & ?';
                        }
                        $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = '.$andOp
                            .' WHERE `id` IN ('.$ids.')');
                        $query->execute(array($permissions));

Those cases are a potentially pitfall for SQL injections when the code is "incorrectly" called and is very hard to audit. This should use a helper for IN statements instead.

Activity

  1. self-assigned this
    on Nov 17, 2014
  2. added this to the milestone on Nov 17, 2014
  3. PVince81 commented on Nov 24, 2014

    @PVince81
    Contributor

    Can we use this: https://github.com/doctrine/dbal/blob/master/docs/en/reference/data-retrieval-and-manipulation.rst#list-of-parameters-conversion ?

    It seems when having arrays we cannot use prepared statements anyway... unless they have the same number of parameters every time.

  4. PVince81 commented on Nov 24, 2014

    @PVince81
    Contributor

    And maybe it could still use a prepared statement when only a single value was passed, in case that happens often.

  5. PVince81 commented on Dec 9, 2014

    @PVince81
    Contributor

    This will also need to support chunking (no more than 1000 elements in the IN) statement

  6. DeepDiver1975 commented on Jan 6, 2015

    @DeepDiver1975
    Contributor

    Can doctrine's query builder help us with this? Any way -> OC8.1

  7. modified the milestones: , on Jan 6, 2015
  8. LukasReschke commented on Feb 18, 2015

    @LukasReschke
    ContributorAuthor

    The query builder does this nicely from what I can tell.

  9. locked as resolved and limited conversation to collaborators on Aug 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions