1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=4 sw=4 et tw=99:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
18 : * May 28, 2008.
19 : *
20 : * The Initial Developer of the Original Code is
21 : * Mozilla Foundation
22 : * Portions created by the Initial Developer are Copyright (C) 2010
23 : * the Initial Developer. All Rights Reserved.
24 : *
25 : * Contributor(s):
26 : * Andreas Gal <gal@mozilla.com>
27 : *
28 : * Alternatively, the contents of this file may be used under the terms of
29 : * either of the GNU General Public License Version 2 or later (the "GPL"),
30 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 : * in which case the provisions of the GPL or the LGPL are applicable instead
32 : * of those above. If you wish to allow use of your version of this file only
33 : * under the terms of either the GPL or the LGPL, and not to allow others to
34 : * use your version of this file under the terms of the MPL, indicate your
35 : * decision by deleting the provisions above and replace them with the notice
36 : * and other provisions required by the GPL or the LGPL. If you do not delete
37 : * the provisions above, a recipient may use your version of this file under
38 : * the terms of any one of the MPL, the GPL or the LGPL.
39 : *
40 : * ***** END LICENSE BLOCK ***** */
41 :
42 : #ifndef jswrapper_h___
43 : #define jswrapper_h___
44 :
45 : #include "mozilla/Attributes.h"
46 :
47 : #include "jsapi.h"
48 : #include "jsproxy.h"
49 :
50 : namespace js {
51 :
52 : class DummyFrameGuard;
53 :
54 : /* No-op wrapper handler base class. */
55 : class JS_FRIEND_API(Wrapper) : public ProxyHandler
56 : {
57 : unsigned mFlags;
58 : public:
59 107255 : unsigned flags() const { return mFlags; }
60 :
61 : explicit Wrapper(unsigned flags);
62 :
63 : typedef enum { PermitObjectAccess, PermitPropertyAccess, DenyAccess } Permission;
64 :
65 : virtual ~Wrapper();
66 :
67 : /* ES5 Harmony fundamental wrapper traps. */
68 : virtual bool getPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id, bool set,
69 : PropertyDescriptor *desc) MOZ_OVERRIDE;
70 : virtual bool getOwnPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id, bool set,
71 : PropertyDescriptor *desc) MOZ_OVERRIDE;
72 : virtual bool defineProperty(JSContext *cx, JSObject *wrapper, jsid id,
73 : PropertyDescriptor *desc) MOZ_OVERRIDE;
74 : virtual bool getOwnPropertyNames(JSContext *cx, JSObject *wrapper, AutoIdVector &props) MOZ_OVERRIDE;
75 : virtual bool delete_(JSContext *cx, JSObject *wrapper, jsid id, bool *bp) MOZ_OVERRIDE;
76 : virtual bool enumerate(JSContext *cx, JSObject *wrapper, AutoIdVector &props) MOZ_OVERRIDE;
77 : virtual bool fix(JSContext *cx, JSObject *wrapper, Value *vp) MOZ_OVERRIDE;
78 :
79 : /* ES5 Harmony derived wrapper traps. */
80 : virtual bool has(JSContext *cx, JSObject *wrapper, jsid id, bool *bp) MOZ_OVERRIDE;
81 : virtual bool hasOwn(JSContext *cx, JSObject *wrapper, jsid id, bool *bp) MOZ_OVERRIDE;
82 : virtual bool get(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id, Value *vp) MOZ_OVERRIDE;
83 : virtual bool set(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id, bool strict,
84 : Value *vp) MOZ_OVERRIDE;
85 : virtual bool keys(JSContext *cx, JSObject *wrapper, AutoIdVector &props) MOZ_OVERRIDE;
86 : virtual bool iterate(JSContext *cx, JSObject *wrapper, unsigned flags, Value *vp) MOZ_OVERRIDE;
87 :
88 : /* Spidermonkey extensions. */
89 : virtual bool call(JSContext *cx, JSObject *wrapper, unsigned argc, Value *vp) MOZ_OVERRIDE;
90 : virtual bool construct(JSContext *cx, JSObject *wrapper, unsigned argc, Value *argv, Value *rval) MOZ_OVERRIDE;
91 : virtual bool nativeCall(JSContext *cx, JSObject *wrapper, Class *clasp, Native native, CallArgs args) MOZ_OVERRIDE;
92 : virtual bool hasInstance(JSContext *cx, JSObject *wrapper, const Value *vp, bool *bp) MOZ_OVERRIDE;
93 : virtual JSType typeOf(JSContext *cx, JSObject *proxy) MOZ_OVERRIDE;
94 : virtual bool objectClassIs(JSObject *obj, ESClassValue classValue, JSContext *cx) MOZ_OVERRIDE;
95 : virtual JSString *obj_toString(JSContext *cx, JSObject *wrapper) MOZ_OVERRIDE;
96 : virtual JSString *fun_toString(JSContext *cx, JSObject *wrapper, unsigned indent) MOZ_OVERRIDE;
97 : virtual bool regexp_toShared(JSContext *cx, JSObject *proxy, RegExpGuard *g) MOZ_OVERRIDE;
98 : virtual bool defaultValue(JSContext *cx, JSObject *wrapper, JSType hint, Value *vp) MOZ_OVERRIDE;
99 : virtual bool iteratorNext(JSContext *cx, JSObject *wrapper, Value *vp) MOZ_OVERRIDE;
100 :
101 : virtual void trace(JSTracer *trc, JSObject *wrapper) MOZ_OVERRIDE;
102 :
103 : /* Policy enforcement traps. */
104 : enum Action { GET, SET, CALL };
105 : virtual bool enter(JSContext *cx, JSObject *wrapper, jsid id, Action act, bool *bp);
106 : virtual void leave(JSContext *cx, JSObject *wrapper);
107 :
108 : static Wrapper singleton;
109 :
110 : static JSObject *New(JSContext *cx, JSObject *obj, JSObject *proto, JSObject *parent,
111 : Wrapper *handler);
112 :
113 : static JSObject *wrappedObject(const JSObject *wrapper);
114 : static Wrapper *wrapperHandler(const JSObject *wrapper);
115 :
116 : enum {
117 : CROSS_COMPARTMENT = 1 << 0,
118 : LAST_USED_FLAG = CROSS_COMPARTMENT
119 : };
120 :
121 : static void *getWrapperFamily();
122 : };
123 :
124 : /* Base class for all cross compartment wrapper handlers. */
125 : class JS_FRIEND_API(CrossCompartmentWrapper) : public Wrapper
126 : {
127 : public:
128 : CrossCompartmentWrapper(unsigned flags);
129 :
130 : virtual ~CrossCompartmentWrapper();
131 :
132 : /* ES5 Harmony fundamental wrapper traps. */
133 : virtual bool getPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id, bool set,
134 : PropertyDescriptor *desc) MOZ_OVERRIDE;
135 : virtual bool getOwnPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id, bool set,
136 : PropertyDescriptor *desc) MOZ_OVERRIDE;
137 : virtual bool defineProperty(JSContext *cx, JSObject *wrapper, jsid id,
138 : PropertyDescriptor *desc) MOZ_OVERRIDE;
139 : virtual bool getOwnPropertyNames(JSContext *cx, JSObject *wrapper, AutoIdVector &props) MOZ_OVERRIDE;
140 : virtual bool delete_(JSContext *cx, JSObject *wrapper, jsid id, bool *bp) MOZ_OVERRIDE;
141 : virtual bool enumerate(JSContext *cx, JSObject *wrapper, AutoIdVector &props) MOZ_OVERRIDE;
142 :
143 : /* ES5 Harmony derived wrapper traps. */
144 : virtual bool has(JSContext *cx, JSObject *wrapper, jsid id, bool *bp) MOZ_OVERRIDE;
145 : virtual bool hasOwn(JSContext *cx, JSObject *wrapper, jsid id, bool *bp) MOZ_OVERRIDE;
146 : virtual bool get(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id, Value *vp) MOZ_OVERRIDE;
147 : virtual bool set(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id, bool strict,
148 : Value *vp) MOZ_OVERRIDE;
149 : virtual bool keys(JSContext *cx, JSObject *wrapper, AutoIdVector &props) MOZ_OVERRIDE;
150 : virtual bool iterate(JSContext *cx, JSObject *wrapper, unsigned flags, Value *vp) MOZ_OVERRIDE;
151 :
152 : /* Spidermonkey extensions. */
153 : virtual bool call(JSContext *cx, JSObject *wrapper, unsigned argc, Value *vp) MOZ_OVERRIDE;
154 : virtual bool construct(JSContext *cx, JSObject *wrapper, unsigned argc, Value *argv, Value *rval) MOZ_OVERRIDE;
155 : virtual bool nativeCall(JSContext *cx, JSObject *wrapper, Class *clasp, Native native, CallArgs args) MOZ_OVERRIDE;
156 : virtual bool hasInstance(JSContext *cx, JSObject *wrapper, const Value *vp, bool *bp) MOZ_OVERRIDE;
157 : virtual JSString *obj_toString(JSContext *cx, JSObject *wrapper) MOZ_OVERRIDE;
158 : virtual JSString *fun_toString(JSContext *cx, JSObject *wrapper, unsigned indent) MOZ_OVERRIDE;
159 : virtual bool defaultValue(JSContext *cx, JSObject *wrapper, JSType hint, Value *vp) MOZ_OVERRIDE;
160 : virtual bool iteratorNext(JSContext *cx, JSObject *wrapper, Value *vp);
161 :
162 : virtual void trace(JSTracer *trc, JSObject *wrapper) MOZ_OVERRIDE;
163 :
164 : static CrossCompartmentWrapper singleton;
165 : };
166 :
167 : /*
168 : * Base class for security wrappers. A security wrapper is potentially hiding
169 : * all or part of some wrapped object thus SecurityWrapper defaults to denying
170 : * access to the wrappee. This is the opposite of Wrapper which tries to be
171 : * completely transparent.
172 : *
173 : * NB: Currently, only a few ProxyHandler operations are overridden to deny
174 : * access, relying on derived SecurityWrapper to block access when necessary.
175 : */
176 : template <class Base>
177 : class JS_FRIEND_API(SecurityWrapper) : public Base
178 0 : {
179 : public:
180 : SecurityWrapper(unsigned flags);
181 :
182 : virtual bool nativeCall(JSContext *cx, JSObject *wrapper, Class *clasp, Native native, CallArgs args) MOZ_OVERRIDE;
183 : virtual bool objectClassIs(JSObject *obj, ESClassValue classValue, JSContext *cx) MOZ_OVERRIDE;
184 : virtual bool regexp_toShared(JSContext *cx, JSObject *proxy, RegExpGuard *g) MOZ_OVERRIDE;
185 : };
186 :
187 : typedef SecurityWrapper<Wrapper> SameCompartmentSecurityWrapper;
188 : typedef SecurityWrapper<CrossCompartmentWrapper> CrossCompartmentSecurityWrapper;
189 :
190 : /*
191 : * A hacky class that lets a friend force a fake frame. We must already be
192 : * in the compartment of |target| when we enter the forced frame.
193 : */
194 : class JS_FRIEND_API(ForceFrame)
195 : {
196 : public:
197 : JSContext * const context;
198 : JSObject * const target;
199 : private:
200 : DummyFrameGuard *frame;
201 :
202 : public:
203 : ForceFrame(JSContext *cx, JSObject *target);
204 : ~ForceFrame();
205 : bool enter();
206 : };
207 :
208 : extern JSObject *
209 : TransparentObjectWrapper(JSContext *cx, JSObject *obj, JSObject *wrappedProto, JSObject *parent,
210 : unsigned flags);
211 :
212 : // Proxy family for wrappers. Public so that IsWrapper() can be fully inlined by
213 : // jsfriendapi users.
214 : extern JS_FRIEND_DATA(int) sWrapperFamily;
215 :
216 : inline bool
217 271290 : IsWrapper(const JSObject *obj)
218 : {
219 271290 : return IsProxy(obj) && GetProxyHandler(obj)->family() == &sWrapperFamily;
220 : }
221 :
222 : // Given a JSObject, returns that object stripped of wrappers. If
223 : // stopAtOuter is true, then this returns the outer window if it was
224 : // previously wrapped. Otherwise, this returns the first object for
225 : // which JSObject::isWrapper returns false.
226 : JS_FRIEND_API(JSObject *) UnwrapObject(JSObject *obj, bool stopAtOuter = true,
227 : unsigned *flagsp = NULL);
228 :
229 : bool IsCrossCompartmentWrapper(const JSObject *obj);
230 :
231 : } /* namespace js */
232 :
233 : #endif
|