fix(sqs): return InvalidAddress for Query requests on a /queue/{region}/ path (#7578)

This commit is contained in:
Arshad authored and GitHub committed 2026-11-05 11:55:05 -04:00
1 parent 02f530bf0c
commit bc9f7b518d
4 files changed
+131 -13

No files matched your search

@@ -0,0 +1,25 @@
package io.github.hectorvent.floci.core.common;
import jakarta.annotation.Priority;
import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.ext.Provider;
/**
* Rejects a query request that {@link SqsQueueUrlRouterFilter} found signed for SQS on a /queue/{region}/ path with
* 404 InvalidAddress, as AWS rejects it. It runs right after {@link SigV4HeaderSignatureFilter}, so when signatures
* are validated a request with a bad signature gets the signature error instead.
*/
@Provider
@Priority(Priorities.AUTHENTICATION + 1)
public class SqsInvalidAddressFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext ctx) {
if (ctx.getProperty(SqsQueueUrlRouterFilter.INVALID_ADDRESS_PROPERTY) != null) {
ctx.abortWith(AwsQueryResponse.error("InvalidAddress", "The specified ID is invalid.",
AwsNamespaces.SQS, 404));
}
}
}
@@ -29,7 +29,9 @@ import java.util.regex.Pattern;
* For the query (form-encoded) protocol, AWS SDK v1 omits QueueUrl from the body and
* uses the queue URL as the HTTP path instead. This filter appends it back into the
* entity stream so SqsQueryHandler can look up the queue normally. Like AWS, a query request
* is only rewritten on the unprefixed path, because the path names the queue.
* is only rewritten on the unprefixed path, because the path names the queue. A query request signed
* for SQS on the prefixed path is rejected with 404 InvalidAddress, as AWS rejects it, by
* {@link SqsInvalidAddressFilter} once its signature has been verified.
* <p>
* S3 object POSTs, which always carry uploads, uploadId, restore or select, are never rewritten.
*/
@@ -44,6 +46,13 @@ public class SqsQueueUrlRouterFilter implements ContainerRequestFilter {
public static final String APPENDED_BODY_BYTES_PROPERTY =
SqsQueueUrlRouterFilter.class.getName() + ".appendedBodyBytes";
/**
* Set on a query request signed for SQS on a /queue/{region}/ path, which {@link SqsInvalidAddressFilter}
* rejects with 404 InvalidAddress after {@link SigV4HeaderSignatureFilter} has verified its signature.
*/
public static final String INVALID_ADDRESS_PROPERTY =
SqsQueueUrlRouterFilter.class.getName() + ".invalidAddress";
// The queue URL path (/{account}/{queue}), optionally behind a /queue/{region}/ prefix, with an optional
// trailing slash, as the AWS SDK v2 may append one. Like AWS, a JSON request is routed on either form because
// it names the queue in its QueueUrl body field, while a Query request is named by its path and is routed
@@ -85,10 +94,20 @@ public class SqsQueueUrlRouterFilter implements ContainerRequestFilter {
// A form-encoded S3 POST to /{bucket}/{key} carries one of S3_OBJECT_POST_PARAMS, handled above.
// S3 presigned POST always goes to /{bucket}, not /{bucket}/{key}.
boolean isSqsQuery = !hasRegionPrefix
&& "application".equals(mt.getType())
boolean isSqsQuery = "application".equals(mt.getType())
&& "x-www-form-urlencoded".equals(mt.getSubtype());
if (isSqsQuery && hasRegionPrefix) {
// AWS does not read a queue from this path. The signing scope names the service, as the AWS endpoint
// does, so the body need not be read here, and requests for other services keep their routing. The
// request is routed to POST / so that its signature is verified before it is rejected.
if (SignedServiceRoute.signingService(ctx).filter("sqs"::equals).isPresent()) {
ctx.setProperty(INVALID_ADDRESS_PROPERTY, Boolean.TRUE);
rewriteToRoot(ctx);
}
return;
}
if (!isSqsJson && !isSqsQuery) {
return;
}
@@ -115,6 +134,10 @@ public class SqsQueueUrlRouterFilter implements ContainerRequestFilter {
}
// Rewrite the path to / so AwsQueryController / AwsJsonController handles the request.
rewriteToRoot(ctx);
}
private void rewriteToRoot(ContainerRequestContext ctx) {
ctx.setRequestUri(ctx.getUriInfo().getRequestUriBuilder()
.replacePath("/")
.build());
@@ -545,4 +545,30 @@ class SigV4HeaderSignatureIntegrationTest {
new XmlPath(refused.body()).getString("ErrorResponse.Error.Code"));
}
}
@Test
@Order(21)
void queryRequestOnPrefixedQueuePathIsVerifiedBeforeItIsRejectedAsInvalidAddress() {
// AWS rejects a Query request on a /queue/{region}/ path, but only once the signature checks out.
String path = "/queue/us-east-1/000000000000/" + QUEUE;
given()
.filter(AwsRequestSigner.signedAs("test", "test", "sqs"))
.contentType(FORM)
.body("Action=GetQueueAttributes&AttributeName.1=All&Version=2012-11-05")
.when()
.post(path)
.then()
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("InvalidAddress"));
given()
.filter(AwsRequestSigner.signedAs("test", "not-the-secret", "sqs"))
.contentType(FORM)
.body("Action=GetQueueAttributes&AttributeName.1=All&Version=2012-11-05")
.when()
.post(path)
.then()
.statusCode(403)
.body("ErrorResponse.Error.Code", equalTo("SignatureDoesNotMatch"));
}
}
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
@@ -20,13 +21,13 @@ import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
/**
* SDKs that use the queue URL as the request endpoint post SQS calls to the queue path instead of "/". Routing
* follows AWS: a JSON request names the queue in its QueueUrl body field, so it reaches SQS on the queue path, on
* a /queue/{region}/ prefixed path and on either with a trailing slash. A Query request without a QueueUrl is
* named by its path, so it reaches SQS only on the queue path, with or without a trailing slash.
* named by its path, so it reaches SQS only on the queue path, with or without a trailing slash, and AWS rejects it
* on a prefixed path with 404 InvalidAddress.
*/
@QuarkusTest
class SqsQueueUrlPathRoutingIntegrationTest {
@@ -35,6 +36,8 @@ class SqsQueueUrlPathRoutingIntegrationTest {
private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded";
private static final String QUEUE_NAME = "path-routing-queue";
private static final String QUEUE_ARN = "arn:aws:sqs:us-east-1:000000000000:" + QUEUE_NAME;
private static final String SQS_AUTHORIZATION =
"AWS4-HMAC-SHA256 Credential=test/20261105/us-east-1/sqs/aws4_request, SignedHeaders=host, Signature=abc";
@BeforeAll
static void configureRestAssured() {
@@ -80,7 +83,7 @@ class SqsQueueUrlPathRoutingIntegrationTest {
"/000000000000/" + QUEUE_NAME + "/"
})
void queryRequestPostedToQueuePathWithoutQueueUrlReachesSqs(String path) {
postQueryWithoutQueueUrl(path)
postQueryWithoutQueueUrl(given(), path)
.then()
.statusCode(200)
.body(containsString(QUEUE_ARN));
@@ -91,12 +94,53 @@ class SqsQueueUrlPathRoutingIntegrationTest {
"/queue/us-east-1/000000000000/" + QUEUE_NAME,
"/queue/us-east-1/000000000000/" + QUEUE_NAME + "/"
})
void queryRequestPostedToPrefixedQueuePathWithoutQueueUrlIsNotRoutedToSqs(String path) {
// AWS rejects these with 404 InvalidAddress.
postQueryWithoutQueueUrl(path)
void queryRequestPostedToPrefixedQueuePathWithoutQueueUrlIsRejectedAsInvalidAddress(String path) {
postQueryWithoutQueueUrl(given().header("Authorization", SQS_AUTHORIZATION), path)
.then()
.statusCode(not(equalTo(200)))
.body(not(containsString(QUEUE_ARN)));
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("InvalidAddress"))
.body("ErrorResponse.Error.Message", equalTo("The specified ID is invalid."));
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void presignedQueryRequestOnPrefixedQueuePathIsRejectedAsInvalidAddress(boolean withBlankAuthorizationHeader) {
// A blank Authorization header counts as absent, so the presigned credential names the service.
RequestSpecification request = given()
.queryParam("X-Amz-Credential", "test/20261105/us-east-1/sqs/aws4_request");
if (withBlankAuthorizationHeader) {
request.header("Authorization", "");
}
postQueryWithoutQueueUrl(request, "/queue/us-east-1/000000000000/" + QUEUE_NAME)
.then()
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("InvalidAddress"));
}
@ParameterizedTest
@ValueSource(strings = {
"",
"AWS4-HMAC-SHA256 Credential=test/20261105/us-east-1/s3/aws4_request, SignedHeaders=host, Signature=abc"
})
void formEncodedPostNotSignedForSqsOnPrefixedQueuePathStaysWithS3(String authorization) {
given()
.when()
.put("/queue")
.then()
.statusCode(200);
RequestSpecification request = given()
.contentType(FORM_CONTENT_TYPE)
.formParam("field", "value");
if (!authorization.isEmpty()) {
request.header("Authorization", authorization);
}
request
.when()
.post("/queue/us-east-1/000000000000/form")
.then()
.statusCode(400)
.body("Error.Code", equalTo("InvalidArgument"));
}
@ParameterizedTest
@@ -138,8 +182,8 @@ class SqsQueueUrlPathRoutingIntegrationTest {
}
}
private static Response postQueryWithoutQueueUrl(String path) {
return given()
private static Response postQueryWithoutQueueUrl(RequestSpecification request, String path) {
return request
.contentType(FORM_CONTENT_TYPE)
.formParam("Action", "GetQueueAttributes")
.formParam("AttributeName.1", "QueueArn")