Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix signature check when content length not included #65

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/versitygw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
adminSecret string
region string
certFile, keyFile string
debug bool
)

var (
Expand Down Expand Up @@ -119,6 +120,11 @@ func initFlags() []cli.Flag {
Usage: "TLS key file",
Destination: &keyFile,
},
&cli.BoolFlag{
Name: "debug",
Usage: "enable debug output",
Destination: &debug,
},
}
}

Expand All @@ -145,6 +151,10 @@ func runGateway(be backend.Backend) error {
opts = append(opts, s3api.WithTLS(cert))
}

if debug {
opts = append(opts, s3api.WithDebug())
}

srv, err := s3api.New(app, be, port,
middlewares.AdminConfig{
AdminAccess: adminAccess,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/aws/aws-sdk-go-v2 v1.18.0
github.com/aws/aws-sdk-go-v2/service/s3 v1.33.1
github.com/aws/smithy-go v1.13.5
github.com/gofiber/fiber/v2 v2.46.0
github.com/google/uuid v1.3.0
github.com/pkg/xattr v0.4.9
Expand All @@ -23,7 +24,6 @@ require (
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.28 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.27 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.14.2 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
10 changes: 7 additions & 3 deletions s3api/middlewares/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ package middlewares
import (
"crypto/sha256"
"encoding/hex"
"os"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/smithy-go/logging"
"github.com/gofiber/fiber/v2"
"github.com/versity/versitygw/backend/auth"
"github.com/versity/versitygw/s3api/controllers"
Expand All @@ -39,7 +41,7 @@ type AdminConfig struct {
Region string
}

func VerifyV4Signature(config AdminConfig, iam auth.IAMService) fiber.Handler {
func VerifyV4Signature(config AdminConfig, iam auth.IAMService, debug bool) fiber.Handler {
acct := accounts{
admin: config,
iam: iam,
Expand Down Expand Up @@ -115,8 +117,10 @@ func VerifyV4Signature(config AdminConfig, iam auth.IAMService) fiber.Handler {
AccessKeyID: creds[0],
SecretAccessKey: secret,
}, req, hexPayload, creds[3], config.Region, tdate, func(options *v4.SignerOptions) {
//options.LogSigning = true
//options.Logger = logging.NewStandardLogger(os.Stdout)
if debug {
options.LogSigning = true
options.Logger = logging.NewStandardLogger(os.Stderr)
}
})
if signErr != nil {
return controllers.Responce[any](ctx, nil, s3err.GetAPIError(s3err.ErrInternalError))
Expand Down
8 changes: 7 additions & 1 deletion s3api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type S3ApiServer struct {
router *S3ApiRouter
port string
cert *tls.Certificate
debug bool
}

func New(app *fiber.App, be backend.Backend, port string, adminUser middlewares.AdminConfig, iam auth.IAMService, opts ...Option) (*S3ApiServer, error) {
Expand All @@ -44,7 +45,7 @@ func New(app *fiber.App, be backend.Backend, port string, adminUser middlewares.
opt(server)
}

app.Use(middlewares.VerifyV4Signature(adminUser, iam))
app.Use(middlewares.VerifyV4Signature(adminUser, iam, server.debug))
app.Use(logger.New())
app.Use(middlewares.VerifyMD5Body())
server.router.Init(app, be)
Expand All @@ -59,6 +60,11 @@ func WithTLS(cert tls.Certificate) Option {
return func(s *S3ApiServer) { s.cert = &cert }
}

// WithDebug sets debug output
func WithDebug() Option {
return func(s *S3ApiServer) { s.debug = true }
}

func (sa *S3ApiServer) Serve() (err error) {
if sa.cert != nil {
return sa.app.ListenTLSWithCertificate(sa.port, *sa.cert)
Expand Down
6 changes: 6 additions & 0 deletions s3api/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func CreateHttpRequestFromCtx(ctx *fiber.Ctx, signedHdrs []string) (*http.Reques
}
})

// Check if Content-Length in signed headers
// If content length is non 0, then the header will be included
if !includeHeader("Content-Length", signedHdrs) {
httpReq.ContentLength = 0
}

// Set the Host header
httpReq.Host = string(req.Header.Host())

Expand Down