<% '======================================================================================================================= ' HTML SECURITY HELPER '======================================================================================================================= Class HTML_Security_Helper_Class '--------------------------------------------------------------------------------------------------------------------- ' Returns a random RFC 4122 v4 UUID (native G3CRYPTO; replaces Scriptlet.TypeLib, which does not exist on AxonASP) Public Function Nonce() Dim crypto Set crypto = Server.CreateObject("G3CRYPTO") Nonce = crypto.UUID() Set crypto = Nothing End Function '--------------------------------------------------------------------------------------------------------------------- 'Name is probably the combined ControllerName and ActionName of the form generator by convention Public Sub SetAntiCSRFToken(name) Session(name & ".anti_csrf_token") = Nonce() End Sub '--------------------------------------------------------------------------------------------------------------------- 'Returns the CSRF token nonce from the session corresponding to the passed name Public Function GetAntiCSRFToken(name) Dim token token = "" & Session(name & ".anti_csrf_token") If Len(token) = 0 Then SetAntiCSRFToken name token = "" & Session(name & ".anti_csrf_token") ' re-read: return the newly created token End If GetAntiCSRFToken = token End Function '--------------------------------------------------------------------------------------------------------------------- 'Removes the current CSRF token nonce for the passed name Public Sub ClearAntiCSRFToken(name) Session.Contents.Remove(name & ".anti_csrf_token") End Sub '--------------------------------------------------------------------------------------------------------------------- 'Returns true if passed nonce matches the stored CSRF token nonce for the specified name, false if not Public Function IsValidAntiCSRFToken(name, nonce) ' An empty/missing nonce is never valid, and a token is never created just to be compared If Len("" & nonce) = 0 Or Len("" & Session(name & ".anti_csrf_token")) = 0 Then IsValidAntiCSRFToken = False Else IsValidAntiCSRFToken = (CStr(Session(name & ".anti_csrf_token")) = CStr(nonce)) End If End Function '--------------------------------------------------------------------------------------------------------------------- 'If an invalid CSRF nonce is passed, sets the flash and redirects using the appropriate MVC.Redirect* method. 'If a valid CSRF nonce is passed, clears it from the cache to reset the state to the beginning. Public Sub OnInvalidAntiCSRFTokenRedirectToAction(token_name, token, action_name) OnInvalidAntiCSRFTokenRedirectToExt token_name, token, MVC.ControllerName, action_name, empty End Sub Public Sub OnInvalidAntiCSRFTokenRedirectToActionExt(token_name, token, action_name, params) OnInvalidAntiCSRFTokenRedirectToExt token_name, token, MVC.ControllerName, action_name, params End Sub Public Sub OnInvalidAntiCSRFTokenRedirectTo(token_name, token, controller_name, action_name) OnInvalidAntiCSRFTokenRedirectToExt token_name, token, controller_name, action_name End Sub Public Sub OnInvalidAntiCSRFTokenRedirectToExt(token_name, token, controller_name, action_name, params) If IsValidAntiCSRFToken(token_name, token) then ClearAntiCSRFToken token_name Else ClearAntiCSRFToken token_name Flash.AddError "Invalid form state. Please try again." MVC.RedirectToExt controller_name, action_name, params End If End Sub End Class dim HTML_Security_Helper__Singleton Function HTMLSecurity() If IsEmpty(HTML_Security_Helper__Singleton) Then set HTML_Security_Helper__Singleton = new HTML_Security_Helper_Class End If set HTMLSecurity = HTML_Security_Helper__Singleton End Function %>