Ghidra is a software reverse engineering (SRE) framework
GP-7348 - Minor changes for PR to escape error dialog html
dragonmacherf57fb10577bc
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/ErrLogExpandableDialog.java b/Ghidra/Framework/Docking/src/main/java/docking/ErrLogExpandableDialog.javaindex 0fb2b31960..c8323e4e04 100644--- a/Ghidra/Framework/Docking/src/main/java/docking/ErrLogExpandableDialog.java+++ b/Ghidra/Framework/Docking/src/main/java/docking/ErrLogExpandableDialog.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -32,9 +32,9 @@ import docking.widgets.label.GHtmlLabel;import docking.widgets.tree.*;import docking.widgets.tree.support.GTreeDragNDropHandler;import generic.theme.GIcon;-import ghidra.util.*;+import ghidra.util.HTMLUtilities;+import ghidra.util.Msg;import ghidra.util.exception.MultipleCauses;-import ghidra.util.html.HTMLElement;import util.CollectionUtils;public class ErrLogExpandableDialog extends AbstractErrDialog {@@ -178,37 +178,23 @@ public class ErrLogExpandableDialog extends AbstractErrDialog {private String getHTML(String msg, Collection<Throwable> report) {- //- // Usage question: The content herein will be escaped unless you call addHTMLContenet().- // Further, clients can provide messages that contain HTML. Is there a- // use case where we want to show escaped HTML content?- //- // For now, I will assume no such use case exists, and allow HTML to go- // through.- //- // If no such use case exists, then we should update HTMLElement to- // not escape HTML. If it does exist, then we should put the onus on- // the client.- //-HTMLElement html = new HTMLElement("html");HTMLElement body = html.addElement("body");if (msg != null) {if (msg.startsWith(HTMLUtilities.HTML)) {- // already HTML from the user- body.addHTMLContent(msg);+ body.addHTMLContent(msg); // already HTML from the user}else {String withBRs = addBR(msg);- body./*addElement("h3").*/addHTMLContent(withBRs);+ body.addHTMLContent(withBRs);}}for (Throwable t : report) {String tMsg = getMessage(t);- if (SystemUtilities.isEqual(msg, tMsg)) {+ if (Objects.equals(msg, tMsg)) {// Don't put the same message on twice. Some clients call this dialog with// the message as simply Throwable.getMessage().continue;@@ -716,3 +702,75 @@ class TransferActionListener implements ActionListener, PropertyChangeListener {}}}++class HTMLElement {++ // this contains content and elements+ private List<Object> contents = new ArrayList<>();++ private String name;+ private Map<String, String> attributes = new HashMap<>();++ HTMLElement(String name) {+ this.name = name;+ }++ String getAttribute(String key) {+ return attributes.get(key);+ }++ String putAttribute(String key, String value) {+ return attributes.put(key, value);+ }++ String removeAttribute(String key) {+ return attributes.remove(key);+ }++ HTMLElement addElement(String elementName) {+ HTMLElement newElement = new HTMLElement(elementName);+ contents.add(newElement);+ return newElement;+ }++ void addHTMLContent(String htmlContent) {+ HTMLContent html = new HTMLContent(htmlContent);+ contents.add(html);+ }++ @Override+ public String toString() {+ // this element's opening tag+ StringBuilder sb = new StringBuilder("<" + name);+ for (Map.Entry<String, String> ent : attributes.entrySet()) {+ sb.append(" ");+ sb.append(ent.getKey());+ sb.append("=\"");+ sb.append(ent.getValue());+ sb.append("\"");+ }+ sb.append(">");++ for (Object obj : contents) {+ // either an HTMLElement or an HTMLContent+ sb.append(obj.toString());+ }++ // closing tag+ sb.append("</" + name + ">");+ return sb.toString();+ }++ private class HTMLContent {+ private String content;++ HTMLContent(String content) {+ this.content = content;+ }++ @Override+ public String toString() {+ return content;+ }+ }+}diff --git a/Ghidra/Framework/Docking/src/test.slow/java/docking/DockingErrorDisplayTest.java b/Ghidra/Framework/Docking/src/test.slow/java/docking/DockingErrorDisplayTest.javaindex fabb185d6a..f1b208b8c9 100644--- a/Ghidra/Framework/Docking/src/test.slow/java/docking/DockingErrorDisplayTest.java+++ b/Ghidra/Framework/Docking/src/test.slow/java/docking/DockingErrorDisplayTest.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -92,6 +92,42 @@ public class DockingErrorDisplayTest extends AbstractDockingTest {close(dialog);}+ @Test+ public void testErrLogDialog_Html_AngleBracketIsEscaped() {+ String result = ErrLogExpandableDialog.addBR("a < b");+ assertTrue("'<' must be escaped to '<' so it is not interpreted as an HTML tag",+ result.contains("<"));+ assertFalse("raw '<' must not appear in HTML output", result.contains("< b"));+ }++ @Test+ public void testErrLogDialog_Html_AmpersandIsEscaped() {+ String result = ErrLogExpandableDialog.addBR("foo & bar");+ assertTrue("'&' must be escaped to '&'", result.contains("&"));+ }++ @Test+ public void testErrLogDialog_Html_HtmlTagsInPlainTextAreEscaped() {+ // addBR is only invoked for non-HTML messages (getHTML handles <html>-prefixed+ // messages separately), so if an angle-bracket tag appears in plain text it must+ // be treated as literal characters, not markup.+ String result = ErrLogExpandableDialog.addBR("<b>bold?</b>");+ assertFalse("HTML tag must not be rendered as markup", result.contains("<b>"));+ assertTrue("escaped opening tag must appear", result.contains("<b>"));+ }++ @Test+ public void testErrLogDialog_Html_NewlinesConvertedToBreakTags() {+ String result = ErrLogExpandableDialog.addBR("line1\nline2");+ assertTrue("newline must be converted to a <BR> tag", result.contains("<br>"));+ assertTrue("first line content must be present", result.contains("line1"));+ assertTrue("second line content must be present", result.contains("line2"));+ }++//=================================================================================================+// Private Methods+//=================================================================================================+private void assertExceptionCount(AbstractErrDialog errDialog, int n) {int actual = errDialog.getExceptionCount();diff --git a/Ghidra/Framework/Docking/src/test/java/docking/ErrLogExpandableDialogAddBRTest.java b/Ghidra/Framework/Docking/src/test/java/docking/ErrLogExpandableDialogAddBRTest.javadeleted file mode 100644index be6748e985..0000000000--- a/Ghidra/Framework/Docking/src/test/java/docking/ErrLogExpandableDialogAddBRTest.java+++ /dev/null@@ -1,81 +0,0 @@-/* ###- * IP: GHIDRA- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package docking;--import static org.junit.Assert.*;--import org.junit.Test;--/**- * Tests the string-building helper {@link ErrLogExpandableDialog#addBR(String)} in isolation,- * without requiring a Swing display environment. The helper is the single point where plain-text- * error messages are converted into HTML-fragment form before being inserted into an HTML body- * element; verifying it here ensures the fix for angle-bracket dropping (GitHub issue #1322)- * is correct without needing a headless GUI test harness.- */-public class ErrLogExpandableDialogAddBRTest {-- @Test- public void testAngleBracketIsEscaped() {- String result = ErrLogExpandableDialog.addBR("a < b");- assertTrue("'<' must be escaped to '<' so it is not interpreted as an HTML tag",- result.contains("<"));- assertFalse("raw '<' must not appear in HTML output", result.contains("< b"));- }-- @Test- public void testTextAfterAngleBracketIsPreserved() {- String result = ErrLogExpandableDialog.addBR("value < threshold: abort");- assertTrue("text after '<' must not be swallowed", result.contains("threshold"));- assertTrue("text before '<' must be preserved", result.contains("value"));- }-- @Test- public void testAmpersandIsEscaped() {- String result = ErrLogExpandableDialog.addBR("foo & bar");- assertTrue("'&' must be escaped to '&'", result.contains("&"));- }-- @Test- public void testGreaterThanIsEscaped() {- String result = ErrLogExpandableDialog.addBR("a > b");- assertTrue("'>' must be escaped to '>'", result.contains(">"));- }-- @Test- public void testNewlinesConvertedToBreakTags() {- String result = ErrLogExpandableDialog.addBR("line1\nline2");- assertTrue("newline must be converted to a <BR> tag", result.toLowerCase().contains("<br>"));- assertTrue("first line content must be present", result.contains("line1"));- assertTrue("second line content must be present", result.contains("line2"));- }-- @Test- public void testPlainTextWithNoSpecialCharsUnchanged() {- String result = ErrLogExpandableDialog.addBR("simple message");- assertTrue("plain text must pass through intact", result.contains("simple message"));- }-- @Test- public void testHtmlTagsInPlainTextAreEscaped() {- // addBR is only invoked for non-HTML messages (getHTML handles <html>-prefixed- // messages separately), so if an angle-bracket tag appears in plain text it must- // be treated as literal characters, not markup.- String result = ErrLogExpandableDialog.addBR("<b>bold?</b>");- assertFalse("HTML tag must not be rendered as markup", result.contains("<b>"));- assertTrue("escaped opening tag must appear", result.contains("<b>"));- }-}diff --git a/Ghidra/Framework/Gui/src/main/java/ghidra/util/html/HTMLElement.java b/Ghidra/Framework/Gui/src/main/java/ghidra/util/html/HTMLElement.javadeleted file mode 100644index 11a855964d..0000000000--- a/Ghidra/Framework/Gui/src/main/java/ghidra/util/html/HTMLElement.java+++ /dev/null@@ -1,94 +0,0 @@-/* ###- * IP: GHIDRA- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package ghidra.util.html;--import java.util.*;--import ghidra.util.HTMLUtilities;--public class HTMLElement extends ArrayList<Object> {- private String name;- private Map<String, String> attributes = new HashMap<>();-- public HTMLElement(String name) {- this.name = name;- }-- public String getAttribute(String key) {- return attributes.get(key);- }-- public String putAttribute(String key, String value) {- return attributes.put(key, value);- }-- public String removeAttribute(String key) {- return attributes.remove(key);- }-- public HTMLElement addElement(String elementName) {- HTMLElement newElement = new HTMLElement(elementName);- add(newElement);- return newElement;- }-- public void addHTMLContent(String htmlContent) {- HTMLContent html = new HTMLContent(htmlContent);- add(html);- }-- @Override- public String toString() {- StringBuilder sb = new StringBuilder("<" + name);- for (Map.Entry<String, String> ent : attributes.entrySet()) {- sb.append(" ");- sb.append(ent.getKey());- sb.append("=\"");- sb.append(ent.getValue());- sb.append("\"");- }- sb.append(">");- for (Object obj : this) {- if (obj == null) {- sb.append("(null)");- }- else if (obj instanceof HTMLElement) {- sb.append(obj.toString());- }- else if (obj instanceof HTMLContent) {- sb.append(obj.toString());- }- else {- sb.append(HTMLUtilities.friendlyEncodeHTML(obj.toString()));- }- }- sb.append("</" + name + ">");- return sb.toString();- }-- private class HTMLContent {- private String content;-- HTMLContent(String content) {- this.content = content;- }-- @Override- public String toString() {- return content;- }- }-}