Skip to content

html search: add safety check before index property accesses #13153

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

Merged
merged 8 commits into from
Jan 2, 2025
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Bugs fixed
* LaTeX: fix a ``7.4.0`` typo in a default for ``\sphinxboxsetup``
(refs: PR #13152).
Patch by Jean-François B.
* #13096: HTML Search: check that query terms exist as properties in
term indices before accessing them.

Testing
-------
6 changes: 4 additions & 2 deletions sphinx/themes/basic/static/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,11 @@ const Search = {
// perform the search on the required terms
searchTerms.forEach((word) => {
const files = [];
// find documents, if any, containing the query word in their text/title term indices
// use Object.hasOwnProperty to avoid mismatching against prototype properties
const arr = [
{ files: terms[word], score: Scorer.term },
{ files: titleTerms[word], score: Scorer.title },
{ files: terms.hasOwnProperty(word) ? terms[word] : undefined, score: Scorer.term },
{ files: titleTerms.hasOwnProperty(word) ? titleTerms[word] : undefined, score: Scorer.title },
];
// add support for partial matches
if (word.length > 2) {
Expand Down
13 changes: 13 additions & 0 deletions tests/js/searchtools.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ describe('Basic html theme search', function() {

});

describe('can handle edge-case search queries', function() {

it('does not find the javascript prototype property in unrelated documents', function() {
eval(loadFixture("partial/searchindex.js"));

searchParameters = Search._parseQuery('__proto__');

hits = [];
expect(Search._performSearch(...searchParameters)).toEqual(hits);
});

});

});

describe("htmlToText", function() {
Expand Down
Loading