fix(audio): strict base64 body input decoding #120
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.
context:
javascripts libs tend to send as base64 what is not base64: it's
<rfc-2397-prefix>,<base64-payload>
https://stackoverflow.com/questions/20517429/parse-base64-string-coming-fron-javascript-front-end-data-uri-rfc2397
-> this is not valid base64 and we should just raise a bad request to that
what happened so far is that the python ends up sending the whole data into
https://docs.python.org/3/library/binascii.html#binascii.a2b_base64
and somehow manages to "decode" even non valid b64 string (how ? discarding non valid chars ? or just by taking the data byte by byte and stripping the 4 upper bits that should be null - but are not necessarily for non valid b64 chars - in every byte and concatenating the kept bits altogether to get the decoded data ? I don't know -> would need to reverse the hexdump)
so we ended up injecting decoded tainted data (garbage header + audio data) into the transformers lib
and somehow the python lib used by this one to parse audio data sometimes managed to recognize the data as proper audio (eg discard the garbage). For example it worked for mp3 but not flac data (don't know the mp3 struct but there must be some header that the lib expected, discarding all padding/garbage before)
in short the data injected into inference was not the same as the data originally sent by the user and we were lucky it answered sth (and even more it answered something relevant to what had been requested)
An (imo bad) alternative to this fix could be to be more flexible and parse rfc 2397 properly when found but there is no reason to call b64 what is not