473,396 Members | 1,996 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,396 software developers and data experts.

The right way to Encode html output

What is the best way for that?
I store all Data in the original form in the Database. To prevent output
fields (especially the fields everyone can use) to do bad things like
killing the page-design or even worse attacking my site with javascript
directives i use stuff like
(WebControls.Label)Output.Text = HttpUtility.HtmlEncode(userDefinedData);
and my own functions which allow Line-Breaks and handle links.

But that way seems not to be very safe since it happens that you forget a
htmlEncode and then there is a security risc.
I saw that a "HtmlControls.HtmlGenericControl" (a html label converted to
RunAt Server) has the very useful attributes "InnerText" and "InnerHtml".
InnerHtml works like the text property of the Label WebControl but InnerText
automatically converts all special signs to the html codes. Why isn't there
a thing like "InnerText" in the Label WebControl? Seems very usefull to me
and it's more likely to forget a HtmlEncode before a Label than to use the
wrong property for setting the controls text.

Or maybe is the beste solution to HtmlEncode all user-input before writing
it to the database? On the one side it could be easier and more secure to
focus on the well-formed data in the database but on the other side i think
to care about valid html data is the business of the asp.net apllication,
not of the database.
Nov 17 '05 #1
2 11497
The best thing to do is always to verify any user input before passing it on
to your application - I would do this long before it gets to the database.

As for HtmlEncode, this uses the approach of removing known bad characters.
This is good. Even better, however, is only explicitly allowing known good
characters, which definitionally removes bad characters (both known and
unknown) which you can do using regular expression validators.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows XP
Windows XP Associate Expert
--
"ViperDK" <no************@gmx.net> wrote in message
news:bf*************@news.t-online.com...
What is the best way for that?
I store all Data in the original form in the Database. To prevent output
fields (especially the fields everyone can use) to do bad things like
killing the page-design or even worse attacking my site with javascript
directives i use stuff like
(WebControls.Label)Output.Text = HttpUtility.HtmlEncode(userDefinedData);
and my own functions which allow Line-Breaks and handle links.

But that way seems not to be very safe since it happens that you forget a
htmlEncode and then there is a security risc.
I saw that a "HtmlControls.HtmlGenericControl" (a html label converted to
RunAt Server) has the very useful attributes "InnerText" and "InnerHtml".
InnerHtml works like the text property of the Label WebControl but InnerText automatically converts all special signs to the html codes. Why isn't there a thing like "InnerText" in the Label WebControl? Seems very usefull to me
and it's more likely to forget a HtmlEncode before a Label than to use the
wrong property for setting the controls text.

Or maybe is the beste solution to HtmlEncode all user-input before writing
it to the database? On the one side it could be easier and more secure to
focus on the well-formed data in the database but on the other side i think to care about valid html data is the business of the asp.net apllication,
not of the database.

Nov 17 '05 #2
Chris,

A few problems with this approach:

1. Screening data on the way in is not a sure thing. Your app is not
necessarily the only thing pushing data into the db. Even if it is now, it
might not be in the future. Even if it remains the only client, you can't
stop the dba from doing whatever he/she likes to the data. I'm not
suggesting that validation should not be performed but, rather, that it's
not sufficient for adequate protection against the problems that
HTML-encoding solves.

2. Sometimes you must accept data that could cause display problems in a
given UI. It is the responsibility of the UI tier (or its developer <g>) to
ensure that these problems do not manifest. For example, in an app with
both web and Windows UIs, why should the Windows app deal with potential
html or javascript inclusions in user text? In fact, there might be cases
where such text should be accepted and/or rendered exactly as original
submitted by a user. However, each UI should take care of rendering these
appropriately (don't render, HTML-encode, or leave as-is).

3. HTML-encoding takes care of more than just the security-related issues
involved in rendering user input. Some characters should be mapped to their
HTML representations (e.g.: &, <, >) if they are to be displayed correctly
in the browser. They should not be stored in the database in their HTML
representations since other clients (other app UI, reporting tools, dba)
might just need to read the stored text as well.

Nicole
"Chris Jackson" <ch****@mvps.org> wrote in message
news:Ot**************@tk2msftngp13.phx.gbl...
The best thing to do is always to verify any user input before passing it on to your application - I would do this long before it gets to the database.

As for HtmlEncode, this uses the approach of removing known bad characters. This is good. Even better, however, is only explicitly allowing known good
characters, which definitionally removes bad characters (both known and
unknown) which you can do using regular expression validators.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows XP
Windows XP Associate Expert
--
"ViperDK" <no************@gmx.net> wrote in message
news:bf*************@news.t-online.com...
What is the best way for that?
I store all Data in the original form in the Database. To prevent output
fields (especially the fields everyone can use) to do bad things like
killing the page-design or even worse attacking my site with javascript
directives i use stuff like
(WebControls.Label)Output.Text = HttpUtility.HtmlEncode(userDefinedData); and my own functions which allow Line-Breaks and handle links.

But that way seems not to be very safe since it happens that you forget a htmlEncode and then there is a security risc.
I saw that a "HtmlControls.HtmlGenericControl" (a html label converted to RunAt Server) has the very useful attributes "InnerText" and "InnerHtml". InnerHtml works like the text property of the Label WebControl but

InnerText
automatically converts all special signs to the html codes. Why isn't

there
a thing like "InnerText" in the Label WebControl? Seems very usefull to me and it's more likely to forget a HtmlEncode before a Label than to use the wrong property for setting the controls text.

Or maybe is the beste solution to HtmlEncode all user-input before writing it to the database? On the one side it could be easier and more secure to focus on the well-formed data in the database but on the other side i

think
to care about valid html data is the business of the asp.net apllication, not of the database.


Nov 17 '05 #3

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

Similar topics

0
by: Ramy076 | last post by:
I have a C App which uses openssl for Encoding and decoding which needs to be ported to java. The decoded text does not match between the C App and the java App. The size of the output itself is...
3
by: user | last post by:
hi there has anyone of you writte a function to encode html from like '&' -> '&amp;' and likes to share it with me.. or can anybody give me a hint how to set up something like that. cheers me. ...
4
by: Newbie | last post by:
How would I modify this form to encode *all* the characters in the 'source' textarea to the '%xx' format & place result code into the 'output' textarea? (cross browser compatable) Any help is...
3
by: Peter | last post by:
Hi, I try to make up a javascript string which contains numeric numbers in any positions. For example, I want to make a string: secretcode, where secretcode.charAt(0)==(-21),...
4
by: Darrel | last post by:
How does HTML.encode work? I'm trying to save text in a hidden form field into a SQL DB. The tedt is HTML (from a WYSIWYG editor...X-standard). One problem I have is that stray apostrophe's in...
7
by: sarada7 | last post by:
Hi, Is there a way to encode/decode HTML using C++?? Thanks, Sarada.
5
by: Timothy Madden | last post by:
Hello Is there a function that will allow me to output text written in utf-8 (from db for example) if my document has Content-Type: text/html; charset=ISO-8859-1 I mean htmlspecialchars()...
15
by: glacier | last post by:
I use chinese charactors as an example here. "'\\xc4\\xe3\\xba\\xc3\\xc2\\xf0'" My first question is : what strategy does 'decode' use to tell the way to seperate the words. I mean since s1 is...
1
by: anonymous | last post by:
1 Objective to write little programs to help me learn German. See code after numbered comments. //Thanks in advance for any direction or suggestions. tk 2 Want keyboard answer input, for...
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: 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
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:
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
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,...
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...
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.