123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020 Nomadic-Labs *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)(* Matchers *)typechunk_matcher=Literalofstring|Wildcardtypepath_matcher=|Exactofchunk_matcherlist|FollowedByAnySuffixofchunk_matcherlisttypemeth_matcher=ExactofResto.meth|Anytypematcher={meth:meth_matcher;path:path_matcher}(* matchers parsing *)letparse_meth_matchers=matchs.[0]with|'/'|' '->(Any,0)|_->ifString.lengths>3&&String.subs03="GET"then(Exact`GET,3)elseifString.lengths>3&&String.subs03="PUT"then(Exact`PUT,3)elseifString.lengths>4&&String.subs04="POST"then(Exact`POST,4)elseifString.lengths>5&&String.subs05="PATCH"then(Exact`PATCH,5)elseifString.lengths>6&&String.subs06="DELETE"then(Exact`DELETE,6)elseraise(Invalid_argument"Resto.Acl.parse: invalid method matcher")letignore_whitespacesoffset=matchString.index_from_optsoffset'/'with|None->raise(Invalid_argument"Resto.Acl.parse: invalid method matcher")|Somefirst_slash->assert(first_slash>=offset);iffirst_slash>offsetthenfori=offsettofirst_slash-1doifs.[i]<>' 'thenraise(Invalid_argument"Resto.Acl.parse: invalid method matcher")done;first_slashletadd_chunk(matcher:path_matcher)(chunk:string)=matchmatcherwith|FollowedByAnySuffix_->raise(Invalid_argument"Resto.Acl.parse: double-star can only appear in suffix position")|Exactf->(matchchunkwith|"**"->FollowedByAnySuffixf|"*"->Exact(Wildcard::f)|literal->String.iter(function|'/'->assertfalse|('*'|'?'|'&'|'#'|'=')asc->Format.kasprintfinvalid_arg"Resto.Acl.parse: %c must be percent-encoded"c|_->())literal;letdecoded_literal=Uri.pct_decodeliteralinExact(Literaldecoded_literal::f))letparse_pathsoffset=String.subsoffset(String.lengths-offset)|>Resto.Utils.split_path|>List.fold_leftadd_chunk(Exact[])|>function|FollowedByAnySuffixm->FollowedByAnySuffix(List.revm)|Exactm->Exact(List.revm)letparse:string->matcher=funs->ifString.lengths=0thenraise(Invalid_argument"Resto.Acl.parse: a filter cannot be empty")elseletmeth,offset=parse_meth_matchersinletoffset=ignore_whitespacesoffsetinletpath=parse_pathsoffsetin{meth;path}(* Serialising *)letto_string_meth=function|Any->""|Exact`GET->"GET"|Exact`PUT->"PUT"|Exact`POST->"POST"|Exact`PATCH->"PATCH"|Exact`DELETE->"DELETE"letescaped_asterisk_seq=String.to_seq"%2A"letto_string_chunk=function|Wildcard->"*"|Literall->lets=Uri.pct_encodelinifString.containss'*'then(* slow path *)String.of_seq(Seq.flat_map(function'*'->escaped_asterisk_seq|c->Seq.returnc)(String.to_seqs))elsesletto_string_chunk_listl="/"^String.concat"/"(List.mapto_string_chunkl)letto_string_path=function|FollowedByAnySuffixl->to_string_chunk_listl^"/**"|Exactl->to_string_chunk_listlletto_string{meth;path}=to_string_methmeth^to_string_pathpath(* Matching paths *)letrecmatches_pathany_suffix_ofmatcherpath=match(matcher,path)with|[],[]->true|[],_::_->any_suffix_of|_::_,[]->false|Wildcard::matcher,_::path->matches_pathany_suffix_ofmatcherpath|Literallit::matcher,chunk::path->String.equallitchunk&&matches_pathany_suffix_ofmatcherpathletmatches_pathpathmatcher=matchmatcherwith|FollowedByAnySuffixmatcher->matches_pathtruematcherpath|Exactmatcher->matches_pathfalsematcherpathletmatches_meth(meth:[<Resto.meth])=function|Any->true|Exactm->m=methletmatches_matchermeth_path_{meth;path}=matches_methmeth_meth&&matches_pathpath_pathletmatches_any_matchermethpathmatchers=List.exists(matches_matchermethpath)matchers(* ACL policy and implementation *)typet=|Allow_allof{except:matcherlist}|Deny_allof{except:matcherlist}letallowedpolicy~meth~path=matchpolicywith|Deny_all{except=[]}->(* All paths are blocked, no exceptions *)false|Allow_all{except=[]}->(* All paths are allowed, no exceptions *)true|Deny_all{except=matchers}->(* default: Deny, matches: Allow *)matches_any_matchermethpathmatchers|Allow_all{except=matchers}->(* default: Allow, matches: Deny *)not@@matches_any_matchermethpathmatchers