mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(sns): refuse a topic policy that grants an action SNS does not have (#9080)
SetTopicAttributes and CreateTopic stored any Policy, so a topic policy
granting an action SNS does not have, such as sns:PublishBatch, was kept
and an AWS::SNS::TopicPolicy carrying one reached CREATE_COMPLETE. Both
now check every sns: Action of every statement against the list
AddPermission uses, ignoring case, and answer the same InvalidParameter
("Policy statement action out of service scope!"). A refused
SetTopicAttributes keeps the old policy and a refused CreateTopic creates
no topic. Receive, which AWS's former default topic policy granted, is
added to that list. Wildcards, NotAction, other services' actions and a
value that is not JSON are left unchecked.
Closes #8897
This commit is contained in:
1 parent
7e2d9e83fb
commit
c9aad84192
3 files changed
+153
-5
No files matched your search
@@ -29,6 +29,7 @@ import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.firehose.FirehoseService;
|
||||
import io.floci.aws.services.firehose.model.Record;
|
||||
import io.floci.aws.services.iam.PolicyDocument;
|
||||
import io.floci.aws.services.lambda.LambdaService;
|
||||
import io.floci.aws.services.lambda.model.InvocationType;
|
||||
import io.floci.aws.services.sns.model.PlatformApplication;
|
||||
@@ -78,6 +79,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
public class SnsService implements Resettable, ResourceProvider {
|
||||
@@ -106,8 +108,10 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
private static final int SANDBOX_PAGE_SIZE = 100;
|
||||
/**
|
||||
* The actions a topic policy statement can grant, which are SNS's actions in AWS's service
|
||||
* reference (https://servicereference.us-east-1.amazonaws.com/v1/sns/sns.json).
|
||||
* {@code AddPermission} refuses any other name.
|
||||
* reference (https://servicereference.us-east-1.amazonaws.com/v1/sns/sns.json), plus
|
||||
* {@code Receive}, which AWS's former default topic policy granted and AWS still accepts.
|
||||
* {@code AddPermission} refuses any other name, and a policy set on a topic any other
|
||||
* {@code sns:} action.
|
||||
*/
|
||||
private static final Set<String> SNS_POLICY_ACTIONS = Set.of(
|
||||
"AddPermission", "CheckIfPhoneNumberIsOptedOut", "ConfirmSubscription",
|
||||
@@ -121,7 +125,11 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
"ListTagsForResource", "ListTopics", "OptInPhoneNumber", "Publish", "PutDataProtectionPolicy",
|
||||
"RemovePermission", "SetEndpointAttributes", "SetPlatformApplicationAttributes",
|
||||
"SetSMSAttributes", "SetSubscriptionAttributes", "SetTopicAttributes", "Subscribe",
|
||||
"TagResource", "Unsubscribe", "UntagResource", "VerifySMSSandboxPhoneNumber");
|
||||
"TagResource", "Unsubscribe", "UntagResource", "VerifySMSSandboxPhoneNumber", "Receive");
|
||||
/** {@link #SNS_POLICY_ACTIONS} in lower case: a policy's action names are matched ignoring case. */
|
||||
private static final Set<String> SNS_POLICY_ACTIONS_LOWER_CASE = SNS_POLICY_ACTIONS.stream()
|
||||
.map(action -> action.toLowerCase(Locale.ROOT))
|
||||
.collect(Collectors.toUnmodifiableSet());
|
||||
/** What the default statement of a topic's policy grants its owner. */
|
||||
private static final List<String> DEFAULT_TOPIC_POLICY_ACTIONS = List.of(
|
||||
"SNS:GetTopicAttributes", "SNS:SetTopicAttributes", "SNS:AddPermission",
|
||||
@@ -611,6 +619,9 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
if (attributes != null && attributes.containsKey(MAXIMUM_MESSAGE_SIZE)) {
|
||||
validateMaximumMessageSize(attributes.get(MAXIMUM_MESSAGE_SIZE), true);
|
||||
}
|
||||
if (attributes != null) {
|
||||
requireSnsPolicyActions(attributes.get("Policy"));
|
||||
}
|
||||
String topicArn = regionResolver.buildArn("sns", region, name);
|
||||
String key = topicKey(region, topicArn);
|
||||
|
||||
@@ -735,10 +746,46 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
&& validateMaximumMessageSize(attributeValue, false) > DEFAULT_MAX_MESSAGE_SIZE) {
|
||||
requireLargePayloadSubscriptions(topicArn, region);
|
||||
}
|
||||
if ("Policy".equals(attributeName)) {
|
||||
requireSnsPolicyActions(attributeValue);
|
||||
}
|
||||
topic.getAttributes().put(attributeName, attributeValue);
|
||||
topicStore.put(key, topic);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses a topic policy whose statements grant an {@code sns:} action SNS does not have, as
|
||||
* AWS does. Wildcards, {@code NotAction}, actions of other services and a value that is not
|
||||
* JSON are left alone.
|
||||
*/
|
||||
private void requireSnsPolicyActions(String policy) {
|
||||
if (policy == null || policy.isBlank()) {
|
||||
return;
|
||||
}
|
||||
JsonNode document;
|
||||
try {
|
||||
document = objectMapper.readTree(policy);
|
||||
} catch (JsonProcessingException ignored) {
|
||||
// Not JSON: this check only reads the actions of a policy that parses.
|
||||
return;
|
||||
}
|
||||
for (JsonNode statement : PolicyDocument.statementsOf(document)) {
|
||||
JsonNode actions = statement.path("Action");
|
||||
for (JsonNode action : actions.isArray() ? actions : List.of(actions)) {
|
||||
String name = action.asText("").toLowerCase(Locale.ROOT);
|
||||
if (name.startsWith("sns:") && !name.contains("*") && !name.contains("?")
|
||||
&& !SNS_POLICY_ACTIONS_LOWER_CASE.contains(name.substring(4))) {
|
||||
throw actionOutOfServiceScope();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException actionOutOfServiceScope() {
|
||||
return new AwsException("InvalidParameter",
|
||||
"Invalid parameter: Policy statement action out of service scope!", 400);
|
||||
}
|
||||
|
||||
/** Exists for CloudFormation rollback: SetTopicAttributes cannot unset an attribute. */
|
||||
public synchronized void removeTopicAttribute(String topicArn, String attributeName, String region) {
|
||||
String key = topicKey(region, topicArn);
|
||||
@@ -772,8 +819,7 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
Topic topic = topicStore.get(key).orElseThrow(SnsService::permissionTopicNotFound);
|
||||
for (String action : actionNames) {
|
||||
if (!SNS_POLICY_ACTIONS.contains(action)) {
|
||||
throw new AwsException("InvalidParameter",
|
||||
"Invalid parameter: Policy statement action out of service scope!", 400);
|
||||
throw actionOutOfServiceScope();
|
||||
}
|
||||
}
|
||||
ObjectNode policy = topicPolicy(topic, topicArn);
|
||||
|
||||
Reference in new issue
Block a user