Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

79 rindas
3.9KB

  1. <%
  2. '=======================================================================================================================
  3. ' HTML SECURITY HELPER
  4. '=======================================================================================================================
  5. Class HTML_Security_Helper_Class
  6. '---------------------------------------------------------------------------------------------------------------------
  7. 'Uses Scriptlet.TypeLib to generate a GUID. There may be a better/faster way than this to generate a nonce.
  8. Public Function Nonce()
  9. dim TL : set TL = CreateObject("Scriptlet.TypeLib")
  10. Nonce = Left(CStr(TL.Guid), 38) 'avoids issue w/ strings appended after this token not being displayed on screen, MSFT bug
  11. set TL = Nothing
  12. End Function
  13. '---------------------------------------------------------------------------------------------------------------------
  14. 'Name is probably the combined ControllerName and ActionName of the form generator by convention
  15. Public Sub SetAntiCSRFToken(name)
  16. Session(name & ".anti_csrf_token") = Nonce()
  17. End Sub
  18. '---------------------------------------------------------------------------------------------------------------------
  19. 'Returns the CSRF token nonce from the session corresponding to the passed name
  20. Public Function GetAntiCSRFToken(name)
  21. dim token : token = Session(name & ".anti_csrf_token")
  22. If Len(token) = 0 or IsEmpty(token) then
  23. SetAntiCSRFToken name
  24. token = Session(name & ".anti_csrf_token")
  25. End If
  26. GetAntiCSRFToken = token
  27. End Function
  28. '---------------------------------------------------------------------------------------------------------------------
  29. 'Removes the current CSRF token nonce for the passed name
  30. Public Sub ClearAntiCSRFToken(name)
  31. Session.Contents.Remove(name & ".anti_csrf_token")
  32. End Sub
  33. '---------------------------------------------------------------------------------------------------------------------
  34. 'Returns true if passed nonce matches the stored CSRF token nonce for the specified name, false if not
  35. Public Function IsValidAntiCSRFToken(name, nonce)
  36. IsValidAntiCSRFToken = (GetAntiCSRFToken(name) = nonce)
  37. End Function
  38. '---------------------------------------------------------------------------------------------------------------------
  39. 'If an invalid CSRF nonce is passed, sets the flash and redirects using the appropriate MVC.Redirect* method.
  40. 'If a valid CSRF nonce is passed, clears it from the cache to reset the state to the beginning.
  41. Public Sub OnInvalidAntiCSRFTokenRedirectToAction(token_name, token, action_name)
  42. OnInvalidAntiCSRFTokenRedirectToExt token_name, token, MVC.ControllerName, action_name, empty
  43. End Sub
  44. Public Sub OnInvalidAntiCSRFTokenRedirectToActionExt(token_name, token, action_name, params)
  45. OnInvalidAntiCSRFTokenRedirectToExt token_name, token, MVC.ControllerName, action_name, params
  46. End Sub
  47. Public Sub OnInvalidAntiCSRFTokenRedirectTo(token_name, token, controller_name, action_name)
  48. OnInvalidAntiCSRFTokenRedirectToExt token_name, token, controller_name, action_name
  49. End Sub
  50. Public Sub OnInvalidAntiCSRFTokenRedirectToExt(token_name, token, controller_name, action_name, params)
  51. If IsValidAntiCSRFToken(token_name, token) then
  52. ClearAntiCSRFToken token_name
  53. Else
  54. ClearAntiCSRFToken token_name
  55. Flash.AddError "Invalid form state. Please try again."
  56. MVC.RedirectToExt controller_name, action_name, params
  57. End If
  58. End Sub
  59. End Class
  60. dim HTML_Security_Helper__Singleton
  61. Function HTMLSecurity()
  62. If IsEmpty(HTML_Security_Helper__Singleton) Then
  63. set HTML_Security_Helper__Singleton = new HTML_Security_Helper_Class
  64. End If
  65. set HTMLSecurity = HTML_Security_Helper__Singleton
  66. End Function
  67. %>

Powered by TurnKey Linux.