Add client.SetMultipartBoundaryFunc and port Blink/WebKit/Firefox implementations #392
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.
Problem
req currently relies on Go's
mime/multipart
package to generate unique boundary delimiters formultipart/form-data
requests. The boundary delimiters generated bymime/multipart
are made up of exactly 60 randomly-generated characters (numbers from 0-9 and lower-case letters from a-f only), e.g.b26dc610159ba676929ced239b36e90370976ad0fc20a615e29e799608d2
(see implementation).All modern browsers use boundary delimiter formats different from those used by
mime/multipart
:----WebKitFormBoundary[a-zA-Z0-9]{16}
.-------------------------\d{1,10}\d{1,10}\d{1,10}
.It's very easy for bot detection tools to detect whether the boundary delimiter used in a
multipart/form-data
request could have originated from the browser indicated by the request'sUser-Agent
header, with a near-zero risk of false positives, and there's evidence that some websites have implemented such checks (e.g. Discord).Solution
This PR introduces a new
client.SetMultipartBoundaryFunc(fn func() string)
method, which accepts a custom function that's called every time a newmultipart/form-data
request is sent.Furthermore, it adds ports of the Blink/WebKit and Firefox implementations for boundary delimiter generation to
client.ImpersonateChrome()
,client.ImpersonateSafari()
andclient.ImpersonateFirefox()
.It also includes test cases for
client.SetMultipartBoundaryFunc
and the Blink/WebKit and Firefox ports.