Skip to content

Commit 64f5bba

Browse files
committed
Allow the stable scope in the policy
1 parent a19bea8 commit 64f5bba

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

policies/authorization_grant/authorization_grant.rego

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,32 @@ allowed_scope(scope) if {
6767
regex.match(`^urn:matrix:org.matrix.msc2967.client:device:[A-Za-z0-9._~!$&'()*+,;=:@/-]{10,}$`, scope)
6868
}
6969

70+
allowed_scope(scope) if {
71+
# Grant access to the C-S API only if there is a user
72+
interactive_grant_type(input.grant_type)
73+
regex.match(`^urn:matrix:client:device:[A-Za-z0-9._~!$&'()*+,;=:@/-]{10,}$`, scope)
74+
}
75+
76+
allowed_scope("urn:matrix:client:api:*") if {
77+
# Grant access to the C-S API only if there is a user
78+
interactive_grant_type(input.grant_type)
79+
}
80+
7081
allowed_scope("urn:matrix:org.matrix.msc2967.client:api:*") if {
7182
# Grant access to the C-S API only if there is a user
7283
interactive_grant_type(input.grant_type)
7384
}
7485

86+
uses_unstable_scopes if {
87+
scope_list := split(input.scope, " ")
88+
count({scope | some scope in scope_list; startswith(scope, "urn:matrix:org.matrix.msc2967.client:")}) > 0
89+
}
90+
91+
uses_stable_scopes if {
92+
scope_list := split(input.scope, " ")
93+
count({scope | some scope in scope_list; startswith(scope, "urn:matrix:client:")}) > 0
94+
}
95+
7596
# METADATA
7697
# entrypoint: true
7798
violation contains {"msg": msg} if {
@@ -85,6 +106,16 @@ violation contains {"msg": "only one device scope is allowed at a time"} if {
85106
count({scope | some scope in scope_list; startswith(scope, "urn:matrix:org.matrix.msc2967.client:device:")}) > 1
86107
}
87108

109+
violation contains {"msg": "only one device scope is allowed at a time"} if {
110+
scope_list := split(input.scope, " ")
111+
count({scope | some scope in scope_list; startswith(scope, "urn:matrix:client:device:")}) > 1
112+
}
113+
114+
violation contains {"msg": "request cannot mix unstable and stable scopes"} if {
115+
uses_stable_scopes
116+
uses_unstable_scopes
117+
}
118+
88119
violation contains {"msg": sprintf(
89120
"Requester [%s] isn't allowed to do this action",
90121
[common.format_requester(input.requester)],

policies/authorization_grant/authorization_grant_test.rego

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test_standard_scopes if {
3535
with input.scope as "profile"
3636
}
3737

38-
test_matrix_scopes if {
38+
test_matrix_unstable_scopes if {
3939
authorization_grant.allow with input.user as user
4040
with input.client as client
4141
with input.grant_type as "authorization_code"
@@ -52,7 +52,24 @@ test_matrix_scopes if {
5252
with input.scope as "urn:matrix:org.matrix.msc2967.client:api:*"
5353
}
5454

55-
test_device_scopes if {
55+
test_matrix_stable_scopes if {
56+
authorization_grant.allow with input.user as user
57+
with input.client as client
58+
with input.grant_type as "authorization_code"
59+
with input.scope as "urn:matrix:client:api:*"
60+
61+
authorization_grant.allow with input.user as user
62+
with input.client as client
63+
with input.grant_type as "urn:ietf:params:oauth:grant-type:device_code"
64+
with input.scope as "urn:matrix:client:api:*"
65+
66+
not authorization_grant.allow with input.user as user
67+
with input.client as client
68+
with input.grant_type as "client_credentials"
69+
with input.scope as "urn:matrix:client:api:*"
70+
}
71+
72+
test_unstable_device_scopes if {
5673
authorization_grant.allow with input.user as user
5774
with input.client as client
5875
with input.grant_type as "authorization_code"
@@ -87,6 +104,58 @@ test_device_scopes if {
87104
with input.scope as "urn:matrix:org.matrix.msc2967.client:device:AAbbCCdd01"
88105
}
89106

107+
test_stable_device_scopes if {
108+
authorization_grant.allow with input.user as user
109+
with input.client as client
110+
with input.grant_type as "authorization_code"
111+
with input.scope as "urn:matrix:client:device:AAbbCCdd01"
112+
113+
authorization_grant.allow with input.user as user
114+
with input.client as client
115+
with input.grant_type as "authorization_code"
116+
with input.scope as "urn:matrix:client:device:AAbbCCdd01-asdasdsa1-2313"
117+
118+
# Too short
119+
not authorization_grant.allow with input.user as user
120+
with input.client as client
121+
with input.grant_type as "authorization_code"
122+
with input.scope as "urn:matrix:client:device:abcd"
123+
124+
# Multiple device scope
125+
not authorization_grant.allow with input.user as user
126+
with input.client as client
127+
with input.grant_type as "authorization_code"
128+
with input.scope as "urn:matrix:client:device:AAbbCCdd01 urn:matrix:client:device:AAbbCCdd02"
129+
130+
# Allowed with the device code grant
131+
authorization_grant.allow with input.user as user
132+
with input.client as client
133+
with input.grant_type as "urn:ietf:params:oauth:grant-type:device_code"
134+
with input.scope as "urn:matrix:client:device:AAbbCCdd01"
135+
136+
# Not authorization_grant.allowed for the client credentials grant
137+
not authorization_grant.allow with input.client as client
138+
with input.grant_type as "client_credentials"
139+
with input.scope as "urn:matrix:client:device:AAbbCCdd01"
140+
}
141+
142+
test_mix_stable_and_unstable_scopes if {
143+
not authorization_grant.allow with input.user as user
144+
with input.client as client
145+
with input.grant_type as "authorization_code"
146+
with input.scope as "urn:matrix:org.matrix.msc2967.client:api:* urn:matrix:client:device:AAbbCCdd01"
147+
148+
not authorization_grant.allow with input.user as user
149+
with input.client as client
150+
with input.grant_type as "authorization_code"
151+
with input.scope as "urn:matrix:client:api:* urn:matrix:org.matrix.msc2967.client:device:AAbbCCdd01"
152+
153+
not authorization_grant.allow with input.user as user
154+
with input.client as client
155+
with input.grant_type as "authorization_code"
156+
with input.scope as "urn:matrix:client:api:* urn:matrix:org.matrix.msc2967.client:api:*"
157+
}
158+
90159
test_synapse_admin_scopes if {
91160
some grant_type in ["authorization_code", "urn:ietf:params:oauth:grant-type:device_code"]
92161

0 commit comments

Comments
 (0)