1

我在核心数据库中有一个查询谓词,但我不知道验证其参数的正确方法是什么?

- (void) queryToDatabaseWithStoreId:(NSInteger) storeId {
   [NSPredicate predicateWithFormat:@"store.storeId = %d", storeId];
}

我的问题是如何验证 storeId 参数或我需要使用什么来消除该漏洞?

如果我有一个清单:

- (void) queryToDataBaseWithListStore:(NSArray<Store *> *) storeList {
   [NSPredicate predicateWithFormat:@"store.storeId IN %@", [storeList valueForObject:@"storeId"]];
}

https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/ValidatingInput.html#//apple_ref/doc/uid/TP40007246-SW3

我需要避免这种情况:

以下常用函数和方法容易受到格式字符串攻击:

标准 C

printf 和 printf(3) 手册页中列出的其他函数 sscanf 和 scanf(3) 手册页中列出的其他函数 syslog 和 vsyslog

AEBuildDesc 和 vAEBuildDesc AEBuildParameters 和 vAEBuildParameters AEBuildAppleEvent 和 vAEBuildAppleEvent 核心基础 CFStringCreateWithFormat CFStringCreateWithFormatAndArguments CFStringAppendFormat CFStringAppendFormatAndArguments

可可

stringWithFormat:, initWithFormat:, 和其他将格式化字符串作为参数的 NSString 方法 appendFormat: 在 NSMutableString 类中 alertWithMessageText:defaultButton:alternateButton: otherButton :informativeTextWithFormat: 在 NSAlert predicateWithFormat:, predicateWithFormat:arguments: 和predicateWithFormat:argumentArray: 在 NSPredicate :format: 和 raise:format:arguments: 在 NSException NSRunAlertPanel 和其他创建或返回面板或工作表的 AppKit 函数中

避免这种攻击的最佳方法是什么?

4

1 回答 1

0

我已经编写了这门课,但我不知道它是否足够。

@implementation StringUtils

+ (BOOL) isEmpty:(id) text {
    if ([text isKindOfClass:[NSNull class]]) {
        return YES;
    } else {
        if (text) {
            if ([text isKindOfClass:[NSString class]]) {
                NSString *textStr = [NSString stringWithFormat:@"%@", text];
                return [textStr isEqualToString:@""];
            }
            return YES;
        } else {
            return YES;
        }
    }
}

+ (NSString *) validateField:(id) text {

    NSInteger numErrors = 0;
    NSString *pattern = @"[^A-Za-z0-9-]+";
    NSError *error = nil;

    NSString *textValidated = @"";
    if ([text isKindOfClass:[NSNumber class]]) {
        textValidated = [text stringValue];
    } else if ([text isKindOfClass:[NSString class]]) {
        textValidated = text;
    } else {
        @try {
            textValidated = [text stringValue];
        } @catch (NSException *exception) {
            numErrors=+1;
        }
    }

    //Only numbers && chars && -
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    NSRange textRange = NSMakeRange(0, textValidated.length);
    NSRange matchRange = [regex rangeOfFirstMatchInString:textValidated options:NSMatchingReportProgress range:textRange];
    if (matchRange.location != NSNotFound) {
        numErrors+=1;
    }

    //Not empty string
    if ([StringUtils isEmpty:textValidated]) {
        numErrors+=1;
    }

    if (numErrors == 0) {
        return textValidated;
    }
    return @"";
}

+ (NSArray *) validateArrayFields:(NSArray *) list {

    NSInteger *numErrors = 0;
    for (id obj in list) {
        if ([StringUtils isEmpty:[StringUtils validateField:obj]]) {
            numErrors+=1;
        }
    }

    if (numErrors == 0) {
        return list;
    }

    return [[NSArray alloc] init];
}

@end

正常使用:

[NSPredicate predicateWithFormat:@"store.storeId = %@", [StringUtils validateField:storeId]];

与数组一起使用:

[NSPredicate predicateWithFormat:@"store.storeId IN %@", [StringUtils validateArrayFields:storeId]];
于 2018-06-27T13:27:55.977 回答