PDA

View Full Version : keyword restriction


sheefo
January 3rd, 2007, 11:48 AM
I have been using Ruby for a long time now, but there are still some thing I don't know. Like in my Ruby/SDL game engine I am making, I need to know how to catch keywords like 'require' when the engine eval's the script and stop them from doing anything, so they cannot require files outside of the engine (like RPG Maker XP).

Does anyone know how to do this? I need the engine to require scripts but when the engine code eval's a script it must not allow the 'require' keyword to do anything.

Any advice is appreciated.

rob
January 3rd, 2007, 08:18 PM
The simplest way I can think if would just be to do some regex pattern subbing with .sub to eliminate any unwanted commands.

sheefo
January 4th, 2007, 06:52 AM
I am not familiar with the Regexp class and what it can do. Can I have an example please? :)

BTW, I use 'script = IO.read('script.rb')' then I use 'eval(script)' to run the script (with error handling, no point posting that code though). I hope that gives you a better idea...?

rob
January 5th, 2007, 10:25 PM
Well, say you don't want them to be able to use "system".

a = String.new
a.sub!(/system/, "")

Would replace any instance of the word "system" with nothing (removing it). You could also do something like:

a.sub!(/system/, "puts \"You cannot use this here.\")

Which would create a stub for the command, letting them know they can't. You could also check for ( and a whitespace after system so that you don't accidentally just grab any string with "system" in it.. like:

a.sub![/(system\()|(system\ )/, "puts \"You can't do that here."]
Hope this helps.