mirror of
https://github.com/versity/versitygw.git
synced 2026-09-25 09:24:22 +00:00
Fixes #2239 Running the gateway with a `--region` other than `us-east-1` turned every request from a client signed for a different region into a dead end. The signing region check correctly rejected the request with `AuthorizationHeaderMalformed`, but the gateway gave the client no usable way to learn which region it should have signed for, so the aws sdks could not re-sign and retry the way they do against a real s3 regional endpoint. The gap was `x-amz-bucket-region`. S3 reports the expected region in that response header as well as in the error body, because a `HEAD` request carries no body for the client to parse. The gateway set the header only on successful responses, so `HeadBucket` and `HeadObject` signed for the wrong region came back as a bare `400` with an empty body and some sdks region redirect had nothing to work with. Region mismatches now report the gateway region through a `RegionMismatchError` interface that `MalformedAuthError`, `AuthQueryParamError` and `InvalidArgumentError` implement, and the error dispatch points set the header from it alongside the existing `Allow` header handling for `MethodNotAllowed`. The error bodies were incomplete in the same way. Only the `Authorization` header path carried a `Region` element, while the presigned url and POST form paths reported the mismatch in prose alone. `AuthorizationQueryParametersError` is now backed by a dedicated `AuthQueryParamError` type that adds the element, and `InvalidArgumentError` grew an optional `Region` field for the POST form credential error, both omitted for every other error of those kinds. The messages themselves now read the way s3 writes them. The region and service names are quoted with apostrophes instead of `%q`, whose double quotes the xml encoder rendered as `"` in the raw response, and the POST form authentication fields are echoed back in `ArgumentName` under their canonical spelling, for example `X-Amz-Credential` rather than the lowercase form the browser submits, while fields outside the signing protocol such as `key` are still reported as submitted.
209 lines
6.6 KiB
Go
209 lines
6.6 KiB
Go
// Copyright 2026 Versity Software
|
|
// This file is licensed under the Apache License, Version 2.0
|
|
// (the "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing,
|
|
// software distributed under the License is distributed on an
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
package s3err
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// argCredential is the canonical spelling of the POST form credential field.
|
|
// S3 echoes the form field names back in <ArgumentName> canonicalized, not as
|
|
// they are spelled in the submitted form.
|
|
const argCredential = "X-Amz-Credential"
|
|
|
|
// canonicalPOSTFormFields maps the POST form authentication field names to the
|
|
// spelling S3 reports them with. Fields that are not part of the signing
|
|
// protocol, such as "key", are reported as submitted.
|
|
var canonicalPOSTFormFields = map[string]string{
|
|
"x-amz-algorithm": "X-Amz-Algorithm",
|
|
"x-amz-credential": argCredential,
|
|
"x-amz-date": "X-Amz-Date",
|
|
"x-amz-signature": "X-Amz-Signature",
|
|
"x-amz-security-token": "X-Amz-Security-Token",
|
|
}
|
|
|
|
// canonicalPOSTFormField returns the spelling S3 reports field with, leaving
|
|
// fields outside the signing protocol untouched.
|
|
func canonicalPOSTFormField(field string) string {
|
|
if canonical, ok := canonicalPOSTFormFields[strings.ToLower(field)]; ok {
|
|
return canonical
|
|
}
|
|
|
|
return field
|
|
}
|
|
|
|
// Factory for building s3 object POST authentication errors.
|
|
func invalidPOSTObjectAuthErr(argName, argValue, format string, args ...any) InvalidArgumentError {
|
|
return InvalidArgumentError{
|
|
ArgumentName: argName,
|
|
ArgumentValue: argValue,
|
|
Description: fmt.Sprintf(format, args...),
|
|
}
|
|
}
|
|
|
|
type invalidPostAuthErr struct{}
|
|
|
|
func (invalidPostAuthErr) InvalidDateFormat(creds, date string) S3Error {
|
|
return invalidPOSTObjectAuthErr(
|
|
argCredential,
|
|
creds,
|
|
"incorrect date format %q. This date in the credential must be in the format \"yyyyMMdd\".",
|
|
date,
|
|
)
|
|
}
|
|
|
|
func (invalidPostAuthErr) MalformedCredential(creds string) S3Error {
|
|
return invalidPOSTObjectAuthErr(
|
|
argCredential,
|
|
creds,
|
|
"the Credential is mal-formed; expecting \"<YOUR-AKID>/YYYYMMDD/REGION/SERVICE/aws4_request\".",
|
|
)
|
|
}
|
|
|
|
func (invalidPostAuthErr) IncorrectTerminal(creds, terminal string) S3Error {
|
|
return invalidPOSTObjectAuthErr(
|
|
argCredential,
|
|
creds,
|
|
"incorrect terminal %q. This endpoint uses \"aws4_request\".",
|
|
terminal,
|
|
)
|
|
}
|
|
|
|
func (invalidPostAuthErr) IncorrectRegion(creds, expected, actual string) S3Error {
|
|
err := invalidPOSTObjectAuthErr(
|
|
argCredential,
|
|
creds,
|
|
"the region '%s' is wrong; expecting '%s'",
|
|
actual,
|
|
expected,
|
|
)
|
|
err.Region = expected
|
|
|
|
return err
|
|
}
|
|
|
|
func (invalidPostAuthErr) IncorrectService(creds, service string) S3Error {
|
|
return invalidPOSTObjectAuthErr(
|
|
argCredential,
|
|
creds,
|
|
"incorrect service %q. This endpoint belongs to \"s3\".",
|
|
service,
|
|
)
|
|
}
|
|
|
|
func (invalidPostAuthErr) MissingField(field string) S3Error {
|
|
name := canonicalPOSTFormField(field)
|
|
|
|
return invalidPOSTObjectAuthErr(
|
|
name,
|
|
"",
|
|
"Bucket POST must contain a field named '%s'. If it is specified, please check the order of the fields.",
|
|
name,
|
|
)
|
|
}
|
|
|
|
var PostAuth invalidPostAuthErr
|
|
|
|
// Factory for building s3 object POST authentication errors.
|
|
func invalidPolicyDocumentErr(format string, args ...any) APIError {
|
|
return APIError{
|
|
Code: "InvalidPolicyDocument",
|
|
Description: fmt.Sprintf(format, args...),
|
|
HTTPStatusCode: http.StatusBadRequest,
|
|
}
|
|
}
|
|
|
|
func invalidAccordingToPolicyErr(format string, args ...any) APIError {
|
|
return APIError{
|
|
Code: "AccessDenied",
|
|
Description: fmt.Sprintf(format, args...),
|
|
HTTPStatusCode: http.StatusForbidden,
|
|
}
|
|
}
|
|
|
|
type invalidPolicyDocument struct{}
|
|
|
|
func (invalidPolicyDocument) EmptyPolicy() S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Expecting '{' but found End-of-Input")
|
|
}
|
|
|
|
func (invalidPolicyDocument) InvalidBase64Encoding() S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: invalid Base64 encoding.")
|
|
}
|
|
|
|
func (invalidPolicyDocument) InvalidJSON() S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Invalid JSON.")
|
|
}
|
|
|
|
func (invalidPolicyDocument) UnexpectedField(field string) S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Unexpected: %q", field)
|
|
}
|
|
|
|
func (invalidPolicyDocument) MissingExpiration() S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Policy missing expiration.")
|
|
}
|
|
|
|
func (invalidPolicyDocument) InvalidExpiration(exp string) S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Invalid 'expiration' value: '%s'", exp)
|
|
}
|
|
|
|
func (invalidPolicyDocument) InvalidConditions() S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Invalid 'conditions' value: must be a List.")
|
|
}
|
|
|
|
func (invalidPolicyDocument) InvalidCondition() S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Invalid condition test: must be a List or Object.")
|
|
}
|
|
|
|
func (invalidPolicyDocument) MissingConditionOperationIdentifier() S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Invalid Condition: missing operation identifier.")
|
|
}
|
|
|
|
func (invalidPolicyDocument) UnknownConditionOperation(op string) S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Invalid Condition: unknown operation '%s'.", op)
|
|
}
|
|
|
|
func (invalidPolicyDocument) IncorrectConditionArgumentsNumber(op string) S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Invalid %s: wrong number of arguments.", op)
|
|
}
|
|
|
|
func (invalidPolicyDocument) MissingConditions() S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Policy missing conditions.")
|
|
}
|
|
|
|
func (invalidPolicyDocument) OnePropSimpleCondition() S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Invalid Simple-Condition: Simple-Conditions must have exactly one property specified.")
|
|
}
|
|
|
|
func (invalidPolicyDocument) InvalidSimpleCondition() S3Error {
|
|
return invalidPolicyDocumentErr("Invalid Policy: Invalid Simple-Condition: value must be a string.")
|
|
}
|
|
|
|
func (invalidPolicyDocument) ConditionFailed(condition string) S3Error {
|
|
return invalidAccordingToPolicyErr("Invalid according to Policy: Policy Condition failed: %s", condition)
|
|
}
|
|
|
|
func (invalidPolicyDocument) ExtraInputField(field string) S3Error {
|
|
return invalidAccordingToPolicyErr("Invalid according to Policy: Extra input fields: %s", field)
|
|
}
|
|
|
|
func (invalidPolicyDocument) PolicyExpired() S3Error {
|
|
return invalidAccordingToPolicyErr("Invalid according to Policy: Policy expired.")
|
|
}
|
|
|
|
var InvalidPolicyDocument invalidPolicyDocument
|