From 42bb801099ea16ff54fd7e7417249c6fe647c09c Mon Sep 17 00:00:00 2001 From: Kamal Joshi Date: Sat, 2 Nov 2024 00:14:09 +0530 Subject: [PATCH] try to fix deep linking to make sure only supported pattern is opened in app --- ApplicationRoot.js | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/ApplicationRoot.js b/ApplicationRoot.js index 2572a1ec..c01ebbb9 100644 --- a/ApplicationRoot.js +++ b/ApplicationRoot.js @@ -63,25 +63,33 @@ export default function App() { const urlParts = url.split('?'); const path = urlParts[0].split('/'); const colorsPart = path[path.length - 1]; - - // Split the colors part by `-` to get the color codes - const colors = colorsPart.split('-'); - - // Parse the query parameters - const queryParamsString = urlParts[1] || ''; - const queryParams = {}; - queryParamsString.split('&').forEach((param) => { - const [key, value] = param.split('='); - queryParams[key] = decodeURIComponent(value); - }); - - const suggestedName = queryParams['name']; - navigationRef.navigate('SavePalette', { - colors: colors.map((color) => { - return { color: '#' + color }; - }), - suggestedName: suggestedName - }); + + // Validate if colorsPart matches a hex color pattern (e.g., FFFFFF-123456) + const isValidPattern = /^([0-9A-Fa-f]{6})(-[0-9A-Fa-f]{6})*$/.test(colorsPart); + + if (isValidPattern) { + // Split the colors part by `-` to get the color codes + const colors = colorsPart.split('-'); + + // Parse the query parameters + const queryParamsString = urlParts[1] || ''; + const queryParams = {}; + queryParamsString.split('&').forEach((param) => { + const [key, value] = param.split('='); + queryParams[key] = decodeURIComponent(value); + }); + + const suggestedName = queryParams['name']; + navigationRef.navigate('SavePalette', { + colors: colors.map((color) => { + return { color: '#' + color }; + }), + suggestedName: suggestedName, + }); + } else { + // Open URL in the default browser if pattern does not match + Linking.openURL(url); + } } catch (error) { notifyMessage('Error parsing url: ' + error.message); navigationRef.navigate(ROUTE_NAMES.HOME);