From dc543b5ae35338604e9ed9ddfa5bf8624fadb059 Mon Sep 17 00:00:00 2001 From: artrejo Date: Mon, 24 Jan 2022 11:08:37 -0800 Subject: [PATCH] Make allowedEndpoints a read-only array --- src/Common/MongoProxyClient.ts | 5 +-- src/ConfigContext.ts | 63 +++++---------------------------- src/Explorer/Explorer.tsx | 10 ++---- src/Utils/EndpointValidation.ts | 10 ++++-- 4 files changed, 20 insertions(+), 68 deletions(-) diff --git a/src/Common/MongoProxyClient.ts b/src/Common/MongoProxyClient.ts index 91218f866..1a0c75a4a 100644 --- a/src/Common/MongoProxyClient.ts +++ b/src/Common/MongoProxyClient.ts @@ -339,10 +339,7 @@ export function createMongoCollectionWithProxy( export function getFeatureEndpointOrDefault(feature: string): string { const endpoint = hasFlag(userContext.features.mongoProxyAPIs, feature) && - validateEndpoint( - userContext.features.mongoProxyEndpoint, - allowedMongoProxyEndpoints.map((endpoint) => endpoint) - ) + validateEndpoint(userContext.features.mongoProxyEndpoint, allowedMongoProxyEndpoints) ? userContext.features.mongoProxyEndpoint : configContext.MONGO_BACKEND_ENDPOINT || configContext.BACKEND_ENDPOINT; diff --git a/src/ConfigContext.ts b/src/ConfigContext.ts index 9a1ae0acc..e5544c3d4 100644 --- a/src/ConfigContext.ts +++ b/src/ConfigContext.ts @@ -95,66 +95,31 @@ export function updateConfigContext(newContext: Partial): void { return; } - if ( - !validateEndpoint( - newContext.ARM_ENDPOINT, - allowedArmEndpoints.map((endpoint) => endpoint) - ) - ) { + if (!validateEndpoint(newContext.ARM_ENDPOINT, allowedArmEndpoints)) { delete newContext.ARM_ENDPOINT; } - if ( - !validateEndpoint( - newContext.AAD_ENDPOINT, - allowedAadEndpoints.map((endpoint) => endpoint) - ) - ) { + if (!validateEndpoint(newContext.AAD_ENDPOINT, allowedAadEndpoints)) { delete newContext.AAD_ENDPOINT; } - if ( - !validateEndpoint( - newContext.EMULATOR_ENDPOINT, - allowedEmulatorEndpoints.map((endpoint) => endpoint) - ) - ) { + if (!validateEndpoint(newContext.EMULATOR_ENDPOINT, allowedEmulatorEndpoints)) { delete newContext.EMULATOR_ENDPOINT; } - if ( - !validateEndpoint( - newContext.GRAPH_ENDPOINT, - allowedGraphEndpoints.map((endpoint) => endpoint) - ) - ) { + if (!validateEndpoint(newContext.GRAPH_ENDPOINT, allowedGraphEndpoints)) { delete newContext.GRAPH_ENDPOINT; } - if ( - !validateEndpoint( - newContext.ARCADIA_ENDPOINT, - allowedArcadiaEndpoints.map((endpoint) => endpoint) - ) - ) { + if (!validateEndpoint(newContext.ARCADIA_ENDPOINT, allowedArcadiaEndpoints)) { delete newContext.ARCADIA_ENDPOINT; } - if ( - !validateEndpoint( - newContext.BACKEND_ENDPOINT, - allowedBackendEndpoints.map((endpoint) => endpoint) - ) - ) { + if (!validateEndpoint(newContext.BACKEND_ENDPOINT, allowedBackendEndpoints)) { delete newContext.BACKEND_ENDPOINT; } - if ( - !validateEndpoint( - newContext.MONGO_BACKEND_ENDPOINT, - allowedMongoBackendEndpoints.map((endpoint) => endpoint) - ) - ) { + if (!validateEndpoint(newContext.MONGO_BACKEND_ENDPOINT, allowedMongoBackendEndpoints)) { delete newContext.MONGO_BACKEND_ENDPOINT; } @@ -162,21 +127,11 @@ export function updateConfigContext(newContext: Partial): void { delete newContext.JUNO_ENDPOINT; } - if ( - !validateEndpoint( - newContext.hostedExplorerURL, - allowedHostedExplorerEndpoints.map((endpoint) => endpoint) - ) - ) { + if (!validateEndpoint(newContext.hostedExplorerURL, allowedHostedExplorerEndpoints)) { delete newContext.hostedExplorerURL; } - if ( - !validateEndpoint( - newContext.msalRedirectURI, - allowedMsalRedirectEndpoints.map((endpoint) => endpoint) - ) - ) { + if (!validateEndpoint(newContext.msalRedirectURI, allowedMsalRedirectEndpoints)) { delete newContext.msalRedirectURI; } diff --git a/src/Explorer/Explorer.tsx b/src/Explorer/Explorer.tsx index 136da15e8..08e9e8c98 100644 --- a/src/Explorer/Explorer.tsx +++ b/src/Explorer/Explorer.tsx @@ -181,10 +181,7 @@ export default class Explorer { // Override notebook server parameters from URL parameters if ( userContext.features.notebookServerUrl && - validateEndpoint( - userContext.features.notebookServerUrl, - allowedNotebookServerUrls.map((endpoint) => endpoint) - ) && + validateEndpoint(userContext.features.notebookServerUrl, allowedNotebookServerUrls) && userContext.features.notebookServerToken ) { useNotebook.getState().setNotebookServerInfo({ @@ -418,10 +415,7 @@ export default class Explorer { useNotebook.getState().setConnectionInfo(connectionStatus); useNotebook.getState().setNotebookServerInfo({ notebookServerEndpoint: - (validateEndpoint( - userContext.features.notebookServerUrl, - allowedNotebookServerUrls.map((endpoint) => endpoint) - ) && + (validateEndpoint(userContext.features.notebookServerUrl, allowedNotebookServerUrls) && userContext.features.notebookServerUrl) || connectionInfo.data.notebookServerUrl, authToken: userContext.features.notebookServerToken || connectionInfo.data.notebookAuthToken, diff --git a/src/Utils/EndpointValidation.ts b/src/Utils/EndpointValidation.ts index a231d1433..5328a72d6 100644 --- a/src/Utils/EndpointValidation.ts +++ b/src/Utils/EndpointValidation.ts @@ -1,8 +1,14 @@ import * as Logger from "../Common/Logger"; -export function validateEndpoint(endpointToValidate: string | undefined, allowedEndpoints: string[]): boolean { +export function validateEndpoint( + endpointToValidate: string | undefined, + allowedEndpoints: ReadonlyArray +): boolean { try { - return validateEndpointInternal(endpointToValidate, allowedEndpoints); + return validateEndpointInternal( + endpointToValidate, + allowedEndpoints.map((e) => e) + ); } catch (reason) { Logger.logError(`${endpointToValidate} not allowed`, "validateEndpoint"); Logger.logError(`${JSON.stringify(reason)}`, "validateEndpoint");