mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(s3): enforce auth on object write paths when enforce-auth is enabled (#2326)
PutObject/DeleteObject and their subresources (tagging, retention, legal-hold, acl, multipart, copy, batch delete, restore) accepted anonymous requests even with enforce-auth enabled, unlike GetObject. Also fixes: batch delete failing whole on a bad access key instead of masking it per-key; explicit policy Deny taking precedence over ACL grants; public-read-write ACL scoped to creating a new key only, per AWS's ACL docs, never overwrite/delete; DeleteObject vs DeleteObjectVersion as distinct actions; and BypassGovernanceRetention required separately from DeleteObject/PutObjectRetention. Fixes #2318 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
573d4bdaff
commit
16a8f042e7
5 files changed
+812
-5
No files matched your search
@@ -114,6 +114,10 @@ record S3AclPolicy(List<S3AclPolicy.Grant> grants) {
|
||||
return grantee.isAllUsersGroup() && permission.allowsRead();
|
||||
}
|
||||
|
||||
boolean allowsPublicWrite() {
|
||||
return grantee.isAllUsersGroup() && permission.allowsWrite();
|
||||
}
|
||||
|
||||
boolean allowsCanonicalUserRead(String canonicalUserId) {
|
||||
return grantee.isCanonicalUser(canonicalUserId) && permission.allowsRead();
|
||||
}
|
||||
@@ -151,6 +155,10 @@ record S3AclPolicy(List<S3AclPolicy.Grant> grants) {
|
||||
boolean allowsRead() {
|
||||
return this == READ || this == FULL_CONTROL;
|
||||
}
|
||||
|
||||
boolean allowsWrite() {
|
||||
return this == WRITE || this == FULL_CONTROL;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class SilentThrowingErrorHandler implements ErrorHandler {
|
||||
|
||||
@@ -23,4 +23,17 @@ final class S3AclPublicAccessEvaluator {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static boolean aclAllowsPublicWrite(String acl) {
|
||||
if (acl == null || acl.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
S3AclPolicy policy = S3AclPolicy.parse(acl);
|
||||
return policy.grants().stream().anyMatch(S3AclPolicy.Grant::allowsPublicWrite);
|
||||
} catch (S3AclPolicy.AclParseException e) {
|
||||
LOG.debugv(e, "Failed to parse S3 ACL for public write evaluation");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -617,19 +617,28 @@ public class S3Controller {
|
||||
byte[] body) {
|
||||
try {
|
||||
key = extractObjectKey(uriInfo, bucket);
|
||||
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
|
||||
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
|
||||
|
||||
if (hasQueryParam(uriInfo, "tagging")) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectTagging", authorization);
|
||||
return handlePutObjectTagging(bucket, key, body);
|
||||
}
|
||||
if (hasQueryParam(uriInfo, "retention")) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectRetention", authorization);
|
||||
if ("true".equalsIgnoreCase(httpHeaders.getHeaderString("x-amz-bypass-governance-retention"))) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:BypassGovernanceRetention", authorization);
|
||||
}
|
||||
return handlePutObjectRetention(bucket, key,
|
||||
uriInfo.getQueryParameters().getFirst("versionId"), httpHeaders, body);
|
||||
}
|
||||
if (hasQueryParam(uriInfo, "legal-hold")) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectLegalHold", authorization);
|
||||
return handlePutObjectLegalHold(bucket, key,
|
||||
uriInfo.getQueryParameters().getFirst("versionId"), body);
|
||||
}
|
||||
if (hasQueryParam(uriInfo, "acl")) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectAcl", authorization);
|
||||
s3Service.putObjectAcl(bucket, key, uriInfo.getQueryParameters().getFirst("versionId"),
|
||||
new String(body, StandardCharsets.UTF_8),
|
||||
httpHeaders.getHeaderString("x-amz-acl"),
|
||||
@@ -642,6 +651,7 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
if (uploadId != null && partNumber != null) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);
|
||||
if (copySource != null && !copySource.isEmpty()) {
|
||||
return handleUploadPartCopy(copySource, bucket, key, uploadId, partNumber, httpHeaders);
|
||||
}
|
||||
@@ -659,6 +669,7 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
if (copySource != null && !copySource.isEmpty()) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);
|
||||
return handleCopyObject(copySource, bucket, key, contentType, httpHeaders);
|
||||
}
|
||||
|
||||
@@ -680,6 +691,7 @@ public class S3Controller {
|
||||
String sseCustomerKey = httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key");
|
||||
String sseCustomerKeyMd5 = httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5");
|
||||
String cannedAcl = httpHeaders.getHeaderString("x-amz-acl");
|
||||
s3Service.authorizePutObject(bucket, key, authorization);
|
||||
S3Object obj = s3Service.putObject(bucket, key, data, contentType, extractUserMetadata(httpHeaders),
|
||||
new PutObjectOptions()
|
||||
.withStorageClass(httpHeaders.getHeaderString("x-amz-storage-class"))
|
||||
@@ -1103,17 +1115,25 @@ public class S3Controller {
|
||||
@Context HttpHeaders httpHeaders) {
|
||||
try {
|
||||
key = extractObjectKey(uriInfo, bucket);
|
||||
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
|
||||
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
|
||||
|
||||
if (hasQueryParam(uriInfo, "tagging")) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:DeleteObjectTagging", authorization);
|
||||
s3Service.deleteObjectTagging(bucket, key);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
if (uploadId != null) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:AbortMultipartUpload", authorization);
|
||||
s3Service.abortMultipartUpload(bucket, key, uploadId);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
boolean bypass = "true".equalsIgnoreCase(
|
||||
httpHeaders.getHeaderString("x-amz-bypass-governance-retention"));
|
||||
s3Service.authorizeDeleteObject(bucket, key, versionId, authorization);
|
||||
if (bypass) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:BypassGovernanceRetention", authorization);
|
||||
}
|
||||
S3Object result = s3Service.deleteObject(bucket, key, versionId, bypass);
|
||||
var resp = Response.noContent();
|
||||
if (result != null) {
|
||||
@@ -1137,11 +1157,12 @@ public class S3Controller {
|
||||
@Produces(MediaType.APPLICATION_XML)
|
||||
public Response handleBucketPost(@PathParam("bucket") String bucket,
|
||||
@HeaderParam("Content-Type") String contentType,
|
||||
@Context HttpHeaders httpHeaders,
|
||||
@Context UriInfo uriInfo,
|
||||
byte[] body) {
|
||||
try {
|
||||
if (hasQueryParam(uriInfo, "delete")) {
|
||||
return handleDeleteObjects(bucket, body);
|
||||
return handleDeleteObjects(bucket, body, httpHeaders, uriInfo);
|
||||
}
|
||||
if (contentType != null && contentType.startsWith("multipart/form-data")) {
|
||||
return handlePresignedPost(bucket, contentType, body);
|
||||
@@ -1168,8 +1189,11 @@ public class S3Controller {
|
||||
byte[] body) {
|
||||
try {
|
||||
key = extractObjectKey(uriInfo, bucket);
|
||||
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
|
||||
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
|
||||
|
||||
if (hasQueryParam(uriInfo, "uploads")) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);
|
||||
MultipartUpload upload = s3Service.initiateMultipartUpload(bucket, key, contentType,
|
||||
extractUserMetadata(httpHeaders),
|
||||
httpHeaders.getHeaderString("x-amz-storage-class"),
|
||||
@@ -1195,13 +1219,12 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
if (hasQueryParam(uriInfo, "restore")) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:RestoreObject", authorization);
|
||||
s3Service.restoreObject(bucket, key, versionId, new String(body, StandardCharsets.UTF_8));
|
||||
return Response.accepted().build();
|
||||
}
|
||||
|
||||
if (hasQueryParam(uriInfo, "select")) {
|
||||
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
|
||||
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
|
||||
s3Service.authorizeGetObject(bucket, key, versionId, authorization);
|
||||
S3Object obj = s3Service.getObject(bucket, key, versionId);
|
||||
byte[] result = s3SelectService.select(obj, new String(body, StandardCharsets.UTF_8));
|
||||
@@ -1211,6 +1234,7 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
if (uploadId != null) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);
|
||||
List<Integer> partNumbers = parseCompleteMultipartBody(new String(body));
|
||||
Response preconditionResponse = checkWritePreconditions(bucket, key, ifMatch, ifNoneMatch);
|
||||
if (preconditionResponse != null) {
|
||||
@@ -1250,7 +1274,7 @@ public class S3Controller {
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleDeleteObjects(String bucket, byte[] body) {
|
||||
private Response handleDeleteObjects(String bucket, byte[] body, HttpHeaders httpHeaders, UriInfo uriInfo) {
|
||||
String xml = new String(body, StandardCharsets.UTF_8);
|
||||
List<String> keys = XmlParser.extractAll(xml, "Key");
|
||||
if (keys.isEmpty()) {
|
||||
@@ -1258,7 +1282,22 @@ public class S3Controller {
|
||||
"The XML you provided was not well-formed.", 400);
|
||||
}
|
||||
boolean quiet = XmlParser.containsValue(xml, "Quiet", "true");
|
||||
S3Service.DeleteObjectsResult result = s3Service.deleteObjects(bucket, keys);
|
||||
|
||||
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
|
||||
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
|
||||
s3Service.authorizeSignedRequest(authorization);
|
||||
List<String> authorizedKeys = new ArrayList<>();
|
||||
List<S3Service.DeleteError> authorizationErrors = new ArrayList<>();
|
||||
for (String key : keys) {
|
||||
try {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:DeleteObject", authorization);
|
||||
authorizedKeys.add(key);
|
||||
} catch (AwsException e) {
|
||||
authorizationErrors.add(new S3Service.DeleteError(key, e.getErrorCode(), e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
S3Service.DeleteObjectsResult result = s3Service.deleteObjects(bucket, authorizedKeys);
|
||||
|
||||
XmlBuilder builder = new XmlBuilder()
|
||||
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
@@ -1275,6 +1314,13 @@ public class S3Controller {
|
||||
builder.end("Deleted");
|
||||
}
|
||||
}
|
||||
for (S3Service.DeleteError e : authorizationErrors) {
|
||||
builder.start("Error")
|
||||
.elem("Key", e.key())
|
||||
.elem("Code", e.code())
|
||||
.elem("Message", e.message())
|
||||
.end("Error");
|
||||
}
|
||||
for (S3Service.DeleteError e : result.errors()) {
|
||||
builder.start("Error")
|
||||
.elem("Key", e.key())
|
||||
|
||||
@@ -589,6 +589,72 @@ public class S3Service implements Resettable {
|
||||
authorizeS3Read(bucketName, key, versionId, action, objectArn, authorization);
|
||||
}
|
||||
|
||||
void authorizePutObject(String bucketName, String key, RequestAuthorization authorization) {
|
||||
authorizeObjectWrite(bucketName, key, "s3:PutObject", authorization);
|
||||
}
|
||||
|
||||
void authorizeDeleteObject(String bucketName, String key, String versionId, RequestAuthorization authorization) {
|
||||
String action = versionId != null ? "s3:DeleteObjectVersion" : "s3:DeleteObject";
|
||||
authorizeObjectWrite(bucketName, key, action, authorization);
|
||||
}
|
||||
|
||||
void authorizeObjectWrite(String bucketName, String key, String action, RequestAuthorization authorization) {
|
||||
if (!enforceAuth) {
|
||||
return;
|
||||
}
|
||||
|
||||
authorizeSignedRequest(authorization);
|
||||
RequestAuthorization requestAuthorization = authorization != null
|
||||
? authorization
|
||||
: RequestAuthorization.unsigned();
|
||||
if (requestAuthorization.signed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Bucket bucket = bucketStore.get(bucketName)
|
||||
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
|
||||
|
||||
String objectArn = S3PublicAccessEvaluator.objectArn(bucketName, key);
|
||||
S3PublicAccessEvaluator.PublicAccessDecision policyDecision =
|
||||
S3PublicAccessEvaluator.publicPolicyDecision(objectMapper, bucket.getPolicy(), action, objectArn);
|
||||
if (policyDecision == S3PublicAccessEvaluator.PublicAccessDecision.DENY) {
|
||||
throw new AwsException("AccessDenied", "Access Denied", 403);
|
||||
}
|
||||
if (policyDecision == S3PublicAccessEvaluator.PublicAccessDecision.ALLOW) {
|
||||
return;
|
||||
}
|
||||
if (isObjectCreationAction(action) && !readableObjectExists(bucketName, key)
|
||||
&& publicBucketAclAllowsWrite(bucket)) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new AwsException("AccessDenied", "Access Denied", 403);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks only credential validity, not per-resource authorization. Batch callers use this to
|
||||
* fail the whole request on a bad access key, rather than the same InvalidAccessKeyId
|
||||
* surfacing as a per-resource error on every item.
|
||||
*/
|
||||
void authorizeSignedRequest(RequestAuthorization authorization) {
|
||||
if (!enforceAuth) {
|
||||
return;
|
||||
}
|
||||
RequestAuthorization requestAuthorization = authorization != null
|
||||
? authorization
|
||||
: RequestAuthorization.unsigned();
|
||||
if (requestAuthorization.signed() && !isKnownAccessKey(requestAuthorization.accessKeyId())) {
|
||||
throw new AwsException("InvalidAccessKeyId",
|
||||
"The AWS Access Key Id you provided does not exist in our records.", 403);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean publicBucketAclAllowsWrite(Bucket bucket) {
|
||||
return Optional.ofNullable(bucket.getAcl())
|
||||
.map(S3AclPublicAccessEvaluator::aclAllowsPublicWrite)
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
boolean isAuthEnforced() {
|
||||
return enforceAuth;
|
||||
}
|
||||
@@ -646,6 +712,16 @@ public class S3Service implements Resettable {
|
||||
return "s3:GetObject".equals(action) || "s3:GetObjectVersion".equals(action);
|
||||
}
|
||||
|
||||
/**
|
||||
* A bucket ACL WRITE grant to a non-owner only authorizes creating a new object; per AWS's
|
||||
* ACL documentation it "denies non-owners the ability to overwrite or delete existing
|
||||
* objects." Floci has no per-object ownership model, so this is approximated as: only
|
||||
* PutObject on a key that doesn't exist yet counts as creation.
|
||||
*/
|
||||
private static boolean isObjectCreationAction(String action) {
|
||||
return "s3:PutObject".equals(action);
|
||||
}
|
||||
|
||||
private static boolean isUnsignedRequest(RequestAuthorization authorization) {
|
||||
return authorization == null || !authorization.signed();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user