You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ro...@apache.org on 2022/06/22 13:33:02 UTC
[couchdb] branch main updated: Prevent error:function_clause in check_security/3 if roles claim is malformed (#4070)
This is an automated email from the ASF dual-hosted git repository.
ronny pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git
The following commit(s) were added to refs/heads/main by this push:
new c417303ef Prevent error:function_clause in check_security/3 if roles claim is malformed (#4070)
c417303ef is described below
commit c417303eff34ed1dfaa3f0d45ec3ce0b7ddbdd62
Author: Ronny <ro...@apache.org>
AuthorDate: Wed Jun 22 15:32:55 2022 +0200
Prevent error:function_clause in check_security/3 if roles claim is malformed (#4070)
If the given UserRoles/Roles/Names aren't lists, ordersets:from_list/1
or lists:member will fail with an error. Prevent this with Erlang
Pattern Matching and the Robot Butt Rule [1].
Thanks @nickva
[1] https://medium.com/erlang-battleground/ode-to-the-robot-butt-bbd69e69beb2
---
src/couch/src/couch_db.erl | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/couch/src/couch_db.erl b/src/couch/src/couch_db.erl
index caf8157a0..c7f1c8b5f 100644
--- a/src/couch/src/couch_db.erl
+++ b/src/couch/src/couch_db.erl
@@ -742,18 +742,18 @@ is_authorized(#user_ctx{name = UserName, roles = UserRoles}, Security) ->
false -> check_security(names, UserName, Names)
end.
-check_security(roles, [], _) ->
- false;
-check_security(roles, UserRoles, Roles) ->
+check_security(roles, [_ | _] = UserRoles, [_ | _] = Roles) ->
UserRolesSet = ordsets:from_list(UserRoles),
RolesSet = ordsets:from_list(Roles),
not ordsets:is_disjoint(UserRolesSet, RolesSet);
-check_security(names, _, []) ->
+check_security(roles, _, _) ->
false;
check_security(names, null, _) ->
false;
-check_security(names, UserName, Names) ->
- lists:member(UserName, Names).
+check_security(names, UserName, [_ | _] = Names) ->
+ lists:member(UserName, Names);
+check_security(names, _, _) ->
+ false.
throw_security_error(#user_ctx{name = null} = UserCtx) ->
Reason = <<"You are not authorized to access this db.">>,