473,805 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoiding an Infinite Loop in Arbitrary eval(user_code)

Hello all,

I want to have a user able to eval code in a text box. However, if he
accidentally types "while(1) { i=0; }" and hits "run", I also want him
to be able to hit a stop button such that his browser does not go into
an infinite, soul-crushing, interface-locking loop. The stop button
would not need to be instantly responsive, but of course the more
responsive the better.

Short of writing a javascript-in-javascript interpreter, is there any
way to do so? Does Caja make this sort of thing possible? Will I need
to restrict myself to Gears+threads to do this?

Thanks for any help,
Bill Mill
bi*******@gmail .com
Jun 27 '08 #1
12 3492
Bill Mill schreef:
Hello all,

I want to have a user able to eval code in a text box. However, if he
accidentally types "while(1) { i=0; }" and hits "run", I also want him
to be able to hit a stop button such that his browser does not go into
an infinite, soul-crushing, interface-locking loop. The stop button
would not need to be instantly responsive, but of course the more
responsive the better.
Hi Bill,

My Firefox notices long running scripts and offers to abort them after a
while.
What browser are you using?

Regards,
Erwin Moller
>
Short of writing a javascript-in-javascript interpreter, is there any
way to do so? Does Caja make this sort of thing possible? Will I need
to restrict myself to Gears+threads to do this?

Thanks for any help,
Bill Mill
bi*******@gmail .com
Jun 27 '08 #2
On 23 Apr, 07:29, Erwin Moller
<Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c om
Hi Bill,

My Firefox notices long running scripts and offers to abort them after a
while.
What browser are you using?
Surely what browser the OP is using has no bearing on this. The
question is what browser will the arbitrary user be using?
Jun 27 '08 #3
Bill Mill wrote:
I want to have a user able to eval code in a text box. However, if he
accidentally types "while(1) { i=0; }" and hits "run", I also want him
to be able to hit a stop button such that his browser does not go into
an infinite, soul-crushing, interface-locking loop. The stop button
would not need to be instantly responsive, but of course the more
responsive the better.

Short of writing a javascript-in-javascript interpreter, is there any
way to do so?
No. ECMAScript implementations so far are single-threaded, and there is yet
an algorithm to be written for a universal solution of the Halting Problem.

http://en.wikipedia.org/wiki/Halting_problem

You will have to rely on the user's user agent to recognize a not-responding
script, and provide the user with such a dialog window, as Gecko-based UAs
(e.g. Mozilla Firefox) do.
Does Caja make this sort of thing possible?
I don't think so:

http://code.google.com/p/google-caja/wiki/AttackVectors

Please be more verbose next time.
Will I need to restrict myself to Gears+threads to do this?
Never heard of those.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jun 27 '08 #4
On Apr 23, 1:07*pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
Bill Mill wrote:
I want to have a user able to eval code in a text box. However, if he
accidentally types "while(1) { i=0; }" and hits "run", I also want him
to be able to hit a stop button such that his browser does not go into
an infinite, soul-crushing, interface-locking loop. The stop button
would not need to be instantly responsive, but of course the more
responsive the better.
Short of writing a javascript-in-javascript interpreter, is there any
way to do so?

No. *ECMAScript implementations so far are single-threaded, and there isyet
an algorithm to be written for a universal solution of the Halting Problem..

http://en.wikipedia.org/wiki/Halting_problem
Thanks, I know just what that is, and I'm not asking for a solution to
it. Allowing a user to stop an eval is not equivalent to determining
prior to the eval whether or not it will ever complete.
>
You will have to rely on the user's user agent to recognize a not-responding
script, and provide the user with such a dialog window, as Gecko-based UAs
(e.g. Mozilla Firefox) do.
I can't rely on this, since I would like to allow the user to write
scripts that take a while to run. Thus, he's likely to disable this
dialog for the page.
>
Does Caja make this sort of thing possible?

I don't think so:

http://code.google.com/p/google-caja/wiki/AttackVectors
How is that relevant to what I asked? I've read the Caja website, as
well as the PDF describing the system, and I'm still not clear on
whether it can or not.
>
Please be more verbose next time.
what more would you like to know?
>
Will I need to restrict myself to Gears+threads to do this?

Never heard of those.
I meant that I might be able to use Google Gears' threads to achieve
what I'm looking for.

-Bill Mill
Jun 27 '08 #5
On Apr 23, 1:57*pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
Bill Mill wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Bill Mill wrote:
I want to have a user able to eval code in a text box. However, if he
accidentally types "while(1) { i=0; }" and hits "run", I also want him
to be able to hit a stop button such that his browser does not go into
an infinite, soul-crushing, interface-locking loop. The stop button
would not need to be instantly responsive, but of course the more
responsive the better.
Short of writing a javascript-in-javascript interpreter, is there any
way to do so?
No. *ECMAScript implementations so far are single-threaded, and thereis yet
an algorithm to be written for a universal solution of the Halting Problem.
>http://en.wikipedia.org/wiki/Halting_problem
Thanks, I know just what that is, and I'm not asking for a solution to
it. Allowing a user to stop an eval is not equivalent to determining
prior to the eval whether or not it will ever complete.

But you will need a threaded implementation
Or a way to bounce out of the eval every x seconds/operations to check
for user input. May I take it that you are saying that once an eval is
started, it's impossible to break escape with a setTimeout or such?
This is the way it seemed to me but I am no expert so I thought I
would ask here.

I could, for example, solve this problem by writing a javascript
interpreter in javascript, then executing the user's code in my
interpreter, which checks for a user interrupt before each operation
and stops if there has been one. (right?) So this problem is not
impossible, but it is a big pain. It also does not intrinsically
require either threads or a solution to the halting problem.
ISTM the developers of Google Caja have not even realized that code as you
suggest would qualify as an attack vector, so it would seem unlikely that
they have succeeded in implementing a counter-measure against it in their code.
Whether or not they have considered it as an attack vector is
irrelevant to the question of whether I could use their code to eval
my user's code in such a way that I could stop it.

-Bill
Jun 27 '08 #6
Bill Mill wrote:
On Apr 23, 1:57 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.dewrote:
>Bill Mill wrote:
>>[...] Thomas 'PointedEars' Lahn [...] wrote:
Bill Mill wrote:
I want to have a user able to eval code in a text box. However,
if he accidentally types "while(1) { i=0; }" and hits "run", I
also want him to be able to hit a stop button such that his
browser does not go into an infinite, soul-crushing,
interface-locking loop. The stop button would not need to be
instantly responsive, but of course the more responsive the
better. Short of writing a javascript-in-javascript interpreter,
is there any way to do so?
No. ECMAScript implementations so far are single-threaded, and
there is yet an algorithm to be written for a universal solution of
the Halting Problem. http://en.wikipedia.org/wiki/Halting_problem
Thanks, I know just what that is, and I'm not asking for a solution
to it. Allowing a user to stop an eval is not equivalent to
determining prior to the eval whether or not it will ever complete.
But you will need a threaded implementation

Or a way to bounce out of the eval every x seconds/operations to check
for user input. May I take it that you are saying that once an eval is
started, it's impossible to break escape with a setTimeout or such?
(Probably you meant "eval" instead of "escape".) Yes, that is correct.
This is the way it seemed to me but I am no expert so I thought I would
ask here.

I could, for example, solve this problem by writing a javascript
interpreter in javascript, then executing the user's code in my
interpreter, which checks for a user interrupt before each
operation and stops if there has been one. (right?)
No. For your "javascript " interpreter written in "javascript ", there are
not operations but statements to consider (for example, the `while'
statement). And your "javascript " interpreter would run single-threaded,
in a single-threaded runtime environment:

User agent
|
'- built-in ECMAScript-compliant script engine
|
'- your "javascript " interpreter
|
'- the user's code

AFAICS, the only possibility that remains is that the user agent recognizes
that the ECMAScript-compliant engine has not returned a status result within
a defined interval and then presents the user with a choice to stop
execution, i.e. kill the engine's thread. And ISTM that is exactly what
Mozilla/5.0 does.
So this problem is not impossible,
Correct, the solution to it is.
but it is a big pain. It also does not intrinsically require either
threads or a solution to the halting problem.
I don't think so.
>ISTM the developers of Google Caja have not even realized that code as
you suggest would qualify as an attack vector, so it would seem
unlikely that they have succeeded in implementing a counter-measure
against it in their code.

Whether or not they have considered it as an attack vector is irrelevant
to the question of whether I could use their code to eval my user's code
in such a way that I could stop it.
(Isn't it a bit presumptuous of you to make such sincere statements but
calling yourself a non-expert?)

Their code would run in a single-threaded environment as well. Unless they
have found a counter-measure to the attack vector of blocking code, it is
unlikely that their code is going to solve your problem. And for that they
would need to have recognized your problem as being one first.
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Jun 27 '08 #7
Bill Mill <bi*******@gmai l.comwrites:
Or a way to bounce out of the eval every x seconds/operations to check
for user input. May I take it that you are saying that once an eval is
started, it's impossible to break escape with a setTimeout or such?
setTimeout & similar functions don't allow that, because of the
single-threadedness. By the way, it's not a rule that javascript
implementations themselves should be single-threaded, but the core specs
don't specify any mechanisms that would make multi-threading
manageable - IOW it may be possible to create a multi-threaded
javascript implementation, but it would need at least specify the
lower-level implications of multi-threading, and provide some
locking/synchronization primitives in addition to the standard.
I could, for example, solve this problem by writing a javascript
interpreter in javascript, then executing the user's code in my
interpreter, which checks for a user interrupt before each operation
and stops if there has been one. (right?)
Yes you could. If your interpreter is fine-grained enough it's perfectly
possible to halt it after some amount of time. A JS interpreter
implemented like that in javascript would probably be pretty slow,
though. You'd also have to take care to provide interruptable variants
of of all host-provided functions that could take a long time or
possibly not return at all (things like alert(), or a synchronized
XMLHttpRequest for example).
So this problem is not impossible, but it is a big pain. It also does
not intrinsically require either threads or a solution to the halting
problem.
Correct.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #8
>
So this problem is not impossible, but it is a big pain. It also does
not intrinsically require either threads or a solution to the halting
problem.

Correct.

--
Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
Thank you very much Joost.

-Bill
Jun 27 '08 #9
In comp.lang.javas cript message <48************ ***********@new s.xs4all.n
l>, Wed, 23 Apr 2008 09:29:48, Erwin Moller <Since_humans_r ead_this_I_am
_s************* **@spamyourself .composted:
>
My Firefox notices long running scripts and offers to abort them after
a while.
What browser are you using?
My Opera does not, which was useful today when I had code to check
several Easter algorithms for 5700000 years, at about 2000 years per
second.

My IE6 would allow longer and longer intervals between successive
warnings, but my IE7 warns every 31000 years or so - tiresome.

It would be nice to have a list of whether/how that can be changed for
various browsers. Obviously it ought not (by default) to be possible to
do it be code, but it would be good for a programmer to be able to ask a
user to make the change.

It would also be nice if the warning box had a control to disable the
feature for the current action or page.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demo n.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jun 27 '08 #10

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

Similar topics

21
1770
by: André | last post by:
Short version of what I am looking for: Given a class "public_class" which is instantiated a few times e.g. a = public_class() b = public_class() c = public_class() I would like to find out the name of the instances so that I could create a list of them e.g.
29
2149
by: Steven Bethard | last post by:
Fuzzyman wrote: > Cameron Laird wrote: > > >>This is a serious issue. >> >>It's also one that brings Tcl, mentioned several >>times in this thread, back into focus. Tcl presents >>the notion of "safe interpreter", that is, a sub- >>ordinate virtual machine which can interpret only
2
3207
by: André | last post by:
Short description: Using a wxPython based app, I would like to take a python script in an editor window, invoke the python interpreter and use another window as stdin/stdout/stderr. Based on what I've read so far, I've figured that I need to do something like: f_in, f_out = popen4("python -u script.py") where I have extracted the text from the editor and put it in "script.py". (However, see below).
3
1095
by: Steve Swift | last post by:
In this newsgroup (and not others on the mailserver I use) I find that most threads start with a "Re:" article, referfing to something that I never see. I'm reasonably competent with NNTP protocol (having used it for about 20 years) which is why I mention that other groups on the same server are not affected. I think the problem in this group is recent (about 3 months) but I suffer memory problems, so I may be wrong about that. My...
0
10607
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...
0
10359
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10364
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
10104
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
9182
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, and deployment—without 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
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
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

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.