CVE-2022-4558: Improper Neutralization (CWE-707)
value: A vulnerability was found in Alinto SOGo up to 5.7.1. It has been classified as problematic. This affects an unknown part of the file SoObjects/SOGo/NSString+Utilities.m of the component Folder/Mail Handler. The manipulation leads to cross site scripting. It is possible to initiate the attack remotely. Upgrading to version 5.8.0 is able to address this issue. The name of the patch is 1e0f5f00890f751e84d67be4f139dd7f00faa5f3. It is recommended to upgrade the affected component. The identifier VDB-215961 was assigned to this vulnerability.
CWE description: The product does not ensure or incorrectly ensures that structured messages or data are well-formed and that certain security properties are met before being read from an upstream component or sent to a downstream component.
CVSS v3 base score: 6.1
Published: 2022-12-16T17:15Z
Repository: https://github.com/Alinto/sogo
Language: Objective-C
Files changed: SoObjects/SOGo/NSString+Utilities.m
Commit date: 2022-11-22 10:09:41 +0100
Commit message: fix(security): Security fix for WSTG-INPV-02. Add XSS protection on folders and mail title / content

Diff: diff --git a/SoObjects/SOGo/NSString+Utilities.m b/SoObjects/SOGo/NSString+Utilities.m
index 9f07c246f..92fb50a4c 100644
--- a/SoObjects/SOGo/NSString+Utilities.m
+++ b/SoObjects/SOGo/NSString+Utilities.m
@@ -896,39 +896,53 @@ - (NSString *) decryptWithKey: (NSString *) theKey
  * @param stripHTMLCode Remove all HTML code from content
  * @return A safe string
  */
 - (NSString *) stringWithoutHTMLInjection: (BOOL)stripHTMLCode
 {
-  NSString *result, *text;
-  NSScanner *theScanner;
-  NSError *error;
-
-  text = nil;
-  error = nil;
-  result = [NSString stringWithString: self];
-
-  if (stripHTMLCode) {
-    // Author : https://www.codercrunch.com/question/1251681838/how-remove-html-tags-string-ios
-    theScanner = [NSScanner scannerWithString: result];
-    while ([theScanner isAtEnd] == NO) {
-      // find start of tag
-      [theScanner scanUpToString: @"<" intoString: NULL];
-      // find end of tag
-      [theScanner scanUpToString: @">" intoString: &text];
-      // replace the found tag with a space
-      //(you can filter multi-spaces out later if you wish)
-      result = [result stringByReplacingOccurrencesOfString:
-              [NSString stringWithFormat: @"%@>", text]
-              withString: @" "];
-    } 
-  } else {
-    // Clean XSS
-    // Examples of injection : https://cheatsheetseries.owasp.org/cheatsheets/XSS_Filter_Evasion_Cheat_Sheet.html#xss-locator-polygot
-
-    // NSRegularExpression is not implemented in old GNUStep versions (ubuntu trusty)
-    if (NSClassFromString(@"NSRegularExpression")) {
-      NSRegularExpression *regex = nil;
+  // NSRegularExpression is not implemented in old GNUStep versions (ubuntu trusty)
+  if (NSClassFromString(@"NSRegularExpression")) {
+    NSString *result, *text;
+    NSScanner *theScanner;
+    NSError *error;
+    NSUInteger numberOfMatches;
+    NSRegularExpression *regex;
+
+    text = nil;
+    error = nil;
+    result = [NSString stringWithString: self];
+    regex = nil;
+
+    if (stripHTMLCode) {
+      // Author : https://www.codercrunch.com/question/1251681838/how-remove-html-tags-string-ios
+      theScanner = [NSScanner scannerWithString: result];
+      while ([theScanner isAtEnd] == NO) {
+        // find start of tag
+        [theScanner scanUpToString: @"<" intoString: NULL];
+        // find end of tag
+        [theScanner scanUpToString: @">" intoString: &text];
+        
+        // Check that text is not <xxx@xx.net
+        regex = [NSRegularExpression regularExpressionWithPattern:@"^<[a-zA-Z.\\-_]+@[a-zA-Z.\\-_]+.[a-zA-Z]+$"
+                                    options: NSRegularExpressionCaseInsensitive error:&error];
+        numberOfMatches = 0;
+        
+        if (text && !error) {
+          numberOfMatches = [regex numberOfMatchesInString: text 
+                                                      options:0 
+                                                      range:NSMakeRange(0, [text length])];
+          if (0 == numberOfMatches) {
+            // replace the found tag with a space
+            //(you can filter multi-spaces out later if you wish)
+            result = [result stringByReplacingOccurrencesOfString:
+                    [NSString stringWithFormat: @"%@>", text]
+                    withString: @" "];
+          }
+        }
+      } 
+    } else {
+      // Clean XSS
+      // Examples of injection : https://cheatsheetseries.owasp.org/cheatsheets/XSS_Filter_Evasion_Cheat_Sheet.html#xss-locator-polygot
 
       // Remove javascript:
       regex = [NSRegularExpression regularExpressionWithPattern:@"j[\\s\\u200B&#x09;&#x0A;&#x0D;\\\\0]*a[\\s\\u200B&#x09;&#x0A;&#x0D;\\\\0]*v[\\s\\u200B&#x09;&#x0A;&#x0D;\\\\0]*a[\\s\\u200B&#x09;&#x0A;&#x0D;\\\\0]*s[\\s\\u200B&#x09;&#x0A;&#x0D;\\\\0]*c[\\s\\u200B&#x09;&#x0A;&#x0D;\\\\0]*r[\\s\\u200B&#x09;&#x0A;&#x0D;\\\\0]*i[\\s\\u200B&#x09;&#x0A;&#x0D;\\\\0]*p[\\s\\u200B&#x09;&#x0A;&#x0D;\\\\0]*t[\\s\\u200B&#x09;&#x0A;&#x0D;\\\\0]*:"
                                   options: NSRegularExpressionCaseInsensitive error:&error];
       result = [regex stringByReplacingMatchesInString:result options:0 range:NSMakeRange(0, [result length]) withTemplate:@""];
@@ -965,12 +979,14 @@ - (NSString *) stringWithoutHTMLInjection: (BOOL)stripHTMLCode
 
       // Remove onmouseover
       regex = [NSRegularExpression regularExpressionWithPattern:@"onmouseover=" 
                                   options: NSRegularExpressionCaseInsensitive error:&error];
       result = [regex stringByReplacingMatchesInString:result options:0 range:NSMakeRange(0, [result length]) withTemplate:@"onmouseo***="];
-    }
-  }  
-  
-  return result;
+    }  
+    
+    return result;
+  } else {
+    return self;
+  }
 }
 
 @end

Vulnerable code:   NSString *result, *text;
  NSScanner *theScanner;
  NSError *error;

  text = nil;
  error = nil;
  result = [NSString stringWithString: self];

  if (stripHTMLCode) {
    // Author : https://www.codercrunch.com/question/1251681838/how-remove-html-tags-string-ios
    theScanner = [NSScanner scannerWithString: result];
    while ([theScanner isAtEnd] == NO) {
      // find start of tag
      [theScanner scanUpToString: @"<" intoString: NULL];
      // find end of tag
      [theScanner scanUpToString: @">" intoString: &text];
      // replace the found tag with a space
      //(you can filter multi-spaces out later if you wish)
      result = [result stringByReplacingOccurrencesOfString:
              [NSString stringWithFormat: @"%@>", text]
              withString: @" "];
    } 
  } else {
    // Clean XSS
    // Examples of injection : https://cheatsheetseries.owasp.org/cheatsheets/XSS_Filter_Evasion_Cheat_Sheet.html#xss-locator-polygot

    // NSRegularExpression is not implemented in old GNUStep versions (ubuntu trusty)
    if (NSClassFromString(@"NSRegularExpression")) {
      NSRegularExpression *regex = nil;
    }
  }  
  
  return result;
Fixed code:   // NSRegularExpression is not implemented in old GNUStep versions (ubuntu trusty)
  if (NSClassFromString(@"NSRegularExpression")) {
    NSString *result, *text;
    NSScanner *theScanner;
    NSError *error;
    NSUInteger numberOfMatches;
    NSRegularExpression *regex;

    text = nil;
    error = nil;
    result = [NSString stringWithString: self];
    regex = nil;

    if (stripHTMLCode) {
      // Author : https://www.codercrunch.com/question/1251681838/how-remove-html-tags-string-ios
      theScanner = [NSScanner scannerWithString: result];
      while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString: @"<" intoString: NULL];
        // find end of tag
        [theScanner scanUpToString: @">" intoString: &text];
        
        // Check that text is not <xxx@xx.net
        regex = [NSRegularExpression regularExpressionWithPattern:@"^<[a-zA-Z.\\-_]+@[a-zA-Z.\\-_]+.[a-zA-Z]+$"
                                    options: NSRegularExpressionCaseInsensitive error:&error];
        numberOfMatches = 0;
        
        if (text && !error) {
          numberOfMatches = [regex numberOfMatchesInString: text 
                                                      options:0 
                                                      range:NSMakeRange(0, [text length])];
          if (0 == numberOfMatches) {
            // replace the found tag with a space
            //(you can filter multi-spaces out later if you wish)
            result = [result stringByReplacingOccurrencesOfString:
                    [NSString stringWithFormat: @"%@>", text]
                    withString: @" "];
          }
        }
      } 
    } else {
      // Clean XSS
      // Examples of injection : https://cheatsheetseries.owasp.org/cheatsheets/XSS_Filter_Evasion_Cheat_Sheet.html#xss-locator-polygot
    }  
    
    return result;
  } else {
    return self;
  }