Skip to content

fix: supervise external archive decoders and bound staging resources - #186

Merged
vitormattos merged 23 commits into
mainfrom
fix/extract-external-decoder-supervision
Nov 22, 2026
Merged

vitormattos merged 23 commits into
mainfrom
fix/extract-external-decoder-supervision

Conversation

@vitormattos

@vitormattos vitormattos commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Scope

Security re-review and corrective follow-up for #76. This PR builds on the ZIP budget from #179 and archive metadata preflight from #183.

Changes

  • Execute 7za, 7z, and unrar with argument-array proc_open, periodic staging-space/entry checks, and a configurable wall-clock timeout.
  • Drain bounded chunks of stdout and stderr to prevent decoder output pipes from stalling a valid extraction.
  • Replace unbounded exec() archive listings with bounded-memory, deadline-controlled subprocess capture (16 MiB of listing output, 20-second deadline).
  • Inspect archive metadata for symbolic links, hard links, and special filesystem entries before the command-line extraction starts, while preserving valid archive records with empty link fields.
  • Where setsid and POSIX APIs are available, supervise a separate process group and terminate remaining children, including after the immediate decoder exits.
  • Replace unbounded PHP RAR RarEntry::extract() calls with app-controlled streams, cumulative quota checks, per-entry byte limits, a wall-clock deadline between reads, and CRC32 verification.
  • Extend regression coverage for ordinary ZIP/7z/TAR/RAR extraction, oversized archive listings, stdout backpressure, invalid metadata, bounded PHP streams, and descendant process cleanup.

Architecture and testability

  • ExtractionService is now a compact, backwards-compatible facade exposing the same extractZip(), extractRar() and extractOther() signatures.
  • Format-specific work is isolated in ZipExtractor, PhpRarExtractor and CliArchiveExtractor. All preserve the previous parser selection, external commands and application-level errors.
  • ArchiveSafetyValidator centralizes path and special-file validation. ExtractionLimits centralizes configuration and available-space budgets.
  • BoundedStreamCopier is shared by ZIP and PHP RAR to enforce cumulative byte budgets, optional declared-size/deadline checks and CRC32 verification. It retains ZIP overwrite versus RAR exclusive-create behavior.
  • Unit tests exercise these collaborators directly, including unsafe path variants, invalid configuration, compressed-entry quotas, CRC failures and overwrite prevention. Existing controller and browser tests still validate the public facade.
  • ResourceLimitedProcess, DecoderProcess and StagingBudgetInspector remain independently testable; no new runtime dependency, background service or additional filesystem pass per poll was introduced.
  • The refactor changes method calls and object construction, but does not change the streaming chunk size, decoder command line or 50 ms staging-inspection interval. End-to-end performance equivalence remains to be measured on large real archives.

Verification

  • Original security-review reproductions (disposable PHP 8.4 scratch directories) confirmed the unsafe symlink-write window, stdout backpressure, and surviving child-process problem before the fixes.
  • Signed incremental commits; DCO and cryptographic verification confirmed.
  • CI on current head 040d58d8: 26 checks passed, 3 checks intentionally skipped, no failures or pending checks. This includes PHPUnit for PHP 8.3–8.5, Psalm/static analysis, syntax/style checks, release packaging, and Nextcloud 33/34/35 Playwright integration. The tests do not constitute a kernel-level sandbox verification or an integration run with the optional PHP rar extension installed.

Important security limits

This remains best-effort resource supervision, not a strict security sandbox. Polling can allow short-lived resource overruns, and archive metadata checks cannot eliminate every time-of-check/time-of-use window. Isolated process groups depend on host facilities. Deployments processing hostile archives still need operating-system-enforced storage/resource isolation. The PHP rar extension streaming implementation needs integration testing on a system with that extension installed; its C-level read operations cannot be preempted by a PHP deadline during an individual blocking read.

The PR must not be treated as automatically resolving the entire security issue. No issues or releases were changed, and no merge is requested as part of this update.

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
…imits

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
…tion

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
@vitormattos
vitormattos merged commit f43ca97 into main Nov 22, 2026
29 checks passed
@vitormattos
vitormattos deleted the fix/extract-external-decoder-supervision branch November 22, 2026 14:38
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