Skip to content

Commit 936895d

Browse files
committed
🛠️ Handle invalid charset encoding in XMLRequestExtractor
1 parent e260359 commit 936895d

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/main/java/io/github/easybill/xrviz/handler/XmlRequestExtractor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ Optional<String> validate(HttpExchange exchange) throws IOException {
3232
String encoding = detector.getDetectedCharset();
3333
detector.reset();
3434

35-
// Fallback to UTF-8 if no encoding was detected
36-
Charset charset = (encoding != null) ? Charset.forName(encoding) : StandardCharsets.UTF_8;
35+
Charset charset = null;
36+
try {
37+
charset = (encoding != null) ? Charset.forName(encoding) : StandardCharsets.UTF_8;
38+
} catch (IllegalArgumentException e) {
39+
logger.severe("Invalid charset: " + encoding);
40+
exchange.sendResponseHeaders(HttpURLConnection.HTTP_BAD_REQUEST, -1);
41+
return Optional.empty();
42+
}
3743

3844
String xml = new String(requestBody, charset);
3945

src/test/http/api-test.http

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,10 @@ Content-Type: application/xml
8787
Accept-Language: de
8888

8989
< ./base-example-utf16le.xml
90+
91+
### Generate a HTML file from a XML with broken encoding
92+
POST {{baseUrl}}/convert.html
93+
Content-Type: application/xml
94+
Accept-Language: de
95+
96+
< ./broken-encoding.xml

src/test/http/broken-encoding.xml

15 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)