Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LeftPad Not Found

LeftPad Not Found

A talk about Node.js security and npm package dependencies best practices.

Talk overview:
1. history and overview of npm
2. the left-pad incident (npm-gate)
3. installing npm packages
4. keeping npm packages updated
5. publishing npm packages

Hans Kristian Flaatten

May 25, 2016
Tweet

More Decks by Hans Kristian Flaatten

Other Decks in Technology

Transcript

  1. ! Starefossen LeftPad Not Found Hans Kristian Flaatten ! Starefossen

    1 Node.js Oslo Meetup May 25, 2016, at MESH Node.js / Web Security
  2. ! Starefossen > cat about.txt Hans Kristian Flaatten I ♥

    Open Source # Computer Science @NTNU $ DevOps @Turistforeningen % Build & Docker @NodeJS 3
  3. ! Starefossen > cat agenda.txt 5 > man npm >

    npm install left-pad > npm install > the npm worm > npm outdated > npm publish > credential leakage
  4. ! Starefossen > history 2009 Ryan Dahl releases Node.js 2010

    Isaac Schlueter releases npm 2011 Node.js v0.6.3 bundles npm 2012 Isaac becomes maintainer of Node.js 2014 npm, Inc. is founded 7
  5. ! Starefossen npm stats 8 Source: modulecounts.com ( 278k public

    packages 1B ) downloads/week Source: npmjs.org
  6. ! Starefossen 9 1 year+ since last release * 133k

    (47%) + 93k (33%) Without a license & 84k (30%) Latest version 0.0.x , 1.1 Maintainers (avg.) npm stats (dark side)
  7. ! Starefossen > npm install left-pad 11 1 module.exports =

    leftpad; 2 function leftpad (str, len, ch) { 3 str = String(str); 4 var i = -1; 5 if (!ch && ch !== 0) ch = ' '; 6 len = len - str.length; 7 while (++i < len) { 8 str = ch + str; 9 } 10 return str; 11 }
  8. ! Starefossen Mar 22th 2:30 PM 4:55 PM - Hijacked

    . Unpublished / Transferred 0 Shit's on fire 1 npm responds 2:35 PM 2:40 PM
  9. ! Starefossen Post Mortem 3 new unpublish policy 1 protect

    yourself • write your own libs • bundling/vendoring • private registry 15
  10. ! Starefossen Install the right thing • Common misspellings •

    coffee-script - coffeescript - coffe-script • uglify-js - uglifyjs • socket.io - socketio 17
  11. ! Starefossen npm-scripts • "scripts" in package.json • start, test,

    build etc. • pre* and post* scripts • preinstall is run automatically before install • postinstall is run automatically after install 18
  12. ! Starefossen > cat package.json 19 { "name": "my-package", "version":

    "1.0.0", "scripts": { ..., "postinstall": "grunt build" }, ... }
  13. ! Starefossen > cat package.json 20 { "name": "my-evil-package", "version":

    "1.0.0", "scripts": { ..., "postinstall": "rm -rf /" }, ... }
  14. ! Starefossen The npm Worm 1.installs itself on «download» 2.modifies

    your package.json 3.publishes a new release US-CERT vulnerability #319816 21
  15. ! Starefossen –The npm Blog 2 npm cannot guarantee that

    packages available on the registry are safe. 23
  16. ! Starefossen Protecting Yourself • npm install --ignore-scripts • npm

    config set ignore-scripts true • npm logout 24
  17. ! Starefossen > npm install nsp 32 > nsp check

    (+) 1 vulnerabilities found ┌───────────────┬────────────────────────────────────────┐ │ │ Cross Site Scripting │ ├───────────────┼────────────────────────────────────────┤ │ Name │ backbone │ ├───────────────┼────────────────────────────────────────┤ │ Installed │ 0.3.3 │ ├───────────────┼────────────────────────────────────────┤ │ Vulnerable │ <= 0.3.3 │ ├───────────────┼────────────────────────────────────────┤ │ Patched │ >= 0.5.0 │ ├───────────────┼────────────────────────────────────────┤ │ Path │ [email protected] > [email protected] │ ├───────────────┼────────────────────────────────────────┤ │ More Info │ https://nodesecurity.io/advisories/108 │ └───────────────┴────────────────────────────────────────┘
  18. ! Starefossen > npm install greenkeeper • 4 greenkeeper.io •

    Key features • dependency monitoring • pull request updates • auto merging (optional) 34
  19. ! Starefossen > npm install npm-check • ! dylang/npm-check •

    Key features • upgrade outdated • fix incorrect • remove unused 36
  20. ! Starefossen Getting Started • npm init • version >=

    1.0.0 • write tests • code linting • use a ci server 39
  21. ! Starefossen Leaked Sources • Dotfiles (.ssh, .npmrc, .gitconfig etc.)

    • Config files (package.json, Gemfile etc.) • Scripts (hardcoded stuff) • Logs 41
  22. ! Starefossen Leak Mitigation • .gitignore + .npmignore (blacklist) •

    package.json "files" (whitelist) • automate publishing to npm 42
  23. ! Starefossen Wrapping Up • Dependent on npm much? •

    Think before you install! • What do you publish? • Automate. Automate. Automate. 45