Skip to content

Commit 5aa7535

Browse files
authored
Merge pull request #144 from tpoisseau/escape-xml-in-svg-depictor
fix(SVGDepictor): escape string injected in svg
2 parents c97c7a2 + b5ae6d8 commit 5aa7535

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

src/main/java/com/actelion/research/chem/SVGDepictor.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,37 @@ private void write(String s) {
9696
buffer.append("\n");
9797
}
9898

99+
public static String escapeXML(String s) {
100+
StringBuilder sb = new StringBuilder();
101+
102+
int length = s.length();
103+
for (int i = 0; i < length; i++) {
104+
int c = s.codePointAt(i);
105+
char ch = (char) c;
106+
switch (c) {
107+
case '&':
108+
sb.append("&amp;");
109+
break;
110+
case '<':
111+
sb.append("&lt;");
112+
break;
113+
case '>':
114+
sb.append("&gt;");
115+
break;
116+
case '\"':
117+
sb.append("&quot;");
118+
break;
119+
case '\'':
120+
sb.append("&apos;");
121+
break;
122+
default:
123+
sb.append(ch);
124+
}
125+
}
126+
127+
return sb.toString();
128+
}
129+
99130
@Override
100131
protected void drawBlackLine(DepictorLine theLine) {
101132
int x1 = (int) theLine.x1;
@@ -107,7 +138,7 @@ protected void drawBlackLine(DepictorLine theLine) {
107138
"y1=\"" + y1 + "\" " +
108139
"x2=\"" + x2 + "\" " +
109140
"y2=\"" + y2 + "\" " +
110-
"style=\"stroke:" + currentColor + "; stroke-width:" + lineWidth + "\"/>";
141+
"style=\"stroke:" + escapeXML(currentColor) + "; stroke-width:" + lineWidth + "\"/>";
111142
write(s);
112143
}
113144

@@ -123,7 +154,7 @@ protected void drawDottedLine(DepictorLine theLine) {
123154
"y1=\"" + y1 + "\" " +
124155
"x2=\"" + x2 + "\" " +
125156
"y2=\"" + y2 + "\" " +
126-
"style=\"stroke:" + currentColor + "; stroke-width:" + lineWidth + "\"/>";
157+
"style=\"stroke:" + escapeXML(currentColor) + "; stroke-width:" + lineWidth + "\"/>";
127158

128159
write(s);
129160
}
@@ -138,7 +169,7 @@ protected void drawPolygon(GenericPolygon p) {
138169
s.append(" ");
139170
}
140171
s.append("\" " +
141-
"style=\"fill:" + currentColor + "; stroke:" + currentColor + "; stroke-width:"+lineWidth+"\"/>");
172+
"style=\"fill:" + escapeXML(currentColor) + "; stroke:" + escapeXML(currentColor) + "; stroke-width:"+lineWidth+"\"/>");
142173
write(s.toString());
143174
}
144175

@@ -151,7 +182,7 @@ protected void drawString(String theString, double x, double y) {
151182
"stroke=\"none\" " +
152183
// "font-family=\" " + currentFont.getName() + "\" " +
153184
"font-size=\"" + currentFont.getSize() + "\" " +
154-
"fill=\"" + currentColor + "\">" + theString +
185+
"fill=\"" + escapeXML(currentColor) + "\">" + escapeXML(theString) +
155186
"</text>";
156187
write(s);
157188
}
@@ -162,7 +193,7 @@ protected void fillCircle(double x, double y, double d) {
162193
"cx=\"" + (int) (x+d/2) + "\" " +
163194
"cy=\"" + (int) (y+d/2) + "\" " +
164195
"r=\"" + (int) (d/2) + "\" " +
165-
"fill=\"" + currentColor + "\" />";
196+
"fill=\"" + escapeXML(currentColor) + "\" />";
166197
write(s);
167198
}
168199

@@ -204,7 +235,7 @@ protected void setRGB(int rgb) {
204235
@Override
205236
protected void onDrawBond(int bond, double x1, double y1, double x2, double y2) {
206237
String s = "<line " +
207-
"id=\"" + getId() + ":Bond:" + bond + "\" " +
238+
"id=\"" + escapeXML(getId()) + ":Bond:" + bond + "\" " +
208239
"class=\"event\" " + // class to respond to the mouse event
209240
"x1=\"" + (int) (x1) + "\" " +
210241
"y1=\"" + (int) (y1) + "\" " +
@@ -220,7 +251,7 @@ protected void onDrawBond(int bond, double x1, double y1, double x2, double y2)
220251
protected void onDrawAtom(int atom, String symbol, double x, double y) {
221252
int r = DEFAULT_ELEM_WIDTH;
222253
String s = "<circle " +
223-
"id=\"" + getId() + ":Atom:" + atom + "\" " +
254+
"id=\"" + escapeXML(getId()) + ":Atom:" + atom + "\" " +
224255
"class=\"event\" " + // class to respond to the mouse event
225256
"cx=\"" + (int) (x) + "\" " +
226257
"cy=\"" + (int) (y) + "\" " +
@@ -233,7 +264,7 @@ protected void onDrawAtom(int atom, String symbol, double x, double y) {
233264
@Override
234265
public String toString() {
235266
String header = "<svg " +
236-
"id=\"" + getId() + "\" " +
267+
"id=\"" + escapeXML(getId()) + "\" " +
237268
"xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" " +
238269
"width=\"" + width + "px\" " +
239270
"height=\"" + height + "px\" " +
@@ -243,9 +274,9 @@ public String toString() {
243274

244275
String style = legacyMode ?
245276
"<style>" +
246-
" #" + getId() +
277+
" #" + escapeXML(getId()) +
247278
" {pointer-events:none; } " + // Disable Mouse events on the root element so they get passed to the childs
248-
" #" + getId() + " .event " +
279+
" #" + escapeXML(getId()) + " .event " +
249280
" { pointer-events:all;} " + // Enable Mouse events for elements possessing the class "event"
250281
" </style>\n"
251282
: "<g style=\"font-size:"+getTextSize()+"px; fill-opacity:1; stroke-opacity:1; fill:black; stroke:black;"

0 commit comments

Comments
 (0)