SP accepts key and returns flag status. In Checkmarx SAST scan key, It is saying parameter not validated before being used in query. Please help to understand what validation can be added so that it passes the vulnerability test.
Create procedure sp_name (key varchar2, v_flag out number) as
v_key varchar2(50);
v_sql clob;
Begin
If key is null then
v_flag:=-1;
Else
v_key:=sys.dbms_assert.enquote_literal(upper(trim(key)));
v_key:=replace(v_key,'''');
v_sql:=q'[select flag from tbl where key=:1]';
Execute immediate v_sql into v_flag using v_key;
End if;
Exception when no_data_found then
v_flag:=-1;
End;
I tried bind variable and dbms_assert but still it shows code is vulnerable
Stop using
EXECUTE IMMEDIATEwhen you do not need to (and trying to make it overly complicated) and use a simpleSELECT ... INTO ...statement with the PL/SQL variable as the bind variable (and give the PL/SQL variable a different name to the table's column).The procedure can be simplified to:
Then, if you have the sample data:
Then:
Outputs:
Or, if you want it slightly more complicated to avoid the
SELECTif the key isNULLthen:fiddle