Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor(control-api): extract display-field validation to @clerum/display-field
Diego Zacarias16c2f53725db
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 541e21f43..a29112a40 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,13 +1,14 @@{"name": "clerum-control-api",- "version": "0.1.448",+ "version": "0.1.449","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.448",+ "version": "0.1.449","dependencies": {+ "@clerum/display-field": "file:../packages/display-field","@clerum/image-policy": "file:../packages/image-policy","@clerum/llm-providers": "file:../packages/llm-providers","@clerum/workflow-recipe-capability-policy": "file:../packages/workflow-recipe-capability-policy",@@ -40,6 +41,11 @@"node": ">=24"}},+ "../packages/display-field": {+ "name": "@clerum/display-field",+ "version": "0.1.0",+ "license": "MIT"+ },"../packages/image-policy": {"name": "@clerum/image-policy","version": "0.1.0",@@ -71,6 +77,10 @@"node": ">=20"}},+ "node_modules/@clerum/display-field": {+ "resolved": "../packages/display-field",+ "link": true+ },"node_modules/@clerum/image-policy": {"resolved": "../packages/image-policy","link": truediff --git a/control-api/package.json b/control-api/package.jsonindex 97cb53ceb..72e70f742 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.448",+ "version": "0.1.449","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {@@ -16,6 +16,7 @@"test:run": "vitest run --no-file-parallelism"},"dependencies": {+ "@clerum/display-field": "file:../packages/display-field","@clerum/image-policy": "file:../packages/image-policy","@clerum/llm-providers": "file:../packages/llm-providers","@clerum/workflow-recipe-capability-policy": "file:../packages/workflow-recipe-capability-policy",diff --git a/control-api/src/routes/admin/resourceFieldValidation.ts b/control-api/src/routes/admin/resourceFieldValidation.tsindex 2e1cb1265..aa4020024 100644--- a/control-api/src/routes/admin/resourceFieldValidation.ts+++ b/control-api/src/routes/admin/resourceFieldValidation.ts@@ -1,3 +1,4 @@+import { validateDisplayField } from '@clerum/display-field'import { RFC1123_RE } from '../../http/rfc1123.js'/**@@ -17,28 +18,12 @@ import { RFC1123_RE } from '../../http/rfc1123.js'* never blocked for unrelated edits.*/-/** Max length for a free-text display field (post-trim). */-export const DISPLAY_FIELD_MAX_LENGTH = 120--// Characters rejected in display fields:-// - C0 range + DEL (\x00-\x1f, \x7f): newlines and tabs are control characters-// too — a display name is a single-line label.-// - C1 range (\x80-\x9f): the second control block. U+0085 (NEL) is a line-// terminator that breaks the "single-line label" invariant, and U+009B (CSI)-// opens an ANSI escape sequence — both must be rejected like C0/DEL.-// - Unicode bidirectional formatting: embeddings/overrides (U+202A–U+202E:-// LRE/RLE/PDF/LRO/RLO) and isolates (U+2066–U+2069: LRI/RLI/FSI/PDI). These-// reorder surrounding text and let an admin craft a spec.host/displayName-// that visually impersonates another resource in the UI/desktop (spoofing).-// - Line/paragraph separators (U+2028/U+2029): line breaks a single-line label-// must not contain (JS regex `.`/`\s` and JSON treat them specially too).-// Deliberately NOT rejected: directional marks (U+200E/U+200F/U+061C) and-// zero-width/invisible characters (U+200B–U+200D, U+2060, U+FEFF). This set is-// the canonical Trojan-Source bidi mitigation; zero-width joiners (U+200D) are-// legitimate in emoji sequences, so blanket-rejecting them would break valid-// display names. Confusable/invisible-glyph spoofing is out of scope here.-// eslint-disable-next-line no-control-regex-const CONTROL_CHAR_RE = /[\x00-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069\u2028\u2029]/+// Display-field validation (free-text: control/bidi chars + trimmed length) is+// the single source of truth in `@clerum/display-field`, shared browser-safe so+// control-ui's edit preflight applies the same rule without re-deriving the+// regex (D4). Re-exported here under the same names so existing consumers/tests+// that import from `resourceFieldValidation.js` keep working.+export { validateDisplayField, DISPLAY_FIELD_MAX_LENGTH } from '@clerum/display-field'export interface FieldIssue {field: string@@ -64,33 +49,6 @@ export function validateResourceName(}}-/**- * Validate a free-text display field (present only). Returns an issue or null.- * Skips when the field is absent (undefined) — display fields are optional.- */-export function validateDisplayField(value: unknown, field: string): FieldIssue | null {- if (value === undefined) return null- if (typeof value !== 'string') {- return { field, message: `${field} must be a string` }- }- if (CONTROL_CHAR_RE.test(value)) {- return {- field,- message: `${field} must not contain control or bidirectional formatting characters`,- }- }- if (value.trim().length === 0) {- return { field, message: `${field} must not be empty or whitespace-only` }- }- if (value.trim().length > DISPLAY_FIELD_MAX_LENGTH) {- return {- field,- message: `${field} must be at most ${DISPLAY_FIELD_MAX_LENGTH} characters`,- }- }- return null-}-/*** Validate an identifier field (present only) as RFC1123. Returns an issue or* null. Skips when absent — presence of identifiers is enforced by the CRDdiff --git a/control-api/test/packages.displayField.test.ts b/control-api/test/packages.displayField.test.tsnew file mode 100644index 000000000..c92e3b1c8--- /dev/null+++ b/control-api/test/packages.displayField.test.ts@@ -0,0 +1,103 @@+import { describe, expect, it } from 'vitest'+import {+ CONTROL_CHAR_RE,+ DISPLAY_FIELD_MAX_LENGTH,+ validateDisplayField,+} from '@clerum/display-field'++// T1: the producer under test IS the shared package `@clerum/display-field`.+// This exercises the canonical rule directly (not via control-api's re-export),+// pinning the exact semantics both control-api and control-ui depend on.+describe('@clerum/display-field · validateDisplayField', () => {+ it('accepts a normal single-line label → null', () => {+ expect(validateDisplayField('My Host', 'spec.host')).toBeNull()+ })++ it('skips an absent (undefined) field → null (optional)', () => {+ expect(validateDisplayField(undefined, 'spec.host')).toBeNull()+ })++ it('rejects a non-string number → "must be a string" with field propagated', () => {+ const issue = validateDisplayField(42, 'spec.host')+ expect(issue).not.toBeNull()+ expect(issue?.field).toBe('spec.host')+ expect(issue?.message).toContain('must be a string')+ })++ it('rejects a non-string object → "must be a string"', () => {+ const issue = validateDisplayField({}, 'spec.displayName')+ expect(issue).not.toBeNull()+ expect(issue?.field).toBe('spec.displayName')+ expect(issue?.message).toContain('must be a string')+ })++ describe('control / bidi characters → "control..." with field propagated', () => {+ const cases: Array<[string, string]> = [+ ['C0 (\\x01)', 'a\x01b'],+ ['C1 U+0085 NEL', 'a\u0085b'],+ ['C1 U+009B CSI', 'a\u009bb'],+ ['bidi U+202E RLO', 'a\u202eb'],+ ['line separator U+2028', 'a\u2028b'],+ ['paragraph separator U+2029', 'a\u2029b'],+ ]+ for (const [label, value] of cases) {+ it(`rejects ${label}`, () => {+ const issue = validateDisplayField(value, 'spec.host')+ expect(issue).not.toBeNull()+ expect(issue?.field).toBe('spec.host')+ expect(issue?.message).toContain('control')+ })+ }+ })++ // T5: the "Deliberately NOT rejected" comment in index.cjs is a security+ // invariant (over-rejecting would break valid emoji/RTL display names). Pin the+ // boundary the Trojan-Source rationale depends on: these pass BY DESIGN.+ describe('deliberately allowed characters → null (must not over-reject)', () => {+ const allowed: Array<[string, string]> = [+ ['ZWJ U+200D (emoji sequences)', 'ab'],+ ['ZWSP U+200B', 'ab'],+ ['ZWNJ U+200C', 'ab'],+ ['LRM U+200E', 'ab'],+ ['RLM U+200F', 'ab'],+ ['ALM U+061C', 'ab'],+ ['word joiner U+2060', 'ab'],+ ['BOM/ZWNBSP U+FEFF', 'ab'],+ ]+ for (const [label, value] of allowed) {+ it(`accepts ${label}`, () => {+ expect(validateDisplayField(value, 'spec.host')).toBeNull()+ })+ }+ })++ it('rejects the empty string → "empty or whitespace-only"', () => {+ const issue = validateDisplayField('', 'spec.host')+ expect(issue).not.toBeNull()+ expect(issue?.message).toContain('empty or whitespace-only')+ })++ it('rejects a whitespace-only string → "empty or whitespace-only"', () => {+ const issue = validateDisplayField(' ', 'spec.host')+ expect(issue).not.toBeNull()+ expect(issue?.message).toContain('empty or whitespace-only')+ })++ it(`rejects ${DISPLAY_FIELD_MAX_LENGTH + 1} chars → "at most ${DISPLAY_FIELD_MAX_LENGTH}"`, () => {+ const issue = validateDisplayField('a'.repeat(DISPLAY_FIELD_MAX_LENGTH + 1), 'spec.host')+ expect(issue).not.toBeNull()+ expect(issue?.field).toBe('spec.host')+ expect(issue?.message).toContain(`at most ${DISPLAY_FIELD_MAX_LENGTH}`)+ })++ it(`accepts exactly ${DISPLAY_FIELD_MAX_LENGTH} chars → null`, () => {+ expect(validateDisplayField('a'.repeat(DISPLAY_FIELD_MAX_LENGTH), 'spec.host')).toBeNull()+ })++ it('exposes the canonical constants', () => {+ expect(DISPLAY_FIELD_MAX_LENGTH).toBe(120)+ expect(CONTROL_CHAR_RE).toBeInstanceOf(RegExp)+ expect(CONTROL_CHAR_RE.test('a\x01b')).toBe(true)+ expect(CONTROL_CHAR_RE.test('clean')).toBe(false)+ })+})diff --git a/packages/display-field/index.cjs b/packages/display-field/index.cjsnew file mode 100644index 000000000..6eb9a46c9--- /dev/null+++ b/packages/display-field/index.cjs@@ -0,0 +1,69 @@+'use strict'++/**+ * Shared free-text display-field validation — the single source of truth for the+ * rule that both control-api (admin CRUD) and control-ui (edit preflight) apply+ * to free-text display fields (host.spec.host, context.spec.displayName): only+ * trimmed length and control/bidi characters are constrained.+ *+ * This is a PURE, browser-safe leaf: zero imports of Node/K8s/env. Extracted+ * from control-api's resourceFieldValidation so the regex and length bound are+ * derived, never duplicated by hand across services (D4).+ */++/** Max length for a free-text display field (post-trim). */+const DISPLAY_FIELD_MAX_LENGTH = 120++// Characters rejected in display fields:+// - C0 range + DEL (\x00-\x1f, \x7f): newlines and tabs are control characters+// too — a display name is a single-line label.+// - C1 range (\x80-\x9f): the second control block. U+0085 (NEL) is a line+// terminator that breaks the "single-line label" invariant, and U+009B (CSI)+// opens an ANSI escape sequence — both must be rejected like C0/DEL.+// - Unicode bidirectional formatting: embeddings/overrides (U+202A–U+202E:+// LRE/RLE/PDF/LRO/RLO) and isolates (U+2066–U+2069: LRI/RLI/FSI/PDI). These+// reorder surrounding text and let an admin craft a spec.host/displayName+// that visually impersonates another resource in the UI/desktop (spoofing).+// - Line/paragraph separators (U+2028/U+2029): line breaks a single-line label+// must not contain (JS regex `.`/`\s` and JSON treat them specially too).+// Deliberately NOT rejected: directional marks (U+200E/U+200F/U+061C) and+// zero-width/invisible characters (U+200B–U+200D, U+2060, U+FEFF). This set is+// the canonical Trojan-Source bidi mitigation; zero-width joiners (U+200D) are+// legitimate in emoji sequences, so blanket-rejecting them would break valid+// display names. Confusable/invisible-glyph spoofing is out of scope here.+// eslint-disable-next-line no-control-regex+const CONTROL_CHAR_RE = /[\x00-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069\u2028\u2029]/++/**+ * Validate a free-text display field (present only). Returns an issue or null.+ * Skips when the field is absent (undefined) — display fields are optional.+ * Measures over `.trim()` but does NOT mutate the value.+ */+function validateDisplayField(value, field) {+ if (value === undefined) return null+ if (typeof value !== 'string') {+ return { field, message: `${field} must be a string` }+ }+ if (CONTROL_CHAR_RE.test(value)) {+ return {+ field,+ message: `${field} must not contain control or bidirectional formatting characters`,+ }+ }+ if (value.trim().length === 0) {+ return { field, message: `${field} must not be empty or whitespace-only` }+ }+ if (value.trim().length > DISPLAY_FIELD_MAX_LENGTH) {+ return {+ field,+ message: `${field} must be at most ${DISPLAY_FIELD_MAX_LENGTH} characters`,+ }+ }+ return null+}++module.exports = {+ DISPLAY_FIELD_MAX_LENGTH,+ CONTROL_CHAR_RE,+ validateDisplayField,+}diff --git a/packages/display-field/index.d.ts b/packages/display-field/index.d.tsnew file mode 100644index 000000000..821425f25--- /dev/null+++ b/packages/display-field/index.d.ts@@ -0,0 +1,22 @@+/**+ * Shared free-text display-field validation — the single source of truth for the+ * control/bidi-character + trimmed-length rule applied to free-text display+ * fields. PURE, browser-safe leaf; zero Node/K8s/env imports.+ */++/** Regex matching any character rejected in a display field (control/bidi). */+export declare const CONTROL_CHAR_RE: RegExp++/** Max length for a free-text display field (post-trim). */+export declare const DISPLAY_FIELD_MAX_LENGTH: number++export interface FieldIssue {+ field: string+ message: string+}++/**+ * Validate a free-text display field (present only). Returns an issue or null.+ * Skips when the field is absent (undefined) — display fields are optional.+ */+export declare function validateDisplayField(value: unknown, field: string): FieldIssue | nulldiff --git a/packages/display-field/package.json b/packages/display-field/package.jsonnew file mode 100644index 000000000..2af305d07--- /dev/null+++ b/packages/display-field/package.json@@ -0,0 +1,16 @@+{+ "name": "@clerum/display-field",+ "version": "0.1.0",+ "private": true,+ "description": "Shared free-text display-field validation (control/bidi chars + length)",+ "main": "index.cjs",+ "types": "index.d.ts",+ "exports": {+ ".": {+ "types": "./index.d.ts",+ "require": "./index.cjs",+ "default": "./index.cjs"+ }+ },+ "license": "MIT"+}