How to escape sql injection from HANA placeholder

社会主义新天地 提交于 2020-01-25 07:21:07

问题


I have some HANA queries which use PLACEHOLDER input and of course I want to prevent an sql injection.

I try to use ? in odbc_prepare()

$query = <<<SQL
SELECT
    col,
    ...
FROM table_name('PLACEHOLDER'=('$$some_key$$', ?))
WHERE col = ?
SQL;
$stmt = \odbc_prepare($conn, $query);

if ($stmt !== false) {
    \odbc_execute($stmt, ['placeholder_value', 'where_value']);
}

but I receive this warning:

Warning: odbc_prepare(): SQL error: [SAP AG][LIBODBCHDB SO][HDBODBC] Syntax error or access violation;257 sql syntax error: incorrect syntax near &quot;?&quot;: line 32 col 40 (at pos 1283), SQL state 37000 in SQLPrepare

and statement wasn't created. So my code now looks like this:

$query = <<<SQL
SELECT
    col,
    ...
FROM table_name('PLACEHOLDER'=('$$some_key$$', 'placeholder_value'))
WHERE col = ?
SQL;
$stmt = \odbc_prepare($conn, $query);

if ($stmt !== false) {
    \odbc_execute($stmt, ['where_value']);
}

As I see here htmlspecialchars() is not enough to prevent an SQL injection.

I can't remove input placeholder, I don't own HANA.

Is there any other way to prevent SQL injection in PLACEHOLDER?


回答1:


The (old) placeholder syntax ('PLACEHOLDER'=('<varname>', '<var value>')) you're using here does not allow for bind variables.

Instead, the new placeholder syntax (PLACEHOLDER."<varname>"=>?) allows using bind variables.

In your code this would look like this:

$query = <<<SQL
SELECT
    col,
    ...
FROM table_name (PLACEHOLDER."$$some_key$$", ?)
WHERE col = ?
SQL;
$stmt = \odbc_prepare($conn, $query);


来源:https://stackoverflow.com/questions/58483948/how-to-escape-sql-injection-from-hana-placeholder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!