Results 1 to 11 of 11

Thread: Javascript eval();

  1. #1

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    This is an example of what it does!

    Code:
    name = "dave"
    greeting = "hello"
    eval_string =  "greeting + name";
    test_string = eval(eval_string);
    document.write(test_string);
    the output is: hellodave

    eval() has no real word programming use that I can think of because,

    Code:
    name = "dave"
    greeting = "hello"
    eval_string =  greeting + name;
    document.write(eval_string);
    would do exactly the same job *shrugs*!

  2. #2
    Addicted Member MrPresident2k's Avatar
    Join Date
    May 2002
    Location
    INDIA
    Posts
    167

    One more use is there

    Hi,

    Eval also calculates expressions as strings.
    say for example

    document.write(eval("1+1"))
    will print 2, even the 1+1 is passed as string

    Pres

  3. #3

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    yes but

    Code:
    document.write(1+1)
    would also print 2 to the browser so there really is no need for eval !

  4. #4

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404

  5. #5
    Addicted Member MrPresident2k's Avatar
    Join Date
    May 2002
    Location
    INDIA
    Posts
    167

    Nice 1 progressive..

    That was a nice page by u progressive.

    Thanks,
    Pres.

  6. #6
    DeadEyes
    Guest
    Care should be taken when using eval(). For example if
    you are using it in combination with an input control the
    user could enter malicous code which will then be executed!

  7. #7
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    Now that last one sounds interesting....

  8. #8

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    I can't see them being able to do anything malicious, since
    javascript is clientside so they'll only be able to affect there
    own machine !

    Can you enlighten us DeadEyes as to what they could do ?

  9. #9
    DeadEyes
    Guest
    Don't take my word for it this is from Mozilla Security Reviewers Guide May 17 2002
    Avoid using eval() whenever possible. Also avoid passing a string as the first argument to setTimeout() and setInterval(),
    as this causes an eval(). Eval, besides being slow, provides a good avenue for inserting and running malicious code. There is
    usually an alternative. If you must use eval(), be sure to verify that the string being passed to it contains an expected value.
    As for what can be done I wasn't thinking in terms of the client
    shooting themselves in the foot(although I did say user), but other people using your
    page to do their dirty work. And just to back peddle I've always balked at the mention of eval, because i understood it to be a
    risk. Where and when I first heard this I can't remember, and it's
    possible it could have been a different language but I don't think
    so

  10. #10
    DeadEyes
    Guest
    Ah yes the old heads working over time
    the phrase I was looking for "Cross Server Scripting"

  11. #11

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    I suppose if the page had frames and eval was used on an users input a DOS attack could be launched on another website.

    by the user entering setTimeout(parent.someframe.location='some site', 1);

    or something to that effect hmmm...

    I din't know their was an eval function in JavaScript until this thread, I was aware of it in Perl though !

    As I've said earlier there really is no need to use it in JavaScript anyway, but if you do you use it to evaluate a users input make sure you perform some sanity checks first, as should be done with any user input. Even more so on the server side.

    eg. use regular expressions to strip out dangerous characters

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width