From 28a1427ea8b590adf86a1da2a23db7d337674710 Mon Sep 17 00:00:00 2001 From: Eduardo Lugo Vela <116361712+JESUSLUG@users.noreply.github.com> Date: Thu, 25 Jun 2026 00:09:34 -0600 Subject: [PATCH] =?UTF-8?q?fix(s3):=20serve=20custom=20error=20document=20?= =?UTF-8?q?for=20missing=20keys=20in=20static=20websi=E2=80=A6=20(#1334)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(s3): serve custom error document for missing keys in static website hosting When a bucket has a website configuration with an ErrorDocument, requests to missing keys now return the error document with a 404 status instead of the default XML NoSuchKey error. * fix(test): handle null output in waitForExecution to prevent flaky CI failures * fix(s3): address review feedback on static website hosting * fix(s3): recognize s3-website hostnames in virtual host filter * fix(s3): gate website error document on website endpoint host * fix(s3): restore validateRawUri inside try block after rebase --- .../floci/services/s3/S3Controller.java | 44 ++++++++- .../services/s3/S3VirtualHostFilter.java | 4 + .../services/s3/S3WebsiteIntegrationTest.java | 95 ++++++++++++++++--- .../StepFunctionsDynamoDbIntegrationTest.java | 5 +- 4 files changed, 132 insertions(+), 16 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java index 9a864b3a34..4ebd776474 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java @@ -307,7 +307,8 @@ public class S3Controller { @QueryParam("encoding-type") String encodingType, @QueryParam("key-marker") String keyMarker, @QueryParam("marker") String marker, - @Context UriInfo uriInfo) { + @Context UriInfo uriInfo, + @Context HttpHeaders httpHeaders) { try { validateRawUri(); if (hasQueryParam(uriInfo, "uploads")) { @@ -368,7 +369,7 @@ public class S3Controller { } // --- Website Hosting Redirection Logic --- - if (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type"))) { + if (isWebsiteRequest(httpHeaders) && (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type")))) { try { WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket); if (webConfig.getIndexDocument() != null) { @@ -381,7 +382,8 @@ public class S3Controller { .header("x-amz-website-redirect-location", "index") .build(); } catch (AwsException e) { - // If index.html is missing, we could serve ErrorDocument, but for now we fall back to listObjects + Response r = serveErrorDocument(bucket, webConfig); + if (r != null) return r; } } } catch (AwsException e) { @@ -634,6 +636,14 @@ public class S3Controller { return fullObjectResponse(bucket, key, versionId, obj, overrides); } catch (AwsException e) { + if ("NoSuchKey".equals(e.getErrorCode()) && isWebsiteRequest(httpHeaders)) { + try { + WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket); + Response r = serveErrorDocument(bucket, webConfig); + if (r != null) return r; + } catch (AwsException ignored) { + } + } return xmlErrorResponse(e); } } @@ -1988,6 +1998,34 @@ public class S3Controller { } } + private static boolean isWebsiteRequest(HttpHeaders httpHeaders) { + String host = httpHeaders.getHeaderString("Host"); + return host != null && host.contains("s3-website"); + } + + private Response serveErrorDocument(String bucket, WebsiteConfiguration cfg) { + if (cfg.getErrorDocument() == null) { + return null; + } + try { + S3Object err = s3Service.getObject(bucket, cfg.getErrorDocument()); + return Response.status(404) + .entity(err.getData()) + .type(err.getContentType()) + .header("Content-Length", err.getSize()) + .header("x-amz-error-code", "NoSuchKey") + .header("x-amz-error-message", "The specified key does not exist.") + .build(); + } catch (AwsException ignored) { + return Response.status(404) + .entity("