Problem/Motivation
Currently the content of raw text elements (script and style) are escaped and converted into HTML entities resulting in invalid HTML being produced.
For example, certain characters like (>, &&, ...) are converted to html entities.
Steps to reproduce
Pasting the following in the editor source will produce invalid HTML throwing the following syntax error: Uncaught SyntaxError: Unexpected token ';'. on the frontend when it's saved and rendered.
<script type="text/javascript">
let x = 10;
let y = 5;
if(y < x){
console.log('is smaller')
}
</script>
<style type="text/css">
.sections > h2 {}
</style>
will be unintentionally converted into:
<script type="text/javascript">
let x = 10;
let y = 5;
if(y < x){
console.log('is smaller')
}
</script>
<style type="text/css">
.sections > h2 {}
</style>
- Tested in a new D10 install
- Has also been discussed here: https://stackoverflow.com/questions/76041173/how-to-add-inline-js-code-i...
Proposed resolution
Update the drupalHtmlEngine.DrupalHtmlEngine CKeditor 5 plugin to handle raw text elements specially.
Remaining tasks
Provide issue fork/MR
And backport/cherry-pick bugfix to 10.x
User interface changes
NA
API changes
NA
Data model changes
NA
Release notes snippet
NA
| Comment | File | Size | Author |
|---|---|---|---|
| #62 | 3364884-script-style-characters-php-8.1.patch | 11.1 KB | asila96 |
| #61 | 3364884-61-10.3.x.patch | 1.72 KB | kavya n n |
| #58 | ckeditorPluginfixes.patch | 6.5 KB | dinesh18 |
Issue fork drupal-3364884
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
sakthi_dev commentedI think for security purpose they disabled it in CKEditor5. Please try with CKEditor4.
Comment #3
wim leersWhat does your text format & text editor configuration look like? Please export both pieces of config and post/upload them here 🙏
Comment #4
dylan donkersgoed commentedI have also encountered this issue. It is not a CKEditor 5 base issue, or at least does not always happen with base CKEditor 5. A minimal CKEditor 5 configuration with source editing does not have the issue, but a minimal Drupal CKEditor 5 configuration does. I'm attaching minimal editor/format configs with the issue and an equivalent minimal vanilla CKEditor 5 setup.
To reproduce the issue in Drupal:
<style>span > a { }</style>>To see the issue does not happen in vanilla CKEditor 5:
<style>span > a { }</style>>Comment #6
dylan donkersgoed commentedI've pushed up an MR and attached a patch with a simple fix which uses the raw textContent for script and style tags. There's probably room for improvement (e.g. there might be other tags affected, or maybe a less naive check than just checking the parent tag name should be used and possibly there should be a test), but this fixes the issue for me.
Comment #7
smustgrave commentedDefinitely something that could benefit from a test case.
Comment #8
wim leers#7++
This patch/MR needs to expand
core/modules/ckeditor5/tests/src/Nightwatch/Tests/drupalHtmlBuilderTest.jsto become committable.Also, it needs to first land in
11.x. A patch may be simpler in this case, so that you can test against all supported branches at the same time.Comment #9
damienmckennaWould allowing the "style" tag to be rendered as raw, without standard filtering, cause security problems? Shouldn't we limit the patch here to just the "script" tag?
Comment #10
damienmckennaJut filter the SCRIPT tag, leave the STYLE tag as-is.
Comment #11
damienmckennaFYI we ran into this on a client's site where they were pasting HTML into a text format that did not have the "Limit allowed HTML" filter enabled.
Moving back to "needs work" as it needs tests and definitive steps to reproduce.
Comment #12
mikesimmons commented#10 works for me. Drupal 10.1.3.
Comment #13
wim leersBased on
\Drupal\Component\Utility\Xss::filterAdmin(): yes.Because this is security-adjacent, this absolutely needs test coverage before it can be considered close to ready.
Comment #14
wim leersComment #15
_utsavsharma commentedTried to fix CCF in #10.
Comment #16
qusai taha commentedRe-roll the patch to 9.5.x
Comment #17
kwfinken commentedRunning into similar issue that may help with knowing the scope...which is definitely beyond the script tag. When I add items with twig such it gets changed by CKEditor before it is even saved and before the twig filter executes:
Steps to reproduce: edit a node, view source and paste the following into the textarea.
Switch from view source to normal editing view and then back to view source and the code now looks like this:
Note that the apostrophe is turned into ' in some areas, but not all, the first line seems untouched but the rest are munged.
Comment #18
ramprassad commentedUpdated the patch #15 to include the updated library version for aggregation for 10.1.x
Comment #19
wim leersClosed #3398013: CKEditor 5 + Full HTML prevents the child combinator CSS selector to be used in <style> as a duplicate of this — thanks @ramprassad for connecting the dots!
Comment #20
wim leersComment #21
wim leersClarifying this is a bug in Drupal's
DrupalHtmlEngineplugin.Comment #22
ramprassad commented@Wim
There is a related issue I have created based on one of the reported issues in our project. Can you please check? https://www.drupal.org/project/drupal/issues/3408656
Regards,
Ramprassad
Comment #23
ioana apetri commentedHello,
tag into wysiwyg editor which allows Full Html in a Drupal field. I applied the latest patch from here and the charcters like < or > are getting enclosed in special characters. How can I solve this? Could someone solve the issue? Thank you!I have the same issue when trying to add
Comment #24
kthullThe patch no longer applies under D10.2.1
Comment #25
sivakarthik229 commentedAdding the patch for latest D10.2.x for allowing only script tag
Comment #26
sivakarthik229 commentedAdding the patch for latest D10.2.x for allowing script and style tags as per our requirement.
Comment #27
kthullConfirming #25 works for me...thanks!
Comment #28
znak commentedOne more patch for 11.x for allowing script and style tags
Comment #29
znak commentedComment #30
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #31
pradhumanjain2311 commentedFix the patch applied issue for 11.x
Please Review.
Comment #32
smustgrave commentedPlease look at the tags for items to address before putting in review.
Still needs steps to reproduce added to the issue summary, actually needs a full issue summary update
Needs test.
Not ready for review yet.
Comment #35
codebymikey commentedAdded a MR with the relevant test, and requires review.
Comment #36
smustgrave commentedHiding patches for clarity.
Added missing sections to issue summary, proposed solution will need to be filled in
Leaving in review for additional eyes.
Comment #37
wim leersOnly 1 of the 3 test cases fails in the test-only CI job. For the
style-only test coverage that's because there's no>anywhere in the CSS 😄 Easily fixed.I'd like to see all 3 new test cases fail 🙏 Then I'll happily RTBC! Having looked at the code, I withdraw the that I added 9 months ago in #3 — that was because the title/report was alarming at the time, now it is not :)
Comment #38
codebymikey commented@Wim Leers good spot on the test-only changes! I switched to the @dataProvider last minute and didn't really contemplate the test cases properly.
Those relevant features have been implemented, and awaiting review
Comment #40
smustgrave commentedSeems all 3 tests are now failing with feedback addressed.
Comment #42
alexpottAdded a comment to the MR. I'm not sure that doing 4 reinstalls just to use a text editor is necessary... see comment on the MR.
Comment #43
codebymikey commentedImplemented the feedback, however it makes testing all the test cases via the test only pipeline a little harder.
Comment #44
smustgrave commentedAppears feedback has been addressed to not use a dataprovider.
Comment #45
wim leersIndeed, but that's a fine trade-off.
In #37 I wrote that all 3 tests should fail. But now there are six test cases.
I saw the 3 failures in https://git.drupalcode.org/issue/drupal-3364884/-/jobs/1119730 … which means the other 3 passed. These 3 did not fail:
script like tagscript to escapeunescaped script tagWhy is that not a problem? 🤔 I feel like I'm overlooking something? 🙈 Sorry!
Comment #46
codebymikey commented@Wim Leers
I introduced 3 new tests in the latest commits (which weren't part of the original test-only pipeline that was ran), which is probably the main discrepancy.
The https://git.drupalcode.org/issue/drupal-3364884/-/jobs/1119730 job was for the
490e9434commit which still had 3 test cases not 6.Comment #47
smustgrave commentedAll 6 tests
Removed that one ran next 5
Removed that one ran next 4
Removed that one ran next 3
Removed that one ran next 2
Removed that one ran last one
Comment #48
alexpottI fixed some whitespace stuff on commit in the test to make it easier to maintain in the future.
Committed and pushed 762d5b9adf to 11.x and ad38efa765 to 10.3.x. Thanks!
I considered backporting to 10.2.x but the changes to JS gave me pause - not sure if we'd need an update function to clear cached js aggregates to left in 10.3.x
Comment #51
nod_I do not get why we have this test case:
This is not the test I added. Can someone explain to me
despite appearances, this is actually part of the script still!because I do not see a reason why this should be only one script tag. For me it's 2 different script tags and should be treated as such. If it's to get around the behavior I'm seeing below it should not work that way.I added the test case:
And that is transformed into:
Above creates a syntax error when it's viewed on the node. The actual result should be somethings like:
So while there is not an issue with our code, CKEditor upstream doesn't parse this correctly and adds a closing script and body tag too early. Having a second script tags seems to go around the problem but fact is that there is still an issue and at least a follow-up and/or upstream issue is needed to fully support
<script>tags in wysiwyg.Comment #52
nod_Opened a followup for this, it's an edge case and this is less broken than it was, so not convinced we need to revert: #3437394: [DrupalHtmlEngine] Follow-up for script and style tags in CKEditor 5
Comment #53
znak commentedComment #55
travisc commentedPatch is failing on D10.2.6, could someone please re-roll it?
Comment #58
dinesh18 commentedPatch for Ckeditor5 , Drupal 10.2.4
Comment #59
haider afridi commentedTo handle the issue & tag converting to & use
Comment #60
dalinOooph. Let's say you've got a site with hundreds of content editors that had this bug for about a year while the site was on D10 but before it got to 10.3. You've now got hundreds of busted script and style tags. (Yes it was bad to allow these tags, but we inherited this site created by a vendor who doesn't do Drupal anymore). Now we've got to write an update to clean this up. something along the lines of:
1. Get all base fields that accept formatted text.
2. Run a query on each table to get all rows containing
<scriptor<style3. Put the content through a DOM parser. Process all the script and style tags. Get their text.
4. Do some regex search/replace. Maybe something like
(the regex above doesn't yet fix corrupted
&&).Some questions:
1. Is this cleanup something that should be in Core, since this caused data corruption over a long period of time between 10.0.0 and 10.2.10 inclusive (i.e. this issue probably should've been marked as Critical, not Major)?
2. If not, should it at least be tracked here (or in a new issue) so that others can find it?
Comment #61
kavya n n commentedRe-rolling the patch for D10.3.x
Comment #62
asila96 commentedFor Drupal 9.5 - combining patch #16 with patch #28 on Issue 3300404 for PHP 8.1 compatibility.