2

I am using javascript generated by Empythoned to create a chrome extension. Python import doesn't work in chrome extension where as it works in web application. Here is the demo.

Sample code:

Web App

Input

import collections
print collections

Output

<module 'collections' from '/lib/python2.7/collections.py'>

Chrome Extension

Input

import collections

Output

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/lib/python2.7/collections.py", line 8, in <module>
from _collections import deque, defaultdict
ImportError: Could not evaluate dynamic lib: //lib/python2.7/_collections.so

Is it something to do with chrome extension handling JS ?

dgilperez
  • 10,716
  • 8
  • 68
  • 96
Kracekumar
  • 19,457
  • 10
  • 47
  • 56

1 Answers1

1

Empythoned is trying to eval() code, and Chrome by default restricts eval() from being used in extensions.

More details on the Content Security Policy can be found here:

https://developer.chrome.com/extensions/contentSecurityPolicy

If you add this line relaxing the security policy to your extension's manifest.json, you should be able to import those modules:

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"

As the documentation warns, eval() is a notorious XSS attack vector, so you should be careful when allowing it in your extensions.

empyrical
  • 362
  • 2
  • 5
  • 16