2016-10-05 5 views
0

내 Silex 프로젝트에서 Doctrine QueryBuilder를 사용하고 있습니다. 나는 사용자 입력을 기반으로 쿼리를 작성하고 있습니다. 매개 변수는 setParameter() 기능으로 이스케이프됩니다. 그러나 addSelect()은 어떨까요?Doctrine QueryBuilder 사용자 입력이있는 addSelect

$stmt = $this->conn->createQueryBuilder() 
    ->addSelect("`".$userinputCol1."`") 
    ->addSelect("`".$userinputCol2."`") 
    ->from('`mytable`') 
    ->where('id = :id') 
    ->setParameter('id', $userinputId) 
    ->execute(); 

이이 SELECT에서 SQL 주입을 방지 할 수 있습니다 :

나는이 알아낼 수 없습니다,이 같은 뭔가를 저장한다?

+0

매개 변수를 사용할 수 없습니까? 'addSelect (': userInputCol1')' – Veve

+0

@Veve No. 그런 다음 작은 따옴표로 이스케이프 처리됩니다. – Timo002

답변

1

일반적으로 나는 사용자 입력을 QueryBuilder 메서드로 직접 전달하지 않으며 심지어 이스케이프 처리도하지 않습니다. 이 -

$possibleSelectFields = ['name', 'dob', 'created', 'something']; 
    if (!in_array($userInputCol1, $possibleSelectFields) || !in_array($userInputCol2, $possibleSelectFields)) { 
     throw new \Exception("Throw an exception, or just silently ignore the user\'s input - depending on how bad this situation is for your application."); 
    } 

    //...Your code as before 

그것은 좀 더 자세한하지만 훨씬 더 방어의 - 아마도 당신은 사용자가 볼 수 싶지 않을 것이라는 점을 그 쿼리가 노출 될 수 있습니다 열이있을 수 있습니다 : 나는 같은 것을 할 것 접근은 그 문제를 피한다.

+0

그건 사실입니다. 나는 그 문제를 해결할 것이다. – Timo002

관련 문제