mirror of
https://github.com/hapifhir/org.hl7.fhir.core.git
synced 2025-03-09 14:31:17 +00:00
Add parsing DTD entity declarations
This commit is contained in:
parent
6707d5a59f
commit
847858c131
@ -120,6 +120,7 @@ public class XhtmlParser {
|
|||||||
|
|
||||||
private Set<String> elements = new HashSet<String>();
|
private Set<String> elements = new HashSet<String>();
|
||||||
private Set<String> attributes = new HashSet<String>();
|
private Set<String> attributes = new HashSet<String>();
|
||||||
|
private Map<String, String> entities = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
public XhtmlParser() {
|
public XhtmlParser() {
|
||||||
@ -522,8 +523,9 @@ private boolean elementIsOk(String name) throws FHIRFormatError {
|
|||||||
readChar();
|
readChar();
|
||||||
if (peekChar() == '!') {
|
if (peekChar() == '!') {
|
||||||
String sc = readToCommentEnd();
|
String sc = readToCommentEnd();
|
||||||
if (sc.startsWith("DOCTYPE"))
|
// moved the validator
|
||||||
throw new FHIRFormatError("Malformed XHTML: Found a DocType declaration, and these are not allowed (XXE security vulnerability protection)");
|
// if (sc.startsWith("DOCTYPE"))
|
||||||
|
// throw new FHIRFormatError("Malformed XHTML: Found a DocType declaration, and these are not allowed (XXE security vulnerability protection)");
|
||||||
node.addComment(sc).setLocation(markLocation());
|
node.addComment(sc).setLocation(markLocation());
|
||||||
} else if (peekChar() == '?')
|
} else if (peekChar() == '?')
|
||||||
node.addComment(readToTagEnd()).setLocation(markLocation());
|
node.addComment(readToTagEnd()).setLocation(markLocation());
|
||||||
@ -786,6 +788,8 @@ private boolean elementIsOk(String name) throws FHIRFormatError {
|
|||||||
readChar();
|
readChar();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean doctypeEntities = false;
|
||||||
|
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
while (!done) {
|
while (!done) {
|
||||||
char c = peekChar();
|
char c = peekChar();
|
||||||
@ -799,8 +803,16 @@ private boolean elementIsOk(String name) throws FHIRFormatError {
|
|||||||
s.append("--");
|
s.append("--");
|
||||||
} else
|
} else
|
||||||
s.append('-');
|
s.append('-');
|
||||||
} else if (simple && peekChar() == '>') {
|
} else if (doctypeEntities && c == ']') {
|
||||||
|
s.append(readChar());
|
||||||
|
if (peekChar() == '>') {
|
||||||
done = true;
|
done = true;
|
||||||
|
}
|
||||||
|
} else if (simple && peekChar() == '>' && !doctypeEntities) {
|
||||||
|
done = true;
|
||||||
|
} else if (c == '[' && s.toString().startsWith("DOCTYPE ")) {
|
||||||
|
doctypeEntities = true;
|
||||||
|
s.append(readChar());
|
||||||
} else if (c != '\0')
|
} else if (c != '\0')
|
||||||
s.append(readChar());
|
s.append(readChar());
|
||||||
else if (mustBeWellFormed)
|
else if (mustBeWellFormed)
|
||||||
@ -811,9 +823,30 @@ private boolean elementIsOk(String name) throws FHIRFormatError {
|
|||||||
readChar();
|
readChar();
|
||||||
skipWhiteSpace();
|
skipWhiteSpace();
|
||||||
}
|
}
|
||||||
|
if (doctypeEntities) {
|
||||||
|
parseDoctypeEntities(s.toString());
|
||||||
|
}
|
||||||
return s.toString();
|
return s.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void parseDoctypeEntities(String s) {
|
||||||
|
while (s.contains("<!ENTITY")) {
|
||||||
|
s = s.substring(s.indexOf("<!ENTITY"));
|
||||||
|
int e = s.indexOf(">");
|
||||||
|
String ed = s.substring(0, e+1);
|
||||||
|
s = s.substring(e+1);
|
||||||
|
ed = ed.substring(8).trim();
|
||||||
|
e = ed.indexOf(" ");
|
||||||
|
String n = ed.substring(0, e).trim();
|
||||||
|
ed = ed.substring(e).trim();
|
||||||
|
e = ed.indexOf(" "); // SYSTEM
|
||||||
|
ed = ed.substring(e).trim();
|
||||||
|
String v = ed.substring(0, ed.length()-1);
|
||||||
|
entities.put(n, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isNameChar(char ch)
|
private boolean isNameChar(char ch)
|
||||||
{
|
{
|
||||||
return Character.isLetterOrDigit(ch) || ch == '_' || ch == '-' || ch == ':';
|
return Character.isLetterOrDigit(ch) || ch == '_' || ch == '-' || ch == ':';
|
||||||
@ -1125,6 +1158,8 @@ private boolean elementIsOk(String name) throws FHIRFormatError {
|
|||||||
s.append((char) 8221);
|
s.append((char) 8221);
|
||||||
else if (c.equals("rdquo"))
|
else if (c.equals("rdquo"))
|
||||||
s.append((char) 201D);
|
s.append((char) 201D);
|
||||||
|
else if (entities.containsKey(c))
|
||||||
|
s.append(entities.get(c));
|
||||||
else
|
else
|
||||||
throw new FHIRFormatError("unable to parse character reference '" + c + "'' (last text = '" + lastText + "'" + descLoc());
|
throw new FHIRFormatError("unable to parse character reference '" + c + "'' (last text = '" + lastText + "'" + descLoc());
|
||||||
}
|
}
|
||||||
|
@ -73,6 +73,12 @@ public class XhtmlNodeTest {
|
|||||||
Assertions.assertEquals("<div xmlns=\"http://www.w3.org/1999/xhtml\"><img src=\"http://pbs.twimg.com/profile_images/544507893991485440/r_vo3uj2_bigger.png\" alt=\"Twitter Avatar\"/>@fhirabend</div>", output);
|
Assertions.assertEquals("<div xmlns=\"http://www.w3.org/1999/xhtml\"><img src=\"http://pbs.twimg.com/profile_images/544507893991485440/r_vo3uj2_bigger.png\" alt=\"Twitter Avatar\"/>@fhirabend</div>", output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseXXE() {
|
||||||
|
XhtmlNode dt = new XhtmlNode();
|
||||||
|
dt.setValueAsString("<div xmlns=\"http://www.w3.org/1999/xhtml\">\n <!DOCTYPE foo [ <!ENTITY xxe SYSTEM \"file://c:\\temp\\xxe.txt\">]>\n <p>This is some narrative &xxe;</p>\n </div>");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSerializable() throws IOException {
|
public void testSerializable() throws IOException {
|
||||||
XhtmlNode node = new XhtmlNode();
|
XhtmlNode node = new XhtmlNode();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user