Skip to content

Commit

Permalink
remove contextualSearch facetFilters merging security
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Nov 23, 2020
1 parent 9e314bc commit 9931a40
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,4 @@ describe('validateThemeConfig', () => {
},
});
});

test('contextualSearch + searchParameters.facetFilters config', () => {
const algolia = {
indexName: 'index',
apiKey: 'apiKey',
contextualSearch: true,
searchParameters: {
facetFilters: ['version:1.0'],
},
};
expect(() =>
testValidateThemeConfig({algolia}),
).toThrowErrorMatchingInlineSnapshot(
`"If you are using algolia.contextualSearch: true, you should not provide algolia.searchParameters.facetFilters, as it is computed for you dynamically"`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Link from '@docusaurus/Link';
import Head from '@docusaurus/Head';
import useSearchQuery from '@theme/hooks/useSearchQuery';
import {DocSearchButton, useDocSearchKeyboardEvents} from '@docsearch/react';
import {useAlgoliaContextualSearchParameters} from '../../utils/algoliaSearchUtils';
import useAlgoliaContextualFacetFilters from '@theme/hooks/useAlgoliaContextualFacetFilters';

let DocSearchModal = null;

Expand All @@ -35,12 +35,20 @@ function ResultsFooter({state, onClose}) {
function DocSearch({contextualSearch, ...props}) {
const {siteMetadata} = useDocusaurusContext();

const contextualSearchParameters = useAlgoliaContextualSearchParameters();
const contextualSearchFacetFilters = useAlgoliaContextualFacetFilters();

const configFacetFilters = props.searchParameters?.facetFilters ?? [];

const facetFilters = contextualSearch
? // Merge contextual search filters with config filters
[...contextualSearchFacetFilters, ...configFacetFilters]
: // ... or use config facetFilters
configFacetFilters;

// we let user override default searchParameters if he wants to
const searchParameters = {
...(contextualSearch ? contextualSearchParameters : {}),
...props.searchParameters,
facetFilters,
};

const {withBaseUrl} = useBaseUrlUtils();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import useContextualSearchFilters from '@theme/hooks/useContextualSearchFilters';

// Translate search-engine agnostic seach filters to Algolia search filters
export function useAlgoliaContextualSearchParameters() {
// Translate search-engine agnostic search filters to Algolia search filters
export default function useAlgoliaContextualFacetFilters() {
const {language, tags} = useContextualSearchFilters();

const languageFilter = `language:${language}`;

const tagsFilter = tags.map((tag) => `docusaurus_tag:${tag}`);

return {facetFilters: [languageFilter, tagsFilter]};
return [languageFilter, tagsFilter];
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,5 @@ exports.validateThemeConfig = function validateThemeConfig({
validate,
themeConfig,
}) {
const normalizedThemeConfig = validate(Schema, themeConfig);

if (
normalizedThemeConfig &&
normalizedThemeConfig.algolia.contextualSearch &&
normalizedThemeConfig.algolia.searchParameters &&
normalizedThemeConfig.algolia.searchParameters.facetFilters
) {
throw new Error(
'If you are using algolia.contextualSearch: true, you should not provide algolia.searchParameters.facetFilters, as it is computed for you dynamically',
);
}
return normalizedThemeConfig;
return validate(Schema, themeConfig);
};

0 comments on commit 9931a40

Please sign in to comment.