Skip to content

Commit f404ee6

Browse files
committed
disallow polyglot and host access functions is the context disallows it
1 parent 82d6fdf commit f404ee6

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/JavaModuleBuiltins.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ abstract static class AddToClassPathNode extends PythonBuiltinNode {
118118
PNone add(VirtualFrame frame, Object[] args,
119119
@Cached CastToStringNode castToString) {
120120
Env env = getContext().getEnv();
121+
if (!env.isHostLookupAllowed()) {
122+
throw raise(PythonErrorType.NotImplementedError, "host access is not allowed");
123+
}
121124
for (Object arg : args) {
122125
String entry = castToString.execute(frame, arg);
123126
try {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PolyglotModuleBuiltins.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ public abstract static class ImportNode extends PythonBuiltinNode {
119119
@Specialization
120120
@TruffleBoundary
121121
public Object importSymbol(String name) {
122-
Object object = getContext().getEnv().importSymbol(name);
122+
Env env = getContext().getEnv();
123+
if (!env.isPolyglotAccessAllowed()) {
124+
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
125+
}
126+
Object object = env.importSymbol(name);
123127
if (object == null) {
124128
return PNone.NONE;
125129
}
@@ -134,6 +138,9 @@ abstract static class EvalInteropNode extends PythonBuiltinNode {
134138
@Specialization
135139
Object evalString(@SuppressWarnings("unused") PNone path, String value, String langOrMimeType) {
136140
Env env = getContext().getEnv();
141+
if (!env.isPolyglotAccessAllowed()) {
142+
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
143+
}
137144
try {
138145
boolean mimeType = isMimeType(langOrMimeType);
139146
String lang = mimeType ? findLanguageByMimeType(env, langOrMimeType) : langOrMimeType;
@@ -159,6 +166,9 @@ private void raiseIfInternal(Env env, String lang) {
159166
@Specialization
160167
Object evalFile(String path, @SuppressWarnings("unused") PNone string, String langOrMimeType) {
161168
Env env = getContext().getEnv();
169+
if (!env.isPolyglotAccessAllowed()) {
170+
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
171+
}
162172
try {
163173
boolean mimeType = isMimeType(langOrMimeType);
164174
String lang = mimeType ? findLanguageByMimeType(env, langOrMimeType) : langOrMimeType;
@@ -179,6 +189,9 @@ Object evalFile(String path, @SuppressWarnings("unused") PNone string, String la
179189
@Specialization
180190
Object evalFile(String path, @SuppressWarnings("unused") PNone string, @SuppressWarnings("unused") PNone lang) {
181191
Env env = getContext().getEnv();
192+
if (!env.isPolyglotAccessAllowed()) {
193+
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
194+
}
182195
try {
183196
return getContext().getEnv().parse(Source.newBuilder(PythonLanguage.ID, env.getTruffleFile(path)).name(path).build()).call();
184197
} catch (IOException e) {
@@ -227,21 +240,33 @@ public abstract static class ExportSymbolNode extends PythonBuiltinNode {
227240
@Specialization
228241
@TruffleBoundary
229242
public Object exportSymbol(Object value, String name) {
230-
getContext().getEnv().exportSymbol(name, value);
243+
Env env = getContext().getEnv();
244+
if (!env.isPolyglotAccessAllowed()) {
245+
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
246+
}
247+
env.exportSymbol(name, value);
231248
return value;
232249
}
233250

234251
@Specialization
235252
@TruffleBoundary
236253
public Object exportSymbol(PFunction fun, @SuppressWarnings("unused") PNone name) {
237-
getContext().getEnv().exportSymbol(fun.getName(), fun);
254+
Env env = getContext().getEnv();
255+
if (!env.isPolyglotAccessAllowed()) {
256+
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
257+
}
258+
env.exportSymbol(fun.getName(), fun);
238259
return fun;
239260
}
240261

241262
@Specialization
242263
@TruffleBoundary
243264
public Object exportSymbol(PBuiltinFunction fun, @SuppressWarnings("unused") PNone name) {
244-
getContext().getEnv().exportSymbol(fun.getName(), fun);
265+
Env env = getContext().getEnv();
266+
if (!env.isPolyglotAccessAllowed()) {
267+
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
268+
}
269+
env.exportSymbol(fun.getName(), fun);
245270
return fun;
246271
}
247272

@@ -280,7 +305,11 @@ protected static boolean isModule(Object o) {
280305

281306
@TruffleBoundary
282307
private void export(String name, Object obj) {
283-
getContext().getEnv().exportSymbol(name, obj);
308+
Env env = getContext().getEnv();
309+
if (!env.isPolyglotAccessAllowed()) {
310+
throw raise(PythonErrorType.NotImplementedError, "polyglot access is not allowed");
311+
}
312+
env.exportSymbol(name, obj);
284313
}
285314
}
286315

0 commit comments

Comments
 (0)