You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uri.ts 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. import { sanitizeUrl as _sanitizeUrl } from '@braintree/sanitize-url';
  2. import { parseURLParams } from './parseURLParams';
  3. import { normalizeNFKC } from './strings';
  4. /**
  5. * Http status codes.
  6. */
  7. export enum StatusCode {
  8. PaymentRequired = 402
  9. }
  10. /**
  11. * The app linking scheme.
  12. * TODO: This should be read from the manifest files later.
  13. */
  14. export const APP_LINK_SCHEME = 'org.jitsi.meet:';
  15. /**
  16. * A list of characters to be excluded/removed from the room component/segment
  17. * of a conference/meeting URI/URL. The list is based on RFC 3986 and the jxmpp
  18. * library utilized by jicofo.
  19. */
  20. const _ROOM_EXCLUDE_PATTERN = '[\\:\\?#\\[\\]@!$&\'()*+,;=></"]';
  21. /**
  22. * The {@link RegExp} pattern of the authority of a URI.
  23. *
  24. * @private
  25. * @type {string}
  26. */
  27. const _URI_AUTHORITY_PATTERN = '(//[^/?#]+)';
  28. /**
  29. * The {@link RegExp} pattern of the path of a URI.
  30. *
  31. * @private
  32. * @type {string}
  33. */
  34. const _URI_PATH_PATTERN = '([^?#]*)';
  35. /**
  36. * The {@link RegExp} pattern of the image data scheme.
  37. *
  38. * @private
  39. * @type {RegExp}
  40. */
  41. const IMG_DATA_URL: RegExp = /^data:image\/[a-z0-9\-.+]+;base64,/i;
  42. /**
  43. * The {@link RegExp} pattern of the protocol of a URI.
  44. *
  45. * FIXME: The URL class exposed by JavaScript will not include the colon in
  46. * the protocol field. Also in other places (at the time of this writing:
  47. * the DeepLinkingMobilePage.js) the APP_LINK_SCHEME does not include
  48. * the double dots, so things are inconsistent.
  49. *
  50. * @type {string}
  51. */
  52. export const URI_PROTOCOL_PATTERN = '^([a-z][a-z0-9\\.\\+-]*:)';
  53. /**
  54. * Excludes/removes certain characters from a specific path part which are
  55. * incompatible with Jitsi Meet on the client and/or server sides. The main
  56. * use case for this method is to clean up the room name and the tenant.
  57. *
  58. * @param {?string} pathPart - The path part to fix.
  59. * @private
  60. * @returns {?string}
  61. */
  62. function _fixPathPart(pathPart?: string) {
  63. return pathPart
  64. ? pathPart.replace(new RegExp(_ROOM_EXCLUDE_PATTERN, 'g'), '')
  65. : pathPart;
  66. }
  67. /**
  68. * Fixes the scheme part of a specific URI (string) so that it contains a
  69. * well-known scheme such as HTTP(S). For example, the mobile app implements an
  70. * app-specific URI scheme in addition to Universal Links. The app-specific
  71. * scheme may precede or replace the well-known scheme. In such a case, dealing
  72. * with the app-specific scheme only complicates the logic and it is simpler to
  73. * get rid of it (by translating the app-specific scheme into a well-known
  74. * scheme).
  75. *
  76. * @param {string} uri - The URI (string) to fix the scheme of.
  77. * @private
  78. * @returns {string}
  79. */
  80. function _fixURIStringScheme(uri: string) {
  81. const regex = new RegExp(`${URI_PROTOCOL_PATTERN}+`, 'gi');
  82. const match: Array<string> | null = regex.exec(uri);
  83. if (match) {
  84. // As an implementation convenience, pick up the last scheme and make
  85. // sure that it is a well-known one.
  86. let protocol = match[match.length - 1].toLowerCase();
  87. if (protocol !== 'http:' && protocol !== 'https:') {
  88. protocol = 'https:';
  89. }
  90. /* eslint-disable no-param-reassign */
  91. uri = uri.substring(regex.lastIndex);
  92. if (uri.startsWith('//')) {
  93. // The specified URL was not a room name only, it contained an
  94. // authority.
  95. uri = protocol + uri;
  96. }
  97. /* eslint-enable no-param-reassign */
  98. }
  99. return uri;
  100. }
  101. /**
  102. * Converts a path to a backend-safe format, by splitting the path '/' processing each part.
  103. * Properly lowercased and url encoded.
  104. *
  105. * @param {string?} path - The path to convert.
  106. * @returns {string?}
  107. */
  108. export function getBackendSafePath(path?: string): string | undefined {
  109. if (!path) {
  110. return path;
  111. }
  112. return path
  113. .split('/')
  114. .map(getBackendSafeRoomName)
  115. .join('/');
  116. }
  117. /**
  118. * Converts a room name to a backend-safe format. Properly lowercased and url encoded.
  119. *
  120. * @param {string?} room - The room name to convert.
  121. * @returns {string?}
  122. */
  123. export function getBackendSafeRoomName(room?: string): string | undefined {
  124. if (!room) {
  125. return room;
  126. }
  127. /* eslint-disable no-param-reassign */
  128. try {
  129. // We do not know if we get an already encoded string at this point
  130. // as different platforms do it differently, but we need a decoded one
  131. // for sure. However since decoding a non-encoded string is a noop, we're safe
  132. // doing it here.
  133. room = decodeURIComponent(room);
  134. } catch (e) {
  135. // This can happen though if we get an unencoded string and it contains
  136. // some characters that look like an encoded entity, but it's not.
  137. // But in this case we're fine going on...
  138. }
  139. // Normalize the character set.
  140. room = normalizeNFKC(room);
  141. // Only decoded and normalized strings can be lowercased properly.
  142. room = room?.toLowerCase();
  143. // But we still need to (re)encode it.
  144. room = encodeURIComponent(room ?? '');
  145. /* eslint-enable no-param-reassign */
  146. // Unfortunately we still need to lowercase it, because encoding a string will
  147. // add some uppercase characters, but some backend services
  148. // expect it to be full lowercase. However lowercasing an encoded string
  149. // doesn't change the string value.
  150. return room.toLowerCase();
  151. }
  152. /**
  153. * Gets the (Web application) context root defined by a specific location (URI).
  154. *
  155. * @param {Object} location - The location (URI) which defines the (Web
  156. * application) context root.
  157. * @public
  158. * @returns {string} - The (Web application) context root defined by the
  159. * specified {@code location} (URI).
  160. */
  161. export function getLocationContextRoot({ pathname }: { pathname: string; }) {
  162. const contextRootEndIndex = pathname.lastIndexOf('/');
  163. return (
  164. contextRootEndIndex === -1
  165. ? '/'
  166. : pathname.substring(0, contextRootEndIndex + 1));
  167. }
  168. /**
  169. * Constructs a new {@code Array} with URL parameter {@code String}s out of a
  170. * specific {@code Object}.
  171. *
  172. * @param {Object} obj - The {@code Object} to turn into URL parameter
  173. * {@code String}s.
  174. * @returns {Array<string>} The {@code Array} with URL parameter {@code String}s
  175. * constructed out of the specified {@code obj}.
  176. */
  177. function _objectToURLParamsArray(obj = {}) {
  178. const params = [];
  179. for (const key in obj) { // eslint-disable-line guard-for-in
  180. try {
  181. params.push(
  182. `${key}=${encodeURIComponent(JSON.stringify(obj[key as keyof typeof obj]))}`);
  183. } catch (e) {
  184. console.warn(`Error encoding ${key}: ${e}`);
  185. }
  186. }
  187. return params;
  188. }
  189. /**
  190. * Parses a specific URI string into an object with the well-known properties of
  191. * the {@link Location} and/or {@link URL} interfaces implemented by Web
  192. * browsers. The parsing attempts to be in accord with IETF's RFC 3986.
  193. *
  194. * @param {string} str - The URI string to parse.
  195. * @public
  196. * @returns {{
  197. * hash: string,
  198. * host: (string|undefined),
  199. * hostname: (string|undefined),
  200. * pathname: string,
  201. * port: (string|undefined),
  202. * protocol: (string|undefined),
  203. * search: string
  204. * }}
  205. */
  206. export function parseStandardURIString(str: string) {
  207. /* eslint-disable no-param-reassign */
  208. const obj: { [key: string]: any; } = {
  209. toString: _standardURIToString
  210. };
  211. let regex;
  212. let match: Array<string> | null;
  213. // XXX A URI string as defined by RFC 3986 does not contain any whitespace.
  214. // Usually, a browser will have already encoded any whitespace. In order to
  215. // avoid potential later problems related to whitespace in URI, strip any
  216. // whitespace. Anyway, the Jitsi Meet app is not known to utilize unencoded
  217. // whitespace so the stripping is deemed safe.
  218. str = str.replace(/\s/g, '');
  219. // protocol
  220. regex = new RegExp(URI_PROTOCOL_PATTERN, 'gi');
  221. match = regex.exec(str);
  222. if (match) {
  223. obj.protocol = match[1].toLowerCase();
  224. str = str.substring(regex.lastIndex);
  225. }
  226. // authority
  227. regex = new RegExp(`^${_URI_AUTHORITY_PATTERN}`, 'gi');
  228. match = regex.exec(str);
  229. if (match) {
  230. let authority: string = match[1].substring(/* // */ 2);
  231. str = str.substring(regex.lastIndex);
  232. // userinfo
  233. const userinfoEndIndex = authority.indexOf('@');
  234. if (userinfoEndIndex !== -1) {
  235. authority = authority.substring(userinfoEndIndex + 1);
  236. }
  237. obj.host = authority;
  238. // port
  239. const portBeginIndex = authority.lastIndexOf(':');
  240. if (portBeginIndex !== -1) {
  241. obj.port = authority.substring(portBeginIndex + 1);
  242. authority = authority.substring(0, portBeginIndex);
  243. }
  244. // hostname
  245. obj.hostname = authority;
  246. }
  247. // pathname
  248. regex = new RegExp(`^${_URI_PATH_PATTERN}`, 'gi');
  249. match = regex.exec(str);
  250. let pathname: string | undefined;
  251. if (match) {
  252. pathname = match[1];
  253. str = str.substring(regex.lastIndex);
  254. }
  255. if (pathname) {
  256. pathname.startsWith('/') || (pathname = `/${pathname}`);
  257. } else {
  258. pathname = '/';
  259. }
  260. obj.pathname = pathname;
  261. // query
  262. if (str.startsWith('?')) {
  263. let hashBeginIndex = str.indexOf('#', 1);
  264. if (hashBeginIndex === -1) {
  265. hashBeginIndex = str.length;
  266. }
  267. obj.search = str.substring(0, hashBeginIndex);
  268. str = str.substring(hashBeginIndex);
  269. } else {
  270. obj.search = ''; // Google Chrome
  271. }
  272. // fragment
  273. obj.hash = str.startsWith('#') ? str : '';
  274. /* eslint-enable no-param-reassign */
  275. return obj;
  276. }
  277. /**
  278. * Parses a specific URI which (supposedly) references a Jitsi Meet resource
  279. * (location).
  280. *
  281. * @param {(string|undefined)} uri - The URI to parse which (supposedly)
  282. * references a Jitsi Meet resource (location).
  283. * @public
  284. * @returns {{
  285. * contextRoot: string,
  286. * hash: string,
  287. * host: string,
  288. * hostname: string,
  289. * pathname: string,
  290. * port: string,
  291. * protocol: string,
  292. * room: (string|undefined),
  293. * search: string
  294. * }}
  295. */
  296. export function parseURIString(uri?: string): any {
  297. if (typeof uri !== 'string') {
  298. return undefined;
  299. }
  300. const obj = parseStandardURIString(_fixURIStringScheme(uri));
  301. // XXX While the components/segments of pathname are URI encoded, Jitsi Meet
  302. // on the client and/or server sides still don't support certain characters.
  303. obj.pathname = obj.pathname.split('/').map((pathPart: any) => _fixPathPart(pathPart))
  304. .join('/');
  305. // Add the properties that are specific to a Jitsi Meet resource (location)
  306. // such as contextRoot, room:
  307. // contextRoot
  308. // @ts-ignore
  309. obj.contextRoot = getLocationContextRoot(obj);
  310. // The room (name) is the last component/segment of pathname.
  311. const { pathname } = obj;
  312. const contextRootEndIndex = pathname.lastIndexOf('/');
  313. obj.room = pathname.substring(contextRootEndIndex + 1) || undefined;
  314. if (contextRootEndIndex > 1) {
  315. // The part of the pathname from the beginning to the room name is the tenant.
  316. obj.tenant = pathname.substring(1, contextRootEndIndex);
  317. }
  318. return obj;
  319. }
  320. /**
  321. * Implements {@code href} and {@code toString} for the {@code Object} returned
  322. * by {@link #parseStandardURIString}.
  323. *
  324. * @param {Object} [thiz] - An {@code Object} returned by
  325. * {@code #parseStandardURIString} if any; otherwise, it is presumed that the
  326. * function is invoked on such an instance.
  327. * @returns {string}
  328. */
  329. function _standardURIToString(thiz?: Object) {
  330. // @ts-ignore
  331. // eslint-disable-next-line @typescript-eslint/no-invalid-this
  332. const { hash, host, pathname, protocol, search } = thiz || this;
  333. let str = '';
  334. protocol && (str += protocol);
  335. // TODO userinfo
  336. host && (str += `//${host}`);
  337. str += pathname || '/';
  338. search && (str += search);
  339. hash && (str += hash);
  340. return str;
  341. }
  342. /**
  343. * Sometimes we receive strings that we don't know if already percent-encoded, or not, due to the
  344. * various sources we get URLs or room names. This function encapsulates the decoding in a safe way.
  345. *
  346. * @param {string} text - The text to decode.
  347. * @returns {string}
  348. */
  349. export function safeDecodeURIComponent(text: string) {
  350. try {
  351. return decodeURIComponent(text);
  352. } catch (e) {
  353. // The text wasn't encoded.
  354. }
  355. return text;
  356. }
  357. /**
  358. * Attempts to return a {@code String} representation of a specific
  359. * {@code Object} which is supposed to represent a URL. Obviously, if a
  360. * {@code String} is specified, it is returned. If a {@code URL} is specified,
  361. * its {@code URL#href} is returned. Additionally, an {@code Object} similar to
  362. * the one accepted by the constructor of Web's ExternalAPI is supported on both
  363. * mobile/React Native and Web/React.
  364. *
  365. * @param {Object|string} obj - The URL to return a {@code String}
  366. * representation of.
  367. * @returns {string} - A {@code String} representation of the specified
  368. * {@code obj} which is supposed to represent a URL.
  369. */
  370. export function toURLString(obj?: (Object | string)) {
  371. let str;
  372. switch (typeof obj) {
  373. case 'object':
  374. if (obj) {
  375. if (obj instanceof URL) {
  376. str = obj.href;
  377. } else {
  378. str = urlObjectToString(obj);
  379. }
  380. }
  381. break;
  382. case 'string':
  383. str = String(obj);
  384. break;
  385. }
  386. return str;
  387. }
  388. /**
  389. * Attempts to return a {@code String} representation of a specific
  390. * {@code Object} similar to the one accepted by the constructor
  391. * of Web's ExternalAPI.
  392. *
  393. * @param {Object} o - The URL to return a {@code String} representation of.
  394. * @returns {string} - A {@code String} representation of the specified
  395. * {@code Object}.
  396. */
  397. export function urlObjectToString(o: { [key: string]: any; }): string | undefined {
  398. // First normalize the given url. It come as o.url or split into o.serverURL
  399. // and o.room.
  400. let tmp;
  401. if (o.serverURL && o.room) {
  402. tmp = new URL(o.room, o.serverURL).toString();
  403. } else if (o.room) {
  404. tmp = o.room;
  405. } else {
  406. tmp = o.url || '';
  407. }
  408. const url = parseStandardURIString(_fixURIStringScheme(tmp));
  409. // protocol
  410. if (!url.protocol) {
  411. let protocol: string | undefined = o.protocol || o.scheme;
  412. if (protocol) {
  413. // Protocol is supposed to be the scheme and the final ':'. Anyway,
  414. // do not make a fuss if the final ':' is not there.
  415. protocol.endsWith(':') || (protocol += ':');
  416. url.protocol = protocol;
  417. }
  418. }
  419. // authority & pathname
  420. let { pathname } = url;
  421. if (!url.host) {
  422. // Web's ExternalAPI domain
  423. //
  424. // It may be host/hostname and pathname with the latter denoting the
  425. // tenant.
  426. const domain: string | undefined = o.domain || o.host || o.hostname;
  427. if (domain) {
  428. const { host, hostname, pathname: contextRoot, port }
  429. = parseStandardURIString(
  430. // XXX The value of domain in supposed to be host/hostname
  431. // and, optionally, pathname. Make sure it is not taken for
  432. // a pathname only.
  433. _fixURIStringScheme(`${APP_LINK_SCHEME}//${domain}`));
  434. // authority
  435. if (host) {
  436. url.host = host;
  437. url.hostname = hostname;
  438. url.port = port;
  439. }
  440. // pathname
  441. pathname === '/' && contextRoot !== '/' && (pathname = contextRoot);
  442. }
  443. }
  444. // pathname
  445. // Web's ExternalAPI roomName
  446. const room = o.roomName || o.room;
  447. if (room
  448. && (url.pathname.endsWith('/')
  449. || !url.pathname.endsWith(`/${room}`))) {
  450. pathname.endsWith('/') || (pathname += '/');
  451. pathname += room;
  452. }
  453. url.pathname = pathname;
  454. // query/search
  455. // Web's ExternalAPI jwt and lang
  456. const { jwt, lang, release } = o;
  457. const search = new URLSearchParams(url.search);
  458. // TODO: once all available versions are updated to support the jwt in the hash, remove this
  459. if (jwt) {
  460. search.set('jwt', jwt);
  461. }
  462. const { defaultLanguage } = o.configOverwrite || {};
  463. if (lang || defaultLanguage) {
  464. search.set('lang', lang || defaultLanguage);
  465. }
  466. if (release) {
  467. search.set('release', release);
  468. }
  469. const searchString = search.toString();
  470. if (searchString) {
  471. url.search = `?${searchString}`;
  472. }
  473. // fragment/hash
  474. let { hash } = url;
  475. if (jwt) {
  476. if (hash.length) {
  477. hash = `${hash}&jwt=${JSON.stringify(jwt)}`;
  478. } else {
  479. hash = `#jwt=${JSON.stringify(jwt)}`;
  480. }
  481. }
  482. for (const urlPrefix of [ 'config', 'iceServers', 'interfaceConfig', 'devices', 'userInfo', 'appData' ]) {
  483. const urlParamsArray
  484. = _objectToURLParamsArray(
  485. o[`${urlPrefix}Overwrite`]
  486. || o[urlPrefix]
  487. || o[`${urlPrefix}Override`]);
  488. if (urlParamsArray.length) {
  489. let urlParamsString
  490. = `${urlPrefix}.${urlParamsArray.join(`&${urlPrefix}.`)}`;
  491. if (hash.length) {
  492. urlParamsString = `&${urlParamsString}`;
  493. } else {
  494. hash = '#';
  495. }
  496. hash += urlParamsString;
  497. }
  498. }
  499. url.hash = hash;
  500. return url.toString() || undefined;
  501. }
  502. /**
  503. * Adds hash params to URL.
  504. *
  505. * @param {URL} url - The URL.
  506. * @param {Object} hashParamsToAdd - A map with the parameters to be set.
  507. * @returns {URL} - The new URL.
  508. */
  509. export function addHashParamsToURL(url: URL, hashParamsToAdd: Object = {}) {
  510. const params = parseURLParams(url);
  511. const urlParamsArray = _objectToURLParamsArray({
  512. ...params,
  513. ...hashParamsToAdd
  514. });
  515. if (urlParamsArray.length) {
  516. url.hash = `#${urlParamsArray.join('&')}`;
  517. }
  518. return url;
  519. }
  520. /**
  521. * Returns the decoded URI.
  522. *
  523. * @param {string} uri - The URI to decode.
  524. * @returns {string}
  525. */
  526. export function getDecodedURI(uri: string) {
  527. return decodeURI(uri.replace(/^https?:\/\//i, ''));
  528. }
  529. /**
  530. * Adds new param to a url string. Checks whether to use '?' or '&' as a separator (checks for already existing params).
  531. *
  532. * @param {string} url - The url to modify.
  533. * @param {string} name - The param name to add.
  534. * @param {string} value - The value for the param.
  535. *
  536. * @returns {string} - The modified url.
  537. */
  538. export function appendURLParam(url: string, name: string, value: string) {
  539. const newUrl = new URL(url);
  540. newUrl.searchParams.append(name, value);
  541. return newUrl.toString();
  542. }
  543. /**
  544. * Adds new hash param to a url string.
  545. * Checks whether to use '?' or '&' as a separator (checks for already existing params).
  546. *
  547. * @param {string} url - The url to modify.
  548. * @param {string} name - The param name to add.
  549. * @param {string} value - The value for the param.
  550. *
  551. * @returns {string} - The modified url.
  552. */
  553. export function appendURLHashParam(url: string, name: string, value: string) {
  554. const newUrl = new URL(url);
  555. const dummyUrl = new URL('https://example.com');
  556. // Copy current hash-parameters without the '#' as search-parameters.
  557. dummyUrl.search = newUrl.hash.substring(1);
  558. // Set or update value with the searchParams-API.
  559. dummyUrl.searchParams.append(name, value);
  560. // Write back as hash parameters.
  561. newUrl.hash = dummyUrl.searchParams.toString();
  562. return newUrl.toString();
  563. }
  564. /**
  565. * Sanitizes the given URL so that it's safe to use. If it's unsafe, null is returned.
  566. *
  567. * @param {string|URL} url - The URL that needs to be sanitized.
  568. *
  569. * @returns {URL?} - The sanitized URL, or null otherwise.
  570. */
  571. export function sanitizeUrl(url?: string | URL): URL | null {
  572. if (!url) {
  573. return null;
  574. }
  575. const urlStr = url.toString();
  576. const result = _sanitizeUrl(urlStr);
  577. if (result === 'about:blank') {
  578. return null;
  579. }
  580. return new URL(result);
  581. }
  582. /**
  583. * Check whether the given url is a valid image data url.
  584. *
  585. * @param {string} url - The url to check.
  586. * @returns {boolean} True if the url is a valid image data url, false otherwise.
  587. */
  588. export function isImageDataURL(url: string): boolean {
  589. return IMG_DATA_URL.test(url);
  590. }