Results 1 to 11 of 11

Thread: Sanitizing a key variable

  1. #1
    Join Date
    May 2005

    Sanitizing a key variable

    Is this line a reasonable way to sanitize an input of a primary key value to prevent SQL injection attacks?

    PHP Code:
        $post_id_sanitized sprintf("%u",mysql_real_escape_string($_POST['id'])); 
    Thanks in advance, I've got a lot of new stuff to work through here.


  2. #2
    Join Date
    May 2005
    Partially answering my own question, I threw this in a page:
    PHP Code:
    $request_input_sanitized sprintf("%u",mysql_real_escape_string($_REQUEST['input']));
    "".$_REQUEST['input']."<br />$request_input_sanitized"
    Then I loaded the page in the browser with the following query string;
    temp.php?input=rew123ture;select * from devices
    The return I got was this;
    123ture;select * from users
    Line two suggests that the function does indeed strip away the malicious code (imagine a 'drop' statement instead of 'select') and force the data to numeric.

    However, I'd still welcome input from others to tell me if I'm missing something. Perhaps this isn't always as effictive as it appears to be here.

  3. #3
    Join Date
    May 2005
    A quick correction to that last.

    If my input string started with rew123ture, I got a zero returned, if my input string started with numbers, I got the numbers, with the trailing string stripped away.

  4. #4
    Join Date
    Oct 2010
    Sprintf &#37;u type casting as unsigned integer. If you want it to be a string, you would need to use %s instead. When PHP converts a string to a number, it starts at the beginning and goes on until it finds a non-numeric character. This means that if it starts with a non-numeric character it will return 0, otherwise the beginning numbers.

    For more info about how PHP changes type cast see this info on type juggling.

    Last edited by Derokorian; 07-31-2011 at 02:52 PM.
    www.rdennispallas.com <-- Personal Site, changing regularly cuz its ugly.

  5. #5
    Join Date
    May 2005
    Thanks for that good info.

    I take it then that this is a valid way to sanitize inputs.

  6. #6
    Join Date
    Aug 2010
    using the proper escape_string function (in this case mysql_real_escape_string) along with building the query via sprintf is very acceptable way to sanitize input, just make sure you are typecasting the variables into the query the way you want them. %s for strings, %d for decimals, %u for unsigned integers, etc =D All the options can be found on the sprintf manual.

  7. #7
    Join Date
    May 2005
    Thank you for your input.

    I'd like to ask one more detail;

    When sanitizing inputs for my PHP/MySQL, I'm not positive what needs to be covered.

    Do I only need to sanitize those SQL queries that have components that come from a form or query string, or do I need to sanitize all queries regardless?

    Are the queries without form or URL input good to sanitize but not absolutely neccesary?

    I have a considerable amount of work to do to clean up a web application, and I'm not positive what needs to be done as I'm new to this aspect of PHP/MySQL.

  8. #8
    Join Date
    Aug 2010
    Personally I sanitize everything that can be found in a superglobal array, IE: $_GET, $_POST, $_COOKIE, and even $_SESSION tho the last may not be truly necessary. Anything hard coded, such as the table name, column names, join equalities and unchanging where clauses don't need any special sanitizing, they just need to be well formed queries.

    Just remember its always better to sanitize something that won't be harmful, than to leave something unsanitized that can be harmful.

  9. #9
    Join Date
    May 2005
    That's good info, thanks.

    I've been thinking more in terms of POST and GET; I might have missed cookies and session variables.

    I've got a lot of work ahead of me but I've got a better picture of what I need to do.

    Thanks again,


  10. #10
    Join Date
    Aug 2010
    I don't know what editor your use, or how your variables are currently placed into the queries. But if they are inserted in a standard way thru all scripts and you use Komodo Edit you could create regex replacement pattern to update all your queries at once! (this is why I love KE so much)

  11. #11
    Join Date
    May 2005
    I use Notepad++ and I'm sure I could use regex with it. Unfortunately, because I learned PHP/MySQL as I wrote this webapp, the structure is not as disciplined and consistent as it would be if I wrote it today.

    This may be an opportunity to impose some order on it, but I probably won't rewrite all the static queries.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11222 seconds
  • Memory Usage 2,953KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (11)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (11)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates