ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
12.21k stars 1.01k forks source link

bug: CapacitorHTTP crashes when a header value is given as number on iOS #6202

Closed nip3o closed 1 year ago

nip3o commented 1 year ago

Bug Report

Capacitor Version

💊 Capacitor Doctor 💊

Latest Dependencies:

@capacitor/cli: 4.6.1 @capacitor/core: 4.6.1 @capacitor/android: 4.6.1 @capacitor/ios: 4.6.1

Installed Dependencies:

@capacitor/android: not installed @capacitor/cli: 4.6.1 @capacitor/ios: 4.6.1 @capacitor/core: 4.6.1

[success] iOS looking great! 👌

Platform(s)

Only iOS

Current Behavior

Giving the value of a HTTP header as a number rather than as string causes the exception Could not cast value of type '__NSCFNumber' (0x122864c30) to 'NSString' (0x1b7cc7fc8). to be thrown when the value is unpacked in HttpRequestHandler.

fetch("https://www.google.com/", {
    headers: {
      thisIsFine: "1337",
      thisThrowsError: 1337,
    },
  })

Expected Behavior

The numeric value should be converted to a string. This is what the Android version of the plugin does, and what the Safari browser implementation of fetch() does. One could definitely argue that header values should always be passed in as strings, but I don't think it's helpful to crash on it. Either way, plugin code should not crash internally due to user errors.

Code Reproduction

https://github.com/nip3o/capacitor-http-headers-crash

Other Technical Details

npm --version output: 6.14.17

node --version output: v14.20.0

pod --version output (iOS issues only): 1.11.3

Additional Context

I did not get a stacktrace, but the crash happens in ios/Capacitor/Capacitor/Plugins/HttpRequestHandler.swift:148: let headers = (call.getObject("headers") ?? [:]) as! [String: String]

Could not cast value of type '__NSCFNumber' (0x122864c30) to 'NSString' (0x1b7cc7fc8).

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.