CVE-2022-4558 (Objective-C, published 2022-12-16T17:15Z)
Description: 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.
Severity: nan (CVSS v3 base score: 6.1)
Weakness: CWE-707 \u2014 Improper Neutralization
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.
Repository: https://github.com/Alinto/sogo
Files changed:
SoObjects/SOGo/NSString+Utilities.m
Commit message: fix(security): Security fix for WSTG-INPV-02. Add XSS protection on folders and mail title / content


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;
  }