1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2017, VU University Amsterdam 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module(pengines_sandbox, []). 36:- use_module(library(pengines), []). 37:- autoload(library(error),[instantiation_error/1]). 38:- autoload(library(lists),[append/3]). 39 40/** <module> Declare Pengine interaction sandbox-safe 41 42This module can be loaded alongside library(pengines) to allow for 43calling remote pengines from a sandboxed environment. This is disallowed 44by default because one of the use-cases of sandboxed pengines is to 45provide a generic application interface. In such cases you do not want 46the application to act as a proxy, in particular not to other systems in 47a protected network. 48*/ 49 50 /******************************* 51 * PENGINE SANDBOX SUPPORT * 52 *******************************/ 53 54:- multifile 55 sandbox:safe_primitive/1, % Goal 56 sandbox:safe_meta/2. % Goal, Calls 57 58%! sandbox:safe_primitive(+Goal) is semidet. 59% 60% Declare the core pengine operations as safe. If we are talking 61% about local pengines, their safety is guaranteed by the 62% sandboxing done for all pengines. 63% 64% @tbd If at some point we allow for `unsafe' pengines, we must 65% reconsider this. 66 67sandbox:safe_primitive(pengines:pengine_destroy(_,_)). 68sandbox:safe_primitive(pengines:pengine_event(_, _)). 69sandbox:safe_primitive(pengines:pengine_send(_, _, _)). 70sandbox:safe_primitive(pengines:pengine_ask(_, _, _)). 71sandbox:safe_primitive(pengines:pengine_pull_response(_,_)). 72sandbox:safe_primitive(pengines:pengine_user(_)). 73sandbox:safe_primitive(system:'#file'(_,_)). 74 75%! sandbox:safe_meta(+Goal, -Called) is semidet. 76% 77% Declare the pengine meta-predicates as safe. Note that the 78% pengine calling predicates are safe because the safety is 79% guaranteed by the recieving pengine. 80 81sandbox:safe_meta(pengines:pengine_create(_), []). 82sandbox:safe_meta(pengines:pengine_rpc(_, _, _), []). 83sandbox:safe_meta(pengines:pengine_event_loop(_,Closure,_,_), [Closure1]) :- 84 extend_goal(Closure, [_], Closure1). 85 86extend_goal(Var, _, _) :- 87 var(Var), 88 !, 89 instantiation_error(Var). 90extend_goal(M:Term0, Extra, M:Term) :- 91 extend_goal(Term0, Extra, Term). 92extend_goal(Atom, Extra, Goal) :- 93 atom(Atom), 94 !, 95 Goal =.. [Atom|Extra]. 96extend_goal(Compound, Extra, Goal) :- 97 compound(Compound), 98 !, 99 compound_name_arguments(Compound, Name, Args0), 100 append(Args0, Extra, Args), 101 compound_name_arguments(Goal, Name, Args)