diff --git a/components/user/dropdown.tsx b/components/user/dropdown.tsx index 32b82a5e..5af96422 100644 --- a/components/user/dropdown.tsx +++ b/components/user/dropdown.tsx @@ -21,7 +21,7 @@ import { import { useAuth } from "@/contexts/auth-context" import { usePermissions } from "@/hooks/use-permissions" import { useSidebar } from "@/components/ui/sidebar" -import { resolveAccountDisplayName } from "@/lib/account-display" +import { resolveAccountAvatarUrl, resolveAccountMenuName } from "@/lib/account-display" import { getThemeManifest } from "@/lib/theme/manifest" function resolveAvatarPath(path: string): string { @@ -54,8 +54,25 @@ export function UserDropdown() { const { state } = useSidebar() const isCollapsed = state === "collapsed" const theme = getThemeManifest() + const accountIdentity = userInfo as { + account_name?: string + username?: string + email?: string + display_name?: string + picture?: string + } | null + const identity = { + access_key: accountIdentity?.account_name ?? "", + username: accountIdentity?.username, + email: accountIdentity?.email, + display_name: accountIdentity?.display_name, + picture: accountIdentity?.picture, + } + const accountName = resolveAccountMenuName(identity) + const identityAvatar = resolveAccountAvatarUrl(identity) const baseAvatarPath = theme.assets.userAvatar ?? "/img/userAvatar.png" - const preferredAvatarPath = resolvedTheme === "dark" ? withDarkVariant(baseAvatarPath) : baseAvatarPath + const preferredAvatarPath = + identityAvatar ?? (resolvedTheme === "dark" ? withDarkVariant(baseAvatarPath) : baseAvatarPath) const [avatar, setAvatar] = useState(() => resolveAvatarPath(preferredAvatarPath)) useEffect(() => { @@ -69,12 +86,6 @@ export function UserDropdown() { } } - const accountIdentity = userInfo as { account_name?: string; username?: string; email?: string } | null - const accountName = resolveAccountDisplayName({ - access_key: accountIdentity?.account_name ?? "", - username: accountIdentity?.username, - email: accountIdentity?.email, - }) const roleLabel = isAdmin ? t("Administrator") : t("User") return ( @@ -88,6 +99,7 @@ export function UserDropdown() { alt="" width={24} height={24} + referrerPolicy="no-referrer" className="size-6 rounded-full object-cover" onError={() => { const fallback = resolveAvatarPath(baseAvatarPath) diff --git a/hooks/use-account.ts b/hooks/use-account.ts index a2378669..eaa5a787 100644 --- a/hooks/use-account.ts +++ b/hooks/use-account.ts @@ -38,6 +38,10 @@ export interface AccountInfo { access_key: string username?: string email?: string + /** Full name from the OIDC identity provider; display only. */ + display_name?: string + /** Avatar URL from the OIDC identity provider; display only. */ + picture?: string identity_type: IdentityType session_access_key?: string is_admin: boolean diff --git a/lib/account-display.ts b/lib/account-display.ts index fabf84af..5dbab9f9 100644 --- a/lib/account-display.ts +++ b/lib/account-display.ts @@ -2,13 +2,31 @@ export interface AccountDisplayIdentity { access_key: string username?: string email?: string + display_name?: string + picture?: string +} + +function firstNonBlank(values: (string | undefined)[]): string { + return values.find((value): value is string => typeof value === "string" && value.trim().length > 0)?.trim() ?? "" } /** Resolve display-only identity metadata without changing the account principal. */ export function resolveAccountDisplayName(identity: AccountDisplayIdentity): string { - return ( - [identity.username, identity.email, identity.access_key] - .find((value): value is string => typeof value === "string" && value.trim().length > 0) - ?.trim() ?? "" - ) + return firstNonBlank([identity.username, identity.email, identity.access_key]) +} + +/** Name for compact identity chrome (user menu): the full name from the identity provider when known. */ +export function resolveAccountMenuName(identity: AccountDisplayIdentity): string { + return firstNonBlank([identity.display_name, resolveAccountDisplayName(identity)]) +} + +/** Avatar URL from the identity provider, accepted only as an absolute https URL. */ +export function resolveAccountAvatarUrl(identity: AccountDisplayIdentity): string | undefined { + const picture = identity.picture?.trim() + if (!picture) return undefined + try { + return new URL(picture).protocol === "https:" ? picture : undefined + } catch { + return undefined + } } diff --git a/tests/lib/account-display.test.ts b/tests/lib/account-display.test.ts index 07c6317d..ee02bf22 100644 --- a/tests/lib/account-display.test.ts +++ b/tests/lib/account-display.test.ts @@ -1,6 +1,6 @@ import test from "node:test" import assert from "node:assert/strict" -import { resolveAccountDisplayName } from "../../lib/account-display" +import { resolveAccountAvatarUrl, resolveAccountDisplayName, resolveAccountMenuName } from "../../lib/account-display" test("account display name prefers the configured OIDC username", () => { assert.equal( @@ -23,3 +23,32 @@ test("account display name falls back to email when username is absent or blank" test("account display name remains compatible with responses that only contain access_key", () => { assert.equal(resolveAccountDisplayName({ access_key: "legacy-account" }), "legacy-account") }) + +test("account menu name prefers the OIDC full name", () => { + assert.equal( + resolveAccountMenuName({ access_key: "virtual-parent", username: "jdoe", display_name: " Jane Doe " }), + "Jane Doe", + ) +}) + +test("account menu name falls back to the username chain when the full name is blank", () => { + assert.equal(resolveAccountMenuName({ access_key: "virtual-parent", username: "jdoe", display_name: " " }), "jdoe") + assert.equal(resolveAccountMenuName({ access_key: "legacy-account" }), "legacy-account") +}) + +test("account avatar accepts only absolute https picture URLs", () => { + assert.equal( + resolveAccountAvatarUrl({ access_key: "a", picture: "https://idp.example.test/a.png" }), + "https://idp.example.test/a.png", + ) + for (const picture of [ + undefined, + "", + "http://idp.example.test/a.png", + "javascript:alert(1)", + "/img/a.png", + "not a url", + ]) { + assert.equal(resolveAccountAvatarUrl({ access_key: "a", picture }), undefined, String(picture)) + } +}) diff --git a/tests/lib/account-surface.test.js b/tests/lib/account-surface.test.js index 4a05462d..dd24e8a4 100644 --- a/tests/lib/account-surface.test.js +++ b/tests/lib/account-surface.test.js @@ -37,8 +37,11 @@ test("the profile page distinguishes a failed read from an empty profile", () => test("the user menu prefers OIDC profile metadata without adding another account request", () => { const source = read("components/user/dropdown.tsx") - assert.match(source, /account_name\?: string; username\?: string; email\?: string/) - assert.match(source, /resolveAccountDisplayName\(\{/) + for (const field of ["account_name", "username", "email", "display_name", "picture"]) { + assert.match(source, new RegExp(`${field}\\?: string`)) + } + assert.match(source, /resolveAccountMenuName\(identity\)/) + assert.match(source, /resolveAccountAvatarUrl\(identity\)/) assert.doesNotMatch(source, /getAccountInfo\(\)/) }) diff --git a/tests/lib/ui-layout-source.test.js b/tests/lib/ui-layout-source.test.js index 81be7525..2b24521b 100644 --- a/tests/lib/ui-layout-source.test.js +++ b/tests/lib/ui-layout-source.test.js @@ -57,7 +57,11 @@ test("the account menu names the signed-in identity and its authority", () => { // A menu that opens on an avatar with no name answers neither "who am I" nor // "with what authority", which is the gap this menu existed with before. - assert.match(source, /const accountName = resolveAccountDisplayName\(\{/) + assert.match(source, /const accountName = resolveAccountMenuName\(identity\)/) + // The identity provider's avatar replaces the theme placeholder, without + // leaking the console URL to the image host. + assert.match(source, /identityAvatar \?\? \(resolvedTheme === "dark"/) + assert.match(source, /referrerPolicy="no-referrer"/) assert.match(source, /access_key: accountIdentity\?\.account_name \?\? ""/) assert.match(source, /const roleLabel = isAdmin \? t\("Administrator"\) : t\("User"\)/) assert.match(source, /\{accountName \|\| t\("Unknown user"\)\}/)