473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

MySQL/PHP: Check data length before INSERT?


Folks,

I've heard of buffer overflows being used/abused by hackers and believe one
method to reduce this from happening is to check the length of my form data
before writing it to my MySQL database.

Is my understanding correct?

At the moment, I pass all my data through htmlentities() before writing to
my database. Is this enough? Should I check each individual columns length
first, or perhaps the overall content length to fit within the maximum
record length?

I found a function called mysql_escape_string() and have thought of using
it - but other than strip_slashes(), I don't know the reverse (unless
strip_slashes() is the recommended opposite of mysql_escape_string()).

Can someone advise? Much appreciated,

--
Replies please... via the newsgroup, so everyone can learn...
Thanks,
Randell D.
Jul 17 '05 #1
7 7368
Randell D. wrote:
I've heard of buffer overflows being used/abused by hackers and believe one
method to reduce this from happening is to check the length of my form data
before writing it to my MySQL database.
MySQL should cut it for you, if it is too long to fit, but of course it
is smart ti check it before inserting it to table, because you never
know if there is a bug in MySQL.
At the moment, I pass all my data through htmlentities() before writing to
my database. Is this enough? Should I check each individual columns length
first, or perhaps the overall content length to fit within the maximum
record length?
Wouldn't it be smart to check it and inform the user that data doesn't
fit? That is propably better than just cuttting end off.
I found a function called mysql_escape_string() and have thought of using
it - but other than strip_slashes(), I don't know the reverse (unless
strip_slashes() is the recommended opposite of mysql_escape_string()).


You don't need to reverse mysql_escape_string() when reading. Just be
sure to call it before writing, because if you don't, you propably have
the most common security hole in your application.

Jul 17 '05 #2

"Aggro" <sp**********@yahoo.com> wrote in message
news:4I************@read3.inet.fi...
Randell D. wrote:
I've heard of buffer overflows being used/abused by hackers and believe one method to reduce this from happening is to check the length of my form data before writing it to my MySQL database.


MySQL should cut it for you, if it is too long to fit, but of course it
is smart ti check it before inserting it to table, because you never
know if there is a bug in MySQL.
At the moment, I pass all my data through htmlentities() before writing to my database. Is this enough? Should I check each individual columns length first, or perhaps the overall content length to fit within the maximum
record length?


Wouldn't it be smart to check it and inform the user that data doesn't
fit? That is propably better than just cuttting end off.
I found a function called mysql_escape_string() and have thought of using it - but other than strip_slashes(), I don't know the reverse (unless
strip_slashes() is the recommended opposite of mysql_escape_string()).


You don't need to reverse mysql_escape_string() when reading. Just be
sure to call it before writing, because if you don't, you propably have
the most common security hole in your application.


Thanks... I have javascript checks on the form field lengths - but my
concern was on folks who try to work their around these tests... I know
MySQL will cut the data but (and I'm probably mixing things up here) I have
read of some bugs being abused on an MS environment whereby hackers somehow
managed to write data after the NULL character (which would normally
terminate a string I think).

Hence I wanted to make sure I was writing the data in some environmentally
friendly way...

Thanks though,
randell d.

Jul 17 '05 #3
Randell D. wrote:
Thanks... I have javascript checks on the form field lengths - but my
concern was on folks who try to work their around these tests... I know
MySQL will cut the data but (and I'm probably mixing things up here) I have
read of some bugs being abused on an MS environment whereby hackers somehow
managed to write data after the NULL character (which would normally
terminate a string I think).


If you use javascript to check the strings, you also need to do checking
at the server using php. You need to do this because:
- A lot of people don't have javascript or it isn't enabled, and they
deserver to know the error also, don't they?
- Security rule number 1: Always check that user input is valid.

Note, that you can still use javascript, if you think that you gain more
than lose with it:
- Extra work (implementation and upkeep)
- Some errors with some browsers might appear which won't look good in
the users eyes
+ It might decrease the server load a little, because in normal scenario
server needs to validate data only once (because javascript handles most
user errors without bothering server)
+ It might be more userfriendly in some cases. For example a counter
that counts the characters that user has inputted so that user can see
in real time how much more he/she can write.

Jul 17 '05 #4
Randell D. wrote:
"Aggro" <sp**********@yahoo.com> wrote in message
news:4I************@read3.inet.fi...
Randell D. wrote:
I've heard of buffer overflows being used/abused by hackers and
believe one method to reduce this from happening is to check the
length of my form data before writing it to my MySQL database.


MySQL should cut it for you, if it is too long to fit, but of course
it is smart ti check it before inserting it to table, because you
never know if there is a bug in MySQL.
At the moment, I pass all my data through htmlentities() before
writing to my database. Is this enough? Should I check each
individual columns length first, or perhaps the overall content
length to fit within the maximum record length?


Wouldn't it be smart to check it and inform the user that data
doesn't fit? That is propably better than just cuttting end off.
I found a function called mysql_escape_string() and have thought of
using it - but other than strip_slashes(), I don't know the reverse
(unless strip_slashes() is the recommended opposite of
mysql_escape_string()).


You don't need to reverse mysql_escape_string() when reading. Just be
sure to call it before writing, because if you don't, you propably
have the most common security hole in your application.


Thanks... I have javascript checks on the form field lengths - but my
concern was on folks who try to work their around these tests... I
know MySQL will cut the data but (and I'm probably mixing things up
here) I have read of some bugs being abused on an MS environment
whereby hackers somehow managed to write data after the NULL
character (which would normally terminate a string I think).


Alright, I'm going to jump in just to prevent any further confusion. A
buffer overrun is where you allocate a block of memory of fixed size, and
then read in an arbitrary amount of data which may run past the end of the
block of memory you have allocated (the buffer), overwriting memory that
could potentially be executed. Since you have no memory management abilities
in PHP, this is not anything that you as a user of PHP need to be concerned
about. It has nothing to do with writing past a null character, it's simply
a matter of reading in data of arbitrary size into a block of memory of
fixed size.
Jul 17 '05 #5
On Wed, 07 Jan 2004 05:32:05 GMT, "Randell D."
<re**********************@and.share.com> wrote:
At the moment, I pass all my data through htmlentities() before writing to
my database. Is this enough? Should I check each individual columns length
first, or perhaps the overall content length to fit within the maximum
record length?
You should store your data as raw as possible and only use
htmlentities() when outputting it in an HTML context.
I found a function called mysql_escape_string() and have thought of using
it - but other than strip_slashes(), I don't know the reverse (unless
strip_slashes() is the recommended opposite of mysql_escape_string()).


addslashes() and stripslashes()

--
David ( @priz.co.uk )
Jul 17 '05 #6

"Agelmar" <if**********@comcast.net> wrote in message
news:bt************@ID-30799.news.uni-berlin.de...
Randell D. wrote:
"Aggro" <sp**********@yahoo.com> wrote in message
news:4I************@read3.inet.fi...
Randell D. wrote:

I've heard of buffer overflows being used/abused by hackers and
believe one method to reduce this from happening is to check the
length of my form data before writing it to my MySQL database.

MySQL should cut it for you, if it is too long to fit, but of course
it is smart ti check it before inserting it to table, because you
never know if there is a bug in MySQL.

At the moment, I pass all my data through htmlentities() before
writing to my database. Is this enough? Should I check each
individual columns length first, or perhaps the overall content
length to fit within the maximum record length?

Wouldn't it be smart to check it and inform the user that data
doesn't fit? That is propably better than just cuttting end off.

I found a function called mysql_escape_string() and have thought of
using it - but other than strip_slashes(), I don't know the reverse
(unless strip_slashes() is the recommended opposite of
mysql_escape_string()).

You don't need to reverse mysql_escape_string() when reading. Just be
sure to call it before writing, because if you don't, you propably
have the most common security hole in your application.

Thanks... I have javascript checks on the form field lengths - but my
concern was on folks who try to work their around these tests... I
know MySQL will cut the data but (and I'm probably mixing things up
here) I have read of some bugs being abused on an MS environment
whereby hackers somehow managed to write data after the NULL
character (which would normally terminate a string I think).


Alright, I'm going to jump in just to prevent any further confusion. A
buffer overrun is where you allocate a block of memory of fixed size, and
then read in an arbitrary amount of data which may run past the end of the
block of memory you have allocated (the buffer), overwriting memory that
could potentially be executed. Since you have no memory management

abilities in PHP, this is not anything that you as a user of PHP need to be concerned about. It has nothing to do with writing past a null character, it's simply a matter of reading in data of arbitrary size into a block of memory of
fixed size.


Great - An answer... and one that I can understand too...

Cheers
Randell D.
Jul 17 '05 #7

"David Mackenzie" <me@privacy.net> wrote in message
news:cf********************************@4ax.com...
On Wed, 07 Jan 2004 05:32:05 GMT, "Randell D."
<re**********************@and.share.com> wrote:
At the moment, I pass all my data through htmlentities() before writing tomy database. Is this enough? Should I check each individual columns lengthfirst, or perhaps the overall content length to fit within the maximum
record length?


You should store your data as raw as possible and only use
htmlentities() when outputting it in an HTML context.
I found a function called mysql_escape_string() and have thought of using
it - but other than strip_slashes(), I don't know the reverse (unless
strip_slashes() is the recommended opposite of mysql_escape_string()).


addslashes() and stripslashes()

--
David ( @priz.co.uk )


Thanks...
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Agathe | last post by:
Bonjour, Je souhaite insérer dans une table MySQL des données provenant d'un fichier texte grâce à un script PHP. Mon fichier porte l'extension "txt" et les données sont séparées par des ";'. ...
13
by: Randell D. | last post by:
Folks, I've heard of buffer overflows being used/abused by hackers and believe one method to reduce this from happening is to check the length of my form data before writing it to my MySQL...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
2
by: PHP_Paul | last post by:
Ok, I'm trying to poineer into the wonderful area of PHP/MySQL programming, but I'm having some difficulties. http://www.paulhq.com/php/freepage.html should register, but when anyone fills something...
1
by: Ike | last post by:
Recently, I began using a different MySQL verver (i.e. different machine as well as different version#, going from 4.12a to 4.1.9 max). The following query used to work: select firstname,...
10
by: eholz1 | last post by:
Hello Members, I am setting up a photo website. I have decided to use PHP and MySQL. I can load jpeg files into the table (medium blob, or even longtext) and get the image(s) to display without...
12
by: mantrid | last post by:
Hello Can anyone point me in the right direction for the way to read a text file a line at a time and separate the fields on that line and use them as data in an INSERT to add a record to a mysql...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.