Skip to content

fix(security): move Data Machine private files out of public uploads - #4290

Merged
chubes4 merged 1 commit into
mainfrom
fix/issue-4289-data-machine
Dec 5, 2026
Merged

chubes4 merged 1 commit into
mainfrom
fix/issue-4289-data-machine

Conversation

@chubes4

@chubes4 chubes4 commented Dec 5, 2026

Copy link
Copy Markdown
Member

Summary

Agent, user and network memory, data packets, email attachments and job artifacts used to sit at guessable paths under public uploads. They now resolve through one private root, DirectoryManager::get_private_base_directory(). The default is uploads/datamachine-files/private-<random key>/, protected by an index.php and a deny-all .htaccess. Setting the DATAMACHINE_PRIVATE_FILES_DIR constant moves it anywhere else. A one-time, idempotent migration moves existing files on single-site and multisite (each site's base plus the network base) and leaves nothing at the old paths. Pipeline job media and FileStorage::get_public_url() URLs are unchanged.

What changed

  • DirectoryManager: get_private_base_directory() is now the single resolver for the private root. When DATAMACHINE_PRIVATE_FILES_DIR (an absolute path) is set, it uses that; multisite sites other than the main site get <dir>/sites/<blog_id>. Otherwise it uses public_base/private-<key>. The key is 40 hex characters from random_bytes(), generated once and stored in the network option datamachine_private_files_key. The root gets index.php and a .htaccess with deny rules for both Apache 2.2 and 2.4. The public datamachine-files/ folder gets an index.php so the private folder name can't be listed.
  • Private layers now resolve under the private root: shared/, agents/, users/, the principal directory, network/ (always the main site's private root), data-packets/, email-attachments/ and datamachine-artifacts/jobs/. New helpers: get_public_base_directory(), get_data_packet_store_root(), get_email_attachments_directory() and get_job_artifacts_directory(). pipeline-*, pipeline-direct and standalone stay under the public base.
  • Removed the ad hoc wp_upload_dir()-based private paths from PacketStoreCleanup, FetchEmailAbility, FileCleanup (job artifacts), JobArtifacts and ImageTemplateAbilities. JobArtifacts no longer exposes a public local_debug.url, and its relative_path is now relative to the private root. ImageTemplateAbilities copies files from the private root into public storage instead of handing out private paths.
  • DataPacketStore: refs are now resolved by content hash (validated as 64 hex characters) through the private root. A ref's stored absolute file_path is no longer trusted. Refs saved before the move still load, and a ref can't point at an arbitrary file.
  • New PrivateFilesMigration, one-time and idempotent: - Per-site layers are tracked in the datamachine_private_files_migrated option. users/ and network/ are tracked in the datamachine_private_files_network_migrated network option. - Directories are renamed when possible and otherwise merged file by file. - If a destination file already exists, the private file is kept and the old copy is saved next to it with a .pre-private-migration suffix inside the private root. - On multisite, the request that finishes the network pass also migrates the other sites (skipped on large networks). Any site it misses migrates on its own next request. - It runs from datamachine_maybe_migrate_private_files() on plugins_loaded (priority 5, skipped during install) and on activation.
  • uninstall.php deletes the private root and the new options.
  • Docs: docs/core-system/files-repository.md has a new 'Private Files Root' section. It covers the layer table, default and multisite locations, the DATAMACHINE_PRIVATE_FILES_DIR constant, an nginx deny snippet and how the migration works. Path references are updated in multi-agent-architecture.md, ai-directives.md, wordpress-as-agent-memory.md and api/endpoints/files.md.
  • Compatibility: - There are no readers for the old paths; existing files are migrated and the old locations emptied. - Public pipeline media URLs are unchanged. - JobArtifacts local_debug.url is removed, and relative_path is now relative to the private root. - nginx sites should add the documented deny rule, because nginx ignores .htaccess.
  • Tests: - New tests/private-files-migration-smoke.php covers resolution, protection files, the migration, idempotence and the conflict merge. - New tests/Unit/Core/FilesRepository/PrivateFilesRootTest.php is a PHPUnit test. - Added tests/private-files-stubs.php. - Updated the artifact and packet smokes and the multisite activation fixture for the new paths.

Verification

All gates passed on db3edd14e8c9. Base main: gated on 380d7b495ff3, target now 380d7b495ff3, re-verified: no.

  • ( if git diff --quiet --no-renames 380d7b495ff38a454fae9f385b6586ecc2901492 -- ':(literal)plugins/data-machine'; then :…: passed, 56050 ms; retained gate metadata.
  • php plugins/data-machine/tests/agents-api-memory-store-smoke.php && php plugins/data-machine/tests/agent-memory-store-r…: passed, 201 ms; retained gate evidence.
  • ( if git diff --quiet --no-renames 380d7b495ff38a454fae9f385b6586ecc2901492 -- ':(literal)plugins/data-machine'; then :…: passed, 974 ms; retained gate evidence.
  • repro: none (no repro gate was declared, so these gates do not prove the issue).
  • CI: pending

Gate 2 output
agents-api-memory-store-smoke

[1] Module bootstrap exposes memory contracts without Data Machine product runtime:
  PASS WP_Agent_Memory_Scope is available
  PASS WP_Agent_Memory_Read_Result is available
  PASS WP_Agent_Memory_Write_Result is available
  PASS WP_Agent_Memory_List_Entry is available
  PASS WP_Agent_Memory_Store is available
  PASS DiskAgentMemoryStore is not loaded by agents-api bootstrap

[2] Fake store satisfies the contract shape in isolation:
  PASS missing read returns not-found sentinel
  PASS scope exposes stable key
  PASS write succeeds
  PASS write returns content hash
  PASS exists reflects write
  PASS read sees stored content
  PASS read content round-trips
  PASS read byte count is stable
  PASS compare-and-swap conflict fails
  PASS compare-and-swap conflict code is stable
  PASS compare-and-swap write succeeds with matching hash
  PASS layer list returns scoped entries
  PASS subtree list filters by prefix
  PASS delete succeeds
  PASS exists reflects delete

All 21 Agents API memory assertions passed.
ok 1 - repeated bootstrap registration adds the disk fallback once
ok 2 - canonical resolver returns the disk store with no host store
ok 3 - valid host store takes precedence over the disk fallback
ok 4 - store filter is invoked once for one resolution
ok 5 - store filter receives null as the default candidate
ok 6 - store filter context receives the scope being resolved
ok 7 - interface write succeeds without concrete-store branching
ok 8 - selected store returns content through the interface contract
ok 9 - selected store preserves content through read/write round trip
ok 10 - invalid host candidate cannot displace the disk fallback
ok 11 - previous agents_api_memory_store filter is not mirrored as a runtime alias
ok 12 - old datamachine_memory_store filter is not mirrored as a runtime alias
Agent memory store resolver contract smoke passed.

Gate 3 output
[output truncated; retained tail]
     "plugins/data-machine/tests/fixtures/action-scheduler-multisite-standalone.php",
        "plugins/data-machine/tests/image-template-file-urls-smoke.php",
        "plugins/data-machine/tests/job-artifact-content-resolver-smoke.php",
        "plugins/data-machine/tests/job-artifact-hashable-smoke.php",
        "plugins/data-machine/tests/job-artifact-retention-smoke.php",
        "plugins/data-machine/tests/principal-memory-scope-smoke.php",
        "plugins/data-machine/tests/private-files-migration-smoke.php",
        "plugins/data-machine/tests/private-files-stubs.php",
        "plugins/data-machine/tests/retention-packet-store-smoke.php",
        "plugins/data-machine/uninstall.php"
      ],
      "commands": [],
      "skipped": [
        {
          "reason": "PR branch filters require GitHub event context",
          "source": {
            "job": null,
            "step": null,
            "workflow": ".github/workflows/data-machine-business.yml"
          }
        },
        {
          "reason": "PR branch filters require GitHub event context",
          "source": {
            "job": null,
            "step": null,
            "workflow": ".github/workflows/data-machine-events.yml"
          }
        },
        {
          "reason": "PR branch filters require GitHub event context",
          "source": {
            "job": null,
            "step": null,
            "workflow": ".github/workflows/data-machine-socials.yml"
          }
        },
        {
          "reason": "PR branch filters require GitHub event context",
          "source": {
            "job": null,
            "step": null,
            "workflow": ".github/workflows/data-machine-test.yml"
          }
        },
        {
          "reason": "PR branch filters require GitHub event context",
          "source": {
            "job": null,
            "step": null,
            "workflow": ".github/workflows/data-machine.yml"
          }
        }
      ]
    },
    "results": []
  }
}

Cook run
  • Cook ID: agent-task-4f8bf867-6509-41bb-a397-74918860fa0d
  • Attempt run ID: agent-task-4f8bf867-6509-41bb-a397-74918860fa0d-attempt-1-3cb62ace
  • Gate 1: tree bb05f62f4e69c83aee35e1b6f2d560e1b5c6b16f
  • Gate 2: tree bb05f62f4e69c83aee35e1b6f2d560e1b5c6b16f
  • Gate 3: tree bb05f62f4e69c83aee35e1b6f2d560e1b5c6b16f

Delivery

  • Delivered: An end-to-end check on a real WordPress install (single site, plus multisite if the harness allows):
  • Delivered: Existing tests stay green.

Evidence

  • Limit: homeboy review test and homeboy review lint stopped locally because Homeboy's 'wordpress' extension manifest could not be read. The PHPUnit suite, including the new PrivateFilesRootTest, was therefore not run under PHPUnit here; the controller gates still need to run it. No evidence files were committed.
  • Lint: PHPCS with the Homeboy wordpress-extension ruleset reports 0 errors and 0 warnings on the changed PHP files. PHPStan level 7, with the repo baseline, reports no errors on the changed source files.
  • Memory read and write after migration: - The datamachine/get-agent-memory ability returned the migrated content. - The datamachine/update-agent-memory ability succeeded, and the file landed under the private root. - wp datamachine memory read returned both the old and the new sections. - memory paths showed every layer under private-<key>. - A new data packet was written under the private root. - An old ref that still held the pre-move absolute file_path loaded correctly.
  • Multisite (subdirectory network, 2 sites, network-activated): one main-site request migrated both sites' folders plus the network-level users/ and network/ folders. All three migration options were set and nothing was left at the old paths. Subsite memory and the subsite packet ref both loaded. The subsite root is uploads/sites/2/datamachine-files/private-<shared key>/.
  • PrivateFilesRootTest: its 4 test methods passed against the real single-site install through a small WP_UnitTestCase shim. They were not run under PHPUnit itself.
  • Single-site end-to-end run on WordPress 7.1.4 (SQLite, WP-CLI 2.12). I seeded the old layout with the base-commit plugin: agent and user memory via wp datamachine memory write, a data packet, an email attachment, a job artifact, NETWORK.md and public pipeline media. After switching to this branch, one request moved everything into uploads/datamachine-files/private-<40-hex>/. No agents, users, shared, network, data-packets, email-attachments or datamachine-artifacts folders were left at the old paths.
  • Smoke tests: - The gate smokes pass: agents-api-memory-store-smoke.php and agent-memory-store-resolver-contract-smoke.php. - private-files-migration-smoke.php passes. - In a full loop over the tests/*.php smokes, three fail: cli-global-options, email-multi-inbox-forward and phpunit-file-isolation. All three fail the same way on base 380d7b4.

AI disclosure

Tool: Homeboy Cook. Agent: Homeboy (roadie). Model: roadie · route cook-heavy · served claude-opus-5-5. Cook time: 27m11s.

The AI implemented the private-root resolver, the migration, the call-site changes, the docs and the tests. It also ran the end-to-end verification on disposable single-site and multisite WordPress installs, and linted the changed files with PHPCS and PHPStan, under the direction of the Cook requester. Homeboy reports controller verification separately from this account of the AI's work. A human reviews before merge.

Refs #4289

@chubes4
chubes4 marked this pull request as ready for review December 5, 2026 16:51
@chubes4
chubes4 merged commit 58f64fe into main Dec 5, 2026
30 of 32 checks passed
@chubes4
chubes4 deleted the fix/issue-4289-data-machine branch December 5, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant