JairTorres1003 / eslint-plugin-sensitive-env

ESLint plugin to prevent sensitive values from being hardcoded in the codebase
https://www.npmjs.com/package/eslint-plugin-sensitive-env
MIT License
1 stars 0 forks source link
environment eslint eslint-plugin npm secrets security sensitive

eslint-plugin-sensitive-env npm

An ESLint plugin designed to prevent hardcoded sensitive values in your code. This plugin ensures that sensitive values, such as API keys, tokens, passwords, and other environment-specific data, are stored in environment variables instead of being hardcoded into the source code.

Features

Installation

To install the plugin, run the following command:

npm install eslint-plugin-sensitive-env --save-dev

or using yarn:

yarn add eslint-plugin-sensitive-env --dev

Usage

Add the plugin to your ESLint configuration:

{
  "plugins": ["sensitive-env"],
  "rules": {
    "sensitive-env/no-hardcoded-values": "error"
  }
}

Rule Options

The no-hardcoded-values rule provides flexible configuration options:

Example Configuration

{
  "rules": {
    "sensitive-env/no-hardcoded-values": [
      "error",
      {
        "envFile": ".env",
        "ignore": ["PUBLIC_LOCALHOST"],
        "noSensitiveValues": ["myPublicValue"]
      }
    ]
  }
}

In this configuration:

Rule Details

The no-hardcoded-values rule checks for sensitive values that should be stored in environment variables instead of being hardcoded. It works by reading an environment file (e.g., .env) and matching values defined by the specified options.

If the environment file does not exist or cannot be found, the rule will produce a warning with the message:

The environment file <envFile> does not exist.

If a hardcoded sensitive value is found, the following error message will be reported:

Do not hardcode sensitive values. Use environment variables instead.

Ignoring Specific Keys and Values

You can customize the behavior of the plugin by defining which keys and values to ignore.

Example: Ignoring Specific Keys

{
  "rules": {
    "sensitive-env/no-hardcoded-values": [
      "error",
      {
        "ignore": ["PASSWORD", "SECRET"]
      }
    ]
  }
}

In this case, values for PASSWORD and SECRET will be ignored, but other keys will still be checked.

Example: Ignoring Specific Values

{
  "rules": {
    "sensitive-env/no-hardcoded-values": [
      "error",
      {
        "noSensitiveValues": ["myPublicValue", "someOtherSafeValue"]
      }
    ]
  }
}

Here, myPublicValue and someOtherSafeValue will not be flagged, even if they appear as hardcoded values.

Testing

To run the tests for this plugin:

npm test

Contributing

Contributions, issues, and feature requests are welcome! Feel free to check out the issues page if you have suggestions or encounter problems.

License

This project is licensed under the MIT License. See the LICENSE file for details.