@@ -1982,6 +1982,79 @@ func TestUserLogout(t *testing.T) {
1982
1982
// - JWT with issuer https://secondary.com
1983
1983
//
1984
1984
// Without this security check disabled, all three above would have to match.
1985
+
1986
+ // TestOIDCDomainErrorMessage ensures that when a user with an unauthorized domain
1987
+ // attempts to login, the error message doesn't expose the list of authorized domains.
1988
+ func TestOIDCDomainErrorMessage (t * testing.T ) {
1989
+ t .Parallel ()
1990
+
1991
+ fake := oidctest .NewFakeIDP (t , oidctest .WithServing ())
1992
+
1993
+ 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
+ })
1998
+
1999
+ server := coderdtest .New (t , & coderdtest.Options {
2000
+ OIDCConfig : cfg ,
2001
+ })
2002
+
2003
+ // Test case 1: Email domain not in allowed list
2004
+ t .Run ("ErrorMessageOmitsDomains" , func (t * testing.T ) {
2005
+ t .Parallel ()
2006
+
2007
+ // Prepare claims with email from unauthorized domain
2008
+ claims := jwt.MapClaims {
2009
+ "email" : "user@unauthorized.com" ,
2010
+ "email_verified" : true ,
2011
+ "sub" : uuid .NewString (),
2012
+ }
2013
+
2014
+ _ , resp := fake .AttemptLogin (t , server , claims )
2015
+ defer resp .Body .Close ()
2016
+
2017
+ require .Equal (t , http .StatusForbidden , resp .StatusCode )
2018
+
2019
+ data , err := io .ReadAll (resp .Body )
2020
+ require .NoError (t , err )
2021
+
2022
+ require .Contains (t , string (data ), "is not from an authorized domain" )
2023
+ require .Contains (t , string (data ), "Please contact your administrator" )
2024
+
2025
+ for _ , domain := range allowedDomains {
2026
+ require .NotContains (t , string (data ), domain )
2027
+ }
2028
+ })
2029
+
2030
+ // Test case 2: Malformed email without @ symbol
2031
+ t .Run ("MalformedEmailErrorOmitsDomains" , func (t * testing.T ) {
2032
+ t .Parallel ()
2033
+
2034
+ // Prepare claims with an invalid email format (no @ symbol)
2035
+ claims := jwt.MapClaims {
2036
+ "email" : "invalid-email-without-domain" ,
2037
+ "email_verified" : true ,
2038
+ "sub" : uuid .NewString (),
2039
+ }
2040
+
2041
+ _ , resp := fake .AttemptLogin (t , server , claims )
2042
+ defer resp .Body .Close ()
2043
+
2044
+ require .Equal (t , http .StatusForbidden , resp .StatusCode )
2045
+
2046
+ data , err := io .ReadAll (resp .Body )
2047
+ require .NoError (t , err )
2048
+
2049
+ require .Contains (t , string (data ), "is not from an authorized domain" )
2050
+ require .Contains (t , string (data ), "Please contact your administrator" )
2051
+
2052
+ for _ , domain := range allowedDomains {
2053
+ require .NotContains (t , string (data ), domain )
2054
+ }
2055
+ })
2056
+ }
2057
+
1985
2058
func TestOIDCSkipIssuer (t * testing.T ) {
1986
2059
t .Parallel ()
1987
2060
const primaryURLString = "https://primary.com"
0 commit comments