Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
48 changes: 35 additions & 13 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,16 @@ func createOIDCConfig(ctx context.Context, logger slog.Logger, vals *codersdk.De
return nil, xerrors.Errorf("parse oidc redirect url %q", err)
}
logger.Warn(ctx, "custom OIDC redirect URL used instead of 'access_url', ensure this matches the value configured in your OIDC provider")
if len(vals.OIDC.RedirectAllowedHosts.Value()) > 0 {
// Static override takes precedence; keep the behavior explicit and
// loud rather than silently mixing the two modes.
logger.Warn(ctx, "ignoring CODER_OIDC_REDIRECT_ALLOWED_HOSTS because CODER_OIDC_REDIRECT_URL is set")
}
}
// Capture the configured scheme for the dynamic-host code path so that
// the dynamic redirect_uri uses the same scheme as the static one even
// when upstream proxies report a misleading X-Forwarded-Proto.
redirectDefaultScheme := redirectURL.Scheme

// If the scopes contain 'groups', we enable group support.
// Do not override any custom value set by the user.
Expand Down Expand Up @@ -259,6 +268,17 @@ func createOIDCConfig(ctx context.Context, logger slog.Logger, vals *codersdk.De
return nil, xerrors.Errorf("pkce detect in claims: %w", err)
}

// CODER_OIDC_REDIRECT_URL is a strict override: when set, the redirect_uri
// is fixed at startup and dynamic-host selection is disabled. Otherwise,
// surface the allowlist to the middleware.
var redirectAllowedHosts []string
if vals.OIDC.RedirectURL.String() == "" {
redirectAllowedHosts = vals.OIDC.RedirectAllowedHosts.Value()
} else {
// Static-override mode does not need the dynamic default scheme.
redirectDefaultScheme = ""
}

return &coderd.OIDCConfig{
OAuth2Config: useCfg,
Provider: oidcProvider,
Expand All @@ -268,19 +288,21 @@ func createOIDCConfig(ctx context.Context, logger slog.Logger, vals *codersdk.De
// matches the issuer URL. This is not recommended.
SkipIssuerCheck: vals.OIDC.SkipIssuerChecks.Value(),
}),
EmailDomain: vals.OIDC.EmailDomain,
AllowSignups: vals.OIDC.AllowSignups.Value(),
UsernameField: vals.OIDC.UsernameField.String(),
NameField: vals.OIDC.NameField.String(),
EmailField: vals.OIDC.EmailField.String(),
AuthURLParams: vals.OIDC.AuthURLParams.Value,
SecondaryClaims: secondaryClaimsSrc,
SignInText: vals.OIDC.SignInText.String(),
SignupsDisabledText: vals.OIDC.SignupsDisabledText.String(),
IconURL: vals.OIDC.IconURL.String(),
IgnoreEmailVerified: vals.OIDC.IgnoreEmailVerified.Value(),
PKCEMethods: pkceSupport.CodeChallengeMethodsSupported,
EmailFallback: vals.OIDC.EmailFallback.Value(),
EmailDomain: vals.OIDC.EmailDomain,
AllowSignups: vals.OIDC.AllowSignups.Value(),
UsernameField: vals.OIDC.UsernameField.String(),
NameField: vals.OIDC.NameField.String(),
EmailField: vals.OIDC.EmailField.String(),
AuthURLParams: vals.OIDC.AuthURLParams.Value,
SecondaryClaims: secondaryClaimsSrc,
SignInText: vals.OIDC.SignInText.String(),
SignupsDisabledText: vals.OIDC.SignupsDisabledText.String(),
IconURL: vals.OIDC.IconURL.String(),
IgnoreEmailVerified: vals.OIDC.IgnoreEmailVerified.Value(),
PKCEMethods: pkceSupport.CodeChallengeMethodsSupported,
EmailFallback: vals.OIDC.EmailFallback.Value(),
RedirectAllowedHosts: redirectAllowedHosts,
RedirectDefaultScheme: redirectDefaultScheme,
}, nil
}

Expand Down
59 changes: 59 additions & 0 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,65 @@ func TestServer(t *testing.T) {
}
}
})

t.Run("RedirectAllowedHosts", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
defer cancel()

// Same fake-issuer setup as the other OIDC subtests.
oidcServer := httptest.NewServer(nil)
fakeWellKnownHandler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
payload := fmt.Sprintf("{\"issuer\": %q}", oidcServer.URL)
_, _ = w.Write([]byte(payload))
}
oidcServer.Config.Handler = http.HandlerFunc(fakeWellKnownHandler)
t.Cleanup(oidcServer.Close)

inv, cfg := clitest.New(t,
"server",
dbArg(t),
"--http-address", ":0",
"--access-url", "http://example.com",
"--oidc-client-id", "fake",
"--oidc-client-secret", "fake",
"--oidc-issuer-url", oidcServer.URL,
"--oidc-redirect-allowed-hosts", "coder.example.com,coder-walle.example.com",
)

clitest.Start(t, inv)
accessURL := waitAccessURL(t, cfg)
client := codersdk.New(accessURL)

randPassword, err := cryptorand.String(24)
require.NoError(t, err)

_, err = client.CreateFirstUser(ctx, codersdk.CreateFirstUserRequest{
Email: "[email protected]",
Password: randPassword,
Username: "admin",
Trial: true,
})
require.NoError(t, err)

loginResp, err := client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
Email: "[email protected]",
Password: randPassword,
})
require.NoError(t, err)
client.SetSessionToken(loginResp.SessionToken)

deploymentConfig, err := client.DeploymentConfig(ctx)
require.NoError(t, err)

// The CLI flag should have populated the runtime config.
require.Equal(t,
[]string{"coder.example.com", "coder-walle.example.com"},
deploymentConfig.Values.OIDC.RedirectAllowedHosts.Value(),
)
})
})

t.Run("RateLimit", func(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions cli/testdata/server-config.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,13 @@ oidc:
# enable if you understand and accept the risk.
# (default: <unset>, type: bool)
dangerousOidcEmailFallback: false
# An allowlist of hostnames that may be used as the host of the OIDC redirect_uri.
# When set, the redirect_uri sent to the OIDC provider is built from the incoming
# request's Host header (validated against this list) instead of from access-url.
# Every listed host must also be registered as a valid redirect URI in the OIDC
# provider. Ignored when oidc-redirect-url is set.
# (default: <unset>, type: string-array)
oidcRedirectAllowedHosts: []
# Telemetry is critical to our ability to improve Coder. We strip all personal
# information before sending data to our servers. Please only disable telemetry
# when required by your organization's security policy.
Expand Down
7 changes: 7 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,12 @@ func New(options *Options) *API {
}

var oidcAuthURLParams map[string]string
var oidcRedirectAllowedHosts []string
var oidcRedirectDefaultScheme string
if options.OIDCConfig != nil {
oidcAuthURLParams = options.OIDCConfig.AuthURLParams
oidcRedirectAllowedHosts = options.OIDCConfig.RedirectAllowedHosts
oidcRedirectDefaultScheme = options.OIDCConfig.RedirectDefaultScheme
}

api.Auditor.Store(&options.Auditor)
Expand Down Expand Up @@ -1111,7 +1115,7 @@ func New(options *Options) *API {
r.Route(fmt.Sprintf("/%s/callback", externalAuthConfig.ID), func(r chi.Router) {
r.Use(
apiKeyMiddlewareRedirect,
httpmw.ExtractOAuth2(externalAuthConfig, options.HTTPClient, options.DeploymentValues.HTTPCookies, nil, externalAuthConfig.CodeChallengeMethodsSupported),
httpmw.ExtractOAuth2(externalAuthConfig, options.HTTPClient, options.DeploymentValues.HTTPCookies, nil, externalAuthConfig.CodeChallengeMethodsSupported, nil, ""),
)
r.Get("/", api.externalAuthCallback(externalAuthConfig))
})
Expand Down Expand Up @@ -1657,14 +1661,14 @@ func New(options *Options) *API {
r.Route("/github", func(r chi.Router) {
r.Use(
// Github supports PKCE S256
httpmw.ExtractOAuth2(options.GithubOAuth2Config, options.HTTPClient, options.DeploymentValues.HTTPCookies, nil, options.GithubOAuth2Config.PKCESupported()),
httpmw.ExtractOAuth2(options.GithubOAuth2Config, options.HTTPClient, options.DeploymentValues.HTTPCookies, nil, options.GithubOAuth2Config.PKCESupported(), nil, ""),
)
r.Get("/callback", api.userOAuth2Github)
})
})
r.Route("/oidc/callback", func(r chi.Router) {
r.Use(
httpmw.ExtractOAuth2(options.OIDCConfig, options.HTTPClient, options.DeploymentValues.HTTPCookies, oidcAuthURLParams, options.OIDCConfig.PKCESupported()),
httpmw.ExtractOAuth2(options.OIDCConfig, options.HTTPClient, options.DeploymentValues.HTTPCookies, oidcAuthURLParams, options.OIDCConfig.PKCESupported(), oidcRedirectAllowedHosts, oidcRedirectDefaultScheme),
)
r.Get("/", api.userOIDC)
})
Expand Down
Loading
Loading