From 429713060e983335bba69bdff720dc00388401cd Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 11 Oct 2026 22:51:34 -0400 Subject: [PATCH] fix: Show the punycode host of client metadata documents on the consent screen A lookalike Unicode host was shown as written. Hostnames now come from a shared getHostname helper that returns the punycode form. --- app/scenes/Login/OAuthAuthorize.tsx | 16 ++++++++------- server/utils/oauth/ClientMetadataHelper.ts | 13 +++++------- shared/utils/urls.test.ts | 24 ++++++++++++++++++++++ shared/utils/urls.ts | 18 ++++++++++++++++ 4 files changed, 56 insertions(+), 15 deletions(-) diff --git a/app/scenes/Login/OAuthAuthorize.tsx b/app/scenes/Login/OAuthAuthorize.tsx index 3ff5a52db5..9941c8fa25 100644 --- a/app/scenes/Login/OAuthAuthorize.tsx +++ b/app/scenes/Login/OAuthAuthorize.tsx @@ -4,7 +4,7 @@ import styled from "styled-components"; import Flex from "@shared/components/Flex"; import { s } from "@shared/styles"; import { parseDomain } from "@shared/utils/domains"; -import { isLoopbackUri } from "@shared/utils/urls"; +import { getHostname, isLoopbackUri } from "@shared/utils/urls"; import { OAuthClientValidation } from "@shared/validations"; import type OAuthClient from "~/models/oauth/OAuthClient"; import ButtonLarge from "~/components/ButtonLarge"; @@ -75,14 +75,16 @@ function inputScopes(scope?: string): string[] { * Returns the hostname of a client ID metadata document URL. * * @param clientId the OAuth client_id parameter. - * @returns the hostname, or undefined if the client does not use a metadata document. + * @returns the punycode hostname, or undefined if the client does not use a metadata document. */ function metadataDocumentHost(clientId: string) { - return clientId.startsWith( - OAuthClientValidation.metadataDocumentClientIdPrefix - ) - ? parseDomain(clientId).host - : undefined; + if ( + !clientId.startsWith(OAuthClientValidation.metadataDocumentClientIdPrefix) + ) { + return undefined; + } + + return getHostname(clientId); } /** diff --git a/server/utils/oauth/ClientMetadataHelper.ts b/server/utils/oauth/ClientMetadataHelper.ts index d0679e2894..fc6416bd05 100644 --- a/server/utils/oauth/ClientMetadataHelper.ts +++ b/server/utils/oauth/ClientMetadataHelper.ts @@ -5,6 +5,7 @@ import { TeamPreference } from "@shared/types"; import { parseDomain } from "@shared/utils/domains"; import { errToString } from "@shared/utils/error"; import { Day, Minute, Second } from "@shared/utils/time"; +import { getHostname } from "@shared/utils/urls"; import { OAuthClientValidation } from "@shared/validations"; import env from "@server/env"; import { AuthorizationError } from "@server/errors"; @@ -121,8 +122,10 @@ export class ClientMetadataHelper { // Users can upload content to this installation, so a document hosted // here could impersonate a client. + const hostname = getHostname(clientId); if ( - await ClientMetadataHelper.isInstallationHost(new URL(clientId).hostname) + !hostname || + (await ClientMetadataHelper.isInstallationHost(hostname)) ) { return null; } @@ -244,13 +247,7 @@ export class ClientMetadataHelper { env.CDN_URL, env.AWS_S3_UPLOAD_BUCKET_URL, env.AWS_S3_ACCELERATE_URL, - ].flatMap((url) => { - try { - return url ? [new URL(url).hostname.toLowerCase()] : []; - } catch { - return []; - } - }); + ].flatMap((url) => getHostname(url) ?? []); if (storageHosts.includes(host)) { return true; } diff --git a/shared/utils/urls.test.ts b/shared/utils/urls.test.ts index a0fac0800e..d8106965af 100644 --- a/shared/utils/urls.test.ts +++ b/shared/utils/urls.test.ts @@ -441,6 +441,30 @@ describe("#urlRegex", () => { }); }); +describe("#getHostname", () => { + it("should return the hostname", () => { + expect(urlsUtils.getHostname("https://Example.com:8080/a?b=c")).toBe( + "example.com" + ); + }); + + it("should return internationalized hostnames as punycode", () => { + expect(urlsUtils.getHostname("https://аpple.com/client.json")).toBe( + "xn--pple-43d.com" + ); + }); + + it("should keep brackets on IPv6 hostnames", () => { + expect(urlsUtils.getHostname("http://[::1]:3000/callback")).toBe("[::1]"); + }); + + it("should return undefined for unparseable input", () => { + expect(urlsUtils.getHostname("not a url")).toBeUndefined(); + expect(urlsUtils.getHostname("")).toBeUndefined(); + expect(urlsUtils.getHostname(undefined)).toBeUndefined(); + }); +}); + describe("#removeUrlFragment", () => { it("should remove a hash fragment", () => { expect( diff --git a/shared/utils/urls.ts b/shared/utils/urls.ts index 0e30e90305..48851c3185 100644 --- a/shared/utils/urls.ts +++ b/shared/utils/urls.ts @@ -217,6 +217,24 @@ export function isLoopbackUri(uri: string | undefined): boolean { } } +/** + * Returns the hostname of a URL. Internationalized hostnames are returned in + * their punycode form. + * + * @param url the URL to parse. + * @returns the hostname, or undefined if the URL cannot be parsed. + */ +export function getHostname(url: string | undefined): string | undefined { + if (!url) { + return undefined; + } + try { + return new URL(url).hostname; + } catch { + return undefined; + } +} + /** * Temporary prefix applied to links in document that are not yet persisted. */