Skip to content

Bug: safe_mailto() inserts array keys into the generated link unescaped #10816

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?

safe_mailto() builds an obfuscated mailto: link through JavaScript. Attribute values are encoded as |NN sequences, so values aren't directly injectable. Attribute names are concatenated raw:

$x[] = ' ' . $key . '="';

If an application builds the attribute array from user-controlled keys, a key can inject a new attribute or break out of the generated tag. Most applications use literal keys like 'class' => '...', so this is low impact. I'm flagging it mostly for consistency with the other attribute helpers.

Example:

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

echo safe_mailto('a@example.com', 'Click', [$attributeNamePayload => 'value']);

The generated script contains this entry:

l[16] = ' data-x" onmouseover="alert(document.domain)" data-y=="';

When the script runs, it calls document.write(unescape(...)) for that entry and writes the raw key into the link. The double quote closes data-x and the browser parses onmouseover as a new attribute.

Steps to Reproduce

I left a runnable script at bug-reports/poc/f05-safe-mailto-attribute-name-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/f05-safe-mailto-attribute-name-injection.php

To check it in a browser, save the output to /tmp/f05.html and open it. The generated script writes the link into the page, and hovering it runs the handler. If the page has a CSP that blocks inline scripts, allow the local test page or temporarily disable CSP for the check.

Expected Output

Attribute names should be checked against a safe pattern, and invalid names should be skipped or rejected. For example:

^[a-zA-Z_:][-a-zA-Z0-9_:.]*$

With that check, the payload key would be skipped. If raw attribute strings are intentional, the docs should state that the string form and array keys are inserted unescaped and must not contain user input.

Anything else?

Validate the key before adding it:

foreach ($attributes as $key => $val) {
if (preg_match('/^[a-zA-Z_:][-a-zA-Z0-9_:.]*$/', (string) $key) !== 1) {
continue;
}

$x[] = ' ' . $key . '="';

for ($i = 0, $l = strlen($val); $i < $l; $i++) {
    $x[] = '|' . ord($val[$i]);
}

$x[] = '"';

}

I'd use the same validator in stringify_attributes() and the other helpers that accept attribute arrays. A test in the URL helper tests with the payload above would cover the regression.

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