Skip to content

ClassCastException when reading non String value from Java System.properties #1139

Description

@pachoo

When JRuby is setting up the global environment it sets the JAVA_ENV variable to the value of Java's System.properties. When there is a non String value the OSEnvironment code below will throw a ClassCastException.

    private static Map getAsMapOfRubyStrings(Ruby runtime, Set<Map.Entry<Object, Object>> entrySet) {
        Map envs = new HashMap();
        Encoding encoding = runtime.getEncodingService().getLocaleEncoding();

        // On Windows, entrySet doesn't have corresponding keys for these
        if (Platform.IS_WINDOWS) {
            // these may be null when in a restricted environment (JRUBY-6514)
            String home = SafePropertyAccessor.getProperty("user.home");
            String user = SafePropertyAccessor.getProperty("user.name");
            addRubyKeyValuePair(runtime, envs, "HOME", home == null ? "/" : home, encoding);
            addRubyKeyValuePair(runtime, envs, "USER", user == null ? "" : user, encoding);
        }

        for (Map.Entry<Object, Object> entry : entrySet) {
            // This will throw a ClassCastException if the value is not a String
            String value = (String)entry.getValue();
            String key = (String)entry.getKey();

            addRubyKeyValuePair(runtime, envs, key, value, encoding);
        }

        return envs;
    }

Though Properties should be all String values, since they are subclassed from java.util.Hashtable the put() method can add a non String object. From the Properties documentation:
"Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail. Similarly, the call to the propertyNames or list method will fail if it is called on a "compromised" Properties object that contains a non-String key."

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions