2014-11-21 94 views
0

公司進行代碼掃描後,我遇到了一個代碼問題。
該報告顯示,我的代碼在嘗試執行Web服務POST請求時,存在XSS攻擊漏洞。
我對安全性問題不太瞭解。
任何人都可以指出我如何解決這個安全漏洞的正確方向?
NSURL POST與iOS的XSS漏洞

非常感謝。

我的Web服務調用的CA可信服務器,所以我用:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { 
    SecTrustResultType result; 
    SecTrustEvaluate(challenge.protectionSpace.serverTrust, & result); 

    if(result == kSecTrustResultProceed || result == kSecTrustResultUnspecified) { 
     [challenge.sender useCredential:[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust] forAuthenticationChallenge: challenge]; 
     return; 

來保護連接。 以下代碼是我編寫我的URL請求的地方。

再次,我沒有很多的安全知識,
所以任何幫助將不勝感激!


- (BOOL)httpPostWithUrl:(NSString *)url headersAndValues:(NSDictionary *)headersAndValues delegate:(id)delegate 
{ 
    NSMutableString *bodyString = [[NSMutableString alloc] initWithString:@""]; 
    for (NSString *key in [headersAndValues allKeys]) 
    { 
     [bodyString appendString:[NSString stringWithFormat:@"%@=%@&", key, headersAndValues[key]]]; 
    } 

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] 
                   cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                  timeoutInterval:20.0f]; 
    [urlRequest setHTTPMethod:@"POST"]; 
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
    if (bodyString.length) 
    { 
     NSString *requestBody = [bodyString substringToIndex:bodyString.length-1]; 
     NSData *requestData = [requestBody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 
     [urlRequest setHTTPBody:requestData]; 

     if (!_connectionRunning) 
     { 
      NSURLConnection *connection =[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

      return YES; 
     } 
     else 
     { 
      // error 
     } 
    } 
} 
return NO; 

}

回答

1

我認爲XSS在這條線:

[bodyString appendString:[NSString stringWithFormat:@"%@=%@&", key, headersAndValues[key]]]; 

你需要檢查鍵和headersAndValues無效字符[關鍵]。

NSString *checkedKey = [self alphanumericStringFromString:checkedKey]; 

+ (NSString *)alphanumericString { // NSString category 
    NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; 
    NSString *trimmedReplacement = [[self componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@""]; 
    return trimmedReplacement; 
} 

希望它可以幫助你。

+0

感謝您的快速回復!但什麼是無效字符?任何你不希望從URL鏈接? – timlwting 2014-11-21 11:48:47

+0

是的。這些符號可以中斷正確的操作。如果您的參數是可執行代碼,並且它們被添加到代碼頁中,則可以執行它們。例如:value =「」。大多數使用它:&和? ( - 可以添加新的參數;),「和',<(html標籤),\(轉義字符)。 – user1502383 2014-11-21 12:22:10