Error when accessing cookies when a cookies without a name exists

99封情书 提交于 2019-12-05 18:37:20

A second answer to my own question and the solution I have now implemented is to add the following code to my common include file.

It tests whether Classic ASP can read the cookies and, using error trapping, ends the response if an error is detected.

On Error Resume Next
Request.Cookies("test")
If Err.Number <> 0 Then Response.End
On Error Goto 0

This is a better solution to my other answer as there is no point in generating a page for what is obviously an attack of some sort so ending the script as soon as possible is a better choice.

My proposed answer to my own question is to create a class that extracts all the valid keys and values for the cookies on initialisation, and has a function to return a value for a specified key.

Unfortunately it doesn't work for cookies that contain a collection of multiple values, but I don't generally use these anyway.

Here is the class:

<%
Class MyRequest
    Private m_objCookies

    Private Sub Class_Initialize()
        Dim strCookies, i, strChar, strName, strValue, blnInValue
        strCookies = Request.ServerVariables("HTTP_COOKIE")
        Set m_objCookies = Server.CreateObject("Scripting.Dictionary")
        i = 1
        strName = ""
        strValue = ""
        blnInValue = False
        Do
            strChar = Mid(strCookies, i, 1)
            If strChar = ";" Or i = Len(strCookies) Then
                strValue = Trim(strValue)
                If strName <> "" And strValue <> "" Then
                    If m_objCookies.Exists(strName) Then
                        m_objCookies.Item(strName) = strValue
                    Else
                        m_objCookies.Add strName, strValue
                    End If
                End If
                If i = Len(strCookies) Then Exit Do 
                strName = ""
                strValue = ""
                blnInValue = False
            ElseIf strChar = "=" Then
                strName = Trim(strName)
                blnInValue = True
            ElseIf blnInValue Then
                strValue = strValue & strChar
            Else
                strName = strName & strChar
            End If
            i = i + 1
        Loop
    End Sub

    Public Function Cookies(strKey)
        Cookies = m_objCookies.Item(strKey)
    End Function
End Class
%>

The changes to my code to use this class are minimal. Where I currently have...

strCookieCart = Request.Cookies("cart")

I will need to change to...

Dim objMyRequest : Set objMyRequest = New MyRequest
strCookieCart = objMyRequest.Cookies("cart")

I have tested the above with many of the bad requests I have logged and it works fine.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!