-
Notifications
You must be signed in to change notification settings - Fork 44
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
Clean up media type in blobToDataUrl #61
Comments
Would it be enough to URL-encode the type? I.e. 'data:' + encodeURIComponent(blob.type) + ';base64,' + base64String ? It seems to me that if a Blob (in this case a File) has whitespace or funky characters in its I don't believe it's the responsibility of |
OTOH if the |
Unfortunately, no. That would also percent-encode characters of the media type that we should keep intact, such as the slash and semicolons. According to the data URL spec quoted above, and especially when following the reasoning of this recently suggested erratum, each parameter value that is a quoted string should be stripped of its quotes and then percent-encoded where needed. This would indeed require some parsing (especially as a quoted value can again contain a quote if preceded by a backslash, according to the
Quite possibly I will have overlooked some complication; regexes do not usually suffice for correctly parsing things. In any case, I agree this should not be a responsability of blob-util. I hope somebody already packaged and published a correct implementation somewhere that could just be used here, or perhaps it’s time to make one. On NPM I can only find the modules attempting to do the reverse (e.g. parse-data-url, data-urls); not sure where else to search. ----
(presuming you meant …then, firstly, it would not be a valid media type parameter anyway as it lacks a
…and secondly, the current implementation would simply append a second Although making modules implement the details correctly seems worthwhile to avoid incompatibilities further down the line, I guess this might (at least for myself) not have the highest priority to dive even deeper into. Personally, I would probably just consider quoted media type parameters as unsupported for the time being, and fix the whitespace issue by just removing all whitespace from the type: |
This sounds like a tricky issue. I guess since blob-util is modular, it may be worth fixing. But your workaround also works fine and it seems like a bit of a edge-case, so maybe that's okay. I'm a bit on-the-fence about this. I'm also nervous about the proposed regex fix, unless it can really handle all possible edge cases (with tests to demonstrate 🙂). |
Currently,
blobToDataUrl
simply putsblob.type
into the URL (at this line), but it should be cleaned up to be valid:blob.type
may contain white space (e.g. I encountered a'data:image/png; qs=0.7;base64,iVB…'
from aResponse.blob()
in Firefox). I looked up its definition in the File API draft spec; which allows it to be any parseable mime type as defined in whatwg’s mime sniffing spec that bases on RFC 7231 §3.1.1.1 (HTTP media types) which indeed allows whitespace around semicolons. (confusingly however, that latter one is based on RFC 2046 which depends on RFC 2045, which in fact seems to not permit whitespace even though spaces do appear in its own examples… though I have not checked all its errata; in any case, fact is that white space can occur) And adata:
url obviously does not permit white space (see its syntax definition in RFC2397).As that data url spec (RFC2397) states, parameter values using quoted strings should instead be percentage-encoded:
Perhaps any other caveats?
I may tackle this myself sometime. For now I thought to just raise the issue already and park my research notes here. :)
The text was updated successfully, but these errors were encountered: