Skip to content
This repository was archived by the owner on Jun 26, 2026. It is now read-only.

Commit fa41573

Browse files
authored
feat(console): mask access/secret keys in application config forms (#5)
* chore(console): add vitest + testing-library setup Adds vitest with jsdom and React Testing Library to apps/console so that schema-form widgets and helpers can be unit-tested. No tests yet; the infra is wired up first to keep follow-up commits focused on behaviour. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * feat(schema-form): detect sensitive string fields by name Adds a pure helper that walks an OpenAPI/RJSF schema and tags every string field whose name matches the conventional credential patterns (access/secret keys, passwords, tokens, private keys, api keys) with a `SensitiveStringWidget` binding in the uiSchema. Cozystack chart schemas do not annotate sensitive fields with a `format: password` hint, so the detection is name-based and runs in the same style as the existing storageClass/backupClassName/disks helpers in SchemaForm. The helper lives in a separate module so it can be covered with unit tests without rendering RJSF. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * feat(schema-form): add reveal-toggle widget for sensitive strings Adds an RJSF widget that renders a credential field as eight bullets plus an eye-icon toggle: clicking the eye swaps the bullets for a plain text input that forwards typing through the standard onChange contract (empty string becomes undefined). When the field is disabled or readonly the toggle is disabled and editing is blocked. The visual style mirrors the SecretRow reveal control already used in the Secrets tab. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * feat(schema-form): mask access/secret keys in application forms Wires the sensitive-field detector into SchemaForm's uiSchema pipeline and registers the toggle widget under the customWidgets map. As a result, fields like postgres' s3AccessKey and s3SecretKey on the application order/edit screens render as bullets with a click-to-reveal control instead of plain text. Detection is purely additive and does not override widgets bound by earlier detectors. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> --------- Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent b123f71 commit fa41573

13 files changed

Lines changed: 1535 additions & 5 deletions

‎.github/workflows/test.yaml‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
16+
- name: Set up pnpm
17+
uses: pnpm/action-setup@v4
18+
19+
- name: Set up Node.js
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: 22
23+
cache: pnpm
24+
25+
- name: Install dependencies
26+
run: pnpm install --frozen-lockfile
27+
28+
- name: Typecheck
29+
run: pnpm typecheck
30+
31+
- name: Test
32+
run: pnpm test

‎apps/console/package.json‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"dev": "vite",
88
"build": "tsc --noEmit && vite build",
99
"lint": "eslint .",
10+
"test": "vitest run",
11+
"test:watch": "vitest",
1012
"typecheck": "tsc --noEmit",
1113
"preview": "vite preview"
1214
},
@@ -29,12 +31,18 @@
2931
},
3032
"devDependencies": {
3133
"@tailwindcss/vite": "^4.2.2",
34+
"@testing-library/jest-dom": "^6.9.1",
35+
"@testing-library/react": "^16.3.2",
36+
"@testing-library/user-event": "^14.6.1",
3237
"@types/js-yaml": "^4.0.9",
3338
"@types/react": "^19.2.14",
3439
"@types/react-dom": "^19.2.3",
3540
"@vitejs/plugin-react": "^6.0.1",
41+
"@vitest/ui": "^4.1.6",
42+
"jsdom": "^29.1.1",
3643
"tailwindcss": "^4.2.2",
3744
"typescript": "~6.0.2",
38-
"vite": "^8.0.4"
45+
"vite": "^8.0.4",
46+
"vitest": "^4.1.6"
3947
}
4048
}

‎apps/console/src/components/SchemaForm.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import validator from "@rjsf/validator-ajv8"
44
import { getDefaultFormState } from "@rjsf/utils"
55
import type { RJSFSchema, UiSchema, TemplatesType } from "@rjsf/utils"
66
import { keysOrderToUiSchema, sanitizeSchema } from "../lib/keys-order.ts"
7+
import { addSensitiveStringWidgets } from "../lib/sensitive-fields.ts"
78
import { customTemplates, customWidgets } from "./rjsf-templates.tsx"
89
import { AdditionalPropertiesField } from "./AdditionalPropertiesField.tsx"
910
import { ResourceQuotasField } from "./ResourceQuotasField.tsx"
@@ -206,18 +207,21 @@ export function SchemaForm({
206207
// Automatically add AdditionalPropertiesField for fields with additionalProperties schema
207208
const withAdditionalProps = addAdditionalPropertiesWidgets(schema, withVMDisk)
208209

210+
// Mask credential-shaped string fields (access/secret keys, passwords, tokens).
211+
const withSensitive = addSensitiveStringWidgets(schema, withAdditionalProps)
212+
209213
// Override resourceQuotas field with structured quota editor.
210214
// Scoped to schemas where resourceQuotas has additionalProperties: {type: "string"}
211215
// (the cozystack-tenants chart shape) to avoid activating on unrelated CRDs.
212216
const rqSchema = (schema as any).properties?.resourceQuotas
213217
if (rqSchema && rqSchema.additionalProperties?.type === "string") {
214-
withAdditionalProps.resourceQuotas = {
215-
...withAdditionalProps.resourceQuotas,
218+
withSensitive.resourceQuotas = {
219+
...withSensitive.resourceQuotas,
216220
"ui:field": "ResourceQuotasField",
217221
}
218222
}
219223

220-
return withAdditionalProps
224+
return withSensitive
221225
}, [keysOrder, schema])
222226

223227
const customFields = useMemo(
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import { useState } from "react"
2+
import { describe, it, expect, vi } from "vitest"
3+
import { render, screen } from "@testing-library/react"
4+
import userEvent from "@testing-library/user-event"
5+
import type { WidgetProps } from "@rjsf/utils"
6+
import { SensitiveStringWidget } from "./SensitiveStringWidget.tsx"
7+
8+
function makeProps(overrides: Partial<WidgetProps> = {}): WidgetProps {
9+
const base = {
10+
id: "sensitive",
11+
name: "sensitive",
12+
label: "sensitive",
13+
value: undefined as unknown,
14+
onChange: vi.fn(),
15+
onBlur: vi.fn(),
16+
onFocus: vi.fn(),
17+
required: false,
18+
disabled: false,
19+
readonly: false,
20+
autofocus: false,
21+
placeholder: "",
22+
options: {},
23+
schema: { type: "string" },
24+
uiSchema: {},
25+
formContext: {},
26+
rawErrors: [],
27+
hideError: false,
28+
multiple: false,
29+
registry: {},
30+
}
31+
return { ...base, ...overrides } as unknown as WidgetProps
32+
}
33+
34+
function renderWithLabel(props: WidgetProps) {
35+
return render(
36+
<>
37+
<label htmlFor={props.id}>access key</label>
38+
<SensitiveStringWidget {...props} />
39+
</>,
40+
)
41+
}
42+
43+
const TOGGLE_NAME = /toggle credential visibility/i
44+
45+
describe("SensitiveStringWidget", () => {
46+
it("renders a real <input> control associated with its label, regardless of state", () => {
47+
renderWithLabel(makeProps({ value: "super-secret" }))
48+
49+
const input = screen.getByLabelText("access key") as HTMLInputElement
50+
expect(input.tagName).toBe("INPUT")
51+
expect(input.type).toBe("password")
52+
expect(input.value).toBe("super-secret")
53+
})
54+
55+
it("flips the input type from password to text when the toggle is pressed", async () => {
56+
const user = userEvent.setup()
57+
renderWithLabel(makeProps({ value: "super-secret" }))
58+
59+
const toggle = screen.getByRole("button", { name: TOGGLE_NAME })
60+
expect(toggle).toHaveAttribute("aria-pressed", "false")
61+
62+
await user.click(toggle)
63+
64+
expect(toggle).toHaveAttribute("aria-pressed", "true")
65+
expect((screen.getByLabelText("access key") as HTMLInputElement).type).toBe("text")
66+
})
67+
68+
it("keeps the toggle's accessible name stable across state changes", async () => {
69+
const user = userEvent.setup()
70+
renderWithLabel(makeProps({ value: "super-secret" }))
71+
72+
const toggle = screen.getByRole("button", { name: TOGGLE_NAME })
73+
await user.click(toggle)
74+
75+
// Same button is still findable by the same stable name after pressing.
76+
expect(screen.getByRole("button", { name: TOGGLE_NAME })).toBe(toggle)
77+
})
78+
79+
it("hides the value again after a second toggle press", async () => {
80+
const user = userEvent.setup()
81+
renderWithLabel(makeProps({ value: "super-secret" }))
82+
83+
const toggle = screen.getByRole("button", { name: TOGGLE_NAME })
84+
await user.click(toggle)
85+
await user.click(toggle)
86+
87+
expect(toggle).toHaveAttribute("aria-pressed", "false")
88+
expect((screen.getByLabelText("access key") as HTMLInputElement).type).toBe("password")
89+
})
90+
91+
it("carries the project's input styling in both states (no shape change on toggle)", async () => {
92+
const user = userEvent.setup()
93+
renderWithLabel(makeProps({ value: "super-secret" }))
94+
95+
const input = screen.getByLabelText("access key") as HTMLInputElement
96+
expect(input).toHaveClass("rounded-md")
97+
expect(input).toHaveClass("border")
98+
99+
await user.click(screen.getByRole("button", { name: TOGGLE_NAME }))
100+
101+
expect(input).toHaveClass("rounded-md")
102+
expect(input).toHaveClass("border")
103+
})
104+
105+
it("opts the input out of browser password-manager autofill", () => {
106+
renderWithLabel(makeProps({ value: "super-secret" }))
107+
108+
const input = screen.getByLabelText("access key") as HTMLInputElement
109+
expect(input).toHaveAttribute("autocomplete", "new-password")
110+
})
111+
112+
it("forwards typing into onChange without requiring the toggle first", async () => {
113+
const user = userEvent.setup()
114+
const onChange = vi.fn()
115+
116+
function Host() {
117+
const [value, setValue] = useState<string | undefined>("")
118+
return (
119+
<>
120+
<label htmlFor="sensitive">access key</label>
121+
<SensitiveStringWidget
122+
{...makeProps({
123+
value,
124+
onChange: (next) => {
125+
onChange(next)
126+
setValue(next as string | undefined)
127+
},
128+
})}
129+
/>
130+
</>
131+
)
132+
}
133+
render(<Host />)
134+
135+
await user.type(screen.getByLabelText("access key"), "abc")
136+
137+
expect(onChange).toHaveBeenLastCalledWith("abc")
138+
})
139+
140+
it("emits undefined when the input is cleared", async () => {
141+
const user = userEvent.setup()
142+
const onChange = vi.fn()
143+
renderWithLabel(makeProps({ value: "secret", onChange }))
144+
145+
await user.clear(screen.getByLabelText("access key") as HTMLInputElement)
146+
147+
expect(onChange).toHaveBeenLastCalledWith(undefined)
148+
})
149+
150+
it("keeps the reveal toggle usable when the form is readonly", async () => {
151+
const user = userEvent.setup()
152+
renderWithLabel(makeProps({ value: "secret", readonly: true }))
153+
154+
const input = screen.getByLabelText("access key") as HTMLInputElement
155+
expect(input).toHaveAttribute("readonly")
156+
157+
await user.click(screen.getByRole("button", { name: TOGGLE_NAME }))
158+
159+
expect(input.type).toBe("text")
160+
expect(input.value).toBe("secret")
161+
})
162+
163+
it("keeps the reveal toggle usable when the form is disabled", async () => {
164+
const user = userEvent.setup()
165+
renderWithLabel(makeProps({ value: "secret", disabled: true }))
166+
167+
const input = screen.getByLabelText("access key") as HTMLInputElement
168+
expect(input).toBeDisabled()
169+
170+
await user.click(screen.getByRole("button", { name: TOGGLE_NAME }))
171+
172+
expect(input.type).toBe("text")
173+
})
174+
})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { useState } from "react"
2+
import { Eye, EyeOff } from "lucide-react"
3+
import type { WidgetProps } from "@rjsf/utils"
4+
5+
// Matches the styling applied to `input[type="text"]` in schema-form.css.
6+
// The CSS rule there is keyed on the `type` attribute, so a `type="password"`
7+
// input would otherwise fall back to native browser chrome and visibly differ
8+
// from neighbouring fields every time the user toggles reveal.
9+
const INPUT_CLASS =
10+
"w-full rounded-md border border-slate-200 bg-white px-2.5 py-1.5 text-sm text-slate-900 shadow-sm outline-none transition-shadow focus:border-blue-500 focus:ring-2 focus:ring-blue-500/20 disabled:opacity-50 disabled:cursor-not-allowed"
11+
12+
const TOGGLE_CLASS =
13+
"flex size-8 shrink-0 items-center justify-center rounded-md text-slate-500 hover:bg-slate-100"
14+
15+
export function SensitiveStringWidget(props: WidgetProps) {
16+
const { id, value, onChange, required, disabled, readonly, autofocus, placeholder } = props
17+
const [revealed, setRevealed] = useState(false)
18+
19+
const stringValue = typeof value === "string" ? value : ""
20+
21+
return (
22+
<div className="flex items-center gap-2">
23+
<input
24+
id={id}
25+
type={revealed ? "text" : "password"}
26+
// Best-effort hint to skip browser password-manager autofill on this
27+
// ad-hoc credential field. Browser behaviour varies; `new-password` is
28+
// the most reliable value but the prompt is not fully suppressible.
29+
autoComplete="new-password"
30+
autoFocus={autofocus}
31+
placeholder={placeholder}
32+
value={stringValue}
33+
required={required}
34+
disabled={disabled}
35+
readOnly={readonly}
36+
onChange={(event) => {
37+
// Empty string is coerced to undefined so the surrounding form
38+
// drops the key from the spec entirely on clear, matching the
39+
// convention used by the other custom widgets in this folder.
40+
const next = event.target.value
41+
onChange(next === "" ? undefined : next)
42+
}}
43+
className={INPUT_CLASS}
44+
/>
45+
<button
46+
type="button"
47+
onClick={() => setRevealed((prev) => !prev)}
48+
aria-label="Toggle credential visibility"
49+
aria-pressed={revealed}
50+
className={TOGGLE_CLASS}
51+
>
52+
{revealed ? <EyeOff className="size-4" /> : <Eye className="size-4" />}
53+
</button>
54+
</div>
55+
)
56+
}

‎apps/console/src/components/rjsf-templates.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { StorageClassWidget } from "./StorageClassWidget.tsx"
1313
import { AdditionalPropertiesWidget } from "./AdditionalPropertiesWidget.tsx"
1414
import { VMDiskWidget } from "./VMDiskWidget.tsx"
1515
import { BackupClassWidget } from "./BackupClassWidget.tsx"
16+
import { SensitiveStringWidget } from "./SensitiveStringWidget.tsx"
1617

1718
function IconButton<
1819
T = any,
@@ -94,4 +95,5 @@ export const customWidgets = {
9495
AdditionalPropertiesWidget: AdditionalPropertiesWidget,
9596
VMDiskWidget: VMDiskWidget,
9697
BackupClassWidget: BackupClassWidget,
98+
SensitiveStringWidget: SensitiveStringWidget,
9799
}

0 commit comments

Comments
 (0)