Skip to content
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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const useDiscoverInTimelineActions = (

const queryClient = useQueryClient();

const { mutateAsync: saveSavedSearch, status } = useMutation({
const { mutateAsync: saveSavedSearch, status: saveSavedSearchStatus } = useMutation({
mutationFn: ({
savedSearch,
savedSearchOptions,
Expand Down Expand Up @@ -189,7 +189,7 @@ export const useDiscoverInTimelineActions = (
*
* */
const updateSavedSearch = useCallback(
async (savedSearch: SavedSearch, timelineId: string) => {
async (savedSearch: SavedSearch, timelineId: string, onUpdate?: () => void) => {
savedSearch.timeRestore = true;
savedSearch.timeRange =
savedSearch.timeRange ?? discoverDataService.query.timefilter.timefilter.getTime();
Expand Down Expand Up @@ -219,7 +219,7 @@ export const useDiscoverInTimelineActions = (
// If no saved search exists. Create a new saved search instance and associate it with the timeline.
try {
// Make sure we're not creating a saved search while a previous creation call is in progress
if (status !== 'idle') {
if (saveSavedSearchStatus === 'loading') {
return;
}
dispatch(
Expand All @@ -244,6 +244,7 @@ export const useDiscoverInTimelineActions = (
);
// Also save the timeline, this will only happen once, in case there is no saved search id yet
dispatch(timelineActions.saveTimeline({ id: TimelineId.active, saveAsNew: false }));
onUpdate?.();
}
} catch (err) {
dispatch(
Expand All @@ -254,7 +255,7 @@ export const useDiscoverInTimelineActions = (
}
}
},
[persistSavedSearch, savedSearchId, dispatch, discoverDataService, status]
[persistSavedSearch, savedSearchId, dispatch, discoverDataService, saveSavedSearchStatus]
);

const initializeLocalSavedSearch = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)),
});
Expand Down Expand Up @@ -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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

dispatch(updateSavedSearchId({ id: timelineId, savedSearchId: null }));
return;
}
if (!savedObjectId) return;
if (!status || status === 'draft') return;
const latestState = getCombinedDiscoverSavedSearchState();
Expand All @@ -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,
Expand All @@ -139,6 +151,8 @@ export const DiscoverTabContent: FC<DiscoverTabContentProps> = ({ timelineId })
isFetching,
timelineId,
dispatch,
savedSearchId,
savedSearchByIdStatus,
]);

useEffect(() => {
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to keep this log in prod?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export const updateTotalCount = actionCreator<{ id: string; totalCount: number }

export const updateSavedSearchId = actionCreator<{
id: string;
savedSearchId: string;
savedSearchId: string | null;
}>('UPDATE_DISCOVER_SAVED_SEARCH_ID');

export const initializeSavedSearch = actionCreator<{
Expand Down