Skip to content

Content Translation: translated text loses its text color background and app links #1268

Open

Description

@ekamran

Description

Content Translation returns the translated content through wp_kses_post() (includes/Abilities/Content_Translation/Content_Translation.php, line 157). The system instruction asks the model to copy every HTML token byte for byte, but wp_kses_post() then changes markup that the editor wrote:

  1. Text color. When a text color is picked with no background, the block editor's Highlight format writes <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color"> (format-library.js, "transparentValue"). safecss_filter_attr() does not allow rgb() or rgba(), so the style attribute is removed. A <mark> with no background shows the browser's yellow highlight, so the translated text gets a yellow background it did not have.
  2. Links with other schemes. A link such as skype:gardener?call loses its scheme, because skype is not in the allowed protocols, and becomes the relative URL gardener?call.

Both survive in the original post for users with unfiltered_html. Only the translation changes them.

Expected: the translated markup keeps the formatting and links of the original.

Changing what this sanitizes affects HTML safety, so I would like your direction before opening a PR. Some options:

  1. Keep wp_kses_post(), allow rgb() and rgba() while it runs (a scoped safecss_filter_attr_allow_css filter), and allow the URL schemes already used in the content that was sent.
  2. Compare the output with the input and only keep tags and attributes that were in the input.
  3. Leave it as is and document it.

Step-by-step reproduction instructions

  1. Enable the Content Translation experiment under Settings > AI, with a connected AI provider.
  2. In a paragraph, select a few words and set a text color with the Highlight option (Text tab), without a background color.
  3. Add a link to skype:gardener?call in the same paragraph.
  4. Use Generate Translation, choose a language under "Translate to" and click Translate.
  5. On the front end, the translated words have a yellow background, and the link points to gardener?call on the site.

Screenshots, screen recording, code snippet

On develop (93eefdf) in wp-env, WordPress 7.1.4, Twenty Twenty-Five. POST /wp-abilities/v1/abilities/ai/content-translation/run (target es-es), with the OpenAI provider mocked through pre_http_request (the same way the plugin's e2e tests do) to return the markup it was sent, unchanged:

sent:     Our garden has <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">red tomatoes</mark> this week. Call <a href="skype:gardener?call">the gardener</a> or <a href="https://example.org/seeds">order seeds</a>.
returned: Our garden has <mark class="has-inline-color has-vivid-red-color">red tomatoes</mark> this week. Call <a href="gardener?call">the gardener</a> or <a href="https://example.org/seeds">order seeds</a>.

Both versions on one page, read with getComputedStyle() in Chrome 152:

before: mark background rgba(0, 0, 0, 0), color rgb(207, 46, 46); link skype:gardener?call
after:  mark background rgb(255, 255, 0), color rgb(207, 46, 46); link http://localhost:8888/translation-proof/gardener?call

Environment info

  • WordPress 7.1.4, PHP 8.3, MariaDB 12.3.3 (wp-env)
  • AI plugin develop at 93eefdf
  • Theme: Twenty Twenty-Five

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except the AI plugin.

Yes. Only the AI plugin, the provider plugins it loads in wp-env, and a small test plugin that mocks the provider were active.

Please confirm which theme type you used for testing.

Block

Activity

  1. dkotter commented on Nov 3, 2026

    @dkotter
    Contributor

    Changing what this sanitizes affects HTML safety, so I would like your direction before opening a PR. Some options:

    1. Keep wp_kses_post(), allow rgb() and rgba() while it runs (a scoped safecss_filter_attr_allow_css filter), and allow the URL schemes already used in the content that was sent.
    2. Compare the output with the input and only keep tags and attributes that were in the input.
    3. Leave it as is and document it.

    I'd personally be okay with option 3 here as I think there's already precedent (in other experiments) that not all formatting survives and it's up to the user to either add that back or not trigger functionality on that text.

    That said, if there's an easy way to prevent this, I'm fine with moving forward with that, which I think is the first part of option 1 (which looks like you have #1269 opened to fix that). I'm not concerned with the URL schemes thing, unless this is something we get multiple reports on.

    I would be interested in knowing if there's a particular reason why RGB attributes are allowed but wp_kses_post strips those, to make sure we're not introducing a security hole here by allowing those back in.

  2. ekamran commented on Nov 3, 2026

    @ekamran
    ContributorAuthor

    I could not find a security reason specific to rgb() or rgba(). Since r11689, safecss_filter_attr() rejects any value that contains ( unless the function is on an allowlist. The main concern raised on #24157, open since 2013, was old IE's CSS expression(), and nacin noted there "We may be able to work around rgb(a), at the very least." Core has since allowed url() with a protocol check, gradients (including rgb() inside them), var(), calc() and others, but not plain color functions.

    The same change is already moving upstream. Gutenberg merged #84213 on 30 October (Gutenberg 24.4), which allows rgb(), rgba() and the other color functions through a safecss_filter_attr_allow_css filter. The Core backport is #66387, on the 7.3 milestone. #56391 is the older ticket for this exact case, the background-color:rgba(0, 0, 0, 0) that the Highlight format writes.

    #1269 is narrower than both. It only accepts rgb() or rgba() whose contents are numbers, ., %, spaces, commas or /. The rest of the value still has to pass Core's own check, Core only runs this filter for properties on its allowlist, and the filter is only added while the translated content goes through wp_kses_post().

    If #66387 lands in 7.3, the filter can be removed once 7.3 is the minimum supported version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions