Skip to content

Bug: anchor(), anchor_popup() and mailto() insert the URL into href and onclick unescaped #10814

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?

anchor() and anchor_popup() treat an absolute URL as raw HTML. The URL goes straight into href, and anchor_popup() also puts it inside a single-quoted JavaScript string in onclick. mailto() does the same with the email address.

The helpers don't say that the URI or email must be pre-escaped, and a single quote in a URL is legal.

Example:

echo anchor('https://example.com/" onclick="alert(document.domain)" data-x="', 'Click');

Output:

Click

anchor_popup() is worse because the URL goes into a JavaScript string:

echo anchor_popup("https://example.com/'+alert(document.domain)+'", 'Click');

Click

mailto() has the same issue with the email value:

echo mailto('a@example.com" onclick="alert(document.domain)" data-x="', 'Click');

Click

Steps to Reproduce

I left a runnable script at bug-reports/poc/f03-url-helper-link-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/f03-url-helper-link-injection.php

To check it in a browser, save the output to /tmp/f03.html and open it. Clicking the first or third link runs the handler. The second one breaks out of the JavaScript string in window.open().

Expected Output

For the href context, I'd expect the URL to be escaped:

Click

For anchor_popup(), the URL should be escaped for the JavaScript string context too, or passed through a scheme allowlist. mailto() should do the same for the email value.

Anything else?

Escape the URL for the HTML attribute context:

return '<a href="' . esc($siteUrl) . '"' . $attributes . '>' . $title . '';

For anchor_popup():

$jsUrl = esc($siteUrl, 'js');

return '<a href="' . esc($siteUrl)
. '" onclick="window.open('' . $jsUrl . "', '" . esc($windowName, 'js') . "', '" . stringify_attributes($atts, true) . "'); return false;""
. $attributes . '>' . $title . '';

For mailto():

return '<a href="mailto:' . esc($email) . '"' . stringify_attributes($attributes) . '>' . $title . '';

I'd also restrict link schemes to http, https, and mailto, and reject javascript:, data:, and similar values. A test in the URL helper tests with a quote payload would cover the href and window.open() paths.

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