1

我正在尝试创建一个工厂类结构,其中我的抽象基类 PDObject 根据在 NSDictionary 中传递给它的信息将实例实例化为适当的子类。这是我的 PDObject 初始化方法:

- (id)initWithDictionary:(NSDictionary *)dictionary inEnvironment:(PDEnvironment *)environment {
    NSString *className = [dictionary objectForKey:@"objectType"];
    if (className) {
        Class objectClass = NSClassFromString(className);
        if ([objectClass isSubclassOfClass:[PDObject class]]) {
            self = [[objectClass alloc] initWithDictionary:dictionary inEnvironment:environment];
        } else {
            NSLog(@"tried to instantiate an object of the wrong object type");
            self = nil;
        }
    } else {
        NSLog(@"tried to instantiate an object without an object type");
    }
    return self;
}

我想知道是否有人知道这种模式的任何安全问题。我担心字典中可能会传入一些恶意的东西并实例化一些意想不到的东西。我检查以确保它是 PDObject 的正确子类。这里有什么我应该担心的,还是我只是偏执?

4

2 回答 2

2

It is unlikely to be a security hole, but passing potentially random strings to runtime functions isn't really something the runtime is hardened against. The risk isn't instantiating random classes, but causing the app to potentially crash or execute random code.

In general, I wouldn't go beyond minimal effort. To that ends, I would suggest using NSScanner to scan the class name to see if it has any characters that are obviously out of bounds. I would think scanning for alphanumericCharacterSet would be sufficient.

于 2013-01-01T19:14:50.747 回答
2

活力很好,我认为这里没有什么特别危险的地方。如果要避免崩溃,可以检查特定对象 a。不是nil(以防万一)和b。响应您要发送的任何选择器。另请注意,无论您使用哪种保护,想要使用您的应用程序模拟的人总是能够使用库插入(遇到臭名昭著的DYLD_INSERT_LIBRARIES环境变量)和 Objective-C 运行时来做到这一点。

于 2013-01-01T19:25:31.470 回答