Repository: pulp/pulpcore

Issue:
Special characters in the directory listing regex should be escaped
Steps to reproduce:

```
http://localhost:5001/pulp/content/43036ebe-5d0e-413c-881c-42c576b067b2/deltas/KK/LCzQ9nUs0sVU9+94E2ek3Dyc+ToKjMpe1pNqMthcg-CpWGufXYjIIlkgTANq_eDb9yzAgANtrjdjgp+Rw8UcU/
```

```
[2023-03-25 22:44:06 +0000] [5590] [ERROR] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib64/python3.8/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
    resp = await request_handler(request)
  File "/usr/local/lib64/python3.8/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
  File "/usr/local/lib64/python3.8/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
  File "/src/pulpcore/pulpcore/content/authentication.py", line 48, in authenticate
    return await handler(request)
  File "/src/pulpcore/pulpcore/content/handler.py", line 245, in stream_content
    return await self._match_and_stream(path, request)
  File "/src/pulpcore/pulpcore/content/handler.py", line 625, in _match_and_stream
    dir_list, dates = await self.list_directory(repo_version, None, rel_path)
  File "/src/pulpcore/pulpcore/content/handler.py", line 458, in list_directory
    return await sync_to_async(list_directory_blocking)()
  File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 448, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "/usr/lib64/python3.8/asyncio/tasks.py", line 455, in wait_for
    return await fut
  File "/usr/lib64/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 490, in thread_handler
    return func(*args, **kwargs)
  File "/src/pulpcore/pulpcore/content/handler.py", line 449, in list_directory_blocking
    name = file_or_directory_name(path, ca.relative_path)
  File "/src/pulpcore/pulpcore/content/handler.py", line 416, in file_or_directory_name
    return "{}{}".format(result.groups()[1], result.groups()[2])
AttributeError: 'NoneType' object has no attribute 'groups'

```

Affected code:
https://github.com/pulp/pulpcore/blob/3879ae14e20947d99029d77baab1b09414ded6b7/pulpcore/content/handler.py#L415

pulp_ostree might expose directories which contain special characters, like this:
```
deltas/KK/LCzQ9nUs0sVU9+94E2ek3Dyc+ToKjMpe1pNqMthcg-CpWGufXYjIIlkgTANq_eDb9yzAgANtrjdjgp+Rw8UcU/
```

Resolution:
We should use something like `re.escape(directory_path))` to handle this scenario.


Discussion:

Resolving patch:
diff --git a/pulpcore/content/handler.py b/pulpcore/content/handler.py
--- a/pulpcore/content/handler.py
+++ b/pulpcore/content/handler.py
@@ -412,7 +412,7 @@ async def list_directory(self, repo_version, publication, path):
         """
 
         def file_or_directory_name(directory_path, relative_path):
-            result = re.match(r"({})([^\/]*)(\/*)".format(directory_path), relative_path)
+            result = re.match(r"({})([^\/]*)(\/*)".format(re.escape(directory_path)), relative_path)
             return "{}{}".format(result.groups()[1], result.groups()[2])
 
         def list_directory_blocking():


Test patch: