@capacitor/ios 6.2.2 → 6.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -98,6 +98,12 @@ open class CapacitorBridge: NSObject, CAPBridgeProtocol {
98
98
  @available(*, deprecated, message: "`httpsInterceptorStartIdentifier` is no longer required. All proxied requests are handled via `httpInterceptorStartIdentifier` instead")
99
99
  public static let httpsInterceptorStartIdentifier = "/_capacitor_https_interceptor_"
100
100
  public static let httpInterceptorUrlParam = "u"
101
+
102
+ /// Whether `path` addresses one of the bridge's internal proxy routes (local files or the HTTP proxy).
103
+ /// These are served at the app's own origin, so they must never be loaded as a document.
104
+ public static func isInternalProxyPath(_ path: String) -> Bool {
105
+ return path.starts(with: fileStartIdentifier) || path.starts(with: httpInterceptorStartIdentifier)
106
+ }
101
107
  public static let defaultScheme = "capacitor"
102
108
 
103
109
  public private(set) var webViewAssetHandler: WebViewAssetHandler
@@ -493,13 +499,13 @@ open class CapacitorBridge: NSObject, CAPBridgeProtocol {
493
499
  let pluginCall = CAPPluginCall(callbackId: call.callbackId, methodName: call.method,
494
500
  options: JSTypes.coerceDictionaryToJSObject(call.options,
495
501
  formattingDatesAsStrings: plugin.shouldStringifyDatesInCalls) ?? [:],
496
- success: {(result: CAPPluginCallResult?, pluginCall: CAPPluginCall?) -> Void in
502
+ success: {(result: CAPPluginCallResult?, pluginCall: CAPPluginCall?) in
497
503
  if let result = result {
498
504
  self?.toJs(result: JSResult(call: call, callResult: result), save: pluginCall?.keepAlive ?? false)
499
505
  } else {
500
506
  self?.toJs(result: JSResult(call: call, result: .dictionary([:])), save: pluginCall?.keepAlive ?? false)
501
507
  }
502
- }, error: {(error: CAPPluginCallError?) -> Void in
508
+ }, error: {(error: CAPPluginCallError?) in
503
509
  if let error = error {
504
510
  self?.toJsError(error: JSResultError(call: call, callError: error))
505
511
  } else {
@@ -146,8 +146,8 @@ extension _JSValueDecoder: Decoder {
146
146
  return date as! T
147
147
  case .formatted(let formatter):
148
148
  guard let value = data as? String else { throw DecodingError.dataCorrupted(data, target: String.self, codingPath: codingPath) }
149
- guard let date = formatter.date(from: value) else { throw DecodingError.dataCorrupted(value, target: Date.self, codingPath: codingPath) }
150
- return date as! T
149
+ guard let date = formatter.date(from: value) else { throw DecodingError.dataCorrupted(value, target: Date.self, codingPath: codingPath) }
150
+ return date as! T
151
151
  case .custom(let decode):
152
152
  return try decode(self) as! T
153
153
  @unknown default:
@@ -155,7 +155,7 @@ extension _JSValueDecoder: Decoder {
155
155
  }
156
156
  case is URL.Type:
157
157
  guard let str = data as? String,
158
- let url = URL(string: str)
158
+ let url = URL(string: str)
159
159
  else { throw DecodingError.dataCorrupted(data, target: URL.self, codingPath: codingPath) }
160
160
 
161
161
  return url as! T
@@ -354,7 +354,7 @@ extension SingleValueContainer: SingleValueDecodingContainer {
354
354
 
355
355
  private func castFloat<N>(to type: N.Type) throws -> N where N: FloatingPoint {
356
356
  if let data = data as? String,
357
- case let .convertFromString(positiveInfinity: pos, negativeInfinity: neg, nan: nan) = options.nonConformingStrategy {
357
+ case let .convertFromString(positiveInfinity: pos, negativeInfinity: neg, nan: nan) = options.nonConformingStrategy {
358
358
  switch data {
359
359
  case pos:
360
360
  return N.infinity
@@ -82,7 +82,6 @@ public final class JSValueEncoder: TopLevelEncoder {
82
82
  self.options = .init(optionalStrategy: optionalEncodingStrategy, dateStrategy: dateEncodingStrategy, dataStrategy: dataEncodingStrategy, nonConformingFloatStrategy: nonConformingFloatEncodingStategy)
83
83
  }
84
84
 
85
-
86
85
  /// Encodes an `Encodable` value to a ``JSValue``
87
86
  /// - Parameter value: The value to encode to ``JSValue``
88
87
  /// - Returns: The encoded ``JSValue``
@@ -30,7 +30,6 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
30
30
  }
31
31
 
32
32
  open func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
33
- let startPath: String
34
33
  let url = urlSchemeTask.request.url!
35
34
  let stringToLoad = url.path
36
35
  let localUrl = URL.init(string: url.absoluteString)!
@@ -46,10 +45,9 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
46
45
  return
47
46
  }
48
47
 
49
- if stringToLoad.starts(with: CapacitorBridge.fileStartIdentifier) {
50
- startPath = stringToLoad.replacingOccurrences(of: CapacitorBridge.fileStartIdentifier, with: "")
51
- } else {
52
- startPath = router.route(for: stringToLoad)
48
+ guard let startPath = startPath(for: stringToLoad) else {
49
+ urlSchemeTask.didFailWithError(URLError(.fileDoesNotExist))
50
+ return
53
51
  }
54
52
 
55
53
  let fileUrl = URL.init(fileURLWithPath: startPath)
@@ -62,6 +60,8 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
62
60
  "Cache-Control": "no-cache"
63
61
  ]
64
62
 
63
+ headers.merge(securityHeaders(forPath: stringToLoad)) { _, new in new }
64
+
65
65
  // if using live reload, then set CORS headers
66
66
  if isUsingLiveReload(localUrl) {
67
67
  headers["Access-Control-Allow-Origin"] = self.serverUrl?.absoluteString
@@ -111,6 +111,27 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
111
111
  urlSchemeTask.didFinish()
112
112
  }
113
113
 
114
+ /// The file to serve for a request path, or nil if the path must not be served.
115
+ /// `URL.path` decodes `%2F`, so an encoded slash can carry `..` segments the web view did not
116
+ /// resolve. Joined to the base path they would leave it and serve any file the app can read.
117
+ private func startPath(for path: String) -> String? {
118
+ if path.starts(with: CapacitorBridge.fileStartIdentifier) {
119
+ return path.replacingOccurrences(of: CapacitorBridge.fileStartIdentifier, with: "")
120
+ }
121
+ if path.split(separator: "/").contains("..") {
122
+ return nil
123
+ }
124
+ return router.route(for: path)
125
+ }
126
+
127
+ /// A proxied file that is ever rendered as a document must stay inert and unframeable.
128
+ private func securityHeaders(forPath path: String) -> [String: String] {
129
+ guard path.starts(with: CapacitorBridge.fileStartIdentifier) else {
130
+ return [:]
131
+ }
132
+ return ["Content-Security-Policy": "sandbox; frame-ancestors 'none'"]
133
+ }
134
+
114
135
  open func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
115
136
  urlSchemeTask.stopped = true
116
137
  }
@@ -143,7 +164,7 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
143
164
  func handleCapacitorHttpRequest(_ urlSchemeTask: WKURLSchemeTask, _ localUrl: URL, _ isHttpsRequest: Bool) {
144
165
  var urlRequest = urlSchemeTask.request
145
166
  guard let url = urlRequest.url else { return }
146
-
167
+
147
168
  let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
148
169
  if let targetUrl = urlComponents?.queryItems?.first(where: { $0.name == CapacitorBridge.httpInterceptorUrlParam })?.value,
149
170
  !targetUrl.isEmpty {
@@ -160,7 +181,10 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
160
181
  }
161
182
 
162
183
  if let response = response as? HTTPURLResponse {
163
- let existingHeaders = response.allHeaderFields
184
+ // The target's CORS grant is not forwarded, so another origin cannot read the proxied response.
185
+ let existingHeaders = response.allHeaderFields.filter {
186
+ ($0.key as? String)?.caseInsensitiveCompare("Access-Control-Allow-Origin") != .orderedSame
187
+ }
164
188
  var newHeaders: [AnyHashable: Any] = [:]
165
189
 
166
190
  // Nothing should render this. If anything does, sandbox keeps it inert.
@@ -76,8 +76,8 @@ open class WebViewDelegationHandler: NSObject, WKNavigationDelegate, WKUIDelegat
76
76
  return
77
77
  }
78
78
 
79
- // The proxy returns a remote body at the app origin, so block it before plugins can allow it.
80
- if navURL.path.starts(with: CapacitorBridge.httpInterceptorStartIdentifier) {
79
+ // The proxy routes return remote or local content at the app origin, so block them before plugins can allow it.
80
+ if CapacitorBridge.isInternalProxyPath(navURL.path) {
81
81
  decisionHandler(.cancel)
82
82
  return
83
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor/ios",
3
- "version": "6.2.2",
3
+ "version": "6.2.4",
4
4
  "description": "Capacitor: Cross-platform apps with JavaScript and the web",
5
5
  "homepage": "https://capacitorjs.com",
6
6
  "author": "Ionic Team <hi@ionic.io> (https://ionic.io)",