Skip to content

Isolate PHPUnit encryption bootstrap from persistent configuration and conversion #4775

Open

Description

@coderabbitai

Summary

Remove persistent encryption provisioning/conversion from the PHPUnit bootstrap. Initialize a valid test-only encryption key in memory instead, and keep provisioning/conversion coverage in isolated tests.

Requested by @objecttothis as a follow-up outside PR #4694. Leave this issue unassigned.

Rationale

Production releases exclude tests, and Config\Database selects the tests database group under the testing environment. This is primarily a developer-checkout and CI isolation concern, not a claim that normal test runs modify a separate production database.

However, tests/bootstrap.php calls checkEncryption() before test setUp() runs:

  • With an absent key, the helper provisions a key and persists it to the checkout's .env.
  • With a nonempty key shorter than 64 characters, the helper reads legacy database secrets and can rotate the .env key and persist converted secrets.
  • Config\SecurityEnv defaults to the checkout's .env, even when the test database is separate.
  • Security-helper tests redirect filesystem paths only in setUp(). Database reset/seeding also happens after bootstrap.
  • A short key can therefore require an available, initialized test database before test setup. A missing key or short key can also require writable checkout configuration.

Resetting the test database later does not undo a bootstrap write to .env. If a developer checkout uses that key for another database, changing the shared key can leave that database's ciphertext out of sync.

Required changes

  • Replace the bootstrap call to checkEncryption() with in-memory test-key initialization before tests use the encrypter.
  • Do not persist the test key or run automatic legacy conversion during general test startup.
  • Preserve explicit provisioning and conversion tests using isolated filesystem paths and mocked or dedicated database state.
  • Account for cached encrypter/config instances so tests use the intended test key.
  • Keep runtime provisioning and CI3-to-CI4 migration behavior unchanged.

Affected areas

  • tests/bootstrap.php
  • tests/Helpers/security_helperTest.php
  • app/Helpers/security_helper.php (behavior exercised, not necessarily requiring changes)
  • app/Config/SecurityEnv.php and app/Config/Database.php (isolation boundaries)

Acceptance criteria

  • Starting a focused database-free unit test with an absent or short checkout key does not modify the checkout's .env, backup, or lock files.
  • General bootstrap does not initiate legacy-secret database reads or conversion.
  • Focused database-free tests do not need an initialized database solely for encryption bootstrap.
  • Tests that need encryption receive a valid in-memory test key.
  • Sandboxed provisioning/conversion tests and database-backed tests continue to pass.

References

Analysis is based on source inspection; no repository code was executed.

Activity

  1. objecttothis commented on Oct 24, 2026

    @objecttothis
    Member

    Claude Issue Checker Agent (cc @objecttothis)

    Investigated against current master.

    Status: Premise doesn't match current master — the described file/flow doesn't exist there.

    • There is no tests/bootstrap.php in the repository. phpunit.xml.dist uses bootstrap="vendor/codeigniter4/framework/system/Test/bootstrap.php" (the framework's own bootstrap), not a custom OSPOS one.
    • checkEncryption() (app/Helpers/security_helper.php) is only invoked from runtime code paths — Config::__construct()/Config::postSaveInfo(), Email_lib, and the 20220127000000_convertToCI4 migration — not from any general PHPUnit bootstrap/startup hook.
    • tests/Helpers/security_helperTest.php already does exactly what this issue's acceptance criteria ask for: it redirects config('SecurityEnv')->envPath/backupPath/lockPath to a unique per-run temp sandbox in setUp()/tearDown(), so its checkEncryption()/rotateEncryptionKey()/conversion tests never touch the real checkout .env, and DB-backed conversion tests use a fake Appconfig rather than a real database.

    So the specific race this issue describes (a shared tests/bootstrap.php calling checkEncryption() globally before setUp(), risking persisted .env writes for unrelated focused/unit tests) isn't present on master as far as I can find — there's no such bootstrap file, and the one test file that does exercise checkEncryption() is already fully sandboxed. The referenced PR #4694 (still open) doesn't appear to introduce this bootstrap call either. I'd flag this to a maintainer to confirm whether the issue was filed against a different/future state of the code, since I can't locate the problematic code path on current master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions