Note: This issue is not critical as it only appears in debug output that should never be active on production and is only possible when the following conditions hold:
- debug mode is on
- dynamic user input of templates is used (This is however active in core right now)
- sandbox mode is not used to restrict possible functions, but even then %trans would probably be allowed.
However, scenario:
- Dump production DB for local development
- Activate debug mode (or have it already active via settings.local.php)
- Get "owned" on your own machine ...
The same could happen on a -dev server or even staging environment.
Problem/Motivation
Given the following twig template:
{% trans %} Hi - ' . die('I am bad code!') . ' {% endtrans %}
the code is executed due to the debug output not being properly escaped for "'".
Proposed resolution
Remove debug output for now. Add it back in #2512672: Add secure debug output to twig trans extension.
Remaining tasks
Review.
User interface changes
- None
API changes
- None
| Comment | File | Size | Author |
|---|---|---|---|
| #38 | interdiff.txt | 2.7 KB | lauriii |
| #38 | arbitrary_code-2489024-38.patch | 6.5 KB | lauriii |
| #38 | arbitrary_code-2489024-test-only-38.patch | 5.45 KB | lauriii |
| #33 | interdiff.txt | 667 bytes | lauriii |
| #33 | arbitrary_code-2489024-33.patch | 5.22 KB | lauriii |
Comments
Comment #1
fabianx commentedComment #2
fabianx commentedDebug output is correct.
Comment #3
fabianx commentedAnd here is a patch to fix it :).
Comment #4
fabianx commentedComment #8
lauriiiComment #9
star-szrFixing the minor whitespace here can be done by a new contributor and should fix the tests :)
Comment #10
zeropx commentedStarting on this at DrupalCon LA Sprints
Comment #11
zeropx commentedAfter spending considerable time today on the ticket with Joel and Lauriii. This is what we came up with.
This patch does address the noted bug. This also introduces other things that affect the build of the test output when options
A noted way it breaks:
When the test has some $options, it breaks here. There is an extra quote being added to the $singular variable that causes the code being generated to break here when trying to concat the string together.
Adding addcslashes() around the $singular variable will give a green pass on the local tests but not the best idea, but proves a point.
Kinda like the following:
Essentially, the goal is to escape any passed in extra quotes.
How I was testing. With using the GUI in Drupal sometimes too.
Side note: It was also brought up, that it would be good to write a test for the this ticket's original focus:
At this point, I am trying to just finish up the thoughts and learning so if i missed anything, sorry. I'll look again this weekend sometime.
Comment #12
star-szr@zeropx I really didn't intend to throw you into the deep end but you can swim quite well ;) thanks for all that!
Comment #13
zeropx commented@Cottser no worries! I really enjoyed the experience. In fact, if i wasn't put on something like this, I wouldn't of met so many people like that. Got to learn a bunch of other things as well during the process.
Between you, @joelpittet and @laurii I got to really expand and play with other areas I have not yet explored. I can't take credit for all of it either, just wrote up the summary of what we collectively found out.
So thanks!. :D
Comment #14
fabianx commentedComment #15
fabianx commentedActually this is critical :(.
Scenario:
- Dump production DB for local development
- Activate debug mode (or have it already active via settings.local.php)
- Get "owned" on your own machine ...
The same could happen on a -dev server or even staging environment.
Comment #16
fabianx commentedComment #17
star-szr:(
Comment #18
dawehnerIn case someone wants to work on the issue but not know how to fix the problem, first try to write a test for it.
First step: Add an entry to
\Drupal\system\Tests\Theme\TwigTransTest::checkForDebugMarkupwhich looks like the issue described in the issue queue.Maybe choose
debug()as it should be easier to test.Comment #19
dawehnerJust an idea how to fix it in a different way: Write an object which captures which values are passed into t() / formatPlural (this should implement
\Drupal\Core\StringTranslation\TranslationInterfaceand use that as part of the debug statement to know which values got passed / returned.Comment #20
alexpottI discussed this with @effulgentsia, @catch and @xjm. Is this actually exploitable by a site builder - or someone who writes twig templates? I.e. how exploitable is this and is it exploitable through the front-end. I guess maybe through the twig templating abilities in views?
Comment #21
dawehnerAs far as I understand you need to either have access to the twig templates or use the views UI and maybe contemplate or similar modules in the future.
At least for views, we need to be aware that this is a restricted permission already.
Comment #22
star-szrMight be also worth mentioning that Twig debug is off by default and enabling it requires altering services.yml (and rebuilding the container). It's also documented as not recommended for production right above the setting:
Comment #23
star-szrAnd on that note…
Comment #24
effulgentsia commentedI don't think that alone should demote this issue's priority, because arbitrary PHP execution on a dev environment or local machine is still pretty nasty.
If this were the only pathway, then I'd recommend demoting to Major.
D8 core no longer has the PHP module, so I think arbitrary PHP execution needs to be closed to all UI users, even those with a restricted permission. Therefore, I think this should remain critical if it's possible to exploit via Views UI.
If the Views UI pathway doesn't exist, but such contrib pathways do, then I'm conflicted on whether this should be Critical or not.
Comment #25
gábor hojtsyBased on the discussion on the security meeting today, the fastest way forward is to temporarily remove the debug output and add it back in a normal task. Opened #2512672: Add secure debug output to twig trans extension for that. Now needs reviews. No interdiff, because this is a trivial new patch.
Comment #26
jibranYeah +1 to the idea. RTBC if green.
Comment #27
lauriiiI think we should still have test coverage that this does not happen after removing that piece of code.
Comment #28
gábor hojtsy@laurii: wanna take that on then?
Comment #30
lauriiiI can try to find a way to figure out how to test that. I will also take a look on failing tests
Comment #31
lauriiiThere's no way to test that this doesn't happen with twig debug because the functionality is removed in the patch. However the original report didn't know whether its with Twig debug on or not so this tests it without Twig debug now. I did also fix the failing tests.
Comment #32
dawehnerThank you gabor!
Comment #33
lauriiiComment #34
fabianx commentedCan we get a test-only patch, too, please?
Comment #35
lauriiiThere's no failing test because the bug doesn't exist. There is still no test coverage for that AFAIK so now there is :)
Comment #36
lauriiiThere's no failing test because the bug doesn't exist. There is still no test coverage for that AFAIK so now there is :)
Comment #37
lauriiialexpott told me how to do this. Will be working on this
Comment #38
lauriiiNever write something is impossible to d.o because someone will prove you its possible :)
Comment #40
fabianx commentedRTBC, fantastic work!
Thanks, laurii.
Comment #41
alexpottCommitted 2ac614c and pushed to 8.0.x. Thanks!
Comment #43
gábor hojtsy