Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: add syntax highlight to invisible characters

adilson85d2e47df8c5
diff --git a/frontend/src/components/v2/SecretInput/SecretInput.test.tsx b/frontend/src/components/v2/SecretInput/SecretInput.test.tsx
new file mode 100644
index 0000000000..524f1713a8
--- /dev/null
+++ b/frontend/src/components/v2/SecretInput/SecretInput.test.tsx
@@ -0,0 +1,86 @@
+import assert from "node:assert/strict";
+import { renderToStaticMarkup } from "react-dom/server";
+import { describe, it, vi } from "vitest";
+
+import { HIDDEN_SECRET_VALUE } from "@app/const/secrets";
+
+import { SecretInput as SecretInputV3 } from "../../v3/platform/SecretInput/SecretInput";
+import { SecretInput as SecretInputV2 } from "./SecretInput";
+
+// The @app/hooks barrel pulls in the router and API client, which cannot load outside the app.
+vi.mock("@app/hooks", async () => import("@app/hooks/useToggle"));
+
+const MARKER_REGEX = /<span class="[^"]*bg-warning\/25[^"]*">([^<]*)<\/span>/g;
+
+const markedChars = (markup: string) => [...markup.matchAll(MARKER_REGEX)].map(([, char]) => char);
+
+describe.each([
+ { name: "v2", SecretInput: SecretInputV2 },
+ { name: "v3", SecretInput: SecretInputV3 }
+])("$name SecretInput invisible characters", ({ SecretInput }) => {
+ const render = (value: string, isVisible = true) =>
+ renderToStaticMarkup(<SecretInput value={value} isVisible={isVisible} />);
+
+ it("marks nothing in a value made of ordinary characters", () => {
+ assert.deepEqual(markedChars(render('{ "KEY": "value with spaces\ttab" }')), []);
+ });
+
+ it("marks a non-breaking space", () => {
+ assert.deepEqual(markedChars(render('"URL": "https://example.com"')), [" "]);
+ });
+
+ it("marks zero-width characters and a byte order mark", () => {
+ assert.deepEqual(markedChars(render("TIMEOUT=30​‍⁠")), [
+ "",
+ "​",
+ "‍",
+ "⁠"
+ ]);
+ });
+
+ it("marks control characters but leaves tabs and newlines alone", () => {
+ assert.deepEqual(markedChars(render("a\u0000b\u001fc\u007fd\t\ne\r")), [
+ "\u0000",
+ "\u001f",
+ "\u007f"
+ ]);
+ });
+
+ it("marks soft hyphens, bidi controls, and unicode space separators", () => {
+ assert.deepEqual(markedChars(render("x­y‮z⁦w v u ")), [
+ "­",
+ "‮",
+ "⁦",
+ " ",
+ " ",
+ " "
+ ]);
+ });
+
+ it("marks each character in a run separately and keeps the surrounding text", () => {
+ const markup = render("before  after");
+ assert.deepEqual(markedChars(markup), [" ", " "]);
+ assert.match(markup, /before<span/);
+ assert.match(markup, /<\/span>after/);
+ });
+
+ it("marks a non-breaking space that stops a reference from resolving", () => {
+ const markup = render("${FOO BAR}");
+ assert.deepEqual(markedChars(markup), [" "]);
+ assert.doesNotMatch(markup, /text-warning\/80/);
+ });
+
+ it("still highlights a valid reference and marks invisible characters around it", () => {
+ const markup = render("​${env.FOO} ");
+ assert.deepEqual(markedChars(markup), ["​", " "]);
+ assert.match(markup, /role="button"[^>]*>env<\/span>/);
+ assert.match(markup, /role="button"[^>]*>FOO<\/span>/);
+ });
+
+ it("does not reveal or mark anything while the value is hidden", () => {
+ const markup = render("secret value", false);
+ assert.deepEqual(markedChars(markup), []);
+ assert.match(markup, new RegExp(HIDDEN_SECRET_VALUE.replace(/[*]/g, "\\*")));
+ assert.doesNotMatch(markup, /secret value/);
+ });
+});
diff --git a/frontend/src/components/v2/SecretInput/SecretInput.tsx b/frontend/src/components/v2/SecretInput/SecretInput.tsx
index 9887916560..9190568747 100644
--- a/frontend/src/components/v2/SecretInput/SecretInput.tsx
+++ b/frontend/src/components/v2/SecretInput/SecretInput.tsx
@@ -1,5 +1,5 @@
/* eslint-disable react/no-danger */
-import { forwardRef, TextareaHTMLAttributes, useEffect, useState } from "react";
+import { forwardRef, ReactNode, TextareaHTMLAttributes, useEffect, useState } from "react";
import { twMerge } from "tailwind-merge";
import { HIDDEN_SECRET_VALUE } from "@app/const/secrets";
@@ -7,6 +7,33 @@ import { useToggle } from "@app/hooks";
const REGEX = /(\${([@a-zA-Z0-9-_. ]+)})/g;
+const INVISIBLE_CHAR_REGEX =
+ // eslint-disable-next-line no-control-regex
+ /([\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u00a0\u00ad\u1680\u2000-\u200f\u2028-\u202f\u205f\u2060\u2066-\u2069\u3000\ufeff])/g;
+
+const markInvisibleChars = (text: string, chunkKey: number) => {
+ const nodes: ReactNode[] = [];
+ let offset = 0;
+
+ text.split(INVISIBLE_CHAR_REGEX).forEach((part, index) => {
+ if (index % 2 === 1) {
+ nodes.push(
+ <span
+ key={`secret-value-invisible-${chunkKey}-${offset}`}
+ className="rounded-xs bg-warning/25 outline outline-1 outline-warning/70"
+ >
+ {part}
+ </span>
+ );
+ } else if (part) {
+ nodes.push(part);
+ }
+ offset += part.length;
+ });
+
+ return nodes;
+};
+
const syntaxHighlight = (
content?: string | null,
isVisible?: boolean,
@@ -78,7 +105,7 @@ const syntaxHighlight = (
}
}}
>
- {segment}
+ {markInvisibleChars(segment, i)}
</span>
{segmentIndex < parts.length - 1 && (
<span className="ph-no-capture pointer-events-none text-warning/80">.</span>
@@ -94,7 +121,7 @@ const syntaxHighlight = (
skipNext = false;
return [];
}
- return el;
+ return markInvisibleChars(el, i);
});
// akhilmhdh: Dont remove this br. I am still clueless how this works but weirdly enough
diff --git a/frontend/src/components/v3/platform/SecretInput/SecretInput.stories.tsx b/frontend/src/components/v3/platform/SecretInput/SecretInput.stories.tsx
index 92396b3c31..389034082a 100644
--- a/frontend/src/components/v3/platform/SecretInput/SecretInput.stories.tsx
+++ b/frontend/src/components/v3/platform/SecretInput/SecretInput.stories.tsx
@@ -2,6 +2,8 @@ import type { Meta, StoryObj } from "@storybook/react-vite";
import { SecretInput } from "./SecretInput";
+const REFERENCE_WITH_NBSP = `\${FOO${"\u00a0"}BAR}`;
+
const meta = {
title: "Platform/SecretInput",
component: SecretInput,
@@ -51,6 +53,29 @@ export const Default: Story = {
}
};
+export const InvisibleCharacters: Story = {
+ args: {
+ isVisible: true,
+ value: [
+ "{",
+ ' "HILTON_BASE_URL":\u00a0"https://kapip-s.hilton.io/gateway",',
+ ' "HILTON_API_KEY": "a1b2c3d4e5",',
+ ' "HILTON_TIMEOUT": 30\u200b',
+ "\ufeff}",
+ REFERENCE_WITH_NBSP
+ ].join("\n"),
+ variant: "plain"
+ },
+ parameters: {
+ docs: {
+ description: {
+ story:
+ "Codepoints that paint as a normal space, or as nothing at all, are marked so they can be spotted in a pasted value. Left to right: a non-breaking space (U+00A0), a zero-width space (U+200B), a byte order mark (U+FEFF), and a non-breaking space inside a secret reference, which stops the reference resolving."
+ }
+ }
+ }
+};
+
export const Plain: Story = {
args: {
variant: "plain"
diff --git a/frontend/src/components/v3/platform/SecretInput/SecretInput.tsx b/frontend/src/components/v3/platform/SecretInput/SecretInput.tsx
index 856f61101c..3e85ac0d52 100644
--- a/frontend/src/components/v3/platform/SecretInput/SecretInput.tsx
+++ b/frontend/src/components/v3/platform/SecretInput/SecretInput.tsx
@@ -1,5 +1,5 @@
/* eslint-disable react/no-danger */
-import { forwardRef, TextareaHTMLAttributes, useEffect, useState } from "react";
+import { forwardRef, ReactNode, TextareaHTMLAttributes, useEffect, useState } from "react";
import { HIDDEN_SECRET_VALUE } from "@app/const/secrets";
import { useToggle } from "@app/hooks";
@@ -8,6 +8,33 @@ import { cn } from "../../utils";
const REGEX = /(\${([@a-zA-Z0-9-_. ]+)})/g;
+const INVISIBLE_CHAR_REGEX =
+ // eslint-disable-next-line no-control-regex
+ /([\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u00a0\u00ad\u1680\u2000-\u200f\u2028-\u202f\u205f\u2060\u2066-\u2069\u3000\ufeff])/g;
+
+const markInvisibleChars = (text: string, chunkKey: number) => {
+ const nodes: ReactNode[] = [];
+ let offset = 0;
+
+ text.split(INVISIBLE_CHAR_REGEX).forEach((part, index) => {
+ if (index % 2 === 1) {
+ nodes.push(
+ <span
+ key={`secret-value-invisible-${chunkKey}-${offset}`}
+ className="rounded-xs bg-warning/25 outline outline-1 outline-warning/70"
+ >
+ {part}
+ </span>
+ );
+ } else if (part) {
+ nodes.push(part);
+ }
+ offset += part.length;
+ });
+
+ return nodes;
+};
+
const syntaxHighlight = (
content?: string | null,
isVisible?: boolean,
@@ -81,7 +108,7 @@ const syntaxHighlight = (
}
}}
>
- {segment}
+ {markInvisibleChars(segment, i)}
</span>
{segmentIndex < parts.length - 1 && (
<span className="ph-no-capture pointer-events-none text-secret/80">.</span>
@@ -97,7 +124,7 @@ const syntaxHighlight = (
skipNext = false;
return [];
}
- return el;
+ return markInvisibleChars(el, i);
});
// akhilmhdh: Dont remove this br. I am still clueless how this works but weirdly enough