Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

Mask agent tokens until explicitly revealed (#2625)

Daniel Jeremiah74b02cbe0dd7
diff --git a/internal/site/src/components/routes/settings/tokens-fingerprints.tsx b/internal/site/src/components/routes/settings/tokens-fingerprints.tsx
index a6859fcae..7f3b1b22e 100644
--- a/internal/site/src/components/routes/settings/tokens-fingerprints.tsx
+++ b/internal/site/src/components/routes/settings/tokens-fingerprints.tsx
@@ -21,6 +21,7 @@ import {
InstallDropdown,
} from "@/components/install-dropdowns"
import { $router } from "@/components/router"
+import { TokenValue } from "@/components/token-value"
import { Button } from "@/components/ui/button"
import {
DropdownMenu,
@@ -179,15 +180,8 @@ const SectionUniversalToken = memo(() => {
updateToken(checked ? 1 : 0, isPermanent ? 1 : 0)
}}
/>
- <div className="min-w-0 flex-1 overflow-auto">
- <span
- className={cn(
- "text-sm text-primary opacity-60 transition-opacity",
- checked ? "opacity-100" : "select-none"
- )}
- >
- {token}
- </span>
+ <div className="min-w-0 flex-1">
+ <TokenValue key={`${checked}:${token}`} token={token} label={t`Universal token`} disabled={!checked} />
</div>
<ActionsButtonUniversalToken token={token} checked={checked} />
</div>
@@ -365,7 +359,13 @@ const SectionTable = memo(({ fingerprints = [] }: { fingerprints: FingerprintRec
<TableCell className="font-medium ps-5 py-2 max-w-60 truncate">
{fingerprint.expand.system.name}
</TableCell>
- <TableCell className="font-mono text-[0.95em] py-2">{fingerprint.token}</TableCell>
+ <TableCell className="py-2">
+ <TokenValue
+ key={fingerprint.token}
+ token={fingerprint.token}
+ label={t`Token for ${fingerprint.expand.system.name}`}
+ />
+ </TableCell>
<TableCell className="font-mono text-[0.95em] py-2">{fingerprint.fingerprint}</TableCell>
{!isReadOnly && (
<TableCell className="py-2 px-4 xl:px-2">
diff --git a/internal/site/src/components/token-value.tsx b/internal/site/src/components/token-value.tsx
new file mode 100644
index 000000000..b7648244f
--- /dev/null
+++ b/internal/site/src/components/token-value.tsx
@@ -0,0 +1,50 @@
+import { t } from "@lingui/core/macro"
+import { CopyIcon, EyeIcon, EyeOffIcon } from "lucide-react"
+import { useState } from "react"
+import { Button } from "@/components/ui/button"
+import { copyToClipboard } from "@/lib/utils"
+
+/** Reduce accidental disclosure during screen sharing; this is not an access control. */
+export function TokenValue({ token, label, disabled = false }: { token: string; label: string; disabled?: boolean }) {
+ const [revealedToken, setRevealedToken] = useState<string | null>(null)
+ const revealed = !disabled && revealedToken === token
+
+ return (
+ <fieldset className="flex items-center gap-2 min-w-0">
+ <legend className="sr-only">{label}</legend>
+ <span className="font-mono text-sm min-w-0 flex-1 overflow-x-auto whitespace-nowrap" aria-hidden={!revealed}>
+ {revealed ? token : "••••••••••••"}
+ </span>
+ {!revealed && <span className="sr-only">{t`Token hidden`}</span>}
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon"
+ className="shrink-0"
+ disabled={disabled}
+ aria-label={revealed ? t`Hide token` : t`Reveal token`}
+ title={revealed ? t`Hide token` : t`Reveal token`}
+ aria-pressed={revealed}
+ onClick={() => setRevealedToken(revealed ? null : token)}
+ >
+ {revealed ? (
+ <EyeOffIcon className="size-4" aria-hidden="true" />
+ ) : (
+ <EyeIcon className="size-4" aria-hidden="true" />
+ )}
+ </Button>
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon"
+ className="shrink-0"
+ disabled={disabled}
+ aria-label={t`Copy token`}
+ title={t`Copy token`}
+ onClick={() => copyToClipboard(token)}
+ >
+ <CopyIcon className="size-4" aria-hidden="true" />
+ </Button>
+ </fieldset>
+ )
+}
diff --git a/internal/site/tests/token-value.test.tsx b/internal/site/tests/token-value.test.tsx
new file mode 100644
index 000000000..bfde9127e
--- /dev/null
+++ b/internal/site/tests/token-value.test.tsx
@@ -0,0 +1,28 @@
+import { describe, expect, mock, test } from "bun:test"
+import { renderToStaticMarkup } from "react-dom/server"
+
+mock.module("@lingui/core/macro", () => ({ t: (strings: TemplateStringsArray) => strings[0] }))
+mock.module("../src/lib/utils", () => ({
+ cn: (...values: unknown[]) => values.filter(Boolean).join(" "),
+ copyToClipboard: async () => {},
+}))
+
+const { TokenValue } = await import("../src/components/token-value")
+const token = "dummy-agent-token-not-a-real-credential"
+
+describe("token display privacy", () => {
+ test("does not place the token in rendered text or attributes before reveal", () => {
+ const html = renderToStaticMarkup(<TokenValue token={token} label="Token for test system" />)
+ expect(html).not.toContain(token)
+ expect(html).toContain("Token hidden")
+ expect(html).toContain('aria-label="Reveal token"')
+ expect(html).toContain('aria-label="Copy token"')
+ expect(html).toContain('aria-pressed="false"')
+ })
+
+ test("disabled universal tokens remain masked and their controls cannot be used", () => {
+ const html = renderToStaticMarkup(<TokenValue token={token} label="Universal token" disabled />)
+ expect(html).not.toContain(token)
+ expect(html.match(/disabled=""/g)).toHaveLength(2)
+ })
+})