Consolidated ASP Classic MVC framework from best components
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

86 lignes
4.1KB

  1. <%
  2. '=======================================================================================================================
  3. ' HTML SECURITY HELPER
  4. '=======================================================================================================================
  5. Class HTML_Security_Helper_Class
  6. '---------------------------------------------------------------------------------------------------------------------
  7. ' Returns a random RFC 4122 v4 UUID (native G3CRYPTO; replaces Scriptlet.TypeLib, which does not exist on AxonASP)
  8. Public Function Nonce()
  9. Dim crypto
  10. Set crypto = Server.CreateObject("G3CRYPTO")
  11. Nonce = crypto.UUID()
  12. Set crypto = Nothing
  13. End Function
  14. '---------------------------------------------------------------------------------------------------------------------
  15. 'Name is probably the combined ControllerName and ActionName of the form generator by convention
  16. Public Sub SetAntiCSRFToken(name)
  17. Session(name & ".anti_csrf_token") = Nonce()
  18. End Sub
  19. '---------------------------------------------------------------------------------------------------------------------
  20. 'Returns the CSRF token nonce from the session corresponding to the passed name
  21. Public Function GetAntiCSRFToken(name)
  22. Dim token
  23. token = "" & Session(name & ".anti_csrf_token")
  24. If Len(token) = 0 Then
  25. SetAntiCSRFToken name
  26. token = "" & Session(name & ".anti_csrf_token") ' re-read: return the newly created token
  27. End If
  28. GetAntiCSRFToken = token
  29. End Function
  30. '---------------------------------------------------------------------------------------------------------------------
  31. 'Removes the current CSRF token nonce for the passed name
  32. Public Sub ClearAntiCSRFToken(name)
  33. Session.Contents.Remove(name & ".anti_csrf_token")
  34. End Sub
  35. '---------------------------------------------------------------------------------------------------------------------
  36. 'Returns true if passed nonce matches the stored CSRF token nonce for the specified name, false if not
  37. Public Function IsValidAntiCSRFToken(name, nonce)
  38. ' An empty/missing nonce is never valid, and a token is never created just to be compared
  39. If Len("" & nonce) = 0 Or Len("" & Session(name & ".anti_csrf_token")) = 0 Then
  40. IsValidAntiCSRFToken = False
  41. Else
  42. IsValidAntiCSRFToken = (CStr(Session(name & ".anti_csrf_token")) = CStr(nonce))
  43. End If
  44. End Function
  45. '---------------------------------------------------------------------------------------------------------------------
  46. 'If an invalid CSRF nonce is passed, sets the flash and redirects using the appropriate MVC.Redirect* method.
  47. 'If a valid CSRF nonce is passed, clears it from the cache to reset the state to the beginning.
  48. Public Sub OnInvalidAntiCSRFTokenRedirectToAction(token_name, token, action_name)
  49. OnInvalidAntiCSRFTokenRedirectToExt token_name, token, MVC.ControllerName, action_name, empty
  50. End Sub
  51. Public Sub OnInvalidAntiCSRFTokenRedirectToActionExt(token_name, token, action_name, params)
  52. OnInvalidAntiCSRFTokenRedirectToExt token_name, token, MVC.ControllerName, action_name, params
  53. End Sub
  54. Public Sub OnInvalidAntiCSRFTokenRedirectTo(token_name, token, controller_name, action_name)
  55. OnInvalidAntiCSRFTokenRedirectToExt token_name, token, controller_name, action_name
  56. End Sub
  57. Public Sub OnInvalidAntiCSRFTokenRedirectToExt(token_name, token, controller_name, action_name, params)
  58. If IsValidAntiCSRFToken(token_name, token) then
  59. ClearAntiCSRFToken token_name
  60. Else
  61. ClearAntiCSRFToken token_name
  62. Flash.AddError "Invalid form state. Please try again."
  63. MVC.RedirectToExt controller_name, action_name, params
  64. End If
  65. End Sub
  66. End Class
  67. dim HTML_Security_Helper__Singleton
  68. Function HTMLSecurity()
  69. If IsEmpty(HTML_Security_Helper__Singleton) Then
  70. set HTML_Security_Helper__Singleton = new HTML_Security_Helper_Class
  71. End If
  72. set HTMLSecurity = HTML_Security_Helper__Singleton
  73. End Function
  74. %>

Powered by TurnKey Linux.