Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix(web): simplify navigation validation to HTTP and HTTPS

AswinAsokb695767c7f14
diff --git a/web/apps/accounts/src/pages/passkeys/index.tsx b/web/apps/accounts/src/pages/passkeys/index.tsx
index f37a17e151..c8aebe3891 100644
--- a/web/apps/accounts/src/pages/passkeys/index.tsx
+++ b/web/apps/accounts/src/pages/passkeys/index.tsx
@@ -39,7 +39,7 @@ import { useBaseContext } from "ente-base/context";
import { isNamedError } from "ente-base/error";
import { formattedDateTime } from "ente-base/i18n-date";
import log from "ente-base/log";
-import { accountsNavigationURL } from "ente-base/safe-navigation";
+import { httpOrHTTPSURL } from "ente-base/safe-navigation";
import { useFormik } from "formik";
import { t } from "i18next";
import React, { useCallback, useEffect, useState } from "react";
@@ -87,7 +87,7 @@ const Page: React.FC = () => {
const { accountsUrl, passkeys } = await getPasskeys(token!);
const accountsURL = accountsUrl
- ? accountsNavigationURL(accountsUrl)
+ ? httpOrHTTPSURL(accountsUrl)
: undefined;
if (accountsURL && accountsURL.origin !== window.location.origin) {
const redirectURL = new URL(accountsURL.href);
diff --git a/web/packages/accounts/services/passkey.ts b/web/packages/accounts/services/passkey.ts
index 4bb762b454..06262f6a09 100644
--- a/web/packages/accounts/services/passkey.ts
+++ b/web/packages/accounts/services/passkey.ts
@@ -15,7 +15,7 @@ import {
publicRequestHeaders,
} from "ente-base/http";
import { apiURL } from "ente-base/origins";
-import { accountsNavigationURL } from "ente-base/safe-navigation";
+import { httpOrHTTPSURL } from "ente-base/safe-navigation";
import { z } from "zod";
import { unstashRedirect } from "./redirect";
@@ -37,7 +37,7 @@ export const passkeyVerificationRedirectURL = (
redirect,
...recoverOption,
});
- const url = accountsNavigationURL(accountsURL);
+ const url = httpOrHTTPSURL(accountsURL);
url.pathname = `${url.pathname.replace(/\/$/, "")}/passkeys/verify`;
url.search = params.toString();
url.hash = "";
@@ -55,7 +55,7 @@ export const openPasskeyVerificationURL = ({
passkeySessionID,
url,
}: OpenPasskeyVerificationURLOptions) => {
- const destination = accountsNavigationURL(url).href;
+ const destination = httpOrHTTPSURL(url).href;
// The finish page ignores redirects whose session id does not match the
// inflight value saved here.
@@ -83,7 +83,7 @@ export const openAccountsManagePasskeysPage = async (
await getAccountsTokenAndURL();
const params = new URLSearchParams({ token });
- const url = accountsNavigationURL(accountsURL);
+ const url = httpOrHTTPSURL(accountsURL);
url.pathname = `${url.pathname.replace(/\/$/, "")}/passkeys`;
url.search = params.toString();
url.hash = "";
diff --git a/web/packages/base/env.ts b/web/packages/base/env.ts
index 128b6cab28..a3b8d4c5fc 100644
--- a/web/packages/base/env.ts
+++ b/web/packages/base/env.ts
@@ -4,7 +4,6 @@ declare const process: {
readonly NODE_ENV: "development" | "production" | "test";
readonly NEXT_PUBLIC_ENSU_DESKTOP_VERSION?: string;
readonly NEXT_PUBLIC_ENTE_ENDPOINT?: string;
- readonly NEXT_PUBLIC_ENTE_ACCOUNTS_ORIGIN?: string;
readonly appName: string;
readonly desktopAppVersion?: string;
readonly gitSHA?: string;
@@ -15,9 +14,6 @@ declare const process: {
export const isDevBuild = process.env.NODE_ENV == "development";
export const buildEnvIsProductionBuild = process.env.NODE_ENV == "production";
-export const buildEnvAccountsOrigin =
- process.env.NEXT_PUBLIC_ENTE_ACCOUNTS_ORIGIN;
-
export const buildEnvCustomAPIEndpoint = process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
export const buildEnvEnsuDesktopVersion =
process.env.NEXT_PUBLIC_ENSU_DESKTOP_VERSION;
diff --git a/web/packages/base/safe-navigation.ts b/web/packages/base/safe-navigation.ts
index 6d80152dac..3a8c3a7c43 100644
--- a/web/packages/base/safe-navigation.ts
+++ b/web/packages/base/safe-navigation.ts
@@ -1,36 +1,8 @@
-import { buildEnvAccountsOrigin } from "./env";
-
-/** Parse an absolute server-supplied URL before using it as a destination. */
-export const trustedNavigationURL = (
- value: string,
- trustedOrigins: readonly string[],
- allowLocalHTTP = false,
-): URL => {
+/** Parse a navigation URL and allow only HTTP or HTTPS. */
+export const httpOrHTTPSURL = (value: string): URL => {
const url = new URL(value);
- const isLocalHTTP =
- allowLocalHTTP &&
- url.protocol === "http:" &&
- ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
- if (
- (url.protocol !== "https:" && !isLocalHTTP) ||
- url.username ||
- url.password ||
- !trustedOrigins.includes(url.origin)
- ) {
- throw new Error("Untrusted navigation URL");
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
+ throw new Error("Unsupported navigation URL protocol");
}
return url;
};
-
-/**
- * Use the configured Accounts origin, or the hosted origins for current and
- * legacy passkeys. Local HTTP is allowed only when explicitly configured.
- */
-export const accountsNavigationURL = (value: string): URL =>
- trustedNavigationURL(
- value,
- buildEnvAccountsOrigin
- ? [new URL(buildEnvAccountsOrigin).origin]
- : ["https://accounts.ente.com", "https://accounts.ente.io"],
- !!buildEnvAccountsOrigin,
- );
diff --git a/web/packages/new/photos/services/user-details.ts b/web/packages/new/photos/services/user-details.ts
index 0961271a49..fb7e5188e2 100644
--- a/web/packages/new/photos/services/user-details.ts
+++ b/web/packages/new/photos/services/user-details.ts
@@ -5,7 +5,7 @@ import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";
import { getKV, setKV } from "ente-base/kv";
import log from "ente-base/log";
import { apiURL } from "ente-base/origins";
-import { trustedNavigationURL } from "ente-base/safe-navigation";
+import { httpOrHTTPSURL } from "ente-base/safe-navigation";
import {
nullishToEmpty,
nullishToZero,
@@ -251,9 +251,7 @@ export const redirectToCustomerPortal = async () => {
);
ensureOk(res);
const portal = z.object({ url: z.string() }).parse(await res.json());
- window.location.href = trustedNavigationURL(portal.url, [
- "https://billing.stripe.com",
- ]).href;
+ window.location.href = httpOrHTTPSURL(portal.url).href;
};
export const isSubscriptionActive = (subscription: Subscription) =>