Skip to content

Strip PHP execution tags from pattern body content#851

Merged
mikachan merged 16 commits into
trunkfrom
fix/sanitise-pattern-content
Jun 16, 2026
Merged

Strip PHP execution tags from pattern body content#851
mikachan merged 16 commits into
trunkfrom
fix/sanitise-pattern-content

Conversation

@mikachan

Copy link
Copy Markdown
Member

Strip PHP open tags from user-supplied pattern body content before it's interpolated into exported .php pattern files. Builds on #817, which closed the docblock-breakout vector in the title/categories/slug fields. This PR closes the equivalent vector in the body field.

To test, ensure these tests pass:

 npm run test:unit:php -- --filter Test_Create_Block_Theme_Patterns

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens exported block pattern/template .php files by stripping PHP execution tags from user-supplied body content before it’s interpolated into generated pattern files, closing the “body field” injection vector (complementing prior docblock sanitization work from #817).

Changes:

  • Add CBT_Theme_Patterns::strip_php_tags() and apply it to wp_block pattern bodies before heredoc construction.
  • Apply the same sanitization at the start of CBT_Theme_Templates::prepare_template_for_export() (before trusted localization PHP injection).
  • Add a dedicated PHPUnit test suite covering open-tag variants and template export behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
tests/test-theme-patterns.php Adds unit tests covering PHP tag stripping in pattern/template export pipelines and on-disk output.
includes/create-theme/theme-templates.php Sanitizes raw template content early in the export pipeline to prevent body injection while preserving trusted localization markers.
includes/create-theme/theme-patterns.php Introduces strip_php_tags() and uses it when building exported pattern file bodies from wp_block content.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread includes/create-theme/theme-patterns.php Outdated
Comment thread tests/test-theme-patterns.php Outdated
Comment thread tests/test-theme-patterns.php Outdated
Comment thread includes/create-theme/theme-patterns.php Outdated
mikachan and others added 4 commits June 16, 2026 13:39
Co-authored-by: Ben Dwyer <ben@scruffian.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
The previous `<?xml` carve-out caused fatal parse errors on hosts with
short_open_tag=1: the exported `.php` pattern file would treat `<?xml`
as a short PHP open tag, parsing `xml version="1.0"...` as PHP code.
Block patterns are HTML/block markup and have no legitimate use for an
XML declaration, so it is stripped along with the other open tags.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

Comment thread includes/create-theme/theme-patterns.php
Comment thread includes/create-theme/theme-patterns.php Outdated
Comment thread tests/test-theme-patterns.php Outdated
Comment thread tests/test-theme-patterns.php Outdated
Comment thread tests/test-theme-patterns.php
mikachan and others added 5 commits June 16, 2026 15:25
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Two follow-ups to Copilot suggestions:
- e05bc1f ("Update docblock") dropped the closing `*/` of the
  strip_php_tags() docblock, so PHP swallowed the function declaration
  as part of an unterminated comment and tests fatally errored with
  "Call to undefined method CBT_Theme_Patterns::strip_php_tags()".
- Applies scruffian/Copilot suggestion to drop the unused $post stdClass
  stub in test_strip_php_tags_handles_non_string_input — $real_post is
  the variable the test actually exercises.
add_patterns_to_theme() doesn't only write the pattern file. Via
replace_local_pattern_references() it also touches existing template
files and calls clear_user_templates_customizations() /
clear_user_template_parts_customizations() which delete user-template
posts from the DB. Those side effects on the active theme could
contaminate subsequent tests in the suite.

Wrap the end-to-end test in create_blank_theme() / uninstall_theme()
so all writes and DB mutations happen inside a throwaway theme that
gets fully torn down at the end. Helpers mirror the ones in
Test_Create_Block_Theme_Fonts.

@scruffian scruffian left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fixes!

@mikachan mikachan merged commit f5a4b43 into trunk Jun 16, 2026
13 checks passed
@mikachan mikachan deleted the fix/sanitise-pattern-content branch June 16, 2026 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants