From e555354e179901ff29f651bb949055cf1ff633f3 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sun, 3 Mar 2024 18:36:12 +0000 Subject: [PATCH 1/4] If TMDB key is empty, don't attempt request --- src/backend/metadata/tmdb.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index 2bae75d1..b143b312 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -144,12 +144,16 @@ export function decodeTMDBId( const baseURL = "https://api.themoviedb.org/3"; +const apiKey = conf().TMDB_READ_API_KEY; + const headers = { accept: "application/json", - Authorization: `Bearer ${conf().TMDB_READ_API_KEY}`, + Authorization: `Bearer ${apiKey}`, }; async function get(url: string, params?: object): Promise { + if (!apiKey) throw new Error("TMDB API key not set"); + const res = await mwFetch(encodeURI(url), { headers, baseURL, From b5604456596d2cf478b03e521445b04a0a14ae0f Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sun, 3 Mar 2024 18:37:28 +0000 Subject: [PATCH 2/4] Return null values where appr and handle the env being blank --- src/setup/config.ts | 58 ++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/setup/config.ts b/src/setup/config.ts index f081e673..26eb814e 100644 --- a/src/setup/config.ts +++ b/src/setup/config.ts @@ -31,10 +31,10 @@ export interface RuntimeConfig { DONATION_LINK: string; DISCORD_LINK: string; DMCA_EMAIL: string | null; - TMDB_READ_API_KEY: string; + TMDB_READ_API_KEY: string | null; NORMAL_ROUTER: boolean; PROXY_URLS: string[]; - BACKEND_URL: string; + BACKEND_URL: string | null; DISALLOWED_IDS: string[]; TURNSTILE_KEY: string | null; CDN_REPLACEMENTS: Array; @@ -66,48 +66,48 @@ const env: Record = { HAS_ONBOARDING: import.meta.env.VITE_HAS_ONBOARDING, }; -// loads from different locations, in order: environment (VITE_{KEY}), window (public/config.js) -function getKeyValue(key: keyof Config): string | undefined { - let windowValue = (window as any)?.__CONFIG__?.[`VITE_${key}`]; - if ( - windowValue !== null && - windowValue !== undefined && - windowValue.length === 0 - ) - windowValue = undefined; - return env[key] ?? windowValue ?? undefined; +function coerceUndefined(value: string | null | undefined): string | undefined { + if (value == null) return undefined; + if (value.length === 0) return undefined; + return value; } -function getKey(key: keyof Config, defaultString?: string): string { - return getKeyValue(key)?.toString() ?? defaultString ?? ""; +// loads from different locations, in order: environment (VITE_{KEY}), window (public/config.js) +function getKeyValue(key: keyof Config): string | undefined { + const windowValue = (window as any)?.__CONFIG__?.[`VITE_${key}`]; + + return coerceUndefined(env[key]) ?? coerceUndefined(windowValue) ?? undefined; +} + +function getKey(key: keyof Config): string | null; +function getKey(key: keyof Config, defaultString: string): string; +function getKey(key: keyof Config, defaultString?: string): string | null { + return getKeyValue(key)?.toString() ?? defaultString ?? null; } export function conf(): RuntimeConfig { - const dmcaEmail = getKey("DMCA_EMAIL"); - const chromeExtension = getKey("ONBOARDING_CHROME_EXTENSION_INSTALL_LINK"); - const firefoxExtension = getKey("ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK"); - const proxyInstallLink = getKey("ONBOARDING_PROXY_INSTALL_LINK"); - const turnstileKey = getKey("TURNSTILE_KEY"); return { APP_VERSION, GITHUB_LINK, DONATION_LINK, DISCORD_LINK, - DMCA_EMAIL: dmcaEmail.length > 0 ? dmcaEmail : null, - ONBOARDING_CHROME_EXTENSION_INSTALL_LINK: - chromeExtension.length > 0 ? chromeExtension : null, - ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK: - firefoxExtension.length > 0 ? firefoxExtension : null, - ONBOARDING_PROXY_INSTALL_LINK: - proxyInstallLink.length > 0 ? proxyInstallLink : null, + DMCA_EMAIL: getKey("DMCA_EMAIL"), + ONBOARDING_CHROME_EXTENSION_INSTALL_LINK: getKey( + "ONBOARDING_CHROME_EXTENSION_INSTALL_LINK", + ), + ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK: getKey( + "ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK", + ), + ONBOARDING_PROXY_INSTALL_LINK: getKey("ONBOARDING_PROXY_INSTALL_LINK"), BACKEND_URL: getKey("BACKEND_URL", BACKEND_URL), TMDB_READ_API_KEY: getKey("TMDB_READ_API_KEY"), - PROXY_URLS: getKey("CORS_PROXY_URL") + PROXY_URLS: getKey("CORS_PROXY_URL", "") .split(",") - .map((v) => v.trim()), + .map((v) => v.trim()) + .filter((v) => v.length > 0), NORMAL_ROUTER: getKey("NORMAL_ROUTER", "false") === "true", HAS_ONBOARDING: getKey("HAS_ONBOARDING", "false") === "true", - TURNSTILE_KEY: turnstileKey.length > 0 ? turnstileKey : null, + TURNSTILE_KEY: getKey("TURNSTILE_KEY"), DISALLOWED_IDS: getKey("DISALLOWED_IDS", "") .split(",") .map((v) => v.trim()) From 404d3b885fc1b260adda067e0aebcecc55837f67 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sun, 3 Mar 2024 18:37:43 +0000 Subject: [PATCH 3/4] Handle nullability of config fields --- src/hooks/auth/useBackendUrl.ts | 2 +- src/pages/parts/admin/TMDBTestPart.tsx | 2 +- src/pages/parts/settings/SidebarPart.tsx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hooks/auth/useBackendUrl.ts b/src/hooks/auth/useBackendUrl.ts index 64545227..adee8622 100644 --- a/src/hooks/auth/useBackendUrl.ts +++ b/src/hooks/auth/useBackendUrl.ts @@ -1,7 +1,7 @@ import { conf } from "@/setup/config"; import { useAuthStore } from "@/stores/auth"; -export function useBackendUrl(): string | undefined { +export function useBackendUrl(): string | null { const backendUrl = useAuthStore((s) => s.backendUrl); return backendUrl ?? conf().BACKEND_URL; } diff --git a/src/pages/parts/admin/TMDBTestPart.tsx b/src/pages/parts/admin/TMDBTestPart.tsx index a7f8fa50..d760c948 100644 --- a/src/pages/parts/admin/TMDBTestPart.tsx +++ b/src/pages/parts/admin/TMDBTestPart.tsx @@ -25,7 +25,7 @@ export function TMDBTestPart() { errorText: "", }); - if (tmdbApiKey.length === 0) { + if (!tmdbApiKey || tmdbApiKey.length === 0) { return setStatus({ hasTested: true, success: false, diff --git a/src/pages/parts/settings/SidebarPart.tsx b/src/pages/parts/settings/SidebarPart.tsx index 13db06fe..98404c84 100644 --- a/src/pages/parts/settings/SidebarPart.tsx +++ b/src/pages/parts/settings/SidebarPart.tsx @@ -14,7 +14,7 @@ import { useAuthStore } from "@/stores/auth"; const rem = 16; -function SecureBadge(props: { url: string | undefined }) { +function SecureBadge(props: { url: string | null }) { const { t } = useTranslation(); const secure = props.url ? props.url.startsWith("https://") : false; return ( From fa8548d3c28b94b9e18c012cd83b84493fb0d459 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sun, 3 Mar 2024 18:37:57 +0000 Subject: [PATCH 4/4] Default config.js to not have TMDB set --- public/config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/config.js b/public/config.js index a0681d06..feb65e84 100644 --- a/public/config.js +++ b/public/config.js @@ -4,7 +4,7 @@ window.__CONFIG__ = { VITE_CORS_PROXY_URL: "", // The READ API key to access TMDB - VITE_TMDB_READ_API_KEY: "CHANGEME", + VITE_TMDB_READ_API_KEY: "", // The DMCA email displayed in the footer, null to hide the DMCA link VITE_DMCA_EMAIL: null, @@ -16,5 +16,5 @@ window.__CONFIG__ = { VITE_BACKEND_URL: null, // A comma separated list of disallowed IDs in the case of a DMCA claim - in the format "series-" and "movie-" - VITE_DISALLOWED_IDS: "" + VITE_DISALLOWED_IDS: "", };