Skip to content

Commit 93d904f

Browse files
chrisbobbegnprice
authored andcommitted
url [nfc]: Use isUrlOnRealm where the logic calls for it
Instead of repeating its implementation everywhere.
1 parent 75b1c4d commit 93d904f

File tree

7 files changed

+16
-8
lines changed

7 files changed

+16
-8
lines changed

src/notification/notifOpen.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { Account, GlobalThunkAction, Narrow, Stream, UserId, UserOrBot } fr
1111
import { topicNarrow, pm1to1NarrowFromUser, pmNarrowFromRecipients } from '../utils/narrow';
1212
import * as logging from '../utils/logging';
1313
import { fromPushNotificationIOS } from './extract';
14-
import { tryParseUrl } from '../utils/url';
14+
import { isUrlOnRealm, tryParseUrl } from '../utils/url';
1515
import { pmKeyRecipientsFromIds } from '../utils/recipient';
1616
import { makeUserId } from '../api/idTypes';
1717
import { getStreamsByName } from '../selectors';
@@ -49,7 +49,7 @@ export const getAccountFromNotificationData = (
4949

5050
const urlMatches = [];
5151
accounts.forEach((account, i) => {
52-
if (account.realm.origin === realmUrl.origin) {
52+
if (isUrlOnRealm(account.realm, realmUrl)) {
5353
urlMatches.push(i);
5454
}
5555
});

src/sentry.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import isAppOwnDomain from './isAppOwnDomain';
1010
import store from './boot/store';
1111
import { getAccountStatuses } from './account/accountsSelectors';
1212
import { sentryKey } from './sentryConfig';
13+
import { isUrlOnRealm } from './utils/url';
1314

1415
export const isSentryActive = (): boolean => {
1516
// Hub#getClient() is documented as possibly returning undefined, but the
@@ -81,7 +82,7 @@ function shouldScrubHost(url: URL, accountStatuses: $ReadOnlyArray<AccountStatus
8182
return true;
8283
}
8384

84-
if (accountStatuses.some(({ realm }) => url.origin === realm.origin)) {
85+
if (accountStatuses.some(({ realm }) => isUrlOnRealm(url, realm))) {
8586
// Definitely a request to a Zulip realm. Will catch requests to realms
8687
// in the account-statuses state, including those without `/api/v1`,
8788
// like `/avatar/{user_id}` (zulip/zulip@0f9970fd3 confirms that this

src/start/webAuth.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as WebBrowser from 'expo-web-browser';
44

55
import type { Auth } from '../types';
66
import { openLinkEmbedded } from '../utils/openLink';
7-
import { tryParseUrl } from '../utils/url';
7+
import { isUrlOnRealm, tryParseUrl } from '../utils/url';
88
import { base64ToHex, hexToAscii, xorHexStrings } from '../utils/encoding';
99

1010
/*
@@ -78,7 +78,7 @@ export const authFromCallbackUrl = (callbackUrl: string, otp: string, realm: URL
7878
return null;
7979
}
8080
const callbackRealm = tryParseUrl(callbackRealmStr);
81-
if (!callbackRealm || callbackRealm.origin !== realm.origin) {
81+
if (!callbackRealm || !isUrlOnRealm(callbackRealm, realm)) {
8282
return null;
8383
}
8484

src/utils/internalLinks.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Narrow, Stream, UserId } from '../types';
66
import { topicNarrow, streamNarrow, specialNarrow, pmNarrowFromRecipients } from './narrow';
77
import { pmKeyRecipientsFromIds } from './recipient';
88
import { ensureUnreachable } from '../generics';
9+
import { isUrlOnRealm } from './url';
910

1011
/**
1112
* For narrow URL https://zulip.example/#narrow/foo/bar, split the part of
@@ -54,7 +55,7 @@ const getHashSegmentsFromNarrowLink = (url: string, realm: URL) => {
5455
export const isNarrowLink = (url: string, realm: URL): boolean => {
5556
const resolved = new URL(url, realm);
5657
return (
57-
resolved.origin === realm.origin
58+
isUrlOnRealm(resolved, realm)
5859
&& resolved.pathname === '/'
5960
&& resolved.search === ''
6061
&& /^#narrow\//i.test(resolved.hash)

src/utils/url.js

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export const tryParseUrl = (url: string, base?: string | URL): URL | void => {
9292
/**
9393
* Test if the given URL is within the given realm.
9494
*/
95+
// This is used in and out of the WebView, so it must work with the native
96+
// URL implementation and also the polyfill we use in React Native land.
9597
export const isUrlOnRealm = (url: URL, realm: URL): boolean => url.origin === realm.origin;
9698

9799
export const getFileExtension = (filename: string): string => filename.split('.').pop();

src/webview/js/generatedEs3.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ var compiledWebviewJs = (function (exports) {
170170
}
171171
}
172172
173+
const isUrlOnRealm = (url, realm) => url.origin === realm.origin;
174+
173175
const inlineApiRoutes = ['^/user_uploads/', '^/thumbnail$', '^/avatar/'].map(r => new RegExp(r));
174176
175177
const rewriteImageUrls = (auth, element) => {
@@ -191,7 +193,7 @@ var compiledWebviewJs = (function (exports) {
191193
return;
192194
}
193195
194-
if (fixedSrc.origin === realm.origin) {
196+
if (isUrlOnRealm(fixedSrc, realm)) {
195197
if (inlineApiRoutes.some(regexp => regexp.test(fixedSrc.pathname))) {
196198
const delimiter = fixedSrc.search ? '&' : '';
197199
fixedSrc.search += "".concat(delimiter, "api_key=").concat(auth.apiKey);

src/webview/js/rewriteHtml.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import type { Auth } from '../../types';
44

5+
import { isUrlOnRealm } from '../../utils/url';
6+
57
/** List of routes which accept the API key appended as a GET parameter. */
68
const inlineApiRoutes: $ReadOnlyArray<RegExp> = [
79
'^/user_uploads/',
@@ -49,7 +51,7 @@ const rewriteImageUrls = (auth: Auth, element: Element | Document) => {
4951
}
5052

5153
// 2: Inject the API key where needed.
52-
if (fixedSrc.origin === realm.origin) {
54+
if (isUrlOnRealm(fixedSrc, realm)) {
5355
if (inlineApiRoutes.some(regexp => regexp.test(fixedSrc.pathname))) {
5456
// Ideally we'd just use `searchParams`, but that was new in Chrome 51
5557
// (and Safari 10).

0 commit comments

Comments
 (0)