mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
fix(sqs): return InvalidAddress for Query requests on a /queue/{region}/ path (#7578)
This commit is contained in:
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());
|
||||
|
||||
Reference in new issue
Block a user