Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Anti SQL Injection Helper

Anti SQL Injection Helper

Scheduled Pinned Locked Moved Visual Basic
regexjavascriptdatabasequestionlounge
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jim Taylor
    wrote on last edited by
    #1

    I want to take a belt and braces approach to avoiding SQL injection attacks. First step is to obviously used parameterised stored procedures. The second is to write a helper class to check input meets the expected format. To this end I'm thinking of writing a helper class that uses regular expressions. Imports System.Text.RegularExpressions Public Class AntiSQLInjectionHelper Private Sub New() End Sub Public Shared Function CheckString(ByVal Value As String, ByVal [CheckType] As CheckType) As Boolean Dim pattern As String Select Case [CheckType] Case CheckType.DateString 'Checks for accepted date format pattern = "TODO" Case CheckType.General 'Checks for accepted general format (ie one that doesn't contain any DROP commands etc) pattern = "TODO" Case CheckType.NumberString 'Checks for accepted number format pattern = "TODO" Case CheckType.PasswordString 'Checks for accepted password format pattern = "TODO" Case CheckType.UsernameString 'Checks for accepted username format pattern = "TODO" End Select Return Regex.IsMatch(Value, pattern) End Function Public Enum CheckType DateString NumberString UsernameString PasswordString General End Enum End Class Has anyone out there got a better way / done anything similar, think its a good idea or know of an alternative? I know there are validation controls that use javascript but a dtermined hacker can circumvent them. Jim

    D S 2 Replies Last reply
    0
    • J Jim Taylor

      I want to take a belt and braces approach to avoiding SQL injection attacks. First step is to obviously used parameterised stored procedures. The second is to write a helper class to check input meets the expected format. To this end I'm thinking of writing a helper class that uses regular expressions. Imports System.Text.RegularExpressions Public Class AntiSQLInjectionHelper Private Sub New() End Sub Public Shared Function CheckString(ByVal Value As String, ByVal [CheckType] As CheckType) As Boolean Dim pattern As String Select Case [CheckType] Case CheckType.DateString 'Checks for accepted date format pattern = "TODO" Case CheckType.General 'Checks for accepted general format (ie one that doesn't contain any DROP commands etc) pattern = "TODO" Case CheckType.NumberString 'Checks for accepted number format pattern = "TODO" Case CheckType.PasswordString 'Checks for accepted password format pattern = "TODO" Case CheckType.UsernameString 'Checks for accepted username format pattern = "TODO" End Select Return Regex.IsMatch(Value, pattern) End Function Public Enum CheckType DateString NumberString UsernameString PasswordString General End Enum End Class Has anyone out there got a better way / done anything similar, think its a good idea or know of an alternative? I know there are validation controls that use javascript but a dtermined hacker can circumvent them. Jim

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      I've never written a class for this... I've always used the Validation controls in my UI code. You might want to check out these two articles, here[^] and here[^], for an idea into using the Validation controls to do what your thinking of. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      J 1 Reply Last reply
      0
      • J Jim Taylor

        I want to take a belt and braces approach to avoiding SQL injection attacks. First step is to obviously used parameterised stored procedures. The second is to write a helper class to check input meets the expected format. To this end I'm thinking of writing a helper class that uses regular expressions. Imports System.Text.RegularExpressions Public Class AntiSQLInjectionHelper Private Sub New() End Sub Public Shared Function CheckString(ByVal Value As String, ByVal [CheckType] As CheckType) As Boolean Dim pattern As String Select Case [CheckType] Case CheckType.DateString 'Checks for accepted date format pattern = "TODO" Case CheckType.General 'Checks for accepted general format (ie one that doesn't contain any DROP commands etc) pattern = "TODO" Case CheckType.NumberString 'Checks for accepted number format pattern = "TODO" Case CheckType.PasswordString 'Checks for accepted password format pattern = "TODO" Case CheckType.UsernameString 'Checks for accepted username format pattern = "TODO" End Select Return Regex.IsMatch(Value, pattern) End Function Public Enum CheckType DateString NumberString UsernameString PasswordString General End Enum End Class Has anyone out there got a better way / done anything similar, think its a good idea or know of an alternative? I know there are validation controls that use javascript but a dtermined hacker can circumvent them. Jim

        S Offline
        S Offline
        Steven Campbell
        wrote on last edited by
        #3

        You missed the most common solution - use command parameters. You don't need stored procedures to get auto-protection from SQL Injection. Regular expressions are just overkill for this. For normal validation they are appropriate, but not at the database level. In terms of what you are doing, one approach is to create data type classes with built in validation. It is an "ok" solution, and works pretty well with inheritance, e.g. DBTypeUserName can inherit from DBTypeString. There are downsides to this approach, such as performance (because you are no longer using value types). Last point on validation - like you mentioned, client-side validation is unreliable. Always validate at least once more on the server-side. That said, the validation controls are harder to circumvent than you imply. The javascript is just one layer, the actual validation can be done on the code-behind, like: Sub MySubmit_Clicked() Page.Validate() If Page.IsValid then ... end If end Sub

        J 1 Reply Last reply
        0
        • D Dave Kreskowiak

          I've never written a class for this... I've always used the Validation controls in my UI code. You might want to check out these two articles, here[^] and here[^], for an idea into using the Validation controls to do what your thinking of. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          J Offline
          J Offline
          Jim Taylor
          wrote on last edited by
          #4

          Thanks very much I will look into the articles :) Jim

          1 Reply Last reply
          0
          • S Steven Campbell

            You missed the most common solution - use command parameters. You don't need stored procedures to get auto-protection from SQL Injection. Regular expressions are just overkill for this. For normal validation they are appropriate, but not at the database level. In terms of what you are doing, one approach is to create data type classes with built in validation. It is an "ok" solution, and works pretty well with inheritance, e.g. DBTypeUserName can inherit from DBTypeString. There are downsides to this approach, such as performance (because you are no longer using value types). Last point on validation - like you mentioned, client-side validation is unreliable. Always validate at least once more on the server-side. That said, the validation controls are harder to circumvent than you imply. The javascript is just one layer, the actual validation can be done on the code-behind, like: Sub MySubmit_Clicked() Page.Validate() If Page.IsValid then ... end If end Sub

            J Offline
            J Offline
            Jim Taylor
            wrote on last edited by
            #5

            Ah I didn't consider command parameters and also traditional custom validation javascript can be circumvented but as you indicated with .NET its a bit more integrated. Thanks for your help, saves me writing unnecessary code. :) Jim

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups