Skip to content

Commit bc2e1d8

Browse files
committed
regress tests fix
1 parent 8b117d7 commit bc2e1d8

File tree

2 files changed

+240
-0
lines changed

2 files changed

+240
-0
lines changed

expected/plpgsql_check_active_3.out

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3801,6 +3801,29 @@ select * from plpgsql_check_function('dyn_sql_3');
38013801
Context: SQL statement "SELECT r.c"
38023802
(2 rows)
38033803

3804+
drop function dyn_sql_3();
3805+
create or replace function dyn_sql_3()
3806+
returns void as $$
3807+
declare
3808+
r record;
3809+
v text = 'select 10 a, 20 b't;
3810+
begin
3811+
select 10 a, 20 b into r;
3812+
for r in execute v
3813+
loop
3814+
raise notice '%', r.a;
3815+
end loop;
3816+
end
3817+
$$ language plpgsql;
3818+
-- should be warning
3819+
select * from plpgsql_check_function('dyn_sql_3');
3820+
plpgsql_check_function
3821+
---------------------------------------------------------------------------------------
3822+
warning:00000:7:FOR over EXECUTE statement:cannot determinate a result of dynamic SQL
3823+
Detail: There is a risk of related false alarms.
3824+
Hint: Don't use dynamic SQL and record type together, when you would check function.
3825+
(3 rows)
3826+
38043827
drop function dyn_sql_3();
38053828
create or replace function dyn_sql_4()
38063829
returns table(ax int, bx int) as $$
@@ -3835,3 +3858,100 @@ select * from plpgsql_check_function('dyn_sql_4()');
38353858
(2 rows)
38363859

38373860
drop function dyn_sql_4();
3861+
create or replace function test_bug(text)
3862+
returns regproc as $$
3863+
begin
3864+
return $1::regproc;
3865+
exception when undefined_function or invalid_name then
3866+
raise;
3867+
end;
3868+
$$ language plpgsql;
3869+
-- should not raise a exception
3870+
select * from plpgsql_check_function('test_bug');
3871+
plpgsql_check_function
3872+
------------------------
3873+
(0 rows)
3874+
3875+
create or replace function test_bug(text)
3876+
returns regproc as $$
3877+
begin
3878+
return $1::regproc;
3879+
exception when undefined_function or invalid_name then
3880+
raise notice '%', $1; -- bug
3881+
end;
3882+
$$ language plpgsql;
3883+
select test_bug('kuku'); -- should to fail
3884+
NOTICE: kuku
3885+
ERROR: control reached end of function without RETURN
3886+
CONTEXT: PL/pgSQL function test_bug(text)
3887+
select * from plpgsql_check_function('test_bug');
3888+
plpgsql_check_function
3889+
--------------------------------------------------------------------
3890+
warning extra:2F005:control reached end of function without RETURN
3891+
(1 row)
3892+
3893+
drop function test_bug(text);
3894+
create or replace function test_bug(text)
3895+
returns regproc as $$
3896+
begin
3897+
return $1::regproc;
3898+
exception when undefined_function or invalid_name then
3899+
raise notice '%', $1;
3900+
return NULL;
3901+
end;
3902+
$$ language plpgsql;
3903+
select test_bug('kuku'); -- should be ok
3904+
NOTICE: kuku
3905+
test_bug
3906+
----------
3907+
3908+
(1 row)
3909+
3910+
select * from plpgsql_check_function('test_bug');
3911+
plpgsql_check_function
3912+
------------------------
3913+
(0 rows)
3914+
3915+
drop function test_bug(text);
3916+
create or replace function foo(a text, b text)
3917+
returns void as $$
3918+
begin
3919+
-- unsecure
3920+
execute 'select ' || a;
3921+
a := quote_literal(a); -- is safe now
3922+
execute 'select ' || a;
3923+
a := a || b; -- it is unsecure again
3924+
execute 'select ' || a;
3925+
end;
3926+
$$ language plpgsql;
3927+
\sf+ foo(text, text)
3928+
CREATE OR REPLACE FUNCTION public.foo(a text, b text)
3929+
RETURNS void
3930+
LANGUAGE plpgsql
3931+
1 AS $function$
3932+
2 begin
3933+
3 -- unsecure
3934+
4 execute 'select ' || a;
3935+
5 a := quote_literal(a); -- is safe now
3936+
6 execute 'select ' || a;
3937+
7 a := a || b; -- it is unsecure again
3938+
8 execute 'select ' || a;
3939+
9 end;
3940+
10 $function$
3941+
-- should to raise two warnings
3942+
select * from plpgsql_check_function('foo', security_warnings := true);
3943+
plpgsql_check_function
3944+
-----------------------------------------------------------------------------
3945+
security:00000:4:EXECUTE:text type variable is not sanitized
3946+
Query: SELECT 'select ' || a
3947+
-- ^
3948+
Detail: The EXECUTE expression is SQL injection vulnerable.
3949+
Hint: Use quote_ident, quote_literal or format function to secure variable.
3950+
security:00000:8:EXECUTE:text type variable is not sanitized
3951+
Query: SELECT 'select ' || a
3952+
-- ^
3953+
Detail: The EXECUTE expression is SQL injection vulnerable.
3954+
Hint: Use quote_ident, quote_literal or format function to secure variable.
3955+
(10 rows)
3956+
3957+
drop function foo(text, text);

expected/plpgsql_check_active_4.out

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3851,6 +3851,29 @@ select * from plpgsql_check_function('dyn_sql_3');
38513851
Context: SQL statement "SELECT r.c"
38523852
(2 rows)
38533853

3854+
drop function dyn_sql_3();
3855+
create or replace function dyn_sql_3()
3856+
returns void as $$
3857+
declare
3858+
r record;
3859+
v text = 'select 10 a, 20 b't;
3860+
begin
3861+
select 10 a, 20 b into r;
3862+
for r in execute v
3863+
loop
3864+
raise notice '%', r.a;
3865+
end loop;
3866+
end
3867+
$$ language plpgsql;
3868+
-- should be warning
3869+
select * from plpgsql_check_function('dyn_sql_3');
3870+
plpgsql_check_function
3871+
---------------------------------------------------------------------------------------
3872+
warning:00000:7:FOR over EXECUTE statement:cannot determinate a result of dynamic SQL
3873+
Detail: There is a risk of related false alarms.
3874+
Hint: Don't use dynamic SQL and record type together, when you would check function.
3875+
(3 rows)
3876+
38543877
drop function dyn_sql_3();
38553878
create or replace function dyn_sql_4()
38563879
returns table(ax int, bx int) as $$
@@ -3885,3 +3908,100 @@ select * from plpgsql_check_function('dyn_sql_4()');
38853908
(2 rows)
38863909

38873910
drop function dyn_sql_4();
3911+
create or replace function test_bug(text)
3912+
returns regproc as $$
3913+
begin
3914+
return $1::regproc;
3915+
exception when undefined_function or invalid_name then
3916+
raise;
3917+
end;
3918+
$$ language plpgsql;
3919+
-- should not raise a exception
3920+
select * from plpgsql_check_function('test_bug');
3921+
plpgsql_check_function
3922+
------------------------
3923+
(0 rows)
3924+
3925+
create or replace function test_bug(text)
3926+
returns regproc as $$
3927+
begin
3928+
return $1::regproc;
3929+
exception when undefined_function or invalid_name then
3930+
raise notice '%', $1; -- bug
3931+
end;
3932+
$$ language plpgsql;
3933+
select test_bug('kuku'); -- should to fail
3934+
NOTICE: kuku
3935+
ERROR: control reached end of function without RETURN
3936+
CONTEXT: PL/pgSQL function test_bug(text)
3937+
select * from plpgsql_check_function('test_bug');
3938+
plpgsql_check_function
3939+
--------------------------------------------------------------------
3940+
warning extra:2F005:control reached end of function without RETURN
3941+
(1 row)
3942+
3943+
drop function test_bug(text);
3944+
create or replace function test_bug(text)
3945+
returns regproc as $$
3946+
begin
3947+
return $1::regproc;
3948+
exception when undefined_function or invalid_name then
3949+
raise notice '%', $1;
3950+
return NULL;
3951+
end;
3952+
$$ language plpgsql;
3953+
select test_bug('kuku'); -- should be ok
3954+
NOTICE: kuku
3955+
test_bug
3956+
----------
3957+
3958+
(1 row)
3959+
3960+
select * from plpgsql_check_function('test_bug');
3961+
plpgsql_check_function
3962+
------------------------
3963+
(0 rows)
3964+
3965+
drop function test_bug(text);
3966+
create or replace function foo(a text, b text)
3967+
returns void as $$
3968+
begin
3969+
-- unsecure
3970+
execute 'select ' || a;
3971+
a := quote_literal(a); -- is safe now
3972+
execute 'select ' || a;
3973+
a := a || b; -- it is unsecure again
3974+
execute 'select ' || a;
3975+
end;
3976+
$$ language plpgsql;
3977+
\sf+ foo(text, text)
3978+
CREATE OR REPLACE FUNCTION public.foo(a text, b text)
3979+
RETURNS void
3980+
LANGUAGE plpgsql
3981+
1 AS $function$
3982+
2 begin
3983+
3 -- unsecure
3984+
4 execute 'select ' || a;
3985+
5 a := quote_literal(a); -- is safe now
3986+
6 execute 'select ' || a;
3987+
7 a := a || b; -- it is unsecure again
3988+
8 execute 'select ' || a;
3989+
9 end;
3990+
10 $function$
3991+
-- should to raise two warnings
3992+
select * from plpgsql_check_function('foo', security_warnings := true);
3993+
plpgsql_check_function
3994+
-----------------------------------------------------------------------------
3995+
security:00000:4:EXECUTE:text type variable is not sanitized
3996+
Query: SELECT 'select ' || a
3997+
-- ^
3998+
Detail: The EXECUTE expression is SQL injection vulnerable.
3999+
Hint: Use quote_ident, quote_literal or format function to secure variable.
4000+
security:00000:8:EXECUTE:text type variable is not sanitized
4001+
Query: SELECT 'select ' || a
4002+
-- ^
4003+
Detail: The EXECUTE expression is SQL injection vulnerable.
4004+
Hint: Use quote_ident, quote_literal or format function to secure variable.
4005+
(10 rows)
4006+
4007+
drop function foo(text, text);

0 commit comments

Comments
 (0)