PostgreSQL 데이터베이스에 쓰는 다음 함수가 있습니다. SQL 인젝션으로부터 안전하게 만들어야하지만 어떻게해야할지 모르겠습니다.
어셈블 된 쿼리의 일부는 pg_query_params
주입 으로부터 안전하지만 (또는 내가 말했듯이) PHP의 문자열 연결을 통해 어셈블 된 쿼리의 다른 부분은 .
주입에 취약합니다.
private function setItem($table, $id, $field, $itemId, $fieldValue){
$_1 = $itemId;
$_2 = $fieldValue;
$_3 = $field;
$_4 = $table;
$_5 = $id;
$parameters = array($_1, $_2);
// If the ID already exists, then update the name!
$sql = 'update ' . $_4 . ' set ' .$_3 . ' = $2 where ' . $_5 . ' = $1;';
/*$result = */pg_query_params($this->database, $sql, $parameters);
// Add ID and Name into table.
$sql = 'insert into ' . $_4 . '(' . $_5 . ',' . $_3 . ') select $1, $2 where not exists(select 1 from ' . $_4 . ' where ' . $_5 . '=$1)';
$result = pg_query_params($this->database, $sql, $parameters);
return $result;
}
편집하다:
PHP에서 SQL 주입을 어떻게 방지 할 수 있습니까? 내 우려를 해결하지 못하는 것 같습니다.
PostgreSQL을 사용하고 있으며 호환되는 것을 찾으려고합니다. pg_query_params
실행할 쿼리를 만들기 전에 quote_ident ()를 사용하여 데이터베이스에 테이블 및 열 이름을 보호하도록 요청할 수 있습니다 . 다음과 같은 것이 필요합니다.
<?php
$table = 'table name'; // unsafe
$column = 'column name'; // unsafe
$result = pg_query_params($connection,
'SELECT quote_ident(CAST($1 AS text)), quote_ident(CAST($2 AS text));',
array($table, $column)
);
$table = pg_fetch_result($result, 0, 0); // safe
$column = pg_fetch_result($result, 0, 1); // safe
$sql = 'INSERT INTO '.$table.'('.$column.') VALUES($1);';
echo $sql;
$result = pg_query_params($connection, $sql, array('foo'));
?>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다