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

Preventing JavaScript Injection

Hi,

I have a site where users have been granted a lot of flexibility when it
comes to entries.
They cannot add any code as far as I can see that would harm the server, but
they could add JavaScript code that could be harmful to visitors, (or just
annoying).

So I want to prevent them from running bad scripts on the visitors of their
pages.

Scripts like...
What I was thinking of doing was
1) Send me an email when any user adds an entry with the word "<SCRIPT" in
it
2) Send me an email when any user adds an entry with the word "<EMBED" in it
3) Replace "location.replace(...)" with "/*location.replace*/(...)" to
prevent been redirected to another page on load.

Would point 1) and 2) ensure that I catch all the possible scripts, (or is
there another way of starting a script)?

What other code do you think I should 'monitor'?

Simon
--
http://urlkick.com/
Free URL redirection service. Turns a long URL into a much shorter one.
Mar 28 '06 #1
6 8668
VK

Simon wrote:
Hi,

I have a site where users have been granted a lot of flexibility when it
comes to entries.
They cannot add any code as far as I can see that would harm the server, but
they could add JavaScript code that could be harmful to visitors, (or just
annoying).

So I want to prevent them from running bad scripts on the visitors of their
pages.

Scripts like...
What I was thinking of doing was
1) Send me an email when any user adds an entry with the word "<SCRIPT" in
it
2) Send me an email when any user adds an entry with the word "<EMBED" in it
3) Replace "location.replace(...)" with "/*location.replace*/(...)" to
prevent been redirected to another page on load.

Would point 1) and 2) ensure that I catch all the possible scripts, (or is
there another way of starting a script)?


No they don't - this primitive defence was bypassed endless amount of
times.

See my post in the thread:
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/eb4dd17efe90f7be/9d0548937813dae7>

The only acceptable security you can get by allowing a pre-defined set
of pseudo-tags, rendered into real tags on server side. Like say [b]
for bold, [em] for emphasis etc.

Also see the mentioned in the thread MyShere case at
<http://blog.outer-court.com/archive/2005-10-13-n73.html>

And at <http://namb.la/popular/tech.html> you can read the actual
hacker explanations how did he bypass the defence of the kind you are
thinking about (but thought to be way more "robust").

Mar 28 '06 #2
"VK" <sc**********@yahoo.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...

No they don't - this primitive defence was bypassed endless amount of
times.

See my post in the thread:
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/eb4dd17efe90f7be/9d0548937813dae7>

The only acceptable security you can get by allowing a pre-defined set
of pseudo-tags, rendered into real tags on server side. Like say [b]
for bold, [em] for emphasis etc.

Also see the mentioned in the thread MyShere case at
<http://blog.outer-court.com/archive/2005-10-13-n73.html>

And at <http://namb.la/popular/tech.html> you can read the actual
hacker explanations how did he bypass the defence of the kind you are
thinking about (but thought to be way more "robust").


Thanks for that, quite an interesting read indeed.

Thankfully, (sic), we are not as big as MySpace or MyShere, but it won't
hurt to follow some/all of their procedures.

In the meantime, I will also flag any entries that has more than 5 or 6 "{",
"(", ")" or "}" that might help a little.

Simon
--
http://urlkick.com/
Free URL redirection service. Turns a long URL into a much shorter one.
Mar 28 '06 #3
"Simon" <sp********@example.com> writes:
In the meantime, I will also flag any entries that has more than 5 or 6 "{",
"(", ")" or "}" that might help a little.


I like to make parenthesised comments, so I would be sure to hit that
limit fast :)

A whitelist is still the only way to be sure. If you publish the rules
you check for (as you just did :), it's fairly simple for a proficient
Javascript programmer to avoid it.

Inject this!

<div style='background-image:url(javascript:document.write("</body>"););">Sorry!</div>

<div onmouseover='location.href="http://mysluttysexsite.com/";'>
my entire message that looks plausible
</div>

They have respectively four and zero parentheses.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Mar 28 '06 #4
VK

Lasse Reichstein Nielsen wrote:
<snip>
<div onmouseover='location.href="http://mysluttysexsite.com/";'>
my entire message that looks plausible
</div>

They have respectively four and zero parentheses.


Hey, the server was down! :-( :-D

:-|
The OP's question is really out of regulations of c.l.j. That is the
famous "acceptable potential hackers" dilemma. Any system is vulnerable
by default to N amount of experienced hackers (a system vulnerable to 0
hackers cannot exist by thermodynamic laws). More robust the system -
lesser the N number. As hackers as human being cannot be fractional
(0.5 hacker cannot be), the system lims to Super System - Super Hacker
solution. So eventually the equation solves to one absolutely
unbreakable system and the only one person able to hack it. If such
person appears to be a law-obeying member of the socially the equation
will be solved by non-mathematical methods. Otherwise your only
solution is to decide what N% is acceptable for your current situation.
Every 2nd? No more than half? 1% of your visitors? For the last the
proposed measures are good enough.

P.S. Super Hacker theory is taken from the black hackers' bible, which
I never saw of course. Just rumors.

Mar 28 '06 #5
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:4q**********@hotpop.com...
"Simon" <sp********@example.com> writes:
In the meantime, I will also flag any entries that has more than 5 or 6
"{",
"(", ")" or "}" that might help a little.


I like to make parenthesised comments, so I would be sure to hit that
limit fast :)

A whitelist is still the only way to be sure. If you publish the rules
you check for (as you just did :), it's fairly simple for a proficient
Javascript programmer to avoid it.

Inject this!

<div
style='background-image:url(javascript:document.write("</body>"););">Sorry!</div>

<div onmouseover='location.href="http://mysluttysexsite.com/";'>
my entire message that looks plausible
</div>

They have respectively four and zero parentheses.

I am not saying that my system would be 100% full proof.
But currently I am rejecting any "javascript", (and thanks to VK I am now
rejecting broken Javascripts, (java\nscript).

I am also removing any onWhatever=....

And I am also removing all the "location.replace" and "location.href".

Over and above that I still think that monitoring entries that contain
suspicious chars, ({, (, ), }) will possibly help me in noticing possible
illegal patterns.

Further more I flag a user that "previews" messages with weird code, because
that usually tells me that they are indeed trying to do something strange.

Simon
--
http://urlkick.com/
Free URL redirection service. Turns a long URL into a much shorter one.
Mar 29 '06 #6
"VK" <sc**********@yahoo.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...

Lasse Reichstein Nielsen wrote:
<snip>
<div onmouseover='location.href="http://mysluttysexsite.com/";'>
my entire message that looks plausible
</div>

They have respectively four and zero parentheses.


Hey, the server was down! :-( :-D

:-|
The OP's question is really out of regulations of c.l.j. That is the
famous "acceptable potential hackers" dilemma. Any system is vulnerable
by default to N amount of experienced hackers (a system vulnerable to 0
hackers cannot exist by thermodynamic laws). More robust the system -
lesser the N number. As hackers as human being cannot be fractional
(0.5 hacker cannot be), the system lims to Super System - Super Hacker
solution. So eventually the equation solves to one absolutely
unbreakable system and the only one person able to hack it. If such
person appears to be a law-obeying member of the socially the equation
will be solved by non-mathematical methods. Otherwise your only
solution is to decide what N% is acceptable for your current situation.
Every 2nd? No more than half? 1% of your visitors? For the last the
proposed measures are good enough.

P.S. Super Hacker theory is taken from the black hackers' bible, which
I never saw of course. Just rumors.


I cannot agree more with you.
My code will not be 100% safe, it never will be safe as far as I can see it.

I also agree that pseudo-tags would be a better option by far.
Unfortunately other blog hosts do not offer such a system and to keep our
users happy, (and to keep making money :)), we have to bend some blatant
rules.
One of those rules is to allow html code.

Simon

--
http://urlkick.com/
Free URL redirection service. Turns a long URL into a much shorter one.
Mar 29 '06 #7

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

Similar topics

2
by: Martin Lucas-Smith | last post by:
Can anyone provide any suggestions/URLs for best-practice approaches to preventing SQL injection? There seems to be little on the web that I can find on this. Martin Lucas-Smith ...
7
by: Robb Meade | last post by:
Hi all, A recent project that I had finished and went live with no apparant problems. My client received an email from a user who mentioned that by accident they had been typing (over the...
7
by: aaj | last post by:
Hi all We had a small problem when an ASP web page had a missing 'where' statement and updated all the records in the table. Luckily we could retrieve all the data from the backups. How do...
5
by: www.douglassdavis.com | last post by:
I have an idea for preventing sql injection attacks, however it would have to be implemented by the database vendor. Let me know if I am on the right track, this totally off base, or already...
10
by: bregent | last post by:
I've seen plenty of articles and utilities for preventing form injections for ASP.NET, but not too much for classic ASP. Are there any good input validation scripts that you use to avoid form...
4
by: Kevin Audleman | last post by:
My site has come under attack from sql injections. I thought I had things handled by replacing all single quotes with two single quotes, aka Replace(inputString, "'", "''") Alas, clever...
13
by: RJ_32 | last post by:
looking here: http://www.devarticles.com/c/a/PHP/Getting-Intimate-With-PHPs-Mail-Function/2/ it says that I have to be careful about what I send to the sendmail process via popen(). Does that...
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
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
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
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.