SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
fix(dashboard-v2): validate key:value tags in the editor (inline-edit errors + backend-rule space/char/length checks) (#12062)
Ashwin Bhatkal8cd207752260
diff --git a/frontend/src/components/TagKeyValueInput/TagKeyValueInput.test.tsx b/frontend/src/components/TagKeyValueInput/TagKeyValueInput.test.tsxnew file mode 100644index 0000000000..75d09d4688--- /dev/null+++ b/frontend/src/components/TagKeyValueInput/TagKeyValueInput.test.tsx@@ -0,0 +1,112 @@+import { render, screen } from '@testing-library/react';+import userEvent from '@testing-library/user-event';++import TagKeyValueInput from './TagKeyValueInput';++const TID = 'tag-key-value-input';++type User = ReturnType<typeof userEvent.setup>;++const startEditingFirstChip = async (user: User): Promise<HTMLElement> => {+ await user.dblClick(screen.getAllByTestId(`${TID}-chip`)[0]);+ return screen.getByTestId(`${TID}-edit`);+};++describe('TagKeyValueInput — inline chip edit', () => {+ it('shows an error and stays in edit mode when Enter commits an invalid value', async () => {+ const user = userEvent.setup();+ const onTagsChange = jest.fn();+ render(<TagKeyValueInput tags={['env:prod']} onTagsChange={onTagsChange} />);++ const input = await startEditingFirstChip(user);+ await user.clear(input);+ await user.type(input, 'novalue{Enter}');++ expect(screen.getByTestId(`${TID}-error`)).toHaveTextContent(+ 'key:value format',+ );+ // Still editing (input present), and no change committed.+ expect(screen.getByTestId(`${TID}-edit`)).toBeInTheDocument();+ expect(onTagsChange).not.toHaveBeenCalled();+ });++ it('shows a duplicate error when Enter commits an existing tag', async () => {+ const user = userEvent.setup();+ const onTagsChange = jest.fn();+ render(+ <TagKeyValueInput+ tags={['env:prod', 'team:pulse']}+ onTagsChange={onTagsChange}+ />,+ );++ const input = await startEditingFirstChip(user);+ await user.clear(input);+ await user.type(input, 'team:pulse{Enter}');++ expect(screen.getByTestId(`${TID}-error`)).toHaveTextContent(+ 'already exists',+ );+ expect(onTagsChange).not.toHaveBeenCalled();+ });++ it('commits a valid edit on Enter', async () => {+ const user = userEvent.setup();+ const onTagsChange = jest.fn();+ render(<TagKeyValueInput tags={['env:prod']} onTagsChange={onTagsChange} />);++ const input = await startEditingFirstChip(user);+ await user.clear(input);+ await user.type(input, 'env:staging{Enter}');++ expect(onTagsChange).toHaveBeenCalledWith(['env:staging']);+ expect(screen.queryByTestId(`${TID}-error`)).not.toBeInTheDocument();+ });++ it('reverts silently (no error) when blurring an invalid edit', async () => {+ const user = userEvent.setup();+ const onTagsChange = jest.fn();+ render(<TagKeyValueInput tags={['env:prod']} onTagsChange={onTagsChange} />);++ const input = await startEditingFirstChip(user);+ await user.clear(input);+ await user.type(input, 'novalue');+ await user.tab(); // blur off the edit input++ expect(screen.queryByTestId(`${TID}-error`)).not.toBeInTheDocument();+ expect(screen.queryByTestId(`${TID}-edit`)).not.toBeInTheDocument();+ expect(onTagsChange).not.toHaveBeenCalled();+ });+});++describe('TagKeyValueInput — backend-rule validation', () => {+ it('rejects a key containing a space on inline edit', async () => {+ const user = userEvent.setup();+ const onTagsChange = jest.fn();+ render(<TagKeyValueInput tags={['env:prod']} onTagsChange={onTagsChange} />);++ const input = await startEditingFirstChip(user);+ await user.clear(input);+ await user.type(input, 'my key:prod{Enter}');++ expect(screen.getByTestId(`${TID}-error`)).toHaveTextContent(+ 'spaces or special characters',+ );+ expect(screen.getByTestId(`${TID}-edit`)).toBeInTheDocument();+ expect(onTagsChange).not.toHaveBeenCalled();+ });++ it('rejects a value containing a space in the new-tag input', async () => {+ const user = userEvent.setup();+ const onTagsChange = jest.fn();+ render(<TagKeyValueInput tags={[]} onTagsChange={onTagsChange} />);++ const input = screen.getByTestId(TID);+ await user.type(input, 'env:pro d{Enter}');++ expect(screen.getByTestId(`${TID}-error`)).toHaveTextContent(+ 'spaces or special characters',+ );+ expect(onTagsChange).not.toHaveBeenCalled();+ });+});diff --git a/frontend/src/components/TagKeyValueInput/TagKeyValueInput.tsx b/frontend/src/components/TagKeyValueInput/TagKeyValueInput.tsxindex f2025733ba..873746be92 100644--- a/frontend/src/components/TagKeyValueInput/TagKeyValueInput.tsx+++ b/frontend/src/components/TagKeyValueInput/TagKeyValueInput.tsx@@ -5,7 +5,7 @@ import { Typography } from '@signozhq/ui/typography';import cx from 'classnames';import TagBadge from '../TagBadge/TagBadge';-import { parseKeyValueTag } from './utils';+import { validateTag } from './utils';import styles from './TagKeyValueInput.module.scss';@@ -43,16 +43,12 @@ function TagKeyValueInput({if (!raw) {return;}- const normalized = parseKeyValueTag(raw);- if (!normalized) {- setError('Tags must be in key:value format (both sides required).');+ const result = validateTag(raw, tags);+ if ('error' in result) {+ setError(result.error);return;}- if (tags.includes(normalized)) {- setError('This tag already exists.');- return;- }- onTagsChange([...tags, normalized]);+ onTagsChange([...tags, result.tag]);setInputValue('');setError('');};@@ -82,15 +78,20 @@ function TagKeyValueInput({const cancelEdit = (): void => {setEditIndex(-1);setEditValue('');+ setError('');};- const commitEdit = (): void => {- const normalized = parseKeyValueTag(editValue);- // Drop into a no-op (revert) on invalid or duplicate edits rather than- // stranding the user in an un-exitable edit box.- if (normalized && !tags.some((t, i) => t === normalized && i !== editIndex)) {- onTagsChange(tags.map((t, i) => (i === editIndex ? normalized : t)));+ const commitEdit = (revertOnInvalid = false): void => {+ const result = validateTag(editValue, tags, editIndex);+ if ('error' in result) {+ if (revertOnInvalid) {+ cancelEdit();+ } else {+ setError(result.error);+ }+ return;}+ onTagsChange(tags.map((t, i) => (i === editIndex ? result.tag : t)));cancelEdit();};@@ -121,11 +122,14 @@ function TagKeyValueInput({value={editValue}autoFocustestId={`${testId}-edit`}- onChange={(e: ChangeEvent<HTMLInputElement>): void =>- setEditValue(e.target.value)- }+ onChange={(e: ChangeEvent<HTMLInputElement>): void => {+ setEditValue(e.target.value);+ if (error) {+ setError('');+ }+ }}onKeyDown={handleEditKeyDown}- onBlur={commitEdit}+ onBlur={(): void => commitEdit(true)}/>) : (<TagBadgediff --git a/frontend/src/components/TagKeyValueInput/utils.ts b/frontend/src/components/TagKeyValueInput/utils.tsindex 8872ccc381..308e59da6f 100644--- a/frontend/src/components/TagKeyValueInput/utils.ts+++ b/frontend/src/components/TagKeyValueInput/utils.ts@@ -15,3 +15,42 @@ export function parseKeyValueTag(raw: string): string | null {}return `${key}:${value}`;}++const TAG_KEY_REGEX = new RegExp('^[a-zA-Z$_@{#][a-zA-Z0-9$_@#{}:/-]*$');+const TAG_VALUE_REGEX = new RegExp('^[a-zA-Z0-9$_@#{}:.+=/-]*$');+const MAX_TAG_LEN = 32;++export type TagValidation = { tag: string } | { error: string };++export function validateTag(+ raw: string,+ existingTags: string[],+ excludeIndex = -1,+): TagValidation {+ const normalized = parseKeyValueTag(raw);+ if (!normalized) {+ return { error: 'Tags must be in key:value format (both sides required).' };+ }+ const separator = normalized.indexOf(':');+ const key = normalized.slice(0, separator);+ const value = normalized.slice(separator + 1);+ if (!TAG_KEY_REGEX.test(key)) {+ return { error: 'Tag keys cannot contain spaces or special characters.' };+ }+ if (!TAG_VALUE_REGEX.test(value)) {+ return { error: 'Tag values cannot contain spaces or special characters.' };+ }+ if (key.length > MAX_TAG_LEN || value.length > MAX_TAG_LEN) {+ return {+ error: `Tag key and value must each be ${MAX_TAG_LEN} characters or fewer.`,+ };+ }+ if (+ existingTags.some(+ (tag, index) => tag === normalized && index !== excludeIndex,+ )+ ) {+ return { error: 'This tag already exists.' };+ }+ return { tag: normalized };+}