[Security Solution] Fix incorrect changes highlighting in diff view #205138
+77
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves: #202016
Summary
This PR resolves an issue where the diff view incorrectly marked certain characters as changed (using bold font) in some cases.
Root Cause
The issue arises from a bug in the
diff
library (v5). The library is used to compute two-way diffs between strings (old field value and new field value), producing an array of change objects that is then used for rendering.Conditions for the bug:
diff
v5 library is in use (fixed in v6 and above) andDiffMethod.WORDS
is passed as a parameter to it.For example, for these input strings:
A more real-life example
Solution
Switching to
DiffMethod.WORDS_WITH_SPACE
avoids this issue.Screenshot showing the difference between
DiffMethod.WORDS
andDiffMethod.WORDS_WITH_SPACE
:Other changes
DiffMethod.TRIMMED_LINES
since it's now deprecated in thediff
library and we are not using it anyways.Screenshots: with and without "zip" (click to expand)
With the "zip" option (how it was before)No "zip" (this branch)
Testing
I thoroughly tested with
DiffMethod.WORDS_WITH_SPACE
across various inputs and scenarios, including:I also validated diffs against real prebuilt rules by installing an older Fleet package version and observed no issues.
You can test by trying different input strings and settings in Storybook.
Run Storybook:
yarn storybook security_solution
.storybook.mov
You can notice that
ComparisonSide
stories are broken, but that's unrelated to these changes and needs to be handled separately.Compatibility with future upgrades
There's an open PR that will upgrade the
diff
library from v5 to v7. I verified the behavior ofDiffMethod.WORDS_WITH_SPACE
on v7 and found no differences compared to v5, so it should be safe to upgrade to v7 without any changes on our end.Work started on 23-Dec-2024.