Skip to content
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 double encoding URI components in ToneAudioBuffer #1169

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Tone/core/context/ToneAudioBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { optionsFromArguments } from "../util/Defaults";
import { noOp } from "../util/Interface";
import { isArray, isNumber, isString } from "../util/TypeCheck";
import { assert } from "../util/Debug";
import { encodeUnencodedURIComponent } from "../util/URI";

interface ToneAudioBufferOptions {
url?: string | AudioBuffer | ToneAudioBuffer;
Expand Down Expand Up @@ -398,7 +399,7 @@ export class ToneAudioBuffer extends Tone {
location.href = baseUrl + url;
location.pathname = (location.pathname + location.hash)
.split("/")
.map(encodeURIComponent)
.map(encodeUnencodedURIComponent)
.join("/");

const response = await fetch(location.href);
Expand Down
21 changes: 21 additions & 0 deletions Tone/core/util/URI.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from "chai";
import { encodeUnencodedURIComponent } from "./URI";

describe("URI", () => {
it("can encode a URI component without double-encoding", () => {
expect(encodeUnencodedURIComponent("")).to.equal("");
expect(encodeUnencodedURIComponent(" ")).to.equal("%20");
expect(encodeUnencodedURIComponent("%20")).to.equal("%20");
expect(encodeUnencodedURIComponent("%20 ")).to.equal("%20 "); // partially encoded strings are not encoded
expect(encodeUnencodedURIComponent("a")).to.equal("a");
expect(encodeUnencodedURIComponent("a%20b")).to.equal("a%20b");
expect(encodeUnencodedURIComponent("a b")).to.equal("a%20b");
expect(encodeUnencodedURIComponent("a+b")).to.equal("a%2Bb");
expect(encodeUnencodedURIComponent("a%2b")).to.equal("a%2b");
expect(encodeUnencodedURIComponent("something\t\telse")).to.equal("something%09%09else");
expect(encodeUnencodedURIComponent("something%09%09else")).to.equal("something%09%09else");
// Based on URL from issue reporter
const exampleUrl = "https://some.domain.com/v0/b/test.appspot.com/o/MyFolder%2FmusicInC%23ornot.mp3?alt=media&token=qwer123-qwer-asdf-1234-asdf324234";
expect(encodeUnencodedURIComponent(exampleUrl)).to.equal(exampleUrl);
});
});
14 changes: 14 additions & 0 deletions Tone/core/util/URI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const isURIComponentEncoded = (uriComponent: string): boolean =>
decodeURIComponent(uriComponent) !== uriComponent;

/**
* Encodes via `encodeURIComponent` only if the given string is not encoded.
* If the given string is already encoded, it will be returned as is.
*
* Note that this will not encode a partially encoded string such as `'%20 '`.
*
* @param uriComponent The URI component to encode
*/
export const encodeUnencodedURIComponent = (uriComponent: string): string => {
return isURIComponentEncoded(uriComponent) ? uriComponent : encodeURIComponent(uriComponent);
};