fix(ses): keep XML-forbidden characters out of v1 error messages (#9579)

A v1 error message that quotes request input, such as the template name or the unsupported operation message, could carry U+FFFE, U+FFFF or a surrogate without its pair into the error document. XML 1.0 forbids those, so an XML-parsing client could not read the error at all. They are now spelled out in the same 0x%04x form already used for control characters. That form was observed on SES only for control characters; what SES writes for these characters is not known, so using it here is Floci's choice, made so the error document always parses.

The XML 1.0 forbidden-character test now lives in XmlBuilder.isForbiddenAt, beside escape, and both the SES error messages and RDS log file data call it. SES still spells out every control character, and RDS still keeps tabs and writes U+FFFD. RDS now also replaces a surrogate without its pair, which it used to pass through and the UTF-8 encoder wrote as '?'.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-11-27 14:18:16 -04:00
1 parent 2485fadf06
commit d37da2fd78
8 files changed
+161 -8

No files matched your search

@@ -113,6 +113,23 @@ public final class XmlBuilder {
return s;
}
/**
* Whether the character at {@code index} cannot appear in an XML 1.0 document, escaped or not:
* a control character other than tab, line feed and carriage return, U+FFFE, U+FFFF, or a
* surrogate without its pair. {@link #escape} leaves these as they are, so a value that may
* carry one is cleaned before it is written.
*/
public static boolean isForbiddenAt(String s, int index) {
char c = s.charAt(index);
if (Character.isHighSurrogate(c)) {
return index + 1 == s.length() || !Character.isLowSurrogate(s.charAt(index + 1));
}
if (Character.isLowSurrogate(c)) {
return index == 0 || !Character.isHighSurrogate(s.charAt(index - 1));
}
return (c < ' ' && c != '\t' && c != '\n' && c != '\r') || c == '\uFFFE' || c == '\uFFFF';
}
/** Appends {@code s} from {@code from} with the XML special characters replaced, in runs. */
private static void appendEscaped(StringBuilder out, String s, int from) {
int len = s.length();
@@ -2,6 +2,7 @@ package io.floci.aws.services.rds;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.Utf8;
import io.floci.aws.core.common.XmlBuilder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
@@ -67,14 +68,15 @@ final class RdsLogFiles {
}
/**
* Replaces the control characters XML 1.0 cannot carry (a terminal colour code, a stray NUL)
* with U+FFFD, so {@code LogFileData} always parses; tabs stay.
* Replaces the characters XML 1.0 cannot carry (a terminal colour code, a stray NUL, a surrogate
* without its pair) and any other control character with U+FFFD, so {@code LogFileData} always
* parses; tabs stay.
*/
static String xmlSafe(String text) {
StringBuilder out = null;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
boolean bad = (c < 0x20 && c != '\t') || c == 0xFFFE || c == 0xFFFF;
boolean bad = (c < 0x20 && c != '\t') || XmlBuilder.isForbiddenAt(text, i);
if (bad && out == null) {
out = new StringBuilder(text.length());
out.append(text, 0, i);
@@ -48,6 +48,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.IntStream;
/**
* Query-protocol handler for SES actions.
@@ -182,7 +183,8 @@ public class SesQueryHandler {
case "ListReceiptFilters" -> handleListReceiptFilters(region);
case "DeleteReceiptFilter" -> handleDeleteReceiptFilter(params, region);
default -> AwsQueryResponse.error("UnsupportedOperation",
"Operation " + action + " is not supported by SES.", AwsNamespaces.SES, 400);
spellOutControlCharacters("Operation " + action + " is not supported by SES."),
AwsNamespaces.SES, 400);
};
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), spellOutControlCharacters(e.getMessage()),
@@ -191,17 +193,18 @@ public class SesQueryHandler {
}
/**
* SES writes a control character a message quotes as {@code 0x0009}, which also keeps the
* characters XML 1.0 forbids out of the error document.
* SES writes a control character a message quotes as {@code 0x0009}. The same form is used for
* the other characters XML 1.0 forbids (U+FFFE, U+FFFF, a surrogate without its pair), where
* what SES writes is not known, so that the error document always parses.
*/
static String spellOutControlCharacters(String message) {
if (message == null || message.chars().noneMatch(c -> c < ' ')) {
if (message == null || IntStream.range(0, message.length()).noneMatch(i -> isSpelledOut(message, i))) {
return message;
}
StringBuilder out = new StringBuilder(message.length() + 8);
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (c < ' ') {
if (isSpelledOut(message, i)) {
out.append(String.format(Locale.ROOT, "0x%04x", (int) c));
} else {
out.append(c);
@@ -210,6 +213,10 @@ public class SesQueryHandler {
return out.toString();
}
private static boolean isSpelledOut(String message, int index) {
return message.charAt(index) < ' ' || XmlBuilder.isForbiddenAt(message, index);
}
private Response handleVerifyEmailIdentity(MultivaluedMap<String, String> params, String region) {
String emailAddress = getParam(params, "EmailAddress");
identityService.verifyEmailIdentity(emailAddress, region);
@@ -0,0 +1,33 @@
package io.floci.aws.core.common;
import org.junit.jupiter.api.Test;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
class XmlBuilderTest {
private static String forbiddenIndexes(String s) {
return IntStream.range(0, s.length())
.filter(i -> XmlBuilder.isForbiddenAt(s, i))
.mapToObj(Integer::toString)
.reduce((a, b) -> a + "," + b)
.orElse("");
}
@Test
void isForbiddenAt_allowsTabLineFeedAndCarriageReturn() {
assertEquals("", forbiddenIndexes("a\tb\nc\rd né \uFFFD"));
}
@Test
void isForbiddenAt_flagsOtherControlCharactersAndTheTwoNoncharacters() {
assertEquals("1,3,5,7", forbiddenIndexes("a\u0000b\u001bc\uFFFEd\uFFFF"));
}
@Test
void isForbiddenAt_flagsOnlyASurrogateWithoutItsPair() {
assertEquals("0,2,5", forbiddenIndexes("\uDE00a\uD83D😀\uD83D"));
}
}
@@ -51,6 +51,13 @@ class RdsLogFilesTest {
assertEquals("continued�[0m\ttab", lines.get(1).text());
}
@Test
void aSurrogateWithoutItsPairIsReplacedAndAPairStays() {
List<RdsLogFiles.LogLine> lines = RdsLogFiles.parseDockerLines(docker(
"2026-10-15T10:00:00Z a\uD83D b\uD83D\uDE00 c\uFFFF\r"));
assertEquals("a\uFFFD b\uD83D\uDE00 c\uFFFD\uFFFD", lines.get(0).text());
}
@Test
void describeFiltersByNameTimeAndSize() {
List<RdsLogFiles.LogFile> files = RdsLogFiles.files("postgres", RdsLogFiles.parseDockerLines(docker(
@@ -0,0 +1,36 @@
package io.floci.aws.services.ses;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
class SesQueryHandlerTest {
@Test
void spellOutControlCharacters_leavesAMessageWithoutThemAlone() {
String message = "Template name <né 😀 \uFFFD>: not allowed.";
assertSame(message, SesQueryHandler.spellOutControlCharacters(message));
assertNull(SesQueryHandler.spellOutControlCharacters(null));
}
@Test
void spellOutControlCharacters_writesAControlCharacterAsHex() {
assertEquals("a0x0009b0x000ac0x000dd0x0001",
SesQueryHandler.spellOutControlCharacters("a\tb\nc\rd\u0001"));
}
@Test
void spellOutControlCharacters_writesTheNonCharactersAsHex() {
assertEquals("a0xfffeb0xffff", SesQueryHandler.spellOutControlCharacters("a\uFFFEb\uFFFF"));
}
// a surrogate pair is one character XML allows; half of one is not
@Test
void spellOutControlCharacters_writesASurrogateWithoutItsPairAsHex() {
assertEquals("0xde00a0xde010xd83d😀0xd83d",
SesQueryHandler.spellOutControlCharacters("\uDE00a\uDE01\uD83D😀\uD83D"));
}
}
@@ -171,6 +171,21 @@ class SesTemplateNameIntegrationTest {
.body("message", equalTo(notAllowed("name\tprobe")));
}
// not probed: U+FFFE and U+FFFF cannot appear in an XML 1.0 document, so v1 spells them out too;
// without the UTF-8 charset RestAssured sends them as '?'
@Test
void v1SpellsOutACharacterXmlForbids() {
SesV1Requests.query("GetTemplate")
.contentType("application/x-www-form-urlencoded; charset=UTF-8")
.formParam("TemplateName", "name\uFFFEprobe\uFFFF")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
.body("ErrorResponse.Error.Message", equalTo(notAllowed("name0xfffeprobe0xffff")));
}
@Test
void a64CharacterNameOfEveryAllowedKindIsAccepted() {
String name = "Name_probe-0123456789" + "a".repeat(43);
@@ -0,0 +1,36 @@
package io.floci.aws.services.ses;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static org.hamcrest.Matchers.equalTo;
@QuarkusTest
class SesV1UnsupportedOperationIntegrationTest {
@Test
void namesTheOperation() {
SesV1Requests.query("NoSuchOperation")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("UnsupportedOperation"))
.body("ErrorResponse.Error.Message", equalTo("Operation NoSuchOperation is not supported by SES."));
}
// the action is request input like any quoted value, so it gets the same treatment in the message;
// without the UTF-8 charset RestAssured sends U+FFFE as '?'
@Test
void spellsOutACharacterXmlForbids() {
SesV1Requests.query("No\tSuch\uFFFEOperation")
.contentType("application/x-www-form-urlencoded; charset=UTF-8")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("UnsupportedOperation"))
.body("ErrorResponse.Error.Message",
equalTo("Operation No0x0009Such0xfffeOperation is not supported by SES."));
}
}