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

Implement safe filter as SafeString and handle SafeString in filters, functions and expressions #394

Merged
merged 11 commits into from
Feb 4, 2020

Conversation

Joeoh
Copy link
Contributor

@Joeoh Joeoh commented Jan 31, 2020

Safe filter:

Replacement for reverted PR: #385
Implemenetation is much simplier than previous PR. Any filter which is receiving a SafeString object will now be handled without any changes to the underlying filter. If a function filter receives a SafeString and returns a String it will be wrapped in a SafeString. This is undesirable in some cases such as the ipAddr filter which does not return some direct version of the input. An override has been added to disable this wrapping for such filters. preserveSafeString can be set to false to disable the wrapping.

Documentation

Marks the value as safe which means that in an environment with automatic escaping enabled the variable will not be escaped.

Jinja docs: http://jinja.palletsprojects.com/en/2.10.x/templates/#working-with-automatic-escaping

Approach:

Wrap String with new class SafeString when the |safe filter is applied to the value. This way we can check the type in ExpressionNode and not escape it. I overwrote toString to make it work as expected here:

String result = Objects.toString(var, "");

Considerations:

Since I introduce a new type here we might need to add some implementation for this in ExpressionResolver or JinjavaInterpreterResolver. The implementation is currently only tested with |safe being used at the end of an expression.

@Joeoh Joeoh changed the title [WIP] Simplify safe filters Simplify safe filters Feb 3, 2020
@Joeoh Joeoh changed the title Simplify safe filters Implement safe filter as SafeString and handle SafeString in filters, functions and expressions Feb 3, 2020
@Joeoh Joeoh requested a review from mattcoley February 3, 2020 17:26
@Joeoh
Copy link
Contributor Author

Joeoh commented Feb 3, 2020

@mattcoley @boulter

This PR is ready for review when you get a chance. This simplifies the implementation a great deal and gives us more confidence that the behaviour will remain consistent as the individual filters have been changed minimally. This resolves this issue replicated in this PR: #392 eg: https://github.com/HubSpot/jinjava/pull/394/files#diff-aa9fe1818d891da524d556e9ed0da866R30

@Joeoh Joeoh requested a review from boulter February 3, 2020 17:28
@Joeoh Joeoh merged commit 0c06b7a into master Feb 4, 2020
@Joeoh Joeoh deleted the simplify-safe-filters branch February 4, 2020 09:40
Joeoh added a commit that referenced this pull request Feb 10, 2020
* First draft of deferring from tag including macros

* Checkstyle

* Add more tests

* Remove incorrect assert

* Add test that checks deferring macros in depth and update DeferredValue & MacroFunction

* Checkstyle

* Make constructor private and overload instance method

* Fixes #159: Adds dict support for DefaultFilter

* removes undesired code from Filter.java

* removes whitespaces from DateTimeFormatFilter.java

* Fixes Style Check Errors

* Removes stringArgs from DateTimeFormatFilter

* Revert "Fixes #159: Adds dict support for DefaultFilter"

* Changes DefaultFilter to extend AdvancedFilter

* adds PyList support to ForTag

* adds tests for ForTag

* removes escape filter from fortag test

* Fix resoncstruct end to honor trim tags

* Add reconstructImage to MacroFunction

* Add test for reconstructing macro with no trim tags

* Whitespace fix

* Implement safe filter as SafeString and handle SafeString in filters, functions and expressions (#385)

* Start implementing safe filter

* Remove comment about pass-through implementation

* Return var if it's not instance of string instead of throwing

* Add test for pass-through

* Add support for SafeString to all filters which handle Strings

* Remove utils for string reverse filter

* Handle SafeString in truncate function

* Formatting fix

* Add SafeStringFilter interface

* Handle safe strings in filters

* rm trailing space

* Formatting fixes

* Move safeFilter method to Filter IF and remove SafeFilter IF

* rm space

* Change behaviour of Urlize filter to not always return a SafeString

* Code style changes

* Remove unnecessary call to safeString

* Style fix

* Add tests to handle Urlize string being escaped and made safe

* rm hardcoded string

* rm uneeded getValue

* Add SafeString type as str

* Handle SafeString in expressions

Co-authored-by: Joe <[email protected]>

* Fix template error line numbers (#380)

* Fix line numbers

* add to some more places

* two levels deep test

* Fix case with child interpreter

* Add deprecation

* Add another test

* Update error messages

* Fix up error messages and tests

* Fix case with scopes

* Add check for inherit

* Put everything on the path stack

* always push parent

* remove callstack crud

* Set back path management

* Fix extend lineNo/position and keep track for each block

* cleanup

* Add test for extends

* Add more tests

* Check parent call stack for emtpy and line numbers

* Fix test

* Reorient line numbers when evaluating macros, make sure to pop import path off of stack

* Add test for imported macros

* Reorient line numbers when resolving blocks

* Reorient line numbers when processing extend parents

* Add tests for extends + includes

* Revert "Implement safe filter as SafeString and handle SafeString in filters, functions and expressions (#385)"

This reverts commit a6bea47.

* Implement safe filter as SafeString and handle SafeString in filters, functions and expressions (#394)

* Start implementing safe filter

* Remove comment about pass-through implementation

* Return var if it's not instance of string instead of throwing

* Add test for pass-through

* Handle safestrings and call implementor

* Add tests

* Handle SafeString in filter using kwargs

* Handle safe strings in most simple expressions

* Handle SafeString in IsUpperExp

* Handle SafeString in filters. Allow overriding from within filter

* Formatting fixes

Co-authored-by: jkollmann <[email protected]>

* First draft of deferring from tag including macros

* Checkstyle

* Add more tests

* Remove incorrect assert

* Add test that checks deferring macros in depth and update DeferredValue & MacroFunction

* Checkstyle

* Make constructor private and overload instance method

* Fix resoncstruct end to honor trim tags

* Add reconstructImage to MacroFunction

* Add test for reconstructing macro with no trim tags

* Whitespace fix

* rm duplicate statement after merge

Co-authored-by: Manish Devgan <[email protected]>
Co-authored-by: Jeff Boulter <[email protected]>
Co-authored-by: Joe <[email protected]>
Co-authored-by: Matt Coley <[email protected]>
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