From: "Skye Shaw!@#$" Date: 2007-08-08T16:45:04+09:00 Subject: Re: non-constant strings On Aug 7, 2:41 pm, Dmitry Bilunov wrote: > Hello. Why does Ruby have non-constant strings? It seems there is a way > to bypass object encapsulation paradigm and break object integrity. Here > is any example: > > class SecureRunner > # This class implements a sudo-like > # runner > > def initialize(command) > # Creates an instance. Guaranties, that a command is safe. > if command.safe? This will result in a no method error, no? > @comamnd = command > else > raise RuntimeError, "Security check failed!" > end > end > > def run > # Only safe commands should be run > system(@command) > end > end > > # This class seems to be safe > # Here is a way to bypass security check: > > command = "some_safe_command" > runner = SecureRunner.new(command) > # a command is safe, so check will be passed > > command.replace("evil_command") # BYPASS THE CHECK > runner.run # runs evil_command, that is not safe Well, this is not the fault of the language, rather your SecureRunner class. def run # Only safe commands should be run if @command.tainted? raise RuntimeError, "Security check failed!" end system(@command) end > The same can be done to fields of instances, which are exported as > read-only (attr_reader). This is the case any language where arguments are passed by reference. > but what is the reason Ruby has non-constant strings You mean mutable strings. Ruby does have constant strings: irb(main):001:0> CONST="assbasscass" => "assbasscass" irb(main):002:0> CONST=123 (irb):2: warning: already initialized constant CONST => 123 The String class wraps an array of chars, realloc()'in as necessary (ruby hackers correct me if I've mistakin). Consider this in Java: class SomeClassThatRequiredAlotOfTyping { private StringBuffer sb //.... public void printBuffer() { System.out.println(sb); } } StringBuffer sb = new StringBuffer("Dont Chnage!"); SomeClassThatRequiredAlotOfTyping clazz = new SomeClassThatRequiredAlotOfTyping(sb); // I'll show that private "read-only" var who's in charge! sb.delete(0,sb.length()); sb.append("VB6, its ByVal keyword rocked... Not!"); clazz.printBuffer(); Hope that helps.