Search code examples
pythonstdinencapsulation

Hide stdin data from user running script?


I am constructing a problem on spoj.com. User has to submit a script that will be pre-appended with a code of mine that will provide him with functions that he has to use. A template of this kind:

def youmustusethis(maptask, reducetask):
  data = sys.stdin.readlines()
  return reduce(reducetask, map(maptask, data))

##### USER WRITES CODE BELOW #####
....

The funny thing is: users are way too often hacking output than solving problems. I anticipate that people will just ignore the functionality and read stdin directly and process it completely ignoring the "framework" I provide.

Is there a way to hide stdin data from the bottom-half of the script in a way that will force the use of my framework to process it?


Solution

  • My suggestion is - don't. There are many things you need to consider in order to make this work properly. Consider the following snippet:

    sys.stdin = open("/dev/stdin")
    

    With ctypes, this might get even more difficult. Instead of hacking around trying to obfuscate a rather big attack vector, I would suggest looking for a sandboxing/restricted Python enviroments that somebody already created. My guess is that libraries for IRC bots should already be doing this properly.