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

dev/core#1328 Add define to optionally enable escape-on-output #21956

Merged
merged 1 commit into from
Nov 5, 2021

Conversation

eileenmcnaughton
Copy link
Contributor

@eileenmcnaughton eileenmcnaughton commented Nov 1, 2021

Overview

Add define to optionally enable escape-on-output

This PR + fixing the use of isset in the template layer are the pre-requisites for
turning escape on output on and off. Keeping this part in an unmerged branch is
tricky as the templates need to be recompiled when you switch to a branch
which does not have this function

I envisage us starting to use this define in our dev environments fairly soon
as it's working well locally for me on
#21935

Before

Only smarty variables specifically piped to escape are escaped

After

If you add the following define

define('CIVICRM_SMARTY_DEFAULT_ESCAPE', TRUE);

Then all variables are escaped.

Technical Details

Note that with this PR if you uncomment the above define your site will crash as long as there are calls to isset in the smarty templates. All of those calls have been removed in #21935 - but not with adequate care (and with some errors) . Merging this helps the process of working through those and the enotice issues since it makes it easier to switch back & forth between having default esaping on & off

Comments

#21979 is open as one PR removal of isset in the template & a bunch of those would need to be merged

https://lab.civicrm.org/dev/core/-/issues/1328#note_66924

@civibot
Copy link

civibot bot commented Nov 1, 2021

(Standard links)

@eileenmcnaughton
Copy link
Contributor Author

test this please

@eileenmcnaughton
Copy link
Contributor Author

@seamuslee001 @demeritcowboy it would make it easier to work on this if we could merge this one - since the presence or not of the function can be a bit of a pain between branches - given it is compiled into the templates

@demeritcowboy
Copy link
Contributor

What is the security team's take on the whole approach? i.e. is there concept approved? That's been my main hesitation from single handedly approving the various PRs.

@eileenmcnaughton
Copy link
Contributor Author

@demeritcowboy yeah - I chatted with @seamuslee001 & @totten today & explained where it was at & the various aspects. I think the mood was generally 'yeah ok' - I do hope that once the basics are working we might get some early adoption in dev environments from people like @pfigel & @adixon - the key parts are

  1. no change unless people set that define
  2. the default escape function has some carve-outs to not break awfully
  3. it changes how we handle things in civi in terms of not using isset in templates & reducing use of empty and also adding use of |smarty nodefaults to specify that default escaping (or modifiers in general) should not apply to a variable

@eileenmcnaughton
Copy link
Contributor Author

I just added a comment here too https://lab.civicrm.org/dev/core/-/issues/1328#note_66924

@eileenmcnaughton eileenmcnaughton changed the title Add define to optionally enable escape-on-output dev/core#1328 Add define to optionally enable escape-on-output Nov 5, 2021
@demeritcowboy
Copy link
Contributor

Ok thanks.
Noting the site will also crash if you don't clear templates_c after setting the setting, separate from isset().

One thing is that it seems to do double-escaping for any template that already has |escape.

Related but separate, it should probably check that escape isn't already in the default_modifiers list before adding it.

Otherwise I don't see any issues.

@adixon
Copy link
Contributor

adixon commented Nov 5, 2021

I love the concept! Looking forward to trying it out.

This adjusts the smarty class such that if you have the define below
escape-on-output is enabled

This PR + fixing the use of isset in the template layer are the pre-requisites for
turning escape on output on and off. Keeping this part in an unmerged branch is
tricky as the templates need to be recompiled when you switch to a branch
which does not have this function

I envisage us starting to use this define in our dev environments fairly soon
as it's working well locally for me on
civicrm#21935
@eileenmcnaughton
Copy link
Contributor Author

@demeritcowboy I've fixed it to check for the modifier

On the double escape - I guess we just need to add smarty:nodefaults - we can't remove the existing escapes

if (!isset($this->_plugins['modifier']['escape'])) {
$this->register_modifier('escape', ['CRM_Core_Smarty', 'escape']);
}
$this->default_modifiers[] = 'escape:"htmlall"';
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry what I meant was suppose $this->default_modifiers already contains something like escape:"html". Then everything would end up with double-escaping.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@demeritcowboy but then why would you add the define if you had some other method adding in a default modifier?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh.

@demeritcowboy demeritcowboy merged commit dab3c5d into civicrm:master Nov 5, 2021
@eileenmcnaughton eileenmcnaughton deleted the escape2 branch November 5, 2021 23:50
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.

3 participants