npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-sensitive-env

v3.0.1

Published

ESLint plugin to prevent sensitive values from being hardcoded in the codebase

Downloads

278

Readme

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

  • Detects hardcoded sensitive values based on .env files.
  • Supports .env files to define environment variables.
  • Allows configuration of environment files and control over which keys and values are checked.
  • Ignores specific keys or values when configured.
  • Predefined non-sensitive values (e.g., 'false', 'null', 'true') are automatically excluded from checks.

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:

  • envFile (optional): The path to the environment file where sensitive values are stored.

    • If no file is provided, the plugin will search for one of the following files:
      [
        ".env.production",
        ".env.development",
        ".env.local",
        ".env",
        ".env.local.example",
        ".env.example"
      ]
  • ignore (optional): An array of uppercase strings representing the environment variable names (keys) to ignore.

    • The rule will not flag hardcoded values of ignored keys.
  • noSensitiveValues (optional): An array of strings representing specific values to ignore as non-sensitive.

    • The rule will not flag these values even if they match a key from the environment file.
    • By default, the following values are ignored:
      [
        "false",
        "null",
        "true",
        "undefined",
        "unknown",
        "nan",
        "infinity",
        "-infinity",
        "1234567890",
        "9876543210"
      ]
    • Additionally, dates in string format (e.g., 2024-10-20 or 10/20/2024) are not considered sensitive. Numerical representations of dates (e.g., 1729464561272) are allowed.
    • URLs defined in environment files are checked based on the hostname to determine if they contain sensitive information.
    • Values with 4 or fewer characters are not considered sensitive.

Example Configuration

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

In this configuration:

  • .env is used as the environment file.
  • The rule will ignore any hard-coded value for the key that contains PUBLIC_LOCALHOST.
  • The value myPublicValue will not be flagged as sensitive, regardless of where it appears.

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.