Skip to content

fix(Security): improve regex and quote handling in strip_image_tags() - #10745

Merged
paulbalandan merged 1 commit into
codeigniter4:developfrom
gr8man:fix/strip-image-tags-regex
Nov 15, 2026
Merged

paulbalandan merged 1 commit into
codeigniter4:developfrom
gr8man:fix/strip-image-tags-regex

Conversation

@gr8man

@gr8man gr8man commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Description
strip_image_tags() used an invalid PCRE character class ([^\1]) that allowed HTML attribute injection upon mixed quotes, while also swallowing intervening text between multiple tags and failing to strip image tags without src. This change resolves these issues using a branch-reset pattern and ensures tags without src are stripped.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 8, 2026
Comment thread system/Helpers/security_helper.php Resolved
Comment thread system/Helpers/security_helper.php Resolved
@gr8man
gr8man force-pushed the fix/strip-image-tags-regex branch from e938e43 to 2965a8d Compare November 9, 2026 18:36
Comment thread system/Helpers/security_helper.php Resolved
Comment thread system/Helpers/security_helper.php Resolved
Comment thread system/Helpers/security_helper.php Resolved
@gr8man
gr8man force-pushed the fix/strip-image-tags-regex branch 2 times, most recently from 2c887b5 to fdfb299 Compare November 12, 2026 12:36

@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 clarify that this helper is not an HTML sanitizer and does not make its output safe to render as HTML. It returns raw src values, so the result still needs appropriate escaping when displayed as text:

This is a security function that will strip image tags from a string. It leaves the image URL as plain text.

Maybe:

This function replaces image tags with their src values and removes tags without src. It does not sanitize HTML or escape the returned values. Use appropriate escaping when displaying the result as text.

Comment thread system/Helpers/security_helper.php Resolved
Comment thread system/Helpers/security_helper.php Resolved
@gr8man
gr8man force-pushed the fix/strip-image-tags-regex branch 3 times, most recently from 26d5f5a to e413069 Compare November 13, 2026 20:06
@paulbalandan paulbalandan changed the title fix(Security): improve regex and quote handling in strip_image_tags() fix(Security): improve regex and quote handling in strip_image_tags() Nov 14, 2026

@paulbalandan paulbalandan 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.

Nit for the docs: please mention that if replacement fails or reaches the iteration limit, remaining <img prefixes are escaped. That explains why callers may see escaped tag text in the result.

@gr8man
gr8man force-pushed the fix/strip-image-tags-regex branch from e413069 to f5ab3d8 Compare November 14, 2026 21:35
@gr8man
gr8man requested a review from paulbalandan November 14, 2026 21:37
@paulbalandan
paulbalandan merged commit 23fee28 into codeigniter4:develop Nov 15, 2026
59 checks passed
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.

3 participants