| 1 |
//nolint:testpackage |
| 2 |
package oauth2 |
| 3 |
|
| 4 |
import ( |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 8 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/idtoken" |
| 9 |
oauth2types "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types" |
| 10 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
func TestResolveClientConfigNamesExpression(t *testing.T) { |
| 15 |
t.Parallel() |
| 16 |
|
| 17 |
conf := config.Defaults |
| 18 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 19 |
conf.OpenVPN.ClientConfig.Expression = `user.groups.filter(g, g in ["base", "admin"]) + [user.username]` |
| 20 |
|
| 21 |
client := Client{conf: &conf} |
| 22 |
require.NoError(t, client.initializeClientConfigResolver()) |
| 23 |
|
| 24 |
names, err := client.ResolveClientConfigNames( |
| 25 |
CELAuthModeInteractive, |
| 26 |
state.State{Client: state.ClientIdentifier{CommonName: "client-cn"}}, |
| 27 |
&idtoken.IDToken{IDTokenClaims: &idtoken.Claims{Claims: map[string]any{}}}, |
| 28 |
oauth2types.UserInfo{ |
| 29 |
Username: "alice", |
| 30 |
Groups: []string{"base", "ignored", "admin"}, |
| 31 |
}, |
| 32 |
) |
| 33 |
|
| 34 |
require.NoError(t, err) |
| 35 |
require.Equal(t, []string{"base", "admin", "alice"}, names) |
| 36 |
} |
| 37 |
|
| 38 |
func TestResolveClientConfigNamesExpressionUsesCommonNameAndUsername(t *testing.T) { |
| 39 |
t.Parallel() |
| 40 |
|
| 41 |
conf := config.Defaults |
| 42 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 43 |
conf.OpenVPN.ClientConfig.Expression = `[openvpn.commonName, user.username]` |
| 44 |
|
| 45 |
client := Client{conf: &conf} |
| 46 |
require.NoError(t, client.initializeClientConfigResolver()) |
| 47 |
|
| 48 |
names, err := client.ResolveClientConfigNames( |
| 49 |
CELAuthModeInteractive, |
| 50 |
state.State{Client: state.ClientIdentifier{CommonName: "client-cn"}}, |
| 51 |
&idtoken.IDToken{IDTokenClaims: &idtoken.Claims{Claims: map[string]any{}}}, |
| 52 |
oauth2types.UserInfo{Username: "alice"}, |
| 53 |
) |
| 54 |
|
| 55 |
require.NoError(t, err) |
| 56 |
require.Equal(t, []string{"client-cn", "alice"}, names) |
| 57 |
} |
| 58 |
|
| 59 |
func TestResolveClientConfigNamesExpressionRejectsInvalidName(t *testing.T) { |
| 60 |
t.Parallel() |
| 61 |
|
| 62 |
conf := config.Defaults |
| 63 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 64 |
conf.OpenVPN.ClientConfig.Expression = `["../admin"]` |
| 65 |
|
| 66 |
client := Client{conf: &conf} |
| 67 |
require.NoError(t, client.initializeClientConfigResolver()) |
| 68 |
|
| 69 |
_, err := client.ResolveClientConfigNames( |
| 70 |
CELAuthModeInteractive, |
| 71 |
state.State{Client: state.ClientIdentifier{CommonName: "client-cn"}}, |
| 72 |
&idtoken.IDToken{IDTokenClaims: &idtoken.Claims{Claims: map[string]any{}}}, |
| 73 |
oauth2types.UserInfo{Username: "alice"}, |
| 74 |
) |
| 75 |
|
| 76 |
require.Error(t, err) |
| 77 |
require.Contains(t, err.Error(), "invalid client config path") |
| 78 |
} |
| 79 |
|
| 80 |
func TestResolveClientConfigNamesExpressionRejectsUnsafeName(t *testing.T) { |
| 81 |
t.Parallel() |
| 82 |
|
| 83 |
conf := config.Defaults |
| 84 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 85 |
conf.OpenVPN.ClientConfig.Expression = `["profile\" autofocus onfocus=alert(1)"]` |
| 86 |
|
| 87 |
client := Client{conf: &conf} |
| 88 |
require.NoError(t, client.initializeClientConfigResolver()) |
| 89 |
|
| 90 |
_, err := client.ResolveClientConfigNames( |
| 91 |
CELAuthModeInteractive, |
| 92 |
state.State{Client: state.ClientIdentifier{CommonName: "client-cn"}}, |
| 93 |
&idtoken.IDToken{IDTokenClaims: &idtoken.Claims{Claims: map[string]any{}}}, |
| 94 |
oauth2types.UserInfo{Username: "alice"}, |
| 95 |
) |
| 96 |
|
| 97 |
require.Error(t, err) |
| 98 |
require.Contains(t, err.Error(), "unsafe client config name") |
| 99 |
} |
| 100 |
|
| 101 |
func TestInitializeClientConfigResolverRejectsScalarExpression(t *testing.T) { |
| 102 |
t.Parallel() |
| 103 |
|
| 104 |
conf := config.Defaults |
| 105 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 106 |
conf.OpenVPN.ClientConfig.Expression = `"base"` |
| 107 |
|
| 108 |
client := Client{conf: &conf} |
| 109 |
|
| 110 |
err := client.initializeClientConfigResolver() |
| 111 |
|
| 112 |
require.Error(t, err) |
| 113 |
require.Contains(t, err.Error(), "must evaluate to list(string), got string") |
| 114 |
} |
| 115 |
|
| 116 |
func TestInitializeClientConfigResolverRejectsNonStringListExpression(t *testing.T) { |
| 117 |
t.Parallel() |
| 118 |
|
| 119 |
conf := config.Defaults |
| 120 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 121 |
conf.OpenVPN.ClientConfig.Expression = `[1]` |
| 122 |
|
| 123 |
client := Client{conf: &conf} |
| 124 |
|
| 125 |
err := client.initializeClientConfigResolver() |
| 126 |
|
| 127 |
require.Error(t, err) |
| 128 |
require.Contains(t, err.Error(), "must evaluate to list(string), got list(int)") |
| 129 |
} |
| 130 |
|
| 131 |
func TestResolveClientConfigNamesExpressionRejectsMissingClaim(t *testing.T) { |
| 132 |
t.Parallel() |
| 133 |
|
| 134 |
conf := config.Defaults |
| 135 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 136 |
conf.OpenVPN.ClientConfig.Expression = `token.claims.groups` |
| 137 |
|
| 138 |
client := Client{conf: &conf} |
| 139 |
require.NoError(t, client.initializeClientConfigResolver()) |
| 140 |
|
| 141 |
_, err := client.ResolveClientConfigNames( |
| 142 |
CELAuthModeInteractive, |
| 143 |
state.State{Client: state.ClientIdentifier{CommonName: "client-cn"}}, |
| 144 |
&idtoken.IDToken{IDTokenClaims: &idtoken.Claims{Claims: map[string]any{}}}, |
| 145 |
oauth2types.UserInfo{Username: "alice"}, |
| 146 |
) |
| 147 |
|
| 148 |
require.Error(t, err) |
| 149 |
} |
| 150 |
|
| 151 |
func TestResolveClientConfigNamesRequiresExpression(t *testing.T) { |
| 152 |
t.Parallel() |
| 153 |
|
| 154 |
conf := config.Defaults |
| 155 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 156 |
|
| 157 |
client := Client{conf: &conf} |
| 158 |
|
| 159 |
_, err := client.ResolveClientConfigNames( |
| 160 |
CELAuthModeInteractive, |
| 161 |
state.State{Client: state.ClientIdentifier{CommonName: "client-cn"}}, |
| 162 |
&idtoken.IDToken{IDTokenClaims: &idtoken.Claims{Claims: map[string]any{}}}, |
| 163 |
oauth2types.UserInfo{Username: "alice"}, |
| 164 |
) |
| 165 |
|
| 166 |
require.Error(t, err) |
| 167 |
require.Contains(t, err.Error(), "client config expression is not configured") |
| 168 |
} |