Jo, det skall du göra om du använder SQL-frågor med strängar som kommer från en formulärinmatning. Det är dock inte av säkerhetsskäl i första hand, utan för att ett inmatat '-tecken bryter SQL-frågan och skapar ett fel.
men kan användare skicka ASP-kod i formulär och förstöra mha det?
Nej, inte om inte du själv bestämt det och exekverar koden mha en metod som ExecuteGlobal el liknande. I standardutförande alltså: Nej.
Egentligen gör det inte det, men det allra bästa är att escape:a det endast. Det gör du genom att byta ut en apostrof mot två. Då är strängen korrekt när du plockar ut den ur databasen, utan att du behöver göra några ersättningar.
Detta är alltså rätt sätt:
strSQL = "SELECT * FROM tblTable WHERE fldField = 'O''Malley'"
Om vi utgår i från att namnet O'Malley finns i asp-variabeln strName kan vi alltså göra så här:
strSQL = "SELECT * FROM tblTable WHERE fldField = '" & replace(strName, "'", "''") & "'"
Jag hittade detta på Micrsoftsida om att kunna förstöra genom att skicka kod i formulär:
Check <FORM> and Querystring Input in Your ASP Code
Many sites use input from a user to call other code or build SQL statements directly. In other words, they're treating the input as valid, well-formed, nonmalicious input. This should not be so; there are a number of attacks where user input is treated incorrectly as valid input and the user could gain access to the server or cause damage. You should always check each <FORM> input and query string before passing it on to another process or method call that might use an external resource such as the file system or a database.
You can perform text checking with the JScript V5 and VBScript V5 regular expression capabilities. The following example code will strip a string of all invalid characters (characters that are not 0-9a-zA-Z or _):
Set reg = New RegExp
reg.Pattern = "\W+" ' One or more characters which
' are NOT 0-9a-zA-Z or '_'
strUnTainted = reg.Replace(strTainted, "")
The following sample will strip all text after a | operator:
Set reg = New RegExp
reg.Pattern = "^(.+)\|(.+)" ' Any character from the start of
' the string to a | character.
strUnTainted = reg.Replace(strTainted, "$1")
Also, be careful when opening or creating files by using Scripting File System Object. If the filename is based on the user's input, the user might attempt to open a serial port or printer. The following JScript code will strip out invalid filenames:
var strOut = strIn.replace(/(AUX|PRN|NUL|COM\d|LPT\d)+\s*$/i,"");