Skip to content

fix: env() TypeError for non-string $_SERVER values + esc() fixes#10305

Open
gr8man wants to merge 6 commits into
codeigniter4:developfrom
gr8man:fix/common-env-typeerror-esc-leak
Open

fix: env() TypeError for non-string $_SERVER values + esc() fixes#10305
gr8man wants to merge 6 commits into
codeigniter4:developfrom
gr8man:fix/common-env-typeerror-esc-leak

Conversation

@gr8man

@gr8man gr8man commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Fix: env() TypeError in CLI + esc() improvements

What

Fixes two bugs in system/Common.php.

env() — In CLI, $_SERVER contains non-string values (argc is int, argv is array). Passing them to strtolower() threw a TypeError under declare(strict_types=1). Added an is_string() guard before the match expression; non-string values are returned as-is.

esc() — Three fixes applied together:

  • foreach ($data as &$value) left a dangling reference after the loop; added unset($value) to prevent silent mutation of the last array element in the calling scope
  • $encoding was not propagated in recursive array calls
  • Replaced single static $escaper with static $escapers[] keyed by encoding for correct per-encoding caching

Tests

Added a data-provider test for env() covering non-string $_SERVER/$_ENV values (int, array, float), and three tests verifying the esc() reference leak is gone.

Files changed

  • system/Common.php
  • tests/system/CommonFunctionsTest.php

- env(): guard non-string values (int argc, array argv in CLI) before
  strtolower() to prevent TypeError under declare(strict_types=1)

- esc(): propagate $encoding in recursive array calls (was ignored before),
  add early return after array processing, replace single static $escaper
  with static $escapers[] cache keyed by encoding

- tests: data-provider test for env() non-string types,
  three tests for esc() foreach reference leak
@gr8man gr8man force-pushed the fix/common-env-typeerror-esc-leak branch from 53a365d to 449dbae Compare June 11, 2026 21:43

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please send one change per PR.

This needs a changelog entry.

Comment thread system/Common.php Outdated
Comment thread system/Common.php Outdated
@michalsn michalsn added the bug Verified issues on the current code behavior or pull requests that will fix them label Jun 12, 2026
Comment thread tests/system/CommonFunctionsTest.php Outdated
gr8man and others added 4 commits June 12, 2026 18:28
Co-authored-by: Michal Sniatala <michal@sniatala.pl>
Co-authored-by: Michal Sniatala <michal@sniatala.pl>
Co-authored-by: Michal Sniatala <michal@sniatala.pl>
@michalsn

Copy link
Copy Markdown
Member

Apart from the changelog entry, we also need some tests to prove the fixed esc() behavior (with changing $encoding) is actually working.

@gr8man gr8man requested a review from michalsn June 12, 2026 17:43

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need two things - see my previous comment.

@gr8man gr8man requested a review from michalsn June 12, 2026 20:01
Comment on lines +323 to +330
public function testEscapeWithChangingEncodingRecursive(): void
{
$data = ['<x'];

$this->assertSame(['&lt;x'], esc($data, 'html', 'utf-8'));
$this->assertSame(['&lt;x'], esc($data, 'html', 'iso-8859-1'));
$this->assertSame(['&lt;x'], esc($data, 'html', 'utf-8'));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use data that will allow us to see the difference and make sure this is working as expected.

Suggested change
public function testEscapeWithChangingEncodingRecursive(): void
{
$data = ['<x'];
$this->assertSame(['&lt;x'], esc($data, 'html', 'utf-8'));
$this->assertSame(['&lt;x'], esc($data, 'html', 'iso-8859-1'));
$this->assertSame(['&lt;x'], esc($data, 'html', 'utf-8'));
}
public function testEscapeWithChangingArrayEncoding(): void
{
$data = [hex2bin('E9')];
$this->assertSame(['&#xE9;'], esc($data, 'attr', 'iso-8859-1'));
$this->assertSame(['&#x0439;'], esc($data, 'attr', 'windows-1251'));
$this->assertSame(['&#xE9;'], esc($data, 'attr', 'iso-8859-1'));
}

Comment thread CHANGELOG.md

### Fixed Bugs

* fix: env() TypeError for non-string values and esc() encoding propagation with reference leak prevention

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the changelog in the user guide, just like you did in other PRs.
https://github.com/codeigniter4/CodeIgniter4/blob/develop/user_guide_src/source/changelogs/v4.7.4.rst

Also, please make it more accurate to the current PR state.

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

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants