snoopdave commented on code in PR #397:
URL: https://github.com/apache/roller/pull/397#discussion_r4711965447
##########
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 "&" 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 "&" 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:
🐞Claude Issue: **Nit:** a use with `escapeHtml="false"` passes. That is
right in an HTML attribute, but not in a JavaScript string: with
`escapeHtml="false" escapeJavaScript="true"`, as at `MediaFileSidebar.jsp:133`,
the `&` from `<s:url>` reaches the browser unchanged. That URL has one
parameter, so it is fine today. Exempting only when `escapeJavaScript` is
absent would close it.
##########
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 "&" 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 "&" 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:
🐞Claude Issue: **Important:** this only finds uses written as `NAME` or
`%{NAME}`. `#NAME` and `%{#NAME}` are skipped, and several JSPs use that form
(`Pings.jsp:78`, `PingTargets.jsp:63`, `CreateDatabase.jsp:41`). None of those
URLs has two parameters today, so nothing is missed yet. Allowing an optional
`#` would cover them.
It also checks only `<s:property>` and `<s:param>`, not `<s:a
href="%{NAME}">` (13 uses, e.g. `Entries.jsp:102`, `EntrySidebar.jsp:60`). If
the `s:a` template escapes `href`, those links have the same problem; if not, a
comment saying so would help.
##########
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 "&" 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 "&" 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:
🐞Claude Issue: **Nit:** this and the `use` pattern below are compiled inside
the loop. The `var=` pattern could be a constant like `URL_WITH_BODY`.
##########
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 "&" 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 "&" 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:
🐞Claude Issue: **Nit:** this searches the whole file, not only after this
`<s:url>`. `EntrySidebar.jsp` defines `editUrl` four times, so a use can be
matched to the wrong definition. Matching from `url.end()` up to the next
`<s:url` that sets the same var would fix it.
--
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]