Skip to content

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

fix: address concerns from code review

IgorHorta862c63b06719
diff --git a/frontend/src/pages/secret-manager/CommitDetailsPage/components/SecretVersionDiffView/SecretVersionDiffView.tsx b/frontend/src/pages/secret-manager/CommitDetailsPage/components/SecretVersionDiffView/SecretVersionDiffView.tsx
index b6fc4ffa4d..eb262d7825 100644
--- a/frontend/src/pages/secret-manager/CommitDetailsPage/components/SecretVersionDiffView/SecretVersionDiffView.tsx
+++ b/frontend/src/pages/secret-manager/CommitDetailsPage/components/SecretVersionDiffView/SecretVersionDiffView.tsx
@@ -46,21 +46,54 @@ const isArray = (obj: JsonValue): obj is JsonArray => {
return Array.isArray(obj);
};
-const deepEqual = (a: JsonValue, b: JsonValue): boolean => {
- if (a === b) return true;
- if (a == null || b == null) return false;
- if (typeof a !== typeof b) return false;
-
- if (isArray(a) && isArray(b)) {
- if (a.length !== b.length) return false;
- return a.every((item: JsonValue, index: number) => deepEqual(item, b[index]));
+// Normalize secret value to match backend behavior:
+// - Trim whitespace
+// - If value ends with \n, preserve it (but trim everything else)
+// This matches the backend transform: (val.at(-1) === "\n" ? `${val.trim()}\n` : val.trim())
+const normalizeSecretValue = (value: string | null | undefined): string => {
+ if (!value || typeof value !== "string") return "";
+ // If value ends with \n, preserve it after trimming
+ if (value.at(-1) === "\n") {
+ return `${value.trim()}\n`;
+ }
+ return value.trim();
+};
+
+// Normalize a JSON value for comparison - specifically handles secretValue strings
+const normalizeValueForComparison = (value: JsonValue, key?: string, path?: string): JsonValue => {
+ // Check if this is a secretValue field - either by key name or path
+ const isSecretValue = key === "secretValue" || path?.endsWith(".secretValue") || path === "secretValue";
+
+ // Only normalize secretValue strings
+ if (isSecretValue && typeof value === "string") {
+ return normalizeSecretValue(value);
+ }
+ return value;
+};
+
+const deepEqual = (a: JsonValue, b: JsonValue, key?: string, path?: string): boolean => {
+ // Normalize values before comparison if they're secretValue strings
+ const normalizedA = normalizeValueForComparison(a, key, path);
+ const normalizedB = normalizeValueForComparison(b, key, path);
+
+ if (normalizedA === normalizedB) return true;
+ if (normalizedA == null || normalizedB == null) return false;
+ if (typeof normalizedA !== typeof normalizedB) return false;
+
+ if (isArray(normalizedA) && isArray(normalizedB)) {
+ if (normalizedA.length !== normalizedB.length) return false;
+ return normalizedA.every((item: JsonValue, index: number) => deepEqual(item, normalizedB[index], undefined, path));
}
- if (isObject(a) && isObject(b)) {
- const keysA = Object.keys(a);
- const keysB = Object.keys(b);
+ if (isObject(normalizedA) && isObject(normalizedB)) {
+ const keysA = Object.keys(normalizedA);
+ const keysB = Object.keys(normalizedB);
if (keysA.length !== keysB.length) return false;
- return keysA.every((key) => keysB.includes(key) && deepEqual(a[key], b[key]));
+ const currentPath = path ? `${path}.${key || ""}` : (key || "");
+ return keysA.every((k) => {
+ const nestedPath = currentPath ? `${currentPath}.${k}` : k;
+ return keysB.includes(k) && deepEqual(normalizedA[k], normalizedB[k], k, nestedPath);
+ });
}
return false;
@@ -69,24 +102,32 @@ const deepEqual = (a: JsonValue, b: JsonValue): boolean => {
const getDiffPaths = (oldObj: JsonValue, newObj: JsonValue, path: string = ""): Set<string> => {
const diffPaths = new Set<string>();
- if (oldObj === newObj) return diffPaths;
+ // Extract the key name from the path for normalization
+ const pathParts = path.split(".");
+ const currentKey = pathParts[pathParts.length - 1] || "";
+
+ // Normalize values before comparison if they're secretValue strings
+ const normalizedOld = normalizeValueForComparison(oldObj, currentKey, path);
+ const normalizedNew = normalizeValueForComparison(newObj, currentKey, path);
+
+ if (normalizedOld === normalizedNew) return diffPaths;
- if (oldObj == null || newObj == null) {
+ if (normalizedOld == null || normalizedNew == null) {
diffPaths.add(path || "root");
return diffPaths;
}
- if (typeof oldObj !== typeof newObj) {
+ if (typeof normalizedOld !== typeof normalizedNew) {
diffPaths.add(path || "root");
return diffPaths;
}
- if (isArray(oldObj) && isArray(newObj)) {
+ if (isArray(normalizedOld) && isArray(normalizedNew)) {
return diffPaths;
}
- if (isObject(oldObj) && isObject(newObj)) {
- const allKeys = new Set([...Object.keys(oldObj), ...Object.keys(newObj)]);
+ if (isObject(normalizedOld) && isObject(normalizedNew)) {
+ const allKeys = new Set([...Object.keys(normalizedOld), ...Object.keys(normalizedNew)]);
allKeys.forEach((key) => {
const currentPath = path ? `${path}.${key}` : key;
@@ -95,16 +136,16 @@ const getDiffPaths = (oldObj: JsonValue, newObj: JsonValue, path: string = ""):
return;
}
- if (!(key in oldObj) || !(key in newObj) || !deepEqual(oldObj[key], newObj[key])) {
+ if (!(key in normalizedOld) || !(key in normalizedNew) || !deepEqual(normalizedOld[key], normalizedNew[key], key, currentPath)) {
diffPaths.add(currentPath);
if (
- key in oldObj &&
- key in newObj &&
- (isObject(oldObj[key]) || isArray(oldObj[key])) &&
- (isObject(newObj[key]) || isArray(newObj[key]))
+ key in normalizedOld &&
+ key in normalizedNew &&
+ (isObject(normalizedOld[key]) || isArray(normalizedOld[key])) &&
+ (isObject(normalizedNew[key]) || isArray(normalizedNew[key]))
) {
- const nestedDiffs = getDiffPaths(oldObj[key], newObj[key], currentPath);
+ const nestedDiffs = getDiffPaths(normalizedOld[key], normalizedNew[key], currentPath);
nestedDiffs.forEach((p) => diffPaths.add(p));
}
}
@@ -112,7 +153,8 @@ const getDiffPaths = (oldObj: JsonValue, newObj: JsonValue, path: string = ""):
return diffPaths;
}
- if (oldObj !== newObj) {
+ // For primitive values (strings, numbers, booleans), compare normalized values
+ if (normalizedOld !== normalizedNew) {
diffPaths.add(path || "root");
}
diff --git a/frontend/src/pages/secret-manager/SecretApprovalsPage/components/SecretApprovalRequest/components/SecretApprovalRequestChangeItem.tsx b/frontend/src/pages/secret-manager/SecretApprovalsPage/components/SecretApprovalRequest/components/SecretApprovalRequestChangeItem.tsx
index 45faafdbb8..f4a2c62d30 100644
--- a/frontend/src/pages/secret-manager/SecretApprovalsPage/components/SecretApprovalRequest/components/SecretApprovalRequestChangeItem.tsx
+++ b/frontend/src/pages/secret-manager/SecretApprovalsPage/components/SecretApprovalRequest/components/SecretApprovalRequestChangeItem.tsx
@@ -25,6 +25,26 @@ const DIFF_TYPE = {
MODIFIED: "modified"
} as const;
+// Normalize secret value to match backend behavior:
+// - Trim whitespace
+// - If value ends with \n, preserve it (but trim everything else)
+// This matches the backend transform: (val.at(-1) === "\n" ? `${val.trim()}\n` : val.trim())
+const normalizeSecretValue = (value: string | null | undefined): string => {
+ if (!value || typeof value !== "string") return "";
+ // If value ends with \n, preserve it after trimming
+ if (value.at(-1) === "\n") {
+ return `${value.trim()}\n`;
+ }
+ return value.trim();
+};
+
+// Compare normalized values to determine if they're actually different
+const areValuesEqual = (oldValue: string | null | undefined, newValue: string | null | undefined): boolean => {
+ const normalizedOld = normalizeSecretValue(oldValue);
+ const normalizedNew = normalizeSecretValue(newValue);
+ return normalizedOld === normalizedNew;
+};
+
const isSingleLine = (str: string | null | undefined): boolean => {
if (!str || typeof str !== "string") return true;
return !str.includes("\n");
@@ -42,9 +62,14 @@ type WordDiff = {
};
// Simple line-by-line diff algorithm
+// Normalizes lines by trimming trailing whitespace before comparison
const computeLineDiff = (oldText: string, newText: string): DiffLine[] => {
- const oldLines = oldText.split("\n");
- const newLines = newText.split("\n");
+ // Normalize the texts before splitting
+ const normalizedOld = normalizeSecretValue(oldText);
+ const normalizedNew = normalizeSecretValue(newText);
+
+ const oldLines = normalizedOld.split("\n");
+ const newLines = normalizedNew.split("\n");
const result: DiffLine[] = [];
let oldIndex = 0;
@@ -63,63 +88,107 @@ const computeLineDiff = (oldText: string, newText: string): DiffLine[] => {
oldLine: oldLines[oldIndex]
});
oldIndex += 1;
- } else if (oldLines[oldIndex] === newLines[newIndex]) {
- result.push({
- type: DIFF_TYPE.UNCHANGED,
- oldLine: oldLines[oldIndex],
- newLine: newLines[newIndex]
- });
- oldIndex += 1;
- newIndex += 1;
- } else {
- const currentOldIndex = oldIndex;
- const currentNewIndex = newIndex;
- const currentOldLine = oldLines[currentOldIndex];
- const currentNewLine = newLines[currentNewIndex];
-
- const nextOldMatch = newLines.findIndex(
- (line, idx) => idx >= currentNewIndex && line === currentOldLine
- );
- const nextNewMatch = oldLines.findIndex(
- (line, idx) => idx >= currentOldIndex && line === currentNewLine
- );
-
- if (nextOldMatch === -1 && nextNewMatch === -1) {
- result.push({
- type: DIFF_TYPE.MODIFIED,
- oldLine: currentOldLine,
- newLine: currentNewLine
- });
- oldIndex += 1;
- newIndex += 1;
- } else if (nextOldMatch !== -1 && (nextNewMatch === -1 || nextOldMatch < nextNewMatch)) {
- while (newIndex < nextOldMatch) {
+ } else {
+ // Trim trailing whitespace from lines for comparison (but preserve leading whitespace)
+ const oldLineTrimmed = oldLines[oldIndex].replace(/\s+$/, "");
+ const newLineTrimmed = newLines[newIndex].replace(/\s+$/, "");
+
+ // If old line is empty/whitespace and new line has content, treat as ADDED
+ if (oldLineTrimmed === "" && newLineTrimmed !== "") {
result.push({
type: DIFF_TYPE.ADDED,
newLine: newLines[newIndex]
});
newIndex += 1;
+ // Don't increment oldIndex, keep it to check if there are more old lines
+ continue;
}
- } else {
- while (oldIndex < nextNewMatch) {
+
+ // If new line is empty/whitespace and old line has content, treat as DELETED
+ if (newLineTrimmed === "" && oldLineTrimmed !== "") {
result.push({
type: DIFF_TYPE.DELETED,
oldLine: oldLines[oldIndex]
});
oldIndex += 1;
+ // Don't increment newIndex, keep it to check if there are more new lines
+ continue;
+ }
+
+ if (oldLineTrimmed === newLineTrimmed) {
+ result.push({
+ type: DIFF_TYPE.UNCHANGED,
+ oldLine: oldLines[oldIndex],
+ newLine: newLines[newIndex]
+ });
+ oldIndex += 1;
+ newIndex += 1;
+ } else {
+ const currentOldIndex = oldIndex;
+ const currentNewIndex = newIndex;
+ const currentOldLine = oldLines[currentOldIndex];
+ const currentNewLine = newLines[currentNewIndex];
+
+ const nextOldMatch = newLines.findIndex(
+ (line, idx) => idx >= currentNewIndex && line.replace(/\s+$/, "") === currentOldLine.replace(/\s+$/, "")
+ );
+ const nextNewMatch = oldLines.findIndex(
+ (line, idx) => idx >= currentOldIndex && line.replace(/\s+$/, "") === currentNewLine.replace(/\s+$/, "")
+ );
+
+ if (nextOldMatch === -1 && nextNewMatch === -1) {
+ result.push({
+ type: DIFF_TYPE.MODIFIED,
+ oldLine: currentOldLine,
+ newLine: currentNewLine
+ });
+ oldIndex += 1;
+ newIndex += 1;
+ } else if (nextOldMatch !== -1 && (nextNewMatch === -1 || nextOldMatch < nextNewMatch)) {
+ while (newIndex < nextOldMatch) {
+ result.push({
+ type: DIFF_TYPE.ADDED,
+ newLine: newLines[newIndex]
+ });
+ newIndex += 1;
+ }
+ } else {
+ while (oldIndex < nextNewMatch) {
+ result.push({
+ type: DIFF_TYPE.DELETED,
+ oldLine: oldLines[oldIndex]
+ });
+ oldIndex += 1;
+ }
+ }
}
}
- }
}
return result;
};
// Word-level diff for modified lines
-const computeWordDiff = (oldText: string, newText: string): WordDiff[] => {
+// Normalizes texts before computing word diff to ignore whitespace-only changes
+// If there are no common words, returns null to indicate entire line should be highlighted
+const computeWordDiff = (oldText: string, newText: string): WordDiff[] | null => {
+ // Normalize the texts before computing word diff
+ const normalizedOld = normalizeSecretValue(oldText);
+ const normalizedNew = normalizeSecretValue(newText);
+
const wordRegex = /(\s+|[^\s\w]+|\w+)/g;
- const oldWords = oldText.match(wordRegex) || [];
- const newWords = newText.match(wordRegex) || [];
+ const oldWords = normalizedOld.match(wordRegex) || [];
+ const newWords = normalizedNew.match(wordRegex) || [];
+
+ // Check if there are any common words (excluding whitespace and punctuation)
+ const oldWordSet = new Set(oldWords.filter(w => /\w/.test(w)));
+ const newWordSet = new Set(newWords.filter(w => /\w/.test(w)));
+ const hasCommonWords = [...oldWordSet].some(word => newWordSet.has(word));
+
+ // If no common words, return null to indicate entire line should be highlighted
+ if (!hasCommonWords && oldWords.length > 0 && newWords.length > 0) {
+ return null;
+ }
const result: WordDiff[] = [];
let oldIdx = 0;
@@ -176,8 +245,26 @@ const renderSingleLineDiffForApproval = (
newText: string,
isOldVersion: boolean
): JSX.Element => {
+ const normalizedOld = normalizeSecretValue(oldText);
+ const normalizedNew = normalizeSecretValue(newText);
+
+ // If completely new or deleted, or no common words, show as single block
const wordDiffs = computeWordDiff(oldText, newText);
+ const shouldShowAsBlock =
+ (normalizedOld === "" && normalizedNew !== "") ||
+ (normalizedOld !== "" && normalizedNew === "") ||
+ wordDiffs === null;
+
+ if (shouldShowAsBlock) {
+ const value = isOldVersion ? normalizedOld : normalizedNew;
+ return (
+ <div className="font-mono text-sm break-words">
+ {value}
+ </div>
+ );
+ }
+ // Show word-by-word diff when there are common words
return (
<div className="font-mono text-sm break-words">
{wordDiffs.map((wordDiff, wordIdx) => {
@@ -237,6 +324,21 @@ const renderMultilineDiffForApproval = (
if (diffLine.type === DIFF_TYPE.MODIFIED && diffLine.oldLine) {
const wordDiffs = computeWordDiff(diffLine.oldLine, diffLine.newLine || "");
+ // If no common words, just show the line with line-level background (no extra inner highlight)
+ if (wordDiffs === null) {
+ return (
+ <div
+ key={lineKey}
+ className={lineClass}
+ data-first-change={isFirstChanged ? "true" : undefined}
+ >
+ <div className="w-4 shrink-0">-</div>
+ <div className="min-w-0 flex-1 break-words">
+ {diffLine.oldLine}
+ </div>
+ </div>
+ );
+ }
return (
<div
key={lineKey}
@@ -283,8 +385,37 @@ const renderMultilineDiffForApproval = (
? "flex min-w-full bg-green-500/50 rounded-xs text-green-300"
: "flex min-w-full";
+ // ADDED lines should always be shown as a single block (no word-by-word highlighting)
+ if (diffLine.type === DIFF_TYPE.ADDED && diffLine.newLine) {
+ return (
+ <div
+ key={lineKey}
+ className={lineClass}
+ data-first-change={isFirstChanged ? "true" : undefined}
+ >
+ <div className="w-4 shrink-0">+</div>
+ <div className="min-w-0 flex-1 break-words">{diffLine.newLine}</div>
+ </div>
+ );
+ }
+
if (diffLine.type === DIFF_TYPE.MODIFIED && diffLine.newLine) {
const wordDiffs = computeWordDiff(diffLine.oldLine || "", diffLine.newLine);
+ // If no common words, just show the line with line-level background (no extra inner highlight)
+ if (wordDiffs === null) {
+ return (
+ <div
+ key={lineKey}
+ className={lineClass}
+ data-first-change={isFirstChanged ? "true" : undefined}
+ >
+ <div className="w-4 shrink-0">+</div>
+ <div className="min-w-0 flex-1 break-words">
+ {diffLine.newLine}
+ </div>
+ </div>
+ );
+ }
return (
<div
key={lineKey}
@@ -480,7 +611,7 @@ export const SecretApprovalRequestChangeItem = ({
) : (() => {
const oldValue = secretVersion?.secretValue ?? "";
const newValue = newVersion?.secretValue ?? "";
- return oldValue !== newValue;
+ return !areValuesEqual(oldValue, newValue);
})() ? (
(() => {
const oldValue = secretVersion?.secretValue ?? "";
@@ -667,7 +798,7 @@ export const SecretApprovalRequestChangeItem = ({
) : (() => {
const oldValue = secretVersion?.secretValue ?? "";
const newValue = newVersion?.secretValue ?? secretVersion?.secretValue ?? "";
- return oldValue !== newValue;
+ return !areValuesEqual(oldValue, newValue);
})() ? (
(() => {
const oldValue = secretVersion?.secretValue ?? "";
diff --git a/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.tsx b/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.tsx
index d3172142ba..1e5af2c17c 100644
--- a/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.tsx
+++ b/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.tsx
@@ -110,7 +110,28 @@ const normalizeValue = (value: any): string | boolean | undefined => {
return value;
};
-const areValuesEqual = (value1: any, value2: any): boolean => {
+// Normalize secret value to match backend behavior:
+// - Trim whitespace
+// - If value ends with \n, preserve it (but trim everything else)
+// This matches the backend transform: (val.at(-1) === "\n" ? `${val.trim()}\n` : val.trim())
+const normalizeSecretValue = (value: string | null | undefined): string => {
+ if (!value || typeof value !== "string") return "";
+ // If value ends with \n, preserve it after trimming
+ if (value.at(-1) === "\n") {
+ return `${value.trim()}\n`;
+ }
+ return value.trim();
+};
+
+const areValuesEqual = (value1: any, value2: any, isSecretValue: boolean = false): boolean => {
+ // For secret values, use the backend normalization
+ if (isSecretValue) {
+ const normalized1 = normalizeSecretValue(value1);
+ const normalized2 = normalizeSecretValue(value2);
+ return normalized1 === normalized2;
+ }
+
+ // For other values, use the original normalization
const normalized1 = normalizeValue(value1);
const normalized2 = normalizeValue(value2);
@@ -139,7 +160,7 @@ const cleanupRevertedSecretFields = (update: PendingSecretUpdate): PendingSecret
if (
cleaned.secretValue !== undefined &&
- !areValuesEqual(cleaned.secretValue, cleaned.originalValue)
+ !areValuesEqual(cleaned.secretValue, cleaned.originalValue, true)
) {
hasChanges = true;
} else {
diff --git a/frontend/src/pages/secret-manager/SecretDashboardPage/components/CommitForm/CommitForm.tsx b/frontend/src/pages/secret-manager/SecretDashboardPage/components/CommitForm/CommitForm.tsx
index 20ad815acd..84a421d0f9 100644
--- a/frontend/src/pages/secret-manager/SecretDashboardPage/components/CommitForm/CommitForm.tsx
+++ b/frontend/src/pages/secret-manager/SecretDashboardPage/components/CommitForm/CommitForm.tsx
@@ -18,6 +18,26 @@ import {
useBatchModeActions
} from "../../SecretMainPage.store";
+// Normalize secret value to match backend behavior:
+// - Trim whitespace
+// - If value ends with \n, preserve it (but trim everything else)
+// This matches the backend transform: (val.at(-1) === "\n" ? `${val.trim()}\n` : val.trim())
+const normalizeSecretValue = (value: string | null | undefined): string => {
+ if (!value || typeof value !== "string") return "";
+ // If value ends with \n, preserve it after trimming
+ if (value.at(-1) === "\n") {
+ return `${value.trim()}\n`;
+ }
+ return value.trim();
+};
+
+// Compare normalized values to determine if they're actually different
+const areValuesEqual = (oldValue: string | null | undefined, newValue: string | null | undefined): boolean => {
+ const normalizedOld = normalizeSecretValue(oldValue);
+ const normalizedNew = normalizeSecretValue(newValue);
+ return normalizedOld === normalizedNew;
+};
+
interface CommitFormProps {
onCommit: (changes: PendingChanges, commitMessage: string) => Promise<void>;
isCommitting?: boolean;
@@ -70,7 +90,7 @@ const RenderSecretChanges = ({ onDiscard, change }: RenderResourceProps) => {
const { existingSecret } = change;
const hasKeyChange = change.newSecretName && change.secretKey !== change.newSecretName;
- const hasValueChange = change.secretValue !== change.originalValue;
+ const hasValueChange = !areValuesEqual(change.secretValue, change.originalValue);
const hasCommentChange = change.secretComment !== change.originalComment;
const hasMultilineChange =
change.skipMultilineEncoding !== change.originalSkipMultilineEncoding;
diff --git a/frontend/src/pages/secret-manager/SecretDashboardPage/components/SecretListView/SecretListView.tsx b/frontend/src/pages/secret-manager/SecretDashboardPage/components/SecretListView/SecretListView.tsx
index dfcaecc226..b9e75948d5 100644
--- a/frontend/src/pages/secret-manager/SecretDashboardPage/components/SecretListView/SecretListView.tsx
+++ b/frontend/src/pages/secret-manager/SecretDashboardPage/components/SecretListView/SecretListView.tsx
@@ -279,19 +279,25 @@ export const SecretListView = ({
(newId) => !orgSecret.secretReminderRecipients?.find((oldId) => newId === oldId.user.id)
) && reminderRecipients?.length === orgSecret.secretReminderRecipients?.length;
+ // Normalize secret value to match backend behavior for comparison
+ const normalizeSecretValue = (value: string | null | undefined): string => {
+ if (!value || typeof value !== "string") return "";
+ // If value ends with \n, preserve it after trimming
+ if (value.at(-1) === "\n") {
+ return `${value.trim()}\n`;
+ }
+ return value.trim();
+ };
+
const isSharedSecUnchanged =
- (
- [
- "key",
- "value",
- "comment",
- "skipMultilineEncoding",
- "reminderRepeatDays",
- "reminderNote",
- "reminderRecipients",
- "secretMetadata"
- ] as const
- ).every((el) => orgSecret[el] === modSecret[el]) &&
+ orgSecret.key === modSecret.key &&
+ normalizeSecretValue(orgSecret.value) === normalizeSecretValue(modSecret.value) &&
+ orgSecret.comment === modSecret.comment &&
+ orgSecret.skipMultilineEncoding === modSecret.skipMultilineEncoding &&
+ orgSecret.reminderRepeatDays === modSecret.reminderRepeatDays &&
+ orgSecret.reminderNote === modSecret.reminderNote &&
+ orgSecret.reminderRecipients === modSecret.reminderRecipients &&
+ JSON.stringify(orgSecret.secretMetadata) === JSON.stringify(modSecret.secretMetadata) &&
isSameTags &&
isSameRecipients;