Repository navigation
Closed
Labels
CodeIgniter4Issue relates to the conversion to CodeIgniter 4enhancementphpPull requests that update Php codetechnical debt
Description
Summary
CodeRabbit review of PR #4656 flags that tests/helpers/security_helperTest.php writes to the repository's real filesystem paths — ROOTPATH/.env, ROOTPATH/.env.lock, and WRITEPATH/backup/.env.bak — rather than isolated, test-specific paths.
This is a known limitation documented in the test file's header comment: ROOTPATH / WRITEPATH are CI4 path constants that are resolved once at bootstrap and cannot be redirected per test, so the filesystem-touching helpers are exercised against the actual project .env.
Why it matters
- Parallel / multi-process test runs can interleave writes. If any test runner or developer workflow runs the suite concurrently (e.g. a future
--processes/max-workersconfiguration, or local development where a live app is running next to a test), reads of.envin one test can be clobbered by writes in another. tearDown()restore is best-effort only. It restores state on normal completion but cannot protect against a test that callsexit()/hard process termination, or against a crash mid-write. A failed test can leave.envin a partial/rotated state.- Test order sensitivity. Tests that rotate the encryption key or write
throttle.keydepend on the.envcaptured insetUp(); any external change between capture and restore defeats the safeguard.
Proposed approach (either/or)
CodeRabbit suggests one of:
- Thread test-specific paths through the helper interfaces. Add
?string $configPath = null,?string $backupPath = null(etc.) parameters to the helpers that touch the filesystem (checkEncryption,checkThrottleEncryption,rotateEncryptionKey,provisionThrottleKey,abortEncryptionConversion,removeBackup,backupEnvFile,writeEnvKey,atomicWriteFile,lockEnvFile), defaulting to the currentROOTPATH/WRITEPATHvalues so existing callers don't change. Tests then pass asys_get_temp_dir()-based path and clean up intearDown(). - Run the affected tests in a process-isolated application copy. Use PHPUnit's
runInSeparateProcess(with asys_get_temp_dir()sandbox) or a dedicated CI job that copies the repo to temp before running this test class.
Option 1 is the more explicit and testable approach and keeps the CI job structure unchanged.
Out of scope
- Changing
config('Encryption')key handling (already covered per-test bysetUp()/tearDown()). - The
throttle.keyenv-var shuffling (already covered by the same per-testrestoreThrottleKey()helper).
Acceptance criteria
-
security_helperTest.phpno longer writes toROOTPATH/.env,ROOTPATH/.env.lock, orWRITEPATH/backup/. - A
sys_get_temp_dir()-based sandbox (or an isolated process) is used for all filesystem-touching tests. -
tearDown()removes the sandbox; no stray temp files remain. - A parallel-run smoke test (PHPUnit
--processes 2) passes with the modified helper signatures. - All existing assertions remain valid (no behaviour change to
checkEncryption,checkThrottleEncryption,rotateEncryptionKey,provisionThrottleKey,abortEncryptionConversion, orremoveBackup).
References
- PR: fix(security): handle special characters in
.envkey values and improve insertion logic #4656 - CodeRabbit re-review comment (item 7): fix(security): handle special characters in
.envkey values and improve insertion logic #4656 (comment) - Test file:
tests/helpers/security_helperTest.php - Helper signatures:
app/Helpers/security_helper.php
Metadata
Metadata
Assignees
Labels
CodeIgniter4Issue relates to the conversion to CodeIgniter 4enhancementphpPull requests that update Php codetechnical debt
Activity
jekkos commentedon Sep 15, 2026
Plan drafted and a proposal posted: #4656 (comment)
Chosen approach (pending objecttothis sign-off):
Implementation is staged locally but not committed/pushed until objecttothis signs off.
objecttothis commentedon Sep 15, 2026
It looks like a sound approach to me. I'd be interested in knowing if @coderabbitai sees any potential drawbacks.
coderabbitai commentedon Sep 15, 2026
🧩 Analysis chain
🏁 Script executed:
Length of output: 25665
🏁 Script executed:
Length of output: 23177
@objecttothis, the approach is sound.Config\SecurityEnvprovides a small, testable seam without changing the helper API.I see three items to specify before implementation:
Derive the backup directory from
backupPath.checkEncryption()currently createsWRITEPATH . '/backup'. After the change, it should usedirname($securityEnv->backupPath). Otherwise, a redirectedbackupPathcan still depend on or create the realWRITEPATH/backupdirectory.Define the
.env.examplebehavior for a redirectedenvPath.The proposal keeps
ROOTPATH . '.env.example'. This is safe for the default production paths. It is not fully sandboxed when a test deletes its sandbox.envto test initialization.Either add
examplePathtoSecurityEnv, or derive it fromdirname($envPath). The test can then seed a sandbox.env.examplewhen it tests the copy path. If the project must always use the shipped example file, document that as an explicit exception.Restore configuration state even when setup fails.
config('SecurityEnv')is shared mutable state.tearDown()should restore all three properties before it removes the sandbox. The test should also avoid leaving the shared configuration redirected if setup fails after mutation. A small setup guard ortry/finallycleanup path can cover this case.The unique
sys_get_temp_dir()sandbox prevents collisions between separate PHPUnit processes. It does not make the sharedSecurityEnvinstance safe for concurrent tests in the same PHP process. PHPUnit normally runs tests sequentially within a worker, so this is acceptable if these tests do not use in-process concurrency.The
.envpre-seed is appropriate for most tests. Add at least one test that removes the sandbox.envand verifies the intended initialization behavior.You are interacting with an AI system.
test(security): isolate helper FS tests via Config\SecurityEnv
objecttothis commentedon Sep 15, 2026
@jekkos let's move on it the way you've described.
fix(security): handle special characters in `.env` key values and imp…