【问题标题】:Sanitize user input with the USING keyword in PL/pgSQL在 PL/pgSQL 中使用 USING 关键字清理用户输入
【发布时间】:2018-01-14 12:23:52
【问题描述】:

这就是我创建search_term 的方式:

    IF char_length(search_term) > 0 THEN
        order_by := 'ts_rank_cd(textsearchable_index_col, to_tsquery(''' || search_term || ':*''))+GREATEST(0,(-1*EXTRACT(epoch FROM age(last_edited)/86400))+60)/60 DESC';
        search_term := 'to_tsquery(''' || search_term || ':*'') @@ textsearchable_index_col';
    ELSE
        search_term := 'true';
    END IF;

我在使用 PLPGSQL 函数时遇到了一些问题:

    RETURN QUERY EXECUTE '
        SELECT
            *
        FROM
            articles
        WHERE
            $1 AND
            ' || publication_date_query || ' AND
            primary_category LIKE ''' || category_filter || ''' AND
            ' || tags_query || ' AND
            ' || districts_query || ' AND
            ' || capability_query || ' AND
            ' || push_notification_query || ' AND
            ' || distance_query || ' AND
            ' || revision_by || ' AND
            ' || publication_priority_query || ' AND
            ' || status_query || ' AND
            is_template = ' || only_templates || ' AND
            status <> ''DELETED''
        ORDER BY ' || order_by || ' LIMIT 500'
        USING search_term;
    END; $$;

返回错误:

AND 的参数必须是布尔类型,而不是在字符 64 处键入文本

相对于:

        RETURN QUERY EXECUTE '
            SELECT
                *
            FROM
                articles
            WHERE
                ' || search_term || ' AND
                ' || publication_date_query || ' AND
                primary_category LIKE ''' || category_filter || ''' AND
                ' || tags_query || ' AND
                ' || districts_query || ' AND
                ' || capability_query || ' AND
                ' || push_notification_query || ' AND
                ' || distance_query || ' AND
                ' || revision_by || ' AND
                ' || publication_priority_query || ' AND
                ' || status_query || ' AND
                is_template = ' || only_templates || ' AND
                status <> ''DELETED''
            ORDER BY ' || order_by || ' LIMIT 500';
        END; $$;

...这行得通。我错过了什么吗?
我的目标是清理我的用户输入。

【问题讨论】:

  • 所以问题出在$1
  • 什么是search_term?如果它是 column_a = 'some_string' 之类的东西,那么它不适用于准备好的语句,因为它们不能用于动态 SQL。
  • search_term 是用户输入,也就是任意字符串值。
  • 还有其他方法可以清理我的用户输入吗?
  • search_term 不能只是任意字符串值。它必须是一个 expression 在执行时评估为布尔值 - 否则您的第二个代码片段也会出错。请显示SSCCE 和一些示例输入。不要用大量不相关的噪音但缺少重要部分来倾倒整个代码片段。对于初学者来说,你的这两种尝试都对清理用户输入没有好处。巨大的 SQL 注入漏洞...

标签: sql database postgresql plpgsql dynamic-sql


【解决方案1】:

如果您的某些输入参数可以是 NULLempty 并且在这种情况下应该被忽略,您最好根据用户输入动态构建整个语句 - 并省略各自的WHERE / ORDER BY 子句完全。

关键是在处理过程中正确、安全(优雅地)处理NULL和空字符串。对于初学者来说,search_term &lt;&gt; '' 是比char_length(search_term) &gt; 0 更智能的测试。见:

而且您需要对 PL/pgSQL 有深入的了解,否则您可能会不知所措。您的案例的示例代码:

CREATE OR REPLACE FUNCTION my_func(
         _search_term            text = NULL  -- default value NULL to allow short call
       , _publication_date_query date = NULL 
    -- , more parameters
       )
  RETURNS SETOF articles AS
$func$
DECLARE
   sql       text;
   sql_order text;   -- defaults to NULL

BEGIN
   sql := concat_ws(' AND '
    ,'SELECT * FROM articles WHERE status <> ''DELETED'''  -- first WHERE clause is immutable
    , CASE WHEN _search_term <> ''            THEN '$1 @@ textsearchable_index_col' END  -- ELSE NULL is implicit
    , CASE WHEN _publication_date_query <> '' THEN 'publication_date > $2'          END  -- or similar ...
 -- , more more parameters
   );

   IF search_term <> '' THEN  -- note use of $1!
      sql_order  := 'ORDER BY ts_rank_cd(textsearchable_index_col, $1) + GREATEST(0,(-1*EXTRACT(epoch FROM age(last_edited)/86400))+60)/60 DESC';
   END IF;

   RETURN QUERY EXECUTE concat_ws(' ', sql, sql_order, 'LIMIT 500')
   USING  to_tsquery(_search_term || ':*')  -- $1  -- prepare ts_query once here!
        , _publication_date_query           -- $2  -- order of params must match!
     -- , more parameters
   ;

END
$func$  LANGUAGE plpgsql;

我为函数参数添加了默认值,因此您可以省略调用中不适用的参数。喜欢:

SELECT * FROM my_func(_publication_date_query => '2016-01-01');

更多:

注意concat_ws() 的战略用途。见:

这是一个相关的答案,有很多解释:

【讨论】:

    猜你喜欢
    • 2011-12-17
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 2013-09-14
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多