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.
This commit is contained in:
Tom Moor committed 2026-10-11 22:51:34 -04:00
1 parent b38123bed9
commit 429713060e
4 files changed
+56 -15

No files matched your search

+9 -7
View File
@@ -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);
}
/**
+5 -8
View File
@@ -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;
}
+24
View File
@@ -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(
+18
View File
@@ -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.
*/