浏览代码

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 10 年之前
父节点
当前提交
d82cae06d5
共有 1 个文件被更改,包括 4 次插入4 次删除
  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(",")
             }