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

When to use HTML encode and when not to?

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 the HTML text are throwing
a SQL error. Html.encode doesn't seem to do anything with these, eh?

Secondly, does HTMLencode also encode already encoded items?

For instance, if I have text in my editor that contains an HTML entity like
< and then I run THAT through HTML.encode, will that decode along with
the html when I use HTML.decode?

-Darrel
Nov 18 '05 #1
4 7542
"Darrel" <no*****@nospam.com> wrote in
news:et**************@TK2MSFTNGP10.phx.gbl:
How does HTML.encode work?
It converts reserved HTML characters into their HTML safe equivalent.
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).
Don't HTML encode the text.
One problem I have is that stray apostrophe's in the HTML text are
throwing a SQL error. Html.encode doesn't seem to do anything with
these, eh?
Nope - you can use SQL parameters to avoid this error, or you can write
your own SQL delimiting function (just double up all single quotes).
Secondly, does HTMLencode also encode already encoded items?
Yes, but since the first pass would have converted everything into HTML
safe equivalents, the second pass does nothing.
For instance, if I have text in my editor that contains an HTML entity
like &lt; and then I run THAT through HTML.encode, will that decode
along with the html when I use HTML.decode?


If you run &lt; through HTML encode you'll get &lt; as there are no
reserved characters to encode.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 18 '05 #2
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"Darrel" <no*****@nospam.com> wrote in
news:et**************@TK2MSFTNGP10.phx.gbl: <snip>
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).


Don't HTML encode the text.


Sorry, but that's simply not the case. In general, all text that is not
hard-coded at design-time should be HTML-encoded. If it isn't, the text
could end up altering the page in such a way as to change its behaviour.
This isn't simply a matter of ensuring expected functionality. It's also a
security issue due to the potential for script and HTML injection.

That said, the built-in ASP.NET controls will perform some HTML-encoding for
you (e.g.: in a multi-line textbox that will render as a textarea element),
but not all that is necessary, so you should always ensure that your code
performs the encoding if the framework code doesn't.

Secondly, does HTMLencode also encode already encoded items?


Yes, but since the first pass would have converted everything into HTML
safe equivalents, the second pass does nothing.


Nope. It will re-encode, as in the example below.
For instance, if I have text in my editor that contains an HTML entity
like &lt; and then I run THAT through HTML.encode, will that decode
along with the html when I use HTML.decode?


If you run &lt; through HTML encode you'll get &lt; as there are no
reserved characters to encode.


The ampersand is a reserved chacter. If you HTML-encode "&lt;", you'll get
"&amp;lt;".



--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 18 '05 #3
Please google sql injection attacks. It sounds like your app is vulnerable
to them. This doesn't answer your question, but it looks like other people
already have.
Nov 18 '05 #4
Server.HTMLEncode(string) encodes the given string as HTML. So, for example,
if you use Server.HtmlEncode("<!--some text -->"), it encodes it as
"&LT;!--some text--&GT;" so that when it appears in an HTML document, it
appears as "<!--some text-->". This is because some text characters are
treated differently by HTML documents, since HTML documents are pure text.
The example, above, if not Html-Encoded, would not appear in the document at
all, as the angle brackets and other symbols create an HTML comment. So, as
far as Server.HtmlEncode, and when to use it, use it when displaying text in
HTML.

The single quote issue has nothing to do with HTML. It has to do with the
SQL language. The single quote is a text delimiter in SQL. To escape it,
double it. Example:

SELECT * FROM MyTable WHERE LastName = 'O''Malley'

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
"Darrel" <no*****@nospam.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
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 the HTML text are throwing a SQL error. Html.encode doesn't seem to do anything with these, eh?

Secondly, does HTMLencode also encode already encoded items?

For instance, if I have text in my editor that contains an HTML entity like &lt; and then I run THAT through HTML.encode, will that decode along with
the html when I use HTML.decode?

-Darrel

Nov 18 '05 #5

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

Similar topics

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...
2
by: OtisUsenet | last post by:
Hello, I am trying to call Javascript from FORM's onSubmit, and return false, so the form is not actually submitted. Normally I can just add 'return false;' to onSubmit to accomplish this, like...
0
by: tkcheng | last post by:
Helllo, For security reason, we are changing our form submission coding with HTML encode on all the text fields to block the SQL injection. However, we encounter a problem on double byte...
2
by: ViperDK | last post by:
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...
7
by: sarada7 | last post by:
Hi, Is there a way to encode/decode HTML using C++?? Thanks, Sarada.
10
by: pak.andrei | last post by:
Here is my script: from mechanize import * from BeautifulSoup import * import StringIO b = Browser() f = b.open("http://www.translate.ru/text.asp?lang=ru") b.select_form(nr=0) b = "hello...
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()...
4
by: Amie | last post by:
Afternoon all. Just want to know how to create html tables using a for loop. I need to display 34 html tables, so I figured a for loop will do. Please show me an example of how to do that. Also...
4
sanjib65
by: sanjib65 | last post by:
Whenever I take user's input through TextBox or anything else, it's good practice to use Html.Encode(TextBox1.Text) for the security purpose. But is it neccessary now as ASP.NET 2.0 has strengthened...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.