Allowing non malicious code into your database

Discussion in 'Security' started by danramosd, Jan 7, 2011.

  1. #1
    I have a project where i Need to allow people to enter code that will be stored in the database. I know your normally need to sanitize a form/text input before processing it but I would like the user to be able to enter code from any language without having it harm my system. What can I do to give the users this luxury without compromising my system? The site is LAMP and uses javascript as well.
     
    danramosd, Jan 7, 2011 IP
  2. cp_

    cp_ Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you want the code to ever execute?
     
    cp_, Jan 7, 2011 IP
  3. danramosd

    danramosd Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    83
    #3
    Nope, I would just like for it to be displayed to other users
     
    danramosd, Jan 7, 2011 IP
  4. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #4
    Store the code as plaintext, just make sure you escape special chars properly.
     
    Alex Roxon, Jan 9, 2011 IP
  5. cp_

    cp_ Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This. You should look into using htmlentities.
     
    cp_, Jan 9, 2011 IP
  6. danramosd

    danramosd Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    83
    #6
    Thanks for the info, never really heard of htmlentities. I just took a look into it and have a couple questions. If i store the data as htmlentities, do I still have to escape special chars? And should I convert the data to an htmlentity before inserting into a database or only when it is displayed to the user? Are there any other security precautions I should take when allowing users to submit their code (can range from JS, html, C++, SQL...)?
     
    danramosd, Jan 10, 2011 IP
  7. cp_

    cp_ Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Don't store the data as htmlentities; only call it when outputting the data. Making permanent changes to the code typically isn't a good idea, especially if you would at some point want to see raw output from the db. Remember to set the encoding on your page correctly (you should be using utf-8) as not doing so can make certain attacks possible. As for any other functions, you may want to use something like mysql(i)_real_escape to escape quotes when inserting and whatnot. I think that should be all you need to be able to accept code in your db...
     
    cp_, Jan 10, 2011 IP
  8. danramosd

    danramosd Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    83
    #8
    Excellent, ill give that a go. Your response was very helpful!
     
    danramosd, Jan 10, 2011 IP
  9. cp_

    cp_ Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You are welcome. ^^
     
    cp_, Jan 10, 2011 IP
  10. interwho

    interwho Member

    Messages:
    198
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #10
    Could you store the base64 version of the code?
     
    interwho, Jan 27, 2011 IP
  11. cp_

    cp_ Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You could, but it won't help much in preventing code from executing when outputting from the db.
     
    cp_, Jan 27, 2011 IP