473,795 Members | 3,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What chars are considered safe?

I was just writing a sanitisation route for a bit of user input. The data is
an English text description of a product, and will go into a DB, then back
out to other user's browsers.

As per normal practise, I was working on the basis of leaving in all
characters that I considered safe and stripping out everything else. This
led me to think of what characters are actually safe, given that the user
will want to be able to use at least basic punctuation, currency symbols
and so on. Avoiding < and > seemed obvious, but most other things have a
use I think.

My current line looks like this:

$data = preg_replace( '/[^\s\w\d@"\'()[]{}:#~!$%&*_-+.,]/', "", $data );

(Note that's a list of chars that are *not* to be replaced.) Are any of
these dangerous? Or have I left out some that are harmless and should be in
there?

--
The email address used to post is a spam pit. Contact me at
http://www.derekfountain.org : <a
href="http://www.derekfounta in.org/">Derek Fountain</a>
Jul 17 '05
11 1728
"Derek Fountain" <no****@example .com> wrote in message
news:42******** *************** @per-qv1-newsreader-01.iinet.net.au ...

: I know to leave < and > out to prevent XSS. I'm not sure about the entity
: constructors & and ;. I'm tempted to leave the single quote out because it
: is too useful in SQL injection. I'm suspicious of backticks because of
: command injection - that's from my Perl background, although I'm not sure
: if that's justified in PHP. I have a natural urge to surpress anything
that
: might be used in scripting, like #, !, and slashes, but again, not for a
: reason I actually claim to understand.
:
: Perhaps I'm going about this the wrong way? What do other PHP coders do
with
: a text value before considering it "safe" to store and relay back to other
: user's browsers?

Well, for starters *anything* that I insert into a web page using php ALWAYS
goes through htmlentities() first, which entitity escapes &quot; &apos; &gt;
&lt; etc - otherwise you'll be making invalid html.

You might want to clean up stuff that people input too - there's a good
chance that someone putting
<script language='javas cript'...
or
<?php
isn't necessarily entering something you'd like to display...

Matt
Jul 17 '05 #11
Michael Fesser wrote:
Every input/output operation of (string) data may need some special
escaping/encoding applied before to be safe, dependent on the target
media. In your case I would simply do it this way:

1) Every user-submitted string data is run through stripslashes() if
magic quotes are enabled (check with get_magic_quote s_gpc()). This way
you get the raw data, don't have to rely on some obscure configuration
setting and can apply a proper escaping yourself whenever necessary.

2) Before storing the data into the DB all the strings are run through
mysql_real_esca pe_string() to escape certain chars (like single quotes)
which might cause trouble for the DB.

3) When printing the data out again to an HTML page htmlspecialchar s()
is called on all strings, which converts some chars that have a special
meaning in HTML (<, >, &, ") to named entity references.

That's all. There's no need to take special care of some particular
chars, the mentioned functions above take care of problems like SQL
injection and XSS.


This makes a lot of sense, thanks. One more question though. Assume I take
what the user has given me and do the DB escaping before I put it in the
database. I then ensure that whenever this string is returned to a browser
I pass it through htmlentities() first. But... what about when I want to
return the string to the user so they can edit it? The user will want to
see the quotes and angle brackets, etc., not the escaped versions of those
characters. Is it safe to hand the string back in a <textarea> or similar
without escaping it?

I experimented with Mozilla, and it seems to do the right thing. If I put
<script>alert(' hello');<script > into my DB then send it out into a textarea
the browser doesn't run the script, but just offers it for editing. Is that
dependable? It would appear to be a browser feature, and not something PHP
has any control over.

--
The email address used to post is a spam pit. Contact me at
http://www.derekfountain.org : <a
href="http://www.derekfounta in.org/">Derek Fountain</a>
Jul 17 '05 #12

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

Similar topics

9
1845
by: Sasha | last post by:
Hi, I am extending standard IEnumerator, and I was just wondering what is the best way to make enumarator safe? What do I mean by safe? Detect deletes and all... My idea is to have private Guid state field in the collection, and every time something is inserted or deleted from the collection, I will just change the guid. Enumerator will just have to compare the guid received in the begging to the current one. If they are different, the...
1
403
by: Ofir | last post by:
Hellow, I have a program that draws lines,text,images on a graphics object, from a Thread, using GDI+. are GDI+ considered to be thread safe, or i must invoke my draw method to the main thraed, and draw from there. thanks,
3
5021
by: Dave Crypto | last post by:
Hi There, SUMMARY: I need to know what the actual maximum date limit possible on a row of a MYSQL database. MORE DETAILS: For example, does a MYSQL database only allow 4032 bytes of data to be
669
26235
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
1
1598
by: chandy | last post by:
I've been working with web technologies for ten years and in all that time a safe urlencoding for a space has always been %20 on every platform I have ever used. Now I am using asp.net and it's encoding a space as a +. What on earth is this? Since when was a plus considered a safe alternative to a space for transmission in a url? Heck there's even an encoding for a plus sign, which shows just how un-safe this is! So, rant over, is...
10
3732
by: _mario.lat | last post by:
hallo, what does it means "the function is not thread-safe"? thak you in advance, Mario.
3
10583
by: Kevin Blount | last post by:
I'm putting a radG:GridTemplateColumn together (which is probably irelevant), and within it I'm using a Label, as so: <asp:Label ID="defaultDescription" runat="server" Text='<%# Eval("description") %>'></asp:Label> Fo this Label, I'd like to only show the first 50 chars of the "description", but I've no idea how to change the Eval to do this.. is there a way?
0
147
by: Maric Michaud | last post by:
Le Monday 16 June 2008 18:58:06 Ethan Furman, vous avez crit: As Larry Bates said the python way is to use str.join, but I'd do it with a genexp for memory saving, and a set to get O(1) test of presence. to_remove = set('chars') ''.join(e for in string_ if e not in to_remove) Note that this one will hardly be defeated by other idioms in python (even regexp).
5
13500
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger will show the name not only the value) cons: enum can not be forward declared which makes all error codes
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.