← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

PHAR Archives & Packaging

PHP PHP 5.2+ Advanced
debt(d5/e5/b3/t9)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list semgrep and psalm as tools, both specialist static analysis tools. The pattern — user-controlled paths reaching file functions — is not caught by default linters or the compiler; it requires dedicated SAST rules (semgrep patterns for phar:// wrapper misuse) or Psalm taint analysis.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix sounds simple ('never allow user-controlled input in file operations'), but in practice every file function call site that accepts user-supplied paths must be audited and sanitised or the phar:// stream wrapper disabled globally — this is a cross-cutting sweep across potentially many files rather than a single-line patch.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The PHAR concern applies to web and CLI contexts but the burden is largely isolated: developers writing file-handling code must stay aware of stream wrapper risks, but most of the codebase that doesn't touch file operations is unaffected. The ongoing tax is real but contained to file-handling code paths.

t9 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'catastrophic trap — the obvious way is always wrong' (t9). The misconception field captures it precisely: developers believe PHAR is only relevant when intentionally distributing archives. In reality, any file function receiving a user-controlled path starting with phar:// triggers PHAR deserialization — meaning the attack surface is every file operation in the codebase, not just explicit PHAR usage. The 'obvious' assumption (PHAR = packaging tool, not a security concern for my app) is categorically wrong.

About DEBT scoring →

Also Known As

PHP Archive PHAR format PHP phar

TL;DR

PHP's native archive format for self-contained distributable applications — like a JAR file for Java — with a bootstrap stub and compressed contents.

Explanation

A PHAR (PHP Archive) bundles PHP code, assets, and metadata into a single file. The stub bootstraps execution. PHARs are used by Composer (composer.phar), PHPUnit, and many CLI tools — run with php tool.phar, required, or accessed via the phar:// stream wrapper. Creating PHARs requires phar.readonly=0 in php.ini. Critical security note: PHAR metadata is deserialized on any file operation using a phar:// URI — enabling PHAR injection attacks without a direct unserialize() call. Never allow user-controlled input to reach file functions that might accept phar:// paths.

Common Misconception

PHAR files are only relevant when you intentionally distribute PHP as archives. Any file function using a user-controlled path starting with phar:// triggers PHAR deserialization — an attacker who can upload any file and influence a file path can exploit this.

Why It Matters

PHP Archives bundle an entire application into a single distributable file — understanding PHAR is important both for using Composer-distributed tools and for securing file upload validation against PHAR injection.

Common Mistakes

  • Not disabling phar.readonly in php.ini when creating PHARs — required for writing but creates security risk if left on.
  • Using user-controlled paths in file functions without blocking phar:// wrapper — triggers PHAR deserialization.
  • Including dev dependencies in a distributed PHAR — increases file size and attack surface.
  • Not signing PHARs for distributed tools — an unsigned PHAR can be replaced with a malicious one.

Code Examples

✗ Vulnerable
// User path in file function — PHAR injection:
$path = $_GET['file'];
if (file_exists($path)) { // ?file=phar://uploads/evil.jpg
    include $path;          // Deserializes and executes PHAR metadata
}
✓ Fixed
// Create a distributable CLI tool as a PHAR archive
$phar = new Phar('mytool.phar');
$phar->startBuffering();
$phar->buildFromDirectory(__DIR__ . '/src', '/\.php\$/');
$phar->setDefaultStub('bin/mytool.php', 'bin/mytool.php');
$phar->stopBuffering();
chmod('mytool.phar', 0755);

// Run: php mytool.phar --help

// Security:
; php.ini: phar.readonly = On  — prevents modification of PHAR files
// Never pass attacker-controlled paths to phar:// stream wrapper
// Validate PHAR signatures before extracting untrusted archives

Added 15 Mar 2026
Edited 22 Mar 2026
Views 53
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 1 ping W 1 ping T 2 pings F 0 pings S 2 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 2 pings M 0 pings T 0 pings W 0 pings T 2 pings F
Brave Search 1 Applebot 1
No pings yesterday
Amazonbot 10 Scrapy 8 Ahrefs 4 ChatGPT 4 PetalBot 4 Google 3 Perplexity 2 SEMrush 2 Twitter/X 2 Claude 1 Bing 1 Meta AI 1 Majestic 1 Brave Search 1 Applebot 1
crawler 40 crawler_json 5
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
DEV INTEL Tools & Severity
🔴 Critical ⚙ Fix effort: Medium
⚡ Quick Fix
Disable Phar stream wrapper if you don't use it: never allow user-controlled input in any file operation function (file_get_contents, file_exists, require) as phar:// can trigger deserialization
📦 Applies To
PHP 5.2+ web cli
🔗 Prerequisites
🔍 Detection Hints
file_get_contents() or file_exists() with user-supplied path that could be phar://; no stream wrapper restriction; Composer archive operations with user paths
Auto-detectable: ✓ Yes semgrep psalm
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Medium ✗ Manual fix Fix: High Context: Function Tests: Update
CWE-502 CWE-694


✓ schema.org compliant