Skip to content

Fix PSR-18 client memory management & harden UploadedFile::moveTo() security#10

Merged
yordadev merged 1 commit into
mainfrom
patch-main-psr18-client-memory-fixes
Jan 16, 2026
Merged

Fix PSR-18 client memory management & harden UploadedFile::moveTo() security#10
yordadev merged 1 commit into
mainfrom
patch-main-psr18-client-memory-fixes

Conversation

@yordadev
Copy link
Copy Markdown
Contributor

This PR addresses two critical areas in the signalforge_http extension:

1. PSR-18 HTTP Client Memory Management Fix

Converted all standard C memory functions to Zend memory management equivalents across the entire client subsystem:

Standard C Zend Equivalent
malloc() emalloc()
calloc() ecalloc()
realloc() erealloc()
free() efree()
strdup() estrdup()

Why this matters:

  • PHP extensions must use Zend allocators for proper memory manager integration
  • Enables request-scoped automatic cleanup (prevents leaks between requests)
  • Respects PHP's memory_limit setting
  • Allows leak detection in debug/valgrind builds
  • Required for production stability in long-running processes (FPM, Swoole, etc.)

2. UploadedFile::moveTo() Security Hardening

Strengthened path validation in moveTo() to prevent several attack vectors:

Attack Vector Mitigation
Null byte injection memchr() check rejects embedded \0
Relative path confusion Require absolute paths (must start with /)
Directory traversal Detect /../ sequences (not just .. substring)
Symlink attacks realpath() resolves symlinks before validation
Missing directory realpath() verifies directory exists

Before: Simple strstr(path, "..") check (false positives on file..name.txt)
After: Proper /../ sequence detection + canonical path resolution

…ecurity

Memory Management:
- Convert malloc/free/strdup/realloc to Zend equivalents (emalloc/efree/estrdup/erealloc)
- Affects: client.c, curl_easy.c, curl_multi_pool.c, curl_worker.c, request_data.c, response_data.c
- Ensures proper PHP memory manager integration and request-scoped cleanup
- Fixes potential memory tracking issues under PHP's memory_limit

Security Hardening (UploadedFile::moveTo):
- Add null byte injection prevention (path truncation attack)
- Require absolute paths to prevent ambiguity
- Improve traversal detection: check for /../ sequences, not just ".." substring
- Add realpath() validation to resolve symlinks and verify directory exists
- Handle root directory paths (/filename) correctly
@yordadev yordadev changed the title Fix PSR-18 client memory management & harden UploadedFile::moveTo() s… Fix PSR-18 client memory management & harden UploadedFile::moveTo() security Jan 16, 2026
@yordadev yordadev merged commit dcbd5ad into main Jan 16, 2026
4 checks passed
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