-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Security Solution] Handle invalid savedSearchId #182937
Merged
logeekal
merged 2 commits into
elastic:main
from
logeekal:fix/timeline_json_import_saved_search_id
May 9, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import { isEqualWith } from 'lodash'; | |
import type { SavedSearch } from '@kbn/saved-search-plugin/common'; | ||
import type { TimeRange } from '@kbn/es-query'; | ||
import { useDispatch } from 'react-redux'; | ||
import { updateSavedSearchId } from '../../../../store/actions'; | ||
import { useDiscoverInTimelineContext } from '../../../../../common/components/discover_in_timeline/use_discover_in_timeline_context'; | ||
import { useSourcererDataView } from '../../../../../common/containers/sourcerer'; | ||
import { useKibana } from '../../../../../common/lib/kibana'; | ||
|
@@ -89,7 +90,11 @@ export const DiscoverTabContent: FC<DiscoverTabContentProps> = ({ timelineId }) | |
); | ||
const { status, savedSearchId, activeTab, savedObjectId, title, description } = timeline; | ||
|
||
const { data: savedSearchById, isFetching } = useQuery({ | ||
const { | ||
data: savedSearchById, | ||
isFetching, | ||
status: savedSearchByIdStatus, | ||
} = useQuery({ | ||
queryKey: ['savedSearchById', savedSearchId ?? ''], | ||
queryFn: () => (savedSearchId ? savedSearchService.get(savedSearchId) : Promise.resolve(null)), | ||
}); | ||
|
@@ -117,6 +122,12 @@ export const DiscoverTabContent: FC<DiscoverTabContentProps> = ({ timelineId }) | |
|
||
useEffect(() => { | ||
if (isFetching) return; | ||
if (savedSearchByIdStatus === 'error' && savedSearchId) { | ||
// when a timeline json is uploaded with a saved search Id that not longer | ||
// exists, we need to reset the saved search Id in the timeline and remove th saved search | ||
dispatch(updateSavedSearchId({ id: timelineId, savedSearchId: null })); | ||
return; | ||
} | ||
if (!savedObjectId) return; | ||
if (!status || status === 'draft') return; | ||
const latestState = getCombinedDiscoverSavedSearchState(); | ||
|
@@ -126,8 +137,9 @@ export const DiscoverTabContent: FC<DiscoverTabContentProps> = ({ timelineId }) | |
if (!index) return; | ||
if (!latestState || combinedDiscoverSavedSearchStateRef.current === latestState) return; | ||
if (isEqualWith(latestState, savedSearchById, savedSearchComparator)) return; | ||
updateSavedSearch(latestState, timelineId); | ||
combinedDiscoverSavedSearchStateRef.current = latestState; | ||
updateSavedSearch(latestState, timelineId, function onUpdate() { | ||
combinedDiscoverSavedSearchStateRef.current = latestState; | ||
}); | ||
}, [ | ||
getCombinedDiscoverSavedSearchState, | ||
savedSearchById, | ||
|
@@ -139,6 +151,8 @@ export const DiscoverTabContent: FC<DiscoverTabContentProps> = ({ timelineId }) | |
isFetching, | ||
timelineId, | ||
dispatch, | ||
savedSearchId, | ||
savedSearchByIdStatus, | ||
]); | ||
|
||
useEffect(() => { | ||
|
@@ -166,9 +180,14 @@ export const DiscoverTabContent: FC<DiscoverTabContentProps> = ({ timelineId }) | |
setDiscoverStateContainer(stateContainer); | ||
let savedSearchAppState; | ||
if (savedSearchId) { | ||
const localSavedSearch = await savedSearchService.get(savedSearchId); | ||
initializeLocalSavedSearch(localSavedSearch, timelineId); | ||
savedSearchAppState = getAppStateFromSavedSearch(localSavedSearch); | ||
try { | ||
const localSavedSearch = await savedSearchService.get(savedSearchId); | ||
initializeLocalSavedSearch(localSavedSearch, timelineId); | ||
savedSearchAppState = getAppStateFromSavedSearch(localSavedSearch); | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.error('Stale Saved search Id which no longer exists', e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to keep this log in prod? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep.. I was thinking the same and decided to keep it. |
||
} | ||
} | ||
|
||
const finalAppState = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove the saved search?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.. will try to do it... In follow up PR.