Skip to content

Commit 3a243c1

Browse files
fix: remove shared mutable state between oidc tests (#17179)
Spotted on main: https://github.com/coder/coder/actions/runs/14179449567/job/39721999486 ``` === FAIL: coderd TestOIDCDomainErrorMessage/MalformedEmailErrorOmitsDomains (0.01s) ================== WARNING: DATA RACE Read at 0x00c060b54e68 by goroutine 296485: golang.org/x/oauth2.(*Config).Exchange() /home/runner/go/pkg/mod/golang.org/x/oauth2@v0.28.0/oauth2.go:228 +0x1d8 github.com/coder/coder/v2/coderd.(*OIDCConfig).Exchange() <autogenerated>:1 +0xb7 github.com/coder/coder/v2/coderd.New.func11.12.1.2.ExtractOAuth2.1.1() /home/runner/work/coder/coder/coderd/httpmw/oauth2.go:168 +0x7b5 net/http.HandlerFunc.ServeHTTP() /opt/hostedtoolcache/go/1.24.1/x64/src/net/http/server.go:2294 +0x47 [...] Previous write at 0x00c060b54e68 by goroutine 55730: github.com/coder/coder/v2/coderd/coderdtest/oidctest.(*FakeIDP).SetRedirect() /home/runner/work/coder/coder/coderd/coderdtest/oidctest/idp.go:1280 +0x1e6 github.com/coder/coder/v2/coderd/coderdtest/oidctest.(*FakeIDP).LoginWithClient() /home/runner/work/coder/coder/coderd/coderdtest/oidctest/idp.go:494 +0x170 github.com/coder/coder/v2/coderd/coderdtest/oidctest.(*FakeIDP).AttemptLogin() /home/runner/work/coder/coder/coderd/coderdtest/oidctest/idp.go:479 +0x624 github.com/coder/coder/v2/coderd_test.TestOIDCDomainErrorMessage.func3() /home/runner/work/coder/coder/coderd/userauth_test.go:2041 +0x1f2 ``` As seen, this race was caused by sharing a `*oidctest.FakeIDP` between test cases. The fix is to simply do the setup twice. ``` $ go test -race -run "TestOIDCDomainErrorMessage" github.com/coder/coder/v2/coderd -count=100 ok github.com/coder/coder/v2/coderd 7.551s ````
1 parent 7d08bf0 commit 3a243c1

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

coderd/userauth_test.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -1988,30 +1988,36 @@ func TestUserLogout(t *testing.T) {
19881988
func TestOIDCDomainErrorMessage(t *testing.T) {
19891989
t.Parallel()
19901990

1991-
fake := oidctest.NewFakeIDP(t, oidctest.WithServing())
1992-
19931991
allowedDomains := []string{"allowed1.com", "allowed2.org", "company.internal"}
1994-
cfg := fake.OIDCConfig(t, nil, func(cfg *coderd.OIDCConfig) {
1995-
cfg.EmailDomain = allowedDomains
1996-
cfg.AllowSignups = true
1997-
})
19981992

1999-
server := coderdtest.New(t, &coderdtest.Options{
2000-
OIDCConfig: cfg,
2001-
})
1993+
setup := func() (*oidctest.FakeIDP, *codersdk.Client) {
1994+
fake := oidctest.NewFakeIDP(t, oidctest.WithServing())
1995+
1996+
cfg := fake.OIDCConfig(t, nil, func(cfg *coderd.OIDCConfig) {
1997+
cfg.EmailDomain = allowedDomains
1998+
cfg.AllowSignups = true
1999+
})
2000+
2001+
client := coderdtest.New(t, &coderdtest.Options{
2002+
OIDCConfig: cfg,
2003+
})
2004+
return fake, client
2005+
}
20022006

20032007
// Test case 1: Email domain not in allowed list
20042008
t.Run("ErrorMessageOmitsDomains", func(t *testing.T) {
20052009
t.Parallel()
20062010

2011+
fake, client := setup()
2012+
20072013
// Prepare claims with email from unauthorized domain
20082014
claims := jwt.MapClaims{
20092015
"email": "user@unauthorized.com",
20102016
"email_verified": true,
20112017
"sub": uuid.NewString(),
20122018
}
20132019

2014-
_, resp := fake.AttemptLogin(t, server, claims)
2020+
_, resp := fake.AttemptLogin(t, client, claims)
20152021
defer resp.Body.Close()
20162022

20172023
require.Equal(t, http.StatusForbidden, resp.StatusCode)
@@ -2031,14 +2037,16 @@ func TestOIDCDomainErrorMessage(t *testing.T) {
20312037
t.Run("MalformedEmailErrorOmitsDomains", func(t *testing.T) {
20322038
t.Parallel()
20332039

2040+
fake, client := setup()
2041+
20342042
// Prepare claims with an invalid email format (no @ symbol)
20352043
claims := jwt.MapClaims{
20362044
"email": "invalid-email-without-domain",
20372045
"email_verified": true,
20382046
"sub": uuid.NewString(),
20392047
}
20402048

2041-
_, resp := fake.AttemptLogin(t, server, claims)
2049+
_, resp := fake.AttemptLogin(t, client, claims)
20422050
defer resp.Body.Close()
20432051

20442052
require.Equal(t, http.StatusForbidden, resp.StatusCode)

0 commit comments

Comments
 (0)