Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Dyanmic Sql Validation and Security

Author  Topic 

lotek
Starting Member

42 Posts

Posted - 2005-11-11 : 14:16:09
Two questions:

1. Is there a way to validate dynamic sql? Or at least exececute it to see if its valid and then return and error if its not?

2. I am building something that allows a user to enter a where clause online to retrieve some records from a table. Does this present any security concerns? I am predefining the table then just concatenating the where, something like:

'SELECT field1, field2 FROM table WHERE (' + userInput + ')'

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2005-11-11 : 15:07:01
[code]select 'SELECT field1, field2 FROM table WHERE (' + '1=0) UNION select name, id from sysobjects where type = (''U''' + ')'
--Got a list of tables!
select 'SELECT field1, field2 FROM table WHERE (' + '1=0) UNION select name, xtype from syscolumns where (id = 1977058079' + ')'
--Got a list of fields!
select 'SELECT field1, field2 FROM table WHERE (' + '1=0) UNION update Employees set LastName = ''blindman'', Title = ''SQL God'' where(1=1' + ')'
--You done been hacked.[/code]
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2005-11-11 : 15:08:09
Oops...gotta change the UNION in the last statement to GO. But hey, I'm a hacker and I've got all day to sit around in my basement and play with this.
Go to Top of Page

lotek
Starting Member

42 Posts

Posted - 2005-11-11 : 15:16:34
Humm i see, is there any "safe" way to set something like this up? Basically i only have two views that the dynamic where queries would be run against. Maybee i could setup a user with read only access to those two views and run the queries using that user? Would that solve the security problem?
Go to Top of Page

lotek
Starting Member

42 Posts

Posted - 2005-11-11 : 15:20:36
Or would there be anyway to populate a table variable with the information that the user is allowed to query and then contrain their query to that data?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-14 : 01:19:51
More on Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -