You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by ottobackwards <gi...@git.apache.org> on 2018/07/31 18:36:22 UTC

[GitHub] nifi pull request #2929: NIFI-5474 ReplaceText RegexReplace evaluates payloa...

GitHub user ottobackwards opened a pull request:

    https://github.com/apache/nifi/pull/2929

    NIFI-5474 ReplaceText RegexReplace evaluates payload as Expression language

    Thank you for submitting a contribution to Apache NiFi.
    
    In order to streamline the review of the contribution we ask you
    to ensure the following steps have been taken:
    
    ### For all changes:
    - [x] Is there a JIRA ticket associated with this PR? Is it referenced 
         in the commit message?
    
    - [x] Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
    
    - [x] Has your PR been rebased against the latest commit within the target branch (typically master)?
    
    - [x] Is your initial contribution a single, squashed commit?
    
    ### For code changes:
    - [x] Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
    - [x] Have you written or updated unit tests to verify your changes?
    - [-] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? 
    - [-] If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
    - [-] If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
    - [-] If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?
    
    ### For documentation related changes:
    - [-] Have you ensured that format looks appropriate for the output in which it is rendered?
    
    ### Note:
    Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/ottobackwards/nifi replacetext-content-el

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/nifi/pull/2929.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #2929
    
----
commit 6316b1cf324a2c0087d8554aaf5f9a4997e5a543
Author: Otto Fowler <ot...@...>
Date:   2018-07-31T18:33:49Z

    Account for Expression Language type text in the content

----


---

[GitHub] nifi pull request #2929: NIFI-5474 ReplaceText RegexReplace evaluates payloa...

Posted by ottobackwards <gi...@git.apache.org>.
Github user ottobackwards commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2929#discussion_r210073535
  
    --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ReplaceText.java ---
    @@ -701,6 +709,27 @@ private static String wrapLiterals(String possibleLiteral) {
             return replacementFinal;
         }
     
    +    /**
    +     * Escapes Expression Language like text from content Strings.
    +     * <p>
    +     * Since we do regular expression replacement on the content and then do Expression Language
    +     * evaluations afterwards, it is possible that if there are Expression Language like text
    +     * in the content that they will be evaluated when they should not be.
    +     * </p>
    +     * <p>
    +     * This function is called to escape any such construct by prefixing a second $ to the ${...} text.
    +     * </p>
    +     *
    +     * @param content the content that may contain Expression Language like text
    +     * @return A {@code String} with any Expression Language text escaped with a $.
    +     */
    +    private static String escapeExpressionsInContent(String content) {
    +        if (!content.contains("${")) {
    +            return content;
    +        }
    +        return content.replaceAll("(\\$\\{.*\\})","\\$$1");
    --- End diff --
    
    If you look at https://github.com/apache/nifi/pull/2748 and https://issues.apache.org/jira/browse/NIFI-4272,  evaluation of the EL before the regex operations resulted in incorrect behavior.  So it is evaluated after now.


---

[GitHub] nifi issue #2929: NIFI-5474 ReplaceText RegexReplace evaluates payload as Ex...

Posted by ottobackwards <gi...@git.apache.org>.
Github user ottobackwards commented on the issue:

    https://github.com/apache/nifi/pull/2929
  
    @JPercivall 


---

[GitHub] nifi pull request #2929: NIFI-5474 ReplaceText RegexReplace evaluates payloa...

Posted by ottobackwards <gi...@git.apache.org>.
Github user ottobackwards commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2929#discussion_r210089927
  
    --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java ---
    @@ -404,6 +404,27 @@ public void testRegexWithExpressionLanguageIsEscaped() throws IOException {
             out.assertContentEquals("Hello, World!");
         }
     
    +    /**
    +     * Test for NIFI-5474
    +     */
    +    @Test
    +    public void testExpressionLanguageInContentIsNotEvaluated() throws IOException {
    +        final TestRunner runner = getRunner();
    +        runner.setProperty(ReplaceText.SEARCH_VALUE, "${replaceKey}");
    +        runner.setProperty(ReplaceText.REPLACEMENT_VALUE, "${replaceValue}");
    +
    +        final Map<String, String> attributes = new HashMap<>();
    +        attributes.put("replaceKey", "Hello");
    +        attributes.put("replaceValue", "Good-bye");
    +        runner.enqueue(Paths.get("src/test/resources/hello_with_expression_like_text.txt"), attributes);
    --- End diff --
    
    done, thanks


---

[GitHub] nifi pull request #2929: NIFI-5474 ReplaceText RegexReplace evaluates payloa...

Posted by ottobackwards <gi...@git.apache.org>.
Github user ottobackwards commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2929#discussion_r210073665
  
    --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ReplaceText.java ---
    @@ -701,6 +709,27 @@ private static String wrapLiterals(String possibleLiteral) {
             return replacementFinal;
         }
     
    +    /**
    +     * Escapes Expression Language like text from content Strings.
    +     * <p>
    +     * Since we do regular expression replacement on the content and then do Expression Language
    +     * evaluations afterwards, it is possible that if there are Expression Language like text
    +     * in the content that they will be evaluated when they should not be.
    +     * </p>
    +     * <p>
    +     * This function is called to escape any such construct by prefixing a second $ to the ${...} text.
    +     * </p>
    +     *
    +     * @param content the content that may contain Expression Language like text
    +     * @return A {@code String} with any Expression Language text escaped with a $.
    +     */
    +    private static String escapeExpressionsInContent(String content) {
    +        if (!content.contains("${")) {
    +            return content;
    +        }
    +        return content.replaceAll("(\\$\\{.*\\})","\\$$1");
    --- End diff --
    
    What would be the correct way to escape this?


---

[GitHub] nifi issue #2929: NIFI-5474 ReplaceText RegexReplace evaluates payload as Ex...

Posted by ottobackwards <gi...@git.apache.org>.
Github user ottobackwards commented on the issue:

    https://github.com/apache/nifi/pull/2929
  
    I have reviewed 
    https://github.com/apache/nifi/pull/2748, I believe that is the better fix for these issues.  I'm going to close this PR.


---

[GitHub] nifi pull request #2929: NIFI-5474 ReplaceText RegexReplace evaluates payloa...

Posted by ottobackwards <gi...@git.apache.org>.
Github user ottobackwards closed the pull request at:

    https://github.com/apache/nifi/pull/2929


---

[GitHub] nifi pull request #2929: NIFI-5474 ReplaceText RegexReplace evaluates payloa...

Posted by markap14 <gi...@git.apache.org>.
Github user markap14 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2929#discussion_r210061986
  
    --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ReplaceText.java ---
    @@ -701,6 +709,27 @@ private static String wrapLiterals(String possibleLiteral) {
             return replacementFinal;
         }
     
    +    /**
    +     * Escapes Expression Language like text from content Strings.
    +     * <p>
    +     * Since we do regular expression replacement on the content and then do Expression Language
    +     * evaluations afterwards, it is possible that if there are Expression Language like text
    +     * in the content that they will be evaluated when they should not be.
    +     * </p>
    +     * <p>
    +     * This function is called to escape any such construct by prefixing a second $ to the ${...} text.
    +     * </p>
    +     *
    +     * @param content the content that may contain Expression Language like text
    +     * @return A {@code String} with any Expression Language text escaped with a $.
    +     */
    +    private static String escapeExpressionsInContent(String content) {
    +        if (!content.contains("${")) {
    +            return content;
    +        }
    +        return content.replaceAll("(\\$\\{.*\\})","\\$$1");
    --- End diff --
    
    I don't believe this is going to properly escape the Expression Language. If you encounter something simple like `${greeting}`it will work ok. But if it encounters `$${greeting}`, this will change that into `$$${greeting}` which will be interpreted as an escaped dollar-sign followed by the expression `${greeting}`.
    
    The question here is - why are we evaluating Expression Language against the content of the FlowFile? I don't think we should be doing that at all..


---

[GitHub] nifi pull request #2929: NIFI-5474 ReplaceText RegexReplace evaluates payloa...

Posted by markap14 <gi...@git.apache.org>.
Github user markap14 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2929#discussion_r210253136
  
    --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ReplaceText.java ---
    @@ -701,6 +709,27 @@ private static String wrapLiterals(String possibleLiteral) {
             return replacementFinal;
         }
     
    +    /**
    +     * Escapes Expression Language like text from content Strings.
    +     * <p>
    +     * Since we do regular expression replacement on the content and then do Expression Language
    +     * evaluations afterwards, it is possible that if there are Expression Language like text
    +     * in the content that they will be evaluated when they should not be.
    +     * </p>
    +     * <p>
    +     * This function is called to escape any such construct by prefixing a second $ to the ${...} text.
    +     * </p>
    +     *
    +     * @param content the content that may contain Expression Language like text
    +     * @return A {@code String} with any Expression Language text escaped with a $.
    +     */
    +    private static String escapeExpressionsInContent(String content) {
    +        if (!content.contains("${")) {
    +            return content;
    +        }
    +        return content.replaceAll("(\\$\\{.*\\})","\\$$1");
    --- End diff --
    
    I don't know if it's something that you can do in a regex or not... it would be difficult... you'd need to find a ${, then count backwards the number of $'s before it, and determine whether or not it's an even number. But again - then, once escaped, you'd be evaluating Expression Language against it, and we should not be evaluating the Expression Language against the content of the FlowFile at all. This would all equate to quite a lot of heavy lifting for the CPU. I do think that I have an alternate approach that would address both NIFI-5474 and NIFI-4272. Will post a PR momentarily, and you can check that out if you'd like.


---

[GitHub] nifi pull request #2929: NIFI-5474 ReplaceText RegexReplace evaluates payloa...

Posted by ottobackwards <gi...@git.apache.org>.
Github user ottobackwards commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2929#discussion_r210089885
  
    --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java ---
    @@ -404,6 +404,27 @@ public void testRegexWithExpressionLanguageIsEscaped() throws IOException {
             out.assertContentEquals("Hello, World!");
         }
     
    +    /**
    +     * Test for NIFI-5474
    --- End diff --
    
    done



---

[GitHub] nifi issue #2929: NIFI-5474 ReplaceText RegexReplace evaluates payload as Ex...

Posted by JPercivall <gi...@git.apache.org>.
Github user JPercivall commented on the issue:

    https://github.com/apache/nifi/pull/2929
  
    Hey @ottobackwards, thanks for the PR. I should be able to review this around mid next week.


---

[GitHub] nifi pull request #2929: NIFI-5474 ReplaceText RegexReplace evaluates payloa...

Posted by markap14 <gi...@git.apache.org>.
Github user markap14 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2929#discussion_r210062603
  
    --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java ---
    @@ -404,6 +404,27 @@ public void testRegexWithExpressionLanguageIsEscaped() throws IOException {
             out.assertContentEquals("Hello, World!");
         }
     
    +    /**
    +     * Test for NIFI-5474
    --- End diff --
    
    Probably makes sense to explain the actual problem here rather than referencing the JIRA number so that if looking at it later it's easier to understand


---

[GitHub] nifi issue #2929: NIFI-5474 ReplaceText RegexReplace evaluates payload as Ex...

Posted by markap14 <gi...@git.apache.org>.
Github user markap14 commented on the issue:

    https://github.com/apache/nifi/pull/2929
  
    @ottobackwards see https://github.com/apache/nifi/pull/2951 - I think this gives us what we need as alternative approach to NIFI-4272 so that we never need to evaluate Expression Language against the contents of the FlowFile at all.


---

[GitHub] nifi pull request #2929: NIFI-5474 ReplaceText RegexReplace evaluates payloa...

Posted by markap14 <gi...@git.apache.org>.
Github user markap14 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2929#discussion_r210062380
  
    --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java ---
    @@ -404,6 +404,27 @@ public void testRegexWithExpressionLanguageIsEscaped() throws IOException {
             out.assertContentEquals("Hello, World!");
         }
     
    +    /**
    +     * Test for NIFI-5474
    +     */
    +    @Test
    +    public void testExpressionLanguageInContentIsNotEvaluated() throws IOException {
    +        final TestRunner runner = getRunner();
    +        runner.setProperty(ReplaceText.SEARCH_VALUE, "${replaceKey}");
    +        runner.setProperty(ReplaceText.REPLACEMENT_VALUE, "${replaceValue}");
    +
    +        final Map<String, String> attributes = new HashMap<>();
    +        attributes.put("replaceKey", "Hello");
    +        attributes.put("replaceValue", "Good-bye");
    +        runner.enqueue(Paths.get("src/test/resources/hello_with_expression_like_text.txt"), attributes);
    --- End diff --
    
    Is there a reason that we're using an external file for this? For a simple case like this i would just use `runner.enqueue("Hello, World! ${DO NOT EVALUATE}", attributes);`


---