mraible commented on code in PR #397:
URL: https://github.com/apache/roller/pull/397#discussion_r4712787939


##########
app/src/test/java/org/apache/roller/weblogger/ui/struts2/util/JspOutputEncodingTest.java:
##########
@@ -134,6 +136,38 @@ void propertiesOutsideScriptsEscapeHtml() throws 
IOException {
         assertEquals(Collections.emptyList(), problems);
     }
 
+    /**
+     * {@code <s:url>} writes "&" as "&amp;" unless escapeAmp is false, and
+     * {@code <s:property>} and the {@code <s:param>} strings of an
+     * {@code <s:text>} escape it again. A link built that way reaches the
+     * browser with "&amp;" in it and loses every parameter after the first.
+     */
+    @Test
+    void urlsWithParamsAreNotEscapedTwice() throws IOException {
+        List<String> problems = new ArrayList<>();
+        for (Path jsp : jsps) {
+            String text = read(jsp);
+            Matcher url = URL_WITH_BODY.matcher(text);
+            while (url.find()) {
+                Matcher var = 
Pattern.compile("\\bvar=\"([^\"]+)\"").matcher(url.group(1));
+                if (!var.find() || url.group(1).contains("escapeAmp=\"false\"")
+                        || url.group(2).split("<s:param\\b", -1).length < 3) {
+                    continue;
+                }
+                Pattern use = 
Pattern.compile("<s:(property|param)\\b[^>]*value=[\"'](%\\{)?"

Review Comment:
   Fixed in 004f75f50. Uses written as `#name` or `%{#name}` are found now; a 
throwaway two-parameter URL used as `%{#probeURL}` fails the test. I left `<s:a 
href>` unchecked on purpose and said so in a comment: the `simple/a-close.ftl` 
template writes `href` without escaping it (`${parameters.href}` in Struts 
2.5.29, `?no_esc` in 7.4.0), so the `&amp;` from `<s:url>` is escaped once, 
which is right.



##########
app/src/test/java/org/apache/roller/weblogger/ui/struts2/util/JspOutputEncodingTest.java:
##########
@@ -134,6 +136,38 @@ void propertiesOutsideScriptsEscapeHtml() throws 
IOException {
         assertEquals(Collections.emptyList(), problems);
     }
 
+    /**
+     * {@code <s:url>} writes "&" as "&amp;" unless escapeAmp is false, and
+     * {@code <s:property>} and the {@code <s:param>} strings of an
+     * {@code <s:text>} escape it again. A link built that way reaches the
+     * browser with "&amp;" in it and loses every parameter after the first.
+     */
+    @Test
+    void urlsWithParamsAreNotEscapedTwice() throws IOException {
+        List<String> problems = new ArrayList<>();
+        for (Path jsp : jsps) {
+            String text = read(jsp);
+            Matcher url = URL_WITH_BODY.matcher(text);
+            while (url.find()) {
+                Matcher var = 
Pattern.compile("\\bvar=\"([^\"]+)\"").matcher(url.group(1));
+                if (!var.find() || url.group(1).contains("escapeAmp=\"false\"")
+                        || url.group(2).split("<s:param\\b", -1).length < 3) {
+                    continue;
+                }
+                Pattern use = 
Pattern.compile("<s:(property|param)\\b[^>]*value=[\"'](%\\{)?"
+                        + Pattern.quote(var.group(1)) + "\\}?[\"'][^>]*>");
+                Matcher written = use.matcher(text);
+                while (written.find()) {
+                    if (!written.group().contains("escapeHtml=\"false\"")) {

Review Comment:
   Fixed in 004f75f50. A use with `escapeJavaScript="true"` now fails even with 
`escapeHtml="false"`; I checked it with a throwaway URL in a `<script>` string.



##########
app/src/test/java/org/apache/roller/weblogger/ui/struts2/util/JspOutputEncodingTest.java:
##########
@@ -134,6 +136,38 @@ void propertiesOutsideScriptsEscapeHtml() throws 
IOException {
         assertEquals(Collections.emptyList(), problems);
     }
 
+    /**
+     * {@code <s:url>} writes "&" as "&amp;" unless escapeAmp is false, and
+     * {@code <s:property>} and the {@code <s:param>} strings of an
+     * {@code <s:text>} escape it again. A link built that way reaches the
+     * browser with "&amp;" in it and loses every parameter after the first.
+     */
+    @Test
+    void urlsWithParamsAreNotEscapedTwice() throws IOException {
+        List<String> problems = new ArrayList<>();
+        for (Path jsp : jsps) {
+            String text = read(jsp);
+            Matcher url = URL_WITH_BODY.matcher(text);
+            while (url.find()) {
+                Matcher var = 
Pattern.compile("\\bvar=\"([^\"]+)\"").matcher(url.group(1));
+                if (!var.find() || url.group(1).contains("escapeAmp=\"false\"")
+                        || url.group(2).split("<s:param\\b", -1).length < 3) {
+                    continue;
+                }
+                Pattern use = 
Pattern.compile("<s:(property|param)\\b[^>]*value=[\"'](%\\{)?"
+                        + Pattern.quote(var.group(1)) + "\\}?[\"'][^>]*>");
+                Matcher written = use.matcher(text);

Review Comment:
   Fixed in 004f75f50. Uses are matched only from the end of the `<s:url>` to 
the next `<s:url>` that sets the same var. A throwaway check with a correct 
definition, its use, and then a second definition of the same var passes now; 
before, the use was matched to the second one.



##########
app/src/test/java/org/apache/roller/weblogger/ui/struts2/util/JspOutputEncodingTest.java:
##########
@@ -134,6 +136,38 @@ void propertiesOutsideScriptsEscapeHtml() throws 
IOException {
         assertEquals(Collections.emptyList(), problems);
     }
 
+    /**
+     * {@code <s:url>} writes "&" as "&amp;" unless escapeAmp is false, and
+     * {@code <s:property>} and the {@code <s:param>} strings of an
+     * {@code <s:text>} escape it again. A link built that way reaches the
+     * browser with "&amp;" in it and loses every parameter after the first.
+     */
+    @Test
+    void urlsWithParamsAreNotEscapedTwice() throws IOException {
+        List<String> problems = new ArrayList<>();
+        for (Path jsp : jsps) {
+            String text = read(jsp);
+            Matcher url = URL_WITH_BODY.matcher(text);
+            while (url.find()) {
+                Matcher var = 
Pattern.compile("\\bvar=\"([^\"]+)\"").matcher(url.group(1));
+                if (!var.find() || url.group(1).contains("escapeAmp=\"false\"")

Review Comment:
   Fixed in 004f75f50: the `var=` pattern is now a constant, `URL_VAR`. The use 
and redefinition patterns stay in the loop, since they are built from each 
URL's var name.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to