数据验证之类型与格式
2005-07-15 16:05
363 查看
我一向给予用户最大的自由和最严格的限制。这两点并不矛盾,对于用户的输入,能不做限制的地方尽量不做限制,但是一旦做出限制,就要进行严格的验证。比如在允许用户输入字符的地方,我会允许用户输入任何字符;在要求用户输入日期的地方,则对日期格式进行验证。
对于接收到的数据,安全的做法是验证其是否为空,长度及数据类型是否符合要求(与数据库字段定义是否一致),格式是否符合要求,甚至需要验证其来源。这样做,不仅是为了避免服务器返回错误,还可以避免遭受功击。
让我们看一下攻击者是怎么做的吧。以SQL注入为例,假如你有一个页面是list.asp?id=1,攻击者做的第一步是在这个URL后面加上and 1=1,如果你对接收到的id的值没有做数据类型的验证,攻击者会得到一个正常的页面,这就等于告诉他们,他们终究会得到他们想要的东西。
下面给出几个我常用的用于验证数据类型及格式的自定义函数
Rem 验证自然数
function CheckNar(thisnar)
dim patrn,matc
patrn = "^[1-9]{1}(/d)*$"
matc = CheckExp(patrn,thisnar)
CheckNar = matc
end function
Rem 验证电子邮件地址
function CheckMail(thismail)
dim patrn,matc
patrn = "^((/w)+[.-_]?(/w)+)+[@]{1}(/w)+([.]{1}(/w)+){1,3}$"
matc = CheckExp(patrn,thismail)
CheckMail = matc
end function
Rem 验证电话号码
function CheckTel(thistel)
dim patrn,matc
patrn = "^([0]{1}(/d)+[-]{1})?([1-9]{1}(/d)+[-]{1})?[1-9]{1}(/d)*$"
matc = CheckExp(patrn,thistel)
CheckTel = matc
end function
Rem 验证邮编
function CheckZip(thiszip)
dim patrn,matc
patrn = "^[0-9]{6}$"
matc = CheckExp(patrn,thiszip)
CheckZip = matc
end function
Rem 验证日期
function CheckDate(thisdate)
dim patrn,matc,theyear,themonth,theday,thisdate_s
patrn = "^(19|20){1}(/d){2}[-]{1}[01]?(/d){1}[-]{1}[0-3]?(/d){1}$"
matc = CheckExp(patrn,thisdate)
if matc = true then
thisdate_s = split(thisdate,"-")
theyear = cint(thisdate_s(0))
themonth = cint(thisdate_s(1))
theday = cint(thisdate_s(2))
if theday = 0 then matc = false
if themonth > 12 or themonth=0 then matc = false
if themonth = 2 then
'闰年
if (theyear mod 4 = 0) and (theyear mod 100 <> 0) or (theyear mod 400 = 0) then
if theday > 29 then matc = false
else
if theday > 28 then matc = false
end if
end if
if themonth = 4 or themonth = 6 or themonth = 9 or themonth = 11 then
if theday > 30 then matc = false
else
if theday > 31 then matc = false
end if
end if
CheckDate = matc
end function
Rem 正则表达式
function CheckExp(patrn,Strng)
dim re,matchs
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern = patrn
matchs = re.Test(Strng)
CheckExp = matchs
set re=Nothing
end function
以上自定义函数全部利用了正则表达式。
电子邮件地址,允许在邮件名(@以前的部分)使用.、-、_三个字符。
电话号码,只允许输入数字及连字符"-",对于010-88888888-8或88888888或13888888888等格式返回True
对于日期的验证,原本我们可以使用ASP内置的CData()函数:
function checkDate(dt)
on error resume next
tmp = cdate(dt)
if err.number <> 0 then
checkDate = false
else
checkDate = true
end if
end function
对于2005-1-1,05-1-1,05/1/1,2000-2-29返回True,对于2005-13-1,2005-2-29返回False,看上去似乎没有问题,但是对于05/2/29,也返回True,因为函数误认为我们要验证的是1929-2-5或2029-2-5,于是,我只好自己写一个函数了。该函数只可验证1900-2099之间的日期,通用性有一定限制,而且只允许以类似2005-7-15的格式输入日期,不过,它很准确。
[本文仅发表于www.netop.cc,blog.csdn.net/netops,netops.blogchina.com 若见于其它各处,皆为转载]
对于接收到的数据,安全的做法是验证其是否为空,长度及数据类型是否符合要求(与数据库字段定义是否一致),格式是否符合要求,甚至需要验证其来源。这样做,不仅是为了避免服务器返回错误,还可以避免遭受功击。
让我们看一下攻击者是怎么做的吧。以SQL注入为例,假如你有一个页面是list.asp?id=1,攻击者做的第一步是在这个URL后面加上and 1=1,如果你对接收到的id的值没有做数据类型的验证,攻击者会得到一个正常的页面,这就等于告诉他们,他们终究会得到他们想要的东西。
下面给出几个我常用的用于验证数据类型及格式的自定义函数
Rem 验证自然数
function CheckNar(thisnar)
dim patrn,matc
patrn = "^[1-9]{1}(/d)*$"
matc = CheckExp(patrn,thisnar)
CheckNar = matc
end function
Rem 验证电子邮件地址
function CheckMail(thismail)
dim patrn,matc
patrn = "^((/w)+[.-_]?(/w)+)+[@]{1}(/w)+([.]{1}(/w)+){1,3}$"
matc = CheckExp(patrn,thismail)
CheckMail = matc
end function
Rem 验证电话号码
function CheckTel(thistel)
dim patrn,matc
patrn = "^([0]{1}(/d)+[-]{1})?([1-9]{1}(/d)+[-]{1})?[1-9]{1}(/d)*$"
matc = CheckExp(patrn,thistel)
CheckTel = matc
end function
Rem 验证邮编
function CheckZip(thiszip)
dim patrn,matc
patrn = "^[0-9]{6}$"
matc = CheckExp(patrn,thiszip)
CheckZip = matc
end function
Rem 验证日期
function CheckDate(thisdate)
dim patrn,matc,theyear,themonth,theday,thisdate_s
patrn = "^(19|20){1}(/d){2}[-]{1}[01]?(/d){1}[-]{1}[0-3]?(/d){1}$"
matc = CheckExp(patrn,thisdate)
if matc = true then
thisdate_s = split(thisdate,"-")
theyear = cint(thisdate_s(0))
themonth = cint(thisdate_s(1))
theday = cint(thisdate_s(2))
if theday = 0 then matc = false
if themonth > 12 or themonth=0 then matc = false
if themonth = 2 then
'闰年
if (theyear mod 4 = 0) and (theyear mod 100 <> 0) or (theyear mod 400 = 0) then
if theday > 29 then matc = false
else
if theday > 28 then matc = false
end if
end if
if themonth = 4 or themonth = 6 or themonth = 9 or themonth = 11 then
if theday > 30 then matc = false
else
if theday > 31 then matc = false
end if
end if
CheckDate = matc
end function
Rem 正则表达式
function CheckExp(patrn,Strng)
dim re,matchs
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern = patrn
matchs = re.Test(Strng)
CheckExp = matchs
set re=Nothing
end function
以上自定义函数全部利用了正则表达式。
电子邮件地址,允许在邮件名(@以前的部分)使用.、-、_三个字符。
电话号码,只允许输入数字及连字符"-",对于010-88888888-8或88888888或13888888888等格式返回True
对于日期的验证,原本我们可以使用ASP内置的CData()函数:
function checkDate(dt)
on error resume next
tmp = cdate(dt)
if err.number <> 0 then
checkDate = false
else
checkDate = true
end if
end function
对于2005-1-1,05-1-1,05/1/1,2000-2-29返回True,对于2005-13-1,2005-2-29返回False,看上去似乎没有问题,但是对于05/2/29,也返回True,因为函数误认为我们要验证的是1929-2-5或2029-2-5,于是,我只好自己写一个函数了。该函数只可验证1900-2099之间的日期,通用性有一定限制,而且只允许以类似2005-7-15的格式输入日期,不过,它很准确。
[本文仅发表于www.netop.cc,blog.csdn.net/netops,netops.blogchina.com 若见于其它各处,皆为转载]
相关文章推荐
- JavaScript常用工具函数(验证数据格式、清除空格、判断浏览器类型、cookie操作等)
- js 正则表达式 验证 a/b/c 格式的类型数据
- js 正则表达式 验证 a/b/c 格式的类型数据
- 将具有固定格式的text 类型中的数据分离出来的一种方法
- Hadoop(十):简单了解Hadoop数据类型,输入输出格式及用户如何自定义。
- Oracle基本数据类型存储格式浅析(五)——RAW类型
- schema 验证python数据格式
- IOS数据类型对应输出格式
- 传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。参数 1 (""): 数据类型 0x38 未知。
- SpringMVC数据格式化——第七章 注解式控制器的数据验证、类型转换及格式化——跟着开涛学SpringMVC
- 第七章 SpringMVC数据类型转换—注解式控制器的数据验证、类型转换及格式化
- SpringMVC数据验证——注解式控制器的数据验证、类型转换及格式化
- 第七章 SpringMVC数据格式化—注解式控制器的数据验证、类型转换及格式化
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
- Hadoop自定义数据类型和输入格式
- mysql中Tinyint(1)数据格式自动转为boolean类型
- date类型数据格式问题
- java,返回的json数据中Date类型格式的转换
- 运算符的优先级和导包后的基本数据类型的格式
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler