The GraphQL endpoint built its gqlgen server with bare handler.New and no limits, so a single request with thousands of aliased resolver calls was parsed, validated, executed, and marshalled in full. Under load this let an unauthenticated client drive excessive CPU and memory use against POST /api/connect/v1/graphql and the console and trust endpoints, which share the same constructor (GHSA-prh2-g8pv-m7p9). Add configurable guards in the shared gqlutils.NewHandler: a parser token limit rejects oversized queries at lex time before any execution, a fixed complexity limit caps field-selection count, an LRU query cache avoids repeated parsing, and field suggestions are disabled. The limits flow from a new APIConfig.GraphQL section through server and api config into all three GraphQL handlers, with PROBOD_API_GRAPHQL_* env vars and Helm values exposed for per-environment tuning. Defaults are sized with generous headroom over real traffic: the parser token limit (15000) and complexity limit (2000) sit far above the largest legitimate frontend query yet well below the proof-of-concept flood, so normal usage is unaffected while floods are rejected cheaply. Signed-off-by: Bryan Frimin <bryan@probo.com>
139 lines
5.3 KiB
Go
139 lines
5.3 KiB
Go
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//go:generate go run github.com/99designs/gqlgen generate
|
|
|
|
// Copyright (c) 2025 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
package connect_v1
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"go.gearno.de/kit/log"
|
|
"go.probo.inc/probo/pkg/baseurl"
|
|
"go.probo.inc/probo/pkg/filemanager"
|
|
"go.probo.inc/probo/pkg/gid"
|
|
"go.probo.inc/probo/pkg/iam"
|
|
"go.probo.inc/probo/pkg/iam/oauth2scope"
|
|
"go.probo.inc/probo/pkg/saferedirect"
|
|
"go.probo.inc/probo/pkg/securecookie"
|
|
"go.probo.inc/probo/pkg/server/api/authn"
|
|
"go.probo.inc/probo/pkg/server/api/authz"
|
|
"go.probo.inc/probo/pkg/server/api/connect/v1/types"
|
|
"go.probo.inc/probo/pkg/server/gqlutils"
|
|
)
|
|
|
|
type (
|
|
Resolver struct {
|
|
authorize authz.AuthorizeFunc
|
|
batchAuthorize authz.BatchAuthorizeFunc
|
|
logger *log.Logger
|
|
iam *iam.Service
|
|
scopeRegistry *oauth2scope.Registry
|
|
fileManager *filemanager.Service
|
|
baseURL *baseurl.BaseURL
|
|
sessionCookie *authn.Cookie
|
|
}
|
|
)
|
|
|
|
func NewMux(
|
|
logger *log.Logger,
|
|
svc *iam.Service,
|
|
cookieConfig securecookie.Config,
|
|
tokenSecret string,
|
|
fileManagerSvc *filemanager.Service,
|
|
baseURL *baseurl.BaseURL,
|
|
allowedRedirectHost saferedirect.AllowedHostFunc,
|
|
isTrustCenterDomain IsTrustCenterDomainFunc,
|
|
graphqlLimits gqlutils.Limits,
|
|
) *chi.Mux {
|
|
r := chi.NewMux()
|
|
|
|
sessionMiddleware := authn.NewSessionMiddleware(svc, cookieConfig)
|
|
apiKeyMiddleware := authn.NewAPIKeyMiddleware(svc, tokenSecret)
|
|
oauth2Middleware := authn.NewOAuth2AccessTokenMiddleware(svc)
|
|
identityPresenceMiddleware := authn.NewIdentityPresenceMiddleware(baseURL)
|
|
graphqlHandler := NewGraphQLHandler(svc, logger, fileManagerSvc, baseURL, cookieConfig, graphqlLimits)
|
|
samlHandler := NewSAMLHandler(svc, cookieConfig, baseURL, logger)
|
|
scimHandler := NewSCIMHandler(svc, logger.Named("scim"))
|
|
|
|
router := r.With(
|
|
sessionMiddleware,
|
|
apiKeyMiddleware,
|
|
oauth2Middleware,
|
|
)
|
|
|
|
oidcHandler := NewOIDCHandler(svc, cookieConfig, logger, allowedRedirectHost, isTrustCenterDomain)
|
|
|
|
router.Handle("/graphql", graphqlHandler)
|
|
router.Get("/saml/2.0/metadata", samlHandler.MetadataHandler)
|
|
router.Post("/saml/2.0/consume", samlHandler.ConsumeHandler)
|
|
router.Get("/saml/2.0/{samlConfigID}", samlHandler.LoginHandler)
|
|
router.Get("/oidc/{provider}/login", oidcHandler.LoginHandler)
|
|
router.Get("/oidc/{provider}/callback", oidcHandler.CallbackHandler)
|
|
|
|
// SCIM 2.0 endpoints - these use their own bearer token authentication
|
|
scimServer := NewSCIMServer(scimHandler)
|
|
r.Mount("/scim/2.0", http.StripPrefix("/scim/2.0", scimHandler.BearerTokenMiddleware(scimServer)))
|
|
|
|
// OAuth2 / OpenID Connect server endpoints.
|
|
oauth2Handler := NewOAuth2Handler(svc, cookieConfig, baseURL, logger)
|
|
|
|
// Public endpoints (no authentication).
|
|
r.Get("/oauth2/jwks", oauth2Handler.JWKSHandler)
|
|
r.Post("/oauth2/token", oauth2Handler.TokenHandler)
|
|
r.Post("/oauth2/device", oauth2Handler.DeviceAuthHandler)
|
|
|
|
// Bearer-token authenticated endpoints.
|
|
bearerAuth := r.With(oauth2Handler.BearerTokenMiddleware)
|
|
bearerAuth.Get("/oauth2/userinfo", oauth2Handler.UserInfoHandler)
|
|
|
|
// Client-authenticated endpoints.
|
|
clientAuth := r.With(oauth2Handler.ClientAuthMiddleware)
|
|
clientAuth.Post("/oauth2/introspect", oauth2Handler.IntrospectHandler)
|
|
clientAuth.Post("/oauth2/revoke", oauth2Handler.RevokeHandler)
|
|
|
|
// Session-authenticated endpoints.
|
|
router.Get("/oauth2/authorize", oauth2Handler.AuthorizeHandler)
|
|
|
|
requireIdentity := router.With(identityPresenceMiddleware)
|
|
requireIdentity.Post("/oauth2/register", oauth2Handler.RegisterHandler)
|
|
|
|
return r
|
|
}
|
|
|
|
func (r *Resolver) Permission(ctx context.Context, obj types.Node, action string) (bool, error) {
|
|
_, err := r.authorize(ctx, obj.GetID(), action, authz.WithDryRun())
|
|
return err == nil, nil
|
|
}
|
|
|
|
func (r *Resolver) SSOLoginURL(samlConfigID gid.GID) string {
|
|
return r.baseURL.WithPath("/api/connect/v1/saml/2.0/" + samlConfigID.String()).MustString()
|
|
}
|