mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-19 18:08:14 +00:00
* fix(MM-70606): send the pre-auth secret over HTTPS only and fix Edit Server save - sanitizeUrl: useHttp no longer downgrades an explicit https:// URL, so getServerUrlAfterRedirect keeps an HTTPS redirect target and strips its query string. - Send the pre-auth secret header only to https/wss URLs: REST client default headers, per-request headers, WebSocket, and the HEAD probe. The secret is still persisted for the server. - Edit Server: try HTTPS first, then fall back to HTTP (as Add Server does). - Edit Server: recreate and reconnect the WebSocket client after the secret changes, so the new secret is used without an app restart. * fix(MM-70606): keep the persisted pre-auth secret on token refresh over HTTP ClientTracking stores the secret in its own field. The token refresh in doFetchWithTracking reads that field instead of the request header, which is only set over TLS, so a refresh on an http:// server no longer resets the saved secret. * fix(MM-70606): clarify when the pre-auth secret header is removed * test(MM-70606): cover the pre-auth header guard in NetworkManager.createClient
306 lines
9.6 KiB
TypeScript
306 lines
9.6 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import GenericClient from '@mattermost/react-native-network-client';
|
|
import {Linking} from 'react-native';
|
|
import urlParse from 'url-parse';
|
|
|
|
import * as ClientConstants from '@client/rest/constants';
|
|
import {Files} from '@constants';
|
|
import {emptyFunction} from '@utils/general';
|
|
import {logDebug} from '@utils/log';
|
|
|
|
import {latinise} from './latinise';
|
|
|
|
const ytRegex = /(?:http|https):\/\/(?:www\.|m\.)?(?:(?:youtube\.com\/(?:(?:v\/)|(?:(?:watch|embed\/watch)(?:\/|.*v=))|(?:embed\/)|(?:user\/[^/]+\/u\/[0-9]\/)))|(?:youtu\.be\/))([^#&?]*)/;
|
|
|
|
export function isValidUrl(url = '') {
|
|
const regex = /^https?:\/\//i;
|
|
return regex.test(url);
|
|
}
|
|
|
|
export function isUrlSafe(url: string): boolean {
|
|
let unescaped: string;
|
|
|
|
try {
|
|
unescaped = decodeURIComponent(url);
|
|
} catch {
|
|
unescaped = unescape(url);
|
|
}
|
|
|
|
unescaped = unescaped.replace(/[^\w:]/g, '').toLowerCase();
|
|
|
|
// eslint-disable-next-line no-script-url
|
|
return !unescaped.startsWith('javascript:') &&
|
|
!unescaped.startsWith('vbscript:') &&
|
|
!unescaped.startsWith('data:');
|
|
}
|
|
|
|
export function isParsableUrl(url: string): boolean {
|
|
try {
|
|
const parsedUrl = new URL(url);
|
|
return Boolean(parsedUrl);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isSecureUrl(url: string) {
|
|
return /^(https|wss):\/\//i.test(url);
|
|
}
|
|
|
|
// useHttp never downgrades a URL that explicitly uses https.
|
|
export function sanitizeUrl(url: string, useHttp = false) {
|
|
let preUrl = urlParse(url, true);
|
|
let protocol = useHttp && preUrl.protocol !== 'https:' ? 'http:' : preUrl.protocol;
|
|
|
|
if (!preUrl.host || preUrl.protocol === 'file:') {
|
|
preUrl = urlParse('https://' + stripTrailingSlashes(url), true);
|
|
}
|
|
|
|
if (preUrl.protocol === 'http:' && !useHttp) {
|
|
protocol = 'https:';
|
|
} else if (!protocol) {
|
|
protocol = useHttp ? 'http:' : 'https:';
|
|
}
|
|
|
|
return stripTrailingSlashes(
|
|
`${protocol}//${preUrl.host}${preUrl.pathname}`,
|
|
);
|
|
}
|
|
|
|
export async function getUrlAfterRedirect(url: string, useHttp = false) {
|
|
const link = sanitizeUrl(url, useHttp);
|
|
try {
|
|
const result = await fetch(link, {
|
|
method: 'HEAD',
|
|
});
|
|
return {url: result.url};
|
|
} catch (error) {
|
|
return {error};
|
|
}
|
|
}
|
|
|
|
export async function getServerUrlAfterRedirect(serverUrl: string, useHttp = false, preauthSecret?: string) {
|
|
let url = sanitizeUrl(serverUrl, useHttp);
|
|
|
|
const headers = {
|
|
...(preauthSecret && isSecureUrl(url)) ? {[ClientConstants.HEADER_X_MATTERMOST_PREAUTH_SECRET]: preauthSecret} : {},
|
|
};
|
|
|
|
try {
|
|
const resp = await GenericClient.head(url, {
|
|
headers,
|
|
});
|
|
if (resp.redirectUrls?.length) {
|
|
url = resp.redirectUrls[resp.redirectUrls.length - 1];
|
|
}
|
|
} catch (error) {
|
|
logDebug('getServerUrlAfterRedirect error', url, error);
|
|
return {error};
|
|
}
|
|
|
|
return {url: sanitizeUrl(url, useHttp)};
|
|
}
|
|
|
|
export function stripTrailingSlashes(url = '') {
|
|
return url.replace(/ /g, '').replace(/^\/+/, '').replace(/\/+$/, '');
|
|
}
|
|
|
|
export function removeProtocol(url = '') {
|
|
return url.replace(/(^\w+:|^)\/\//, '');
|
|
}
|
|
|
|
export function extractFirstLink(text: string) {
|
|
const pattern = /(^|[\s\n]|<br\/?>)((?:https?|ftp):\/\/[-A-Z0-9+\u0026\u2019@#/%?=()~_|!:,.;]*[-A-Z0-9+\u0026@#/%=~()_|])/i;
|
|
let inText = text;
|
|
|
|
// strip out code blocks
|
|
inText = inText.replace(/`[^`]*`/g, '');
|
|
|
|
// strip out inline markdown images
|
|
inText = inText.replace(/!\[[^\]]*]\([^)]*\)/g, '');
|
|
|
|
const match = pattern.exec(inText);
|
|
if (match) {
|
|
return match[0].trim();
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
export function extractStartLink(text: string) {
|
|
const pattern = /^((?:https?|ftp):\/\/[-A-Z0-9+\u0026\u2019@#/%?=()~_|!:,.;]*[-A-Z0-9+\u0026@#/%=~()_|])/i;
|
|
let inText = text;
|
|
|
|
// strip out code blocks
|
|
inText = inText.replace(/`[^`]*`/g, '');
|
|
|
|
// strip out inline markdown images
|
|
inText = inText.replace(/!\[[^\]]*]\([^)]*\)/g, '');
|
|
|
|
const match = pattern.exec(inText);
|
|
if (match) {
|
|
return match[0].trim();
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
export function isYoutubeLink(link: string) {
|
|
return link.trim().match(ytRegex);
|
|
}
|
|
|
|
export function isImageLink(link: string) {
|
|
let linkWithoutQuery = link;
|
|
if (link.indexOf('?') !== -1) {
|
|
linkWithoutQuery = linkWithoutQuery.split('?')[0];
|
|
}
|
|
|
|
for (let i = 0; i < Files.IMAGE_TYPES.length; i++) {
|
|
const imageType = Files.IMAGE_TYPES[i];
|
|
|
|
if (linkWithoutQuery.endsWith('.' + imageType) || linkWithoutQuery.endsWith('=' + imageType)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// Converts the protocol of a link (eg. http, ftp) to be lower case since
|
|
// Android doesn't handle uppercase links.
|
|
export function normalizeProtocol(url: string) {
|
|
const index = url.indexOf(':');
|
|
if (index === -1) {
|
|
// There's no protocol on the link to be normalized
|
|
return url;
|
|
}
|
|
|
|
const protocol = url.substring(0, index);
|
|
return protocol.toLowerCase() + url.substring(index);
|
|
}
|
|
|
|
export function getShortenedURL(url = '', getLength = 27) {
|
|
if (url.length > 35) {
|
|
const subLength = getLength - 14;
|
|
return url.substring(0, 10) + '...' + url.substring(url.length - subLength, url.length) + '/';
|
|
}
|
|
return url + '/';
|
|
}
|
|
|
|
export function cleanUpUrlable(input: string) {
|
|
let cleaned = latinise(input);
|
|
cleaned = cleaned.trim().replace(/-/g, ' ').replace(/[^\w\s]/gi, '').toLowerCase().replace(/\s/g, '-');
|
|
cleaned = cleaned.replace(/-{2,}/, '-');
|
|
cleaned = cleaned.replace(/^-+/, '');
|
|
cleaned = cleaned.replace(/-+$/, '');
|
|
return cleaned;
|
|
}
|
|
|
|
export function getScheme(url: string) {
|
|
const match = (/([a-z0-9+.-]+):/i).exec(url);
|
|
|
|
return match && match[1];
|
|
}
|
|
|
|
export function getYouTubeVideoId(link?: string) {
|
|
if (!link) {
|
|
return '';
|
|
}
|
|
|
|
// https://youtube.com/watch?v=<id>
|
|
let match = (/youtube\.com\/watch\?\S*\bv=([a-zA-Z0-9_-]{6,11})/g).exec(link);
|
|
if (match) {
|
|
return match[1];
|
|
}
|
|
|
|
// https://youtube.com/embed/<id>
|
|
match = (/youtube\.com\/embed\/([a-zA-Z0-9_-]{6,11})/g).exec(link);
|
|
if (match) {
|
|
return match[1];
|
|
}
|
|
|
|
// https://youtu.be/<id>
|
|
match = (/youtu.be\/([a-zA-Z0-9_-]{6,11})/g).exec(link);
|
|
if (match) {
|
|
return match[1];
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
export function tryOpenURL(url: string, onError: (error: unknown) => void = emptyFunction, onSuccess = emptyFunction) {
|
|
Linking.openURL(url).
|
|
then(onSuccess).
|
|
catch(onError);
|
|
}
|
|
|
|
// Given a URL from an API request, return a URL that has any parts removed that are either sensitive or that would
|
|
// prevent properly grouping the messages in Sentry.
|
|
export function cleanUrlForLogging(baseUrl: string, apiUrl: string): string {
|
|
let url = apiUrl;
|
|
|
|
// Trim the host name
|
|
url = url.substring(baseUrl.length);
|
|
|
|
// Filter the query string
|
|
const index = url.indexOf('?');
|
|
if (index !== -1) {
|
|
url = url.substring(0, index);
|
|
}
|
|
|
|
// A non-exhaustive whitelist to exclude parts of the URL that are unimportant (eg IDs) or may be sentsitive
|
|
// (eg email addresses). We prefer filtering out fields that aren't recognized because there should generally
|
|
// be enough left over for debugging.
|
|
//
|
|
// Note that new API routes don't need to be added here since this shouldn't be happening for newly added routes.
|
|
const whitelist = new Set([
|
|
'api', 'v4', 'users', 'teams', 'scheme', 'name', 'members', 'channels', 'posts', 'reactions', 'commands',
|
|
'files', 'preferences', 'hooks', 'incoming', 'outgoing', 'oauth', 'apps', 'emoji', 'brand', 'image',
|
|
'data_retention', 'jobs', 'plugins', 'roles', 'system', 'timezones', 'schemes', 'redirect_location', 'patch',
|
|
'mfa', 'password', 'reset', 'send', 'active', 'verify', 'terms_of_service', 'login', 'logout', 'ids',
|
|
'usernames', 'me', 'username', 'email', 'default', 'sessions', 'revoke', 'all', 'device', 'status',
|
|
'search', 'switch', 'authorized', 'authorize', 'deauthorize', 'tokens', 'disable', 'enable', 'exists', 'unread',
|
|
'invite', 'batch', 'stats', 'import', 'schemeRoles', 'direct', 'group', 'convert', 'view', 'search_autocomplete',
|
|
'thread', 'info', 'flagged', 'pinned', 'pin', 'unpin', 'opengraph', 'actions', 'thumbnail', 'preview', 'link',
|
|
'delete', 'logs', 'ping', 'config', 'client', 'license', 'websocket', 'webrtc', 'token', 'regen_token',
|
|
'autocomplete', 'execute', 'regen_secret', 'policy', 'type', 'cancel', 'reload', 'environment', 's3_test', 'file',
|
|
'caches', 'invalidate', 'database', 'recycle', 'compliance', 'reports', 'cluster', 'ldap', 'test', 'sync', 'saml',
|
|
'certificate', 'public', 'private', 'idp', 'elasticsearch', 'purge_indexes', 'analytics', 'old', 'webapp', 'fake',
|
|
]);
|
|
|
|
url = url.split('/').map((part) => {
|
|
if (part !== '' && !whitelist.has(part)) {
|
|
return '<filtered>';
|
|
}
|
|
|
|
return part;
|
|
}).join('/');
|
|
|
|
if (index !== -1) {
|
|
// Add this on afterwards since it wouldn't pass the whitelist
|
|
url += '?<filtered>';
|
|
}
|
|
|
|
return url;
|
|
}
|
|
|
|
export function extractFilenameFromUrl(url: string) {
|
|
const uri = urlParse(url);
|
|
return uri.pathname.split('/').pop();
|
|
}
|
|
|
|
export function safeDecodeURIComponent(v: string) {
|
|
try {
|
|
return decodeURIComponent(v);
|
|
} catch {
|
|
return v;
|
|
}
|
|
}
|
|
|
|
export function getUrlDomain(url: string): string {
|
|
const parsed = urlParse(url);
|
|
return parsed.hostname || url;
|
|
}
|