Skip to main content
Black Clouds & Silver LiningsBlack Clouds & Silver Linings
in Node.js SecurityĀ in Node.js SecurityĀ 
Liran TalLiran Tal
Developer Advocate @ SnykDeveloper Advocate @ Snyk
@liran_tal github.com/lirantal
May 2019May 2019
@liran_tal
github.com/lirantal
Liran TalLiran Tal
Developer AdvocateDeveloper Advocate
0101 Black Clouds in Node.js SecurityBlack Clouds in Node.js Security
02Ā 02Ā ||
||
03Ā 03Ā ||
Common Security VulnerabilitiesCommon Security Vulnerabilities
Silver Linings in Node.js SecuritySilver Linings in Node.js Security
Black Clouds & Silver LiningsBlack Clouds & Silver Linings
in Node.js Securityin Node.js Security
Node.js is JavaScriptNode.js is JavaScript
JavaScript is EverywhereJavaScript is Everywhere
FrontendFrontend
BackendBackend
IoTIoT
DatabasesDatabases
ChatbotsChatbots
MachineMachine
LearningLearning
WebAssemblyWebAssembly
RoboticsRobotics
src: https://snyk.io/opensourcesecurity-2019
Invites big risksInvites big risks
The Biggest RepositoryThe Biggest Repository
Invites big risksInvites big risks
The Biggest RepositoryThe Biggest Repository
Lucrative attack impactLucrative attack impact
Invites big risksInvites big risks
The Biggest RepositoryThe Biggest Repository
Lucrative attack impactLucrative attack impact
Open and free-to-publish ecosystemOpen and free-to-publish ecosystem
Invites big risksInvites big risks
The Biggest RepositoryThe Biggest Repository
Lucrative attack impactLucrative attack impact
Open and free-to-publish ecosystemOpen and free-to-publish ecosystem
Difficult to counter-measureDifficult to counter-measure
Black Clouds inBlack Clouds in
Node.js SecurityNode.js Security
Malicious ModulesMalicious Modules
Black Clouds inBlack Clouds in
Node.js SecurityNode.js Security
Malicious ModulesMalicious Modules
Typosquatting AttacksTyposquatting Attacks
Compromised AccountsCompromised Accounts
Social EngineeringSocial Engineering
Malicious ModulesMalicious Modules
timetime
JanĀ 2015
rimrafallrimrafall
rimrafallrimrafall
rimrafallrimrafall
Malicious ModulesMalicious Modules
timetime
JanĀ 2015
rimrafallrimrafall
JanĀ 2017
crossenvcrossenv
cross-envcross-env
$ npm install crossenv --save
crossenvĀ  Ā  !=Ā  Ā cross-envcrossenvĀ  Ā  !=Ā  Ā cross-env
$ npm install crossenv --save
coffescriptĀ  Ā  Ā coffescriptĀ  Ā  Ā  oror Ā  Ā  Ā coffe-scriptĀ Ā  Ā  Ā coffe-scriptĀ 
coffescriptĀ  Ā  Ā coffescriptĀ  Ā  Ā  oror Ā  Ā  Ā coffe-scriptĀ Ā  Ā  Ā coffe-scriptĀ 
coffeescriptcoffeescript
src: https://snyk.io/vuln
post-install script āœ…post-install script āœ…
post-install script āœ…post-install script āœ…
call-home base64 payload āœ…call-home base64 payload āœ…
How did we find out about this maliciousHow did we find out about this malicious
crossenv package?crossenv package?
post-install script āœ…post-install script āœ…
call-home base64 payload āœ…call-home base64 payload āœ…
Malicious ModulesMalicious Modules
timetime
JanĀ 2015
rimrafallrimrafall
JanĀ 2017
crossenvcrossenv
MayĀ 2018
getcookiesgetcookies
Malicious ModulesMalicious Modules
timetime
JanĀ 2015
rimrafallrimrafall
JanĀ 2017
crossenvcrossenv
MayĀ 2018
getcookiesgetcookies
JulĀ 2018
eslint-eslint-
scopescope
eslint-scope 3.7.2eslint-scope 3.7.2
malicious package publishedmalicious package published
eslint-scope 3.7.2eslint-scope 3.7.2
malicious package publishedmalicious package published
What's going on?What's going on?
Who depends on eslint-scope?Who depends on eslint-scope?
Who depends on eslint-scope?Who depends on eslint-scope?
babel-eslintbabel-eslint
Who depends on eslint-scope?Who depends on eslint-scope?
babel-eslintbabel-eslint
eslinteslint
Who depends on eslint-scope?Who depends on eslint-scope?
babel-eslintbabel-eslint
eslinteslint
webpackwebpack
npm invalidates all tokensnpm invalidates all tokens
<= 2018-07-12<= 2018-07-12
Ā 
npm invalidates all tokensnpm invalidates all tokens
<= 2018-07-12<= 2018-07-12
Ā 
estimated potential ~4,500 accountsĀ estimated potential ~4,500 accountsĀ 
were compromisedĀ were compromisedĀ 
How does something likeHow does something like
this happen?this happen?
Compromised Contributors ?Compromised Contributors ?CompromisedCompromised ContributorsContributors ??
Compromised Contributors ?Compromised Contributors ?
14%14%
compromised npm modulescompromised npm modules
CompromisedCompromised ContributorsContributors ??
src: https://github.com/ChALkeR/notes
Compromised Contributors ?Compromised Contributors ?
20%20%
npm total monthly downloadsnpm total monthly downloads
CompromisedCompromised ContributorsContributors ??
Compromised Contributors ?Compromised Contributors ?
20%20%
npm total monthly downloadsnpm total monthly downloads
expressexpress reactreact
debugdebug
momentmoment
requestrequest
CompromisedCompromised ContributorsContributors ??
https://giphy.com/embed/aWPGuTlDqq2yc
Compromised Contributors ?Compromised Contributors ?
662662 usersusers
123456123456
had their password set tohad their password set to
CompromisedCompromised ContributorsContributors ??
Compromised Contributors ?Compromised Contributors ?
14091409 usersusers
had their password set tohad their password set to
their usernametheir username
CompromisedCompromised ContributorsContributors ??
Compromised Contributors ?Compromised Contributors ?
11%11% usersusers
had their password set tohad their password set to
previously leaked passwordpreviously leaked password
CompromisedCompromised ContributorsContributors ??
Malicious ModulesMalicious Modules
timetime
JanĀ 2015
rimrafallrimrafall
JanĀ 2017
crossenvcrossenv
MayĀ 2018
getcookiesgetcookies
JulĀ 2018
eslint-eslint-
scopescope
event-streamevent-stream
NovĀ 2019
src: https://snyk.io/blog/a-post-mortem-of-the-malicious-event-stream-backdoor
Dependency ManagementDependency Management
Black Clouds inBlack Clouds in
Node.js SecurityNode.js Security
(CC BY-NC-SA 2.0)
OWASP Top 10:OWASP Top 10:
Using Components WithUsing Components With
Known VulnerabilitiesKnown Vulnerabilities
who watches after all thesewho watches after all these
modules ?modules ?
who watches after all thesewho watches after all these
modules ?modules ?
who watches after all thesewho watches after all these
modules ?modules ?
0101 The Scary State of Node.js SecurityThe Scary State of Node.js Security
02Ā 02Ā ||
||
03Ā 03Ā ||
Selected Vulnerabilities in Node.jsSelected Vulnerabilities in Node.js
Silver Linings in Node.js SecuritySilver Linings in Node.js Security
Black Clouds & Silver LiningsBlack Clouds & Silver Linings
in Node.js Securityin Node.js Security
Command InjectionCommand Injection
Common SecurityCommon Security
VulnerabilitiesVulnerabilities
Command InjectionCommand Injection
Command InjectionCommand Injection
Command InjectionCommand Injection
DemoDemo
Best Practice:Best Practice:
execFile('git', [...args])execFile('git', [...args])
Command InjectionCommand Injection
Best Practice:Best Practice:
execFile('git', [...args])execFile('git', [...args])
Maintain a whitelist of allowed argsMaintain a whitelist of allowed args
Blacklist special shell chars like ;Blacklist special shell chars like ;
PrayPray
Command InjectionCommand Injection
Regular ExpressionsRegular Expressions
Regular ExpressionsRegular Expressions
Regular ExpressionsRegular Expressions
^([01]?dd?|2[0-4]d|25
[0-5]).([01]?dd?|2[0-4]
d|25[0-5]).([01]?dd?|
2[0-4]d|25[0-5]).([01]?
dd?|2[0-4]d|25[0-5])$
Regular ExpressionsRegular Expressions
^([01]?dd?|2[0-4]d|25
[0-5]).([01]?dd?|2[0-4]
d|25[0-5]).([01]?dd?|
2[0-4]d|25[0-5]).([01]?
dd?|2[0-4]d|25[0-5])$
IP AddressIP Address
Regular ExpressionsRegular Expressions
https://giphy.com/embed/xNBcChLQt7s9a
Regular ExpressionsRegular Expressions
Matching a Song TitleMatching a Song Title
^([a-zA-Z0-9])$^([a-zA-Z0-9])$
Regular ExpressionsRegular Expressions
Matching a Song TitleMatching a Song Title
^([a-zA-Z0-9])$^([a-zA-Z0-9])$^([a-zA-Z0-9]+s?)$^([a-zA-Z0-9]+s?)$
Regular ExpressionsRegular Expressions
Matching a Song TitleMatching a Song Title
^([a-zA-Z0-9])$^([a-zA-Z0-9])$^([a-zA-Z0-9]+s?)$^([a-zA-Z0-9]+s?)$^([a-zA-Z0-9]+s?)+$^([a-zA-Z0-9]+s?)+$
Regular ExpressionsRegular Expressions
Catastrophic BacktrackingCatastrophic Backtracking
Exploits greedy quantifiersExploits greedy quantifiers
Simple regexs are vulnerable tooSimple regexs are vulnerable too
/^(a+)+$//^(a+)+$/
Regular ExpressionsRegular Expressions
20172017 msms||
20162016 MomentMoment||
20182018 ||
20182018 ua-parser-jsua-parser-js|| 20M DL20M DL ||
96M DL96M DL ||
36M DL36M DL ||
sshpksshpk40M DL40M DL ||
Regular ExpressionsRegular Expressions
20172017 msms||
20162016 MomentMoment||
20182018 ||
20182018 ua-parser-jsua-parser-js|| 20M DL20M DL ||
96M DL96M DL ||
36M DL36M DL ||
sshpksshpk40M DL40M DL ||
Best Practices ?Best Practices ?
Regular ExpressionsRegular Expressions
Best Practices ?Best Practices ?
Regular ExpressionsRegular Expressions
Best Practice #1Best Practice #1
DO NOT WRITE YOUR OWN REGEXDO NOT WRITE YOUR OWN REGEX
Regular ExpressionsRegular Expressions
Best Practice #1Best Practice #1
DO NOT WRITE YOUR OWN REGEXDO NOT WRITE YOUR OWN REGEX
Best Practice #2Best Practice #2
DO NOT WRITE YOUR OWN REGEXDO NOT WRITE YOUR OWN REGEX
Regular ExpressionsRegular Expressions
Best Practice #3Best Practice #3
ValidatorValidator Node.js moduleNode.js module
Regular ExpressionsRegular Expressions
Best Practice #4Best Practice #4
Safe-RegexSafe-Regex Node.js moduleNode.js module
const safeRegex = require('safe-regex')
let regex = /^(([a-z])+.)+[A-Z]([a-z])+$/
let isSafe = safeRegex(regex)
0101 The Scary State of Node.js SecurityThe Scary State of Node.js Security
02Ā 02Ā ||
||
03Ā 03Ā ||
Selected Vulnerabilities in Node.jsSelected Vulnerabilities in Node.js
Silver Linings in Node.js SecuritySilver Linings in Node.js Security
Black Clouds & Silver LiningsBlack Clouds & Silver Linings
in Node.js Securityin Node.js Security
The npmjs EcosystemThe npmjs Ecosystem
Silver Linings inSilver Linings in
Node.js SecurityNode.js Security
FightingFighting TyposquattingTyposquatting
Package Moniker RulesPackage Moniker Rules
FightingFighting TyposquattingTyposquatting
JSONStreamĀ  Ā JSONStreamĀ  Ā  !=!=Ā  Ā  jsonstreamĀ  Ā  jsonstream
Package Moniker RulesPackage Moniker Rules
FightingFighting TyposquattingTyposquatting
Package Moniker RulesPackage Moniker Rules
react-nativereact-native
FightingFighting TyposquattingTyposquatting
Package Moniker RulesPackage Moniker Rules
react-nativereact-native
reactnativereactnative
FightingFighting TyposquattingTyposquatting
Package Moniker RulesPackage Moniker Rules
rea-ct.nativerea-ct.native
react-nativereact-native
reactnativereactnative
FightingFighting TyposquattingTyposquatting
Package Moniker RulesPackage Moniker Rules
rea-ct.nativerea-ct.native
react-nativereact-native
reactnativereactnative
react_nativereact_native
FightingFighting TyposquattingTyposquatting
Package Moniker RulesPackage Moniker Rules
rea-ct.nativerea-ct.native
react-nativereact-native
reactnativereactnative
react_nativereact_native
Ā  Ā @lirantal/rea-ct.nativeĀ  Ā @lirantal/rea-ct.native
FightingFighting TyposquattingTyposquatting
Package Moniker RulesPackage Moniker Rules
Package PublishingPackage Publishing NotificationsNotifications
$ npm profile enable-2fa
2FA successfully enabled.
Below are your recovery codes,
please print these out.
2FA tokens2FA tokens for npm >= 5.5.1for npm >= 5.5.1
$ npm profile enable-2fa
2FA successfully enabled.
Below are your recovery codes,
please print these out.
2FA tokens2FA tokens for npm >= 5.5.1for npm >= 5.5.1
TakingTaking OwnershipOwnership ofof
Your App SecurityYour App Security
TakingTaking OwnershipOwnership ofof
Your App SecurityYour App Security
Source: The State of Open Source Security Report 2019, Snyk
https://snyk.io/opensourcesecurity-2019/