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

Use URLSearchParams instead of transitive dependency querystring #13138

Merged
merged 9 commits into from
Jul 16, 2021
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
"jest": {
"testEnvironment": "jest-environment-jsdom-sixteen",
"testMatch": [
"<rootDir>/test/**/*-test.js"
"<rootDir>/test/**/*-test.[tj]s"
],
"setupFilesAfterEnv": [
"<rootDir>/node_modules/matrix-react-sdk/test/setupTests.js"
Expand Down
14 changes: 7 additions & 7 deletions src/vector/jitsi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
// We have to trick webpack into loading our CSS for us.
require("./index.scss");

import * as qs from 'querystring';
import { KJUR } from 'jsrsasign';
import {
IOpenIDCredentials,
Expand Down Expand Up @@ -52,15 +51,16 @@ let meetApi: any; // JitsiMeetExternalAPI

(async function() {
try {
// The widget's options are encoded into the fragment to avoid leaking info to the server. The widget
// spec on the other hand requires the widgetId and parentUrl to show up in the regular query string.
const widgetQuery = qs.parse(window.location.hash.substring(1));
const query = Object.assign({}, qs.parse(window.location.search.substring(1)), widgetQuery);
// The widget's options are encoded into the fragment to avoid leaking info to the server.
const widgetQuery = new URLSearchParams(window.location.hash.substring(1));
// The widget spec on the other hand requires the widgetId and parentUrl to show up in the regular query string.
const realQuery = new URLSearchParams(window.location.search.substring(1));
const qsParam = (name: string, optional = false): string => {
if (!optional && (!query[name] || typeof (query[name]) !== 'string')) {
const vals = widgetQuery.has(name) ? widgetQuery.getAll(name) : realQuery.getAll(name);
if (!optional && vals.length !== 1) {
throw new Error(`Expected singular ${name} in query string`);
}
return <string>query[name];
return <string>vals[0];
};

// If we have these params, expect a widget API to be available (ie. to be in an iframe
Expand Down
10 changes: 5 additions & 5 deletions src/vector/url_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import * as qs from 'querystring';
import { QueryDict, decodeParams } from "matrix-js-sdk/src/utils";

// We want to support some name / value pairs in the fragment
// so we're re-using query string like format
Expand All @@ -32,15 +32,15 @@ export function parseQsFromFragment(location: Location) {

const result = {
location: decodeURIComponent(hashparts[0]),
params: <qs.ParsedUrlQuery>{},
params: <QueryDict>{},
};

if (hashparts.length > 1) {
result.params = qs.parse(hashparts[1]);
result.params = decodeParams(hashparts[1]);
}
return result;
}

export function parseQs(location: Location) {
return qs.parse(location.search.substring(1));
export function parseQs(location: Location): QueryDict {
return decodeParams(location.search.substring(1));
}
50 changes: 50 additions & 0 deletions test/unit-tests/url_utils-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2020 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { parseQsFromFragment, parseQs } from "../../src/vector/url_utils";

describe("url_utils.ts", function() {
// @ts-ignore
const location: Location = {
hash: "",
search: "",
};

it("parseQsFromFragment", function() {
location.hash = "/home?foo=bar";
expect(parseQsFromFragment(location)).toEqual({
location: "home",
params: {
"foo": "bar",
},
});
});

describe("parseQs", function() {
location.search = "?foo=bar";
expect(parseQs(location)).toEqual({
"foo": "bar",
});
});

describe("parseQs with arrays", function() {
location.search = "?via=s1&via=s2&via=s2&foo=bar";
expect(parseQs(location)).toEqual({
"via": ["s1", "s2", "s2"],
"foo": "bar",
});
});
});