Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17115

Re: eval - how to

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!news.karotte.org!uucp.gnuu.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <2836184.TlQAl5i4AK@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Sat, 10 Nov 2012 11:15:39 +0100
User-Agent KNode/4.4.11
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: eval - how to
Newsgroups comp.lang.javascript
References <k7iqg9$n49$1@speranza.aioe.org> <ad4ffcf9-f94f-44c4-b425-7310b78e92bb@m13g2000vbd.googlegroups.com> <k7jsi7$h6a$1@speranza.aioe.org>
Followup-To comp.lang.javascript
MIME-Version 1.0
Lines 110
NNTP-Posting-Date 10 Nov 2012 11:15:39 CET
NNTP-Posting-Host a3a21096.newsspool2.arcor-online.net
X-Trace DXC=59aQ0Y1nfbkEB;5>eE0T7mA9EHlD;3Ycb4Fo<]lROoRa8kF<OcfhCOk9[GHo:1OE=iDZm8W4\YJNlJ`:FmdeL>K`l>hCXB2:Z4fJEM1O2SEVCd
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:17115

Followups directed to: comp.lang.javascript

Show key headers only | View raw


Cezary Tomczyk wrote:

> W dniu 2012-11-09 14:53, Asen Bozhilov pisze:
> 
> Correct me if I am wrong, but if I will use closure around my methods
> and variables then they are not outside available.

Correct.

> So, potentially bad code, which can be run using Function, have no access
> to my variables and methods, right? Example:
> 
> var t = 50;
> 
> (function(){
>    var s = 100;
> })();

That is a closure.

> (function  () {
>   var a = 10;

That is a closure.

>   Function('a = 20; b = t; c = s;')();

That is *not* a closure.  The assignments are to (usually newly created) 
properties of the Global Object …

>    console.log(a,b,c); //10,50

… so the local variables of the calling context (here: a) are not affected, 
but the global ones (here: b, c) are.

>   })();
> console.log(a,b,c); //20,50

QED.  This code would throw a ReferenceError exception if the innermost 
Function code was strict mode code:

  /* ReferenceError: a is not defined */
  Function('"use strict"; a = 20;')();

Perhaps a figure helps (use a fixed-width font).  Assuming `t' is a global 
variable:

                                                ,--------->----------.
                                                :                    :
                                      ,--------------------.         :
                                      : Global object      :         :
                                      :--------------------.         :
var t = 50; ----------------------------> t : number = 50 --->-.     :
                               ,--------> a : number = 20  :   :     :
                               : ,------> b : number = 50  :   :     :
                               : : ,----> c : number =  ? -------.-. :
                               : : :  `--------------------'   : : : :
                               : : :                           : : : :
(function(){                   : : :  ,--------------------.   : : v :
   var s = 100; ---------------:-:-:----> s : number = 100 :   : : : :
})();                          : : :  `--------------------'   : : : :
                               : : :                           : : : :
(function () {                 : : :   ,-------------------.   : : : :
  var a = 10; -----------------:-:-:-----> a : number = 10 --->----. :
                               : : :   `-------------------'   : : : :
                               : : :                           : : : :
  Function(                    : : :                           : : : :
      'a = 20;' ---------------' : :                           : : : :
    + 'b = t;' ------------------'<----------------------------' : : :
    + 'c = s;' --------------------'<----------------------------' : :
  )();                                                             : :
                                                                   : :
  console.log(a,b,c); //10,50 <------------------------------------' :
})();                                                                :
                                                                     :
console.log(a,b,c); //20,50 <----------------------------------------'

In non-strict code, the error occurs where the `?' is.  In strict code, the 
error occurs earlier, where the second `a' is attempted to be assigned to.  
For strict mode essentially forbids augmenting the Global Object (or any 
other object in the scope chain outside the local execution context) by 
simple assignment to an identifier in function or eval code.

> Then I've got from console "ReferenceError: s is not defined" which is
> expected by me.

Exactly, but your logic is flawed.
 
> So, if we speaking about getting string from server (XMLHttpRequest) and
> execute code what exactly means that using eval is insecure?

Unless in ES 5+ strict mode, the scope chain of eval code is not empty.  
Code can be injected into your program and use the capabilities of your 
program.  You would want only trustworthy code to do that, and there are no 
guarantees.  Besides, eval code is harder to debug.

This can be useful; eval() can be used to hide potentially unsupported 
program syntax.

But if all you want to transfer is data, and not business logic, then you 
should parse the data and you should not allow business logic to be 
injected.


PointedEars
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

eval - how to Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-11-09 12:47 +0100
  Re: eval - how to Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-11-09 05:53 -0800
    Re: eval - how to Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-11-09 22:28 +0100
      Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-10 10:23 +0100
      Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-10 11:15 +0100
        Re: eval - how to Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-11-11 00:35 +0100
          Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-11 20:34 +0100
    Re: eval - how to Dr J R Stockton <reply1245@merlyn.demon.co.uk.invalid> - 2012-11-10 22:42 +0000
  Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-09 17:46 +0100
    Re: eval - how to Tim Streater <timstreater@greenbee.net> - 2012-11-09 17:00 +0000
      Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-09 18:19 +0100
    Re: eval - how to Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-11-09 22:49 +0100
      Re: eval - how to Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-11-09 16:40 -0800
        Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-10 11:36 +0100
          Re: eval - how to Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-11-10 13:29 +0100
            Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-10 13:55 +0100
              Re: eval - how to Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-11-10 14:17 +0100
                Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-10 20:50 +0100
                Re: eval - how to Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-11-10 22:54 +0100
                Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-11 15:22 +0100
                Re: eval - how to Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-11-11 16:24 +0100
                Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-11 19:23 +0100
          Re: eval - how to Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-11-11 10:40 -0800
            Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-11 20:41 +0100
              Re: eval - how to Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-11-11 13:34 -0800
                Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-11 23:03 +0100
      Re: eval - how to Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-10 10:21 +0100
  Re: eval - how to SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> - 2012-11-10 04:16 +0100

csiph-web