Add fallback for allowedHostnames environment variable#4565
Add fallback for allowedHostnames environment variable#4565objecttothis wants to merge 2 commits into
Conversation
- In some cases allowedHostnames is set in env but not loaded at the time of check, yet available in other sources. This adds fallback checks. - Add UnitTest Signed-off-by: objec <objecttothis@gmail.com>
📝 WalkthroughWalkthroughApp config now reads allowed hostnames through a centralized getEnvString helper (checking env(), $_ENV, $_SERVER, getenv) and prefers ChangesAllowed Hostnames Configuration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Signed-off-by: objec <objecttothis@gmail.com>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
app/Config/App.php (1)
359-373: ⚡ Quick winDocument why the second lookup is load-bearing (not redundant).
In CodeIgniter 4,
env()reads$_ENV[$key] ?? $_SERVER[$key] ?? getenv($key)and then converts the magic words'true'/'false'/'empty'/'null'viamatch(strtolower($value))(returning booleans,'', ornull). SincegetEnvString()only returns non-empty strings, literal values like"false"/"null"would otherwise be type-juggled away—so the raw fallback must stay to preserve the original literal string.📝 Suggested clarifying comment
private function getEnvString(string $key): ?string { + // env() type-juggles the magic words "true"/"false"/"empty"/"null" + // into non-string values, so when it returns a non-string we re-read + // the raw value to preserve a legitimate literal hostname string. $value = env($key); if (is_string($value) && trim($value) !== '') { return $value; } $raw = $_ENV[$key] ?? $_SERVER[$key] ?? getenv($key); if (is_string($raw) && trim($raw) !== '') { return $raw; } return null; }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@app/Config/App.php` around lines 359 - 373, Add a clarifying comment inside the getEnvString method explaining that the second/raw lookup (using $_ENV[$key] ?? $_SERVER[$key] ?? getenv($key)) is intentionally required because env() normalizes magic words (e.g., "true"/"false"/"empty"/"null") into booleans, empty string or null, which would drop literal string values like "false" or "null"; the raw lookup preserves the original literal string for return when getEnvString must only return non-empty strings. This comment should be placed immediately before the $raw = $_ENV... line in getEnvString to document why the fallback is not redundant.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@app/Config/App.php`:
- Around line 359-373: Add a clarifying comment inside the getEnvString method
explaining that the second/raw lookup (using $_ENV[$key] ?? $_SERVER[$key] ??
getenv($key)) is intentionally required because env() normalizes magic words
(e.g., "true"/"false"/"empty"/"null") into booleans, empty string or null, which
would drop literal string values like "false" or "null"; the raw lookup
preserves the original literal string for return when getEnvString must only
return non-empty strings. This comment should be placed immediately before the
$raw = $_ENV... line in getEnvString to document why the fallback is not
redundant.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: f0ae257b-05b9-4f1b-b2e1-611aebd67150
📒 Files selected for processing (2)
app/Config/App.phptests/Config/AppTest.php
Bug: getenv('app.allowedHostnames') returns false intermittently even though .env has it set.
Key insight from DotEnv.php line 97:
if (getenv($name, true) === false) {
putenv("{$name}={$value}");
}
Guard skips putenv if value already exists. But App.php line 292 uses getenv('app.allowedHostnames') without true flag. Different lookup path.
More importantly — App.php reads getenv() but never checks $_ENV or $_SERVER. DotEnv always sets those. So fix: make App.php check $_ENV/$_SERVER too.
Let me check one more thing to confirm the intermittency source.
Run Command
Confirm getenv misses $_ENV/$_SERVER values
Confirmed. When putenv is skipped (guard on line 97), getenv() returns false — even though $_ENV and $_SERVER have value.
Fix: App.php constructor should check $_ENV/$_SERVER as fallbacks, not rely solely on getenv().
Now writing plan.
Write File
i-got-this-error-drifting-riddle.md
CONTENT
Fix: allowedHostnames intermittently not read from .env
Context
Error log shows
allowedHostnames is not configuredeven though.envhasapp.allowedHostnames = 'localhost'. Happens intermittently.Root cause: CI4's DotEnv
setVariable()has a guard (line 97) that skipsputenv()ifgetenv($name, true)already returns a value. Whenputenvis skipped,getenv()(withouttrue) returnsfalse— it can't see values only in$_ENV/$_SERVER. But DotEnv always sets$_ENVand$_SERVER.App.php constructor (line 290-292) only checks
getenv(), never$_ENV/$_SERVER. So when theputenvguard triggers,getenv('app.allowedHostnames')returnsfalseand allowedHostnames stays empty.Fix
File:
app/Config/App.php(lines 290-293)Change the env reading to also check
$_ENVand$_SERVERas fallbacks, matching how CI4's own DotEnv stores values.Replace:
With:
Verification
vendor/bin/phpunit tests/Config/AppTest.phpTool Search
QUERY
select:ExitPlanMode
MAX_RESULTS
1
Root cause confirmed: getenv() returns false when putenv was skipped by DotEnv's guard, even though $_ENV/$_SERVER have the value. Intermittent because it depends on whether a previous request in same PHP worker already called putenv for that key.
Fix: check $_ENV/$_SERVER as fallbacks in App.php constructor.
Summary by CodeRabbit