need to add string in between a SQL statement using TCL -
i have sql statement stored inside variable:
i "statement" ".sql" file using file operations
set statement "select build_package, replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(substring(build_package, length(build_package) - position('/' in reverse(build_package)) + 2), '-bsd',''),'-lnx',''),'-esxi',''),'.repo',''),'.vmdk',''),'.qcow2',''),'.rpm',''),'.tbz',''),'-version',''),'.el7.x86_64','') ,'-nr',''),'-kvm',''),'-x86_64',''),'ptsvpl','svpts'),'spbvpl','svspb'),'sdevpl','svsde'),'-amd64',''),'.noarch',''),'.el6',''),'.x86_64',''),'tsevpl','svtse') scheduler_jobs id = '1617075' order id desc"
here clause e.g.: where id = 1617075
id dynamically generated.
so need first store sql without where id = 1617075
clause , add clause later when inside 'statement' variable.
basically add data called $id
in statement.
use prepared statement bound variables.
the example documentation:
set statement [db prepare { select phone_num directory first_name = :firstname , last_name = :lastname }] set firstname fred set lastname flintstone
as documentation explains,
the prepare object command against connection accepts arbitrary sql code executed against database. sql code may contain bound variables, strings of alphanumeric characters or underscores (the first character of string may not numeric), prefixed colon (:). if bound variable appears in sql statement, , not in string set off single or double quotes, nor in comment introduced --, becomes value substituted when statement executed. bound variable becomes single value (string or numeric) in resulting statement.
drivers responsible ensuring mechanism binding variables prevents sql injection.
in other words, while is driver's responsibility, should protected against sql injection. in other words, if id
isn't 1617075
rather 1617075; drop table scheduler_jobs;--
, should still okay, because driver has escaped (instead of you having this, missing edge case leaves vulnerable).
for example, leaving out replace
s, become
set statement [db prepare { select build_package scheduler_jobs id = :scheduler_job_id order id desc }] set scheduler_job_id 1617075
Comments
Post a Comment