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

security: limit max content length to prevent a possible dos attack #423

Merged
merged 4 commits into from
Apr 9, 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
39 changes: 39 additions & 0 deletions cmd/api.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"

"entgo.io/contrib/entgql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/playground"
sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
_ "github.com/lib/pq"
"github.com/rs/cors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -67,12 +71,47 @@ var apiCmd = &cobra.Command{
AllowCredentials: true,
Debug: os.Getenv("DEBUG") == "true",
}).Handler)

router.Use(middleware.Timeout(60*time.Second), middleware.RealIP)
router.Use(api.AuthzByPolicyMiddleware)
router.Use(api.AuthenticationMiddleware)
if os.Getenv("DEBUG") == "true" {
router.Use(api.LoggingMiddleware)
}

if os.Getenv("DEBUG_PPROF") == "true" {
router.Mount("/debug", middleware.Profiler())
}

// Limit the request body size for all requests to the
// This is based on the chi middleware.RequestSize. Way this middleware is
// added to the router to use it
// https://github.com/go-chi/chi/blob/master/middleware/request_size.go
router.Use(func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {

// When image transfer is enabled, the request body size is limited.
// This is done to prevent the server from being overloaded by large
// DoS attack over the network.
const _50KB = (1 << 10) * 50

limitedBody := http.MaxBytesReader(w, r.Body, _50KB)
bodyBytes, err := ioutil.ReadAll(limitedBody)
limitedBody.Close()

// if r.Body reach the max size limit, the request will be canceled
// and the error will be returned to the client
if r.ContentLength > _50KB || err != nil {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return
}

r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
})

if *playgroudActive {
router.Handle("/", playground.Handler("GraphQL playground", "/graphql"))
log.Info().Msgf("connect to http://localhost:%s/ for GraphQL playground", *apiPortFlag)
Expand Down
2 changes: 1 addition & 1 deletion deploy/stacks/apps/s42/api.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module "api" {
replicas = 1
autoscaling = {
enabled = true
minReplicas = 1
minReplicas = 2
maxReplicas = 10
metrics = {
cpu = {
Expand Down
2 changes: 1 addition & 1 deletion deploy/stacks/apps/s42/interface.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module "interface" {
replicas = 1
autoscaling = {
enabled = true
minReplicas = 1
minReplicas = 2
maxReplicas = 10
metrics = {
cpu = {
Expand Down
2 changes: 1 addition & 1 deletion deploy/stacks/apps/s42/jwtks_service.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module "jwtks_service" {
replicas = 1
autoscaling = {
enabled = true
minReplicas = 1
minReplicas = 2
maxReplicas = 10
metrics = {
cpu = {
Expand Down
23 changes: 11 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ go 1.18
require (
entgo.io/contrib v0.3.0
entgo.io/ent v0.11.4
github.com/99designs/gqlgen v0.17.5-0.20220428154617-9250f9ac1f90
github.com/99designs/gqlgen v0.17.28
github.com/bwmarrin/discordgo v0.25.0
github.com/getsentry/sentry-go v0.13.0
github.com/go-chi/chi v1.5.4
github.com/go-chi/chi/v5 v5.0.7
github.com/go-chi/chi/v5 v5.0.8
github.com/google/go-github/v47 v47.0.1-0.20220822225427-243bda850b1f
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.1.1
Expand All @@ -22,16 +21,16 @@ require (
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.10.1
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.8.1
github.com/vektah/gqlparser/v2 v2.4.3-0.20220508162109-d3d9eb001575
github.com/stretchr/testify v1.8.2
github.com/vektah/gqlparser/v2 v2.5.1
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.9
go.opentelemetry.io/otel v1.9.0
go.opentelemetry.io/otel/exporters/jaeger v1.9.0
go.opentelemetry.io/otel/sdk v1.9.0
go.opentelemetry.io/otel/trace v1.9.0
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.28.0
google.golang.org/protobuf v1.28.1
)

require (
Expand All @@ -53,7 +52,7 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
Expand All @@ -80,11 +79,11 @@ require (
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.13-0.20220804200503-81c7dc4e4efa // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.6.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220414192740-2d67ff6cf2b4 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
Expand Down
Loading