Пример:
create table documents (id, value) as
(
select 1, 'x' from dual union all
select 2, 'x' from dual union all
select 3, 'y' from dual
)
Уязвимая функция:
create or replace function countDocs(pValue IN varchar2) return number is
vRetVal number;
begin
execute immediate 'select count(*) from documents where value = ''' || pValue || ''''
into vRetVal;
return vRetVal;
end;
Что вы можете сделать:
SQL> select countDocs('y') from dual;
COUNTDOCS('Y')
--------------
1
SQL> select countDocs('y'' or ''a''=''a') from dual;
COUNTDOCS('Y''OR''A''=''A')
---------------------------
3
Безопасный способ может быть с переменными связывания:
create or replace function countDocsSafe(pValue IN varchar2) return number is
vRetVal number;
begin
execute immediate 'select count(*) from documents where value = :bindVar'
into vRetVal
using pValue ;
return vRetVal;
end;
Что дает:
SQL> select countDocsSafe('y') from dual;
COUNTDOCSSAFE('Y')
------------------
1
SQL> select countDocsSafe('y'' or ''a''=''a') from dual;
COUNTDOCSSAFE('Y''OR''A''=''A')
-------------------------------
0