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

Escape special characters in the XML generated in the delete() method #356

Closed
Closed
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
6 changes: 4 additions & 2 deletions pysolr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
import time
from xml.etree import ElementTree
import xml.sax.saxutils

import requests
from pkg_resources import DistributionNotFound, get_distribution, parse_version
Expand Down Expand Up @@ -1119,11 +1120,12 @@ def delete(
else:
doc_id = list(filter(None, id))
if doc_id:
m = "<delete>%s</delete>" % "".join("<id>%s</id>" % i for i in doc_id)
m = "<delete>%s</delete>" % "".join("<id>%s</id>" % xml.sax.saxutils.escape(i) for i in doc_id)
else:
raise ValueError("The list of documents to delete was empty.")
elif q is not None:
m = "<delete><query>%s</query></delete>" % q
xml_escaped_q = xml.sax.saxutils.escape(q)
m = "<delete><query>%s</query></delete>" % xml_escaped_q

return self._update(
m,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,14 @@ def test_delete(self):
self.assertEqual(len(self.solr.search("type_s:grandchild")), 1)
self.solr.delete(q="price:[0 TO 15]")
self.solr.delete(q="type_s:parent", commit=True)

# Test a query that would need to be quoted when using the XML API.
# These will delete too much when using v3.9.0 or earlier.
self.solr.delete(q='id:*</query><query> id:999 AND id:9999')
self.solr.delete(id='doc_4</id><id>doc_3</id><id>doc_2</id><id>doc_1',commit=True)
# Ids with a "<" character will give an error using v3.9.0 or earlier
self.solr.delete(id='cats<dogs')

# one simple doc should remain
# parent documents were also deleted but children remain as orphans
self.assertEqual(len(self.solr.search("doc")), 1)
Expand Down