For now ctools_context_entity_convert() don't use sanitize option with token_generate().

token_generate() add the sanitize option to TRUE as default value.
Some modules (ex: panel_breadcrumb) use the token's values directly in the l() function where values are sanitized again.
So string like : L'Apostrophe
show as : L'Apostrophe

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Damien Tournoud’s picture

Status: Active » Needs review

So, the problem here is that it's very unclear what the result of ctools_context_keyword_substitute() is: an HTML string or plaintext?

In the current situation, ctools_context_entity_convert() calls token_generate() without 'sanitize' => FALSE, which means that the result is going to be returned as an HTML string, and then potentially double encoded when used in a title or in the breadcrumb links generated by panels_breadcrumb.

In the patch attached in the original post, @bennetteson suggests that ctools_context_keyword_substitute() should return a plain-text string by default, but supports an option that allow the caller to decide it wants to process the string as plaintext.

Could we get a review on the principle of this patch?

merlinofchaos’s picture

Ok, the principle of the patch is okay.

We have to be very careful in checking all uses of this function to see if there is a potential security issue, which is going to be somewhat time consuming, but luckily I think it is not actually called in that many places so it hopefully will not be a big deal. And I believe most of the places we are seeing this we will see it already sanitized. The idea is that anything that can have a token in it is user input, and therefore MUST be sanitized after the token replacement. This is perfectly valid behavior and I think is the correct behavior.

In a minor nit, this patch unnecessarily removes whitespace that I prefer. I tend to add a lot of extra CRs between code blocks in order to facilitate readability.

merlinofchaos’s picture

Status: Needs review » Needs work
bennetteson’s picture

Resend the patch without removing the whitespaces.
For the security issue I thinks you're write but this patch don't change the current behavior of the tokken engine. All input will be sanitized unless you give the option you're self to not do the sanitize at your own risk.

This is useful sometime to have the original string without sanitization and in fact I have to get this work like that.

Remember this patch dosen't had a security issue but give a way to add a security issue but the security issue will be in other module than your.

Damien Tournoud’s picture

Actually, I now realize this is not the proper approach.

The token system in Drupal 7 does *not* support returning plaintext tokens. It only support two things:

  • HTML-encoded strings (when 'sanitize' = TRUE)
  • Raw tokens (when 'sanitize' = FALSE), which is *not* plaintext but rather "whatever form the source thing is stored in")

Which means that if you want plaintext, you don't have any other solution then calling token_replace() with 'sanitize' = TRUE and you need to convert the resulting HTML to plaintext yourself (whether using drupal_html_to_text() or strip_tags(decode_entities($text))).

bennetteson’s picture

Yes and I don't want to get plain text bug only get a tokens replacement option to array('sanitize' => FALSE) to get the raw tokens.

LCG’s picture

Hello,

using the "Menu Token" module, i have the same problem with my user menu.
This menu is something like : "Hello Bob El'roy".

Could this patch do something for me or must I look in another way?

Thanks.

bennetteson’s picture

@LCG Not directly. This patch only add a the ability to a module to add some options to ctools_context_entity_convert().

bennetteson’s picture

Status: Needs work » Needs review
FileSize
2.96 KB

OK, my bad...
Now I got it !

See the new patch.

bennetteson’s picture

@LCG With the good patch you still need to patch again, see my exemple with panel_breadcrumbs http://pastebin.com/4VypnP04

LCG’s picture

I'm not sure to understand.

After aplying the patch in #9 to ctools, I still need to find a patch for Menu Token?

bennetteson’s picture

@LCG No you can't. "Menu Token" use token_replace_multiple no ctools_context_keyword_substitute.

merlinofchaos’s picture

I actually need this patch for other reasons, but Damien is right. Just using 'sanitize' => FALSE is not necessarily what you want. Interesting.

@@ -265,8 +265,9 @@ function ctools_context_entity_convert($context, $type) {
   }
 
   $tokens = token_info();
+  $options += array('sanitize' => TRUE);
 
-  $values = token_generate($token, array($type => $type), array($token => $context->data));
+  $values = token_generate($token, array($type => $type), array($token => $context->data), $options);
   if (isset($values[$type])) {
     return $values[$type];
   }

The $options addition is unnecessary there because token_generate already has that exact same line. So it is completely redundant in every case unless token_generate changes its default in the future, which it would never do because of the security implications.

Otherwise I think I am going to use this, as it is valuable.

Committed and pushed with the change I made above.

bennetteson’s picture

@merlinofchaos my last patch #9 as exactly the same code.
Now I use $options += array('sanitize' => TRUE); so by default this patch still use the legacy behavior. No more security risk added in this module.

merlinofchaos’s picture

No no. What I was saying was that the $options += array('sanitize' => TRUE) is unnecessary -- token_generate has that exact line of code. So omitting the line is the exact same result.

bennetteson’s picture

Ok you're right, I just see that.
So here the good patch.

merlinofchaos’s picture

bennetteson’s picture

looks good for me

merlinofchaos’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

' show as '