Skip to content

Fix template directive injection handling - #41244

Open
angelvilaplana wants to merge 1 commit into
magento:2.4-developfrom
angelvilaplana:fix/template-directive-injection
Open

Fix template directive injection handling#41244
angelvilaplana wants to merge 1 commit into
magento:2.4-developfrom
angelvilaplana:fix/template-directive-injection

Conversation

@angelvilaplana

Copy link
Copy Markdown
Contributor

Description (*)

This PR fixes an issue in the template directive filtering that can cause directive content originating from template data to be processed again when deferred directives are resolved.

The issue can be reproduced through a guest cart by storing a template directive in an address field and subsequently triggering the Payflow Pro response handling flow.

The proposed changes introduce two mechanisms to prevent this behavior:

  • Signature markers to identify directives that were intentionally deferred by a template.
  • Placeholders to prevent the output of one directive from being re-scanned and interpreted as another directive during the same filtering operation.

Unresolved signature markers are also removed when the root template finishes processing, preventing internal marker values from reaching the rendered output.

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes magento/magento2#<issue_number>

Manual testing scenarios (*)

  1. Create a guest cart using the createGuestCart GraphQL mutation and save the returned cart ID.

  2. Set the billing address of the guest cart using the following setBillingAddressOnCart GraphQL mutation. Replace <CART_ID> with the guest cart ID returned in the previous step.

mutation SetBillingAddressOnCart {
  setBillingAddressOnCart(
    input: {
      cart_id: "<CART_ID>"
      billing_address: {
        address: {
          firstname: "Injected Code Example"
          lastname: "lastname"
          country_code: "GB"
          city: "city"
          street: "street"
          region: "{{if postcode}}{{var postcode}}{{/if}}{{/var}}{{if postcode}}{{var postcode}}{{/if}}{{if city}}{{var firstname}}{{/if}}"
          postcode: "{{var postcode}}"
          telephone: "1"
        }
      }
    }
  ) {
    cart {
      billing_address {
        firstname
        lastname
        street
        city
        postcode
        country {
          code
        }
      }
    }
  }
}

The important part of this payload is that the firstname is set to Injected Code Example, while the region field contains the template directive: {{if city}}{{var firstname}}{{/if}}

  1. Execute the handlePayflowProResponse GraphQL mutation for the same guest cart:
mutation  {
  handlePayflowProResponse(
    input: {
      cart_id: "<CART_ID>"
      paypal_payload: "RESULT=12&RESPMSG=Declined"
    }
  ) {
    cart {
      id
    }
  }
}
  1. After the request, check the email in MailCatcher (or an equivalent tool). Before the fix, Injected Code Example appears twice in the payment transaction failure information: once as the firstname value and once where the template directive is injected.
image

If you do the same steps with this PR applied, you can see that this unintended replacement doesn't happen.

image

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

@m2-assistant

m2-assistant Bot commented Sep 8, 2026

Copy link
Copy Markdown

Hi @angelvilaplana. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@angelvilaplana

Copy link
Copy Markdown
Contributor Author

@magento run all tests

@angelvilaplana
angelvilaplana force-pushed the fix/template-directive-injection branch from d157315 to 326de5f Compare September 8, 2026 18:52
@angelvilaplana

Copy link
Copy Markdown
Contributor Author

@magento run all tests

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.

1 participant