Skip to content

Commit a6809be

Browse files
committed
Fix prompt parsing issues
1 parent c323023 commit a6809be

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

semantickernel-api/src/main/java/com/microsoft/semantickernel/implementation/chatcompletion/ChatXMLPromptParser.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Locale;
1919
import java.util.Map;
2020
import javax.annotation.Nullable;
21+
import javax.xml.XMLConstants;
2122
import javax.xml.namespace.QName;
2223
import javax.xml.stream.XMLEventReader;
2324
import javax.xml.stream.XMLInputFactory;
@@ -32,6 +33,29 @@ public class ChatXMLPromptParser {
3233

3334
private static final Logger LOGGER = LoggerFactory.getLogger(ChatXMLPromptParser.class);
3435

36+
private static XMLInputFactory createXMLInputFactory() {
37+
XMLInputFactory factory = XMLInputFactory.newInstance();
38+
39+
trySetProperty(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
40+
trySetProperty(factory, XMLConstants.ACCESS_EXTERNAL_DTD, "");
41+
trySetProperty(factory, XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
42+
43+
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
44+
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
45+
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
46+
47+
return factory;
48+
}
49+
50+
private static void trySetProperty(XMLInputFactory factory, String property, Object value) {
51+
try {
52+
factory.setProperty(property, value);
53+
} catch (IllegalArgumentException e) {
54+
// Property not supported by this XMLInputFactory implementation
55+
LOGGER.trace("XMLInputFactory property '{}' not supported", property);
56+
}
57+
}
58+
3559
public static <T> ChatPromptParseVisitor<T> parse(
3660
String rawPrompt,
3761
ChatPromptParseVisitor<T> chatPromptParseVisitor) {
@@ -64,7 +88,7 @@ private static <T> ChatPromptParseVisitor<T> getChatRequestMessages(String promp
6488
// In this way, we can avoid parsing the whole prompt twice and easily extend the parsing logic.
6589

6690
try (InputStream is = new ByteArrayInputStream(prompt.getBytes(StandardCharsets.UTF_8))) {
67-
XMLInputFactory factory = XMLInputFactory.newInstance();
91+
XMLInputFactory factory = createXMLInputFactory();
6892
XMLEventReader reader = factory.createXMLEventReader(is);
6993
while (reader.hasNext()) {
7094
XMLEvent event = reader.nextEvent();
@@ -109,7 +133,7 @@ private static <T> ChatPromptParseVisitor<T> getFunctionDefinitions(String promp
109133
// </function>
110134

111135
try (InputStream is = new ByteArrayInputStream(prompt.getBytes(StandardCharsets.UTF_8))) {
112-
XMLInputFactory factory = XMLInputFactory.newInstance();
136+
XMLInputFactory factory = createXMLInputFactory();
113137
XMLEventReader reader = factory.createXMLEventReader(is);
114138
FunctionDefinition functionDefinition = null;
115139
Map<String, String> parameters = new HashMap<>();

0 commit comments

Comments
 (0)