Skip to content

Commit

Permalink
fix: Fix data URI parsing when charset present
Browse files Browse the repository at this point in the history
Data URIs can have additional parameters other than just the MIME type
and encoding.  While working on TTML fixes, I was debugging by
inserting data URIs as TTML text tracks.  But the data URI encoder I
used was inserting a "charset" parameter into the URI, which was
rejected by Shaka Player.

This fixes the data URI parser to understand these extra parameters.

While researching this fix, I also found that only the base64 encoding
is specified for data URIs.  So now only that specific parameter is
supported as an encoding, and the UNKNOWN_DATA_URI_ENCODING error code
has been retired.

Change-Id: I7022529b0931e80a77a1120fc86c25b42e1fb3d9
  • Loading branch information
joeyparrish committed Aug 4, 2020
1 parent d9122a1 commit 5a03835
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
28 changes: 14 additions & 14 deletions lib/net/data_uri_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,30 @@ shaka.net.DataUriPlugin = class {
const info = infoAndData[0];
const dataStr = window.decodeURIComponent(infoAndData.slice(1).join(','));

// Extract the encoding (optional).
const typeAndEncoding = info.split(';');
let encoding = null;
if (typeAndEncoding.length > 1) {
encoding = typeAndEncoding[1];
// The MIME type is always the first thing in the semicolon-separated list
// of type parameters. It may be blank.
const typeInfoList = info.split(';');
const contentType = typeInfoList[0];

// Check for base64 encoding, which is always the last in the
// semicolon-separated list if present.
let base64Encoded = false;
if (typeInfoList.length > 1 &&
typeInfoList[typeInfoList.length - 1] == 'base64') {
base64Encoded = true;
typeInfoList.pop();
}

// Convert the data.
/** @type {BufferSource} */
let data;
if (encoding == 'base64') {
if (base64Encoded) {
data = shaka.util.Uint8ArrayUtils.fromBase64(dataStr);
} else if (encoding) {
shaka.log.error('Bad data URI, unknown encoding');
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.NETWORK,
shaka.util.Error.Code.UNKNOWN_DATA_URI_ENCODING,
uri);
} else {
data = shaka.util.StringUtils.toUTF8(dataStr);
}

return {data: data, contentType: typeAndEncoding[0]};
return {data: data, contentType};
}
};

Expand Down
6 changes: 1 addition & 5 deletions lib/util/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,7 @@ shaka.util.Error.Code = {
*/
'MALFORMED_DATA_URI': 1004,

/**
* A network request was made with a data URI using an unknown encoding.
* <br> error.data[0] is the URI.
*/
'UNKNOWN_DATA_URI_ENCODING': 1005,
// RETIRED: 'UNKNOWN_DATA_URI_ENCODING': 1005,

/**
* A request filter threw an error.
Expand Down
21 changes: 19 additions & 2 deletions test/net/data_uri_plugin_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,35 @@ describe('DataUriPlugin', () => {
'data:;base64,SGVsbG8sIFdvcmxkIQ%3D%3D', '', 'Hello, World!');
});

it('supports extra colin', async () => {
it('supports extra colon', async () => {
await testSucceeds('data:,Hello:', '', 'Hello:');
});

it('supports extra semi-colin', async () => {
it('supports extra semi-colon', async () => {
await testSucceeds('data:,Hello;', '', 'Hello;');
});

it('supports extra comma', async () => {
await testSucceeds('data:,Hello,', '', 'Hello,');
});

it('supports character set metadata', async () => {
await testSucceeds(
'data:text/plain;charset=UTF-8,Hello,', 'text/plain', 'Hello,');
});

it('supports arbitrary metadata', async () => {
await testSucceeds(
'data:text/plain;foo=bar,Hello,', 'text/plain', 'Hello,');
});

it('supports arbitrary metadata with base64 encoding', async () => {
await testSucceeds(
'data:text/plain;foo=bar;base64,SGVsbG8sIFdvcmxkIQ%3D%3D',
'text/plain',
'Hello, World!');
});

it('fails for empty URI', async () => {
await testFails('', shaka.util.Error.Code.MALFORMED_DATA_URI);
});
Expand Down

0 comments on commit 5a03835

Please sign in to comment.