Skip to content

Commit

Permalink
fix: LowerCaseQueryStringMiddleware should not truncate query param…
Browse files Browse the repository at this point in the history
…eters (developmentseed#677)

* fix: Lower case string middleware fails on pre signed url

* test for lower case middleware query string stripping fix

* note middleware fix in changelog

---------

Co-authored-by: Jack Arthur Harrhy <me@jackharrhy.com>
  • Loading branch information
jthetzel and jackharrhy authored Aug 21, 2023
1 parent dc9e0c7 commit 5023044
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## [unreleased]

- fix `LowerCaseQueryStringMiddleware` unexpectedly truncating query parameters (https://github.com/developmentseed/titiler/pull/677)

## 0.13.0 (2023-07-27)

* update core requirements to libraries using pydantic **~=2.0**
Expand Down
23 changes: 23 additions & 0 deletions src/titiler/core/tests/test_case_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,26 @@ async def route1(value: Annotated[List[str], Query()]):

response = client.get("/route1?VALUE=lorenzori&VALUE=dogs&value=trucks")
assert response.json() == {"value": ["lorenzori", "dogs", "trucks"]}


def test_lowercase_middleware_url_with_query_parameters():
"""Make sure all query parameters return."""
app = FastAPI()

@app.get("/route1")
async def route1(url: List[str] = Query(...)):
"""route1."""
return {"url": url}

app.add_middleware(LowerCaseQueryStringMiddleware)

client = TestClient(app)

url = "https://developmentseed.org?solutions=geospatial&planet=better"
url_encoded = (
"https%3A%2F%2Fdevelopmentseed.org%3Fsolutions%3Dgeospatial%26planet%3Dbetter"
)

response = client.get(f"/route1?url={url_encoded}")

assert response.json() == {"url": [url]}
3 changes: 2 additions & 1 deletion src/titiler/core/titiler/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import re
import time
import urllib.parse
from typing import Optional, Set

from fastapi.logger import logger
Expand Down Expand Up @@ -160,7 +161,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):

query_string = ""
for k, v in request.query_params.multi_items():
query_string += k.lower() + "=" + v + "&"
query_string += k.lower() + "=" + urllib.parse.quote(v) + "&"

query_string = query_string[:-1]
request.scope["query_string"] = query_string.encode(DECODE_FORMAT)
Expand Down

0 comments on commit 5023044

Please sign in to comment.