For the argument, let's just say that we must create a local variable containing an SQL query with INSERT:
DECLARE @insert NVARCHAR(MAX) SELECT @insert = 'INSERT INTO [dbo].[' + @table + '] VALUES... EXEC (@insert)
This INSERT will also contain the column value:
DECLARE @insert NVARCHAR(MAX) SELECT @insert = 'INSERT INTO [dbo].[' + @table + '] VALUES (N''' + @message + ''')' EXEC (@insert)
Now I am clearly concerned about the injection attack and would like to make sure that the @message value cannot make the @insert value malicious or distorted as an EXEC request.
This brings us to my question: are there enough "characters in @message"? Are there any other characters that may appear in @message that may fail?
Example:
DECLARE @insert NVARCHAR(MAX) SELECT @message = REPLACE(@message,'''','''''') SELECT @insert = 'INSERT INTO [dbo].[' + @table + '] VALUES (N''' + @message + ''')' EXEC (@insert)
(When I say “ must ”, this is because my request is in a stored procedure, and this stored procedure accepts @table, which includes the INSERT destination table. I'm not interested in discussing my architecture or why the INSERT table is “dynamically” set using the procedure parameter. Please refrain from commenting on this if there is no other way than EXEC () for the query to specify the INSERT table when then the table name is accepted as the procedure parameter.)
sql sql-injection sql-server
core
source share