webpack-encrypt-nodejs-module
This is a webpack plugin (webpack >= 5
) that creates an encrypted bundle nodejs module that will be decrypted at runtime on server using an environment variable as the desencryption key.
how to use it
add it to a webpack project
npm i webpack-encrypt-nodejs-module -D
if you are creating a webpack configuration from zero use this
npm i @babel/core @babel/preset-env babel-loader copy-webpack-plugin webpack webpack-cli webpack-obfuscator webpack-node-externals encrypt-nodejs-module-webpack-plugin -D
generate a password
openssl rand -base64 128
then create a webpack.config.js configuration file or modify yours
const path = require("path");
const webpack = require("webpack");
const fs = require("fs");
const nodeExternals = require("webpack-node-externals");
const JavaScriptObfuscator = require("webpack-obfuscator");
const EncryptModule = require("webpack-encrypt-nodejs-module");
const CopyPlugin = require("copy-webpack-plugin");
module.exports = {
entry: {
server: "./index.js",
},
output: {
path: path.join(__dirname, "dist"),
publicPath: "/",
filename: "[name].js",
},
target: "node",
node: {
__dirname: false,
__filename: false,
},
externals: [nodeExternals()],
plugins: [
new EncryptModule({
algorithm: "aes-192-cbc",
keylen: 24,
ivlen: 16,
password:
"copy here in one line the generated password with the command -> openssl rand -base64 128",
envVar: "PROTECTION_KEY",
}),
new JavaScriptObfuscator(
{
unicodeEscapeSequence: true,
rotateUnicodeArray: true,
},
["excluded_bundle_name.js"]
),
new CopyPlugin({
patterns: ["package.json"],
}),
],
};
to build with a command add this to your package.json
"scripts": {
"build": "webpack --mode production --config webpack.config.js",
}
then run
npm run build
you should see the final result in dist folder
To test the result execute nodejs with the password in an environment variable called varName
which is by default PROTECTION KEY
PROTECTION_KEY=yourpassword node dist/server.js
then use your selected method to upload your code to server
then in your env variables add envVarName key (in this case is PROTECTION_KEY) with the value of your password
PROTECTION_KEY=content of your public key