Slide 1

Slide 1 text

Javascript Static Code Analyzer Héctor Quartino [email protected]

Slide 2

Slide 2 text

About me • 10+ years of developer experience. • Multiple languages / technologies (Java, C#, JS). • Curious about Security for a while. • AppSec Engineer @ NetSuite.

Slide 3

Slide 3 text

About this talk • Motivation. • A bit of theory on the concepts behind static code analyzers. • Related Javascript tools.

Slide 4

Slide 4 text

Static Code Analysis – What & Why? • Runs on source code (white-box testing). • Can use project specific rules. • Can be automated. • Highly effective with specific patterns. • Early detection of potential issues.

Slide 5

Slide 5 text

Why analyze Javascript code? • Scripting language of the Web. – Web Applications vs. Native Apps on mobile. • JavaScript Server Applications – Rhino – NodeJS • IoT – Embedded Devices – Home Automation – Robotics – DIY

Slide 6

Slide 6 text

Why a Javascript Static Code Analyzer? • Scenario of 100% Javascript Applications is more common. • Testing infrastructure in Javascript too! • Goal: One language can give more flexibility to teams. • Goal: Run static code analysis on CI / nightly builds. • Goal: Leverage existing tools available in the Javascript community.

Slide 7

Slide 7 text

Static Code Analysis Limitations • False positives – Need a way to filter successive scan results. • False negatives – Some vulnerabilities need to be discovered via dynamic code analysis. – Unaware of custom APIs, cross layer vulnerabilities, or configuration files. – Add custom rules to mitigate.

Slide 8

Slide 8 text

A few compiler concepts • Compiler: transforms code from one language to another. Lexer Parser Optimizer Generator Token s AST IR Output Input

Slide 9

Slide 9 text

A few compiler concepts • Compiler: transforms code from one language to another. Lexer Parser Optimizer Generator Token s AST IR Output Input Used by Static Analysis

Slide 10

Slide 10 text

A few compiler concepts (cont.) • Lexer: transforms code into tokens. Lexer Token s Input var foo = bar; Type Value Keyword var Identifier foo Punctuator = Identifier bar Punctuator ; Lexical Grammar

Slide 11

Slide 11 text

A few compiler concepts (cont.) • Parser: transforms tokens into AST. • Abstract Syntax Tree: represents program structure. Parser Token s AST Type Value Keyword var Identifier foo Punctuator = Identifier bar Punctuator ; VariableDeclaration bar foo Syntactic Grammar

Slide 12

Slide 12 text

A few Static Code Analyzer techniques • Data Flow Analysis – Collect dynamic information about data by analyzing static information. – Determine the Control Flows for a given program. if (a > 1) { pathA(a); } else { pathB(1); } if path A path B end- if

Slide 13

Slide 13 text

A few Static Code Analyzer techniques • Taint Analysis: Trace variables from user inputs (taints) to potentially vulnerable functions (sinks). var searchTerms = $(‘q’).val(); var trimmedSearchTerms = searchTerms.trim(); // … code displaySearchResults(trimmedSearchTerms, results); function displaySearchResults(terms, results) { $(selector).append(terms); // XSS }

Slide 14

Slide 14 text

Javascript Tools • Parser: Esprima, Acorn. • AST: ESTree. • Code Analyzer: ScanJS. • Code Analyzer, take 2: ESLint.

Slide 15

Slide 15 text

Esprima • Standard ECMAScript parser written in ECMAScript. – Supports ES6. – Standard AST based on ESTree project. – Heavily used, some examples: • Code Minifier: esmangle • Code Instrumentation: Instanbul • ES6 to ES5 transpiler: esnext (uses Espree)

Slide 16

Slide 16 text

ESTree • De facto standard AST specification. • Originated as part of SpiderMonkey’s Parser API. • Lots of manipulation tools available. DEMO

Slide 17

Slide 17 text

ScanJS • Focused on client-side web applications. • Web UI, no command line tools makes it hard to integrate with build tools. • Developed by Mozilla, unfortunately deprecated, but lives on as plugin for ESLint: eslint-config-scanjs. DEMO

Slide 18

Slide 18 text

ESLint • Uses Espree (fork of Esprima) for parsing. • Pluggable architecture: – All rules are plugins, more can be added at runtime. – Different parsers can be used (Esprima, Espree or Babel are currently compatible). – Language extensions specified at configuration. – Global variables predefined through Environments. • Integrates with editors, build systems, command line tools, and more!

Slide 19

Slide 19 text

DEMO

Slide 20

Slide 20 text

Q&A

Slide 21

Slide 21 text

Reference material • Esprima: http://esprima.org/ • ESTree spec: https://github.com/estree/estree/blob/master/spec.md • ESTools projects: https://github.com/estools • ESLint: http://eslint.org/ • ESLint integrations: http://eslint.org/docs/user-guide/integrations • ScanJS ESLint Plugin: https://github.com/mozfreddyb/eslint-config-scanjs • ScanJS ESLint Rules: https://github.com/mozfreddyb/eslint-plugin-scanjs-rules