diff --git a/tests/routes/test_cog.py b/tests/routes/test_cog.py index 6e8d0ea49..2f70e9847 100644 --- a/tests/routes/test_cog.py +++ b/tests/routes/test_cog.py @@ -240,6 +240,18 @@ def test_tile(rio, app): assert response.status_code == 200 assert response.headers["content-type"] == "image/png" + cmap = urlencode({"colormap": json.dumps({"1": [58, 102]})}) + response = app.get( + f"/cog/tiles/8/53/50.png?url=https://myurl.com/above_cog.tif&bidx=1&{cmap}" + ) + assert response.status_code == 400 + + cmap = urlencode({"colormap": {"1": "#ddcb9aFF"}}) + response = app.get( + f"/cog/tiles/8/53/50.png?url=https://myurl.com/above_cog.tif&bidx=1&{cmap}" + ) + assert response.status_code == 400 + response = app.get( "/cog/tiles/8/53/50.png?url=https://myurl.com/above_cog.tif&bidx=1&colormap_name=above&resampling_method=somethingwrong" ) diff --git a/titiler/dependencies.py b/titiler/dependencies.py index a54efff42..89526d29f 100644 --- a/titiler/dependencies.py +++ b/titiler/dependencies.py @@ -17,7 +17,7 @@ from .custom import tms as custom_tms from .utils import get_hash -from fastapi import Query +from fastapi import HTTPException, Query from starlette.requests import Request @@ -78,10 +78,15 @@ def ColorMapParams( return cmap.get(colormap_name.value) if colormap: - return json.loads( - colormap, - object_hook=lambda x: {int(k): parse_color(v) for k, v in x.items()}, - ) + try: + return json.loads( + colormap, + object_hook=lambda x: {int(k): parse_color(v) for k, v in x.items()}, + ) + except json.JSONDecodeError: + raise HTTPException( + status_code=400, detail="Could not parse the colormap value." + ) return None diff --git a/titiler/errors.py b/titiler/errors.py index 9375319ac..e03e1f406 100644 --- a/titiler/errors.py +++ b/titiler/errors.py @@ -14,6 +14,7 @@ EmptyMosaicError, InvalidAssetName, InvalidBandName, + InvalidColorFormat, MissingAssets, MissingBands, RioTilerError, @@ -52,6 +53,7 @@ class BadRequestError(TilerError): RasterioIOError: status.HTTP_404_NOT_FOUND, MissingBands: status.HTTP_400_BAD_REQUEST, MissingAssets: status.HTTP_400_BAD_REQUEST, + InvalidColorFormat: status.HTTP_400_BAD_REQUEST, InvalidAssetName: status.HTTP_404_NOT_FOUND, InvalidBandName: status.HTTP_404_NOT_FOUND, MosaicError: status.HTTP_424_FAILED_DEPENDENCY,