Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
grunt-mjs-strip-code
Advanced tools
The grunt-strip-code plugin is used to remove sections of code from production builds that are only needed in development and test environments. grunt-strip-code uses start and end comments to identify the code sections to strip out. For example:
/* test-code */
removeMeInProduction();
/* end-test-code */
doNotRemoveMe();
A use-case for this practice is to make private JavaScript functions accessible to unit tests without exposing them in production builds. This blog post goes into more detail about the concept and implementation.
This plugin requires Grunt ~0.4.1
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-strip-code --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-strip-code');
In your project's Gruntfile, add a section named strip_code
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
strip_code: {
options: {
// Task-specific options go here.
},
your_target: {
// Target-specific file lists and/or options go here.
},
},
})
Type: String
Default value: test-code
The text inside the opening comment used to identify code to strip.
Type: String
Default value: end-test-code
The text inside the closing comment used to identify code to strip.
Type: RegExp
Default value: (a generated RegExp matching the start and end comments)
If the default start and end comment matching doesn't work for you needs, you can supply your own RegExp to match against. If the pattern
option is specified, the start_comment
and end_comment
options are ignored.
The following source code exposes the bar
function to the public API for testing, but the bar
function should not be accessible in the released library. grunt-strip-code (with the default options) will remove the comment blocks from the example below keeping the bar
function private in production:
(function() {
function bar() {
doSomething();
}
var api = {
foo: function() {
bar();
return "foo";
}
}
/* test-code */
api._bar = bar;
/* end-test-code */
return api;
}());
The following configuration will strip out code that begins with the /* start-test-block */
comment and ends with the /* end-test-block */
comment from all .js
files in the dist/
folder.
grunt.initConfig({
strip_code: {
options: {
start_comment: 'start-test-block',
end_comment: 'end-test-block',
},
src: 'dist/*.js'
},
})
The following configuration will remove log()
statements from all .js
files in the dist/
folder
grunt.initConfig({
strip_code: {
options: {
pattern: /log\(\)/g
},
src: 'dist/*.js'
},
})
The normal behavior is to strip out code in the source files and then save those files with the same name. If you need to save them to a different name, you can specify a dest
option as well.
grunt.initConfig({
strip_code: {
options: { },
files: [
{src: 'tmp/my-app.js', dest: 'dist/my-app.js'},
{src: 'tmp/my-lib.js', dest: 'dist/my-lib.js'},
],
},
})
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
FAQs
Remove dev and test only code in production builds
The npm package grunt-mjs-strip-code receives a total of 1 weekly downloads. As such, grunt-mjs-strip-code popularity was classified as not popular.
We found that grunt-mjs-strip-code demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.