mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(iam): resolve s3:ExistingObjectTag and s3:RequestObjectTag under enforcement (#3946)
* fix(iam): resolve s3:ExistingObjectTag and s3:RequestObjectTag under enforcement The S3 condition context was built for s3:ListBucket and the bucket-tagging actions only, so a statement conditioned on an object tag never matched and a tag-conditioned allow denied everything, including what AWS allows. Object reads are now given s3:ExistingObjectTag/<key> from the object's tags, PutObject is given s3:RequestObjectTag/<key> from x-amz-tagging, and PutObjectTagging both. Which action gets which key follows what real AWS was measured to do rather than what reads naturally: s3:DeleteObject is given neither, and s3:PutObject never sees the tags of an object it is about to overwrite. A PutObject carrying If-Match is additionally authorized as s3:GetObject with the object tags stripped from the context, as measured: under a tag-conditioned GetObject allow the conditional write is denied, under a prefix-scoped one it succeeds, and If-None-Match needs no such permission. The per-resource evaluation loop is extracted into abortIfDenied so the second authorization reuses it rather than duplicating it. The class comment named floci.iam.enforcement-enabled; the key that works is floci.services.iam.enforcement-enabled. Closes #209 (cherry picked from commit 402920cf137c98f45283ad093f3df97807bb1dc7) * fix(iam): authorize an object-tag condition against the version the request names Review follow-ups on #3946. s3:ExistingObjectTag was read from the current version whatever the request asked for, so an allow conditioned on a tag decided a versioned read against tags the older version does not carry, letting it through. The lookup now passes the versionId query parameter, via a new S3Service.getObjectTagging overload that takes one. A malformed x-amz-tagging value such as "a=%zz" threw IllegalArgumentException out of URLDecoder inside the filter, answering 500 where S3 returns 400 InvalidTag. An undecodable pair is now dropped, so the policy decides. Documents both condition keys under "Condition keys floci populates" in docs/services/iam.md, including which actions receive them, the If-Match coupling, and that s3:RequestObjectTagKeys is not populated. Replaces the two var declarations in the new test with explicit types, and drops the downstream project's name from the comments and test fixtures.
This commit is contained in:
1 parent
ecdc9ccfdd
commit
09b1dc0803
6 files changed
+450
-3
No files matched your search
+130
@@ -51,6 +51,8 @@ public class IamConditionContextResolver {
|
||||
private static final String BUFFERED_FORM_BODY = "floci.bufferedFormBody";
|
||||
private static final String REQUEST_TAG_PREFIX = "aws:RequestTag/";
|
||||
private static final String RESOURCE_TAG_PREFIX = "aws:ResourceTag/";
|
||||
public static final String EXISTING_OBJECT_TAG_PREFIX = "s3:ExistingObjectTag/";
|
||||
public static final String REQUEST_OBJECT_TAG_PREFIX = "s3:RequestObjectTag/";
|
||||
|
||||
private final Instance<DynamoDbService> dynamoDbService;
|
||||
private final Instance<Ec2Service> ec2Service;
|
||||
@@ -124,10 +126,138 @@ public class IamConditionContextResolver {
|
||||
case "s3:PutBucketTagging" -> s3PutBucketTaggingConditionContext(ctx);
|
||||
case "s3:GetBucketTagging", "s3:DeleteBucketTagging", "s3:DeleteBucket" ->
|
||||
s3BucketResourceTagConditionContext(ctx);
|
||||
// Object tags. Which action is given which key is what real AWS was measured to do,
|
||||
// not what reads naturally: s3:DeleteObject is given neither key, and s3:PutObject is
|
||||
// given the tags of the REQUEST only, never those of an object it is about to
|
||||
// overwrite. See S3ObjectTagConditionEnforcementIntegrationTest.
|
||||
//
|
||||
// s3:GetObjectVersion is deliberately absent. IamActionRegistry authorizes a GET
|
||||
// carrying ?versionId= as s3:GetObject, and IamEnforcementFilter is the only caller
|
||||
// of resolve, so an arm for it would never be reached. The versionId is read from the
|
||||
// request instead, below.
|
||||
case "s3:GetObject", "s3:GetObjectTagging", "s3:GetObjectAcl",
|
||||
"s3:PutObjectAcl", "s3:DeleteObjectTagging" -> s3ExistingObjectTagConditionContext(ctx);
|
||||
case "s3:PutObject" -> s3RequestObjectTagConditionContext(ctx);
|
||||
case "s3:PutObjectTagging" -> merge(s3ExistingObjectTagConditionContext(ctx),
|
||||
s3PutObjectTaggingBodyConditionContext(ctx));
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code s3:ExistingObjectTag/<key>} from the tags of the version this request targets. An
|
||||
* object that does not exist, or has no tags, offers no keys, so a {@code StringEquals} on one
|
||||
* does not match and an allow conditioned on it does not apply.
|
||||
*
|
||||
* <p>The {@code versionId} query parameter decides which version is read. Falling back to the
|
||||
* current version would authorize a read of an older version against tags it does not carry,
|
||||
* which lets a request through that the policy refuses.
|
||||
*/
|
||||
private Map<String, List<String>> s3ExistingObjectTagConditionContext(ContainerRequestContext ctx) {
|
||||
if (ctx.getUriInfo() == null || !s3Service.isResolvable()) {
|
||||
return null;
|
||||
}
|
||||
String path = ctx.getUriInfo().getPath();
|
||||
String bucket = s3BucketName(path);
|
||||
String key = s3ObjectKey(path);
|
||||
if (bucket == null || key == null) {
|
||||
return null;
|
||||
}
|
||||
String versionId = ctx.getUriInfo().getQueryParameters().getFirst("versionId");
|
||||
Map<String, String> tags;
|
||||
try {
|
||||
tags = s3Service.get().getObjectTagging(bucket, key, versionId);
|
||||
} catch (RuntimeException e) {
|
||||
LOG.debugv(e, "Could not read object tags for the IAM condition context: {0}/{1} version {2}",
|
||||
bucket, key, versionId);
|
||||
return null;
|
||||
}
|
||||
if (tags == null || tags.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
tags.forEach((k, v) -> conditions.put(EXISTING_OBJECT_TAG_PREFIX + k, List.of(v)));
|
||||
return conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code s3:RequestObjectTag/<key>} from a PutObject's {@code x-amz-tagging} header, which the
|
||||
* S3 model documents as a URL query parameter encoding of the tag-set.
|
||||
*/
|
||||
private Map<String, List<String>> s3RequestObjectTagConditionContext(ContainerRequestContext ctx) {
|
||||
String header = ctx.getHeaderString("x-amz-tagging");
|
||||
if (header == null || header.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
for (String pair : header.split("&")) {
|
||||
int eq = pair.indexOf('=');
|
||||
if (eq <= 0) {
|
||||
continue;
|
||||
}
|
||||
String name = urlDecode(pair.substring(0, eq));
|
||||
String value = urlDecode(pair.substring(eq + 1));
|
||||
if (name == null || value == null) {
|
||||
continue;
|
||||
}
|
||||
conditions.put(REQUEST_OBJECT_TAG_PREFIX + name, List.of(value));
|
||||
}
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
|
||||
/** {@code s3:RequestObjectTag/<key>} from the {@code <Tagging>} XML body of PutObjectTagging. */
|
||||
private Map<String, List<String>> s3PutObjectTaggingBodyConditionContext(ContainerRequestContext ctx) {
|
||||
byte[] body = bufferEntity(ctx);
|
||||
if (body == null || body.length == 0) {
|
||||
return null;
|
||||
}
|
||||
Map<String, String> tags = XmlParser.extractPairs(
|
||||
new String(body, StandardCharsets.UTF_8), "Tag", "Key", "Value");
|
||||
if (tags.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
tags.forEach((k, v) -> conditions.put(REQUEST_OBJECT_TAG_PREFIX + k, List.of(v)));
|
||||
return conditions;
|
||||
}
|
||||
|
||||
private static Map<String, List<String>> merge(Map<String, List<String>> a, Map<String, List<String>> b) {
|
||||
if (a == null) {
|
||||
return b;
|
||||
}
|
||||
if (b == null) {
|
||||
return a;
|
||||
}
|
||||
Map<String, List<String>> merged = new LinkedHashMap<>(a);
|
||||
merged.putAll(b);
|
||||
return merged;
|
||||
}
|
||||
|
||||
/**
|
||||
* Null for a value {@code URLDecoder} refuses, such as the {@code %zz} of a malformed
|
||||
* {@code x-amz-tagging} header. The caller drops the pair rather than letting the
|
||||
* {@code IllegalArgumentException} out of the filter, where it becomes a 500 in place of the
|
||||
* 400 {@code InvalidTag} the handler returns for the same header.
|
||||
*/
|
||||
private static String urlDecode(String value) {
|
||||
try {
|
||||
return URLDecoder.decode(value, StandardCharsets.UTF_8);
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.debugv(e, "Undecodable tag component in the IAM condition context: {0}", value);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** The object key of a path-style (or filter-rewritten) S3 path, null for a bucket-level one. */
|
||||
private static String s3ObjectKey(String path) {
|
||||
String stripped = path.startsWith("/") ? path.substring(1) : path;
|
||||
int slash = stripped.indexOf('/');
|
||||
if (slash < 0 || slash == stripped.length() - 1) {
|
||||
return null;
|
||||
}
|
||||
return stripped.substring(slash + 1);
|
||||
}
|
||||
|
||||
Map<String, List<String>> s3BucketListConditionContext(MultivaluedMap<String, String> queryParameters) {
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
addQueryCondition(conditions, "s3:prefix", queryParameters, "prefix");
|
||||
|
||||
@@ -34,7 +34,8 @@ import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* JAX-RS filter that enforces IAM policies on every incoming request when
|
||||
* {@code floci.iam.enforcement-enabled = true}.
|
||||
* {@code floci.services.iam.enforcement-enabled = true}
|
||||
* ({@code FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED=true} in the environment).
|
||||
*
|
||||
* <p>Bypass rules (request is always allowed through):
|
||||
* <ul>
|
||||
@@ -236,6 +237,31 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
targetContexts.add(targetContext);
|
||||
}
|
||||
|
||||
if (abortIfDenied(ctx, caller, action, credentialScope, resources, targetContexts,
|
||||
region, accountId, akid)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// A PutObject carrying If-Match compares against the object it replaces, and S3 authorizes
|
||||
// that read as s3:GetObject, WITHOUT the object's tags in the request context. Measured
|
||||
// against real AWS: under a GetObject allow conditioned on s3:ExistingObjectTag the
|
||||
// conditional write is AccessDenied, under a GetObject allow scoped by prefix alone it
|
||||
// succeeds, and with no GetObject at all it is denied. If-None-Match needs no such
|
||||
// permission.
|
||||
if ("s3:PutObject".equals(action) && ctx.getHeaderString("If-Match") != null) {
|
||||
abortIfDenied(ctx, caller, "s3:GetObject", credentialScope, resources,
|
||||
withoutObjectTags(targetContexts), region, accountId, akid);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates one action against every resource and target context, aborting the request with
|
||||
* AccessDenied on the first DENY. Returns true when the request was aborted.
|
||||
*/
|
||||
private boolean abortIfDenied(ContainerRequestContext ctx, CallerContext caller, String action,
|
||||
String credentialScope, List<String> resources,
|
||||
List<Map<String, List<String>>> targetContexts,
|
||||
String region, String accountId, String akid) {
|
||||
for (String resource : resources) {
|
||||
List<ResourcePolicyProvider.ResourcePolicy> resourcePolicies = resolveResourcePolicies(credentialScope, resource);
|
||||
String resourceOwnerAccountId = resourcePolicies.isEmpty() ? null : resourcePolicies.getFirst().ownerAccountId();
|
||||
@@ -267,9 +293,28 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
+ " because no identity-based policy allows the " + action + " action";
|
||||
emitS3DenialIfApplicable(akid, action, resource, ctx, region, denyMessage);
|
||||
ctx.abortWith(accessDeniedResponse(action, credentialScope, ctx.getMediaType(), resource));
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** The same contexts with every object-tag key removed, keeping the principal and global keys. */
|
||||
private static List<Map<String, List<String>>> withoutObjectTags(
|
||||
List<Map<String, List<String>>> targetContexts) {
|
||||
List<Map<String, List<String>>> stripped = new ArrayList<>();
|
||||
for (Map<String, List<String>> targetContext : targetContexts) {
|
||||
if (targetContext == null) {
|
||||
stripped.add(null);
|
||||
continue;
|
||||
}
|
||||
Map<String, List<String>> copy = new HashMap<>(targetContext);
|
||||
copy.keySet().removeIf(key ->
|
||||
key.startsWith(IamConditionContextResolver.EXISTING_OBJECT_TAG_PREFIX)
|
||||
|| key.startsWith(IamConditionContextResolver.REQUEST_OBJECT_TAG_PREFIX));
|
||||
stripped.add(copy.isEmpty() ? null : copy);
|
||||
}
|
||||
return stripped;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2003,7 +2003,12 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
public Map<String, String> getObjectTagging(String bucketName, String key) {
|
||||
S3Object obj = getStoredObject(bucketName, key, null);
|
||||
return getObjectTagging(bucketName, key, null);
|
||||
}
|
||||
|
||||
/** Tags of one version, or of the current version when {@code versionId} is null or "null". */
|
||||
public Map<String, String> getObjectTagging(String bucketName, String key, String versionId) {
|
||||
S3Object obj = getStoredObject(bucketName, key, "null".equals(versionId) ? null : versionId);
|
||||
return obj.getTags() != null ? obj.getTags() : Map.of();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user