-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix boundary value extraction for form-data requests #7518
Open
michaelwolz
wants to merge
10
commits into
ionic-team:main
Choose a base branch
from
michaelwolz:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+68
−5
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The changes introduced in ionic-team#7306 fixed several bugs with multipart/form-data requests on iOS. However, the extraction of the actual boundary value might fail due to the value being surrounded by double quotes, which is allowed and happens occasionally. Additionally, the `Content-Type` header might include other keys such as `charset`. This change extracts the boundary value regardless of the order of the individual keys within the `Content-Type` header.
Actually I just saw that the same issue exists with Android. I will try to also provide a fix for this :). Edit: Done in bc6a6e0, example app also updated to include android platform |
Applies changes made to iOS in 58045b0 to android platform
michaelwolz
commented
Jun 19, 2024
android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java
Outdated
Show resolved
Hide resolved
3 tasks
3 tasks
3 tasks
michaelwolz
changed the title
Fix boundary value extraction for form-data requests on iOS
Fix boundary value extraction for form-data requests on iOS / Android
Aug 14, 2024
michaelwolz
changed the title
Fix boundary value extraction for form-data requests on iOS / Android
Fix boundary value extraction for form-data requests
Aug 14, 2024
3 tasks
michaelwolz
commented
Aug 14, 2024
// If no boundary is provided, generate a random one and set the Content-Type header accordingly | ||
// or otherwise servers will not be able to parse the request body. Browsers do this automatically | ||
// but here we need to do this manually in order to comply with browser api behavior. | ||
boundary = UUID.randomUUID().toString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this part was missing in the iOS implementation but present for Android already.
This comment was marked as abuse.
This comment was marked as abuse.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The changes introduced in #7306 fixed several bugs with multipart/form-data requests on iOS. However, the extraction of the actual boundary value might fail due to the value being surrounded by double quotes, which is allowed and happens occasionally (See MDN Reference which utilizes double quotes for the boundary in their example: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST#example).
In addition, the
Content-Type
header might include other keys such ascharset
.This change extracts the boundary value regardless of the order of the individual keys within the
Content-Type
header.Example Code
(Assuming that the plugin override is enable in capacitor.config.ts)
The current implementation only considers the last string after any equal sign (
contentType.components(separatedBy: "=").last
). This results in--utf8
being set as the boundary in the request body. Even if there is no other key involved in the request it will include double quotes"
in the request body, leading to a malformed request.Working example including the changes can be found here:
https://github.com/michaelwolz/multipart-form-data-not-working-on-ios