mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(s3): resolve index documents for static-website directory requests (#1815)
* feat(s3): resolve static website index documents * fix(s3): drop the query string from the website folder redirect Verified against a live S3 website endpoint in us-east-1 rather than inferred: a bucket with photos/index.html answers GET /photos?code=abc&state=xyz -> 302, Location: /photos/ with a bare Location. Same for HEAD and for nested prefixes (/a/b?x=1 -> /a/b/). An earlier commit added query-string preservation on the strength of a review comment asserting AWS behaved that way; the probe shows it does not, and LocalStack's website_hosting.py agrees with the probe. An SPA whose OAuth callback lands on a slash-less folder loses its parameters on real AWS too, so the emulator matches rather than improving on it.
This commit is contained in:
1 parent
41d2a3cd19
commit
6d829ff7a4
5 files changed
+423
-52
No files matched your search
@@ -231,6 +231,12 @@ public class S3Controller {
|
||||
try {
|
||||
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
|
||||
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
|
||||
if (isWebsiteRequest(httpHeaders, uriInfo)) {
|
||||
Response websiteResponse = serveWebsiteObject(bucket, "", authorization);
|
||||
if (websiteResponse != null) {
|
||||
return headOnlyResponse(websiteResponse);
|
||||
}
|
||||
}
|
||||
s3Service.authorizeBucketRead(bucket, "s3:ListBucket", authorization);
|
||||
s3Service.headBucket(bucket);
|
||||
String bucketRegion = s3Service.getBucketRegion(bucket);
|
||||
@@ -497,35 +503,15 @@ public class S3Controller {
|
||||
return Response.ok(s3Service.getBucketRequestPayment(bucket)).build();
|
||||
}
|
||||
|
||||
// --- Website Hosting Redirection Logic ---
|
||||
if (isWebsiteRequest(httpHeaders, uriInfo) && (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type")))) {
|
||||
try {
|
||||
WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);
|
||||
if (webConfig.getIndexDocument() != null) {
|
||||
try {
|
||||
s3Service.authorizeGetObject(bucket, webConfig.getIndexDocument(), null, authorization);
|
||||
S3Object indexObj = s3Service.getObject(bucket, webConfig.getIndexDocument());
|
||||
return Response.ok(indexObj.getData())
|
||||
.type(indexObj.getContentType())
|
||||
.header("Content-Length", indexObj.getSize())
|
||||
.header("ETag", indexObj.getETag())
|
||||
.header("x-amz-website-redirect-location", "index")
|
||||
.build();
|
||||
} catch (AwsException e) {
|
||||
if (!isWebsiteErrorDocumentTrigger(e)) {
|
||||
throw e;
|
||||
}
|
||||
Response r = serveErrorDocument(bucket, webConfig, authorization, e.getHttpStatus());
|
||||
if (r != null) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
if (!"NoSuchWebsiteConfiguration".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
// Bucket is not a website, continue to listObjects
|
||||
// --- S3 static-website index resolution (site root) ---
|
||||
// A website endpoint has no S3 REST API, so it serves the index document for the site root
|
||||
// regardless of any query string — e.g. a single-page-app OAuth callback GET
|
||||
// /?code=...&state=... must return index.html, not a ListObjects response. (?list-type and
|
||||
// other sub-resource queries only reach the REST endpoint, never a website host.)
|
||||
if (isWebsiteRequest(httpHeaders, uriInfo)) {
|
||||
Response website = serveWebsiteObject(bucket, "", authorization);
|
||||
if (website != null) {
|
||||
return website;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -746,6 +732,13 @@ public class S3Controller {
|
||||
authorization = S3RequestAuthorizationParser.parseIfRequired(
|
||||
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
|
||||
|
||||
if (isWebsiteRequest(httpHeaders, uriInfo)) {
|
||||
Response website = serveWebsiteObject(bucket, key, authorization);
|
||||
if (website != null) {
|
||||
return website;
|
||||
}
|
||||
}
|
||||
|
||||
if (uploadId != null) {
|
||||
s3Service.authorizeObjectRead(bucket, key, versionId, "s3:ListMultipartUploadParts", authorization);
|
||||
return handleListParts(bucket, key, uploadId, maxPartsQuery, partNumberMarkerQuery);
|
||||
@@ -816,16 +809,9 @@ public class S3Controller {
|
||||
} catch (AwsException e) {
|
||||
emitCloudTrailEvent("GetObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage());
|
||||
if (isWebsiteErrorDocumentTrigger(e) && isWebsiteRequest(httpHeaders, uriInfo)) {
|
||||
try {
|
||||
WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);
|
||||
Response r = serveErrorDocument(bucket, webConfig, authorization, e.getHttpStatus());
|
||||
if (r != null) {
|
||||
return r;
|
||||
}
|
||||
} catch (AwsException websiteException) {
|
||||
if (!"NoSuchWebsiteConfiguration".equals(websiteException.getErrorCode())) {
|
||||
return xmlErrorResponse(websiteException);
|
||||
}
|
||||
Response websiteError = serveWebsiteErrorResponse(bucket, authorization, e);
|
||||
if (websiteError != null) {
|
||||
return websiteError;
|
||||
}
|
||||
}
|
||||
return xmlErrorResponse(e);
|
||||
@@ -964,10 +950,17 @@ public class S3Controller {
|
||||
@HeaderParam("x-amz-checksum-mode") String checksumMode,
|
||||
@Context UriInfo uriInfo,
|
||||
@Context HttpHeaders httpHeaders) {
|
||||
S3Service.RequestAuthorization authorization = S3Service.RequestAuthorization.unsigned();
|
||||
try {
|
||||
key = extractObjectKey(uriInfo, bucket);
|
||||
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
|
||||
authorization = S3RequestAuthorizationParser.parseIfRequired(
|
||||
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
|
||||
if (isWebsiteRequest(httpHeaders, uriInfo)) {
|
||||
Response websiteResponse = serveWebsiteObject(bucket, key, authorization);
|
||||
if (websiteResponse != null) {
|
||||
return headOnlyResponse(websiteResponse);
|
||||
}
|
||||
}
|
||||
s3Service.authorizeGetObject(bucket, key, versionId, authorization);
|
||||
|
||||
S3Object obj = s3Service.headObject(bucket, key, versionId);
|
||||
@@ -1002,6 +995,12 @@ public class S3Controller {
|
||||
return resp.build();
|
||||
} catch (AwsException e) {
|
||||
emitCloudTrailEvent("HeadObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage());
|
||||
if (isWebsiteErrorDocumentTrigger(e) && isWebsiteRequest(httpHeaders, uriInfo)) {
|
||||
Response websiteError = serveWebsiteErrorResponse(bucket, authorization, e);
|
||||
if (websiteError != null) {
|
||||
return headOnlyResponse(websiteError);
|
||||
}
|
||||
}
|
||||
return xmlErrorResponse(e);
|
||||
}
|
||||
}
|
||||
@@ -2193,12 +2192,104 @@ public class S3Controller {
|
||||
return host != null && host.contains("s3-website");
|
||||
}
|
||||
|
||||
private Response serveErrorDocument(String bucket, WebsiteConfiguration cfg,
|
||||
S3Service.RequestAuthorization authorization, int status) {
|
||||
if (cfg.getErrorDocument() == null) {
|
||||
private static Response headOnlyResponse(Response response) {
|
||||
return Response.fromResponse(response)
|
||||
.entity(null)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies S3 static-website index-document resolution to a website-endpoint GET, mirroring how the
|
||||
* real {@code <bucket>.s3-website-<region>.amazonaws.com} endpoint serves a site:
|
||||
* <ul>
|
||||
* <li>a "directory" request (the site root, or any key ending in {@code /}) serves the index
|
||||
* document for that prefix — e.g. {@code /docs/} serves {@code docs/index.html};</li>
|
||||
* <li>a non-slash path that is not itself an object but has an index document underneath it is a
|
||||
* folder, so it 302-redirects to the slash-terminated form (so the page's relative asset URLs
|
||||
* resolve against the right base);</li>
|
||||
* <li>a missing index document returns the configured error document, or S3's default
|
||||
* website error response when no custom document is configured.</li>
|
||||
* </ul>
|
||||
* Returns {@code null} when the request should be served by the normal object path — i.e. an exact
|
||||
* object hit, or a bucket that has no website configuration at all. The index read is authorized
|
||||
* (a no-op unless S3 auth enforcement is enabled), matching the object-serving path.
|
||||
*/
|
||||
private Response serveWebsiteObject(String bucket, String key,
|
||||
S3Service.RequestAuthorization authorization) {
|
||||
WebsiteConfiguration cfg;
|
||||
try {
|
||||
cfg = s3Service.getBucketWebsite(bucket);
|
||||
} catch (AwsException e) {
|
||||
// Only "no website configuration" means fall through to normal handling; a real error
|
||||
// (e.g. NoSuchBucket) must propagate rather than be masked as "not a website".
|
||||
if (!"NoSuchWebsiteConfiguration".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
String index = cfg.getIndexDocument();
|
||||
if (index == null) {
|
||||
return null;
|
||||
}
|
||||
// The routing layer strips a trailing slash from the object key, so recover the "directory"
|
||||
// intent from the raw request path: a trailing slash (or the site root) means "serve this
|
||||
// prefix's index document"; the prefix is the key without any trailing slash.
|
||||
var request = currentVertxRequest.getCurrent().request();
|
||||
String rawPath = request.path();
|
||||
boolean directory = key.isEmpty() || rawPath.endsWith("/");
|
||||
String prefix = key.endsWith("/") ? key.substring(0, key.length() - 1) : key;
|
||||
|
||||
if (directory) {
|
||||
String indexKey = prefix.isEmpty() ? index : prefix + "/" + index;
|
||||
try {
|
||||
s3Service.authorizeGetObject(bucket, indexKey, null, authorization);
|
||||
S3Object indexObj = s3Service.headObject(bucket, indexKey, null);
|
||||
// A website endpoint serves the index document with no response-header overrides and no
|
||||
// checksum headers (no viewer sends response-* or x-amz-checksum-mode to a website endpoint).
|
||||
return fullObjectResponse(bucket, indexKey, null, indexObj,
|
||||
new ResponseHeaderOverrides(null, null, null, null, null, null), false);
|
||||
} catch (AwsException e) {
|
||||
if (!isWebsiteErrorDocumentTrigger(e)) {
|
||||
throw e;
|
||||
}
|
||||
Response err = serveErrorDocument(bucket, cfg, authorization, e.getHttpStatus());
|
||||
return err != null ? err : xmlErrorResponse(e);
|
||||
}
|
||||
}
|
||||
// Not slash-terminated: an exact object is served by the normal path; a prefix that exists only
|
||||
// as a "folder" (an index document lives beneath it) 302-redirects to the slash-terminated form
|
||||
// so the page's relative asset URLs resolve against the right base (matching real S3).
|
||||
if (!s3Service.objectExists(bucket, prefix) && s3Service.objectExists(bucket, prefix + "/" + index)) {
|
||||
// The query string is deliberately dropped: real S3 answers
|
||||
// GET /photos?code=abc&state=xyz with a bare "Location: /photos/" (verified against a
|
||||
// live website endpoint in us-east-1, same for HEAD and for nested prefixes).
|
||||
return Response.status(Response.Status.FOUND)
|
||||
.header("Location", rawPath + "/")
|
||||
.build();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Response serveWebsiteErrorResponse(String bucket,
|
||||
S3Service.RequestAuthorization authorization,
|
||||
AwsException cause) {
|
||||
try {
|
||||
WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);
|
||||
return serveErrorDocument(bucket, webConfig, authorization, cause.getHttpStatus());
|
||||
} catch (AwsException websiteException) {
|
||||
if (!"NoSuchWebsiteConfiguration".equals(websiteException.getErrorCode())) {
|
||||
return xmlErrorResponse(websiteException);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Response serveErrorDocument(String bucket, WebsiteConfiguration cfg,
|
||||
S3Service.RequestAuthorization authorization, int status) {
|
||||
int responseStatus = status == 403 ? 403 : 404;
|
||||
if (cfg.getErrorDocument() == null) {
|
||||
return defaultWebsiteErrorResponse(responseStatus);
|
||||
}
|
||||
try {
|
||||
s3Service.authorizeGetObject(bucket, cfg.getErrorDocument(), null, authorization);
|
||||
S3Object err = s3Service.getObject(bucket, cfg.getErrorDocument());
|
||||
@@ -2213,15 +2304,21 @@ public class S3Controller {
|
||||
if (!isWebsiteErrorDocumentTrigger(e)) {
|
||||
throw e;
|
||||
}
|
||||
return Response.status(responseStatus)
|
||||
.entity(defaultWebsiteErrorBody(responseStatus))
|
||||
.type(MediaType.TEXT_HTML)
|
||||
.header("x-amz-error-code", websiteErrorCode(responseStatus))
|
||||
.header("x-amz-error-message", websiteErrorMessage(responseStatus))
|
||||
.build();
|
||||
return defaultWebsiteErrorResponse(responseStatus);
|
||||
}
|
||||
}
|
||||
|
||||
private static Response defaultWebsiteErrorResponse(int status) {
|
||||
String body = defaultWebsiteErrorBody(status);
|
||||
return Response.status(status)
|
||||
.entity(body)
|
||||
.type(MediaType.TEXT_HTML)
|
||||
.header("Content-Length", body.getBytes(StandardCharsets.UTF_8).length)
|
||||
.header("x-amz-error-code", websiteErrorCode(status))
|
||||
.header("x-amz-error-message", websiteErrorMessage(status))
|
||||
.build();
|
||||
}
|
||||
|
||||
private static boolean isWebsiteErrorDocumentTrigger(AwsException e) {
|
||||
return "NoSuchKey".equals(e.getErrorCode()) || "AccessDenied".equals(e.getErrorCode());
|
||||
}
|
||||
|
||||
@@ -639,6 +639,22 @@ public class S3Service implements Resettable {
|
||||
return getObjectMetadata(bucketName, key, versionId);
|
||||
}
|
||||
|
||||
/** Returns true when the (latest-version) object exists, without throwing on a miss. */
|
||||
public boolean objectExists(String bucketName, String key) {
|
||||
try {
|
||||
getObjectMetadata(bucketName, key, null);
|
||||
return true;
|
||||
} catch (AwsException e) {
|
||||
// Only a genuine miss means "does not exist"; surface any other storage error (e.g.
|
||||
// NoSuchBucket) instead of masking it as absent, which would let callers act on a wrong
|
||||
// answer (e.g. issue a website redirect that hides the real failure).
|
||||
if ("NoSuchKey".equals(e.getErrorCode()) || "NoSuchVersion".equals(e.getErrorCode())) {
|
||||
return false;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream openObjectStream(String bucketName, String key, String versionId) {
|
||||
getObjectMetadata(bucketName, key, versionId);
|
||||
if (inMemory) {
|
||||
|
||||
Reference in new issue
Block a user