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

[RTM] Allow to disable input encoding for a whole dca #708

Merged
merged 5 commits into from
Apr 21, 2017
Merged

[RTM] Allow to disable input encoding for a whole dca #708

merged 5 commits into from
Apr 21, 2017

Conversation

Toflar
Copy link
Member

@Toflar Toflar commented Feb 15, 2017

Here's another feature for Contao 4.4.

For my own modules I want to be able to disable the input encoding all together and output stuff using twig or encode it myself. I have an application which even doesn't output anything at all but manages content that's fetched through an API. I have to set the eval attributes on every field which is a bit tedious. I think it can be very helpful to disable this completely and prepare for the future already in your own bundles/applications :-)

@aschempp
Copy link
Member

Input would still be encoded, right? I mean script tags and null bytes stripped and other "safety checks". We would need to use the request class instead of the input class if the flag is enabled, right?

I mean I'm totally fine with the feature, but the name is a bit confusing. It would better be something like allowHtmlForAll 😂

@Toflar
Copy link
Member Author

Toflar commented Feb 15, 2017

It will use Input::postRaw() with this config.

@leofeyer
Copy link
Member

Input::postRaw() applies the following filters:

  • Input::preserveBasicEntities()
  • Input::xssClean()
  • Input::encodeInsertTags() (FE only)

I guess that's why @aschempp suggested to use the Symfony Request class in this case, which will not alter the input values at all.

@leofeyer leofeyer changed the title Allow to disable input encoding for a whole dca [RFC] Allow to disable input encoding for a whole dca Feb 17, 2017
@Toflar
Copy link
Member Author

Toflar commented Feb 17, 2017

Ah, I see. Yeah, so we already have our own use case for #674 😄

@aschempp
Copy link
Member

You mean the core would apply that if the flag is set? 😂

@Toflar
Copy link
Member Author

Toflar commented Feb 22, 2017

I don't have another option I guess :) So we need both, a new config and a new eval flag. How would you like to name it? What about disableInputEncoding is not fine? It's pretty much stating what it does :-)

@aschempp
Copy link
Member

It depends on the implementation. If it means flag all fields with decodeEntities or thelike I'm fine with that. But if it means we use Symfony Request I would prefer something else, like useSymfonyRequest

@Toflar
Copy link
Member Author

Toflar commented Feb 24, 2017

What do the others think? /cc @contao/developers

@discordier
Copy link
Contributor

I'd do some flag on dca level and forward this to the Widget class.
In the widget class, we check the flag and it will then use the raw value from the request.

I think we could combine this with my changes from #575, sadly I still did not come around to shift the remaining methods from Input over to StringUtils so it would be RTM.

@ausi
Copy link
Member

ausi commented Feb 25, 2017

Looking at this, I thought we must already have such a flag for the password fields in the backend, but it looks like we haven’t. If I change my password in the backend to foobar\0baz I have to login with foobarbaz. So this would be another use case for this feature.

I think the name disableInputEncoding is fine. But I’m not sure if we should use the Symfony request or Input::postUnsafeRaw() instead.

@discordier
Copy link
Contributor

Request, definately!

@Toflar
Copy link
Member Author

Toflar commented Feb 27, 2017

Yeah, request for sure but why should I name the eval option useSymfonyRequest? It's just the regular, unmodified value coming from the current request. Whether it is a Symfony subrequest or not does not matter, it's just representing the current request value. So I'm still all for disableInputEncoding.

@bytehead
Copy link
Member

I'm fine with disableInputEncoding.

@discordier
Copy link
Contributor

So am I.

@Toflar
Copy link
Member Author

Toflar commented Feb 28, 2017

Okay, but I don't want to rebase all the time. I'll only finish once @leofeyer merged #674.

@leofeyer
Copy link
Member

leofeyer commented Mar 23, 2017

As discussed in Mumble on March 23rd, the flag shall be called useRawRequestData and work on table level and on field level.

@Toflar
Copy link
Member Author

Toflar commented Mar 23, 2017

Note to self: Check password fields (they should not strip null bytes etc.)

…it to either a field or the whole table configuration and fixed encoding on password fields
@Toflar
Copy link
Member Author

Toflar commented Mar 28, 2017

PR is updated and ready for review.

@@ -263,6 +265,14 @@ public function __set($strKey, $varValue)
$this->strPrefix = $varValue;
break;

case 'useRawRequestData':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you have declared the property as a boolean, we should handle the false case as well, shouldn't we?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah not all attributes do unfortunately. So it's inconsistent already anyway but I added it.

@leofeyer leofeyer added this to the 4.4.0 milestone Mar 28, 2017
{
/** @var Request $request */
$request = \System::getContainer()->get('request_stack')->getCurrentRequest();
$this->setInputCallback(function() use ($request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could (but don't have to) simplify this:

$this->setInputCallback(function () {
    return \System::getContainer()->get('request_stack')->getCurrentRequest()->request->get($this->name);
});

@@ -273,6 +273,12 @@ protected function row($strPalette=null)
$this->varValue = \StringUtil::insertTagToSrc($this->varValue);
}

// Use raw request if set globally
if (isset($GLOBALS['TL_DCA'][$this->strTable]['config']['useRawRequestData']) && $GLOBALS['TL_DCA'][$this->strTable]['config']['useRawRequestData'] === true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a !isset($arrData['eval']['useRawRequestData']) check here too? This would make it possible to use the raw request for the whole DCA but opt-out of it for single fields.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, added in fd2ef3f.

/** @var Request $request */
$request = \System::getContainer()->get('request_stack')->getCurrentRequest();
$this->setInputCallback(function() use ($request) {
return $request->request->get($this->name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Widget::getPost() supports array syntax like field[foo][bar], do we need this functionality here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. If you want the raw request data you need to handle the array yourself if you need to, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea where this array syntax is needed, maybe @aschempp can help us here? See Widget.php:781

@ausi
Copy link
Member

ausi commented Mar 28, 2017

Why did you use setInputCallback()? Couldn’t this be buggy in some cases? e.g.:

$widget->setInputCallback(...);
$widget->useRawRequestData = true;

The second line would overwrite the already set input callback. Couldn’t we just add a check for the useRawRequestData flag in the Widget::getPost() method?

@Toflar
Copy link
Member Author

Toflar commented Mar 28, 2017

he second line would overwrite the already set input callback.

Exactly. Which is the desired workflow. There can only ever be one responsible function fetching the input. If you handle useRawRequestData within getPost() the setInputCallback() method could result having no effect at all which is very confusing I find.

@ausi
Copy link
Member

ausi commented Mar 28, 2017

What about this example:

$widget->useRawRequestData = true;
$widget->setInputCallback(null);

This would not use the raw request.

I think this is a confusing public API, if useRawRequestData would just be a simple flag, I could check for this flag in my own input callback too:

$widget->setInputCallback(function() {
	if ($widget->useRawRequestData) {
		...
	}
});

@Toflar
Copy link
Member Author

Toflar commented Mar 28, 2017

I think this is a confusing public API, if useRawRequestData would just be a simple flag, I could check for this flag in my own input callback too:

Which means that you can change the behavior someone else wanted. In my opinion both ways can be confusing. It's just the way it is.

@ausi
Copy link
Member

ausi commented Mar 28, 2017

Isn’t this inconsistent? allowHtml => Input::postHtml() and preserveTags => Input::postRaw() are already two flags that Widget::getPost() handles, why should useRawRequestData behave differently than the existing flags?

@Toflar
Copy link
Member Author

Toflar commented Mar 28, 2017

No idea. Because it's cleaner? :) I changed it.

@Toflar Toflar changed the title [RFC] Allow to disable input encoding for a whole dca [RTM] Allow to disable input encoding for a whole dca Mar 28, 2017
@@ -83,6 +84,7 @@
* @property string $slabel The submit button label
* @property boolean $preserveTags Preserve HTML tags
* @property boolean $decodeEntities Decode HTML entities
* @property boolean useRawRequestData Use the raw request data from the Symfony request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add useRawRequestData to the switch-case on line 345 too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jop! Added in 31732f9.

@@ -320,6 +321,8 @@ public function authenticate()
*/
public function login()
{
/** @var Request $request */
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a behavior change, shouldn't we use getMasterRequest?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's almost always wrong to take the master request. The use cases are only very, very limited. So no: the current request is the way to go.

if ($this->useRawRequestData === true)
{
/** @var Request $request */
$request = \System::getContainer()->get('request_stack')->getCurrentRequest();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I think we should use the master request?

@leofeyer leofeyer self-assigned this Apr 21, 2017
@leofeyer leofeyer merged commit 750cac6 into contao:develop Apr 21, 2017
@Toflar Toflar deleted the feature/disable-input-encoding branch April 24, 2017 09:45
leofeyer pushed a commit that referenced this pull request Sep 26, 2019
Description
-----------

While writing tests for #708 I noticed that the `VarDumper` component isn't actually required by the `core-bundle`, even though `\Contao\Template::dumpTemplateVars()` uses it. This PR adds the dependency and also adds a test for it.

_Note:_ this needs to be added to the `composer.json` of the `contao/contao` mono repository as well presumably, but I am not sure what the procedure here is?

_Note:_ this requirement is not needed for the `4.4` branch - but the test could be added there. I can provide a separate PR for that.

Commits
-------

6ee2945c require symfony/var-dumper
61bd510f simplify dumpTemplateVars test
c47b9961 remove unused use statements
2db49377 Fix the Travis build
9dbbb302 Sync the composer.json files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants