fix(security): require setup token for first user on tunnels Prevent unauthorized users from claiming Owner access on public `try.coder.app` tunnels before initial setup. - Generate a setup token at startup when using a tunnel with no existing users. - Require the token for first-user creation via the API, web UI, and CLI. - Block GitHub/OIDC first-user signup until setup is complete. - Support custom tokens via `--setup-token` / `CODER_SETUP_TOKEN` and non-interactive CLI setup. - Update development scripts, documentation, and tests. - Preserve existing behavior for deployments with an access URL or existing users. Validated with automated tests and 23/23 manual scenarios using a real tunnel. Refs CORE-784
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package coderd
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"crypto/subtle"
|
|
"net/http"
|
|
|
|
"github.com/coder/coder/v2/coderd/idpsync"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// setupTokenRequired reports whether the first user can only be
|
|
// created with the setup token.
|
|
func (api *API) setupTokenRequired() bool {
|
|
return api.SetupToken != ""
|
|
}
|
|
|
|
// setupTokenMatches compares digests so that the time taken does not
|
|
// depend on the length of got or on where it first differs.
|
|
func (api *API) setupTokenMatches(got string) bool {
|
|
want := sha256.Sum256([]byte(api.SetupToken))
|
|
have := sha256.Sum256([]byte(got))
|
|
return subtle.ConstantTimeCompare(want[:], have[:]) == 1
|
|
}
|
|
|
|
// setupTokenRefused is the one response for a missing or wrong token,
|
|
// so a caller cannot learn how the token was wrong.
|
|
func setupTokenRefused() codersdk.Response {
|
|
return codersdk.Response{
|
|
Message: "A setup token is required to create the first user. Find it in the output of coder server.",
|
|
Validations: []codersdk.ValidationError{{
|
|
Field: "setup_token",
|
|
Detail: "Missing or incorrect setup token.",
|
|
}},
|
|
}
|
|
}
|
|
|
|
// errSetupNotComplete refuses an OAuth sign-up of the first user.
|
|
func errSetupNotComplete() *idpsync.HTTPError {
|
|
return &idpsync.HTTPError{
|
|
Code: http.StatusForbidden,
|
|
Msg: "Setup is not complete",
|
|
Detail: "Create the first user with the setup token from the output of coder server. You can sign in after that.",
|
|
RenderStaticPage: true,
|
|
}
|
|
}
|