Skip to content

Commit

Permalink
Ensure consistency between constant and env variable in generated cache
Browse files Browse the repository at this point in the history
  • Loading branch information
amiut committed Feb 23, 2024
1 parent f8cfbf5 commit dc86bff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/Env/WordPressEnvBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,15 +565,17 @@ public function dumpCached(string $file): bool
$symfonyLoaded and $content .= "putenv('SYMFONY_DOTENV_VARS={$symfonyLoaded}');\n\n";

foreach (self::$cache as $key => list($value, $filtered)) {
$slashed = str_replace("'", "\'", $value);
$slashed = addslashes($value);
$preparedValue = $value !== $filtered
? $filtered
: $slashed;

// For defined constants, dump the `define` with filtered value, if any.
if (
in_array($key, $this->definedConstants, true)
|| array_key_exists($key, self::WP_CONSTANTS)
) {
$define = $value !== $filtered
? var_export($filtered, true) // phpcs:ignore
: "'{$slashed}'";
$define = var_export($preparedValue, true); // phpcs:ignore
$content .= "define('{$key}', {$define});\n";
}

Expand All @@ -584,9 +586,10 @@ public function dumpCached(string $file): bool
}

// For env loaded from file, dump the variable definition.
$content .= "putenv('{$key}={$slashed}');\n";
$content .= "\$_ENV['{$key}'] = '{$slashed}';\n";
(strpos($key, 'HTTP_') !== 0) and $content .= "\$_SERVER['{$key}'] = '{$slashed}';\n\n";
$content .= "putenv('{$key}={$preparedValue}');\n";
$content .= "\$_ENV['{$key}'] = '{$preparedValue}';\n";
(strpos($key, 'HTTP_') !== 0)
and $content .= "\$_SERVER['{$key}'] = '{$preparedValue}';\n\n";
}

$content .= sprintf("return %s;\n", var_export(static::$cache, true)); // phpcs:ignore
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ PLUGIN_CONFIG_TWO=2
PLUGIN_CONFIG_THREE=true
PLUGIN_CONFIG_FOUR=4
PLUGIN_CONFIG_FIVE=5
AUTH_COOKIE=BAD&Auth<Cookie
7 changes: 7 additions & 0 deletions tests/integration/Env/WordPressEnvBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function testLoadFile(): void
static::assertSame('xxx_', $bridge->read('DB_TABLE_PREFIX'));
static::assertSame('wp_user', $bridge->read('DB_USER'));
static::assertSame('', $bridge->read('COOKIE_DOMAIN'));
static::assertSame('BAD&amp;Auth', $bridge->read('AUTH_COOKIE'));
}

/**
Expand Down Expand Up @@ -417,6 +418,12 @@ function (): array {
static::assertSame('localhost', $cachedBridge->read('DB_HOST'));
// ...and it should still be able to read things from actual env set after cache was built
static::assertSame('XYZ', $cachedBridge->read('XYZ'));

// Test consitency of defined constansts, getenv, $_ENV and $_SERVER in filtered variables
static::assertSame('BAD&amp;Auth', AUTH_COOKIE);
static::assertSame('BAD&amp;Auth', getenv('AUTH_COOKIE'));
static::assertSame('BAD&amp;Auth', $_ENV['AUTH_COOKIE'] ?? null);
static::assertSame('BAD&amp;Auth', $_SERVER['AUTH_COOKIE'] ?? null);
}

/**
Expand Down

0 comments on commit dc86bff

Please sign in to comment.