├── .gitignore ├── .github └── FUNDING.yml ├── manifest.yml ├── package.json ├── LICENSE ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [iamanishroy] -------------------------------------------------------------------------------- /manifest.yml: -------------------------------------------------------------------------------- 1 | name: netlify-plugin-js-obfuscator 2 | 3 | inputs: 4 | - name: compact 5 | description: Compact code output on one line. 6 | default: false 7 | - name: numbersToExpressions 8 | description: Enables numbers conversion to expressions 9 | default: true 10 | - name: simplify 11 | description: Enables additional code obfuscation through simplification. 12 | default: true 13 | - name: shuffleStringArray 14 | description: Randomly shuffles the stringArray array items 15 | default: true 16 | - name: splitStrings 17 | description: Splits literal strings into chunks with length of splitStringsChunkLength option value 18 | default: true 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "netlify-plugin-js-obfuscator", 3 | "version": "1.0.20", 4 | "description": "JavaScript Obfuscator for your sites hosted on netlify, which provide protection for your source code.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/iamanishroy/netlify-plugin-js-obfuscator.git" 12 | }, 13 | "keywords": [ 14 | "netlify", 15 | "netlify-plugin", 16 | "JavaScript", 17 | "JS", 18 | "obfuscator", 19 | "plugin" 20 | ], 21 | "author": "", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/iamanishroy/netlify-plugin-js-obfuscator/issues" 25 | }, 26 | "homepage": "https://github.com/iamanishroy/netlify-plugin-js-obfuscator#readme", 27 | "dependencies": { 28 | "readdirp": "^3.5.0", 29 | "javascript-obfuscator": "^2.10.3" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Tom Bonnike 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const fs = require("fs"); 2 | const util = require("util"); 3 | const readFile = util.promisify(fs.readFile); 4 | const writeFile = util.promisify(fs.writeFile); 5 | const readdirp = require("readdirp"); 6 | const JavaScriptObfuscator = require("javascript-obfuscator"); 7 | var excludedDir = ["!node_modules", "!plugins"]; 8 | 9 | // listing all the '.js' files 10 | const getJsFiles = async (directory, func_dir) => { 11 | if (func_dir) { 12 | excludedDir.push(`!${func_dir}`); 13 | } 14 | const files = await readdirp.promise(directory, { 15 | fileFilter: "*.js", 16 | directoryFilter: excludedDir, 17 | }); 18 | return files.map((file) => file.fullPath); 19 | }; 20 | 21 | // Reading the files -> obfuscating the code -> Writing the obfuscated code 22 | const obfuscateCode = async (filePath, custom, utils) => { 23 | var file = await readFile(filePath, "utf8"); 24 | try { 25 | var obfuscationResult = JavaScriptObfuscator.obfuscate(file, { 26 | compact: custom[0], 27 | controlFlowFlattening: true, 28 | controlFlowFlatteningThreshold: 1, 29 | numbersToExpressions: custom[1], 30 | simplify: custom[2], 31 | shuffleStringArray: custom[3], 32 | splitStrings: custom[4], 33 | }); 34 | } catch (error) { 35 | return utils.build.failBuild(`Failed to Obfuscate '${filePath}' .`, { error }); 36 | } 37 | await writeFile(filePath, obfuscationResult.getObfuscatedCode()); 38 | return 1; 39 | }; 40 | module.exports = { 41 | onPostBuild: async ({ inputs, constants, utils }) => { 42 | const jsFiles = await getJsFiles( 43 | constants.PUBLISH_DIR, 44 | constants.FUNCTIONS_SRC 45 | ); 46 | for (const filePath of jsFiles) { 47 | console.log(filePath); 48 | await obfuscateCode( 49 | filePath, 50 | [ 51 | inputs.compact, 52 | inputs.numbersToExpressions, 53 | inputs.simplify, 54 | inputs.shuffleStringArray, 55 | inputs.splitStrings, 56 | ], 57 | utils 58 | ); 59 | } 60 | console.log("JS files successfully Obfuscated!"); 61 | }, 62 | }; 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # netlify-plugin-js-obfuscator 2 | 3 | A Netlify Build plugin to extract and obfuscate your JavaScript Source Code, which provide protection for your source code. [JS Obfuscator](https://github.com/iamanishroy/netlify-plugin-js-obfuscator), built on top of the [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator). 4 | JavaScript Obfuscator is a powerful free obfuscator for JavaScript, containing a variety of features which provide protection for your source code. 5 | 6 | **Key features:** 7 | 8 | - variables renaming 9 | - strings extraction and encryption 10 | - dead code injection 11 | - control flow flattening 12 | - various code transformations 13 | 14 | The example of obfuscated code: [github.com](https://github.com/javascript-obfuscator/javascript-obfuscator/blob/master/examples/javascript-obfuscator.js) 15 | 16 | ## :warning: Important 17 | 18 | As describe in the [`javascript-obfuscator` package](https://github.com/javascript-obfuscator/javascript-obfuscator/blob/master/README.md#warning-important), code obfuscation may impact your website's performance. 19 | 20 | ## Usage and inputs 21 | 22 | To install the plugin in the Netlify UI, use this [direct in-app installation link](https://app.netlify.com/plugins/netlify-plugin-js-obfuscator/install) or go to the [Plugins directory](https://app.netlify.com/plugins). 23 | 24 | For file-based installation, add it to your `netlify.toml` file. 25 | 26 | ```toml 27 | [[plugins]] 28 | package = "netlify-plugin-js-obfuscator" 29 | 30 | # All inputs are optional, so you can omit this section. 31 | # Defaults are shown below. 32 | # You can also refer to `javascript-obfuscator`’s documentation: https://github.com/javascript-obfuscator/javascript-obfuscator. 33 | [plugins.inputs] 34 | # Compact code output on one line. 35 | compact = false 36 | # Enables numbers conversion to expressions 37 | numbersToExpressions = true 38 | # Enables additional code obfuscation through simplification. 39 | simplify = true 40 | # Randomly shuffles the stringArray array items 41 | shuffleStringArray = true 42 | # Splits literal strings into chunks with length of splitStringsChunkLength option value 43 | splitStrings = true 44 | ``` 45 | 46 | To complete file-based installation, from your project's base directory, use npm package manager to add the plugin to `devDependencies` in `package.json`. 47 | 48 | ```bash 49 | npm install -D netlify-plugin-js-obfuscator 50 | ``` 51 | 52 | Once installed and configured, the plugin will automatically run on the Netlify CI. 53 | 54 | ### Testing locally 55 | 56 | To test this plugin locally, you can use the [Netlify CLI](https://github.com/netlify/cli): 57 | 58 | ```bash 59 | # Install the Netlify CLI. 60 | npm install netlify-cli -g 61 | 62 | # In the project working directory, run the build as Netlify would with the build bot. 63 | netlify build 64 | ``` 65 | --------------------------------------------------------------------------------