validating javascript from realworld websites in a
play

Validating Javascript from Realworld Websites in a Browser-like - PowerPoint PPT Presentation

Validating Javascript from Realworld Websites in a Browser-like Environment Sander Snajalg JavaScript (ECMAScript) Object-oriented / functional scripting language Dynamic typing Prototype inheritance Objects are dictionaries of properties


  1. Validating Javascript from Realworld Websites in a Browser-like Environment Sander Sõnajalg

  2. JavaScript (ECMAScript) Object-oriented / functional scripting language Dynamic typing Prototype inheritance Objects are dictionaries of properties Standardized by ECMA, spec. 262.

  3. JS in the Web - Example <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra.. <html xmlns= "http://www.w3.org/1999/xhtml" xml:l.. <head> <meta http-equiv= "content-type" content= "text/h.. <title>Images for David Bowie</title> LINKED <script type= "text/javascript" / src= "/js/global-v77.js" / > <script type= "text/javascript" > DISCOGS.set("Session", {loggedin: false , userna.. DISCOGS.set('static_server', ''); </script> EMBEDDED </head>

  4. JavaScript in the Web Every web page is a JavaScript “program” of: JS core functions (ECMA standard) Browser's DOM implementation All linked and embedded snippets of JavaScript Browsers use different JS engines .. and different DOM implementations

  5. Browser Environment General model: In a browser: The website Client's custom JS Custom JS Client's Libs Client's Libs DOM Browser DOM impl Browser JS Core APIs impl W3C standard ECMA's standard The browser

  6. The Goals (1) Statically validate web site JavaScript's syntax (2) Simulate the execution inside a browser, detect runtime errors (3) Report usage of properties/functions that might not work in some browsers

  7. Towards Goal 1: syntax errors Basically equal to parsing Implement myself? Reuse something available? Some browser's JS engine? Which one? Google's V8? Mozilla's SpiderMonkey?

  8. Rhino Mozilla's alternative JavaScript engine Open-source Parser detects syntactical mistakes GOAL 1 Evaluation detects some API-level mistakes Tests for language-extensions can be implemented

  9. Simulated browser environment General model: Simulated environment: Client's custom JS Client's Libs DOM Core APIs Rhino

  10. Env.js A JavaScript implementation of DOM Can be loaded with Rhino

  11. Simulated Browser Environment In a browser: Simulated environment: The website Custom JS Custom JS Client's Libs Client's Libs GOAL 2 Env.js Browser DOM impl Browser JS Rhino impl The browser

  12. Browser-Specific Code // Firefox if (window.addEventListener) { myObject.addEventListener("mouseover", myEventHandler, false ); // IE } else { myObject.attachEvent("onmouseover", myEventHandler); } Quite impossible to distinguish real errors from false positives Mostly in JS libraries

  13. Challenge 1: Eliminating libraries, generated code No point to validate them Contain lot of browser-specific code Various ways: By names of linked scripts By headers If compressed Case-by-case

  14. Challenge 2: Unknown types Example: property srcElement of class Event is undefined in some browsers // ======= e is an Event var e = window.event function MyElement(src) { this .srcElement = src; } // ======= f is something different var f = new MyElement("foo", "bar"); NEED TO REPORT var g = e.srcElement; FALSE POSITIVE IF var h = f.srcElement; REPORTED

  15. Challenge 2: Unknown types State of the Art Type-inference on formalized subset of JS [Anderson et al] Abstract interpretation of JS programs [Jensen et al] Can we do it in a simpler way?

  16. Challenge 2: Unknown types Suggested Approach Rewrite this ... GOAL 3 var g = e.srcElement; ?? var h = f.srcElement; .. to this: if (e instanceof Event) { _report_call$Event$srcElement("myScript.js", 233); } var g = e.srcElement; if (f instanceof Event) { _report_call$Event$srcElement("myScript.js", 234); } var h = f.srcElement;

  17. Demo output

  18. Demo Output (1) =============================================================== *** VALIDATION ERRORS FOR SCRIPT : [embedded javascript, 25 lines] =============================================================== [ERROR] uncaught JavaScript runtime exception: TypeError: Cannot call method "substring" of undefined (around line 2) [WARNING] Reference to undefined property "language" (around line 11) [WARNING] Reference to undefined property "browserLanguage" (around line 11) [WARNING] Reference to undefined property "characterSet" (around line 11) [WARNING] Reference to undefined property "charset" (around line 11) [WARNING] Reference to undefined property "_domain" (around line 5754) [WARNING] Reference to undefined property "domain" (around line 5754)

  19. Demo output (2) S U M M A R Y ============= Runtime Browser- Script Warnings Errors errors specific --------------------------------------------------------------------- fEpost.js 0 0 0 0 embedded script 'var bannersFor 0 1 0 0 js.js 0 0 0 0 embedded script 'var gaJsHost = 0 1 0 0 embedded script 'var pageTracke 6 1 0 0

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend