Skip to content

Latest commit

 

History

History
74 lines (60 loc) · 6.87 KB

File metadata and controls

74 lines (60 loc) · 6.87 KB

Mitigation Strategies Analysis for vlucas/phpdotenv

  • Description:

    1. Use createImmutable(): In your PHP code, always use Dotenv\Dotenv::createImmutable() instead of Dotenv\Dotenv::createMutable() or Dotenv\Dotenv::create(). This is the core of this mitigation.

    2. Specify Path: Provide the correct, secure path to the .env file to the createImmutable() method. Example:

      $dotenv = Dotenv\Dotenv::createImmutable('/var/www/config'); // Secure directory
      $dotenv->load();
    3. Understand Precedence: Be explicitly aware that with createImmutable(), existing environment variables (set at the system level, before your PHP script runs) will not be overwritten by values in the .env file. This is a crucial security feature.

    4. Avoid overload(): Do not use the $dotenv->overload() method. This method forces overwriting of existing environment variables, which defeats the purpose of immutability and can introduce security risks if system-level variables were set for a reason. Only use overload() if you have an extremely specific and well-understood reason to do so, and document it thoroughly.

    5. Consider safeLoad(): If you want to load the environment variables but don't want to throw an exception if the .env file is missing, use $dotenv->safeLoad() instead of $dotenv->load(). This is useful in situations where the .env file is optional. However, be very careful about how you handle missing variables if you use this.

  • Threats Mitigated:

    • Threat: Accidental Overwriting of System Environment Variables (Severity: Medium). createImmutable() prevents .env from overriding critical system-level settings, which might be set for security or operational reasons. This is the primary threat this mitigation addresses.
    • Threat: Configuration Errors (Severity: Low). Using createImmutable() makes the behavior of environment variable loading more predictable and less prone to subtle errors caused by unexpected overwrites.
    • Threat: Unexpected application behavior due to missing .env file (Severity: Low). safeLoad() prevents throwing an exception.
  • Impact:

    • Accidental Overwriting: Risk reduced to Zero. createImmutable() guarantees that existing variables won't be overwritten.
    • Configuration Errors: Risk reduced. The behavior is more deterministic and easier to reason about.
    • Unexpected application behavior: Risk reduced. Application will not crash.
  • Currently Implemented:

    • Example: All instances of Dotenv::create() and Dotenv::createMutable() have been replaced with Dotenv::createImmutable() in index.php and config.php. safeLoad() is used in optional_config.php.
  • Missing Implementation:

    • Example: Need to double-check all code that uses environment variables to ensure that the developers understand the precedence rules (system variables always win when using createImmutable()). A code review focused on this aspect is needed.
  • Description:

    1. Use required(): After loading the .env file (using createImmutable() and load()), use the $dotenv->required() method to assert that specific environment variables are present and, optionally, that they meet certain criteria.
    2. Basic Presence Check:
      $dotenv->required('DATABASE_HOST'); // Ensures DATABASE_HOST is set
      $dotenv->required(['DATABASE_USER', 'DATABASE_PASSWORD']); // Multiple variables
    3. Type and Value Validation: Use the fluent interface provided by required() to add validation rules:
      $dotenv->required('CACHE_ENABLED')->isBoolean(); // Must be 'true' or 'false'
      $dotenv->required('PORT')->isInteger(); // Must be an integer
      $dotenv->required('ALLOWED_IPS')->notEmpty(); // Must not be empty
      $dotenv->required('MODE')->allowedValues(['development', 'production', 'testing']); //Limited set of values
    4. Combine checks:
      $dotenv->required('TIMEOUT')->isInteger()->notEmpty();
    5. Handle Validation Failures: If any of the required() checks fail, phpdotenv will throw an exception (Dotenv\Exception\ValidationException). You must handle this exception appropriately (see previous strategy on general file validation for error handling ideas). A try-catch block is essential.
    6. Use ifPresent(): If some variables are optional, you can use the ifPresent() method to apply validation rules only if the variable is present in the .env file.
      $dotenv->ifPresent('OPTIONAL_SETTING')->isInteger();
  • Threats Mitigated:

    • Threat: Application Errors Due to Missing or Invalid Environment Variables (Severity: Medium). required() ensures that the application doesn't proceed with missing or incorrectly formatted configuration, preventing unexpected behavior or crashes.
    • Threat: Use of Default Values When .env is Misconfigured (Severity: Medium). By explicitly requiring variables, you avoid situations where the application silently falls back to potentially insecure default values because a variable was misspelled or omitted in the .env file.
    • Threat: Type Mismatches (Severity: Low to Medium). Using type validation (e.g., isInteger(), isBoolean()) prevents errors caused by using a string where an integer is expected, or vice-versa.
  • Impact:

    • Application Errors: Risk significantly reduced. The application will halt with an informative error if required variables are missing or invalid.
    • Use of Default Values: Risk reduced. Forces explicit configuration.
    • Type Mismatches: Risk reduced. Ensures variables are of the expected type.
  • Currently Implemented:

    • Example: $dotenv->required() is used in config.php to check for DATABASE_HOST, DATABASE_USER, and DATABASE_PASSWORD. Basic presence checks are in place.
  • Missing Implementation:

    • Example: Need to add type validation to the existing required() calls (e.g., $dotenv->required('DATABASE_PORT')->isInteger()). Also, need to implement required() checks for all environment variables used by the application, not just the database credentials. A comprehensive review of all environment variable usage is required. Add try-catch block to handle Dotenv\Exception\ValidationException.