mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(s3): place the encryption condition keys on a put, and on a part from its upload (#7050)
* fix(s3): place the encryption condition keys on a put, and on a part from its upload A policy on s3:x-amz-server-side-encryption or s3:x-amz-server-side-encryption-aws-kms-key-id read neither key, so a Deny under a negated operator refused every put, encrypted or not. A PutObject, CopyObject or CreateMultipartUpload now carries both from its headers, each value as sent, in the enforcement filter and in S3's own bucket policy check. A part or a completion sends neither header and carries what its upload recorded when it was created. A part naming an upload not held answers NoSuchUpload, as S3 does under such a policy. * fix(s3): check as an object write a PUT or POST naming an upload but not routed as a part The enforcement filter let through a request naming an upload not held, and judged one naming a held upload by that upload's encryption, on uploadId alone. A PUT with no partNumber, or a POST asking for a new upload, creates an object or an upload whatever that parameter says, so it skipped IAM or read the wrong encryption. Both now match the routes S3Controller takes as a part or a completion. A part copy naming an upload not held stays checked, since it names a source. * test(s3): put the multipart encryption cases beside kmsOnlyBucket The multipart cases copied S3BucketPolicyEnforcementIntegrationTest's two-statement KMS-only policy and BUCKET_KEY. They now sit beside kmsOnlyBucket and reuse it, with the policy written once (putKmsOnlyPolicy). The two cases whose upload is opened before the policy stands use a bucket with no default encryption, as before: S3 records the bucket's default on an upload that asks for none, where Floci records only what the request asks for.
This commit is contained in:
1 parent
1b4f99cb8a
commit
fccc81b61d
8 files changed
+376
-7
No files matched your search
+53
-4
@@ -87,6 +87,8 @@ public class IamConditionContextResolver {
|
||||
public static final String OBJECT_LOCK_RETAIN_UNTIL_DATE = "s3:object-lock-retain-until-date";
|
||||
public static final String OBJECT_LOCK_REMAINING_RETENTION_DAYS = "s3:object-lock-remaining-retention-days";
|
||||
public static final String OBJECT_LOCK_LEGAL_HOLD = "s3:object-lock-legal-hold";
|
||||
public static final String SERVER_SIDE_ENCRYPTION = "s3:x-amz-server-side-encryption";
|
||||
public static final String SERVER_SIDE_ENCRYPTION_KMS_KEY_ID = "s3:x-amz-server-side-encryption-aws-kms-key-id";
|
||||
static final String REQUEST_AUTH_TYPE = "lambda:Request/WebFunctionAuthType";
|
||||
static final String RESOURCE_AUTH_TYPE = "lambda:Resource/WebFunctionAuthType";
|
||||
|
||||
@@ -403,10 +405,13 @@ public class IamConditionContextResolver {
|
||||
"s3:PutObjectVersionAnnotation", "s3:DeleteObjectAnnotation",
|
||||
"s3:DeleteObjectVersionAnnotation" -> s3ExistingObjectTagConditionContext(ctx);
|
||||
// The Object Lock keys come from what the request sets: an object creation's headers, the
|
||||
// retention and legal-hold bodies.
|
||||
case "s3:PutObject" -> S3ObjectActions.objectCreationConditions(ctx::getHeaderString,
|
||||
s3RequestTagging(ctx), S3ObjectActions.isCopy(ctx.getMethod(), ctx::getHeaderString),
|
||||
clock.instant());
|
||||
// retention and legal-hold bodies. A part or a completion creates nothing from its own
|
||||
// headers, and is judged by the encryption its upload recorded.
|
||||
case "s3:PutObject" -> isPartOrCompletion(ctx)
|
||||
? s3UploadEncryptionConditionContext(ctx)
|
||||
: S3ObjectActions.objectCreationConditions(ctx::getHeaderString,
|
||||
s3RequestTagging(ctx), S3ObjectActions.isCopy(ctx.getMethod(), ctx::getHeaderString),
|
||||
clock.instant());
|
||||
case "s3:PutObjectRetention" -> S3ObjectActions.retentionBodyConditions(bodyText(ctx), clock.instant());
|
||||
case "s3:PutObjectLegalHold" -> S3ObjectActions.legalHoldBodyConditions(bodyText(ctx), clock.instant());
|
||||
case "s3:PutObjectTagging", "s3:PutObjectVersionTagging" -> merge(
|
||||
@@ -415,6 +420,50 @@ public class IamConditionContextResolver {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The encryption keys of a part or a completion: what its upload recorded when it was created, as
|
||||
* measured on S3, since a part sends neither header. An upload not held offers neither.
|
||||
*/
|
||||
private Map<String, List<String>> s3UploadEncryptionConditionContext(ContainerRequestContext ctx) {
|
||||
if (!s3Service.isResolvable()) {
|
||||
return null;
|
||||
}
|
||||
String bucket = s3BucketName(ctx.getUriInfo().getPath());
|
||||
try {
|
||||
return s3Service.get().multipartEncryptionConditions(bucket, s3RequestObjectKey(ctx, bucket),
|
||||
ctx.getUriInfo().getQueryParameters().getFirst("uploadId"));
|
||||
} catch (AwsException expected) {
|
||||
// The handler answers an upload not held as NoSuchUpload; here it offers no keys.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A part or a completion as S3Controller routes them: a PUT naming an upload and a part number, or a
|
||||
* POST naming an upload and not asking for a new one. Any other PUT or POST naming an {@code uploadId}
|
||||
* creates an object or an upload, whatever that parameter says, and is judged by its own headers.
|
||||
*/
|
||||
private static boolean isPartOrCompletion(ContainerRequestContext ctx) {
|
||||
MultivaluedMap<String, String> params = ctx.getUriInfo().getQueryParameters();
|
||||
if (!params.containsKey("uploadId")) {
|
||||
return false;
|
||||
}
|
||||
return "PUT".equalsIgnoreCase(ctx.getMethod())
|
||||
? params.containsKey("partNumber")
|
||||
: "POST".equalsIgnoreCase(ctx.getMethod()) && !params.containsKey("uploads");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a request checked as {@code s3:PutObject} is a part or a completion of an upload S3 does
|
||||
* not hold. There is no encryption to judge it by, and S3 answers it {@code NoSuchUpload} under a
|
||||
* policy on the encryption keys, as measured.
|
||||
*/
|
||||
boolean namesUploadNotHeld(String action, ContainerRequestContext ctx) {
|
||||
return "s3:PutObject".equals(action) && isPartOrCompletion(ctx)
|
||||
&& ctx.getHeaderString("x-amz-copy-source") == null
|
||||
&& s3UploadEncryptionConditionContext(ctx) == null && s3Service.isResolvable();
|
||||
}
|
||||
|
||||
private static String bodyText(ContainerRequestContext ctx) {
|
||||
byte[] body = bufferEntity(ctx);
|
||||
return body == null ? null : new String(body, StandardCharsets.UTF_8);
|
||||
|
||||
@@ -535,6 +535,11 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
}
|
||||
}
|
||||
|
||||
// A policy on the encryption keys would refuse this for keys it cannot have: the handler answers it.
|
||||
if (conditionContextResolver.namesUploadNotHeld(action, ctx)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, List<String>> conditionContext = conditionContextResolver.resolve(credentialScope, action, ctx);
|
||||
// A request naming several resources is authorized once per resource, as on AWS, so a
|
||||
// permitted first target cannot carry later targets that the policy does not allow.
|
||||
|
||||
@@ -911,7 +911,7 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
if (uploadId != null && partNumber != null) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);
|
||||
s3Service.authorizeMultipartWrite(bucket, key, uploadId, authorization);
|
||||
int parsedPartNumber = PartRange.parseUploadPartNumber(partNumber);
|
||||
if (copySource != null && !copySource.isEmpty()) {
|
||||
return handleUploadPartCopy(copySource, bucket, key, uploadId, parsedPartNumber,
|
||||
@@ -1728,7 +1728,7 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
if (uploadId != null) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);
|
||||
s3Service.authorizeMultipartWrite(bucket, key, uploadId, authorization);
|
||||
authorizeConditionalWriteRead(bucket, key, ifMatch, authorization);
|
||||
List<CompletedPart> completedParts = parseCompleteMultipartBody(new String(body, StandardCharsets.UTF_8));
|
||||
List<Integer> partNumbers = completedParts.stream().map(CompletedPart::partNumber).toList();
|
||||
|
||||
@@ -156,9 +156,27 @@ public final class S3ObjectActions {
|
||||
if (lock != null) {
|
||||
conditions.putAll(lock);
|
||||
}
|
||||
conditions.putAll(encryptionConditions(header.apply("x-amz-server-side-encryption"),
|
||||
header.apply("x-amz-server-side-encryption-aws-kms-key-id")));
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code s3:x-amz-server-side-encryption} and {@code s3:x-amz-server-side-encryption-aws-kms-key-id}
|
||||
* as the request asks for them, each value as sent: an alias or a bare key id is not resolved to an
|
||||
* ARN, and a bucket's default encryption fills neither.
|
||||
*/
|
||||
public static Map<String, List<String>> encryptionConditions(String encryption, String kmsKeyId) {
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
if (encryption != null && !encryption.isBlank()) {
|
||||
conditions.put(IamConditionContextResolver.SERVER_SIDE_ENCRYPTION, List.of(encryption));
|
||||
}
|
||||
if (kmsKeyId != null && !kmsKeyId.isBlank()) {
|
||||
conditions.put(IamConditionContextResolver.SERVER_SIDE_ENCRYPTION_KMS_KEY_ID, List.of(kmsKeyId));
|
||||
}
|
||||
return conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the new object gets the request's {@code x-amz-tagging}: a PutObject's and a
|
||||
* CreateMultipartUpload's always, a CopyObject's only under {@code x-amz-tagging-directive:
|
||||
|
||||
@@ -5425,6 +5425,22 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
return getMultipartUpload(bucket, key, uploadId);
|
||||
}
|
||||
|
||||
/** The encryption condition keys of an upload's parts and completion: what it recorded when created. */
|
||||
public Map<String, List<String>> multipartEncryptionConditions(String bucket, String key, String uploadId) {
|
||||
MultipartUpload upload = getMultipartUpload(bucket, key, uploadId);
|
||||
return S3ObjectActions.encryptionConditions(upload.getServerSideEncryption(), upload.getSseKmsKeyId());
|
||||
}
|
||||
|
||||
/** Checks a part or a completion against the bucket policy by the encryption its upload recorded. */
|
||||
void authorizeMultipartWrite(String bucket, String key, String uploadId, RequestAuthorization authorization) {
|
||||
if (!enforceAuth) {
|
||||
return;
|
||||
}
|
||||
authorizeSignedRequest(authorization);
|
||||
authorizeObjectWrite(bucket, key, null, "s3:PutObject", authorization,
|
||||
multipartEncryptionConditions(bucket, key, uploadId));
|
||||
}
|
||||
|
||||
public MultipartUpload getMultipartUpload(String bucket, String key, String uploadId) {
|
||||
MultipartUpload upload = multipartUploads.get(uploadId);
|
||||
if (upload == null || !upload.getBucket().equals(bucket) || !upload.getKey().equals(key)
|
||||
|
||||
Reference in new issue
Block a user