Skip to content

Commit 673df15

Browse files
committed
regress test
1 parent 589963d commit 673df15

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

expected/plpgsql_check_active.out

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3953,3 +3953,45 @@ select * from plpgsql_check_function('test_bug');
39533953
(0 rows)
39543954

39553955
drop function test_bug(text);
3956+
create or replace function foo(a text, b text)
3957+
returns void as $$
3958+
begin
3959+
-- unsecure
3960+
execute 'select ' || a;
3961+
a := quote_literal(a); -- is safe now
3962+
execute 'select ' || a;
3963+
a := a || b; -- it is unsecure again
3964+
execute 'select ' || a;
3965+
end;
3966+
$$ language plpgsql;
3967+
\sf+ foo(text, text)
3968+
CREATE OR REPLACE FUNCTION public.foo(a text, b text)
3969+
RETURNS void
3970+
LANGUAGE plpgsql
3971+
1 AS $function$
3972+
2 begin
3973+
3 -- unsecure
3974+
4 execute 'select ' || a;
3975+
5 a := quote_literal(a); -- is safe now
3976+
6 execute 'select ' || a;
3977+
7 a := a || b; -- it is unsecure again
3978+
8 execute 'select ' || a;
3979+
9 end;
3980+
10 $function$
3981+
-- should to raise two warnings
3982+
select * from plpgsql_check_function('foo', security_warnings := true);
3983+
plpgsql_check_function
3984+
-----------------------------------------------------------------------------
3985+
security:00000:4:EXECUTE:text type variable is not sanitized
3986+
Query: SELECT 'select ' || a
3987+
-- ^
3988+
Detail: The EXECUTE expression is SQL injection vulnerable.
3989+
Hint: Use quote_ident, quote_literal or format function to secure variable.
3990+
security:00000:8:EXECUTE:text type variable is not sanitized
3991+
Query: SELECT 'select ' || a
3992+
-- ^
3993+
Detail: The EXECUTE expression is SQL injection vulnerable.
3994+
Hint: Use quote_ident, quote_literal or format function to secure variable.
3995+
(10 rows)
3996+
3997+
drop function foo(text, text);

sql/plpgsql_check_active.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,3 +2886,22 @@ select test_bug('kuku'); -- should be ok
28862886
select * from plpgsql_check_function('test_bug');
28872887

28882888
drop function test_bug(text);
2889+
2890+
create or replace function foo(a text, b text)
2891+
returns void as $$
2892+
begin
2893+
-- unsecure
2894+
execute 'select ' || a;
2895+
a := quote_literal(a); -- is safe now
2896+
execute 'select ' || a;
2897+
a := a || b; -- it is unsecure again
2898+
execute 'select ' || a;
2899+
end;
2900+
$$ language plpgsql;
2901+
2902+
\sf+ foo(text, text)
2903+
2904+
-- should to raise two warnings
2905+
select * from plpgsql_check_function('foo', security_warnings := true);
2906+
2907+
drop function foo(text, text);

src/check_expr.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,9 @@ plpgsql_check_expr_as_rvalue(PLpgSQL_checkstate *cstate, PLpgSQL_expr *expr,
773773
Node *node = plpgsql_check_expr_get_node(cstate, expr, false);
774774
int location;
775775

776-
if (!plpgsql_check_is_sql_injection_vulnerable(cstate, expr, node, &location))
776+
if (plpgsql_check_is_sql_injection_vulnerable(cstate, expr, node, &location))
777+
cstate->safe_variables = bms_del_member(cstate->safe_variables, targetdno);
778+
else
777779
cstate->safe_variables = bms_add_member(cstate->safe_variables, targetdno);
778780
}
779781
}

0 commit comments

Comments
 (0)