Explorar el Código

Fixed potential crash case in MIMEType logic exposed by chaining.

The trimmed string needs to be pulled into a separate variable. Otherwise, the chained substring call is grabbing indices from the previous string, not the trimmed version. This can lead to index out of bounds problems.
Christian Noon hace 10 años
padre
commit
d82cae06d5
Se han modificado 1 ficheros con 4 adiciones y 4 borrados
  1. 4 4
      Source/Validation.swift

+ 4 - 4
Source/Validation.swift

@@ -72,9 +72,9 @@ extension Request {
         let subtype: String
 
         init?(_ string: String) {
-            let components = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
-                                   .substringToIndex(string.rangeOfString(";")?.endIndex ?? string.endIndex)
-                                   .componentsSeparatedByString("/")
+            let stripped = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
+            let split = stripped.substringToIndex(stripped.rangeOfString(";")?.endIndex ?? stripped.endIndex)
+            let components = split.componentsSeparatedByString("/")
 
             if let
                 type = components.first,
@@ -141,7 +141,7 @@ extension Request {
     public func validate() -> Self {
         let acceptableStatusCodes: Range<Int> = 200..<300
         let acceptableContentTypes: [String] = {
-            if let accept = self.request?.valueForHTTPHeaderField("Accept") {
+            if let accept = request?.valueForHTTPHeaderField("Accept") {
                 return accept.componentsSeparatedByString(",")
             }