From adc28d2d74eaad9fa940f1e07fbed70d754ea621 Mon Sep 17 00:00:00 2001 From: Nicolas MASSART Date: Thu, 29 Aug 2024 14:05:06 +0200 Subject: [PATCH 1/3] remove cookies for external links --- app/actions/browser/index.js | 4 +++- app/components/Views/Browser/index.js | 11 ++++++----- app/components/Views/BrowserTab/index.js | 24 ++++++++++++++++++++++-- app/reducers/browser/index.js | 9 ++++++++- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/app/actions/browser/index.js b/app/actions/browser/index.js index 2d21c3a2182..1148ca544df 100644 --- a/app/actions/browser/index.js +++ b/app/actions/browser/index.js @@ -68,11 +68,13 @@ export function closeAllTabs() { * Creates a new tab * * @param {string} url - The website's url + * @param {string} linkType - optional link type */ -export function createNewTab(url) { +export function createNewTab(url, linkType) { return { type: 'CREATE_NEW_TAB', url, + linkType, id: Date.now(), }; } diff --git a/app/components/Views/Browser/index.js b/app/components/Views/Browser/index.js index f67f0700bd4..9aadd2a9681 100644 --- a/app/components/Views/Browser/index.js +++ b/app/components/Views/Browser/index.js @@ -32,7 +32,7 @@ import getAccountNameWithENS from '../../../util/accounts'; import Device from '../../../util/device'; import { useTheme } from '../../../util/theme'; import Tabs from '../../UI/Tabs'; -import BrowserTab from '../BrowserTab'; +import BrowserTab, { EXTERNAL_LINK_TYPE } from '../BrowserTab'; import { isEqual } from 'lodash'; import URL from 'url-parse'; @@ -114,8 +114,8 @@ export const Browser = (props) => { [navigation, route, colors], ); - const newTab = (url) => { - createNewTab(url || AppConstants.HOMEPAGE_URL); + const newTab = (url, linkType) => { + createNewTab(url || AppConstants.HOMEPAGE_URL, linkType); }; const updateTabInfo = (url, tabID) => @@ -230,7 +230,7 @@ export const Browser = (props) => { const existingTabId = route.params?.existingTabId; if (newTabUrl && deeplinkTimestamp) { // Open url from deeplink. - newTab(newTabUrl); + newTab(newTabUrl, EXTERNAL_LINK_TYPE); } else if (existingTabId) { const existingTab = tabs.find((tab) => tab.id === existingTabId); if (existingTab) { @@ -359,6 +359,7 @@ export const Browser = (props) => { id={tab.id} key={`tab_${tab.id}`} initialUrl={tab.url || AppConstants.HOMEPAGE_URL} + linkType={tab.linkType} updateTabInfo={updateTabInfo} showTabs={showTabs} newTab={newTab} @@ -384,7 +385,7 @@ const mapStateToProps = (state) => ({ }); const mapDispatchToProps = (dispatch) => ({ - createNewTab: (url) => dispatch(createNewTab(url)), + createNewTab: (url, linkType) => dispatch(createNewTab(url, linkType)), closeAllTabs: () => dispatch(closeAllTabs()), closeTab: (id) => dispatch(closeTab(id)), setActiveTab: (id) => dispatch(setActiveTab(id)), diff --git a/app/components/Views/BrowserTab/index.js b/app/components/Views/BrowserTab/index.js index 713ab39a8fe..3bee75085a0 100644 --- a/app/components/Views/BrowserTab/index.js +++ b/app/components/Views/BrowserTab/index.js @@ -1,4 +1,10 @@ -import React, { useState, useRef, useEffect, useCallback } from 'react'; +import React, { + useState, + useRef, + useEffect, + useCallback, + useMemo, +} from 'react'; import { Text, StyleSheet, @@ -254,6 +260,8 @@ const createStyles = (colors, shadows) => const sessionENSNames = {}; const ensIgnoreList = []; +export const EXTERNAL_LINK_TYPE = 'external-link'; + export const BrowserTab = (props) => { const [backEnabled, setBackEnabled] = useState(false); const [forwardEnabled, setForwardEnabled] = useState(false); @@ -1490,6 +1498,11 @@ export const BrowserTab = (props) => { ); + const isExternalLink = useMemo( + () => props.linkType === EXTERNAL_LINK_TYPE, + [props.linkType], + ); + /** * Main render */ @@ -1517,7 +1530,10 @@ export const BrowserTab = (props) => { renderError={() => ( )} - source={{ uri: initialUrl }} + source={{ + uri: initialUrl, + ...(isExternalLink ? { headers: { Cookie: '' } } : null), + }} injectedJavaScriptBeforeContentLoaded={entryScriptWeb3} style={styles.webview} onLoadStart={onLoadStart} @@ -1562,6 +1578,10 @@ BrowserTab.propTypes = { * InitialUrl */ initialUrl: PropTypes.string, + /** + * linkType - type of link to open + */ + linkType: PropTypes.string, /** * Protocol string to append to URLs that have none */ diff --git a/app/reducers/browser/index.js b/app/reducers/browser/index.js index fec7595d4d2..0ba6887ab35 100644 --- a/app/reducers/browser/index.js +++ b/app/reducers/browser/index.js @@ -51,7 +51,14 @@ const browserReducer = (state = initialState, action) => { case 'CREATE_NEW_TAB': return { ...state, - tabs: [...state.tabs, { url: action.url, id: action.id }], + tabs: [ + ...state.tabs, + { + url: action.url, + ...(action.linkType && { linkType: action.linkType }), + id: action.id, + }, + ], }; case 'CLOSE_TAB': return { From e5121fae97ddbe85657de32170ede40324de258c Mon Sep 17 00:00:00 2001 From: Nicolas MASSART Date: Thu, 29 Aug 2024 20:49:26 +0200 Subject: [PATCH 2/3] move constant to own file update comments about deeplinks --- app/components/UI/CollectibleModal/CollectibleModal.tsx | 2 ++ app/components/Views/Browser/index.js | 9 +++++---- app/components/Views/BrowserTab/index.js | 3 +-- app/constants/browser.ts | 2 ++ app/core/DeeplinkManager/Handlers/handleBrowserUrl.ts | 2 ++ 5 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 app/constants/browser.ts diff --git a/app/components/UI/CollectibleModal/CollectibleModal.tsx b/app/components/UI/CollectibleModal/CollectibleModal.tsx index 123a7424da9..118aa84f13f 100644 --- a/app/components/UI/CollectibleModal/CollectibleModal.tsx +++ b/app/components/UI/CollectibleModal/CollectibleModal.tsx @@ -28,6 +28,7 @@ import { MetaMetricsEvents } from '../../../core/Analytics'; import { selectChainId } from '../../../selectors/networkController'; import { getDecimalChainId } from '../../../util/networks'; import { Nft } from '@metamask/assets-controllers'; +import { EXTERNAL_LINK_TYPE } from '../../../constants/browser'; const CollectibleModal = () => { const navigation = useNavigation(); @@ -92,6 +93,7 @@ const CollectibleModal = () => { screen: Routes.BROWSER_VIEW, params: { newTabUrl: url, + linkType: EXTERNAL_LINK_TYPE, timestamp: Date.now(), }, }); diff --git a/app/components/Views/Browser/index.js b/app/components/Views/Browser/index.js index 9aadd2a9681..3ffa49e3f46 100644 --- a/app/components/Views/Browser/index.js +++ b/app/components/Views/Browser/index.js @@ -32,7 +32,7 @@ import getAccountNameWithENS from '../../../util/accounts'; import Device from '../../../util/device'; import { useTheme } from '../../../util/theme'; import Tabs from '../../UI/Tabs'; -import BrowserTab, { EXTERNAL_LINK_TYPE } from '../BrowserTab'; +import BrowserTab from '../BrowserTab'; import { isEqual } from 'lodash'; import URL from 'url-parse'; @@ -67,6 +67,7 @@ export const Browser = (props) => { const { trackEvent } = useMetrics(); const { toastRef } = useContext(ToastContext); const browserUrl = props.route?.params?.url; + const linkType = props.route?.params?.linkType; const prevSiteHostname = useRef(browserUrl); const { accounts, ensByAccountAddress } = useAccounts(); const accountAvatarType = useSelector((state) => @@ -222,15 +223,15 @@ export const Browser = (props) => { [tabs], ); - // Handle deeplinks. + // Handle links with associated timestamp. useEffect( () => { const newTabUrl = route.params?.newTabUrl; const deeplinkTimestamp = route.params?.timestamp; const existingTabId = route.params?.existingTabId; if (newTabUrl && deeplinkTimestamp) { - // Open url from deeplink. - newTab(newTabUrl, EXTERNAL_LINK_TYPE); + // Open url from link. + newTab(newTabUrl, linkType); } else if (existingTabId) { const existingTab = tabs.find((tab) => tab.id === existingTabId); if (existingTab) { diff --git a/app/components/Views/BrowserTab/index.js b/app/components/Views/BrowserTab/index.js index 3bee75085a0..31e88cf430d 100644 --- a/app/components/Views/BrowserTab/index.js +++ b/app/components/Views/BrowserTab/index.js @@ -109,6 +109,7 @@ import { trackDappViewedEvent } from '../../../util/metrics'; import trackErrorAsAnalytics from '../../../util/metrics/TrackError/trackErrorAsAnalytics'; import { selectPermissionControllerState } from '../../../selectors/snaps/permissionController'; import { isTest } from '../../../util/test/utils.js'; +import { EXTERNAL_LINK_TYPE } from '../../../constants/browser'; const { HOMEPAGE_URL, NOTIFICATION_NAMES } = AppConstants; const HOMEPAGE_HOST = new URL(HOMEPAGE_URL)?.hostname; @@ -260,8 +261,6 @@ const createStyles = (colors, shadows) => const sessionENSNames = {}; const ensIgnoreList = []; -export const EXTERNAL_LINK_TYPE = 'external-link'; - export const BrowserTab = (props) => { const [backEnabled, setBackEnabled] = useState(false); const [forwardEnabled, setForwardEnabled] = useState(false); diff --git a/app/constants/browser.ts b/app/constants/browser.ts new file mode 100644 index 00000000000..8ca50d12576 --- /dev/null +++ b/app/constants/browser.ts @@ -0,0 +1,2 @@ +// eslint-disable-next-line import/prefer-default-export +export const EXTERNAL_LINK_TYPE = 'external-link'; diff --git a/app/core/DeeplinkManager/Handlers/handleBrowserUrl.ts b/app/core/DeeplinkManager/Handlers/handleBrowserUrl.ts index 2a130f8d6eb..182a99ad9ad 100644 --- a/app/core/DeeplinkManager/Handlers/handleBrowserUrl.ts +++ b/app/core/DeeplinkManager/Handlers/handleBrowserUrl.ts @@ -1,6 +1,7 @@ import Routes from '../../../constants/navigation/Routes'; import { InteractionManager } from 'react-native'; import DeeplinkManager from '../DeeplinkManager'; +import { EXTERNAL_LINK_TYPE } from '../../../constants/browser'; function handleBrowserUrl({ deeplinkManager, @@ -19,6 +20,7 @@ function handleBrowserUrl({ screen: Routes.BROWSER.VIEW, params: { newTabUrl: url, + linkType: EXTERNAL_LINK_TYPE, timestamp: Date.now(), }, }); From 75312a7b9c8b5b57beebe5fb29797588236e0c2f Mon Sep 17 00:00:00 2001 From: Nicolas MASSART Date: Tue, 3 Sep 2024 17:00:23 +0200 Subject: [PATCH 3/3] remove rule exception, not needed anymore --- app/constants/browser.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/app/constants/browser.ts b/app/constants/browser.ts index 8ca50d12576..2044f2f2ad5 100644 --- a/app/constants/browser.ts +++ b/app/constants/browser.ts @@ -1,2 +1 @@ -// eslint-disable-next-line import/prefer-default-export export const EXTERNAL_LINK_TYPE = 'external-link';