Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3999310
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:38:28+00:00 2026-05-20T07:38:28+00:00

I want to disallow access to file system from clients code, so I think

  • 0

I want to disallow access to file system from clients code, so I think I could overwrite open function

env = {
   'open': lambda *a: StringIO("you can't use open")
}

exec(open('user_code.py'), env)

but I got this

unqualified exec is not allowed in function 'my function' it contains a 
nested function with free variables

I also try

 def open_exception(*a):
     raise Exception("you can't use open")
 env = {
     'open': open_exception
 }

but got the same Exception (not “you can’t use open”)

I want to prevent of:

executing this:

"""def foo():
     return open('some_file').read()
print foo()"""

and evaluate this

"open('some_file').write('some text')"

I also use session to store code that was evaluated previously so I need to prevent of executing this:

"""def foo(s):
   return open(s)"""

and then evaluating this

"foo('some').write('some text')"

I can’t use regex because someone could use (eval inside string)

"eval(\"opxx('some file').write('some text')\".replace('xx', 'en')"

Is there any way to prevent access to file system inside exec/eval? (I need both)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:38:29+00:00Added an answer on May 20, 2026 at 7:38 am

    There’s no way to prevent access to the file system inside exec/eval. Here’s an example code that demonstrates a way for the user code to call otherwise restricted classes that always works:

    import subprocess
    code = """[x for x in ().__class__.__bases__[0].__subclasses__() 
               if x.__name__ == 'Popen'][0](['ls', '-la']).wait()"""
    # Executing the `code` will always run `ls`...
    exec code in dict(__builtins__=None)
    

    And don’t think about filtering the input, especially with regex.

    You might consider a few alternatives:

    1. ast.literal_eval if you could limit yourself only to simple expressions
    2. Using another language for user code. You might look at Lua or JavaScript – both are sometimes used to run unsafe code inside sandboxes.
    3. There’s the pysandbox project, though I can’t guarantee you that the sandboxed code is really safe. Python wasn’t designed to be sandboxed, and in particular the CPython implementation wasn’t written with sandboxing in mind. Even the author seems to doubt the possibility to implement such sandbox safely.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to loop over the contents of a text file and do a
I want to setup a cron job to rsync a remote system to a
I want to prevent the robots from accessing URLs that end with /new .
The following code produces pretty much what I want, but the scrollable pane on
Want to know what the stackoverflow community feels about the various free and non-free
Want my FireFox at work to be in sync with my FireFox at my
I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the
I want to reference a COM DLL in a .NET project, but I also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.