From faa806668b4ea331ddfc03467b11ae277b422ba9 Mon Sep 17 00:00:00 2001 From: Sky Mulley Date: Tue, 6 Oct 2026 22:32:56 +0100 Subject: [PATCH] mask database password when viewing credentials --- .../admin/servers/ServerDatabaseRow.tsx | 5 +- .../components/elements/SecretInput.spec.tsx | 54 +++++++++++++++ .../components/elements/SecretInput.tsx | 67 +++++++++++++++++++ .../server/databases/DatabaseTable.tsx | 5 +- 4 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 resources/scripts/components/elements/SecretInput.spec.tsx create mode 100644 resources/scripts/components/elements/SecretInput.tsx diff --git a/resources/scripts/components/admin/servers/ServerDatabaseRow.tsx b/resources/scripts/components/admin/servers/ServerDatabaseRow.tsx index 2b6cee48b..98c97cf0f 100644 --- a/resources/scripts/components/admin/servers/ServerDatabaseRow.tsx +++ b/resources/scripts/components/admin/servers/ServerDatabaseRow.tsx @@ -15,6 +15,7 @@ import Label from '@/components/elements/Label'; import { TextInput } from '@/components/form/controls'; import Button from '@/components/elements/Button'; import CopyOnClick from '@/components/elements/CopyOnClick'; +import SecretInput from '@/components/elements/SecretInput'; import { DeleteAction, RowActionButton, RowActions } from '@/components/elements/table/RowActions'; import { relationshipAttributes } from '@/api/relationships'; @@ -154,9 +155,7 @@ function ServerDatabaseConnectionDialog({ serverId, database, open, onClose }: S
- - - +
diff --git a/resources/scripts/components/elements/SecretInput.spec.tsx b/resources/scripts/components/elements/SecretInput.spec.tsx new file mode 100644 index 000000000..259a2fdc6 --- /dev/null +++ b/resources/scripts/components/elements/SecretInput.spec.tsx @@ -0,0 +1,54 @@ +/** @vitest-environment jsdom */ + +import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import SecretInput from './SecretInput'; + +const clipboard = vi.hoisted(() => ({ copy: vi.fn<(text: string) => Promise>() })); + +vi.mock('@/lib/clipboard', () => ({ default: clipboard.copy })); + +afterEach(() => { + cleanup(); + document.getElementById('modal-portal')?.remove(); +}); +beforeEach(() => { + const portal = document.createElement('div'); + portal.id = 'modal-portal'; + document.body.append(portal); + clipboard.copy.mockReset(); + clipboard.copy.mockResolvedValue(true); +}); + +describe('SecretInput', () => { + it('masks the value until revealed', () => { + const { container } = render(); + const input = container.querySelector('input')!; + + expect(input).toHaveAttribute('type', 'password'); + + fireEvent.click(screen.getByRole('button', { name: 'Show password' })); + expect(input).toHaveAttribute('type', 'text'); + + fireEvent.click(screen.getByRole('button', { name: 'Hide password' })); + expect(input).toHaveAttribute('type', 'password'); + }); + + it('copies the value without revealing it', async () => { + const { container } = render(); + + await act(async () => fireEvent.click(screen.getByRole('button', { name: 'Copy password' }))); + + expect(clipboard.copy).toHaveBeenCalledTimes(1); + expect(clipboard.copy).toHaveBeenCalledWith('hunter2'); + expect(container.querySelector('input')).toHaveAttribute('type', 'password'); + expect(screen.getByRole('status')).toHaveTextContent('Copied text to clipboard.'); + }); + + it('disables both actions when there is no value', () => { + render(); + + expect(screen.getByRole('button', { name: 'Show password' })).toBeDisabled(); + expect(screen.getByRole('button', { name: 'Copy password' })).toBeDisabled(); + }); +}); diff --git a/resources/scripts/components/elements/SecretInput.tsx b/resources/scripts/components/elements/SecretInput.tsx new file mode 100644 index 000000000..73b24bd20 --- /dev/null +++ b/resources/scripts/components/elements/SecretInput.tsx @@ -0,0 +1,67 @@ +import { useState } from 'react'; +import { Copy, Eye, EyeOff } from 'lucide-react'; +import Button from '@/components/elements/Button'; +import CopyOnClick from '@/components/elements/CopyOnClick'; +import Icon from '@/components/elements/Icon'; +import Tooltip from '@/components/elements/tooltip/Tooltip'; +import { TextInput } from '@/components/form/controls'; + +interface SecretInputProps { + id?: string; + value: string | null | undefined; + label: string; +} + +const SecretInput = ({ id, value, label }: SecretInputProps) => { + const [visible, setVisible] = useState(false); + const toggleLabel = `${visible ? 'Hide' : 'Show'} ${label.toLowerCase()}`; + const copyLabel = `Copy ${label.toLowerCase()}`; + + return ( +
+ +
+ + + setVisible((current) => !current)} + > + + + + + + + + + + + + + +
+
+ ); +}; + +export default SecretInput; diff --git a/resources/scripts/components/server/databases/DatabaseTable.tsx b/resources/scripts/components/server/databases/DatabaseTable.tsx index a7dc96cbd..aacbfffc3 100644 --- a/resources/scripts/components/server/databases/DatabaseTable.tsx +++ b/resources/scripts/components/server/databases/DatabaseTable.tsx @@ -11,6 +11,7 @@ import Button from '@/components/elements/Button'; import Can from '@/components/elements/Can'; import CopyOnClick from '@/components/elements/CopyOnClick'; import Label from '@/components/elements/Label'; +import SecretInput from '@/components/elements/SecretInput'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import DataTableColumnHeader from '@/components/elements/table/DataTableColumnHeader'; import { actionsColumn, DeleteAction, RowActionButton, RowActions } from '@/components/elements/table/RowActions'; @@ -136,9 +137,7 @@ function DatabaseConnectionDialog({
- - - +