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

Skip to content

fix: ignore https redirect for DERP meshing #10738

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions cli/cliutil/sink_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package cliutil_test

import (
"errors"
"testing"

"github.com/stretchr/testify/require"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/cliutil"
)

func TestDiscardAfterClose(t *testing.T) {
t.Parallel()
exErr := errors.New("test")
exErr := xerrors.New("test")
fwc := &fakeWriteCloser{err: exErr}
uut := cliutil.DiscardAfterClose(fwc)

Expand Down
11 changes: 9 additions & 2 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
// the request is not to a local IP.
var handler http.Handler = coderAPI.RootHandler
if vals.RedirectToAccessURL {
handler = redirectToAccessURL(handler, vals.AccessURL.Value(), tunnel != nil, appHostnameRegex)
handler = redirectToAccessURL(handler, vals.AccessURL.Value(), tunnel != nil, appHostnameRegex, options.IgnoreRedirectHostnames...)
}

// ReadHeaderTimeout is purposefully not enabled. It caused some
Expand Down Expand Up @@ -1916,7 +1916,7 @@ func ConfigureHTTPClient(ctx context.Context, clientCertFile, clientKeyFile stri
}

// nolint:revive
func redirectToAccessURL(handler http.Handler, accessURL *url.URL, tunnel bool, appHostnameRegex *regexp.Regexp) http.Handler {
func redirectToAccessURL(handler http.Handler, accessURL *url.URL, tunnel bool, appHostnameRegex *regexp.Regexp, ignoreHosts ...string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
redirect := func() {
http.Redirect(w, r, accessURL.String(), http.StatusTemporaryRedirect)
Expand Down Expand Up @@ -1945,6 +1945,13 @@ func redirectToAccessURL(handler http.Handler, accessURL *url.URL, tunnel bool,
return
}

for _, ignore := range ignoreHosts {
if r.Host == ignore {
handler.ServeHTTP(w, r)
return
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ain't gonna work. We install this middleware at start of day, before we learn about any other replicas, and they change over time.

I think we should blanket refuse to redirect the path /derp. DERP is encrypted, so even if some client connection got the http URL for this path, it would be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How so? We're only looking at the redirect for ourself, so it doesn't matter which replicas come up or down, it'll always be the same host locally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you're right. I'm thinking about it the wrong way around. Sorry.

However, disabling the redirect for all routes on that host feels too broad. I think we generally do want to redirect for non-DERP routes if the request comes to e.g. our IP address or a non-cannonical hostname.


redirect()
})
}
Expand Down
7 changes: 4 additions & 3 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ type Options struct {
RealIPConfig *httpmw.RealIPConfig
TrialGenerator func(ctx context.Context, email string) error
// TLSCertificates is used to mesh DERP servers securely.
TLSCertificates []tls.Certificate
TailnetCoordinator tailnet.Coordinator
DERPServer *derp.Server
TLSCertificates []tls.Certificate
TailnetCoordinator tailnet.Coordinator
IgnoreRedirectHostnames []string
DERPServer *derp.Server
// BaseDERPMap is used as the base DERP map for all clients and agents.
// Proxies are added to this list.
BaseDERPMap *tailcfg.DERPMap
Expand Down
3 changes: 3 additions & 0 deletions enterprise/cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func (r *RootCmd) Server(_ func()) *clibase.Cmd {

CheckInactiveUsersCancelFunc: dormancy.CheckInactiveUsers(ctx, options.Logger, options.Database),
}
if o.DERPServerRelayAddress != "" {
o.Options.IgnoreRedirectHostnames = append(o.Options.IgnoreRedirectHostnames, o.DERPServerRelayAddress)
}

if encKeys := options.DeploymentValues.ExternalTokenEncryptionKeys.Value(); len(encKeys) != 0 {
keys := make([][]byte, 0, len(encKeys))
Expand Down
5 changes: 2 additions & 3 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import (
"sync"
"time"

"golang.org/x/xerrors"
"tailscale.com/tailcfg"

"github.com/cenkalti/backoff/v4"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/xerrors"
"tailscale.com/tailcfg"

"cdr.dev/slog"
"github.com/coder/coder/v2/coderd"
Expand Down
5 changes: 3 additions & 2 deletions enterprise/replicasync/replicasync.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ type Options struct {
TLSConfig *tls.Config
}

// New registers the replica with the database and periodically updates to ensure
// it's healthy. It contacts all other alive replicas to ensure they are reachable.
// New registers the replica with the database and periodically updates to
// ensure it's healthy. It contacts all other alive replicas to ensure they are
// reachable.
func New(ctx context.Context, logger slog.Logger, db database.Store, ps pubsub.Pubsub, options *Options) (*Manager, error) {
if options == nil {
options = &Options{}
Expand Down