From f57397f92dea3e938e6920505da96a08fc6979eb Mon Sep 17 00:00:00 2001 From: Christian Czezatke Date: Wed, 13 Jan 2021 13:48:14 -0800 Subject: [PATCH 1/2] Prevent double-encoding of path components in queries. --- aioelasticsearch/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aioelasticsearch/connection.py b/aioelasticsearch/connection.py index 570ba280..2111ac34 100644 --- a/aioelasticsearch/connection.py +++ b/aioelasticsearch/connection.py @@ -109,7 +109,7 @@ async def perform_request( ): url_path = url - url = (self.base_url / url.lstrip('/')).with_query(params) + url = self.base_url.join(URL.build(path=url.lstrip('/'), encoded=True)).with_query(params) start = self.loop.time() try: From 555b4fbb9cdb553cadf24041c5e2124b18118d4a Mon Sep 17 00:00:00 2001 From: Christian Czezatke Date: Fri, 15 Jan 2021 13:46:21 -0800 Subject: [PATCH 2/2] Added unit test. --- tests/test_connection.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_connection.py b/tests/test_connection.py index 232f5269..c576ae01 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -1,10 +1,12 @@ import asyncio import ssl from unittest import mock +from urllib.parse import unquote import aiohttp import pytest from elasticsearch import ConnectionTimeout +from elasticsearch.client.utils import _make_path from aioelasticsearch.connection import (AIOHttpConnection, ConnectionError, SSLError) @@ -114,3 +116,23 @@ async def coro(*args, **Kwargs): use_ssl=True)) with pytest.raises(expected): await conn.perform_request('HEAD', '/') + + +@pytest.mark.run_loop +async def test_path_encoding(loop): + class StopProcessing(Exception): + pass + + class DummyClientSession(object): + def request(self, method, url, **kwargs): + raise StopProcessing(unquote(url.path)) + + conn = AIOHttpConnection(session=DummyClientSession(), loop=loop) + + for id in ("123abc", "123+abc", "123%4"): + path = _make_path("index", "_doc", id) + + with pytest.raises(StopProcessing) as sp: + await conn.perform_request("GET", path) + + assert id == str(sp.value).split("/")[-1]