Skip to content

Bug: img() and script_tag() insert array values into the tag unescaped #10813

Open

Description

@BL4CK570RM

PHP Version

8.4

CodeIgniter4 Version

4.7.7-dev (commit 08039be). I also checked 4.7.6 and the same code is there.

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

Linux

Which server did you use?

cli

Environment

development

Database

Not required for the CLI reproduction.

What happened?

img() and script_tag() concatenate the values from the $src array directly into the tag. img() escapes its second $attributes parameter through stringify_attributes(), but the first array is raw. script_tag() doesn't escape anything.

The docs say the array form gives "complete control over all attributes and values", so maybe raw output is intentional. I'm not sure. I'm reporting it because the behavior is inconsistent with the other helpers and there's no warning about passing user data into the array.

Example:

$payload = 'x" onmouseover="alert(document.domain)" data-x="';

echo img(['src' => 'https://example.com/a.png', 'alt' => $payload]);

Output:

x

The same thing happens with a relative source:

echo img('a.png' . $payload);

script_tag() behaves the same way:

echo script_tag(['src' => 'https://example.com/a.js', 'data-x' => $payload]);

<script src="https://example.com/a.js" data-x="x" onmouseover="alert(document.domain)" data-x=""></script>

Steps to Reproduce

I left a runnable script at bug-reports/poc/f02-img-script-tag-attribute-injection.php. It loads the real helper and stubs the few framework functions it needs, so no Composer install is required.

From the project root:

php bug-reports/poc/f02-img-script-tag-attribute-injection.php

To check it in a browser, save the output to /tmp/f02.html and open it. Hover the image.

Expected Output

If the values are meant to be escaped:

x" onmouseover="alert(document.domain)" data-x="

<script src="https://example.com/a.js" data-x="x" onmouseover="alert(document.domain)" data-x=""></script>

If raw HTML is the intended contract, the docs should say that clearly and warn callers to escape every value themselves.

Anything else?

Escaping the values in img():

foreach ($src as $key => $value) {
$img .= ' ' . $key . '="' . esc($value) . '"';
}

And in script_tag():

foreach ($src as $k => $v) {
if ($k === 'src' && preg_match('#^([a-z]+:)?//#i', $v) !== 1) {
if ($indexPage) {
$script .= 'src="' . esc(site_url($v)) . '" ';
} else {
$script .= 'src="' . esc(slash_item('baseURL') . $v) . '" ';
}
} else {
$script .= $k . (null === $v ? ' ' : '="' . esc($v) . '" ');
}
}

I'd also validate attribute names with something like /^[a-zA-Z_:][-a-zA-Z0-9_:.]*$/. A test in tests/system/Helpers/HTMLHelperTest.php with a quote payload would cover both helpers.

Activity

  1. added
    bug
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions