Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filters to respond in html #3578

Merged
merged 1 commit into from
Jun 23, 2017
Merged

Allow filters to respond in html #3578

merged 1 commit into from
Jun 23, 2017

Conversation

Kaik
Copy link
Contributor

@Kaik Kaik commented Apr 29, 2017

Allow filters to respond in html

Q A
Bug fix? [no]
New feature? [yes]
BC breaks? no
Deprecations? no
Fixed tickets -
Refs tickets -
License MIT
Changelog updated [no]

Allow filters to respond in html
@Kaik
Copy link
Contributor Author

Kaik commented Apr 29, 2017

What should I do with SensioLabsInsight — Code quality below expectations ?

@craigh
Copy link
Member

craigh commented Apr 29, 2017

What should I do with SensioLabsInsight — Code quality below expectations

nothing

@craigh
Copy link
Member

craigh commented Apr 29, 2017

but I'm not sure I'm in favor of this PR. I think all the hook calls apply the |raw filter now, so this shouldn't be needed

@Kaik
Copy link
Contributor Author

Kaik commented Apr 29, 2017

Hmm, I have checked this some time ago and |raw didn't make any changes. I will test it now to confirm.

@Kaik
Copy link
Contributor Author

Kaik commented Apr 29, 2017

Ok this is the call {{ managedPost.get.postText|notifyFilters('dizkus.filter_hooks.post.filter')|raw }} and it does not work, same as {{ managedPost.get.postText|raw|notifyFilters('dizkus.filter_hooks.post.filter')}}
Hooks filter is bbcode filter that returns html instead of [quote].. and without this PR it shows escaped html.

@craigh
Copy link
Member

craigh commented Apr 30, 2017

It looks like in the past (1)(2)(3) with Smarty we used |strip_tags|safehtml or at least |safehtml

because hooks potentially provide user content we should probably do the same. So I propose closing this PR and documenting somewhere that hook filters should also be further filtered by the safeHtml filter.

- [x] investigate whether the filter can be implemented internally in the hook filter so it doesn't need to be manually added.

  • removal of |raw where in use
    - [ ] if not added internally
  • add |safeHtml anywhere hooks are used.
  • documentation of requirement

(1) https://github.com/craigh/PostCalendar/blob/master/templates/user/list.tpl#L31
(2) https://github.com/zikula-modules/FAQ/blob/master/templates/user/display.tpl#L4
(3) https://github.com/zikula-modules/News/blob/master/templates/user/preview.tpl#L4

craigh added a commit to zikula-modules/Pages that referenced this pull request Apr 30, 2017
@craigh
Copy link
Member

craigh commented Apr 30, 2017

more thoughts:

  1. I don't we want to internally implement safeHtml within the notifyFilter filter, because its usage should be optional to the developer.

  2. We need to document clearly that when user content is displayed, it should be filtered.

@Kaik
Copy link
Contributor Author

Kaik commented Apr 30, 2017

Hmm I'm not sure if I understand you correctly. There are two things:

  1. is_safe => 'html' in Twig HookExtension
  2. raw http://stackoverflow.com/questions/8355123/display-a-string-that-contains-html-in-twig-template

First one is deactivating "safe html" feature in notifiFilters method which strips tags by default.
|raw does a similar thing but not for notifyFilters, only for variables so we cannot use it here because tags are already striped somewhere in Twig filters declaration and |raw does not turn it off I guess.

For user content in filter hooks, if tags need to be stripped then this should occur way before notifyFilters call, so hook filters can return output with some additional markup. Best examples are BBCode and BBsmile but I can imagine some kind of censor filter or even words highlighter or something like nl2br.
Exactly similar thing is happening for notifyDisplayHooks.
Maybe order of the filters in twig will do the job

{{ managedPost.get.postText|striptags|notifyFilters('dizkus.filter_hooks.post.filter')}}
I will do some tests today later on this should work if filters are applied in order they are written.
Unless there will be some filters that play with markup like adding classes to divs or something that requires postText to be raw this should work.

Btw correct usage for pages:
This will strip tags befre content is processed by hook filters
{{ content|safeHtml|notifyFilters('pages.filter_hooks.pages.filter') }}
This will strip tags after content is processed by hook filters
{{ content|notifyFilters('pages.filter_hooks.pages.filter')|safeHtml }}
In first case hook filters can respond with markup added by filter while user content is filtered before
Second case filter everything so no html at all...

@craigh
Copy link
Member

craigh commented Apr 30, 2017

Hmm I'm not sure if I understand you correctly.

I'm suggesting this is the correct implementation:

{{ content|notifyFilters('pages.filter_hooks.pages.filter')|safeHtml }}

@Kaik
Copy link
Contributor Author

Kaik commented Apr 30, 2017

If is_safe =>'html' will be added to notifyFilters method then this do not really matter as it is developers choice. I think that safeHtml filter should be before notifyFilters so filter hooks can work on already html safe content, while they can inject some html that will not be filtered out.

So this PR, will be merged? or you have other plans?

@Kaik
Copy link
Contributor Author

Kaik commented Apr 30, 2017

And btw I really think we do not understand each other.

I don't we want to internally implement safeHtml within the notifyFilter filter, because its usage should be optional to the developer.

This PR is removing safeHtml filter in notifyFilter by adding 'is_safe' => 'html' option this is disabling twig internall safeHtml filter https://twig.sensiolabs.org/doc/2.x/advanced.html#automatic-escaping
We could go further and use 'pre_escape' => 'html' as well and we could remove safeHtml filter from templates compleatly
$filter = new Twig_Filter('somefilter', 'somefilter', array('pre_escape' => 'html', 'is_safe' => array('html')));

@craigh
Copy link
Member

craigh commented Jun 23, 2017

@Kaik please sign the clahub agreement

@craigh craigh merged commit f9b640b into zikula:1.5 Jun 23, 2017
@craigh
Copy link
Member

craigh commented Jun 23, 2017

thank you @Kaik

@Kaik Kaik deleted the patch-2 branch December 25, 2017 00:09
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.

2 participants