Does Virus affected Node Modules transfer in Contents of SPFx deployment package
-
30-01-2021 - |
Question
When we scaffold an SPFx webpart/Extension we get many Node Modules as part of dependencies. These can be seen in node_modules
folder and there are also entries in package-lock.json
file.
I know these are dependencies for development.
Now assume a case where one of the Node Modules contains Virus (like in flatmap-stream and event-stream). Does the package files generated from commands gulp bundle --ship
and gulp package-solution --ship
will contain any of the code from all or effected Node Modules?
Solution
TLDR;
The short answer is no, the package doesn't contain all of the code from node_modules, yet it contains some very small amount of proven code (mostly css-loader code from corresponding webpack loader).
Long answer
SharePoint Framework uses webpack to bundle your code into a single file (aka package). Apart your code SharePoint Framework contains a lot of components which you reference in your code via imports (import smth from '@microsoft/sp-webpart-base'
etc.), however, those components are not included in your code. They serve as "externals" and are loaded by SharePoint Framework at runtime.
If you take a look at webpack configuration, you will see below picture:
....
devtool: 'source-map',
entry:
{ 'hello-world-web-part':
'C:\\temp\\spfx-test\\lib\\webparts\\helloWorld\\HelloWorldWebPart.js',
'my-new-wp-web-part':
'C:\\temp\\spfx-test\\lib\\webparts\\myNewWp\\MyNewWpWebPart.js' },
externals:
[ '@microsoft/sp-lodash-subset',
'@microsoft/sp-core-library',
'@microsoft/decorators',
'@microsoft/office-ui-fabric-react-bundle',
'@microsoft/sp-diagnostics',
'@microsoft/sp-dynamic-data',
'@microsoft/sp-polyfills',
'@microsoft/sp-http',
'@microsoft/sp-page-context',
'@microsoft/sp-loader',
'@microsoft/sp-component-base',
'@microsoft/sp-webpart-base',
'@microsoft/sp-office-ui-fabric-core',
'@microsoft/sp-extension-base',
'@microsoft/sp-application-base',
'@microsoft/sp-client-preview',
'@microsoft/sp-webpart-workbench',
'react',
'react-dom',
'HelloWorldWebPartStrings',
'MyNewWpWebPartStrings' ],
output:
....
Everything which is in the external section won't be included into the resulting bundle, yet it will be served at runtime by Microsoft. Your package contains only your code (plus a bit of code from webpack's css-loader like mentioned earlier). You can run gulp clean
then gulp bundle
and open ./dist
folder to review the generated bundle. Only your code will be included.
Which basically means that you shouldn't care about such things like viruses, etc. However, Microsoft should care that all externals modules they use (aka @microsoft/*
and react*
) are virus-free and safe.
From your side, you should only care about any external modules you install after scaffolding your project. For example react modules, or helpers, or whatever else.
Additional reading:
250+ vulnerabilities in a new SharePoint Framework project
Don't be alarmed by vulnerabilities after running NPM Install
Code Security Audit using “npm audit”