Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable pyimport by default, for security #45

Closed
carsonyl opened this issue Jun 4, 2016 · 10 comments
Closed

Disable pyimport by default, for security #45

carsonyl opened this issue Jun 4, 2016 · 10 comments

Comments

@carsonyl
Copy link

carsonyl commented Jun 4, 2016

I'm exploring the use of this library for a project I'm working on, and it looks amazing. I am interested in using this library to parse and execute functions from potentially untrusted JavaScript files, so I am concerned by the availability of the pyimport keyword. Is it possible to add an option to enable/disable this feature, and have it disabled by default?

Doing this would make the default behaviour of Js2Py much safer and similar to standard JavaScript.

@Lexcon
Copy link
Contributor

Lexcon commented Jun 4, 2016

I'm not sure if disabling pyimport would be sufficient. Running
untrusted source using this library is a ballgame that would require
severe thinking rather than a quick fix. Potential issues are which
objects are directly exposed in the runnable space, and which one more
indirectly. The usual suspects are:

exec
eval
type

http://programmers.stackexchange.com/questions/191623/best-practices-for-execution-of-untrusted-code

Read the post of Martijn Pieters.

Robert

On 6/4/2016 2:31 AM, Carson Lam wrote:

I'm exploring the use of this library for a project I'm working on,
and it looks amazing. I am interested in using this library to parse
and execute functions from potentially untrusted JavaScript files, so
I am concerned by the availability of the |pyimport| keyword. Is it
possible to add an option to enable/disable this feature, /and/ have
it disabled by default?

Doing this would make the default behaviour of Js2Py much safer and
similar to standard JavaScript.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#45, or mute the
thread
https://github.com/notifications/unsubscribe/AD5u-33kt0KYJUGfxnm3HevFcflyBnV6ks5qIMdmgaJpZM4IuAdh.

@carsonyl
Copy link
Author

carsonyl commented Jun 4, 2016

In my use case, I'll only inject a few simple functions into JavaScript, using the EvalJs constructor. I definitely won't be exposing things like exec, eval, type, os, subprocess, pickle, etc. In my mind, what I'm doing should already be safely sandboxed, unless it's somehow possible to inject code into the Python functions I added to the JavaScript scope...

@Lexcon
Copy link
Contributor

Lexcon commented Jun 4, 2016

Myfunction.class.class will give you the type function. With that you can pretty much hack into anything. It's not the primary objects you need to worry about its the introspection.

Verzonden vanaf mijn Samsung-apparaat

-------- Oorspronkelijk bericht --------
Van: Carson Lam notifications@github.com
Datum: 04-06-16 22:43 (GMT+01:00)
Aan: PiotrDabkowski/Js2Py Js2Py@noreply.github.com
Cc: Lexcon robert@dotdata.nl, Comment comment@noreply.github.com
Onderwerp: Re: [PiotrDabkowski/Js2Py] Disable pyimport by default, for security
(#45)

In my use case, I'll only inject a few simple functions into JavaScript, using the EvalJs constructor. I definitely won't be exposing things like exec, eval, type, os, subprocess, pickle, etc. In my mind, what I'm doing should already be safely sandboxed, unless it's somehow possible to inject code into the Python functions I added to the JavaScript scope...


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

@carsonyl
Copy link
Author

carsonyl commented Jun 4, 2016

Hmm, interesting. Thanks for the insight - I am new to this topic.

I just did a quick test and it looks like I can't access myFunction.class.class from within Js2Py:

>>> import js2py
>>> def hello(world):
...     print(world)
...     return world
>>> js = js2py.EvalJs({'hello': hello})
>>> js.execute('hello')
>>> js.execute('hello("world")')
'world'
>>> js.execute('hello.__class__')
>>> js.execute('hello.__class__.__class__')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\carso\env\lib\site-packages\js2py\evaljs.py", line 131, in execute
    exec(compiled, self._context)
  File "<EvalJS snippet>", line 2, in <module>
  File "C:\Users\carso\env\lib\site-packages\js2py\base.py", line 263, in get
    raise MakeError('TypeError', 'Undefiend and null dont have properties!')
js2py.base.PyJsException: TypeError: Undefiend and null dont have properties!

Though it's unclear whether this is by design.

@Lexcon
Copy link
Contributor

Lexcon commented Jun 5, 2016

My test confirmed this. Maybe it's not that unsafe after all although it
should be assessed that all loops are closed. I also can't access
builtins, which is pretty neat.

What kind of app did you want to use it for? It sounds like a similar
use case I had in mind for my application although I never found time to
pursue it, so I'll stick to using my built-in compiler (for a VFP like
language, https://en.wikipedia.org/wiki/Visual_FoxPro) that's not half
as cool as js2py.

Robert

On 6/4/2016 11:43 PM, Carson Lam wrote:

Hmm, interesting. Thanks for the insight - I am new to this topic.

I just did a quick test and it looks like I can't access
myFunction.class.class from within Js2Py:

import js2py
def hello(world):
... print(world)
... return world
js= js2py.EvalJs({'hello': hello})
js.execute('hello')
js.execute('hello("world")')
'world'
js.execute('hello.class')
js.execute('hello.class.class')
Traceback (most recent call last):
File"", line1,in
File"C:\Users\carso\env\lib\site-packages\js2py\evaljs.py", line131,in execute
exec(compiled,self._context)
File"", line2,in
File"C:\Users\carso\env\lib\site-packages\js2py\base.py", line263,in get
raise MakeError('TypeError','Undefiend and null dont have properties!')
js2py.base.PyJsException:TypeError: Undefiendand null dont have properties!

Though it's unclear whether this is by design.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#45 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AD5u--swzpZD6q5uiV9Dl08vzprDvs2Mks5qIfF2gaJpZM4IuAdh.

@carsonyl
Copy link
Author

carsonyl commented Jun 5, 2016

I hope to use Js2Py for parsing and executing PAC files, which contain a JavaScript function that accepts a URL and outputs the proxy server to use for that URL. One way to obtain a PAC file is to go up the DNS hierarchy using a known URL pattern, and using the first valid response.

@carsonyl
Copy link
Author

carsonyl commented Nov 5, 2016

@PiotrDabkowski Can you explain the decision you've made here? In PyPAC, my solution to this problem was to monkeypatch Js2Py to disable pyimport. It would be nice if this could be supported officially.

@PiotrDabkowski
Copy link
Owner

PiotrDabkowski commented Nov 7, 2016

Hey, Js2Py is insecure enough even without pyimport :) I mean I tried to make it secure as far as possible, but I cannot guarantee anything - removing pyimport makes exploiting Js2Py harder, but I am pretty sure its still possible. I you want I can add a flag disabling pyimport

@PiotrDabkowski PiotrDabkowski reopened this Nov 7, 2016
@carsonyl
Copy link
Author

carsonyl commented Nov 7, 2016

Do you have theories on other ways to break the security of Js2Py, even when pyimport is disabled? I'm curious to know, as this is something I'd like to explore, but I've only tried the most obvious potential security issues.

I definitely would appreciate a flag to disable pyimport, because my monkeypatch method is fragile to internal changes within Js2Py.

@PiotrDabkowski
Copy link
Owner

PiotrDabkowski commented Nov 15, 2016

Done! Now you can call js2py.disable_pyimport to disable it for current session.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants