473,395 Members | 1,616 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.

Security in data handling

For some Open Source (GPL licensed) programs that I am
working on, I am trying to create some PHP routines for data
handling with good security.

The routines I have created for this are given below and I
would like these reviewed for security problems or other
problems that they may have.

My requirements are as follows:

1) The data needs to be made safe for placing into a SQL
statement for use with a Mysql database as well as being
safe to take out of the database and sent to a web page.

2) All work in making the data safe should be done on entry
to the database. Display of query listings is time critical
while data entry is not. Consequently, I prefer for
htmlspecialchars() or similar operations to be done before
data is entered into the database, not after.

3) Some fields have the requirement that they need to allow
the user to include a limited subset of simple html tags.
The user will not be allowed to use any parameters within
these tags. (Note. Code below only shows a few tags. With
final code I will add use of tables and lists etc)

4) Being Free Open Source it must be easily implemented on a
wide range of environments

My initial attempt at making routines to handle this is as
follows:

//The manual is ambiguous and nobody seems to know exactly
// what magic_quotes_runtime does so it is better to set it
// to off.
set_magic_quotes_runtime(0);

// Internal function used to start making the data safe.
function start_make_safe($in_string){
$in_string = isset($in_string) ? trim($in_string) : "";
// Allow this to work regardless of the setting of
// magic_quotes_gps
if (get_magic_quotes_gpc()){
$in_string = stripslashes($in_string);
}
// Stop tags being disguised with \0
return str_replace("\0", '', $in_string);
}

// Internal function used as part of making data safe.
function more_make_safe($in_string){
// Stop tricks of the type "<sc<script>ipt>"
// which if <script> was removed would still leave <script>
while ($in_string != strip_tags($in_string)){
$in_string = strip_tags($in_string);
}
//Replace new lines or linefeeds with <br>
global $linefeeds;
if ($linefeeds == "Y"){
$in_string =
preg_replace("/\r\n|\n\r|\n|\r/","<br>",$in_string);
}
// Allow this to be used in both PHP 5 and most of PHP 4.
return (function_exists(mysql_real_escape_string) ?
mysql_real_escape_string($in_string)
: mysql_escape_string($in_string));

//Note: If LIKE, GRANT, or REVOKE is used in the mysql
// statement then we need to also escape '%' and '_'
// as these are used for wildcards.
}
function make_html_safe($in_string){
$in_string = start_make_safe($in_string);
//change simple tags <br> </b> etc to {{br}} {{/b}} etc
$allowed =
'/<(\/?br)>|<(\/?p)>|<(\/?b)>|<(\/?i)>|<(\/?u)>|<(\/?h[1-5])>/i';
$in_string =
preg_replace($allowed, '{{$1$2$3$4$5$6}}', $in_string);
$in_string = more_make_safe($in_string);
$in_string = htmlentities($in_string, ENT_QUOTES);
//replace simple tags
$allowed =
'/{{(\/?br)}}|{{(\/?p)}}|{{(\/?b)}}|{{(\/?i)}}|{{(\/?u)}}|{{(\/?h[1-5
])}}/i';
return preg_replace($allowed, '<$1$2$3$4$5$6>', $in_string);
}

function make_URL_safe($in_string){
$in_string = more_make_safe(start_make_safe($in_string));
// Try to stop people using any tag or tag completion.
// Note that an '=' and '&' is allowed within the URL.
// Can't use htmlentities as it replaces the '&' symbol.
return (preg_replace("/>|</", "", $in_string));
}

function make_safe($in_string){
$in_string = more_make_safe(start_make_safe($in_string));
return htmlentities($in_string, ENT_QUOTES);
}

// Use of the above routines will be through the use
// of calls similar to the following:

$sql = sprintf ('INSERT INTO mytable (name, userwww,
phone, longdesc, useremail) values
("%s", "%s", "%s", "%s", "%s")'
, make_safe($_POST['name'])
, make_URL_safe($_POST['user_URL'])
, make_safe($_POST['phone_num'])
, make_html_safe($_POST['long_descript'])
, make_safe($_POST['user_email']));

$query = 'SELECT * FROM mytable WHERE name = "'
. make_safe($_POST['name']) . '"';
I have made some simple checks to ensure that the above
functions appear to work.

I would like the above reviewed for any security problems
that they may have or any other problems that it may cause
when placed out in the wild. Any suggestions welcome!

Thank you for your help

Ken Dawber

Jul 17 '05 #1
0 1406

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

Similar topics

3
by: Derrald | last post by:
I have had two clients recently ask me about shopping carts. I have also been using PHP on a more regular basis and thought I could find an open source shopping cart such as oscommerce. It seems...
9
by: chris | last post by:
im just starting to learn how sessions work and how to use them my question is if im geting a username and password from a visitor is it secure to store that information in a session variable or...
3
by: Ryan N. | last post by:
Hello, I saw a brief blurb on this somewhere and am unable to recall where... In the context of Security, what are some best practices for handling -storing, locating, retrieving- database OLEDB...
7
by: PaulThomas | last post by:
I am fighting with XP-Pro and VS.Net trying to allow some of the pages in my application to be accessable by 'all' I am using <authentication mode="Forms" /> and if I Login - everything works...
2
by: John J. Hughes II | last post by:
I am having some major problem with maintaining security for my windows application to the SQL. Currently my application access the SQL using System.Data.SqlClient and all forms use stored...
9
by: Marcus | last post by:
Hello, Currently all of my php pages use SSL, not just my initial login. Originally I thought this would be more secure, but after thinking about things and looking at sites like Amazon and...
8
by: adserte | last post by:
I have a security related question. I was wondering how i can set up security so that for a table: a user can read all data in the table but only update and delete their own data (there is a...
2
by: Aggelos | last post by:
Hello, I can't get my head around form mail scripts and people injecting extra code in there. I don't know if they actually achieve anything or not. I am using a script from Web4Future Easiest...
15
by: himilecyclist | last post by:
My State government organization has written a PHP/MySQL application which has been in production for about 6 months and has been highly successful. We are now embarking on a similar database...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.