fix(dashboard): keep short keys masked and show a revealed key as returned

- formatApiKeyLast4() keeps a key shorter than 8 characters wholly masked,
  including the stored mask of one, instead of showing it as its last 4.
- EditConnectionModal reformats only the default stored mask; a key the
  API revealed (ALLOW_API_KEY_REVEAL) is shown as returned again, which
  the connModals suite asserts.

Refs #19982
This commit is contained in:
Rafa Martins
2026-11-18 22:11:13 -03:00
committed by diegosouzapw
parent 991af36aab
commit 51aa1a4199
4 changed files with 54 additions and 8 deletions

View File

@@ -19,7 +19,7 @@ import {
normalizeAntigravityClientProfileSetting,
} from "@/shared/constants/antigravityClientProfile";
import { parseExtraApiKeys } from "@/shared/utils/parseApiKeys";
import { formatApiKeyLast4 } from "@/shared/utils/apiKeyMask";
import { formatApiKeyLast4, looksLikeMaskedApiKey } from "@/shared/utils/apiKeyMask";
import { freeOnlyImportHintKey, providerSupportsFreeOnlyImport } from "@/shared/utils/freeModels";
import { useNotificationStore } from "@/store/notificationStore";
import { type CodexServiceTier } from "@/lib/providers/requestDefaults";
@@ -276,6 +276,11 @@ export default function EditConnectionModal({
? providerText(t, "awsPollySecretAccessKeyLabel", "AWS Secret Access Key")
: (vertexCopy?.label ?? (apiKeyOptional ? t("apiKeyOptionalLabel") : t("apiKeyLabel")));
const maskedSavedKey = connection?.apiKey ? formatApiKeyLast4(connection.apiKey) : null;
// A key revealed by the API (ALLOW_API_KEY_REVEAL) is shown as returned; only the
// default stored mask is reformatted to its last 4 characters.
const primaryKeyDisplay = looksLikeMaskedApiKey(connection?.apiKey)
? maskedSavedKey
: connection?.apiKey;
const apiCredentialPlaceholder = isWebSessionCredential
? webSessionCredential.placeholder
: (vertexCopy?.placeholder ?? (maskedSavedKey || t("enterNewApiKey")));
@@ -1513,7 +1518,7 @@ export default function EditConnectionModal({
<span
className={`flex-1 min-w-0 break-all font-mono text-xs bg-bg-subtle px-3 py-2 rounded border border-border ${statusColor}`}
>
{statusIcon} {t("primaryKey")}: {maskedSavedKey ?? connection.apiKey}
{statusIcon} {t("primaryKey")}: {primaryKeyDisplay}
</span>
{health && (
<span

View File

@@ -392,6 +392,30 @@ describe("conn-modals (Phase 1c extraction)", () => {
expect(c.textContent).not.toContain("test-p...78b9");
});
it("EditConnectionModal shows a stored-mask API key as its last 4 characters only", () => {
const onSave = vi.fn().mockResolvedValue(undefined);
const connection = {
id: "conn-masked-key",
name: "Masked Connection",
provider: "openai",
authType: "apikey",
priority: 1,
apiKey: "sk-proj-****a82f",
};
const c = renderModal(
<EditConnectionModal
isOpen={true}
connection={connection}
providerId="openai"
onSave={onSave}
onClose={vi.fn()}
/>
);
expect(c.textContent).toContain("sk-••••••••a82f");
expect(c.textContent).not.toContain("sk-proj-****a82f");
});
it("EditConnectionModal renders OpenRouter preset when provider comes from the page", () => {
const onSave = vi.fn().mockResolvedValue(undefined);
const connection = {

View File

@@ -16,10 +16,22 @@ export function maskStoredApiKey(key: unknown): string | null {
return key.slice(0, 8) + MASK + key.slice(-4);
}
const LAST4_BULLETS = "••••••••";
/** Below this many characters the last 4 would give most of the key away. */
const MIN_KEY_LENGTH_FOR_LAST4 = 8;
/**
* Length of the key behind `value`. A stored mask keeps at most the first 8
* characters, so a shorter head is the whole key; a full head means "8 or more".
*/
function keyLengthBehind(value: string): number {
return looksLikeMaskedApiKey(value) ? value.indexOf(MASK) : value.length;
}
/**
* Format an API key (plaintext or already stored mask) to display only its prefix
* and the last 4 characters, with bullet masking in between.
* Example: `sk-••••••••a82f`
* Example: `sk-••••••••a82f`. A key shorter than 8 characters stays wholly masked.
*
* Safe for display on the dashboard: never exposes the secret body.
*/
@@ -27,10 +39,10 @@ export function formatApiKeyLast4(key: unknown): string | null {
if (typeof key !== "string") return null;
const trimmed = key.trim();
if (!trimmed) return null;
const last4 = trimmed.length <= 4 ? trimmed : trimmed.slice(-4);
if (keyLengthBehind(trimmed) < MIN_KEY_LENGTH_FOR_LAST4) return LAST4_BULLETS;
const match = trimmed.match(/^([a-zA-Z0-9]+[_-])/);
const prefix = match ? match[1] : "";
return `${prefix}••••••••${last4}`;
return `${prefix}${LAST4_BULLETS}${trimmed.slice(-4)}`;
}
/** True when `value` has the shape of a mask produced by `maskStoredApiKey`. */

View File

@@ -33,9 +33,14 @@ describe("API Key Last 4 Masking and Display (#19982)", () => {
assert.equal(formatApiKeyLast4(bareKey), "••••••••1234");
});
it("formats short keys without throwing errors", () => {
assert.equal(formatApiKeyLast4("sk-1"), "sk-••••••••sk-1");
assert.equal(formatApiKeyLast4("abcd"), "••••••••abcd");
it("keeps short keys wholly masked instead of showing them as their last 4", () => {
assert.equal(formatApiKeyLast4("sk-1"), "••••••••");
assert.equal(formatApiKeyLast4("abcd"), "••••••••");
assert.equal(formatApiKeyLast4("abcdefg"), "••••••••");
// The dashboard receives the stored mask: a head shorter than 8 is the whole key.
assert.equal(formatApiKeyLast4(maskStoredApiKey("abcd")), "••••••••");
assert.equal(formatApiKeyLast4(maskStoredApiKey("abcdefg")), "••••••••");
assert.equal(formatApiKeyLast4("abcdefgh"), "••••••••efgh");
});
it("formats already stored masked keys consistently", () => {